/* Simple function to do a very quick test for basic email address reliability */
function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

}

/* Perform all of the field validation neccessary for the form */
function confirmSubmit() {
	
	/* Make sure their first name is provided to accept the form */
	if (document.contactForm.firstName.value == '') {
		alert("Please provide your first name to continue.");
		return false;
	}

	/* Make sure their last name is provided to accept the form */
	if (document.contactForm.lastName.value == '') {
		alert("Please provide your last name to continue.");
		return false;
	}

	/* Make sure that they have selected a preferred method of contact */
	if (document.contactForm.methodOfContact.selectedIndex > 0) {

		/* If they indicated that email was their preferred method of contact, make sure they have provided a valid email address */
		if (document.contactForm.methodOfContact.selectedIndex == 5) {
			if (!isValidEmail(document.contactForm.email.value)) {
				alert("Please provide a valid email address to continue.");
				return false;
			}
		
		/* Otherwise, we assume they requested to be contacted by phone, so make sure the 3 phone number fields are filled out */
		} else {
			if ( (document.contactForm.areacode.value == '') || (document.contactForm.phone1.value == '') || (document.contactForm.phone2.value == '') ) {
				alert("Please provide your area code and phone number to continue.");
				return false;
			}
		}
	
	} else {
		alert("Please select a preferred method of contact to continue.");
		return false;
	}

	/* Finally, make sure that they provide a question or comment to reply to */
	if (document.contactForm.comments.value == '') {
		alert("Please provide your questions and/or comments to continue.");
		return false;
	}

	return true;
}

/* This function is responsible for flipping the on/off display state of the phone/email div layers */
function changeLayer(layer_ref,state) {
	if (document.all) { //IS IE 4 or 5 (or 6 beta)
		eval( "document.all." + layer_ref + ".style.display = state" );
	}

	if (document.layers) { //IS NETSCAPE 4 or below
		document.layers[layer_ref].display = state;
	}

	if (document.getElementById &&!document.all) {
		hza = document.getElementById(layer_ref);
		hza.style.display = state;
	}
}

/* This function is the logic behind which layer to display based on their preferred method of contact select-box choice. */
function updateLayers() {
	if (document.contactForm.methodOfContact.selectedIndex > 0) {
		if ( (document.contactForm.methodOfContact.selectedIndex > 0) && (document.contactForm.methodOfContact.selectedIndex < 5)) {
			changeLayer('phoneLayer','block');
			changeLayer('emailLayer','none');
		} else {
			changeLayer('phoneLayer','none');
			changeLayer('emailLayer','block');
		}
	} else {
		changeLayer('phoneLayer','none');
		changeLayer('emailLayer','none');
	}
	
}
