var xmlFile;var validateObj = new Array();function refreshForm(varName, varValu) {    var newURL = window.location.pathname + "?" + varName + "=" + varValu;	document.forms[0].action = newURL;	document.forms[0].submit()}function importXML(file, func) {	if(document.implementation && document.implementation.createDocument) {		xmlFile = document.implementation.createDocument("", "", null);		xmlFile.onload = func;	} else if(window.ActiveXObject) {		xmlFile = new ActiveXObject("Microsoft.XMLDOM");		xmlFile.onreadystatechange = function () {			if(xmlFile.readyState == 4) {func();};		};	}	xmlFile.load(file);}function parseFormXml() {	if(document.getElementById(xmlFile.documentElement.getAttribute("id")) == null) {return;};	form = document.getElementById(xmlFile.documentElement.getAttribute("id"));	addEvent(form, "submit", onsubmitCheck, false);	inputs = xmlFile.getElementsByTagName("inputbox");	for(var i = 0; i < inputs.length; i++) {		val = new inputValidate(inputs[i]);		validateObj[val.id] = val;		addEvent(val.inputf, "blur", onblurCheck, false);	};}function getXmlUrl() {	var url = window.location.href;	url = url.split("?")[0];	url = url.match(/\w+\.[a-zA-Z0-9]+$/).toString();	var dot = url.lastIndexOf(".");	url = url.substring(0, dot);	url = url + ".xml";	return url;}function inputValidate(node) {	this.id = node.getAttribute("id");	this.inputf = document.getElementById(this.id);	this.valid = true;	this.minr = node.getAttribute("min");	this.maxr = node.getAttribute("max");	this.sreq = (node.getAttribute("req") == "true");	this.regs = new Array();	curNode = node.getElementsByTagName("regex");	for(i = 0; i < curNode.length; i++ ) {		if(curNode[i] != null) {this.regs[i] = new RegExp(curNode[i].childNodes[0].nodeValue);};	};	this.creq = new Array();	curNode = node.getElementsByTagName("coreq");	for(i = 0; i < curNode.length; i++ ) {		if(curNode[i] != null) {this.creq[i] = document.getElementById(curNode[i].childNodes[0].nodeValue);};	};	err = node.getElementsByTagName("errmsg")[0];	if(err != null) {		this.error = err.childNodes[0].nodeValue;	} else {		this.error = null;	};	this.name = node.getAttribute("lbl");	this.parentClass = this.inputf.parentNode.className;		this.errMsgL = function() {		var strTmp = this.name.substring(0,1).toUpperCase();		var strInt = strTmp + this.name.substring(1, this.name.length) + " must ";		if(this.minr != null) {			if(this.maxr != null) {				strTmp = strInt + "be between " + this.minr + " and " + this.maxr;			} else {				strTmp = strInt + "have more than " + this.minr;			};		} else {			if(this.maxr != null) {				strTmp = strInt + "have less than " + this.maxr;			} else {				strTmp = "have more than 0";			};		};		strTmp += " characters.";		return strTmp;	};		this.markInvalid = function(errMsg, skipCoreq) {  		this.inputf.parentNode.className = this.parentClass + " errorarea";  		if(this.valid == false) {  			errorNode = document.getElementById(this.id + "errmsg");  			textNode = document.createTextNode(errMsg);  			errorNode.replaceChild(textNode, errorNode.childNodes[0]);  		} else {			errp = document.createElement("p");			errp.className = "errortext";			errp.setAttribute("id", this.id + "errmsg");			textNode = document.createTextNode(errMsg);			errp.appendChild(textNode);			this.inputf.parentNode.appendChild(errp);  		};  		this.valid = false;		if((!skipCoreq)&&(this.creq.length > 0)) {			var coreqField;			for(i = 0; i < this.creq.length; i++) {				coreqField = validateObj[this.creq[i].getAttribute("id")];				coreqField.markInvalid(coreqField.error, true);			};		};	};		this.markValid = function(skipCoreq) {  		if(this.valid == false) {  			errorNode = document.getElementById(this.id + "errmsg");  			this.inputf.parentNode.removeChild(errorNode);  		};  		this.valid = true;  		this.inputf.parentNode.className = this.parentClass;		if((!skipCoreq)&&(this.creq.length > 0)) {			var coreqField;			for(i = 0; i < this.creq.length; i++) {				coreqField = validateObj[this.creq[i].getAttribute("id")];				if(this.creq[i].value == "") {					coreqField.markValid(true);				} else {					if(coreqField.valid) {coreqField.markValid(true);};				};			};		};	};		this.check = function() {		var fldValue = this.inputf.value;		var getError = "";		if(fldValue == "") {			getError = "You must fill out this field.";			if(this.creq.length > 0) {				var coreqsOK;				if(this.sreq) {					var coreqsOK = true;					for(i = 0; i < this.creq.length; i++) {						if(this.creq[i].value != "") {							coreqsOK = false;							break;						};					};				} else {					var coreqsOK = false;					for(i = 0; i < this.creq.length; i++) {						if((this.creq[i].value != "")&&(validateObj[this.creq[i].getAttribute("id")].valid == true)) {							coreqsOK = true;							break;						};					};				};				if(coreqsOK) {					this.markValid();					return;				};			} else {				if(this.sreq == false) {					return;				};			};		} else {			if(((this.minr != null)&&(fldValue.length < this.minr)) || ((this.maxr != null)&&(fldValue.length > this.maxr))) {				getError = this.errMsgL();			};			if(this.regs.length > 0) {				var formatOK = false;				for(var i = 0; i < this.regs.length; i++) {					if(this.regs[i].test(fldValue)) {						formatOK = true;						break;					};				};				if(formatOK == false) {getError = this.error;};			};		};		var reqCoreq = (this.sreq == true) && (this.creq.length > 0);		if(getError == "") {			this.markValid(reqCoreq);		} else {			this.markInvalid(getError, reqCoreq);		};	};}function onsubmitCheck(event) {	var allOk = true;	for(var i in validateObj) {		validateObj[i].check();		if(validateObj[i].valid == false) {allOk = false;};	};	if(allOk == false) {		message = "This form requires some more information from you!\n";		message += "Please review the fields in red and provide the correct information.";		alert(message);		if(document.all) {			event.returnValue = false;		} else {			event.preventDefault();		};	};}function addEvent(obj, etyp, elis, ucap) {	if(obj.addEventListener) {		obj.addEventListener(etyp, elis, ucap);		return true;	} else if(obj.attachEvent) {		var objAttach = obj.attachEvent("on"+etyp, elis);		return objAttach;	} else {		return false;	};}function onblurCheck(event) {	if(document.all) {		id = event.srcElement.getAttribute("id");	} else {		id = this.getAttribute("id");	};	validateObj[id].check();}importXML(getXmlUrl(), parseFormXml);
