﻿function EmailIsValid () {
	with ( document.ContForm ) {
	
		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 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.ContForm ) {
	
		for( i=0 ; i<fieldAry.length ; i++ ) {
			switch ( document.ContForm.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.ContForm.elements["required"] != null )
		tempReqFldAry=document.ContForm.elements["required"].value.split(",");
	else
		tempReqFldAry=new Array();
			
	return tempReqFldAry;
}

function SubmitForm () {

	// Build list of required fields
	var reqFields=BuildReqFldAry();
	
	// alert ( "Sugmit Form JS" );

	// Check if required fields are valid
	if ( (FieldsAreValid(reqFields)) && (EmailIsValid()) ) {
		// If so, submit contact form
		return true;
	} else {
		// Else, do not submit form
		return false;
	}
}
