addOnload(contactInit);
addOnload(initForm);
addOnload(rolloverInit);

function addOnload(newFunction){
	var oldOnload = window.onload;
	
	if(typeof oldOnload == "function"){
		window.onload = function(){
			if(oldOnload){ //cross browsers issues thus you need if statements
				oldOnload();
			}
			newFunction();
		}
	}
	else{
		window.onload = newFunction;
	}
}


// ------------- START OF AUTOTAB CODING --------------//

function contactInit() {
	var allTags = document.getElementsByTagName("*");

	for (var i=0; i<allTags.length; i++) {
		if(allTags[i].className.indexOf("phone") > -1){
			allTags[i].onkeyup = autoTab;
		}
	}
}

function autoTab() {
	if(this.value.length == this.getAttribute("maxlength")){
		for(var i=0; i<2; i++){
			switch(this.id){
				case "mobileNumber" + i:
					document.getElementById("mobileNumber" + (i+1)).focus();
					break;
				case "homeNumber" + i:
					document.getElementById("homeNumber" + (i+1)).focus();
					break;
				case "officeNumber" + i:
					document.getElementById("officeNumber" + (i+1)).focus();
					break;
				default:
					break;
			}
		}
	}
}


					
			
//----------------------------START OF VERIFYING CODING -------------------- //



function initForm() {
	for (var i=0; i< document.forms.length; i++) {
		document.forms[i].onsubmit = validForm;
	}
}

/*loops through all tags and only pushes through those tags with class containing "reqd", then runs verify function, stores in array = reqdArrayResults
if any of the tags comes back with false, the next loop will search for that and then will return false.
the 2nd loop is there so that ALL fields that are required and is missing something will be pointed out to user, not just the current field*/

function validForm() {
	var allGood = true;
	var allTags = document.getElementsByTagName("*");
	var reqdArrayResults = new Array();
	var allTagsLength = allTags.length;
	
	//This for loop is backwards so that the focus is on the top most field, instead of the bottom most field//
	
	for (var i=allTagsLength-1; i>-1; i--) {
		if(allTags[i].className.indexOf("reqd") > -1){			
			reqdArrayResults.push(validTag(allTags[i]));
		}
	}
			
	for(var i2=0; i2<reqdArrayResults.length; i2++){
		if (!reqdArrayResults[i2]){
			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;
		
		//Resets parent tags to blank so that if user enters correct info, label will not be hightlighted//
		
		document.getElementById("textArea").className = "";
		thisTag.parentNode.parentNode.className = "";
	
		if (outClass.indexOf("invalid") > -1) {
			invalidLabel(thisTag.parentNode.parentNode);
			
			thisTag.focus();
			if (thisTag.nodeName == "INPUT") {
				thisTag.select();
			}
			return false;
		}
		return true;
		
		function validBasedOnClass(thisClass) {
			var classBack = "";

// ----------- This switch determines which reqd does not meet the necessary requirements, NOTE: maybe if we take out allGood from ifs, it might run the script= NEGATIVE!)//


			switch(thisClass) {
				case "":
				case "invalid":
					break;
				case "reqd":
					if (allGood && thisTag.value == "") {
						classBack = "invalid ";
					}
					classBack += thisClass;
					break;
				case "emailAddr":
					if(allGood && !crossCheck(thisTag,thisClass)) {
						classBack = "invalid ";
					}
					classBack += thisClass;
					break;
				case "email":
					if (allGood && !validEmail(thisTag.value)) {
						classBack = "invalid ";
					}
					classBack += thisClass;
					break;
				default:
					classBack += thisClass;
					break;
			}
			return classBack;
		}
				
		function crossCheck(inTag,otherFieldID) {
			if (!document.getElementById(otherFieldID)) {
				return false;
			}
			return (inTag.value == document.getElementById(otherFieldID).value);
		}

// VERIFYING EMAIL TO INSURE IT IS NOT A BULLSHIT EMAIL ADDY =)//

				
		function validEmail(email) {
			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;
		}
		
		function invalidLabel(parentTag) {
			if (parentTag.nodeName == "TR") {
				parentTag.className += " invalid";
				document.getElementById("textArea").className += " invalid";
			}
		}
	}
}


//------------------- PNG ROLLOVER AND OUT FUNCTIONS!!! ----------------------//

function rolloverInit() {
	for (var i=0; i<document.images.length; i++) {
		if(document.images[i].parentNode.tagName == "SPAN"){
				setupPNGRollOver(document.images[i]);
		}
	}
}

function setupPNGRollOver(thisImage){
	thisImage.outImage = new Image();
	thisImage.outImage.src = thisImage.src;
	thisImage.onmouseout = rollOut;

	thisImage.overImage = new Image();
	thisImage.overImage.src = "../images/" + thisImage.id + "-on.png";
	thisImage.onmouseover = rollOver;
}

function rollOut() {
	this.src = this.outImage.src;
}

function rollOver() {
	this.src = this.overImage.src;
}

