//
// FIELD VALIDATION CODE version 2.1.1
// courtesy www.richarddiamond.com
function genericFormValidate (theForm) {
	var i, sname, aname, errMessage, action, field, title, field2, title2, re;
	errMessage = "";
	for (var i=0;i <= theForm.elements.length;i++) {
		if (theForm.elements[i] && theForm.elements[i].name) {
			sname = theForm.elements[i].name;
			if (sname.indexOf ("_validate_") != -1 ) {
				field = sname.substring(0,sname.indexOf ("_validate_"));
action = sname.substr(sname.indexOf ("_validate_")+10);
				if (theForm.elements[field].title) {
					title = theForm.elements[field].title;
				} else {
					title = field;
				}
				switch (action) {
					case "required" :
						if (theForm.elements[field].value == "") {
							errMessage += title + " is a required field\n";
						};
						break;
					case "equalsfield" :
						field2 = theForm.elements[i].value;
						if (theForm.elements[field2].title) {
							title2 = theForm.elements[field2].title;
						} else {
							title2 = theForm.elements[field2].name;
						}
						if (theForm.elements[field].value != theForm.elements[field2].value) {
							errMessage += title + " must match " + title2 + "\n";
						}
						break;
					case "equals" :
						if (theForm.elements[i].title) {
							title2 = theForm.elements[i].title;
						} else {
							title2 = title + " must be '" + theForm.elements[i].value + "'";
						}
						if (theForm.elements[field].value != theForm.elements[i].value) {
							errMessage += title2 + "\n";
						}
						break;
					case "notequal" :
						if (theForm.elements[i].title) {
							title2 = theForm.elements[i].title;
						} else {
							title2 = title + " cannot be '" + theForm.elements[i].value + "'";
						}
						if (theForm.elements[field].value == theForm.elements[i].value) {
							errMessage += title2 + "\n";
						}
						break;
					case "regexp" :
						re = new RegExp(theForm.elements[i].value);
						if (theForm.elements[i].title) {
							title2 = theForm.elements[i].title;
						} else {
							title2 = "is not a valid entry";
						}
						if (theForm.elements[field].value && !theForm.elements[field].value.match(re)) {
							errMessage += title + " " + title2 +"\n";
						}
						break;
					case "date" :
						/* the only problem with doing it this way is that JavaScript automatically corrects dates that don't make sense to ASP (like 22/22/2000) so, maybe you're better off using a reg-exp                    */
						if (theForm.elements[field].value && Date.parse(theForm.elements[field].value) != Date.parse(theForm.elements[field].value)) {
							errMessage += title + " is not a valid date\n";
						}
//						var newDate = new Date(Date.parse(theForm.elements[field].value));
//						alert (newDate);
						break;
					case "radiorequired" :
						found = false;
						for (counter = 0; counter < theForm.elements[field].length; counter++)
						{
							if (theForm.elements[field][counter].checked)
								found = true; 
						}
						if (!found) {
							errMessage += theForm.elements[i].value + "\n";
						}
						break;
					case "selected" :
						if (theForm.elements[field].selectedIndex == 0) {
							if (theForm.elements[field].title) {
								errMessage += theForm.elements[field].title + " must be selected\n"
							} else {
								if (theForm.elements[i].title) {
									errMessage += theForm.elements[i].title + " must be selected\n"
								} else {
									errMessage += title + " must be selected\n"
								}
							}
						}
						break;
					case "checkgroup" :
						var a = theForm.elements[i].value.split ('|');
						var mincount = a[0];
						var maxcount = a[1];
						var thecount = 0;
						for (var j=2; j<a.length; j++) {
							if (theForm.elements[a[j]].checked) {
								thecount++;
							}
						};
						if (theForm.elements[i].title) {
							title2 = theForm.elements[i].title;
						} else {
							title2 = "requires a selection";
						}
						if ((mincount > thecount) || ((maxcount >= 0) && (thecount > maxcount))) {
							errMessage += title + " " + title2 + "\n";
						}
						break;
					default : ;
				}
			}	
		}
	}
	if (errMessage.length > 0) {
		alert (errMessage);
		return false;
	} else {
		return true;
	}
}
