﻿function EmailIsValid () {
	with ( document.forms[0] ) {
		if ( !(checkMail (elements["email"])) ) {
							elements["email"].select();
							elements["email"].focus();
							alert("The email address is not valid. Please enter another one. Click OK and you will be taken to it.");
							return false;
		}
	}
	
	return true;
}

function EmailsAreValid () {
	var i;
	
	with ( document.forms[0] ) {
		// Check if email list field exists and has a value
		if ( (elements["Emails"] != Null) && (elements["Emails"].value != '') ) {
			// Capture size Emails list field
			listSize=elements["Emails"].length;
			// Loop through each email address field and check if it is valid
			for ( i=0 ; i<listSize ; i++ ) {
				alert ( "Email " + i + ": " + elements["Emails"][i].value );
				// Validate current email address field
				if ( !(checkMail(elements["Emails"][i])) ) {
					elements["email"].select();
					elements["email"].focus();
					alert("The email address is not valid. Please enter another one. Click OK and you will be taken to it.");
					return false;
				}
			}
		} else {
			// Else, validate email field, if it exists
			EmailIsValid();
		}
	}
}


function FieldsAreValid (fieldAry) {

// Validates fields listed in the field array, fieldAryy. Each entry in the fieldAry is the name of the field to validate

	var i=0;

	with ( document.forms[0] ) {
	
		for( i=0 ; i<fieldAry.length ; i++ ) {
			switch ( document.forms[0].elements[fieldAry[i]].type ) {
				case "text":
					if ( elements[fieldAry[i]].value == '' ) {
						elements[fieldAry[i]].select();
						elements[fieldAry[i]].focus();
						alert("Required information has not been entered on this form. Click OK and you will be taken to the required field.");
						return false;
					}
					break;
				case "file":
					if ( elements[fieldAry[i]].value == '' ) {
						elements[fieldAry[i]].select();
						elements[fieldAry[i]].focus();
						alert("Please select a file. Click OK.");
						return false;
					}
					break;
				case "select-one":
					if ( elements[fieldAry[i]].options[elements[fieldAry[i]].selectedIndex].value == '' ) {
						elements[fieldAry[i]].focus();
						alert("Required information has not been entered on this form. Click OK and you will be taken to the required field.");
						return false;
					}
					break;
			}
		}
	}

	return true;

}

function BuildReqFldAry () {

	var tempReqFldAry;
	
	if ( document.forms[0].elements["required"] != null )
		tempReqFldAry=document.forms[0].elements["required"].value.split(",");
	else
		tempReqFldAry=new Array();
			
	return tempReqFldAry;
}

function SubmitHVForm () {

	// Build list of required fields
	var reqFields=BuildReqFldAry();

	// Check if required fields are valid
	if ( (FieldsAreValid(reqFields)) && (EmailIsValid()) && (ConfEmailIsValid()) && (ZIPIsValid()) ) {
		// If so, submit contact form
		return true;
	} else {
		// Else, do not submit form
		return false;
	}
}