/*

Author: Donny
Date: Prior to 2004
Desc: main validation file

Change 1: 
Author: Brian
Date: Aprill 12, 2005
Desc: I added in the isDateKey from OSI to replace the existing one - works better 

Change 2: 
Author: Brian
Date: Aprill 12, 2005
Desc: I added in 2x email validation functions
*/
var dateformat = "##-##-####";

var delimiter_date_RE = /#/gi;
var delimiter_date = dateformat.replace(delimiter_date_RE,'').charAt(0);
var delimiter_date_pos1 = dateformat.indexOf(delimiter_date);
var delimiter_date_pos2 = delimiter_date_pos1 + dateformat.substring(delimiter_date_pos1+1,dateformat.length-1).indexOf(delimiter_date) + 1;
function isDateKey(thisField,evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type dates
	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);

	if (theKey == 13)
		return true;
	else if (isNaN(theChar) || theChar == " " || (!document.selection.createRange().text.length && thisField.value.length >= 10))
		return false;
	else{
		if(document.selection.createRange().text.length)
			document.selection.createRange().text = '';
		var placeholder = "|";
		var original_value = thisField.value;
		document.selection.createRange().text = placeholder;
		var caretPos = thisField.value.indexOf(placeholder);
		
		original_value = original_value.substring(0,caretPos) + theChar + original_value.substring(caretPos,original_value.length)

		//Strip the delimiter
		var tempRE = new RegExp(delimiter_date, "gi");
		thisField.value = original_value.replace(tempRE,'');

		//Insert first delimiter if needed
		if (thisField.value.length >= delimiter_date_pos1){
			thisField.value = thisField.value.substring(0,delimiter_date_pos1) + delimiter_date + thisField.value.substring(delimiter_date_pos1,thisField.value.length);
			if(caretPos == delimiter_date_pos1-1) caretPos++;
		}

		//Insert second delimiter if needed
		if (thisField.value.length >= delimiter_date_pos2){
			thisField.value = thisField.value.substring(0,delimiter_date_pos2) + delimiter_date + thisField.value.substring(delimiter_date_pos2,delimiter_date_pos2+4);
			if(caretPos == delimiter_date_pos2-1) caretPos++;
		}
		var temp = thisField.createTextRange();
		temp.move("character",caretPos+1);
		temp.select();

		return false;
	}
}
//--- ****************************************************************************************************************** --->
function isValidEmail(thisEmail){
		var validEmailFormat = new RegExp("^[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]$", "gi");

		if(validEmailFormat.test(thisEmail))
			return true

		return false
}
//--- ****************************************************************************************************************** --->
function isValidEmailEmpty(thisEmail){
		var validEmailFormat = new RegExp("^[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]$", "gi");

		if(validEmailFormat.test(thisEmail))
			return true
			
		if(thisEmail=="")
			return true

		return false
}
//--- ****************************************************************************************************************** --->
/*
validate.js
Contains standard validation functions for HTML forms
*/

//Initialize the validation message
var valMsg = "";

function isNumberKey(evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type numbers

	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if ((theChar != "." && isNaN(theChar)) || theChar == " ")
		return false;
	else
		return true;
}
//--- ****************************************************************************************************************** --->
function isNumber(val){
	if ((val != "." && isNaN(val)) || val == " ")
		return false;
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isIntNumberKey(evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type positive integers
	
	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if ((isNaN(theChar)) || theChar == " ")
		return false;
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isIntNumberKeyNeg(evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type positive and negative integers

	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if ((theChar != "-" && isNaN(theChar)) || theChar == " ")
		return false;
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isPhoneKey(evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type numbers

	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if ((theChar != "-" && isNaN(theChar)) || theChar == " ")
		return false;
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
/*function isDateKey(thisField,evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type numbers and slashes in the correct position

	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if (((thisField.value.length < 4) || (thisField.value.length < 7 && thisField.value.length > 4) || (thisField.value.length > 7)) && (isNaN(theChar)) || theChar == " ")
		return false;
	else if (((thisField.value.length == 4) || (thisField.value.length == 7)) && theChar != "-")
		return false;
	else
		return true;
}*/
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function moveNextFld(thisField, fieldLen, nextField)
{
	//This function moves the focus to the next field if the current field is full
	if (thisField.value.length == fieldLen)
		nextField.focus();
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isBlank(theField,fieldName)
{
	//Used to check for empty fields
	if (theField.value.length < 1 || theField.value == "" || theField.value == null)
		valMsg = valMsg + "You must provide a value for " + fieldName + ".\n";
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isChecked(theField,fieldName)
{
	//Used to check for empty checkboxes
	if (!theField.checked)
		valMsg = valMsg + "You must check the " + fieldName + " box.\n";
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isRange(theField,fieldName,fromNo,toNo)
{
	//Used to check for numeric ranges in fields, does not check for blanks
	if ((theField.value.length > 0 || theField.value != "") && (theField.value < fromNo || theField.value > toNo))
		valMsg = valMsg + "The " + fieldName + " must be between " + fromNo + " and " + toNo + ".\n";
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isRange2(evt, field, from, to){
	//Used in the onKeyPress event of a form field, only allows the user to type positive and negative integers

	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if ((theChar != "." && theChar != "-" && isNaN(theChar)) || theChar == " ")
		return false;
	else{
		if((field.value + theChar) >= from && (field.value + theChar) <= to)
			return true;
		else
			return false;
	}		
}

//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function mustChoose(theField,fieldName)
{
	//Used to force a selection from a drop-down (with a default value of 0)
	if (theField.value == 0)
		valMsg = valMsg + "You must choose " + fieldName + ".\n";
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function confirmAction(entName,entValue,actionName)
{
	//Used to confirm the deletion of an item
	return confirm("Are you sure you want to " + actionName + "\n" + entName + ": " + entValue);
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function checkVal()
{
	//Used in the onSubmit event to check for any error messages
	if (valMsg != "")
	{
		alert(valMsg);
		return false;
	}
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function getToday(theField)
{
	//Used to populate a form field with today's date
	var theDate = new Date();
	theField.value =  (theDate.getMonth()+1) + "/" + theDate.getDate() + "/" + theDate.getFullYear();
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function textAreaMaxLength(theField,maxLength)
{
	//Used in the onKeyPress event of a textarea field, to provide a maxlength capability
	if (theField.value.length > maxLength - 1)
	{
		alert("You have reaced the limit of " + maxLength + " characters allowed in this field");
		return false;
	}
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function updateMessage(entType,entId)
{
	// Display a message informing the user that an update has been performed
	alert("Thank you. " + entType + " " + entId + " has been updated.")
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isIntNumberBlankKey(evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type positive integers
	
	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if (isNaN(theChar))
		return false;
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isPostalCodeForceFormatKey(thisField,evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type canadian postalcode

	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	// no space or longer than 6 char
	if  (theChar == " " || thisField.value.length > 6)
		return false;
	// must be in X6X 6X6 format
	// 1st, 3rd,  6th char is a character
	else if (!isNaN(theChar) && (thisField.value.length == 0 || thisField.value.length == 2 || thisField.value.length == 5))
		return false;
	// 2st, 4rd,  7th char is a number
	else if (isNaN(theChar) && (thisField.value.length == 1 || thisField.value.length == 3 || thisField.value.length == 6))
		return false;
	// automatic spacing in canadian postal code	
	else if (thisField.value.length == 3)
		thisField.value = thisField.value + ' ';
	else{
		thisField.value = thisField.value.toUpperCase();
		return true;
	}
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isZipCodeForceFormatKey(thisField,evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type US Zip Code
	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	// no space or longer than 5 char
	if  (theChar == " " || thisField.value.length > 4)
		return false;
	// must be all numbers: 12345 
	else if (isNaN(theChar))
		return false;
	else{
		return true;
	}
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->

/*
function isPhoneForceFormatKey(thisField,evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type numbers

	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if (isNaN(theChar) || theChar == " " || thisField.value.length > 12)
		return false;
	else{
		if (thisField.value.length == 3 || thisField.value.length == 7)
			thisField.value = thisField.value + "-";
		return true;
	}
}
*/

// (Jan 12, 2004) Donny: Updated version of Forcing Phone format
// - typing in mid string won't screw up the format
// - the 'dash' always stay at the right place
function isPhoneForceFormatKey(thisField,evt)
{
	//Used in the onKeyUp event of a form field, only allows the user to type phone numbers and extension
	// e.g. 555-555-5555 x567
	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	// - if user is play with 
	// the Home, End, arrow keys (theKey = 35 -> 40)
	// backspace, delete keys (theKey == 8, 46)
	if (theKey == 8 || theKey == 46 || (theKey >= 35 && theKey <= 40)){
		return true;
	} else {
		// strip value of any whilespace,-,x
		regex = /[^0-9]/gi; // global match of anything non-numeric (ignore case)
		thisField.value = thisField.value.replace(regex,'');
		// put in "dash" for phone no.
		if (thisField.value.length > 3)
			thisField.value = thisField.value.substr(0,3)+'-'+thisField.value.substr(3);
		// put in "dash" for phone no.
		if (thisField.value.length > 7)
			thisField.value = thisField.value.substr(0,7)+'-'+thisField.value.substr(7);
		// put in "extension" for phone no.
		if (thisField.value.length > 12)
			thisField.value = thisField.value.substr(0,12)+' x'+thisField.value.substr(12);
		return true;
	}
}

//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isDateForceFormatKey(thisField,evt)
{
	//Used in the onKeyUp event of a form field, only allows the user to type dates
	// e.g. 12-31-2003
	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	// - if user is play with 
	// the Home, End, arrow keys (theKey = 35 -> 40)
	// backspace, delete keys (theKey == 8, 46)
	// - return true and ignore it
	//	alert(theKey);
	if (theKey == 8 || theKey == 46 || (theKey >= 35 && theKey <= 40)){
		return true;
	} else {
		// strip value of any whilespace,-,x
		regex = /[^0-9]/gi; // global match of anything non-numeric (ignore case)
		thisField.value = thisField.value.replace(regex,'');
		// put in "dash" for date
		if (thisField.value.length > 2)
			thisField.value = thisField.value.substr(0,2)+'-'+thisField.value.substr(2);
		// put in "dash" for date
		if (thisField.value.length > 5)
			thisField.value = thisField.value.substr(0,5)+'-'+thisField.value.substr(5);
		return true;
	}
/*
	//Used in the onKeyPress event of a form field, only allows the user to type numbers
	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if (isNaN(theChar) || theChar == " " || thisField.value.length > 10)
		return false;
	else{
		if (thisField.value.length == 4 || thisField.value.length == 7)
			thisField.value = thisField.value + "-";
		return true;
	}
*/
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isValidPhone(thisField,fieldName)
{
	if (thisField.value.length){
		var error = false;
		if (thisField.value.length != 12){
			error = true;
		}
		else if (thisField.value.charAt(3) != "-" || thisField.value.charAt(7) != "-"){
			error = true;
		}
		else{				
			for (i=0;i<12;i++){			
				if ((i != 3 && i != 7) && thisField.value.charAt(i) == "-")
					error = true;				
			}
		}
		if (error) valMsg = valMsg + fieldName + " must be in the format ###-###-####.\n";
	}
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isFutureDate(thisField,fieldName,Today)
{
	if (thisField.value.length && thisField.value != 'yyyy-mm-dd'){
		var error = false;
		if (thisField.value.length != 10){
			error = true;
		}
		else if (thisField.value.charAt(4) != "-" || thisField.value.charAt(7) != "-"){
			error = true;
		}
		else{				
			for (i=0;i<10;i++){			
				if ((i != 4 && i != 7) && thisField.value.charAt(i) == "-")
					error = true;				
			}
		}
		if (error) valMsg = valMsg + fieldName + " must be in the format yyyy-mm-dd.\n";
		else{
			var CYear = Today.substring(0,4)-0;
			var CMonth = Today.substring(5,7)-0;
			var CDay = Today.substring(8,10)-0;
			var CDate = new Date(CYear,CMonth-1,CDay);			
			var Year = thisField.value.substring(0,4)-0;
			var Month = thisField.value.substring(5,7)-0;
			var Day = thisField.value.substring(8,10)-0;
			var TestDate = new Date(Year,Month-1,Day);
			if (Month != TestDate.getMonth()+1 || Day != TestDate.getDate() || Year != TestDate.getFullYear())
				valMsg = valMsg + "You must provide a valid value for "+fieldName+".\n";
			else if (Year < 1900)
				valMsg = valMsg + "You must provide a " + fieldName + " after the year 1900.\n";
			else if (TestDate > CDate)
				valMsg = valMsg + "You must provide a " + fieldName + " on or before "+Today.substring(0,4)+"-"+Today.substring(5,7)+"-"+Today.substring(8,10)+".\n";
		}

	}
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isValidDateRange(thisField1,fieldName1,thisField2,fieldName2)
{
	if (thisField1.value.length && thisField1.value != 'yyyy-mm-dd' && thisField2.value.length && thisField2.value != 'yyyy-mm-dd')
	{
		// Donny: check if text length is 10 char
		var error = false;
		if (thisField1.value.length != 10 || thisField2.value.length != 10){
			error = 1;
		}
		// Donny: check if - exist on pos 4 and 7 of the string
		else if (thisField1.value.charAt(4) != "-" || thisField1.value.charAt(7) != "-" || thisField2.value.charAt(4) != "-" || thisField2.value.charAt(7) != "-"){
			error = 2;
		}
		else{				
			for (i=0;i<10;i++){			
				if ((i != 4 && i != 7) && (thisField1.value.charAt(i) == "-" || thisField2.value.charAt(i) == "-"))
					error = 3;
			}
		}
		if (error){
			valMsg = valMsg + fieldName1 +" & " +fieldName2 + " must be in the format yyyy-mm-dd.\n";
		}else{
			var Year1 = thisField1.value.substring(0,4)-0;
			var Month1 = thisField1.value.substring(5,7)-0;
			var Day1 = thisField1.value.substring(8,10)-0;
			var Year2 = thisField2.value.substring(0,4)-0;
			var Month2 = thisField2.value.substring(5,7)-0;
			var Day2 = thisField2.value.substring(8,10)-0;
			var TestDate1 = new Date(Year1,Month1-1,Day1);
			var TestDate2 = new Date(Year2,Month2-1,Day2);
			if (Month1 != TestDate1.getMonth()+1 || Day1 != TestDate1.getDate() || Year1 != TestDate1.getFullYear() || Month2 != TestDate2.getMonth()+1 || Day2 != TestDate2.getDate() || Year2 != TestDate2.getFullYear())
				valMsg = valMsg + "You must provide a valid value for "+fieldName1+" & "+fieldName2+".\n";
			else if (Year1 < 1900 || Year2 < 1900)
				valMsg = valMsg + "You must provide a " + fieldName1 + " & " + fieldName2 + " after the year 1900.\n";
			else if (TestDate1 > TestDate2)
				valMsg = valMsg + fieldName1 + " must be before "+ fieldName2 +".\n";
		}
	}else{
		valMsg = valMsg + fieldName1 +" & " + fieldName2 + " must be in the format yyyy-mm-dd.\n";
	}
}
//--- ****************************************************************************************************************** --->
function isValidDate(thisDate){//alert("|"+thisDate+"|");
		var convertFormatToRE = new RegExp('#', "gi");
		var validDateFormat = new RegExp(dateformat.replace(convertFormatToRE,'[0-9]'), "gi");

		if(validDateFormat.test(thisDate)){
			var temp_Date = thisDate.split(delimiter_date);
			var temp_Year = temp_Date[2];
			var temp_Month = temp_Date[0];
			var temp_Day = temp_Date[1];
			var TestDate = new Date(temp_Year,temp_Month-1,temp_Day);
			if (temp_Month != TestDate.getMonth()+1 || temp_Day != TestDate.getDate() || temp_Year != TestDate.getFullYear() || temp_Year < 1900) return false

			return true
		}
		return false
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isCapitalize(thisField){
	var strLen = thisField.value.length;
	thisField.value = thisField.value.substring(0,1).toUpperCase() + thisField.value.substring(1,strLen);
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isNormalCharForceFormatKey(thisField,evt){
	//Used in the onKeyPress event of a form field, 
	//only allows the user to type characters that is not # < "
	//note: ASCII code '34' is double quote

	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if (theChar == "#" || theChar == "<" || theKey=="34")
		return false;
	else{
		return true;
	}
}
