addEvent(window, "load", setupVal, false) ;
function setupVal() {
	var valFields = getElementsByClassName(document, "input", "validate") ;
	for (var i=0; i<valFields.length; i++) {
		var parentForm = climbDom(valFields[i], "form") ;
		if (parentForm && !classContains(parentForm, "requiresValidation")) {
			addEvent (parentForm, "submit", validateForm, false) ;
			addClass(parentForm, "requiresValidation") ;
		}
	}
}

function validateForm(e) {
	var myTarget = getTarget(e) ;
	if (!myTarget) {
		cancelEvent(e) ;
		return false ;
	}
	whatForm = climbDom(myTarget, "form") ;
	var valFields = getElementsByClassName(whatForm, "*", "validate") ;
	var errorText = "" ;
	for (var i=0; i<valFields.length; i++) {
		var allClassNames = valFields[i].className.split(" ") ;
		for (var j=0; j<allClassNames.length; j++) {
			if (allClassNames[j].indexOf("val-") == 0) {
				var validationRule = allClassNames[j].substring(4) ;
				// alert("Testing " + validationRule) ;
				switch (validationRule) {
					case "nonblank" :
						if (valFields[i].options) {
							if (valFields[i].options[valFields[i].selectedIndex].value.length == 0) {
								errorText += " - " + valFields[i].title + "\n" ;
							}
						}
						else if (valFields[i].value.length == 0) {
							errorText += " - " + valFields[i].title + "\n" ;
						}
					break ;
					case "nonzero" :
						if (valFields[i].value== 0) {
							errorText += " - " + valFields[i].title + "\n" ;
						}
					break ;
				}
			}
		}
	}
	if (errorText.length) {
		alert("Errors found:\n" + errorText) ;
		cancelEvent(e) ;
		return false ;
	}
	else return true ;
}

function cancelEvent(e) {
	// Cross-browser cancel event
	if (e && e.preventDefault) e.preventDefault(); // DOM style
	else return false; // IE style ;
}
