function checkemail(toField,tuNoAlert) {
	var lcEmail=new String(toField.value);
	lcEmail=lcEmail.replace(/(^\s*)|(\s*$)/g, "");
	var llGoodEmail=lcEmail.length==0;
	if (!llGoodEmail)
		// Is this a valid email address?
		llGoodEmail=lcEmail.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	if (!llGoodEmail && tuNoAlert==null)
		{
		alert("Invalid Email address. Please correct.")
		toField.focus();
		}
	return llGoodEmail;
}
// Determine which radio button in a group has been selected
function getRadioValue(whichRadio)
  {
    for (var i=0; i<whichRadio.length; i++)
    {
        if (whichRadio[i].checked) return whichRadio[i].value;
    }
    return "";
  }
// Check the clicked radio button in a group
function radiohit(toRadio,tnIndex){
    for (var i=0;i<toRadio.length;i++) {
        toRadio[i].checked= i==tnIndex;
    }
	//toRadio[tnIndex].focus();
}
function clearfields(toForm) {
    var myform=toForm;
    for (var i = 0; i < myform.length; i++) {
        switch (myform.elements[i].type) {
   	      case "text" :
   	      	myform.elements[i].value="";
			break;
   	      case "textarea" :
			myform.elements[i].value="";
			break;
	      case "select" :
	      	for (j = 1; j < myform.elements[i].length;j++) {
	      	    myform.elements[i].selected[j]=false;
	      	}
	      	myform.elements[i].selected[0]=true;
			break;
		case "checkbox" :
			myform.elements[i].checked=false;
			break;
		case "radio" :
			myform.elements[i].checked=false;
			break;
	   }
    }
}

// Restrict entry to digits only.
// Call from onkeypress event of an INPUT tag, e.g.:
// onkeypress="javascript:digitsonly(event);"
// MSIE only -- NS equiv. didn't work in NS 2.0
// and still doesn't work in NS 6.0.
// Netscape--putting the 'backward' in backward compatibility.
function digitsonly(e) {
   if (navigator.userAgent.indexOf("MSIE") !=-1) {
       var keyChar = String.fromCharCode(e.keyCode);
        // Skip: tab,enter,backspace,delete,arrow left, and arrow right
		if (e.keyCode != 13 && e.keyCode != 9 && e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 39) {
            e.returnValue=(keyChar >= '0' && keyChar <= '9');
    	}
   }
}

// Restrict entry to alphanumeric only.
// Call from onkeypress event of an INPUT tag, e.g.:
// onkeypress="javascript:alphanumonly(event);"
// MSIE only -- NS equiv. has never worked as advertised
function alphanumonly(e) {
    if (navigator.userAgent.indexOf("MSIE") !=-1) {
       var keyChar = String.fromCharCode(e.keyCode);
        // Skip: tab,enter,backspace,delete,arrow left, and arrow right
		if (e.keyCode != 13 && e.keyCode != 9 && e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 39) {
            if (!((keyChar >= '0' && keyChar <= '9') || (keyChar >= 'A' && keyChar <= 'Z') || (keyChar >= 'a' && keyChar <= 'z'))) {
			   e.returnValue=false;
			   return (false);
			}
    	}
   }
return (true);
}

// VALIDATE FORM FUNCTION:
// This function requires that fields to be validated
// have been assigned a 'requiredMsg' property that is
// set to a text value. This is done by including something
// like the following in the body of the document:
// <scriptx language=javascript>
// document.myform.myfield.requiredMsg="This is an error message.";
// </scriptx>
// ...for each required field on the form. This function checks
// only whether or not the field has a 'requiredMsg' property
// and, if so, whether or not some value is present. Use with
// INPUT tags for "text" and "textarea" and "select" tags ONLY.
// The function halts at the first instance of an empty
// required field, displays the error message, and returns 'false'.
// If all fields pass, 'true' is returned.

function validateform(toForm) {
	for (var i = 0; i < toForm.length; i++) {
	    if (toForm.elements[i].requiredMsg != null) {
		    if (toForm.elements[i].value.length==0) {
			    alert(toForm.elements[i].requiredMsg);
				toForm.elements[i].focus();
				return (false);
			}
		}
	}
return (true);
}

function checkphone(toAC,toRC,toNR,toDest) {
    // Check the 3 fields that comprise a phone number
    // If they are valid, concatenate to the toDest field
    // and return 'true'. If not, return 'false'. Caller handles the error.
    toDest.value="";
    if ((toAC.value.length != 3) || (toRC.value.length != 3) || (toNR.value.length != 4)) {
        return (false);
        }
    if ((!alldigits(toAC.value)) || (!alldigits(toRC.value)) || (!alldigits(toNR.value))) {
    	  return (false);
    	  }
    toDest.value=toAC.value+toRC.value+toNR.value;
    return (true);
}

function alldigits(lcStr) {
    for (var i = 0; i < lcStr.length;i++) {
        if ((lcStr.substr(i,1) < "0") || (lcStr.substr(i,1) >"9")) {
        	return (false);
        	}
    }
    return (true);
}

function SaValZipCodeFormatShort(number) {
	if(number.length != 5) {
		return false;
	}
	for (var i=0; i<number.length; i++) {
		var ch = number.charAt(i);
		if( ch < "0" || "9" < ch) {
			return false;
		}
	} 
	return true;
}

function SaValZipCodeFormatLong(number) {
	if(number.length != 10) {
		return false;
	}
	if(number.indexOf("-") != 5) {
		return false;
	}
	for (var i=0; i<number.length; i++) {
		if( i==5 ) {
				continue;
		}
		var ch = number.charAt(i);
		if( ch < "0" || "9" < ch) {
			return false;
		}
	} 
	return true;
}

function SaValFixPhoneNumber(theField) {
        var inStr = theField.value;
        var inLen = inStr.length;
	var needFix = false;
	var allDigits = true;

	if(inLen < 10)
	{
		if (inLen==0)
		   {
		   return true;
		   }
		alert("not enough digits for phone number");
		return false;
	}

        // If this is a ten digit number XXXYYYZZZZ
        if(inLen == 10) {
		for(var i=0; i<inLen; i++) {
			var ch = inStr.charAt(i);
			if (ch < "0" || "9" < ch)
				return false;
		}
		var  fixedNumber = inStr.substring(0,3) 
                    + "-"
                    + inStr.substring(3,6)
                    + "-"
                    + inStr.substring(6,10) ;

		theField.value = fixedNumber;
		// alert("Reformatted the Phone field to '" + fixedNumber + "'.");
        	return true;
        }

	if (inLen == 12)
	{
		var ch = inStr.charAt(3);
		if(ch == " " || ch == "-")
		{
			allDigits = false;
		}		
	}
        
        // Is this is a twelve digit number WWXXXYYYZZZZ
	if (inLen == 12 && allDigits) {
		for (var i = 0; i < 12; i++) {
		var ch = inStr.charAt(i);
		if (ch < "0" || "9" < ch) {
			alert("invalid phone number");
			return false;
		}
	}

            var  fixedNumber = inStr.substring(0,2)
                    + "-"
                    + inStr.substring(2,5)
                    + "-"
                    + inStr.substring(5,8)
                    + "-"
                    + inStr.substring(8,12) ;

            theField.value = fixedNumber;

            // alert("Reformatted the Phone field to '" + fixedNumber + "'.");

            return true;
        }

	if (inLen == 12 && !allDigits) {
		/* check country code */
		for (var i = 0; i < 12; i++) {
			var ch = inStr.charAt(i);
			if(i==3 || i==7)
			{
				if(ch == "-")
					continue;
				if(ch == " ")
				{
					needFix = true;
					continue;
				}
				alert("invalid phone number");
				return false;
			}
                	if (ch < "0" || "9" < ch)
			{
				alert("invalid phone number");
				return false;
			}
            	}

		if(needFix)
		{
			var  fixedNumber = inStr.substring(0,3)
				+ "-"
				+ inStr.substring(4,7)
				+ "-"
				+ inStr.substring(8,12) ;

			theField.value = fixedNumber;

			// alert("Reformatted the Phone field to '" + fixedNumber + "'.");
		}

		return true;
	}

	if (inLen == 15) {
		/* check country code */
		for (var i = 0; i < 15; i++) {
			var ch = inStr.charAt(i);
			if(i==2 || i==6 || i==10)
			{
				if(ch == "-")
					continue;
				if(ch == " ")
				{
					needFix = true;
					continue;
				}
				alert("invalid phone number");
				return false;
			}
                	if (ch < "0" || "9" < ch)
			{
				alert("invalid phone number");
				return false;
			}
            	}

		if(needFix)
		{
			var  fixedNumber = inStr.substring(0,2)
				+ "-"
				+ inStr.substring(3,6)
				+ "-"
				+ inStr.substring(7,10)
				+ "-"
				+ inStr.substring(11,15) ;

			theField.value = fixedNumber;

			// alert("Reformatted the Phone field to '" + fixedNumber + "'.");
		}

		return true;
	}
    
	alert("invalid phone number");
	return false;
}

function SaValZipCodeFormat(toField) {
	var number=toField.value;
	if (number.length==0) {return true;}
	if(!SaValZipCodeFormatLong(number) && !SaValZipCodeFormatShort(number)) {
		alert("Incorrect zip code format");
		toField.focus();
		return false;
	}
	return true;
}

function SaValSSNFormat(toField) {
	var number = toField.value;
	if (number.length==0) {return;}
	if(number.length != 11) {
		alert("Incorrect social security number format");
		toField.focus();
		return false;
	}
	if(number.indexOf("-") != 3 || number.indexOf("-", 4) != 6) {
		alert("Incorrect social security number format");
		toField.focus();
		return false;
	}
	for (var i=0; i<number.length; i++) {
		if( i==3 || i==6 ) {
				continue;
		}
		var ch = number.charAt(i);
		if( ch < "0" || "9" < ch) {
			alert("Incorrect social security number format");
			toField.focus();
			return false;
		}
	} 
	return true;
}

function checkdate(objName) {
var datefield = objName;
if (chkdate(objName) == false) {
datefield.select();
alert("That date is invalid.  Please try again.");
datefield.focus();
return false;
}
else {
return true;
   }
}
function chkdate(objName) {
var strDatestyle = "US"; //United States date style
//var strDatestyle = "EU";  //European date style
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var datefield = objName;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
var err = 0;
var strMonthArray = new Array(12);
strMonthArray[0] = "Jan";
strMonthArray[1] = "Feb";
strMonthArray[2] = "Mar";
strMonthArray[3] = "Apr";
strMonthArray[4] = "May";
strMonthArray[5] = "Jun";
strMonthArray[6] = "Jul";
strMonthArray[7] = "Aug";
strMonthArray[8] = "Sep";
strMonthArray[9] = "Oct";
strMonthArray[10] = "Nov";
strMonthArray[11] = "Dec";
strDate = datefield.value;
if (strDate.length < 1) {
return true;
}
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
strDateArray = strDate.split(strSeparatorArray[intElementNr]);
if (strDateArray.length != 3) {
err = 1;
return false;
}
else {
strDay = strDateArray[0];
strMonth = strDateArray[1];
strYear = strDateArray[2];
}
booFound = true;
   }
}
if (booFound == false) {
if (strDate.length>5) {
strDay = strDate.substr(0, 2);
strMonth = strDate.substr(2, 2);
strYear = strDate.substr(4);
   }
}
if (strYear.length == 2) {
   if (strYear <= '15') {
   	  strYear = '20' + strYear;
	  }
   else {
   	  strYear = '19' + strYear;
   }
}
// US style
if (strDatestyle == "US") {
strTemp = strDay;
strDay = strMonth;
strMonth = strTemp;
}
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
err = 2;
return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
for (i = 0;i<12;i++) {
if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
intMonth = i+1;
strMonth = strMonthArray[i];
i = 12;
   }
}
if (isNaN(intMonth)) {
err = 3;
return false;
   }
}
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
err = 4;
return false;
}
if (intMonth>12 || intMonth<1) {
err = 5;
return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
err = 6;
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
err = 7;
return false;
}
if (intMonth == 2) {
if (intday < 1) {
err = 8;
return false;
}
if (LeapYear(intYear) == true) {
if (intday > 29) {
err = 9;
return false;
}
}
else {
if (intday > 28) {
err = 10;
return false;
}
}
}
if (strDatestyle == "US") {
// datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
   datefield.value = intMonth + "/" + intday + "/"+strYear;
}
else {
datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
}
return true;
}
function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}
function doDateCheck(from, to) {
if (Date.parse(from.value) <= Date.parse(to.value)) {
alert("The dates are valid.");
}
else {
if (from.value == "" || to.value == "") 
alert("Both dates must be entered.");
else 
alert("To date must occur after the from date.");
   }
}
