var whitespace = " \t\n\r";
var iAlphabetic = "This field must contain only letters. Please reenter it now."

function isEmpty(s)
{   
	s = trim(s);
	return ((s == null) || (s.length == 0) || (s.charAt(0) == " "));

}


function trim(strText)
{
  while(strText.substring(0,1) == ' ')
   strText = strText.substring(1,strText.length);

  while(strText.substring(strText.length-1,strText.length) == ' ')
   strText = strText.substring(0,strText.length-1);

  return strText;
}

function isWhitespace (s)

{   var i;

    if (isEmpty(s)) return true;

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    return true;
}

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}


function isAlphanumeric (s)

{   var i;

    if (isEmpty(s))
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }

    return true;
}

function isAlphabetic (s)

{   var i;

    if (isEmpty(s))
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);


    for (i = 0; i < s.length; i++)
    {
        // Check that current character is letter.
        var c = s.charAt(i);

        if (!isLetter(c))
        return false;
    }

    // All characters are letters.
    return true;
}

function checkstring(thisele) {
var elementlength = 0
var thiselement = ""
thiselement = thisele.value;
thiselement = thiselement.replace(" ", "");
elementlength = thiselement.length;
if (elementlength == 0) {
	alert ("Please fill in the \"" + thisele.validatorname + "\".");
	thisele.focus();
	return false;
 }
return true;
}


//------------------------------------------------------------------------------------------
// validate a telephone number
//------------------------------------------------------------------------------------------
function validateTELEPHONE(fieldVal)
{
// var pocMASK = /^(\d{3}-)?(\d{3})-(\d{4})(\d{5})?$/
 
 var pocMASK = /^(\d{3})-(\d{3})-(\d{4})$/
 
 var matchArray = fieldVal.match(pocMASK)
 
 if (matchArray==null)
 {
  alert("Not a valid telephone. Use the 999-999-9999 format.");
  bValidData = false;
  return false;
 }
 bValidData = true;
 return true;
}


function isChecked(elename) {
	var chkSelect=0  
	for (i=0;i < document.forms[0].elements.length;i++){
		if (document.forms[0].elements[i].name == elename) {
			if (document.forms[0].elements[i].checked == true) {
				chkSelect=chkSelect+1
			return (true);
			}
		}
	}	
	if (chkSelect == 0){
	  return (false);				
	  }	
}


//------------------------------------------------------------------------------------------
// validate a zip code
//------------------------------------------------------------------------------------------
function validatePOC(fieldVal)
{
// var pocMASK = /^(\d{5})(-(\d{4}))?$/
// var pocMASK = /^(\d{5})$/
// var matchArray = fieldVal.match(pocMASK)
// if (matchArray==null)
// {
//  alert("Not a valid postal code. Use 99999 format")
//  bValidData = false;
//  return false;
// }
//  bValidData = true;
// return true;

   var blnResult = true;
   var strValidChars = "0123456789-"
   for (i=0;i<fieldVal.length && blnResult == true;i++)
    {
     strchar = fieldVal.charAt(i);
     if (strValidChars.indexOf(strchar) == -1)
	{ blnResult = false;bValidData = false;}
    }
   if(! blnResult)
	{ alert("Not a valid postal code. Use 99999 format");bValidData = false;}
   else
	bValidData = true;
   return blnResult;	
}


//-----------------------------------------
// Validates a date field.
//-----------------------------------------
function validate_date(docobj) {
    var dateStr=docobj.value 
	var datePat = /^(\d{2})(\/|-)(\d{2})\2(\d{4})$/;
	var matchArray = dateStr.match(datePat); 
	if (matchArray == null) {
	alert("Date entered is not in valid MM/DD/YYYY format.")
	return false;
	}
	month = matchArray[1]; 
	day = matchArray[3];
	year = matchArray[4];
	if (month < 1 || month > 12) { 
		alert("Month must be between 1 and 12.");
		docobj.focus();
	return false;
	}
	if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
	    docobj.focus();
	return false;
	}
	//check for 30 days in certain months
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn't have 31 days!")
	    docobj.focus();
	return false
	}
	// check for february 29th
	if (month == 2) { 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			alert("February " + year + " doesn't have " + day + " days!");
	    docobj.focus();
		return false;
		}
	}  

return (true);
}

function isValidDate(date2check){
  // determines if the date string passed represents a valid date.
  // returns 0 if the date is valid
  // returns 1 if the date is not in the format of mm/dd/ccyy, mm-dd-ccyy, or mmddccyy
  //      m/d/ccyy & m-d-ccyy are also acceptable
  // returns 2 if the date is not a legal date (i.e. 02/30/1999)
  var retval = 0
  var aMMDDCCYY
  var dtest
  // use a regular expression pattern match to determine if the date format is valid
  if (/^(\d\d?-\d\d?-\d{4})|(\d\d?\/\d\d?\/\d{4})|(\d{8})$/.test(date2check)){
    dtest = new Date(date2check);
    if (/\//.test(date2check)){
      aMMDDCCYY = date2check.split("/");
    }else{
      if (/-/.test(date2check)){
        aMMDDCCYY = date2check.split("-");
      }else{
        aMMDDCCYY = Array(date2check.substr(0,2), date2check.substr(2,2), date2check.substr(4,4))
        dtest = new Date(aMMDDCCYY[0] + "/" + aMMDDCCYY[1] + "/" + aMMDDCCYY[2]);
      }
    }
    if (dtest.getMonth() + 1 != aMMDDCCYY[0] || dtest.getDate() != aMMDDCCYY[1] || dtest.getFullYear() != aMMDDCCYY[2]){
      retval = 2
    }
  }else{
    retval = 1
  }
  return retval
}


function StdDateFormat(strDate){
  // receives a date string in the format of mm/dd/ccyy, mm-dd-ccyy, or mmddccyy
  // returns a date string in the standard format: mm/dd/ccyy.
  var retval = ""
  var aMMDDCCYY
  if (isValidDate(strDate) == 0){
     if (/\//.test(strDate)){
             retval = strDate;
     }else{
        if (/-/.test(strDate)){
           aMMDDCCYY = strDate.split("-");
        }else{
           aMMDDCCYY = Array(strDate.substr(0,2), strDate.substr(2,2), strDate.substr(4,4));
        }
        retval = (aMMDDCCYY[0] + "/" + aMMDDCCYY[1] + "/" + aMMDDCCYY[2]);
     }
  }
  return retval;
}


function VerifyEmail(email) {
// VerifyEmail() verifies that the email address has the correct format
var at_pos;
var pd_pos;
var sp_pos;
var em_len;
	// Make sure there is a value
	em_len = email.length;
	if (em_len < 1) {
		return -1;
	}
	// Check for spaces
	sp_pos = email.indexOf(" ", 0);
	if (sp_pos != -1) {
		return -2;
	 }
	// Check for an @ symbol
	at_pos = email.indexOf("@", 0);
	if (at_pos == -1) {
		return -3;
	}
	 // Check for a period after the @ symbol
	pd_pos = email.indexOf(".", at_pos);
	if (pd_pos == -1) {
		return -4;
	}
	// Check for period as last character
	if (pd_pos == (em_len - 1)) {
		return -5;
	}
	// Make sure there is at least one character between @ and .
	if (pd_pos == (at_pos + 1)) {
		return -6;
	}
	 // Make sure address doesn't start with @ symbol
	if (at_pos == 0) {
		return -7;
	}
  return 1;
}

//---------------
//Validate integer
//---------------
function validateINT(fieldVal)
{
 var intMASK = /^(\d{1,10})$/
 var matchArray = fieldVal.match(intMASK)
 if (matchArray==null)
 {
  return false;
 }
 return true;
}


//------------------------------------------------------------------------------------------
//validate time fields
//------------------------------------------------------------------------------------------
function validateTIME(field)
{
fieldVal = field.value;
// Checks if time is in HH:MM:SS AM/PM format.
// The seconds and AM/PM are optional.

//var timePat = /^(\d{1,2})(:)(\d{2})(\2(\d{1,2}))?$/;
// Use this to include seconds. am or pm is not in 6 instead of 4
// # of seconds in 4
// s sign (seconds) in 5
//var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

// Checks if time is in HH:MMAM|am/PM|pm format.
var timePat = /^(\d{1,2}):(\d{2})\s(AM|am|PM|pm)$/;
var matchArray = fieldVal.match(timePat);
//alert(fieldVal + "  " + timePat);
if (matchArray == null)
{
  alert("Time is not in a valid format.\nUse the HH:MM *m format");
  field.focus();
  return false;
}
hour = matchArray[1];
minute = matchArray[3];

 if (hour < 0  || hour > 12)
  {
    alert("Hour must be between 1 and 12.");
    field.focus();
    return false;
  }

if (minute<0 || minute > 59) 
{
  alert("Minute must be between 0 and 59.");
  field.focus();
  return false;
}

return true;
}


checkarray = new Array(true, true, true);

function chktab(ele, maxlength, chk) {
var x = 0
//alert ("Name: " + ele.name + " " + ele.value.length + "==" + maxlength + "?");
if (ele.value.length == maxlength) {
	if (checkarray[chk]==false) checkarray[chk] = true;
		else {
		while (x < document.forms[0].elements.length) {
			if (document.forms[0].elements[x].name == ele.name) document.forms[0].elements[x+1].focus();
				x ++
			}
			checkarray[chk] = false;
		}
	}
}


function validate_phone()
{
  for (i=0;i < document.forms[0].elements.length;i++){
      if (document.forms[0].elements[i].name == "phone1") {
          if (document.forms[0].elements[i].value == "" || document.forms[0].elements[i].value.length < 3 ) {
              alert("Please enter a valid Area Code for Phone Number field.");
              (document.forms[0].elements[i].focus());
              return false;
          }
       }
       if (document.forms[0].elements[i].name == "phone2") {
          if (document.forms[0].elements[i].value == "" || document.forms[0].elements[i].value.length < 3 ) {
              alert("Please enter Prefix for Phone Number field.");
              (document.forms[0].elements[i].focus());
              return false;
          }
       }
       if (document.forms[0].elements[i].name == "phone3") {
           if (document.forms[0].elements[i].value == "" || document.forms[0].elements[i].value.length < 4 ) {
               alert("Please enter complete Phone Number field.");
              (document.forms[0].elements[i].focus());
               return false;
           }
       }
  }

return (true);

}

var FileName
function searchBox(FileName)
{
	newWindow=window.open(FileName,'listWindow','scrollbars=1,toolbar=1,resizable=1,height=400,width=325');
}