// JavaScript Document
/* This file is local */
/**************************************************************************************
Purpose:		To support the required field form validation for the AFALSS contact page.
Created On:	1/12/09
Code Source:	Visual Quickstart Guide: JavaScript & Ajax Seventh Edition by Tom Negrino and Dori Smith
Modified By:	Phil Allen
Modifications:		If the user forgets to fill in a required field that happens to be a
			text box, the code will put the word "Required" into the textbox field to
			help prompt the user to fill it in.
				If the user forgets to fill in the field for the user's home state, the
			field will be focused on, so the user has an indication that they need to
			choose their home state.
				The MM_preloadImages function was disabled because it prevented the form
			validation from working at all in Internet Explorer 7. The original purpose of
			the function was to load the navigation mouseOver images as soon as the page
			loaded to prevent any delay for users on a slow internet connection when they
			moused over one of the changing navigation images. 
****************************************************************************************/

/****************************************************************************************************
window.onload = MM_preloadImages('img/contact_on.jpg','img/about_on.jpg','img/serv_prod_on.jpg');
*****************************************************************************************************/
/* The previous line was added so that there would not be a conflict between the onLoad event
in the body tag of the contact.asp file and the "window.onload = initForms;" statement at the 
beginning of this file. The onLoad event was moved from the body tag of the contact.asp file
to the previous statement so that the form validation for required fields would work. */


function initForms() {
	
	for (var i=0; i<document.forms.length; i++) {
		document.forms[i].onsubmit = function() {return validForm();}
	}
}

function validForm() {
	var allGood = true;
	var allTags = document.getElementsByTagName("*");

	for (var i=0; i<allTags.length; i++) {
		if (!validTag(allTags[i])) {
			allGood = false;
		}
	}
	return allGood;

	function validTag(thisTag) {
		var outClass = "";
		var allClasses = thisTag.className.split(" ");
	
		for (var j=0; j<allClasses.length; j++) {
			outClass += validBasedOnClass(allClasses[j]) + " ";
		}
	
		thisTag.className = outClass;
	
		if (outClass.indexOf("invalid") > -1) {
			thisTag.focus();
			if (thisTag.nodeName == "INPUT")
			{
				thisTag.value = "Required"; //This line was added to make the word, "Required" appear in the field when the field is selected.
				thisTag.select();
			}
								/***** Beginning of section not included with original code.  ******/
								/***** This section was added to make the validation work for
									  the drop-down field for 'State', but it doesn't work
									  right now (1/7/2009).                                  ******/
/*			else if (thisTag.nodeName == "SELECT") //  && thisTag.selectedIndex.value == "default"
			{
//				 thisTag.options[thisTag.selectedIndex].text = "Required";
				 thisTag.select();
			}
								/****** Ending of section not included with original code.  *******/
//			else 
				return false;
		}
		return true;
		
		function validBasedOnClass(thisClass) {
			var classBack = "";
		
			switch(thisClass) {
				case "":
				case "invalid":
					break;
				case "reqd":
					if (allGood && thisTag.value == "") {classBack = "invalid ";}
					else if (allGood && thisTag.value == "default") {classBack = "invalid ";}
					else if (allGood && thisTag.value == "Required") {classBack = "invalid ";}
					classBack += thisClass;
					break;
					default:
					classBack += thisClass;
			}
			return classBack;
		}
	}
}