// *********************************************************************
// * Created Date: 12/23/2003                                          *
// * Modified Date : 02/05/2004                                        *
// * Version: Data Validation version 2.0                              *
// * Created By: Steven Ramos                                          *
// * Modified By: Steven Ramos                                         *
// * File: glb_datavalidation_002.js                                   *
// *********************************************************************

// ************************************************************************
// * Begin Data Validation constructor
// ************************************************************************

function glbDataValidation(paramFormName){
	/************************************************************
	* Begin configure error message
	************************************************************/
	var REQUIRED 				= "The <fieldlabel> field is required";
	var LENGTHMIN 				= "The <fieldlabel> field can not contain fewer than <length> characters.";
	var LENGTHMAX 				= "The <fieldlabel> field can not contain more than <length> characters.";
	var TEXTONLY 				= "The <fieldlabel> field must contain text only.";
	var NUMONLY 				= "The <fieldlabel> field must contain only numbers.";
	var NUMONLY_LT_MAXIMUM 		= "The <fieldlabel> field must be a number less than or equal to <maximum>.";
	var NUMONLY_GT_MINIMUM 		= "The <fieldlabel> field must be a number greater than or equal to <minimum>.";
	var ISMONEY 				= "The <fieldlabel> field must contain a currency format such as 200,000.00.";
	var ISMONEY_DOLLARSIGN		= "The <fieldlabel> field contains a dollar sign. Remove it and try again.";
	var ISMONEY_LT_MAXIMUM 		= "The <fieldlabel> field must be a number less than or equal to <maximum>.";
	var ISMONEY_GT_MINIMUM 		= "The <fieldlabel> field must be a number greater than or equal to <minimum>.";
	var ISNUMFORMAT				= "The <fieldlabel> field must contain only numbers.";
	var ISNUMFORMAT_LT_MAXIMUM 	= "The <fieldlabel> field must be a number less than or equal to <maximum>.";
	var ISNUMFORMAT_GT_MINIMUM 	= "The <fieldlabel> field must be a number greater than or equal to <minimum>.";
	var ALPHACHARONLY 			= "The <fieldlabel> field  must contain only letters from A-Z.";
	var NOCHARONLY 				= "The <fieldlabel> field field  must contain only letters from A-Z and numbers from 0-9";
	var ISEMAIL 				= "The <fieldlabel> field contains an invalid email address\. Example: myname@domain\.com";
	var COMPAREFIELD			= "The <fieldlabel>  does not confirm. Check the spelling then try again";	
	var ISUSERNAME 				= "The <fieldlabel> field is an invalid user name\.";	
		ISUSERNAME 				+= "The <fieldlabel> accepts letters from A-Z, numbers from 0-9, \'_\', and \'-\'\.";
		ISUSERNAME 				+= "The <fieldlabel> must contain more than 4 and less than 30 characters\.";
	var ISPASSWORD				= "The <fieldlabel> field is an invalid password\.";	
		ISPASSWORD 				+= " The field only accepts letters from A-Z, numbers from 0-9, \'_\', and \'-\'\.";
		ISPASSWORD 				+= " The field must contain more than 4 and less than 30 characters\.";

	/************************************************************
	* End configure error message
	************************************************************/

	/************************************************************
	* Begin public methods
	************************************************************/
	this.isFieldValid = isFieldValid; 
	this.getErrorMessage = getErrorMessage; 
	this.doErrorAlertPrompt = doErrorAlertPrompt; 
	this.getFieldObject = getFieldObject;
	this.isBlank = isBlank; // returns a true or false;
	/************************************************************
	* End public methods
	************************************************************/

	/************************************************************
	* Begin create the form object if the client passes an object 
	* or an form name;
	************************************************************/
	var objForm = null;
	if(typeof paramFormName == 'object'){ // if the client passes a form object
		objForm = paramFormName;
	} else { // if the user passes a form name
		objForm = eval('document.'+paramFormName);
	}
	/************************************************************
	* End create the form object if the client passes an object 
	************************************************************/
	
	/************************************************************
	* Begin variables/objects used by methods through out the component.  
	************************************************************/
	 var  objField = null;
	 var strErrMessage = "";
	/************************************************************
	* End variables/objects used by methods through out the component.  
	************************************************************/
	 

	function getErrorMessage(){
		return strErrMessage;
	}

	function doErrorAlertPrompt(){
		alert(strErrMessage);
		return false;
	}
	
	function setErrorMessage(msg){
		strErrMessage = msg;
	}

	function getFieldObject(){
		return objField;
	}


	function doErrorDetected(msg){
		strErrMessage = msg;
		blnErrorFlag = true;
	}

	function isBlank(s){ // This function is used to check for blank field
		var checkchar = s.replace(/\s*\n*\t*\r*/g,'');
		if (checkchar == ''){
		return true;
			}	else { 
				return false;
		}
	}

	function isFieldValid(frmFieldName,frmFriendlyName,frmValidate){
		objField = eval('objForm'+'.'+frmFieldName);
		blnErrorFlag = false;

		// ************************************************************************
		// * Begin prepare the field object variables use for validation
		// ************************************************************************

		var objTextProp 				= new Object();
		objTextProp.frmFieldType	= objField.type;

		frmValidate = frmValidate.toLowerCase()


		if(!objTextProp.frmFieldType){
			objTextProp.frmFieldType = 'checked' ;
		}		
		// ************************************************************************
		// End prepare the field object variables use for validation
		// ************************************************************************
		// ************************************************************************
		// *Begin Only Text, Textarea and Password fields can access the following code
		// ************************************************************************
	
		if (objTextProp.frmFieldType == 'text' || objTextProp.frmFieldType == 'textarea' || 
			objTextProp.frmFieldType == 'password' || objTextProp.frmFieldType == 'hidden' ||
			objTextProp.frmFieldType == 'file'){
			
			
			if (blnErrorFlag == false && frmValidate.match(/required/g)){
				if (isBlank(objField.value)){			
					msg = REQUIRED.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					doErrorDetected(msg);
				}
			} 
			
	
			if (blnErrorFlag == false && frmValidate.match(/lengthmin=\d*/g)){
				var minlength = eval('"'+frmValidate.match(/lengthmin=\d*/g)+'"'); 
				minlength = parseInt(minlength.match(/\d+/));
				if (!isBlank(objField.value) && objField.value.length < minlength){
					msg = LENGTHMIN.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					msg = msg.replace('<length>',minlength);
					doErrorDetected(msg);
				}
			}
	
			if (blnErrorFlag == false && frmValidate.match(/lengthmax=\d*/g)){
				var maxlength = eval('"'+frmValidate.match(/lengthmax=\d*/g)+'"'); 
				 maxlength = parseInt(maxlength.match(/\d+/));
				if (!isBlank(objField.value) && objField.value.length > maxlength){
					msg = LENGTHMAX.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					msg = msg.replace('<length>',maxlength);
					doErrorDetected(msg);
				}
			}
	
			if (blnErrorFlag == false && frmValidate.match(/textonly/g)){
				if (!isNaN(objField.value) && !isBlank(objField.value)){			
					msg = TEXTONLY.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					doErrorDetected(msg);
				}
			}
	
			// Begin only number validations
			 if (blnErrorFlag == false && frmValidate.match(/numonly/g)){
				if (isNaN(objField.value.replace(/[\.]/g,'a')) && !isBlank(objField.value) ){
					msg = NUMONLY.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					doErrorDetected(msg);
				}
	
				if (!isNaN(objField.value.replace(/[\.]/g,'a')) 
					&& !isBlank(objField.value) ){
					tmpMinMaxValue = frmValidate.match(/numonly\[[\d]+,[\d]+\]/g);
	
					if(tmpMinMaxValue){
					// Begin build the array
					tmpMinMaxValue2 = ''+frmValidate.match(/numonly\[[\d]+,[\d]+\]/g);
					tmpMinMaxValue2 = eval(tmpMinMaxValue2.replace(/numonly/,''));
					tmpMinValue  = tmpMinMaxValue2[0] // Min
					tmpMaxValue  = tmpMinMaxValue2[1] // Max
					// End build the array
	
						// Begin check to see if the developer has the number range correct.
						// Else check the validation.
						if(tmpMinValue > tmpMaxValue){
							alert('Component Error: The Minimun can not greater that the Maximum');
							return false;
						// Begin check to see if the developer has the number range correct.
	
						} else {
							if(objField.value.replace(/[\.]/g,'a') < tmpMinValue){
								msg = NUMONLY_GT_MINIMUM.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
								msg = msg.replace('<minimum>',tmpMinValue);
								doErrorDetected(msg);
							} else if(objField.value.replace(/[\.]/g,'a') > tmpMaxValue){
								msg = NUMONLY_LT_MAXIMUM.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
								msg = msg.replace('<maximum>',tmpMaxValue);
								doErrorDetected(msg);
							}
						}
					} 
				}
			}
			// End only number validations
	
			// Begin money format.
			if (blnErrorFlag == false && frmValidate.match(/ismoney/g)){
				if (isNaN(objField.value.replace(/[,$]/g,'')) 
					&& !isBlank(objField.value)){
					msg = ISMONEY.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					doErrorDetected(msg);
				} else if(objField.value.match(/[$]/g)) {
					msg = ISMONEY_DOLLARSIGN.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					doErrorDetected(msg);
				}
	
				if (!blnErrorFlag){
					tmpMinMaxValue = frmValidate.match(/ismoney\[[\d]+,[\d]+\]/g);
	
					if(tmpMinMaxValue){
					// Begin build the array
					tmpMinMaxValue2 = ''+frmValidate.match(/ismoney\[[\d]+,[\d]+\]/g);
					tmpMinMaxValue2 = eval(tmpMinMaxValue2.replace(/ismoney/,''));
					tmpMinValue  = tmpMinMaxValue2[0] // Min
					tmpMaxValue  = tmpMinMaxValue2[1] // Max
					// End build the array
	
						// Begin check to see if the developer has the number range correct.
						// Else check the validation.
						if(tmpMinValue > tmpMaxValue){
							alert('Component Error: The Minimun can not greater that the Maximum');
							return false;
						} else {
						// End check to see if the developer has the number range correct.
	
							if(objField.value.replace(/[,$]/g,'') < tmpMinValue){
								msg = ISMONEY_GT_MINIMUM.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
								msg = msg.replace('<minimum>',tmpMinValue);
								doErrorDetected(msg);
							} else if(objField.value.replace(/[,$]/g,'') > tmpMaxValue){
								msg = ISMONEY_LT_MAXIMUM.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
								msg = msg.replace('<maximum>',tmpMinValue);
								doErrorDetected(msg);
							}
						}
					} 
				}
			}
			// End money format.
	
			// Begin Number Format
			if (blnErrorFlag == false && frmValidate.match(/isnumformat/g)){
	
				if (isNaN(objField.value.replace(/[,]/g,'')) 
						&& !isBlank(objField.value) ){
					msg = ISNUMFORMAT.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					doErrorDetected(msg);
				}
	
				if (!blnErrorFlag){
					tmpMinMaxValue = frmValidate.match(/isnumformat\[[\d]+,[\d]+\]/g);
	
					if(tmpMinMaxValue){
					// Begin build the array
					tmpMinMaxValue2 = ''+frmValidate.match(/isnumformat\[[\d]+,[\d]+\]/g);
					tmpMinMaxValue2 = eval(tmpMinMaxValue2.replace(/isnumformat/,''));
					tmpMinValue  = tmpMinMaxValue2[0] // Min
					tmpMaxValue  = tmpMinMaxValue2[1] // Max
					// End build the array
	
	
						if(tmpMinValue > tmpMaxValue){
							alert('Component Error: The Minimun can not greater that the Maximum');
							return false;
						} else {
	
							if(objField.value.replace(/[,]/g,'') < tmpMinValue){
								msg = ISNUMFORMAT_GT_MINIMUM.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
								msg = msg.replace('<minimum>',tmpMinValue);
								doErrorDetected(msg);
							} else if(objField.value.replace(/[,]/g,'') > tmpMaxValue){
								msg = ISNUMFORMAT_GT_MINIMUM.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
								msg = msg.replace('<minimum>',tmpMaxValue);
								doErrorDetected(msg);
							}
						}
					} 
				}
			}
			//End Number Format
			if (blnErrorFlag == false && frmValidate.match(/alphacharonly/g)){
				if (objField.value.replace(/(([a-z])*)(([A-Z])*)/g,'') != '' 
					&& !isBlank(objField.value)){			
					msg = ALPHACHARONLY.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					doErrorDetected(msg);
				}
			} 
			if (blnErrorFlag == false && frmValidate.match(/nocharonly/g)){
				if (eval(objField.value.replace(/(([a-z])*)(([A-Z])*)(\d*)/g,'')) != '' 
						&& !isBlank(objField.value)){			
					msg = NOCHARONLY.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					doErrorDetected(msg);
				}
			} 
			if (blnErrorFlag == false && frmValidate.match(/isemail/g)){
				if (eval(objField.value.match(/\w@\w+\.\w{2,3}/g)) == null  
						&& !isBlank(objField.value)){
					msg = ISEMAIL.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					doErrorDetected(msg);
				}
			} 
			if (blnErrorFlag == false && frmValidate.match(/comparefield/g)){
				var txtFieldObject = eval('"'+frmValidate.match(/comparefield=\w*.\w*.\w*/g)+'"')
					txtFieldObject = txtFieldObject.replace(/CompareField=/,'')
				if (eval(objField.value) != eval(txtFieldObject+'.value')){
					msg = COMPAREFIELD.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					doErrorDetected(msg);
				}
			}		 
			if (blnErrorFlag == false && frmValidate.match(/isusername/g)){
				if (
					(
						(objField.value.match(/[aA-zZ|0-9|_|-]+/g)
						!= objField.value)
					 	&& !isBlank(objField.value)
					 )
					 ||
					 (
					 (objField.value.length < 4) || (objField.value.length > 30))
				){
					msg = ISUSERNAME.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					doErrorDetected(msg);
				}
			} 
			if (blnErrorFlag == false && frmValidate.match(/ispassword/g)){
				if (
					(
					(objField.value.match(/[aA-zZ|0-9|_|-]+/g) 
						!= objField.value)
					 && !isBlank(objField.value)
					 )
					 ||
					 (
					 (objField.value).length < 4 || eval(objField.value.length > 30)
					 )
				){			
					msg = ISPASSWORD.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					doErrorDetected(msg);
				}
			}
		}

		// ************************************************************************
		// *End Only Text, Textarea and Password fields can access the following code
		// ************************************************************************

		// ************************************************************************
		// * Only OPTION fields can access the following code
		// ************************************************************************
		if (objTextProp.frmFieldType == 'select-multiple' || objTextProp.frmFieldType == 'select-one'){
	
		if (blnErrorFlag == false && frmValidate.match(/required/g)){
			var strSelectedValue = '';
			var strSelectedLength = 0;
			for (var i = 0; i < objField.length; i++) { // Loop through the selected items to create the strSelectedValue 
				if (objField.options[i].selected){
				strSelectedValue = strSelectedValue += objField.options[i].value;
				}
			}
	
			if (strSelectedValue == ''){
					msg = REQUIRED.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
				doErrorDetected(msg);
			}
		}  
	
		if (blnErrorFlag == false && frmValidate.match(/lengthmin=\d*/g)){
			var strSelectedLength = 0;
			var minlength = eval('"'+frmValidate.match(/lengthmin=\d*/g)+'"'); 
			 minlength = parseInt(minlength.match(/\d+/));
			for (var i = 0; i < objField.length; i++) { // Loop through the selected items to create the strSelectedValue 
				if (objField.options[i].selected){
				++strSelectedLength;
				}
			}
	
			if (strSelectedLength < minlength){
				msg = LENGTHMIN.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
				msg = msg.replace('<length>',minlength);
				if(minlength == 1){
					msg += ".";
				} else {
					msg += "s.";
				}
				doErrorDetected(msg);
			}
		} 
		
		if (blnErrorFlag == false && frmValidate.match(/lengthmax=\d*/g)){
			var strSelectedLength = 0;
			var maxlength = eval('"'+frmValidate.match(/lengthmax=\d*/g)+'"'); 
			 maxlength = parseInt(maxlength.match(/\d+/));
	
			for (var i = 0; i < objField.length; i++) { // Loop through the selected items to create the strSelectedValue 
				if (objField.options[i].selected){
				strSelectedLength = strSelectedLength+1;
				}
			}
			if (strSelectedLength > maxlength ){
				msg = LENGTHMAX.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
				msg = msg.replace('<length>',maxlength);
				doErrorDetected(msg);
				}
			}
		}
		// ************************************************************************
		// * End of Only OPTION fields can access the following code
		// ************************************************************************
	
		// ************************************************************************
		// * Only radio and check box fields can access the following code
		// ************************************************************************
		if (objTextProp.frmFieldType == 'checked'){
			if (blnErrorFlag == false && frmValidate.match(/required/g)){
				var strSelectedValue = '';
	
				for (i = 0, j = objField.length; i < j; i++) { // Loop through the selected items to create the strSelectedValue 
					if (objField[i].checked == true){
						strSelectedValue = strSelectedValue += objField[i].value;
					}
				}
	
				if (strSelectedValue == ''){
					msg = REQUIRED.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					doErrorDetected(msg);
				}
	
			} 
			
		if (blnErrorFlag == false && frmValidate.match(/lengthmin=\d*/g)){
				var strSelectedLength = 0
				var minlength = eval('"'+frmValidate.match(/lengthmin=\d*/g)+'"'); 
				 minlength = parseInt(minlength.match(/\d+/));
	
				for (i = 0, j = objField.length; i < j; i++) { // Loop through the selected items to create the strSelectedValue 
					if (objField[i].checked == true){
						++strSelectedLength; 
					}
				}
	
				if (strSelectedLength < minlength){
					msg = LENGTHMIN.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					msg = msg.replace('<length>',minlength);
					if(minlength == 1){
						msg += ".";
					} else {
						msg += "s.";
					}
					doErrorDetected(msg);
				}
			}
			 
			 if (blnErrorFlag == false && frmValidate.match(/lengthmax=\d*/g)){
				var strSelectedLength = 0
				var maxlength = eval('"'+frmValidate.match(/lengthmax=\d*/g)+'"'); 
				 maxlength = parseInt(maxlength.match(/\d+/));
	
				for (i = 0, j = objField.length; i < j; i++) { // Loop through the selected items to create the strSelectedValue 
					if (objField[i].checked == true){
						++strSelectedLength; 
					}
				}
	
				if (strSelectedLength > maxlength){
					msg = LENGTHMAX.replace('<fieldlabel>',frmFriendlyName.toUpperCase());
					msg = msg.replace('<length>',maxlength);
					doErrorDetected(msg);
				}
			}
		}
		// ************************************************************************
		// * End of only radio and check box fields can access the following code
		// ************************************************************************

	// Begn the return value sent back let the client know the field pass or failed
	// the validation
	return blnErrorFlag;
	// End the return value sent back let the client know the field pass or failed
	}

}



function Confirmit(mess) {
		var confirmVar=confirm(mess);
		return confirmVar;
		
	}	
	
// ************************************************************************
	// * A new function added to check for valid dates
	// ************************************************************************

	
	
	function isDate(dateStr) {

		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
		var matchArray = dateStr.match(datePat); // is the format ok?
		
		if (matchArray == null) {
		alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
		return false;
		}
		
		month = matchArray[1]; // p@rse date into variables
		day = matchArray[3];
		year = matchArray[5];
		
		if (month < 1 || month > 12) { // check month range
		alert("Month must be between 1 and 12.");
		return false;
		}
		
		if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
		return false;
		}
		
		if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn`t have 31 days!")
		return false;
		}
		
		if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
		alert("February " + year + " doesn`t have " + day + " days!");
		return false;
		}
		}
		return true; // date is valid
	}

	function isEuroDate(dateStr) {

		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
		var matchArray = dateStr.match(datePat); // is the format ok?
		
		if (matchArray == null) {
		alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
		return false;
		}
		
		day = matchArray[1]; // p@rse date into variables
		month = matchArray[3];
		year = matchArray[5];
		
		if (month < 1 || month > 12) { // check month range
		alert("Month must be between 1 and 12.");
		return false;
		}
		
		if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
		return false;
		}
		
		if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn`t have 31 days!")
		return false;
		}
		
		if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
		alert("February " + year + " doesn`t have " + day + " days!");
		return false;
		}
		}
		return true; // date is valid
	}


