function Validate(oForm) {
	var VALString;
	var VALArray;
	var VALField;
	var VALErrMessage;
	var VALPhoneTypeField;
	var ReturnValue = true;
	
	
	
	/////////////////////////////////////
	// Validate Required CheckBox Fields
	/////////////////////////////////////

	//First Check for an array of values
	//alert (oForm.name);
	if(oForm.VALCheckboxRequired) {
		if(oForm.VALCheckboxRequired.length) {
			var idx;
			for (var idx=0;idx < oForm.VALCheckboxRequired.length ;idx++) {
				VALString = oForm.VALCheckboxRequired[idx].value;
				VALArray = VALString.split(",");
				VALField = VALArray[0];
				VALErrMessage = VALArray[1];
				if (!ValidateRequiredCheckbox(oForm, VALField,VALErrMessage)) {
					ReturnValue = false;
					break;
				}
			}
		}
		else {
			VALString = oForm.VALCheckboxRequired.value;
			VALArray = VALString.split(",");
			VALField = VALArray[0];
			VALErrMessage = VALArray[1];
			if (!ValidateRequiredCheckbox(oForm, VALField,VALErrMessage)) {
				ReturnValue = false;
			}
		}
	}

	if(!ReturnValue) {
		return ReturnValue;
	}

	/////////////////////////////////////
	//Validate Required Text Fields
	/////////////////////////////////////

	//First Check for an array of values
	if (oForm.VALTextRequired) {
		if(oForm.VALTextRequired.length) {
			var idx;
			for (var idx=0;idx < oForm.VALTextRequired.length ;idx++) {
				VALString = oForm.VALTextRequired[idx].value;
				VALArray = VALString.split(",");
				VALField = VALArray[0];
				VALErrMessage = VALArray[1];
				if (!ValidateRequiredText(oForm, VALField,VALErrMessage)) {
					ReturnValue = false;
					break;
				}
			}
		}
		else {
			
			VALString = oForm.VALTextRequired.value;
			VALArray = VALString.split(",");
			VALField = VALArray[0];
			VALErrMessage = VALArray[1];
			if (!ValidateRequiredText(oForm, VALField,VALErrMessage)) {
				ReturnValue = false;
			}
		}
	}
	
	if(!ReturnValue) {
		return ReturnValue;
	}
	/////////////////////////////////////
	// Validate Required Select Fields
	/////////////////////////////////////

	//First Check for an array of values
	if(oForm.VALSelectRequired) {
		if(oForm.VALSelectRequired.length) {
			var idx;
			for (var idx=0;idx < oForm.VALSelectRequired.length ;idx++) {
				VALString = oForm.VALSelectRequired[idx].value;
				VALArray = VALString.split(",");
				VALField = VALArray[0];
				VALErrMessage = VALArray[1];
				if (!ValidateRequiredSelect(oForm, VALField,VALErrMessage)) {
					ReturnValue = false;
					break;
				}
			}
		}
		else {
			VALString = oForm.VALSelectRequired.value;
			VALArray = VALString.split(",");
			VALField = VALArray[0];
			VALErrMessage = VALArray[1];
			if (!ValidateRequiredSelect(oForm, VALField,VALErrMessage)) {
				ReturnValue = false;
			}
		}
	}

	if(!ReturnValue) {
		return ReturnValue;
	}



	
	/////////////////////////////////////
	//Validate Valid Date Fields
	/////////////////////////////////////
	
	//First Check for an array of values
	if(oForm.VALValidDate) {
		if(oForm.VALValidDate.length) {
			var idx;
			for (var idx=0;idx < oForm.VALValidDate.length ;idx++) {
				VALString = oForm.VALValidDate[idx].value;
				VALArray = VALString.split(",");
				VALField = VALArray[0];
				VALErrMessage = VALArray[1];
				if (!ValidateValidDate(oForm, VALField,VALErrMessage)) {
					ReturnValue = false;
					break;
				}
			}
		}
		else {
			VALString = oForm.VALValidDate.value;
			VALArray = VALString.split(",");
			VALField = VALArray[0];
			VALErrMessage = VALArray[1];
			if (!ValidateValidDate(oForm, VALField,VALErrMessage)) {
				ReturnValue = false;
			}
		}
	}


	if(!ReturnValue) {
		return ReturnValue;
	}




	/////////////////////////////////////
	//Validate Valid Email Fields
	/////////////////////////////////////
	//First Check for an array of values
	if(oForm.VALValidEmail) {
		if(oForm.VALValidEmail.length) {
			var idx;
			for (var idx=0;idx < oForm.VALValidEmail.length ;idx++) {
				VALString = oForm.VALValidEmail[idx].value;
				VALArray = VALString.split(",");
				VALField = VALArray[0];
				VALErrMessage = VALArray[1];
				if (!ValidateValidEmail(oForm, VALField, VALErrMessage)) {
					ReturnValue = false;
					break;
				}
			}
		}
		else {
			VALString = oForm.VALValidEmail.value;
			VALArray = VALString.split(",");
			VALField = VALArray[0];
			VALErrMessage = VALArray[1];
			if (!ValidateValidEmail(oForm, VALField, VALErrMessage)) {
				ReturnValue = false;
			}
		}
	}


	if(!ReturnValue) {
		return ReturnValue;
	}
	
	/////////////////////////////////////
	//Validate Valid Phone Number Fields
	// These have comma separated values for
	// controlling the validation as follows:
	// Position 0 = FIELD NAME TO BE VALIDATEd
	// Position 1 = FIELD NAME OF RADIO BUTTON for US/INT if only US then enter just US
	// Position 2 = Error Message  this is only for the name of the field.
	//							the error messages are several and must be i.e. enter User Phone Number
	//  You can also do the following in the onBlur event on the phone number field:
	//		isValidTelephoneNumber(this,document.forms[0].PHONETYPE_FIELD,'Customer Phone Number')
	/////////////////////////////////////
	
	//First Check for an array of values
	if (oForm.VALValidTelephone) {
		if(oForm.VALValidTelephone.length) {
			var idx;
			for (var idx=0;idx < oForm.VALValidTelephone.length ;idx++) {
				VALString = oForm.VALValidTelephone[idx].value;
				VALArray = VALString.split(",");
				VALField = VALArray[0];
				VALPhoneTypeField = VALArray[1];
				VALErrMessage = VALArray[2];
				if (!ValidateTelephoneNumber(oForm, VALField, VALPhoneTypeField, VALErrMessage)) {
					ReturnValue = false;
					break;
				}
			}
		}
		else {
			VALString = oForm.VALValidTelephone.value;
			VALArray = VALString.split(",");
			VALField = VALArray[0];
			VALPhoneTypeField = VALArray[1];
			VALErrMessage = VALArray[2];
			if (!ValidateTelephoneNumber(oForm, VALField, VALPhoneTypeField, VALErrMessage)) {
				ReturnValue = false;
			}
		}
	}
	
	if(!ReturnValue) {
		return ReturnValue;
	}	
	

	/////////////////////////////////////
	//Validate Numeric Fields
	/////////////////////////////////////
	
	//First Check for an array of values
	if(oForm.VALValidNumber) {
		if(oForm.VALValidNumber.length) {
			var idx;
			for (var idx=0;idx < oForm.VALValidNumber.length ;idx++) {
				VALString = oForm.VALValidNumber[idx].value;
				VALArray = VALString.split(",");
				VALField = VALArray[0];
				VALErrMessage = VALArray[1];
				if (!ValidateNumeric(oForm, VALField,VALErrMessage)) {
					ReturnValue = false;
					break;
				}
			}
		}
		else {
			VALString = oForm.VALValidNumber.value;
			VALArray = VALString.split(",");
			VALField = VALArray[0];
			VALErrMessage = VALArray[1];
			if (!ValidateNumeric(oForm, VALField,VALErrMessage)) {
				ReturnValue = false;
			}
		}
	}

	if(!ReturnValue) {
		return ReturnValue;
	}

	/////////////////////////////////////
	//Validate Postitive Numeric Fields
	/////////////////////////////////////
	
	//First Check for an array of values
	if(oForm.VALPositiveNumber) {
		if(oForm.VALValidNumber.length) {
			var idx;
			for (var idx=0;idx < oForm.VALPositiveNumber.length ;idx++) {
				VALString = oForm.VALPositiveNumber[idx].value;
				VALArray = VALString.split(",");
				VALField = VALArray[0];
				VALErrMessage = VALArray[1];
				if (!ValidateNumericPositive(oForm, VALField,VALErrMessage)) {
					ReturnValue = false;
					break;
				}
			}
		}
		else {
			VALString = oForm.VALPositiveNumber.value;
			VALArray = VALString.split(",");
			VALField = VALArray[0];
			VALErrMessage = VALArray[1];
			if (!ValidateNumericPositive(oForm, VALField,VALErrMessage)) {
				ReturnValue = false;
			}
		}
	}


	return ReturnValue;
}
				
function ValidateRequiredText(oForm, VALField, VALErrMessage) {
	if(eval("oForm." + VALField + ".value.length") == 0) {
		alert(VALErrMessage);
		eval("oForm." + VALField + ".focus()");
		
		return false;
	}
	return true;
}

function ValidateRequiredSelect(oForm, VALField, VALErrMessage) {
	if(eval("oForm." + VALField + ".selectedIndex") == 0) {
		alert(VALErrMessage);
		eval("oForm." + VALField + ".focus()");
		
		return false;
	}
	return true;
}

function ValidateRequiredCheckbox(oForm, VALField, VALErrMessage) {
	var oField = eval("oForm." + VALField);
	var bChecked = false;
	if(oField[0]){
		for(var i=0;i< oField.length; i++) {
			if(oField[i].checked) {
				bChecked = true;
			}
		}
		if(!bChecked)  {
			alert(VALErrMessage);
			eval("oForm." + VALField + "[0].focus()");
			return false;
		}
	}
	else {
		if (!oField.checked){
			alert(VALErrMessage);
			eval("oForm." + VALField + ".focus()");
			return false;
		}
	}
	return true;
}


function ValidateValidDate(oForm, VALField, VALErrMessage) {
	if (eval("oForm." + VALField + ".value.length") > 0) {
		if (eval("isValidDate(oForm." + VALField +")")==false) {
			alert(VALErrMessage);
			eval("oForm." + VALField + ".focus()");
			
			return false;
		}
	}
	return true;
}

function ValidateValidEmail(oForm, VALField, VALErrMessage) {
	
	if (eval("oForm." + VALField + ".value.length") > 0) {
	
		if (eval("isValidEmail(oForm." + VALField +".value)")==false) {
			alert(VALErrMessage);
			eval("oForm." + VALField + ".focus()");
			return false;
		}
	}
	return true;
}

function ValidateNumeric(oForm, VALField, VALErrMessage) {
	
	if (eval("oForm." + VALField + ".value.length") > 0) {
		if (eval("isNaN(oForm." + VALField +".value)")) {
			alert(VALErrMessage);
			eval("oForm." + VALField + ".focus()");
			return false;
		}
	}
	return true;
}

function isValidDate(datField) {
	var dateStr;
	dateStr = datField.value
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
		//alert("Date is not in a valid format.")
		datField.value = '';
		return false;
	}
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	if (month < 1 || month > 12) { // check month range
		//alert("Month must be between 1 and 12.");
		datField.value = '';
		return false;
	}
	if (day < 1 || day > 31) {
		//alert("Day must be between 1 and 31.");
		datField.value = '';
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		//alert("Month "+ month +" doesn't have 31 days!")
		datField.value = '';
		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!");
			datField.value = '';
			return false;
		}
	}
	return true;  // date is valid
}
function ValidateNumericPositive(VALField, VALErrMessage) {
	if (eval("document.forms[0]." + VALField + ".value.length") > 0) {
		if (eval("isNaN(document.forms[0]." + VALField +".value)")) {
			alert(VALErrMessage);
			eval("document.forms[0]." + VALField + ".focus()");
			return false;
		}
	
		if (eval("document.forms[0]." + VALField + ".value") <= 0) {
			alert(VALErrMessage);
			eval("document.forms[0]." + VALField + ".focus()");
			return false;
		}
		
	}
	return true;
}



function ValidateTelephoneNumber(oForm, VALField, VALPhoneTypeField, VALErrMessage) {
	
	if (eval("oForm." + VALField + ".value.length") > 0) {
		if (eval("isValidTelephoneNumber(oForm." + VALField + ",oForm." + VALPhoneTypeField + ", VALErrMessage)")==false) {
			eval("oForm." + VALField + ".focus()");
			return false;
		}
	}
	return true;
}


function isValidTelephoneNumber(VALField,VALPhoneTypeField,VALErrMessage)
	{
		var CheckInternational= true;
	
		if (VALField.name == 'US') {
			CheckInternational = false;
					
		} 
		else {
			if (VALPhoneTypeField[0].checked) {
				CheckInternational = false; 
			} 
			else {
				CheckInternational = true;
			}
		}
		if (!CheckInternational)	{
			var efv=VALField.value;
			var old="";
			var formatted="";
			var digits="0123456789";
		
			// Create a string that holds only the numbers from edit field
			for (a=0;a<efv.length;a++) if (digits.indexOf(efv.charAt(a)) != -1) old+=efv.charAt(a);
		
			// Bail if no numbers were entered
			if (old.length==0) { efv=""; return false; }
		
			// Bail if no area code
			if (old.length==7) 
				{
				alert("Please include your area code as part of the " + VALErrMessage + ".");
				VALField.focus();
				return false;
				}
		
			// Bail if wrong number of numbers were entered
			if (old.length!=10) 
				{
				alert("Please enter your area code followed by the " + VALErrMessage + ". \nFor example, (800)444-7595.");
				VALField.focus();
				return false;
				}
		
			// Create new formatted string
			for (a=0;a<old.length;a++)
				{
				if (a==0) formatted+="(";
				if (a==3) formatted+=") ";
				if (a==6) formatted+="-";
				formatted+=old.charAt(a);
				}
		
			// Place new string in edit field
			VALField.value = formatted;
			return true;
		}

		//Check International Number
		else {
			var efv=VALField.value;
			var old="";
			var formatted="";
			var digits="0123456789";
		
			// Create a string that holds only the numbers from edit field
			for (a=0;a<efv.length;a++) if (digits.indexOf(efv.charAt(a)) != -1) old+=efv.charAt(a);
			
			for (a=0;a<old.length;a++)
				{
				formatted+=old.charAt(a);
				}

			// Place new string in edit field
			VALField.value = formatted;
			return true;
		}
}




function isValidEmail(emailStr) {

	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	if (user.match(userPat)==null) {
	    // user is not valid
	    return false
	}

	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	    // this is an IP address
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
			return false
		    }
	    }
	    return true
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
	    return false
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
	   return false
	}

	if (len<2) {
	   return false
	}
	return true;
}

	
function CheckBox (oFrm, fieldName,idx) {
		var obj;
		if(eval('document.forms[0].' + fieldName + '[' + idx + ']')) {
			obj = eval('document.forms[0].' + fieldName + '[' + idx + ']');
		}
		else {
			obj = eval('document.forms[0].' + fieldName );		
		}
		obj.checked = !obj.checked;
}

