var ValidationGoodColor = "#FFFFFF";
var ValidationBadColor = "#CEE0FF";

var ValidationList = new Array();
var ValidationConditions = new Array("1==1");

function DumpFormFields(theFormName) {
	var tx="<div align=left><pre>";
	for (var i=0; i<document.forms[theFormName].elements.length; i++) {
		tx+=document.forms[theFormName].elements[i].name+"\n";
	}
	tx+="</pre></div>";
	document.write(tx);
}

function ResetValidation() {
	ValidationList = new Array();
	ValidationConditions = new Array("1==1");
}

function ValidateCondition(theConditionID,theExpr) {
	ValidationConditions[theConditionID] = theExpr;
}

function Validate(theConditionID,theType,theName,thePrompt,theOpt) {
	ValidationList[ValidationList.length] = new Array(theConditionID,theType,theName,thePrompt,theOpt);
}

function validateForm(theForm) {
	// build states table //
	var Conditions = new Array();
	for (var i=0; i<ValidationConditions.length; i++) {
		Conditions[i] = eval(ValidationConditions[i]);
	}

	// validate each item in ValidationList //
	var FirstErrorObject = null;
	var FirstErrorPrompt = "";
	for (var i=0; i<ValidationList.length; i++) {
		var temp = ValidationList[i];
		var conditionID = temp[0];
		var theType = temp[1];
		var theName = temp[2];
		var thePrompt = temp[3];
		var theOpt = temp[4];

		if (Conditions[conditionID]==true) {
			if (!theForm[theName]) {
				alert ("Validation can not find field: " + theName);
				return false;
			}

			var theField = theForm[theName];

			// get value of field //
			var theValue = ""+theField.value;

			switch (theField.type) {
				case undefined:
					theValue = "";
					for (var j=0; j<theField.length; j++) {
						if (theForm[theName][j].checked) {theValue=theForm[theName][j].value;}
					}
					break;

				case "select-one":
					theValue = (theField.selectedIndex<0)? "" : theField.options[theField.selectedIndex].value;
					break;

				case "checkbox":
					theValue = (theField.checked)? theField.value : "";
					break;
			}

			// check if this value is optional, or not //
			if (theValue=="") {
				var okay = (theOpt==true)? true: false;
			} else {
				// use reg-exp to check each type of input string //
				var re=/oops/;
				switch(theType.toUpperCase()) {
					case "ADDRESS":		re = /^[\s]*[\w\-']+([\s\., ]+[\w\-']+){3,30}\s*$/; break;
					case "ALPHA": 		re = /^[a-zA-Z0-9]+$/; break;
					case "BOOLEAN": 	re = /^[0-1]+$/; break;
					case "CHECK":		re = /^.+$/; break;
					case "CITY":		re = /^[a-zA-Z]{2,}([-' ][a-zA-Z]+)*$/; break;
					case "CURRENCY":	re = /^\+?\d{1,}\.?\d*$/; break;
					case "DATE":		re = /\d{1,2}\/\d{1,2}\/(19|20)\d\d$/; break;
					case "DATETIME":	re = /\d{1,2}\/\d{1,2}\/(19|20)\d\d\s+([1-9]|[1-2][0-9]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/; break;
					case "DOB":			re = /\d{1,2}\/\d{1,2}\/(19|20)\d\d/; break;
					case "DOMAIN":		re = /(.\w+){1,4}/; break;
					case "EMAIL":		re = /^[\w-\._]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$/; break;
					case "EMAILLIST":	re = /^[\w-\._]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}(,[\w-\._]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3})*$/; break;
					case "FILENAME":	re = /^[\w\-_ \.\(\)]{1,}.[\w]$/; break;
					case "FULLADDRESS": re = /^[\w]+(\s+[\w\-\.]+){2,10}$/; break;
					case "FULLNAME":	re = /^[a-zA-Z]{1,}[ a-zA-Z'\-]{1,}$/; break;
					case "HEADING":		re = /^[\w\W]+/; break;
					case "ID":			re = /^\d+$/; break;
					case "IDLIST":		re = /^\d+(,\d+)*$/; break;
					case "IDNAME":		re = /^[\w\/\-': ]+$/; break;
					case "IPADDRESS":	re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; break;
					case "INTEGER":		re = /(^[-+]?\d\d*$)/; break;
					case "KEYWORDS":	re = /^\w+(,[\w]+)*$/; break;
					case "LOCALPATH":	re = /^[\w\W]+$/; break;
					case "MENU":		re = /^.+$/; break;
					case "SELECT":		re = /^[^0].*$/; break;
					case "MONTHS":		re = /^([0-9])|(1[12])$/; break;
					case "NAME":		re = /^[a-zA-Z][a-zA-Z'\-]{1,}$/; break;
					case "NOTES":		re = /^[\w\W]*$/; break;
					case "NUMBER":		re = /(^-?\+?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; break;
					case "PASSKEY":		re = /^\w{8,50}$/; break;
					case "PASSWORD":	re = /^[\w\-_ ]{6,}$/; break;
					case "PATH":		re = /^[A-Za-z]:\\([\w\.\-\(\)\&_\\]+)*$/; break;
					case "PHONE":		re = /^\+?[-\d\(\) ]{5,}$/; break;
					case "POSTCODE":	re = /^[\w- ]{4,10}$/; break;
					case "PRICE":		re = /^\+?\d{1,}(,\d\d\d)*(\.\d{1,2})?$/; break;
					case "RADIO":		re = /.+/; break;
					case "SITENAME":	re = /[\w\-\.]+/; break;
					case "STOCKCODE":	re = /^[\w\/\-'+ #\(\)\.]+$/; break;
					case "STREET":		re = /^[\w',\-\.\f\r ]{2,}$/; break;
					case "TEXT":		re = /^[\w\W]+$/; break;
					case "TIME":		re = /^([1-9]|[1-2][0-9]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/; break;
					case "TITLE":		re = /^[a-zA-Z]{2,10}\.?$/; break;
					case "TRACKCODE":	re = /^[\w]{1,}[\w_\@\.,\{\}\#\+\-\/]*$/; break;
					case "URL":			re = /^([\w-]+\.)+(\/[\w- .\/?%&=]*)?/; break;
					case "USERNAME":	re = /^[\w\-_]{6,}$/; break;
					case "YEARS":		re = /^[1-9]+\d*$/; break;

					default: alert ("Unknown form-field validation type:"+theType);
				}
				var okay = re.test(theValue);
			}

			// highlite errors (if needed) //

			if (okay==true) {
				var theRGB = ValidationGoodColor;
			} else {
				var theRGB = ValidationBadColor;
				if (FirstErrorObject==null) {
					FirstErrorObject = theField;
					FirstErrorPrompt = thePrompt;
					if (theField.type!="hidden" && theField.focus) theField.focus();
				}
			}

			// set BGcolor IE 4,5,6 and Netscape 6,7 //
			if (document.all) {
				if (theField.length) {
					// radio buttons //
					if (okay==true) {
						theField[0].style.background = "";
					} else {
						theField[0].style.background = theRGB;
					}
				} else {
					theField.style.background = theRGB;
				}
			}

			if (!document.all && document.getElementById) {
				if (theField.length) {
					var fieldname = theField[0].name;
					var ele = document.getElementById(fieldname);
					if (ele) ele.style.backgroundColor = theRGB;
				} else {
					var fieldname = theField.name;
					var ele = document.getElementById(fieldname);
					if (ele) ele.style.backgroundColor = theRGB;
				}
			}
		}

	// end of loop //
	}

	// display prompt for first error field //
	if (FirstErrorObject!=null) {
		alert (FirstErrorPrompt);
		return false;
	} else {
		return true;
	}
}
