/*
<script language="JavaScript">
	objFormRetailer = new glbDataValidation("myForm");
	function doValidation(){
		if(objFormRetailer.isFieldValid("LocationName","Location Name","Required")){
			return objFormRetailer.doErrorAlertPrompt()
		} else if(objFormRetailer.isFieldValid("Address1","Address 1","Required")){
			return objFormRetailer.doErrorAlertPrompt()
		} else if(objFormRetailer.isFieldValid("City","City","Required")){
			return objFormRetailer.doErrorAlertPrompt()
		} else if(objFormRetailer.isFieldValid("StateID","State","Required")){
			return objFormRetailer.doErrorAlertPrompt()
		} else if(objFormRetailer.isFieldValid("ZipCode","Zip Code","Required,NumOnly,LengthMin=5")){
			return objFormRetailer.doErrorAlertPrompt()
		} else if(objFormRetailer.isFieldValid("Phone","Phone","Required,TextOnly")){
			return objFormRetailer.doErrorAlertPrompt()
		}
	}
</script>

<form action="" method="post" name="myForm" onsubmit="return doValidation()">
</form>
*/

// ************************************************************************
// * Begin Data Validation constructor
// ************************************************************************

function glbDataValidation(paramFormName){
	/************************************************************
	* Begin configure error message
	************************************************************/
	var REQUIRED 				= "<fieldlabel> 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 				= "your <fieldlabel> is invalid. 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.isBlank = isBlank; // returns a true or false;
	/************************************************************
	* end public methods
	************************************************************/

	 var strFormName = paramFormName;
	 var strErrMessage = "";
	 

	function getErrorMessage(){
		return strErrMessage;
	}

	function doErrorAlertPrompt(){
		alert(strErrMessage);
		return false;
	}
	
	function setErrorMessage(msg){
		strErrMessage = msg;
	}

	function doErrorDetected(msg){
		strErrMessage = msg;
		blnErrorFlag = true;
	}
	
	
		
	function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		  
		   return false
		}
	
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		  
		   return false
		}
	
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   
			return false
		}
	
		 if (str.indexOf(at,(lat+1))!=-1){
		   
			return false
		 }
	
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   
			return false
		 }
	
		 if (str.indexOf(dot,(lat+2))==-1){
		   
			return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		   
			return false
		 }
	
		 return 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('document.'+strFormName+'.'+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'){
	
			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  
				if (!echeck(objField.value)	&& !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
	}

}


