/* form_validate.js - javascript to validate data input forms. 
 * 
 * You can create forms on the fly and validate fields in them by defining specific classes
 * for fields.
 *
 * 	  <b><label for="email">E-mail Address: <input  id="email" class="email reqd" type="text"
 *		name="email" style="width:200px"></label></b><br>
 *	  <b><label for="email2">Re-type E-mail Address: <input  id="email2" class="email reqd"
 * 		type="text" name="email2" style="width:200px"></label></b><br>
 *
 * Two types of fields currently supported: 
 *  reqd - mandatory. Flagged as error if not filled in
 *  email - field is an email field. Does rudimentary syntax checking for obvious errors.
 *
 * additionally, if a field exists in the form named "email2", it presumes we're doing the 
 * "type the email address in twice" check, and validates that the content of email2 matches
 * the content of "email"; if it doesn't, it's flagged as an error. 
 *
 * note format of form items above; by putting the form item inside a <label></label>, we can CSS
 * the label to note the error. The following CSS turns errored fields red:
 *
 * input.invalid {
 * 	color: #f00;
 * 	border: 3px red inset;
 * }
 * 
 * label.invalid {
 *     color: #f00;
 * }
 *
 * script is self-configuring if you use the format above
 *
 * chuq von rospach, laszlo systems 
 */

addOnLoad(initForms);

function addOnLoad(newFunction) {
    var oldOnload = window.onload;

    if (typeof oldOnload == 'function') {
        window.onload = function() {
            if (oldOnload) {
                oldOnload();
            }
            newFunction();
        }
    } else {
        window.onload = newFunction;
    }
}

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) {
			invalidLabel(thisTag.parentNode);
			thisTag.focus();

			if (thisTag.nodeName == "INPUT") {
				thisTag.select();
			}
			return false;
		}
		return true;    
		
		function validBasedOnClass(thisClass) {
			var classBack = "";
			switch(thisClass) {
				case "":
				case "invalid":
					break;
				
				case "reqd":
					if (allGood && thisTag.value == "") classBack = "invalid ";
						classBack += thisClass;
					break;

				case "email":
					if (allGood && !validEmail(thisTag.value)) {
						classBack = "invalid ";
						classBack += thisClass;
						break;
					}

					if (thisTag.name == "email2") {
			
						if (!(thisTag.value == document.getElementById("email").value)) {
							classBack = "invalid ";
							classBack += thisClass;
							break;
							}
					}
					break;

				default:
					classBack += thisClass; 
					break;
			}
			return classBack;
		}
		
		function validEmail(email) {
			var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		
			return re.test(email);
		}

		function invalidLabel(parentTag) {
			if (parentTag.nodeName == "LABEL") {
				parentTag.className += " invalid";
			}
		}
	}	
}
