window.onload = initAll;

function initAll() {
	for (var i=0; i< document.forms.length; i++) { //initiates form validations
			document.forms[i].onsubmit = checkValidForm; 
		}
		
	var allDivs = document.getElementsByTagName("div"); //this sets the initial state of the menu drop downs
	
	for (var i=0; i<allDivs.length; i++) {
		if (allDivs[i].className.indexOf("dropMenuLink") > -1) { //expand menu
				allDivs[i].onclick = toggleMenu;
		}
		if (allDivs[i].className.indexOf("initialOpen") > -1) { //expand menu for selected item
			var dropDiv = 	allDivs[i].id.replace("menu","link");
			document.getElementById(dropDiv).style.backgroundImage = "url(images/template/menu_arrow_down.gif)";
			allDivs[i].style.display = "block";
		}
	}
	
	var allLinks = document.getElementsByTagName("a"); //this initiates the rollovers
	img = new Array();
	
	for (var i=0; i<allLinks.length; i++) {
		if (allLinks[i].className.indexOf("overItem") > -1) { // rollovers, images must have id
				allLinks[i].onmouseover = toggleOverOn;
				allLinks[i].onmouseout = toggleOverOff;				
			//preloads the over images
			img[i] = new Image();
			img[i].src = document.getElementById(allLinks[i].children[0].id).src.replace("up.gif","over.gif");
		}
	}
}

function toggleMenu() {
	var dropDiv = 	this.id.replace("link","menu");
	if (document.getElementById(dropDiv).style.display == "block") {
		document.getElementById(dropDiv).style.display = "none";
		this.style.backgroundImage = "url(images/template/menu_arrow_right.gif)";
	} else {
		document.getElementById(dropDiv).style.display = "block";
		this.style.backgroundImage = "url(images/template/menu_arrow_down.gif)";
	}
	return false;
}

function toggleOverOn() {
		document.getElementById(this.children[0].id).src = document.getElementById(this.children[0].id).src.replace("up.gif","over.gif");
}

function toggleOverOff() {
	document.getElementById(this.children[0].id).src = document.getElementById(this.children[0].id).src.replace("over.gif","up.gif");
}


function checkValidForm() {  //form validation functions
	var allGood = true;
	var errormsg ="";
	var allTags = document.getElementsByTagName("*");

	for (var i=0; i<allTags.length; i++) {
		if (!validTag(allTags[i])) {
			allGood = false;
		}
	}
	
	if (!errormsg == "") {
		alert(errormsg);
	}
	
	return allGood;
	
function validTag(thisTag) { //checks each tag
		var thisTagCheck = true;
		var allClasses = thisTag.className.split(" ");
	
		for (var j=0; j<allClasses.length; j++) {
			validBasedOnClass(allClasses[j]);
		}
		
	return thisTagCheck;
	
function validBasedOnClass(thisClass) {  // checks each class
		switch(thisClass) {
			case "reqd":
				if (thisTag.value == "") {
					var lableText = thisTag.parentNode.innerText || thisTag.parentNode.textContent || ""; //need this for IE which does not have textContent
					var rtpos = lableText.indexOf("*");  //use lable to left of * in error message
					errormsg += lableText.substring(0,rtpos) + " is required\n" ;
					thisTagCheck = false;
				}
				break;
			case "email":
				if (!validEmail(thisTag.value)) {
					errormsg += "Email address is not valid\n" ;
					thisTagCheck = false;
				}
				break;
			default:
		}
	}
			
	}
	
function validEmail(email) { //does a general format test for valid email address
		var invalidChars = " /:,;";
	
		if (email == "") {
			return false;
		}
		for (var k=0; k<invalidChars.length; k++) {
			var badChar = invalidChars.charAt(k);
			if (email.indexOf(badChar) > -1) {
				return false;
			}
		}
		var atPos = email.indexOf("@",1);
		if (atPos == -1) {
			return false;
		}
		if (email.indexOf("@",atPos+1) != -1) {
			return false;
		}
		var periodPos = email.indexOf(".",atPos);
		if (periodPos == -1) {	
			return false;
		}
		if (periodPos+3 > email.length)	{
			return false;
		}
		return true;
	}
}
