
//onclick="return validate(document.FORM-NAME);"


// addValidation function
// Description: 
//	- Adds to global array of validation objects to be checked on submit.
// Template:
//	  addValidation('fieldName' , 'displayName', 'fieldType', 'altMessage');
// Arguments:
//	- fieldName - name of form field, or array of form fields for checkOne validation type.
//	- displayName - name to use in alert message.
//	- fieldType - a string indicating the type of validation to perform
//		'text' - requires a length of greater than 0
//		'checkOne' - requires one of the fields in fieldName (an array) to be checked
//		'radio' - requires one of the radio button instances is selected
//		'select' - requires a value
//		'credit' - validates a credit card #, requires the ChkCard.js to be included
//		'email' - validates email 
//		'password' - validates that fieldName and fieldName & '2' have the same value
//		'number' - validates that fieldName and fieldName & '2' have the same value
//		'maxlength' - validates value not longer than value of validateThis.altMessage (useful for textarea's)
//		'minlength' - validate value is longer than value of validateThis.altMessage
//		'telephone' - validate value is in the form of a phone number
//		'ssn' - validate the value is in the form of a Social Security Number
//		'date' - validate the value can be accepted as a date, checks most date formats.
//		'time' - validate the value can be accepted as a time, checks am/pm and military.
//		'month' - validate the value is a month, checks numeric, abbreviations, and full month names.
//		'year' - validate the value is a year, assumes 2 digit years are 2000+
//		'ansi2oem' - formats text pasted from MS Word, etc. Requires the ansi2oem.js to be included
//	- altMessage - text to display instead of generic alert message, pass '' to use generic message.

	function addValidation (fieldName,displayName,fieldType,altMessage) {
		thingsToValidate[thingsToValidate.length] = new somethingToValidate(fieldName,displayName,fieldType,altMessage);
	}

	var thingsToValidate = new Array();

	function somethingToValidate (fieldName,displayName,fieldType,altMessage) {
		this.fieldName = fieldName;
		this.displayName = displayName;
		this.fieldType = fieldType;
		this.altMessage = altMessage;
	}

	// hands off validation for each field needing validation based on the validation type
	// to one of the functions below. add validation types as needed.
	function validate(frm) {
		// walk through all the validation objects that have been added
		for (var i = 0; i < thingsToValidate.length; i++) { 
			validateThis = thingsToValidate[i];			
			// calls the specific validation function for this
			// validation object based on the validationType...
			if (validateThis.fieldType=='credit') {
				if (!validate_credit(frm, validateThis)) {return false;};			
			} else {
				if (!eval('validate_' + validateThis.fieldType + '(frm,validateThis)')) {return false};
			}
		}
		return true;
	}

	//***************** Validation Functions ********************
	function validate_number(frm, validateThis) {
		var strValidChars = "0123456789";
		var strChar;
		var blnResult = true;
		var fld = eval('frm.' + validateThis.fieldName);
		var strString = fld.value;
		
		//  test strString consists of valid characters listed above
		for (i = 0; i < strString.length && blnResult == true; i++)
		  {
		  strChar = strString.charAt(i);
		  if (strValidChars.indexOf(strChar) == -1)
		     {
		     blnResult = false;
		     }
		  }
		if (!blnResult) {
			if (validateThis.altMessage=='') {
				alert('The value for ' + validateThis.displayName + ' must be a number.');
				
			}
		}
		return blnResult;
	}

	function validate_telephone(frm, validateThis) {    
		var fld = eval('frm.' + validateThis.fieldName);
		var aChar = null;
		var blnResult = true;
		
	
		//If the field is blank, return true to avoid alerting users that they have entered
		// a non valid Telephone when telephone is NOT required. Use MinLength Validation in
		// conjunction with Telephone validation to validate a required telephone number.		
		if(fld.value.length == 0) {
		    blnResult = true;
		}
		//If the field has some value in it, but it is less than 10 chars, it is not a phone number.
		else if (fld.value.length < 10) {
			blnResult = false;
		}
		
		//For each character in the field value, check if it is a valid character for a phone number.
		for(var i=0; i < fld.value.length; i++) {
			aChar = fld.value.charAt(i);
			
			if (!(aChar=="(" || aChar==")" || aChar=="-" || (parseInt(aChar, 10) >= 0 && parseInt(aChar, 10) <= 9 ))) {
				blnResult = false;
			}
		}
	
		if (blnResult == false) {
			alert('Please enter a valid ' + validateThis.displayName + '.');
			fld.focus();
		}
		
		return blnResult;
	}
	
	
	function validate_password(frm, validateThis) {
		fld = eval('frm.' + validateThis.fieldName);
		fld2 = eval('frm.' + validateThis.fieldName + '2');		
		if (fld.value!=fld2.value) {
			if (validateThis.altMessage == '') {
				alert('The two passwords entered do not match.');
			} else {
				alert(validateThis.altMessage);
			}
			fld.focus();
			return false;
		}
		return true;
	}
	

	function validate_email (frm, validateThis) {
		var fld = eval('frm.' + validateThis.fieldName);
		if (!testEmail(fld.value)) {
			if (validateThis.altMessage == '') {
				alert('Please enter a valid email address.');
			} else {
				alert(validateThis.altMessage);
			}
			fld.focus();
			return false;
		}
		return true;
	}

	
	function validate_credit(frm, validateThis) {
		if (!CheckCardNumber(frm)) {
			if (validateThis.altMessage == '') {
				alert('Please enter a valid credit card number.');
			} else {
				alert(validateThis.altMessage);
			}
			frm.fld_credit_number.focus();
			return false;
		} else {
			return true;
		}
	}

	function validate_text(frm, validateThis) {
		fld = eval('frm.' + validateThis.fieldName);
		if (fld.value=="") {
			if (validateThis.altMessage == '') {
				alert('Please enter a value for ' + validateThis.displayName + '.');
			} else {
				alert(validateThis.altMessage);
			}
			fld.focus();
			return false;
		}
		return true;
	}

	function validate_maxlength(frm, validateThis) {
		fld = eval('frm.' + validateThis.fieldName);
		if (fld.value.length>validateThis.altMessage) {
			alert('The value in the ' + validateThis.displayName + ' field is too long. The maximum length is ' + validateThis.altMessage + ' characters.');
			fld.focus();
			return false;
		}
		return true;
	}
	
	function validate_minlength(frm, validateThis) {
		fld = eval('frm.' + validateThis.fieldName);
		if (fld.value.length<validateThis.altMessage) {
			alert('The value in the ' + validateThis.displayName + ' field is not complete. Please enter at least ' + validateThis.altMessage + ' characters.');
			fld.focus();
			return false;
		}
		return true;
	}

	function validate_radio(frm, validateThis) {
		fld = eval('frm.' + validateThis.fieldName);
		if (fld.length==null) {
			if (fld.checked) return true;
		} else {
			for (i=0 ; i<fld.length ; i++) {
				if (fld[i].checked) return true;
			}
		}
		if (validateThis.altMessage == '') {
			alert('Please select an option for ' + validateThis.displayName + '.');
		} else { 
			alert(validateThis.altMessage);
		}
		if (fld.length==null) {
			fld.focus();
		} else {
			fld[0].focus();
		}
		return false;
	}
	
	function validate_checkOne (frm, validateThis) {
		oneChecked = false;
		fld = eval('frm.' + validateThis.fieldName);
		for (var x = 0; x < fld.length; x++) {
			if (fld[x].checked) {
				oneChecked = true;
			}
		}
		if (!oneChecked) {
			if (validateThis.altMessage == '') {
				alert('Please check at least one box under ' + validateThis.displayName + '.');
			} else {
				alert(validateThis.altMessage);
			}
			return false;
		}
		return true;
	}

	function validate_select (frm, validateThis) {
		oneChecked = false;
		fld = eval('frm.' + validateThis.fieldName);
		if (fld[fld.selectedIndex].value == '') {
			if (validateThis.altMessage == '') {
				alert('Please make a selection for ' + validateThis.displayName + '.');				
			} else {
				alert(validateThis.altMessage);
			}
			fld.focus();
			return false;
		}
		return true;
	}

	function validate_ssn(frm, validateThis) {
		fld = eval('frm.' + validateThis.fieldName);
		if (fld.value.length!=9) {
			//alert('The value in the ' + validateThis.displayName + ' field is too long. The maximum length is ' + validateThis.altMessage + ' characters.');
			alert('Please enter a valid 9 digit Social Security Number.');
			fld.focus();
			return false;
		}
		return true;
	}
		
	function validate_date(frm, validateThis) {
		fld = eval('frm.' + validateThis.fieldName);
		if (checkDate(fld) == false) {
			fld.select();
			alert('The date entered for ' + validateThis.displayName + ' is invalid.  Please try again.');
			fld.focus();
			return false;
		}
		else {
			return true;
		}
	}
	
	function validate_time(frm, validateThis) {
		var timefield = validateThis.fieldName;
		if (checkTime(validateThis.fieldName) == false) {
			timefield.select();
			alert('The time entered for ' + validateThis.displayName + ' is invalid.  Please try again.');
			timefield.focus();
			return false;
		}
		else {
			return true;
		}
	}
		
	function validate_month(frm, validateThis) {
		var monthfield = validateThis.fieldname;
		if (checkMonth(validateThis.fieldname) == false) {
			monthfield.select();
			alert('The month entered for ' + validateThis.displayName + ' is invalid.  Please try again.');
			monthfield.focus();
			return false;
		}
		else {
			return true;
		}
	}
	function validate_year(frm, validateThis) {
		var yearfield = validateThis.fieldname;
		if (checkYear(validateThis.fieldname) == false) {
			yearfield.select();
			alert('The year entered for ' + validateThis.displayName + ' is invalid.  Please try again.');
			yearfield.focus();
			return false;
		}
		else {
			return true;
		}
	}
	
	function validate_ansi2oem(frm,validateThis) {
		fld = eval('frm.' + validateThis.fieldName);
		fld.value = ansi2oem ( fld.value );	
	}
	
	//************ Supporting Functions ********************
	
	// Used in validate_email
	function testEmail(email) {
		var firstChar, lastChar, atPos, dotPos, legalChars;
		legalChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-';
		firstChar = email.charAt(0);
		if (legalChars.indexOf(firstChar)==-1) return false;
		lastChar = email.charAt(email.length-1);
		if (legalChars.indexOf(lastChar)==-1) return false;
		atPos = email.indexOf('@');
		if (atPos==-1) return false;
		dotPos = email.indexOf('.',atPos);
		if (dotPos==-1) return false;
		if (dotPos==atPos+1) return false;
		return true;
	}
	
	//Used in validate_month, validate_date
	function checkMonth(strMonth) {
		var intMonth;
		var strMonthArray = new Array(12);
		var strLongMonthArray = new Array(12);
		
		strLongMonthArray[0] = "January";
		strLongMonthArray[1] = "February";
		strLongMonthArray[2] = "March";
		strLongMonthArray[3] = "April";
		strLongMonthArray[4] = "May";
		strLongMonthArray[5] = "June";
		strLongMonthArray[6] = "July";
		strLongMonthArray[7] = "August";
		strLongMonthArray[8] = "September";
		strLongMonthArray[9] = "October";
		strLongMonthArray[10] = "November";
		strLongMonthArray[11] = "December";		
		
		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";
		
		
		intMonth = parseInt(strMonth, 10);
		
		//If intMonth is not a number, check the array of month abbreviations.
		if (isNaN(intMonth)) {
			//Check Month abbreviation array first
			for (i = 0;i<12;i++) {
				if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
					intMonth = i+1;
					//If the month is found, set strMonth to its numeric equivalent
					strMonth = strMonthArray[i];
					i = 12;
		  		}
			}
		
			//Check full Month name array next
			for (i = 0;i<12;i++) {
				if (strMonth.toUpperCase() == strLongMonthArray[i].toUpperCase()) {
					intMonth = i+1;
					//If the month is found, set strMonth to its numeric equivalent
					strMonth = strLongMonthArray[i];
					i = 12;
		  		}
			}		
			//if intMonth is still not a number, then it was not found in either array.
			if (isNaN(intMonth)) return false;
		}
		
		//At this point in the function, intMonth must be a number, check to see
		//it falls in the valid range.
		if (intMonth>12 || intMonth<1) return false;
		
		return true;
	}

	//Used in validate_year, validate_date	
	function checkYear(strYear) {
		var intYear;
		
		//Assume 2 digit years are 2000+
		if (strYear.length == 2) strYear = '20' + strYear;
		
		intYear = parseInt(strYear, 10);
		
		if (isNaN(intYear)) return false;	
		
		return true;
	}
	
	//Used in validate_date	
	function checkDate(datefield) {
		var strDatestyle = "US"; //United States date style
		//var strDatestyle = "EU";  //European date style
		var strDate, strDateArray, strDay, strMonth, strYear, intday;
		var intMonth, intYear
		
		var booFound = false;
		var strSeparatorArray = new Array("/");
		var intElementNr;
		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) {
					return false;
				}
				else {
					strDay = strDateArray[0];
					strMonth = strDateArray[1];
					strYear = strDateArray[2];
				}
				booFound = true;
		   	}
		}
		
		if (booFound == false) {
			return false;
		}
		
		// US style
		if (strDatestyle == "US") {
			strTemp = strDay;
			strDay = strMonth;
			strMonth = strTemp;
		}
		
		intday = parseInt(strDay, 10);
		
		if (isNaN(intday)) return false;
		
		//Call checkMonth Function to determine if month is valid.
		if (checkMonth(strMonth) == false) return false;
		
		//Call checkYear Function to determine if year is valid.
		if (checkYear(strYear) == false) return false;
		
		intMonth = parseInt(strMonth, 10);
		
		intYear = parseInt(strYear, 10);
	
		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) return false;

			if (LeapYear(intYear) == true) {
				if (intday > 29) return false;
			}
			else {
				if (intday > 28) return false;
			}
		}
		
		if (strDatestyle == "US") {
		//	datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
		}
		else {
			datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
		}
		
		return true;
	}
		
	//Used in validate_date		
	function LeapYear(intYear) {
		if (intYear % 100 == 0) {
			if (intYear % 400 == 0) { return true; }
		}
		else {
			if ((intYear % 4) == 0) { return true; }
		}
		
		return false;
	}

	//Used in validate_time		
	function checkTime(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) return false;
				
		if  (hour > 12 && ampm != null) return false;
	
		if (minute<0 || minute > 59) return false;
	
		if (second != null && (second < 0 || second > 59)) return false;
	
		return true;
	}
