/* ======================================================================
	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:	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:	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:	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:	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:	StripNonNumeric

	INPUT:  	str (string) - a string to be altered

	RETURN: 	a string containing only numeric characters 0-9;
				returns null if invalid arguments were passed

	DESC:		This function removes all non-numeric characters 
				from a given string.  It is useful for removing 
				dashes, parentheses, etc. from input strings such 
				as credit card numbers or phone nubmers.
====================================================================== */
function StripNonNumeric( str ) {
	var 	resultStr = "";

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)	
		return null;

	// Make sure the argument is a string
	str += "";

	// Loop through entire string, adding each character from the original
	// string if it is a number
	for (var i=0; i < str.length; i++) {
		if ( (str.charAt(i) >= "0") && (str.charAt(i) <= "9") )
			resultStr = resultStr + str.charAt(i);

	} // end for loop      

	return resultStr;
}  // end StripNonNumeric


/* ======================================================================
	FUNCTION:	IsValidPhone

	INPUT:		str (string) - a 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, incAreaCode ) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "") 
		return false;

	// Set default value for incAreaCode to false, if undefined or null
	if (incAreaCode+"" == "undefined" || incAreaCode+"" == "null") 
		incAreaCode = false;

	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+"");
	if (incAreaCode && str.length != 10)
		isValid = false;
	if (!incAreaCode && str.length != 7)
		isValid = false;

	return isValid;
} // end IsValidPhone


/* ======================================================================
	FUNCTION:	checkEmailAddress

	INPUT:		sEmail - String containing e-mail address

	RETURN:		true, if valid;
				false, otherwise.
====================================================================== */
function checkEmailAddress(sEmail) {
	var oRegExp = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/g;
	if (oRegExp.test(sEmail)) {
		return true;
	}
	else {
		return false;
	}
}


/* ======================================================================
	FUNCTION:	val_form

	INPUT:		form (string/number)   - Pointer to the form to be 
				validated

	RETURN:		true, if 
				false, otherwise.

	onSubmit = "this..notOptional=true; 
				this..isZipCode=true; 
				this..isPhone=true; 
				this..incAreaCode=(true|false);
				this..isInt=true; 
				this..allowNegatives=(true|false);
				this..isEmail=true;
				return val_form(this);"

====================================================================== */
function validateForm(form) {
	for (var i = 0; i < form.length; i++) {
		var lF = form.elements[i];	  // get input fields from form

		// Check for required field.
		if ( lF.notOptional && IsBlank(lF.value) ) {
			msg  = "____________________________________________________\n";
			msg += "The form was not submited because a required field is empty.\n\n";
			msg += "Please correct the error and re-submit.\n";
			msg += "____________________________________________________\n\n";
			alert(msg);
			lF.focus();
			lF.select();
			return false;
		}

		// Perform remaining checks if field is not blank 
		if (!IsBlank(lF.value)) {
			// Check integer fields.
			if ( lF.isInt && !IsInt(lF.value, lF.allowNegatives) ) {
				msg  = "____________________________________________________\n";
				msg += "The form was not submited because an Integer field contains an invalid character or is negative.\n\n";
				msg += "Please correct the error and re-submit.\n";
				msg += "____________________________________________________\n\n";
				alert(msg);

				lF.focus();
				lF.select();
				return false;
			}

			// Check date fields.
			if ( lF.isDate && !IsDate(lF) ) {
				msg  = "____________________________________________________\n";
				msg += "The form was not submited because a Date field is invalid.\n\n";
				msg += "Please correct the error and re-submit.\n";
				msg += "____________________________________________________\n\n";
				alert(msg);
				lF.focus();
				lF.select();
				return false;
			}

			// Check phone number fields (with or without area code acceptable).
			if ( lF.isPhone && !IsValidPhone(lF.value, lF.incAreaCode) ) {
				msg  = "____________________________________________________\n";
				msg += "The form was not submited because a Phone Number field is invalid.\n\n";
				msg += "Please correct the error and re-submit.\n";
				msg += "____________________________________________________\n\n";
				alert(msg);
				lF.focus();
				lF.select();
				return false;
			}

			// Check ZIP code fields.
			if ( lF.isZipCode && !IsValid5DigitZip(lF.value) && !IsValid5Plus4DigitZip(lF.value) ) {
				msg  = "____________________________________________________\n";
				msg += "The form was not submited because a ZIP or ZIP+4 field is invalid.\n\n";
				msg += "Please correct the error and re-submit.\n";
				msg += "____________________________________________________\n\n";
				alert(msg);
				lF.focus();
				lF.select();
				return false;
			}

			// Check e-mail address fields.
			if ( lF.isEmail && !checkEmailAddress(lF.value) ) {
				msg  = "____________________________________________________\n";
				msg += "The form was not submited because an e-mail address is invalid.\n\n";
				msg += "Please correct the error and re-submit.\n";
				msg += "____________________________________________________\n\n";
				alert(msg);
				lF.focus();
				lF.select();
				return false;
			}
		}
	}

	return true; //will return true if no input errors by user and form will be submitted
}


/* ======================================================================
	FUNCTION:	IsDate

	The following date formats are accepted:
	
		mm-dd-yyyy, mm/dd/yyyy, mm.dd.yyyy, mm dd yyyy, 
		mmm dd yyyy, mmddyyyy, m-d-yyyy, m/d/yyyy, m.d.yyyy, 
		m d yyyy, mmm d yyyy, m-d-yy, m/d/yy, m.d.yy, m d yy, 
		mmm d yy (yy is 20yy)

	INPUT:		objName - the field (not value) to be tested

	RETURN:		true, if the value is null;
				false, otherwise.
====================================================================== */
function IsDate(objName) {
	var strDatestyle = "US"; //United States date style; "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) {
		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 (IsDate_LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return false;
			}
		}
		else {
			if (intday > 28) {
				err = 10;
				return false;
			}
		}
	}

	if (strDatestyle == "US") {
		datefield.value = intMonth + "/" + intday +"/" + strYear;
	}
	else {
		datefield.value = intday + "/" + intMonth + "/" + strYear;
	}

	return true;
}

function IsDate_LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { 
			return true; 
		}
	}
	else {
		if ((intYear % 4) == 0) { 
			return true; 
		}
	}

	return false;
}

