<!--

function validateCreditCardNumber(type, ccnum) {
   if (type == "Visa") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "MC") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Disc") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "AmEx") {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   } else if (type == "Diners") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   }
   if (!re.test(ccnum)) return false;
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}

function validateNotEmpty(strValue) {
	//Validates that a string is not all blank (whitespace) characters. Utilizes trimAll function.
	//Returns true if valid, otherwise false.
	var strTemp = strValue;
	strTemp = trimAll(strTemp);
	if(strTemp.length > 0) {
		return true;
	}
	return false;
}

function trimAll(strValue) {
	//Removes leading and trailing spaces. Returns source string with whitespaces removed.
	var objRegExp = /^(\s*)$/;
	
	//check for all spaces
	if(objRegExp.test(strValue)) {
		strValue = strValue.replace(objRegExp, '');
		if(strValue.length == 0) {
			return strValue;
		}
	}
	
	//check for leading & trailing spaces
	objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
	if(objRegExp.test(strValue)) {
		//remove leading and trailing whitespace characters
		strValue = strValue.replace(objRegExp, '$2');
	}
	return strValue;
}

function validateRadioChecked(objRadio) {
	//Checks that one option of a set of radio buttons is checked and if so returns true,
	//otherwise false
	for (var r=0; r<objRadio.length; r++) {
		if (objRadio[r].checked) {
			return true;
		}
	}
	return false;
}

//Declare module level variable to be assigned in validateWFSIllegalChars function below and used in
//the alert message in when called from the validateForm function
var m_strIllegalCharsMsg
function validateWFSIllegalChars(strValue, blnNoIllegalChars) {
	//Validates that a string does not contain any of the WFS Illegal Characters as specified by the
	//data formatting rules of the WFS Standard Format Document version 3.1. Returns true if valid,
	//otherwise false.
	if (blnNoIllegalChars) {
		var objRegExp = /^[a-zA-Z0-9\s`]*$/;
		strIllegalCharsMsg = "Invalid characters cannot be entered in any field. Please try again.";
	}
	else {
		var objRegExp = /^[a-zA-Z0-9_\s`@\.\/\-\,\_]*$/;
		strIllegalCharsMsg = "Invalid characters cannot be entered in any field. Please try again.";
	}		
  	return objRegExp.test(strValue);
}

function validateInteger(strValue) {
	//Validates that a string contains only a valid positive integer number.
	//Returns true if valid, otherwise false.
	var objRegExp  = /^\d\d*$/;
	//check for integer characters
	return objRegExp.test(strValue);
}

function validateNumeric(strValue) {
//Validates that a string contains only valid numbers with NO decimals. Does
//not allow for negative numbers. Returns true if valid, otherwise false.
	//var objRegExp = /(^\d\d*\.\d*$)|(^\d\d*$)|(^\.\d\d*$)/;		//This pattern allows decimals
	var objRegExp = /^\d\d*$/;
	//check for numeric characters
	return objRegExp.test(strValue);
}

function validateNumericDecimal(strValue, intDecimalPlaces) {
//Validates that a string contains 1 or more valid numbers to the left of the decimal and requires the
//decimal with the specified number of digits following it. Returns true if valid, otherwise false.
	if(intDecimalPlaces == 2){
		var objRegExp = /^\d\d*\.\d{2}$/;
	}
	else if(intDecimalPlaces == 4){
		var objRegExp = /^\d\d*\.\d{4}$/;
	}
	//check for decimal characters
	return objRegExp.test(strValue);
}

function validatePhoneNumber(strValue, intField) {
//Validates for a valid phone number 1 field at a time when phone number exists in 3 separate fields.
//Returns true if individual phone number field is valid, otherwise false.
	if (intField == 1 || intField == 2) {
		var objRegExp = /^\d{3}$/;
	}
	else if (intField == 3) {
		var objRegExp = /^\d{4}$/;
	}
	//check for a valid phone number 1 field at a time
	return objRegExp.test(strValue);
}

//Declare module level variable to be used in validateEIN function below
var m_strEIN
function validateEIN(strValue, intField) {
//Validates for a valid employer identification number 1 field at a time when EIN exists in 2 separate fields.
//Returns true if individual EIN field is valid, otherwise false.
	switch(intField) {
		case 1:
			m_strEIN = strValue;	//start building EIN string
			//Check that first field is valid format						
			var objRegExp = /^\d{2}$/;
			return objRegExp.test(strValue)
		case 2:
			m_strEIN = m_strEIN + strValue;	//complete building of entire Ein string
			//Check that third field is valid format
			var objRegExp = /^\d{7}$/;
			if (!objRegExp.test(strValue)) {
				return false;
			}
			else {
				//Check that every character in entire EIN is not the same number
				var objRegExp = /^(1{9}|2{9}|3{9}|4{9}|5{9}|6{9}|7{9}|8{9}|9{9}|0{9})$/;
				if (objRegExp.test(m_strSSN)) {
					return false;
				}
				else {
					//Check that entire EIN is not 123456789
					var objRegExp = /123456789/;
					if (objRegExp.test(m_strSSN)) {
						return false;
					}
					else {
						//Check that entire EIN is not 987654321
						var objRegExp = /987654321/;
						if (objRegExp.test(m_strSSN)) {
							return false;
						}
					}
				}
			}
			return true;
	}
}

//Declare module level variable to be used in validateSSN function below
var m_strSSN
function validateSSN(strValue, intField) {
//Validates for a valid social security number 1 field at a time when SSN exists in 3 separate fields.
//Returns true if individual SSN field is valid, otherwise false.
	switch(intField) {
		case 1:
			m_strSSN = strValue;	//start building SSN string
			//Check that first field is valid format						
			var objRegExp = /^\d{3}$/;
			return objRegExp.test(strValue)
		case 2:
			m_strSSN = m_strSSN + strValue;	//continure building SSN string
			//Check that second field is valid format
			var objRegExp = /^\d{2}$/;
			return objRegExp.test(strValue);
		case 3:
			m_strSSN = m_strSSN + strValue;	//complete building of entire SSN string
			//Check that third field is valid format
			var objRegExp = /^\d{4}$/;
			if (!objRegExp.test(strValue)) {
				return false;
			}
			else {
				//Check that every character in entire SSN is not the same number
				var objRegExp = /^(1{9}|2{9}|3{9}|4{9}|5{9}|6{9}|7{9}|8{9}|9{9}|0{9})$/;
				if (objRegExp.test(m_strSSN)) {
					return false;
				}
				else {
					//Check that entire SSN is not 123456789
					var objRegExp = /123456789/;
					if (objRegExp.test(m_strSSN)) {
						return false;
					}
					else {
						//Check that entire SSN is not 987654321
						var objRegExp = /987654321/;
						if (objRegExp.test(m_strSSN)) {
							return false;
						}
					}
				}
			}
			return true;
	}
}

//Declare module level variables to be used in validateDate function below
var m_strMonth
var m_strDay
var m_strYear
function validateDate(strValue, intField) {
//Validates for a valid date 1 field at a time when date (birthdate, etc.) exists in 3 separate field in
//the mm/dd/yyyy format. Returns true if individual date field is valid, otherwise false. Also evaluates //the date in it's entirety and handles correct days per month including leap years! Returns false if
//entire date is invalid in any way.
	switch(intField) {
		case 1:
			m_strMonth = strValue;	//Assign value submitted in month field to month variable
			//Check for valid month format and that month value is valid
			var objRegExp = /^\d{2}$/;
			if (!objRegExp.test(m_strMonth) || m_strMonth < 1 || m_strMonth > 12) {
				return false;
			}
			else {
				break;
			}
		case 2:
			m_strDay = strValue;	//Assign value submitted in day field to day variable
			//Check for valid day format and that day value is at least 1 or greater
			var objRegExp = /^\d{2}$/;
			if (!objRegExp.test(m_strDay) || m_strDay < 1) {
				return false;
			}
			else {
				//Check that day value is valid according to month value submitted
				switch(m_strMonth) {
					case "01":
					case "03":
					case "05":
					case "07":
					case "08":
					case "10":
					case "12":
						if (m_strDay > 31) {
							return false;
						}
						else {
							break;
						}
					case "04":
					case "06":
					case "09":
					case "11":
						if (m_strDay > 30) {
							return false;
						}
				}
				break;
			}
		case 3:
			m_strYear = strValue;	//Assign value submitted in year field to year variable
			//Check for valid year format and that year is between 1800 and 2099(standard format 3.1
			//specification). Also, check that if month value is February that the day value is valid
			//depending on if it's a leap year or not.
			var objRegExp = /^\d{4}$/;
			if (!objRegExp.test(m_strYear) || m_strYear < 1800 || m_strYear > 2099) {
				return false;
			}
			else if (m_strMonth == "02" && ((m_strYear % 4 != 0 && m_strDay > 28) || (m_strYear % 4 == 0 && m_strDay > 29))) {
				return false;
			}
	}
	return true;
}

function validateZipCode(strValue) {
//Validates for a valid US Zipcode (5 digit format or zip -4 format). Returns true if valid Zipcode,
//otherwise returns false.
	var objRegExp  = /^(\d{5}(\d{4})?)$/;
	//check for valid US Zipcode
	return objRegExp.test(trimAll(strValue));
}

function validateEmail(strValue) {
//Validates for a valid email address format. Returns true if valid email, otherwise returns false.
	var objRegExp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$/;
	//check for valid email
	return objRegExp.test(strValue);
}

function validateTextArea(strValue, intMinWords, intMaxChars) {
//Validates a text area field VALUE to make sure it contains the minimum number of words
//and does not exceed the maximum number of characters passed in. Returns true if valid, otherwise
//returns false. NOTE: DOES NOT VALIDATE FOR NOT EMPTY! (Use the validateNotEmpty function for that!)
	var arrNumWords = strValue.split(/\s+/);
	if (arrNumWords.length >= intMinWords && strValue.length <= intMaxChars) {
		return true;
	}
	return false;
}

function validateEmployeeID(strValue) {
//Validates that the Employee ID submitted is valid.  It should either be the 5-digit Payroll ID or
//the 7-character temporary/contractor ID
	var objRegExp = /(^\d{5}$)|(^(tmp|con)\d{3}[a-zA-Z]{1}$)/i;
	return objRegExp.test(strValue);
}

function validatePassword(strValue) {
//Validate that the Password submitted is valid and meets the minimum requirements as set forth
//by IT Compliance.
	//Password cannot be the same as User ID.  Password must be minimum 7-characters, maximum 15, 
	//and consist of at least 1 alpha and at least 1 numeric character
	if(strValue.search(/[a-zA-Z]+/) != -1 && strValue.search(/\d+/) != -1 && (strValue.length >= 7 && strValue.length <= 9)){
		return true;
	}else{
		return false;
	}
}

function convertFieldName(strValue) {
//Used to replace the form field name with a name used for alert messages when using multiple cases 
//grouped together in a switch statement during validation of form fields.
//strValue is the name of the form field (e.g., AIFirstName, APIStreetName, etc.).
//intFormPrefix is the number of prefix characters for the form field that we need to drop
	//Split the string and create an array with each letter of the string, an element of the array
	var objRegExp = /[A-Z]/
	var arrString = strValue.split("")
	var strNewValue 
	
	//Find each instance of capital letters, except the first one and add a space in front of it.
	for(i=1;i<arrString.length;i++){
		if(objRegExp.test(arrString[i])){
			arrString[i] = " " + arrString[i];
		}
	}
	
	//Join the array to create a new string value that can be used in alert messages
	strNewValue = arrString.join("");						//Must specify no space as the delimiter
	return strNewValue;
}

function convertFromCamelBack(strValue) {
//Used to replace the form field name with a name used for alert messages when using multiple cases 
//grouped together in a switch statement during validation of form fields.
//strValue is the name of the form field (e.g., firstName, lastName, etc.).
//intFormPrefix is the number of prefix characters for the form field that we need to drop
	//Split the string and create an array with each letter of the string, an element of the array
	var objRegExp = /[A-Z]/
	var arrString = strValue.split("")
	var strNewValue 
	
	//Capitalize the first letter
	arrString[0] = arrString[0].toUpperCase();
	
	//Find each instance of subsequent capital letters and adds a space in front it.
	for(i=1;i<arrString.length;i++){
		if(objRegExp.test(arrString[i])){
			arrString[i] = " " + arrString[i];
		}
	}
	
	//Join the array to create a new string value that can be used in alert messages
	strNewValue = arrString.join("");						//Must specify no space as the delimiter
	return strNewValue;
}
//-->
