var ValidationGoodColor = "#FFFFEE";
var ValidationBadColor = "#FF9A63";

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 "category":
					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 "STREET":		re = /^[\s]*[\w\-'\s\.,]+([\s\., ]+[\w\-']+){0,30}\s*$/; break;
					
					case "COMPANY_NAME":
								re=/^[a-zA-Z]{1,}[ a-zA-Z0-9'\-\s\.,]{1,}$/;
					break;
					case "CATEGORY":
								re=/^[a-zA-Z0-9]{1,}[ a-zA-Z0-9'\-]{1,}$/;
					break;
					case "SEARCH":
								re=/^[a-zA-Z0-9\s]{1,}$/;
					break;
					case "CATEGORY":
								re=/^[a-zA-Z]{1,}[ a-zA-Z'\-]{1,}$/;
					break;
					case "HOUSE_NO":
								re=/^[a-zA-Z0-9\s\.,]{1,}[ a-zA-Z'\-\s\.,]{0,}$/;
					break;

					case "COUNTY":
								re=/^[a-zA-Z]{2,}$/; 
					case "COUNTY1":
								re=/^[a-zA-Z0-9'\-\s\.,]{2,}$/; 									break;
					case "COUNTRY":
								re=/^[a-zA-Z]{2,}([-'][a-zA-Z]+)*$/; 								break;
					
					case "CITY":		re = /^[a-zA-Z]{2,}([-' ][a-zA-Z]+)*$/; break;
					case "CURRENCY":	re = /^\+?\d{1,}\.?\d*$/; break;
					case "DATE":		re = /(?:[0-9]|0[1-9]|[12][0-9]|3[01])\/(?:[0-9]|0[1-9]|1[0-2])\/(?:19[0-9][0-9]|20[0-9][0-9])/; 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 "EMAIL":		re = /^[\w-\._]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$/; break;
					
					
					case "FULLADDRESS": re = /^[\w]+(\s+[\w\-\.]+){2,10}$/; break;
					case "FULLNAME":	re = /^[a-zA-Z]{1,}[ a-zA-Z'\-]{1,}$/; break;
					
					
					
					
					
					
					case "PASSWORD":	re = /^[a-zA-Z]{2}[\w\-_ ]{1,}$/; break;
					case "RETYPEPASSWORD":	re = /^[\w\-_ ]{6,}$/; break;
					
					case "PHONE":		re = /^\+?[-\d\(\) ]{5,}$/; break;
					
					case "FAX":		re = /^\+?[-\d\(\) ]{5,}$/; break;
					case "POSTCODE":	re = /([a-zA-Z][\w]{1,6})$/; break;
					
					case "MENU":		re = /^.+$/; break;
					
					case "TITLE":		re=/([\w-\s\,\.]+?)$/g; break;
					case "SERVICES":		re=/([\w-\s\,\.]+?)$/g; break;
					
					case "URL":			re = /^([\w-]+\.?)+(\/[\w- .\/?%&=]*)?/; break;
					case "USERNAME":	re = /^[\w\-_]{6,}$/; 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;
	}
}
