// JavaScript Document
window.onload = initForms;

function initForms() {
	for (var i=0; i< document.forms.length; i++) {
		document.forms[i].onsubmit = function() {return validForm();}
	}
	webinit();         // run normal onload
}

function validForm() {
	var allGood = true;
	var allInputTags = document.getElementsByTagName("input");
  
	for (var i=0; i<allInputTags.length; i++) {
    if (!validTag(allInputTags[i])) {
			allGood = false;
		}
	}
	
  var allTextAreaTags = document.getElementsByTagName("textarea");
	for (var i=0; i<allTextAreaTags.length; i++) {
    if (!validTag(allTextAreaTags[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.select();
			}
			return false;
		}
		return true;
		
		function validBasedOnClass(thisClass) {
			var classBack = "";
		
			switch(thisClass) {
				case "":
				case "invalid":
					break;
				case "fmail":
				case "fmailro":
				case "fmailsubmit":
					classBack += thisClass;
					break;
				case "email":
					if (allGood && !validEmail(thisTag.value)) {
            document.getElementById("errortext").innerHTML='Please correct the following error:<br><span style="color:green">INVALID email address syntax (should be in form email@server.com)!</span>';
            classBack = "invalid ";
          }
					classBack += thisClass;
					break;				  
				case "reqd":
					if (allGood && thisTag.value == "") {
            document.getElementById("errortext").innerHTML='Please correct the following error:<br><span style="color:green">This field is REQUIRED!</span>';
            classBack = "invalid ";
					}
          classBack += thisClass;
					break;
				default:
					if (allGood && !crossCheck(thisTag,thisClass)) {
            document.getElementById("errortext").innerHTML='Please correct the following error:<br><span style="color:green">Email addresses do NOT match!</span>';
            classBack = "invalid ";
					}
          classBack += thisClass;
			}
			return classBack;
		}
				
		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 crossCheck(inTag,otherFieldID) {
			if (!document.getElementById(otherFieldID)) return false;
			return (inTag.value == document.getElementById(otherFieldID).value);
		}
	}
}

