//
// FIELD VALIDATION CODE version 2.1.2 (2010-03-08)
// courtesy www.richarddiamond.com
var validation_debug = window.location.search.toLowerCase().indexOf('validation_debug') > -1;
var normalizeactions = "|stripnonnumeric|trim|uppercase|stripfromstart|";
function genericFormValidate (theForm) {
	
	if(!$('.checkbox-indent').is(':checked'))
		{
			alert("Please check at least one item.");
  			return false;
		}

	normalizeFormData (theForm); //runs the data clean up function defined below

	var i, sname, aname, errMessage, validationtype, field, title, field2, title2, re,fieldToValidate,fieldToValidate2;
	errMessage = "";
	if (theForm && theForm.elements)
		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_"));
					validationtype = sname.substr(sname.indexOf ("_validate_")+10);
					
					fieldToValidate = theForm.elements[field]; // use for singletons--not selects, radios, etc.
					fieldToValidate2 = theForm.elements[field]; // for selects, radios, etc.
					if (fieldToValidate) {
						if (theForm.elements[field].length && (theForm.elements[field].length > 1)) {
							for (var j = 0; j < theForm.elements[field].length; j++) {
								if (!theForm.elements[field][j].disabled) {
									fieldToValidate = theForm.elements[field][j];
								}
							}
						}
						var tagstofind = String("|select|radio|");
						if (theForm.elements[field].length && (theForm.elements[field].length > 1) && (theForm.elements[field][0].tagName) && (tagstofind.indexOf(theForm.elements[field][0].tagName.toLowerCase()) > 0)  ) {
							for (var j = 0; j < theForm.elements[field].length; j++) {
								if (!theForm.elements[field][j].disabled) {
									fieldToValidate2 = theForm.elements[field][j];
								}
							}
						}
						if (fieldToValidate.title) {
							title = fieldToValidate.title;
						} else {
							title = field;
						}
						//alert(validationtype+"type")
					//alert(field+"field")
						switch (validationtype) {
							case "required" :
								if (fieldToValidate.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 (fieldToValidate.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 (fieldToValidate.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 (fieldToValidate.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 (fieldToValidate.value && !fieldToValidate.value.match(re)) {
									errMessage += title + " " + title2 +"\n";
								}
								break;
							case "notregexp" :
								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 (fieldToValidate.value && fieldToValidate.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 (fieldToValidate.value && Date.parse(fieldToValidate.value) != Date.parse(fieldToValidate.value)) {
									errMessage += title + " is not a valid date\n";
								}
		//						var newDate = new Date(Date.parse(fieldToValidate.value));
		//						alert (newDate);
								break;
							case "radiorequired" :
								found = false;
								for (counter = 0; counter < fieldToValidate2.length; counter++)
								{
									if (fieldToValidate2[counter].checked)
										found = true; 
								}
								if (!found) {
									errMessage += theForm.elements[i].value + "\n";
								}
								break;
							case "selected" :
								var isSelected = false
								if (fieldToValidate2.multiple) {
									var theoptions = fieldToValidate2.options;
									for (var z=0; z<theoptions.length; z++) {
										isSelected = isSelected || (theoptions[z].selected && (theoptions[z].value != ''));
									}
								} else {
									isSelected = ((fieldToValidate2.selectedIndex != 0) && (fieldToValidate2.options[fieldToValidate2.selectedIndex].value != ''));
								}
								if (!isSelected) {
									if (fieldToValidate2.title) {
										errMessage += fieldToValidate2.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;
							case "phonebycountry" :
								var CountryField;
								if (theForm.elements[i].value)
									CountryField = theForm.elements[theForm.elements[i].value];
								if (CountryField) {
									if ("|United States|Canada|Puerto Rico|".indexOf(CountryField.value) > 0) {
										re = /(\D*\d\D*){11}/; // must contain fewer than 11 digits, other characters may be present
										if (theForm.elements[i].title) {
											title2 = theForm.elements[i].title;
										} else {
											title2 = "is not a valid entry for your country";
										}
										if (fieldToValidate.value && fieldToValidate.value.match(re)) {
											errMessage += title + " " + title2 +"\n";
										}
									}
								}
								break;								
							default :
								if (normalizeactions && (normalizeactions.indexOf(validationtype) < 0))
									validateLogError("Unknown validation action in '" + sname + "'.");
						}
					}
				}	
			}
		}
	if (validation_debug && ValidationErrors && ValidationErrors.length > 0)
		alert("Validator Configuration Errors:\n\n" + ValidationErrors);
	if (errMessage.length > 0) {
		alert (errMessage);
		return false;
	} else {
		return true;
	}
}
function normalizeFormData (theForm) {
	var i, sname, aname, errMessage, validationtype, field, title, field2, title2, re, fieldToValidate, fieldToValidate2;
	errMessage = "";
	if (theForm && theForm.elements)
	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_"));
				validationtype = sname.substr(sname.indexOf ("_validate_")+10);
				fieldToValidate = theForm.elements[field]; // use for singletons--not selects, radios, etc.
				fieldToValidate2 = theForm.elements[field]; // for selects, radios, etc.
				if (fieldToValidate)
				{
					if (theForm.elements[field] && theForm.elements[field].length && (theForm.elements[field].length > 1)) {
						for (var j = 0; j < theForm.elements[field].length; j++) {
							if (!theForm.elements[field][j].disabled) {
								fieldToValidate = theForm.elements[field][j];
							}
						}
					}
					var tagstofind = String("|select|radio|");
					if (theForm.elements[field] && theForm.elements[field].length && (theForm.elements[field].length > 1) && (theForm.elements[field][0].tagName) && (tagstofind.indexOf(theForm.elements[field][0].tagName.toLowerCase()) > 0)  ) {
						for (var j = 0; j < theForm.elements[field].length; j++) {
							if (!theForm.elements[field][j].disabled) {
								fieldToValidate2 = theForm.elements[field][j];
							}
						}
					}
					if (fieldToValidate.title) {
						title = fieldToValidate.title;
					} else {
						title = field;
					}
					switch (validationtype) {
						case "stripnonnumeric" :
							var sOutput = "";
							var sInput = fieldToValidate.value;
							re = /\d/g;
							while (result = re.exec(sInput)) {
								sOutput += result[0];
							}
							fieldToValidate.value = sOutput;
							break;
						case "trim" :
							if (theForm.elements[i].value == "left") 
								fieldToValidate.value = fieldToValidate.value.replace(/^\s+/,"");
							else if (theForm.elements[i].value == "right")
								fieldToValidate.value = fieldToValidate.value.replace(/\s+$/,"");
							else
								fieldToValidate.value = fieldToValidate.value.replace(/^\s+|\s+$/g,"");
							break;
						case "uppercase" :
							if ((theForm.elements[i].value == 1) && fieldToValidate.value.length > 1)
								fieldToValidate.value = fieldToValidate.value.substr(0,1).toUpperCase() + fieldToValidate.value.substr(1);
							else
								fieldToValidate.value = fieldToValidate.value.toUpperCase();
							break;
						case "stripfromstart" :
								if (theForm.elements[i].value && theForm.elements[i].value.length > 0) 
									re = new RegExp('^' + theForm.elements[i].value,'i');
								else
									re = new RegExp ('^(\\s*(Mr|Dr|Ms|Mrs|Rev|Prof)(\\.|\\s)+)+','i');
								fieldToValidate.value = fieldToValidate.value.replace(re,"");
	//							fieldToValidate.value = fieldToValidate.value.replace(/^(\s*(Mr|Dr|Ms|Mrs|Rev|Prof)(\.|\s)+)+/i,"");
							break;					
						default : 
							;
					}
				}	else
					validateLogError("Form field '" + field + "' not found in form.");
			}
		}
	}
	else
		validateLogError("Form contains no elements.  Did you use 'this' when you meant 'this.form' or a named form?");
	return true;
}
var ValidationErrors = "";
function validateLogError (s) {
	if (validation_debug)
		ValidationErrors += s + "\n";
}

