// *****************BEGIN GENERIC FUNCTIONS*******************************

/* ======================================================================
FUNCTION:  	IsAlpha
INPUT:		str (string) - the string to be tested
RETURN:  	true, if the string contains only alphabetic characters false, otherwise.
====================================================================== */
function IsAlpha( str ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;

	str += "";	// convert to a string for performing string comparisons.

	// Loop through string one character at time,  breaking out of for
	// loop when an non Alpha character is found.
  	for (i = 0; i < str.length; i++) {
		// Alpha must be between "A"-"Z", or "a"-"z"
		if ( !( ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
      			((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ) ) {
         				isValid = false;
         				break;
      			}
   } // end for loop
   
	return isValid;
}  // end IsAlpha 


/* ======================================================================
FUNCTION:	IsAlphaNum
INPUT:		str (string) - a string that will be tested to ensure that
    		each character is a digit or a letter.
RETURN:  	true, if all characters in the string are a character from 0-9
     		or a-z or A-Z;  
			false, otherwise
====================================================================== */
function checkDate(dVal) {
	//Check date is valid
		
	if (dVal.length == 0){
		return false; // cancel the submit
	}
	if (isNaN(Number(new Date(dVal)))){
		return false; // cancel the submit
	}
			
	return true; // dont cancel
}



/* ======================================================================
FUNCTION:	IsAlphaNum
INPUT:		str (string) - a string that will be tested to ensure that
			each character is a digit or a letter.
RETURN:  	true, if all characters in the string are a character from 0-9
			or a-z or A-Z; false, otherwise
====================================================================== */
function IsAlphaNum( str ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;
	
	// convert to a string for performing string comparisons.
   	str += "";	

	// Loop through length of string and test for any alpha numeric 
	// characters
   	for (i = 0; i < str.length; i++)
   	{
			// Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
      	if (!(((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) || 
      			((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
      			((str.charAt(i) >= "A") && (str.charAt(i) <= "Z"))))
			{
				isValid = false;
				break;
			}	
   	} // END for   
   
   	return isValid;
}  // end IsAlphaNum



/* ======================================================================
FUNCTION:	IsAlphaNum
INPUT:		str (string) - a string that will be tested to ensure that
			each character is a digit or a letter.
RETURN:  	true, if all characters in the string are a character from 0-9
			or a-z or A-Z or spaces; false, otherwise
====================================================================== */
function IsAlphaNumOrSC( str ) {

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;
	
	// convert to a string for performing string comparisons.
   	str += "";	

	// Loop through length of string and test for any alpha numeric 
	// characters
   	for (i = 0; i < str.length; i++)
   	{
			// Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
      	if (!(((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) || 
      			((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
      			((str.charAt(i) >= "A") && (str.charAt(i) <= "Z"))||    
				(str.charAt(i) == " ")||
				(str.charAt(i) == "'") ))
			{
				isValid = false;
				break;
			}	
   	} // END for   
   
   	return isValid;
}  // end IsAlphaNumOrSpace



/* ======================================================================
FUNCTION:	IsAlphaNumOrUnderscore
INPUT:		str (string) - the string to be tested
RETURN:  	true, if the string contains only alphanumeric characters or underscores.
			false, otherwise.
====================================================================== */
function IsAlphaNumOrUnderscore( str ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;

	str += "";	// convert to a string for performing string comparisons.
	// Loop through string one character at a time. If non-alpha numeric
	// is found then, break out of loop and return a false result

	for (i = 0; i < str.length; i++)
   	{
		// Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
      		if ( !( ((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) || 
      			((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
      			((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ||
      			(str.charAt(i) == "_") ) )
      		{
   				isValid = false;
         		break;
      		}

	} // END for   
   
	return isValid;

}  // end IsAlphaNumOrUnderscore




/* ======================================================================
FUNCTION:  	IsInt
INPUT:  	numstr (string/number) 	 - the string that will be tested to ensure 
			that each character is a digit
			allowNegatives (boolean) - (optional) when true, allows numstr to be
			negative (contain a '-').  When false, any negative number or a string starting
			with a '-' will be considered invalid.
RETURN:  	true, if all characters in the string are a character from 0-9,
			regardless of value for allowNegatives
			true, if allowNegatives is true and the string starts with a '-', and all other
			characters are 0-9. false, otherwise.
====================================================================== */
function IsInt( numstr, allowNegatives ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")	
		return false;

	// Default allowNegatives to true when undefined or null
	if (allowNegatives+"" == "undefined" || allowNegatives+"" == "null")	
		allowNegatives = true;

	var isValid = true;

	// convert to a string for performing string comparisons.
	numstr += "";	

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special case for negative numbers (first char == '-').   
	for (i = 0; i < numstr.length; i++) {
    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || (numstr.charAt(i) == "-"))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "-" && i != 0) || 
				(numstr.charAt(i) == "-" && !allowNegatives)) {
       	isValid = false;
       	break;
      }
         	         	       
   } // END for   
   
   	return isValid;
}  // end IsInt




/* ======================================================================
FUNCTION:	IsBlank
INPUT:		val - the value to be tested
RETURN:  	true, if the string is null, undefined or an empty string, ""
			false, otherwise.
CALLS:		IsNull(), IsUndef() which are defined elsewhere in the Script Library
====================================================================== */
function IsBlank( str ) {
	var isValid = false;

 	if ( IsNull(str) || IsUndef(str) || (str+"" == "") )
 		isValid = true;
		
	return isValid;
}  // end IsBlank




/* ======================================================================
FUNCTION:	IsNull
INPUT:		val - the value to be tested
RETURN:  	true, if the value is null;
      		false, otherwise.
====================================================================== */
function IsNull( val ) {
	var isValid = false;

 	if (val+"" == "null")
 		isValid = true;
		
	return isValid;
}  // end IsNull




/* ======================================================================
FUNCTION:  	IsNum
INPUT:  	numstr (string/number) - the string that will be tested to ensure 
      		that the value is a number (int or float)
RETURN:  	true, if all characters represent a valid integer or float
     		false, otherwise.
====================================================================== */
function IsNum( numstr ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")	
		return false;

	var isValid = true;
	var decCount = 0;		// number of decimal points in the string

	// convert to a string for performing string comparisons.
	numstr += "";	

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special cases for negative numbers (first char == '-')
	// and a single decimal point (any one char in string == '.').   
	for (i = 0; i < numstr.length; i++) {
		// track number of decimal points
		if (numstr.charAt(i) == ".")
			decCount++;

    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || 
				(numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "-" && i != 0) ||
				(numstr.charAt(i) == "." && numstr.length == 1) ||
			  (numstr.charAt(i) == "." && decCount > 1)) {
       	isValid = false;
       	break;
      }         	         	       
//if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9")) || 
   } // END for   
   
   	return isValid;
}  // end IsNum



/* ======================================================================
FUNCTION:  	IsMoney
INPUT:  	numstr (string/number) - the string that will be tested to ensure 
      		that the value is a number (int or float),
      		fldName - the name of the form field 
RETURN:  	true, if all characters represent a valid integer or float
     		false, otherwise.
====================================================================== */
function IsMoney( numstr, fldName ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")	
		return false;
	
	var isValid = true;
	var decCount = 0;		// number of decimal points in the string

	// convert to a string for performing string comparisons.
	numstr += "";	

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special cases for dollar sign (first char == '$'), comma (any char in string == ','),
	// and a single decimal point (any one char in string == '.').   
	for (i = 0; i < numstr.length; i++) {
		// track number of decimal points
		if (numstr.charAt(i) == ".")
			decCount++;

    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || (numstr.charAt(i) == ",") || 
				(numstr.charAt(i) == "$") || (numstr.charAt(i) == "."))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "$" && i != 0) ||
				(numstr.charAt(i) == "." && numstr.length == 1) ||
				(numstr.charAt(i) == "," && numstr.length < 4) ||
				(numstr.charAt(i) == "." && decCount > 1)) {
       	isValid = false;
       	break;
      }         	         	       
//if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9")) || 
   } // END for   
   
  // alert ("isValid=" + isValid)
   if (isValid){
		var strRes = StripNonNumericMoney(numstr,fldName)
	//	alert ("strRes=" + strRes)
		if (strRes !=""){
		 	isValid = true;
		}else{
			isValid = false;
		}	
	}
	return isValid;
}  // end IsMoney



/* ======================================================================
FUNCTION:	IsUndef
INPUT:		val - the value to be tested
RETURN:  	true, if the value is undefined
      		false, otherwise.
====================================================================== */
function IsUndef( val ) {
	var isValid = false;

 	if (val+"" == "undefined")
 		isValid = true;
		
	return isValid;
}  // end IsUndef




/* ======================================================================
FUNCTION:  	ParseQueryString
INPUT:		searchstr - the querystring value to be returned
RETURN:  	the value of that querystring item, or "" if blank
====================================================================== */
function ParseQueryString(searchStr) 
{
	var tempStr = window.location.search;
	var startOfString = tempStr.indexOf(searchStr);
	var result = "";

	result = "";
	if (startOfString != -1) {
		var endOfString = tempStr.indexOf("&",startOfString+searchStr.length+1);
	
		if (endOfString != -1)  {               
			var result = tempStr.substring(startOfString+searchStr.length+1, endOfString);
		} else {
			var result = tempStr.substring(startOfString+searchStr.length+1, tempStr.length);
		}
	}
	return unescape(result);
}



/* ======================================================================
FUNCTION:  	StripNonNumeric
INPUT:    	str (string)
RETURN:  	true, if the string contains digits and dashes in the form 111-12-3456;
CALLS:		IsInt(), which is defined elsewhere in the Script Library
====================================================================== */
function StripNonNumeric( strN, fldName, formName ) {
	// Return immediately if an invalid value was passed in
	if (strN+"" == "undefined" || strN+"" == "null" || strN+"" == "")	
		return "";
		
	strN += "";	// make sure it's a string
	var strD = strN;

	for (j = 0; j < strN.length; j++) {
		if (strD+"" == "" || strD.length == 0) return;
		
		// track number of decimal points
		if (strN.charAt(j) == " " || !IsNum(strN.charAt(j))) {
			strD = strN.substring(0,j) + strN.substring(j+1,strN.length);
			j-- ;
		}else{
			if (strN.charAt(j) == "-") strD = strN.substring(0,j) + strN.substring(j+1,strN.length);
			if (strN.charAt(j) == "(") strD = strN.substring(0,j) + strN.substring(j+1,strN.length);
			if (strN.charAt(j) == ")") strD = strN.substring(0,j) + strN.substring(j+1,strN.length);
			if (strN.charAt(j) == " ") strD = strN.substring(0,j) + strN.substring(j+1,strN.length);
		}
		strN = strD;
	}
	
	if (fldName != "") {
		var eStr = "document." + formName + "." + fldName + ".value=" + strD + ";";
		eval(eStr);
	}

// alert(strD);
 	return strD;
} // end StripNonNumeric

/* ======================================================================
FUNCTION:  	StripNonNumericMoney
INPUT:    	str (string)
RETURNS:  	if successful, will set form field to string of numbers
			(all characters stripped except decimal);
CALLS:		IsInt(), which is defined elsewhere in the Script Library
====================================================================== */
function StripNonNumericMoney( strN, fldName ) {
	// Return immediately if an invalid value was passed in
	if (strN+"" == "undefined" || strN+"" == "null" || strN+"" == "")	
		return "";
		
	strN += "";	// make sure it's a string
	var strD = strN;

	for (j = 0; j < strN.length; j++) {
		if (strD+"" == "" || strD.length == 0) return;
		
		// track number of decimal points
		
		if (!IsNum(strN.charAt(j))){
			if (strN.charAt(j)!= ".") {
				strD = strN.substring(0,j) + strN.substring(j+1,strN.length);
				j-- ;
			}
		}
		strN = strD;
	}
	
	if (fldName != "" && strD != "") {
		var eStr = "document.forms[0]." + fldName + ".value=" + strD + ";";
		eval(eStr);
	}

// alert(strD);
 	return strD;
} // end StripNonNumericMoney



// *****************END GENERIC FUNCTIONS*********************************





// *****************BEGIN SPECIAL FUNCTIONS*******************************

/* ======================================================================
FUNCTION:  	IsValidEmail
INPUT:    	str (string) - an e-mail address to be tested
RETURN:  	true, if the string contains a valid e-mail address which is a string
			plus an '@' character followed by another string containing at least 
			one '.' and ending in an alpha (non-punctuation) character.
			false, otherwise
CALLS:		IsBlank(), IsAlpha() which are defined elsewhere in the Script Library
====================================================================== */
function IsValidEmail( str ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;

	str += "";

	namestr = str.substring(0, str.indexOf("@"));  // everything before the '@'
	domainstr = str.substring(str.indexOf("@")+1, str.length); // everything after the '@'
	dotstr = domainstr.substring(domainstr.indexOf(".")+1, domainstr.length);
	// Rules: namestr cannot be empty, or that would indicate no characters before the '@',
	// domainstr must contain a period that is not the first character (i.e. right after
	// the '@').  The last character must be an alpha.
   	if (IsBlank(str) || (namestr.length == 0) || 
			(domainstr.indexOf(".") <= 0) ||
			(domainstr.indexOf("@") != -1) ||
			(dotstr == 0) ||
			!IsAlpha(str.charAt(str.length-1)))
		isValid = false;

//		if (dotstr.length < 2 || dotstr.length > 4)
//		isValid = false;

		CharText = "[]'.@-_+`~&^1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
		for(j=0; j<str.length ;j++)
		{
			if(CharText.indexOf(str.charAt(j))<0)
				isValid = false;
		} 
		if(str.indexOf("..") > 0 )
			isValid = false;
		if(str.indexOf("@@") > 0 )
			isValid = false;
		if(str.indexOf("[[") > 0 )
			isValid = false;
		if(str.indexOf("]]") > 0 )
			isValid = false;
		if(str.indexOf("''") > 0 )
			isValid = false;
   	return isValid;
} // end IsValidEmail



/* ======================================================================
FUNCTION:  	IsValidPhone
INPUT:    	str (string) - an phone number to be tested
			incAreaCode (boolean) - if true, area code is included (10-digits);
			if false or undefined, area code not included
RETURN:  	true, if the string contains a 7-digit phone number and incAreaCode == false
			or is undefined 
			true, if the string contains a 10-digit phone number and incAreaCode == true
			false, otherwise
CALLS:		StripNonNumeric(), which is defined elsewhere in the Script Library
====================================================================== */
function IsValidPhone(str, fldName, formName, incAreaCode) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	// Set default value for incAreaCode to true, if undefined or null
	if (incAreaCode+"" == "undefined" || incAreaCode+"" == "null")	
		incAreaCode = true;

	var isValid = true;

	str += "";

	// After stripping out non-numeric characters, such as dashes, the
	// phone number should contain 7 digits (no area code) or 10 digits (area code)
	str = StripNonNumeric(str+"", fldName, formName);

	if (str.length != 10) isValid = false;
	
   	return isValid;
} // end IsValidPhone


/* ======================================================================
FUNCTION:  	IsValidCCNum
			This function tests a credit card number with a LUHN-10 algorithm
			to validate - it works for MasterCard, Visa, AMEX, Diners Club,
			Discover, EnRoute, and JCB
INPUT:		ccVal - the credit card number
RETURN:  	true or false
====================================================================== */
function IsValidCCNum(ccVal)
{
	var checkOK = "0123456789";
	var checkStr = ccVal;
	var CrValid = true;
	var checksum=0;
	var ddigit=0;
	var kdig = 0;
	if (checkStr.length < 13) alert ('You have not entered enough digits. Please check the number for errors.');

// CHECK FOR TEST CC NUMS
/* ALLOW ALL TEST CC NUMS TO WORK - TEMPORARILY
	if (ccVal == "4111111111111111") return false;	//FAKE VISA
	if (ccVal == "5555555555554444") return false;	//FAKE MASTERCARD
	if (ccVal == "378282246310005") return false;	//FAKE AMEX
	if (ccVal == "6011111111111117") return false;	//FAKE DISCOVER
*/
	for (i = checkStr.length-1;  i >= 0;  i--)
	{
		kdig++;
		ch = checkStr.charAt(i);
		if ((kdig % 2) != 0)
			checksum=checksum+parseInt(ch)
		else {
			ddigit=parseInt(ch)*2;

		if (ddigit >= 10)
			checksum=checksum+1+(ddigit-10)
		else
			checksum=checksum+ddigit;
		}

		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length)
			{
				return(false);
			}
		}

		if ((checksum % 10) != 0){
			return (false);
		}else{
			return(true);
		}
	}





/* ======================================================================
FUNCTION:  	IsValid5DigitZip
INPUT:    	str (string) - a 5-digit zip code to be tested
RETURN:  	true, if the string is 5-digits long
			false, otherwise
CALLS:		IsBlank(), IsInt() which are defined elsewhere in the Script Library
====================================================================== */
function IsValid5DigitZip( str ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;
	
	var isValid = true;

	str += "";

	// Rules: zipstr must be 5 characters long, and can only contain numbers from
   // 0 through 9
   if (IsBlank(str) || (str.length != 5) || !IsInt(str, false))
		isValid = false;
   
   return isValid;
} // end IsValid5DigitZip




/* ======================================================================
FUNCTION:  	IsValid5Plus4DigitZip
INPUT:    	str (string) - a 5+4 digit zip code to be tested
RETURN:  	true, if the string contains 5-digits followed by a dash followed by 4 digits
			false, otherwise
CALLS:		IsBlank(), IsInt() which are defined elsewhere in the Script Library
====================================================================== */
function IsValid5Plus4DigitZip( str ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;

	str += "";

	// Rules: The first five characters may contain only digits 0-9,
	// the 6th character must be a dash, '-', and 
	// the last four characters may contain only digits 0-9.
	// The total string length must be 10 characters.
   	if (IsBlank(str) || (str.length != 10) || 
			!IsInt(str.substring(0,5), false) || str.charAt(5) != '-' ||
			!IsInt(str.substring(6,10), false))
		isValid = false;
   
   	return isValid;
} // end IsValid5Plus4DigitZip




/* ======================================================================
FUNCTION:  	IsValidSSN
INPUT:    	str (string) - an phone number to be tested
			incDashes (boolean) - if true, str includes dashes (e.g. 111-12-3456);
			if false, str contains only digits
RETURN:  	true, if the string contains digits and dashes in the form 111-12-3456;
			true, if the string contains a 9-digit number and incDashes is false;
			false, otherwise
CALLS:		IsInt(), which is defined elsewhere in the Script Library
====================================================================== */
function IsValidSSN( str, incDashes ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;

	// Set default value for incDashes to true, if undefined or null
	if (incDashes+"" == "undefined" || incDashes+"" == "null")	
		incDashes = true;

	str += "";	// make sure it's a string

	if (!incDashes && (!IsNum(str) || str.length != 9))
		isValid = false;

	var part1 = str.substring(0,3);
	var part2 = str.substring(4,6);
	var part3 = str.substring(7,str.length);

	// Ensure that the first part is a number and 3 digits long,
	// the second part is a number and 2 digits long,
	// the third part is a number and 4 digits long, e.g. 111-22-3333
	if (incDashes && ((!IsInt(part1, false) || part1.length != 3) ||
			(!IsInt(part2, false) || part2.length != 2) || 
			(!IsInt(part3, false) || part3.length != 4)) )
		isValid = false;

   	return isValid;
} // end IsValidSSN


/* ======================================================================
FUNCTION:  	IsValidDate
INPUT:    	str (string) - a date string to be tested 
			Accepts dashes or forwardslashes as separators
RETURNS:  	true, if the string is a valid date;
			false, otherwise
CALLS:		LeapYear(), which is defined below
====================================================================== */
function IsValidDate(str) {

// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;
	
	if (str.length < 6)
		return false;
		
var strDatestyle = "US"; //United States date style
//var strDatestyle = "EU";  //European date style
var strDate = str;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
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";
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) {
strYear = '20' + 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;
}
}
}
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:  	IsValidTime
INPUT:    	timeStr
RETURN:  	True if valid time, else false
CALLS:		Nothing
====================================================================== */
function IsValidTime(timeStr) 
{
	// 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})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
		return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];

	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }

	if (hour < 0  || hour > 23) {
		alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
		return false;
	}
	if (hour <= 12 && ampm == null) {
		if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
			alert("You must specify AM or PM.");
			return false;
		}
	}
	if  (hour > 12 && ampm != null) {
		alert("You can't specify AM or PM for military time.");
		return false;
	}
	if (minute<0 || minute > 59) {
		alert ("Minutes must be a number between 0 and 59.");
		return false;
	}
	if (second != null && (second < 0 || second > 59)) {
		alert ("Seconds must be a number between 0 and 59.");
		return false;
	}

	return true;
}


// *****************END SPECIAL FUNCTIONS*********************************




// *****************BEGIN CUSTOM FUNCTIONS********************************

/* ======================================================================
FUNCTION:  	testRadio
INPUT:    	radioFld - the field name of the radio button to test
RETURN:  	the value of the radio field's selected item. If none selected,
			then it returns "" (empty string). 
CALLS:		Nothing
====================================================================== */
	function testRadio(radioFld){
		var fReturn = "";
		for (var i=0; i < radioFld.length; i ++){
			if (radioFld[i].checked) fReturn = radioFld[i].value;
		}
		return fReturn;
	}

	
/* ======================================================================
FUNCTION:  	testCheckbox
INPUT:    	ckFld - the field name of the checkbox to test
RETURN:  	the value of the checkbox's selected item(s). If none selected,
			then it returns "" (empty string). 
CALLS:		Nothing
====================================================================== */
	function testCheckbox(ckFld){
		var fReturn = "";
		for (var i=0; i < ckFld.length; i ++){
			if (ckFld[i].checked) fReturn = fReturn + ckFld[i].value + ",";
		}
		return fReturn;
	}

// *****************END CUSTOM FUNCTIONS**********************************



/* ======================================================================
FUNCTION:  	FormatDateTime
INPUT:    	datetime, FormatType
			FomatType takes the following values
			1 - General Date = Friday, October 30, 1998
			2 - Typical Date = 10/30/98
			3 - Standard Time = 6:31 PM
			4 - Military Time = 18:31
RETURN:  	Returns an expression formatted as a date or time
CALLS:		Nothing
====================================================================== */

function FormatDateTime(datetime, FormatType)
{
	var strDate = new String(datetime);

	if (strDate.toUpperCase() == "NOW") {
		var myDate = new Date();
		strDate = String(myDate);
	} else {
		var myDate = new Date(datetime);
		strDate = String(myDate);
	}


	// Get the date variable parts
	var Day = new String(strDate.substring(0,3));
	if (Day == "Sun") Day = "Sunday";
	if (Day == "Mon") Day = "Monday";
	if (Day == "Tue") Day = "Tuesday";
	if (Day == "Wed") Day = "Wednesday";
	if (Day == "Thu") Day = "Thursday";
	if (Day == "Fri") Day = "Friday";
	if (Day == "Sat") Day = "Saturday";	
	
	var Month = new String(strDate.substring(4,7)), MonthNumber = 0;
	if (Month == "Jan") { Month = "January"; MonthNumber = 1; }
	if (Month == "Feb") { Month = "February"; MonthNumber = 2; }
	if (Month == "Mar") { Month = "March"; MonthNumber = 3; }
	if (Month == "Apr") { Month = "April"; MonthNumber = 4; }
	if (Month == "May") { Month = "May"; MonthNumber = 5; }
	if (Month == "Jun") { Month = "June"; MonthNumber = 6; }
	if (Month == "Jul") { Month = "July"; MonthNumber = 7; }
	if (Month == "Aug") { Month = "August"; MonthNumber = 8; }
	if (Month == "Sep") { Month = "September"; MonthNumber = 9; }
	if (Month == "Oct") { Month = "October"; MonthNumber = 10; }
	if (Month == "Nov") { Month = "November"; MonthNumber = 11; }
	if (Month == "Dec") { Month = "December"; MonthNumber = 12; }
	
	var curPos = 11;
	var MonthDay = new String(strDate.substring(8,10));
	if (MonthDay.charAt(1) == " ") {
		MonthDay = "0" + MonthDay.charAt(0);
		curPos--;
	}	
	
	var MilitaryTime = new String(strDate.substring(curPos,curPos + 5));
	
	var Year = new String(strDate.substring(strDate.length - 4, strDate.length));	
	
	document.write(strDate + "");	

	// Format Type decision time!
	if (FormatType == 1)
		strDate = Day + ", " + Month + " " + MonthDay + ", " + Year;
	else if (FormatType == 2)
		strDate = MonthNumber + "/" + MonthDay + "/" + Year.substring(2,4);
	else if (FormatType == 3) {
		var AMPM = MilitaryTime.substring(0,2) >= 12 && MilitaryTime.substring(0,2) != "24" ? " PM" : " AM";
		if (MilitaryTime.substring(0,2) > 12)
			strDate = (MilitaryTime.substring(0,2) - 12) + ":" + MilitaryTime.substring(3,MilitaryTime.length) + AMPM;
		else {
			if (MilitaryTime.substring(0,2) < 10)
				strDate = MilitaryTime.substring(1,MilitaryTime.length) + AMPM;
			else
				strDate = MilitaryTime + AMPM;
		}
	}	
	else if (FormatType == 4)
		strDate = MilitaryTime;


	return strDate;
}


