<!--- Generalized script library of validation and formatting routines --->

<!--- Validate eMail address --->
function ValidateEmail( str ) {
  <!--- are regular expressions supported? --->
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
} <!--- end function ValidateEmail(str) --->



<!--- Returns true if value is a number or is NULL otherwise returns false --->
function CheckNumber( textObj )
{
	var newValue = textObj
	if (newValue.length == 0)
		return true;
<!--- Returns true if value is a number defined as having an optional leading + or -. --->
<!--- having at most 1 decimal point.  otherwise containing only the characters 0-9. --->
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

	<!--- The first character can be + - .  blank or a digit. --->
	check_char = start_format.indexOf(newValue.charAt(0))
	<!--- Was it a decimal? --->
	if (check_char == 1)
		decimal = true;
	else if (check_char < 1)
		return false;

	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < newValue.length; i++) {
		check_char = number_format.indexOf(newValue.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1) {
			if (decimal)	<!--- Second decimal --->
				return false;
			else
				decimal = true;
		} else if (check_char == 0) {
			if (decimal || digits)
				trailing_blank = true;
		<!--- ignore leading blanks --->

		} else if (trailing_blank)
			return false;
		else
			digits = true;
	}
	<!--- All tests passed, so... --->
	return true
} <!--- end function CheckNumber() --->



<!--- Check integer --->
function CheckInteger( textObj ) {
   <!--- Returns true if value is a number or is NULL   otherwise returns false --->

	var newValue = textObj ;
	if (newValue.length == 0)
		return true;
<!--- Returns true if value is an integer defined as   having an optional --->
<!--- leading + or -. otherwise containing only the characters 0-9. --->
	var decimal_format = ".";
	var check_char;

	<!--- The first character can be + -  blank or a digit --->
	check_char = newValue.indexOf(decimal_format)
	<!--- Was it a decimal? --->
	if (check_char < 1)
		return CheckNumber( newValue );
	else
		return false;
} <!--- end function CheckInteger() --->


function onError( form_object, input_object, object_value, error_message ) {
	alert( error_message );
	return false;	
}

function NumberRange( object_value, min_value, max_value ) {
    <!--- check minimum --->
    if (min_value != null) {
        if (object_value < min_value)
		return false;
	}

    <!--- check maximum --->
    if (max_value != null) {
		if (object_value > max_value)
			return false;
	}
	
	<!--- All tests passed, so... --->
	return true;
}



function CheckRange( object_value, min_value, max_value ) {
	<!--- if value is in range then return true else return false --->

	if ( object_value.length == 0 )
		return true;

	if (! CheckNumber( object_value )) {
		return false;
	} else {
		return ( NumberRangeXX(( eval(object_value) ), min_value, max_value));
	}
	
	<!--- All tests passed, so --->
	return true;
} <!--- end function CheckRange( object_value, min_value, max_value ) --->


function ValidatePhone( textObj ) {
	if ( textObj.length == 0 )
		return true;
		
	if ( textObj.length != 12 )
		return false;

	<!--- check if first 3 characters represent a valid area code --->
	if (! CheckNumber( textObj.substring(0,3) ))
		return false;
	else
	if (! NumberRange( (eval(textObj.substring(0,3))), 100, 1000))
		return false;

	<!--- check if area code/exchange separator is either a'-' or ' ' or '.' --->
	if (textObj.charAt(3) != "-" && textObj.charAt(3) != " " && textObj.charAt(3) != "." )
		return false

	<!--- check if  characters 5 - 7 represent a valid exchange --->
	if (! CheckNumber(textObj.substring(4,7)))
		return false;
	else
	if (! NumberRange((eval(textObj.substring(4,7))), 100, 1000))
		return false;
	
	<!--- check if exchange/number separator is either a '-' or ' ' or '.' --->
	if ( textObj.charAt(7) != "-" && textObj.charAt(7) != " " && textObj.charAt(7) != ".")
		return false;

	<!--- make sure last four digits are a valid integer --->
	if ( textObj.charAt(8) == "-" || textObj.charAt(8) == "+")
		return false;
	else
	{
		return (CheckInteger( textObj.substring(8,12)) );
	}
} <!--- end function ValidatePhone( textObj ) --->


<!--- Credit card check --->
function ValidateCreditCard( textObj ) {
	var newValue = textObj.value ;
	var white_space = " -" ;
	var creditcard_string = "" ;
	var check_char ;

	if (newValue.length == 0) {
		return true;
	}
	
	<!--- squish out the white space --->
	for ( var i = 0; i < newValue.length; i++ ) {
		check_char = white_space.indexOf( newValue.charAt(i) )
		if (check_char < 0)
			creditcard_string += newValue.substring(i, (i + 1));
	}	

	<!--- if all white space return error --->
    if ( creditcard_string.length == 0 )
        return false;
	 	
	<!--- make sure number is a valid integer --->
	if ( creditcard_string.charAt(0) == "+" )
        return false;

	if (! CheckInteger(creditcard_string) )
		return false;

    <!--- now check mod10 --->
	var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
	var checkdigit = 0;
	var tempdigit;

	for ( var i = 0; i < creditcard_string.length; i++ ) {
		tempdigit = eval( creditcard_string.charAt(i) )

		if ( doubledigit ) {
			tempdigit *= 2;
			checkdigit += ( tempdigit % 10 );

			if ( (tempdigit / 10) >= 1.0 ) {
				checkdigit++;
			}

			doubledigit = false;
		} else {
			checkdigit += tempdigit;
			doubledigit = true;
		}
	}	
	return (checkdigit % 10) == 0 ? true : false;
} <!---  end function ValidateCreditCard() --->



<!--- Function to format a variable of type money --->
function moneyFormat( textObj ) {
   var newValue = textObj.value
   var decAmount = ""
   var dolAmount = ""
   var decFlag = false
   var aChar = ""

	<!--- Check for only digits, commas and decimal points --->
	for ( i=0; i < newValue.length; i++ ) {
		aChar = newValue.substring(i,i+1) ;
		if ( ( aChar >= "a" && aChar <= "z" ) || ( aChar >= "A" && aChar <= "Z" ) ) {
			return false ;
		}
		if ( aChar >= "0" && aChar <= "9" ) {
			if ( decFlag ) {
				decAmount = "" + decAmount + aChar
			}
			else {
				dolAmount = "" + dolAmount + aChar
			}
		}
		if ( aChar == "." ) {
			if ( decFlag ) {
				dolAmount = ""
				break
			}
			decFlag=true
		}
	}

   <!--- Ensure that at least a zero appears for the dollar amount --->
   if ( dolAmount == "" ) {
      dolAmount = "0"
   }
   <!--- Strip leading zeros --->
   if ( dolAmount.length > 1 ) {
      while ( dolAmount.length > 1 && dolAmount.substring(0,1) == "0" ) {
         dolAmount = dolAmount.substring(1,dolAmount.length)
      }
   }

   <!--- Round the decimal amount --->
   if ( decAmount.length > 2 ) {
      if ( decAmount.substring(2,3) > "4" ) {
         decAmount = parseInt(decAmount.substring(0,2)) + 1
         if ( decAmount < 10 ) {
            decAmount = "0" + decAmount
         }
         else {
            decAmount = "" + decAmount
         }
      }
      else {
         decAmount = decAmount.substring(0,2)
      }
      if (decAmount == 100) {
         decAmount = "00"
         dolAmount = parseInt(dolAmount) + 1
      }
   }

   <!--- Pad right side of decAmount --->
   if (decAmount.length == 1) {
      decAmount = decAmount + "0"
   }
   if (decAmount.length == 0) {
      decAmount = decAmount + "00"
   }

   <!--- Check for negative values and reset textObj --->
   if (newValue.substring(0,1) != '-' || (dolAmount == "0" && decAmount == "00")) {
      textObj.value = dolAmount + "." + decAmount
   }
   else{
      textObj.value = '-' + dolAmount + "." + decAmount
   }
   return true ;
}


<!--- Function to Check for Valid Dates --->
function checkdate(objName) 
{
	var datefield = objName;
	if (chkdate(objName) == false) 
	{
		datefield.select();
		alert("That date is invalid. Please try again. Use mm/dd/yyyy format");
		datefield.focus();
		return false;
	}
	else 
	{
		return true;
	}
}

function chkdate(objName) 
{
	var strDatestyle = "US"; <!--- United States date style Month Day Year --->
	//var strDatestyle = "EU"; <!--- European date style Day Month Year --->
	var runTime = new Date();
	var strCurrentYear = runTime.getYear();
	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 < 2) || (strDateArray.length > 3)) 
			{
				err = 1;
				alert('X');
				return false;
			}
			else 
			{
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				if (strDateArray.length == 2) 
					strYear = strCurrentYear ;
				else
					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 == null) 
	{
	  return false;
	}

	if (strYear.length == 1) 
	{
		strYear = '0' + strYear ;
	}

	if (strYear.length == 2) 
	{
		if (strYear <= 50)
			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;
	}
	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 to Check for Valid Time --->
function checktime(objName) 
{
	var timefield = objName;
	if (chktime(objName) == false) 
	{
		timefield.select();
		alert("That time is invalid. Please try again. Use hh:mm format");
		timefield.focus();
		return false;
	}
	else 
	{
		return true;
	}
} // end function checktime()

function chktime(objName)
{
	var strtime;
	var strtimeArray;
	var strHour;
	var strMinute;
	var booFound = false;
	var timefield = objName;
	var strSeparatorArray = new Array(":"," ",".");
	var intElementNr;
	var err = 0;
	strtime = timefield.value;
	if (strtime.length < 1) 
	{
		return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) 
	{
		if (strtime.indexOf(strSeparatorArray[intElementNr]) != -1) 
		{
			strtimeArray = strtime.split(strSeparatorArray[intElementNr]);
			if ((strtimeArray.length < 2) || (strtimeArray.length > 2)) 
			{
				err = 1;
				alert('X');
				return false;
			}
			else 
			{
				strHour = strtimeArray[0];
				strMinute = strtimeArray[1];
			}
			booFound = true;
		}
	}
	if (booFound == false) 
	{
		if (strtime.length>5) 
		{
			strHour = strtime.substr(0, 2);
			strMinute = strtime.substr(2, 2);
		}
	}

	if (strHour == null) 
	{
	  return false;
	}
	if (strHour.length == 1) 
	{
		strHour = '0' + strHour ;
	}

	if (strMinute == null) 
	{
	  return false;
	}
	if (strMinute.length == 1) 
	{
		strMinute = '0' + strMinute ;
	}


	intHour = parseInt(strHour, 10);
	if ( (isNaN(intHour)) || (intHour > 12) )
	{
		err = 2;
		return false;
	}

	intMinute = parseInt(strMinute, 10);
	if ( (isNaN(intMinute)) || (intMinute > 59) ) 
	{
		err = 3;
		return false;
	}

	// All is well
	return true;
} // end function chktime()