


function ValidateDate(strDate)
{
/* 
Called from ValidateFormFields
Valid date field with either formats: MM/DD/YYYY  MM-DD-YYYY 
*/

	var ValidDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	var strMatchArray = strDate.match(ValidDatePattern); 
	if (strMatchArray == null) {
		return false;
	}
	month = strMatchArray[1];
	day = strMatchArray[3];
	year = strMatchArray[4];
	if (year < 1901 || year > 2077) {
		return false;
	}
	if (month < 1 || month > 12) {
		return false;
	}
	if (day < 1 || day > 31) {
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		return false;
	}
	if (month == 2) { 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			return false;
	   }
	}
	return true;  
}


function ValidateURL(strURL)
{
/* 
Called from ValidateFormFields
Valid URL field to make sure it contains 'http://' at the beginning
*/
/* Commented out this code asking for http:// since it should not be required.
   Other matching code should eventually be entered

	var strLowerURL = strURL.toLowerCase();
	
	if (strLowerURL.substring(0,7) != "http://") {
		return false;
	}
*/
	return true;
}


function TrimField(strField)
{
	while(strField.charAt(strField.length-1)==' '){
		strField=strField.substring(0,strField.length-1);
	}
	return strField
}

function ValidateField(objFormElement, validateString, alertDesc)
{
/* This section was copied from ValidateFormFields to allow validating one field at a 
   time instead of the whole form and without having to change the form field names*/

	var ValidEmailPattern=/^(.+)@(.+)$/


/* Trim the Field of any spaces*/
	objFormElement.value = TrimField(objFormElement.value);

/* Validate Required Fields */
	if ( (validateString.substring(0,9) == "REQUIRED_") ||  (validateString.substring(0,4) == "REQ_") ) {	/* Required AlphaNumeric field */
		if ( ((objFormElement.type=="text" || objFormElement.type=="textarea") && objFormElement.value=='') ||	/*TEXT or TEXTAREA data type*/
		     (objFormElement.type.toString().charAt(0)=="s" && objFormElement.selectedIndex==0) ) {				/*SELECT data type*/
			alert(alertDesc +" is Required.");
			objFormElement.focus()
			return false;
		}
	}											

/* Validate Numeric */                                
	if ( (validateString.substring(0,11) == "REQUIRED_N_") || (validateString.substring(0,11) == "OPTIONAL_N_") ||  (validateString.substring(0,6) == "REQ_N_") ||  (validateString.substring(0,6) == "OPT_N_") ) {		/* Must be Numeric */
		if ( (objFormElement.type=="text" || objFormElement.type=="textarea") && isNaN(objFormElement.value) ) {			
			alert(alertDesc +" Must be Numeric.");
			objFormElement.focus()
			return false;
		}
	}
			

/* Validate Required Minimum Length*/
	if ( (validateString.substring(0,08) == "REQ_A_M_") || (validateString.substring(0,08) == "REQ_N_M_") ) {		/* Must have Min length */
		if ( (objFormElement.type=="text" || objFormElement.type=="textarea") && (objFormElement.value.length < validateString.substring(08,10)) ) {			
			alert(alertDesc +" Must be at least " + validateString.substring(08,10) + " long.");
			objFormElement.focus()
			return false;
		}
	}

/* Validate Optional Minimum Length*/
	if ( (validateString.substring(0,08) == "OPT_A_M_") || (validateString.substring(0,08) == "OPT_N_M_") ) {		/* Must have Min length or null*/
		if ( (objFormElement.type=="text" || objFormElement.type=="textarea") && (objFormElement.value.length > 0) && (objFormElement.value.length < validateString.substring(08,10)) ) {			
			alert(alertDesc +" Must be at least " + validateString.substring(08,10) + " long or blank.");
			objFormElement.focus()
			return false;
		}
	}
						
/* Validate Email */
	if ( (validateString.substring(0,11) == "REQUIRED_E_") || (validateString.substring(0,11) == "OPTIONAL_E_") ) {		/* Must be valid Email */
		if ( (objFormElement.type=="text" || objFormElement.type=="textarea") && 
			 (((objFormElement.value != "") ) && (objFormElement.value.match(ValidEmailPattern)== null)) ) {			
			alert(alertDesc +" not a valid Email Format.");
			objFormElement.focus()
			return false;
		}
	}
			
/* Validate Date */
	if ( (validateString.substring(0,11) == "REQUIRED_D_") || (validateString.substring(0,11) == "OPTIONAL_D_") ) {		/* Must be valid date */
		if ( (objFormElement.type=="text" || objFormElement.type=="textarea") && 
			 ( (objFormElement.value != "")  && (!ValidateDate(objFormElement.value)) ) ) {			
			alert(alertDesc +" is Not a valid Date Format.");
			objFormElement.focus()
			return false;
		}
	}
					 
/* Validate URL */
	if ( (validateString.substring(0,11) == "REQUIRED_U_") || (validateString.substring(0,11) == "OPTIONAL_U_") ) {		/* Must be valid URL */
		if ( (objFormElement.type=="text" || objFormElement.type=="textarea") && 
			 ( (objFormElement.value != "")  && (!ValidateURL(objFormElement.value)) ) ) {			
			alert(alertDesc +" is Not a valid URL Format."); /* Make sure you include 'HTTP://' at the beginning.");*/
			objFormElement.focus()
			return false;
		}
	}
	
	return true;
}

function ValidateFormFields(objForm) 
{
/*
Used by all ASPs that require generic form validation
Validates all fields in a form that are named with the following prefixes:
	REQUIRED_A_			:	Required Alphanumeric
	OPTIONAL_A_			:	Optional Alphanumeric
	REQUIRED_N_			:	REQUIRED Numeric
	OPTIONAL_N_			:	Optional Numeric
	REQ_A_M_xx_			:	Required Alphanumeric with Minimumn of xx length
	OPT_A_M_xx_			:	Optional Alphanumeric with Minimumn of xx length
	REQ_N_M_xx_			:	REQUIRED Numeric with Minimumn of xx length
	OPT_N_M_xx_			:	Optional Numeric with Minimumn of xx length
	REQUIRED_E_			:	REQUIRED Email
	OPTIONAL_E_			:	Optional Email
	REQUIRED_D_			:	REQUIRED Date
	OPTIONAL_D_			:	Optional Date
	REQUIRED_U_			:	REQUIRED URL
	OPTIONAL_U_			:	Optional URL
*/

	var blnValid = true;
	var ValidEmailPattern=/^.+@.+\..{2,3}$/
	//"/^(.+)@(.+)$/"

	if (document.images) {
		for (i=0; i<objForm.length; i++) {
			var objFormElement = objForm.elements[i];
			if (objFormElement.type!="button" && objFormElement.type!="submit" && objFormElement.type!="reset") {
				objFormElement.value = TrimField(objFormElement.value);
			}
/* Validate Required Fields */
			if ( (objFormElement.name.substring(0,9) == "REQUIRED_") ||  (objFormElement.name.substring(0,4) == "REQ_") ) {						/* Required AlphaNumeric field */
				if ( ((objFormElement.type=="text" || objFormElement.type=="textarea") && objFormElement.value=='') ||	/*TEXT or TEXTAREA data type*/
				     (objFormElement.type.toString().charAt(0)=="s" && objFormElement.selectedIndex==0) ) {				/*SELECT data type*/
					alert(objFormElement.name.substring(11,40) +" is Required.");
					objFormElement.focus()
					blnValid=false;
					break;
				}
			}											

/* Validate Numeric */                                
			if ( (objFormElement.name.substring(0,11) == "REQUIRED_N_") || (objFormElement.name.substring(0,11) == "OPTIONAL_N_") ||  (objFormElement.name.substring(0,6) == "REQ_N_") ||  (objFormElement.name.substring(0,6) == "OPT_N_") ) {		/* Must be Numeric */
				if ( (objFormElement.type=="text" || objFormElement.type=="textarea") && isNaN(objFormElement.value) ) {			
					alert(objFormElement.name.substring(11,40) +" Must be Numeric.");
					objFormElement.focus()
					blnValid=false;
					break;
				}
			}
			

/* Validate Required Minimum Length*/
			if ( (objFormElement.name.substring(0,08) == "REQ_A_M_") || (objFormElement.name.substring(0,08) == "REQ_N_M_") ) {		/* Must have Min length */
				if ( (objFormElement.type=="text" || objFormElement.type=="textarea") && (objFormElement.value.length < objFormElement.name.substring(08,10)) ) {			
					alert(objFormElement.name.substring(11,40) +" Must be at least " + objFormElement.name.substring(08,10) + " long.");
					objFormElement.focus()
					blnValid=false;
					break;
				}
			}

/* Validate Optional Minimum Length*/
			if ( (objFormElement.name.substring(0,08) == "OPT_A_M_") || (objFormElement.name.substring(0,08) == "OPT_N_M_") ) {		/* Must have Min length or null*/
				if ( (objFormElement.type=="text" || objFormElement.type=="textarea") && (objFormElement.value.length > 0) && (objFormElement.value.length < objFormElement.name.substring(08,10)) ) {			
					alert(objFormElement.name.substring(11,40) +" Must be at least " + objFormElement.name.substring(08,10) + " long or blank.");
					objFormElement.focus()
					blnValid=false;
					break;
				}
			}
						
/* Validate Email */
			if ( (objFormElement.name.substring(0,11) == "REQUIRED_E_") || (objFormElement.name.substring(0,11) == "OPTIONAL_E_") ) {		/* Must be valid Email */
				if ( (objFormElement.type=="text" || objFormElement.type=="textarea") && 
					 (((objFormElement.value != "") ) && (objFormElement.value.match(ValidEmailPattern)== null)) ) {			
					alert(objFormElement.name.substring(11,40) +" not a valid Email Format.");
					objFormElement.focus()
					blnValid=false;
					break;
				}
			}
			
/* Validate Date */
			if ( (objFormElement.name.substring(0,11) == "REQUIRED_D_") || (objFormElement.name.substring(0,11) == "OPTIONAL_D_") ) {		/* Must be valid date */
				if ( (objFormElement.type=="text" || objFormElement.type=="textarea") && 
					 ( (objFormElement.value != "")  && (!ValidateDate(objFormElement.value)) ) ) {			
					alert(objFormElement.name.substring(11,40) +" is Not a valid Date Format.");
					objFormElement.focus()
					blnValid=false;
					break;
				}
			}
					 
/* Validate URL */
			if ( (objFormElement.name.substring(0,11) == "REQUIRED_U_") || (objFormElement.name.substring(0,11) == "OPTIONAL_U_") ) {		/* Must be valid URL */
				if ( (objFormElement.type=="text" || objFormElement.type=="textarea") && 
					 ( (objFormElement.value != "")  && (!ValidateURL(objFormElement.value)) ) ) {			
					alert(objFormElement.name.substring(11,40) +" is Not a valid URL Format."); /* Make sure you include 'HTTP://' at the beginning.");*/
					objFormElement.focus()
					blnValid=false;
					break;
				}
			}
					 
		}
	}
	return blnValid;
}


function CompareDates(strDate1, strDate2)
{
/* 
Called from any screens that require dates comparisons
Date fields formats should be: MM/DD/YYYY  MM-DD-YYYY 
Returns:	
	Null: if invalid dates
	1	: if strDate1 > strDate2
	0	: if strDate1 = strDate2
	-1	: if strDate1 < strDate2
*/

	var ValidDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

	var strMatchArray1 = strDate1.match(ValidDatePattern); 
	if (strMatchArray1 == null) {
		return null;
	}
	var month1 = strMatchArray1[1];
	var day1 = strMatchArray1[3];
	var year1 = strMatchArray1[4];
	
	var strMatchArray2 = strDate2.match(ValidDatePattern); 
	if (strMatchArray2 == null) {
		return null;
	}
	var month2 = strMatchArray2[1];
	var day2 = strMatchArray2[3];
	var year2 = strMatchArray2[4];
	
	if (parseInt(year1,10) == parseInt(year2,10)) {
		if (parseInt(month1,10) == parseInt(month2,10)) {
			if (parseInt(day1,10) == parseInt(day2,10)) {
				return 0;
			}
			else {
				if (parseInt(day1,10) > parseInt(day2,10)) {return 1;} else {return -1;}
			}
		}
		else {
			if (parseInt(month1,10) > parseInt(month2,10)) {return 1;} else {return -1;}
		}
	}
	else {
		if (parseInt(year1,10) > parseInt(year2,10)) {return 1;} else {return -1;}
	}
	
	return null;		
}



/***************************************************************************
 * Screen Specific Validation goes below                                   *
 ***************************************************************************/ 

function ValidateRecordLeave(objForm)
{
/*
This function is not currently used by this system.  It was left here as an example.
*/
	if (!ValidateFormFields(objForm)){
		return false;
		}
	var blnDate = CompareDates(objForm.REQUIRED_D_StartDate.value, objForm.REQUIRED_D_EndDate.value)
	if ( (blnDate != -1) &&				//Start > End
		 (blnDate != 0) ) {				//Start != End
		alert("Leave End Date Must Be Greater Than or Equal to Leave Start Date.");
		return false;
		}
	return true;
}



/*
	The code below is used when you want to warn the user that the changes they made on
	the screen won't be saved unless they press the "save" button.  Every field on the 
	screen must set the blnStateModified variable to true in the onModify event.
	
	This code is currently not being used.
*/

var blnStateModified = false
		
function ConfirmSelection()
{
	if (blnStateModified){
		if (confirm("Are you sure you want to proceed without saving the changes you made?")){
			return true;
		}
		else{
			return false;
		}
	}
	else{
		return true;
	}	
}



