


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
*/
	var strLowerURL = strURL.toLowerCase();
	
	if (strLowerURL.substring(0,7) != "http://") {
		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=/^(.+)@(.+)$/

	if (document.images) {
		for (i=0; i<objForm.length; i++) {
			var objFormElement = objForm.elements[i];

/* 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,30) +" 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,30) +" 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(09,10)) ) {			
					alert(objFormElement.name.substring(11,40) +" Must be at least " + objFormElement.name.substring(09,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(09,10)) ) {			
					alert(objFormElement.name.substring(11,40) +" Must be at least " + objFormElement.name.substring(09,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,30) +" 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,30) +" 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,30) +" 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
*/
//alert("DEBUG: start="+strDate1+"  end="+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];
//alert("DEBUG: start: month="+parseInt(month1)+"  day="+parseInt(day1)+"  year="+parseInt(year1));
	
	var strMatchArray2 = strDate2.match(ValidDatePattern); 
	if (strMatchArray2 == null) {
		return null;
	}
	var month2 = strMatchArray2[1];
	var day2 = strMatchArray2[3];
	var year2 = strMatchArray2[4];
//alert("DEBUG: end:  day="+day2+" day(parsed,10)="+parseInt(day2,10));

/*	TMS 9/18/2000
	This logic doesn't work.  New logic below.
	
	if ((year1 == year2) && (month1 == month2) && (day1 == day2)){
		return 0
		}
	if (year1 > year2) {return 1;} else {return -1;}
	if (month1 > month2) {return 1;} else {return -1;}
	if (day1 > day2) {return 1;} else {return -1;}
*/
	
	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 ValidateLibDocUpload(objForm)
{
/*
Used by lib_doc_upload.asp for extra validation
*/
	var strFileName		 = objForm.LdCustomFileName.value
	/*var strFileExtension = objForm.FileFormat.value
	var isIEBrowser = (navigator.userAgent.indexOf('MSIE') != -1)*/
	
	if (!ValidateFormFields(objForm)){
		return false;
		}
	if ( (objForm.LdCustomFileName.value == "") &&
	     ((objForm.REQUIRED_U_Offsite_URL.value == "") ||
	      (objForm.REQUIRED_U_Offsite_URL.value == "http://")) )     {
		alert("Please enter an Upload Document value");
		objForm.LdCustomFileName.focus();
		return false;
		}

/*	if ( (objForm.LdCustomFileName.value != "") && (isIEBrowser) &&
		(strFileName.substring(strFileName.lastIndexOf(".")+1).toLowerCase() != strFileExtension) ){
		alert("Please select a document format that matches your selected upload file type");
		objForm.LdCustomFileName.focus();
		return false;
		}
*/

	objForm.HiddenLdCustomFileName.value	= objForm.LdCustomFileName.value

}


function ValidateSetupLibDocType(objForm)
{
/*
Used by SetupLibDoc.asp for extra validation
*/								  
	var strFileName		 = objForm.LdCustomFileName.value
	var strFileExtension = objForm.FileFormat.value
	var isIEBrowser = (navigator.userAgent.indexOf('MSIE') != -1)

	if (!ValidateFormFields(objForm)){
		return false;
		}
	if (objForm.LdCustomFileName.value == "") {
		alert("Please enter an Upload Document value");
		objForm.LdCustomFileName.focus();
		return false;
		}
	if ( (objForm.LdCustomFileName.value != "") && (isIEBrowser) &&
		(strFileName.substring(strFileName.lastIndexOf(".")+1).toLowerCase() != strFileExtension) ){
		alert("Please select a document format that matches your selected upload file type");
		objForm.LdCustomFileName.focus();
		return false;
		}
	objForm.HiddenLdCustomFileName.value	= objForm.LdCustomFileName.value
}



function ValidateRecordLeave(objForm)
{
/*
Used by record_leave.asp for extra validation
*/
	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;
}



function ValidateUpdateEmployeeData(objForm)
{
/*
Used by update_employee_data.asp for extra validation
*/								  

	if (!ValidateFormFields(objForm)){
		return false;
		}
		
	if ( ((objForm.OPT_N_M_03_HomePhone1.value != "") ||  (objForm.OPT_N_M_03_HomePhone2.value != "") || (objForm.OPT_N_M_04_HomePhone3.value != "")) &&
		 ((objForm.OPT_N_M_03_HomePhone1.value == "") ||  (objForm.OPT_N_M_03_HomePhone2.value == "") || (objForm.OPT_N_M_04_HomePhone3.value == "")) ){
		alert("Please complete all phone number entries");
		objForm.OPT_N_M_03_HomePhone1.focus();
		return false;
		}
	if ( ((objForm.OPT_N_M_03_BusPhone1.value != "") ||  (objForm.OPT_N_M_03_BusPhone2.value != "") || (objForm.OPT_N_M_04_BusPhone3.value != "")) &&
		 ((objForm.OPT_N_M_03_BusPhone1.value == "") ||  (objForm.OPT_N_M_03_BusPhone2.value == "") || (objForm.OPT_N_M_04_BusPhone3.value == "")) ){
		alert("Please complete all phone number entries");
		objForm.OPT_N_M_03_BusPhone1.focus();
		return false;
		}
	if ( ((objForm.OPT_N_M_03_ContactPhone1.value != "") ||  (objForm.OPT_N_M_03_ContactPhone2.value != "") || (objForm.OPT_N_M_04_ContactPhone3.value != "")) &&
		 ((objForm.OPT_N_M_03_ContactPhone1.value == "") ||  (objForm.OPT_N_M_03_ContactPhone2.value == "") || (objForm.OPT_N_M_04_ContactPhone3.value == "")) ){
		alert("Please complete all phone number entries");
		objForm.OPT_N_M_03_ContactPhone1.focus();
		return false;
		}

	/* TMS 7/5/2000 - don't allow administrators to change their own privilege level */
	if ((objForm.sessEmpID.value == objForm.strEmpID.value) && 
			(objForm.Access_Level.selectedIndex < 2)) {		/* selectIndex: 0 = "3", 1 = "6", 2 = "9" */
		alert("You cannot change the access level for yourself.  You must use a different administrator ID.");
		return false;
		}

	/* TMS 9/27/2000 - don't allow administrators to deactivate themselves */
	if ((objForm.sessEmpID.value == objForm.strEmpID.value) && 
			!(objForm.EmpActive.checked)) {
		alert("You cannot deactivate your own account.  You must use a different administrator ID.");
		return false;
		}

	return true;
}


function ValidateUpdateIntacctEmployeeData(objForm)
{
/*
Used by intacct_employee_setup.asp for extra validation
*/								  

	if (!ValidateFormFields(objForm)){
		return false;
		}
		
	if ( ((objForm.OPT_N_M_03_ContactPhone1.value != "") ||  (objForm.OPT_N_M_03_ContactPhone2.value != "") || (objForm.OPT_N_M_04_ContactPhone3.value != "")) &&
		 ((objForm.OPT_N_M_03_ContactPhone1.value == "") ||  (objForm.OPT_N_M_03_ContactPhone2.value == "") || (objForm.OPT_N_M_04_ContactPhone3.value == "")) ){
		alert("Please complete all phone number entries");
		objForm.OPT_N_M_03_ContactPhone1.focus();
		return false;
		}

	/* TMS 7/5/2000 - don't allow administrators to change their own privilege level */
	if ((objForm.sessEmpID.value == objForm.strEmpID.value) && 
			(objForm.Access_Level.value < 9)) {
		alert("You cannot change the access level for yourself.  You must use a different administrator ID.");
		return false;
		}

	return true;
}


/* 
TMS 9/18/2000 
Used by edit_company_su.asp for extra validation
*/
function ValidateEditCompanySU(objForm)
{
	if (!ValidateFormFields(objForm)){
		return false;
		}
		
	if (objForm.OPTIONAL_D_Exp_Date.value != "") {
		var today = new Date()
		var strToday = today.getMonth()+1 + '/' + today.getDate() + '/' + today.getYear()
		var blnDate = CompareDates(objForm.OPTIONAL_D_Exp_Date.value, strToday)
		if ( (blnDate <= 0) &&					//exp_date <= today
			 (objForm.CompActive.checked) ) {	//Active is checked
			alert("A company cannot be marked as 'Active' with an expiration date equal to or before today.");
			return false;
			}
		}
		
	return true;
}


function ValidateDeptEdit(objForm)
{
/*
Used by update_dept_edit.asp for extra validation
*/
	if (!ValidateFormFields(objForm)){
		return false;
		}
	if ( objForm.MgrID.value == 0 ) {				// No manager selected
		alert("You must select a manager for this department.");
		return false;
		}
	return true;
}




var blnStateModified = false
		
function ConfirmSelection()
{
/*
Used by update_employee_data.asp for employee dropdown validation
*/								  
	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;
	}	
}



