// General form validation routines


// Return the error message associated with the element e.
function getMsg(e) {
	
	var msg;

	// Idiosyncratic error messages can be assigned in the form:
//	if (e.msg) {
//		msg = e.msg;
//	}
//	else {
		// These steps imposes a naming convention on form elements that don't have a special error message applied.
		// E.g., first_name => First name
		msg = e.name.replace(/_/g, " ");
		// In Perl this is just \u...
		// msg = msg.replace(/^(.)/, $1.toUpperCase()) doesn't work
		msg = msg.substring(0,1).toUpperCase() + msg.substring(1,msg.length);
		if (e.msg) msg += " " + e.msg;

//	}
	//return msg + "\n\n";	
	return msg ;	
}



// Validate form submission data.  "this" is the form object.
// If any form data is invalid, generate an alert and return false; else return true.
function verifySubmit() {
	var msg = "";
	var msg1 = "";
	var emptyFields = "";
	var formatErrors = "";
	var otherErrors = "";
	
	document.getElementById('error').innerHTML = "";

	var ls="";
	for (var i = 0; i < this.length; i++) { //This clears the red from the form
		var e = this.elements[i];
		ls += e.name+" ";		
		removeErrorFromNearestLabel(e);
		removeerror(e);
	}
//alert(ls);

	for (var i = 0; i < this.length; i++) {
		var e = this.elements[i];
		var t = e.type;

		//if (e.isValid == "ignore") continue;
		if (e.ignore) continue;
		if (e.isValid != null) {
			if (!eval(e.isValid)(e)) {
//alert("validating: "+ i+ e.name);
				if (formatErrors) formatErrors += ", ";
				formatErrors += getMsg(e);
				e.className += " error";
				addErrorToNearestLabel(e);
			}
		}

		// Input text and textareas:
		//if ( e.type == "text" || e.type == "textarea" ) {
		//var t = e.getAttribute("type");
		else if (t == "text" || t == "textarea" || t =="password") {
			if (isblank(e.value)) {   // 1. Check for blank required elements
				if (!e.optional) {
					if (emptyFields) emptyFields += ", ";
					emptyFields += getMsg(e);
					addErrorToNearestLabel(e);
					continue;
				}
			}
			// 2. Non-blank form fields: validate as specified in the form validation rules
			else if (e.validate != null) {
			
				switch (e.validate) {
				
					case "state": 
						if (isValidState(e.value)) {
							continue;
						}
						break;
						
					case "email": 
						if (isValidEmail(e.value)) {
							continue;
						}
						break;
						
					case "zipcode":
						if (isValidZipcode(e.value)) {
							continue;
						}
						break;
						
					case "phone_number":
						if (isValidPhoneNumber(e.value)) {
							continue;
						}
						break;
					
					case "Telephone_number":
						if (isValidPhoneNumber(e.value)) {
							continue;
						}
						break;
						
					case "year":
						if (isYear(e.value)) {
							continue;
						}
						break;
						
					case "date_mmyyyy":
						if (isDate_mmyyyy(e.value)) {
							continue;
						}
						break;			
						
					case "date_mmddyyyy":
						if (isDate_mmddyyyy(e.value)) {
							continue;
						}
						break;
						
					case "int":
						if (isInt(e.value)) {
							continue;
						}
						break;
						
					case "posint":
						if (isInt(e.value) && e.value>=0) {
							continue;
						}
						break;
						
					case "float":
						if (isFloat(e.value)) {
							continue;
						}
						break;
						
					default: 
						continue;
						break;					
				}
				if (formatErrors) formatErrors += ", ";
//	alert(e.name);
				formatErrors += getMsg(e);
				addErrorToNearestLabel(e);
				e.className += " error";
			}	
		}
		
		// Radio buttons and checkboxes
	    else if (t == "radio" || t == "checkbox") {
			var elementName = e.name;
			// Check only once per group of radio buttons/checkboxes.
			// Optionality and special error messages are defined as attributes of the first element
			// of the array.
			if (e != this[elementName][0]) continue;
			
			//removeerror(e.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('h6').item( 0 ));
			///removeErrorFromNearestLabel(e);
			if (!e.optional) {
				if (!optionSelected(this, elementName)) {
					if (emptyFields) emptyFields += ", ";
					emptyFields += getMsg(e);
					//e.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('h6').item( 0 ).className += " error";
					///removeErrorFromNearestLabel(e);
				}
			}
		}
	}
	
	// Execute form-specific validation code
	if (this.validationFunction) {
		// Another strategy: always name the function validateForm, and put each one in
		// a different file. The right file will be determined by the <script> tag.	
	
		// Can't figure out syntax for calling the function without passing "this" as argument
		// Not: otherErrors = eval(this.validationFunction)();
alert("other");
		if (formatErrors) formatErrors += ", ";
		formatErrors = eval(this.validationFunction)(this); 
		
	}

	
	if (!emptyFields && !formatErrors && !otherErrors) return true;
	
	// Report the errors
	//var divider = "-----------------------------------------------------------------------\n\n";
	var divider = "<p>-----------------------------------------------------------------------</p>";
	
	msg = "<strong>The form was not submitted due to errors or missing information in the input.\n" +
		  "Please correct the items marked in red and resubmit the form." ;
		  //+ divider;
	window.scrollTo(280, 485);
	msg1="";
	if (emptyFields) {
		msg += "<br><span class=\"errordetail\">The following required information has not been specified:\n" + emptyFields + "</span>\n";
		//msg += "<p>The following required information has not been specified:</p>" + emptyFields + "\n";
	}
	if (formatErrors) {
		msg += "<br><span class=\"errordetail\">The following input has an invalid format:\n\n" + formatErrors + "</span>\n";
		//msg += "<p>The following input has an invalid format:</p>" + formatErrors + divider;
	}
	if (otherErrors) {
		msg += "Miscellaneous errors:\n" + otherErrors + "\n";
		//msg += "<p>Miscellaneous errors:</p>" + otherErrors + divider;
	}
	
	//msg += " <a href=\"onclick=\'alert(\"BOO\');\" >(Details)</a>";
	document.getElementById('error').innerHTML =  msg + "</strong><br /><br /><br />";
	return false; 
}



/*******************************************************************************
Another way of handling radio buttons/checkboxes - handle each element as it
passes through the main loop.  checkedOptions is declared at top of function as
checkedOptions = new Array();

		else if ( e.type == "radio" || e.type == "checkbox" ) {
		
			var elementName = e.name;
			var firstElement = formObj[elementName][0];
			var lastElement = formObj[elementName][formObj[elementName].length - 1];
			
			// Optionality for the group is defined as an attribute of the first element, for convenience.
			if (!firstElement.optional) {	
				// We've already seen a checked option for this group
				if (checkedOptions[elementName] == true) {
					continue;
				}
				// If this element is checked, add its name to the array of checked options
				else if (e.checked == true) {
					checkedOptions[elementName] = true;
				}
				// If we've reached the last element and no options have been checked, issue an error message
				else if (e == lastElement) {
					// An error message for the group is defined as an attribute of the first 
					// element, for convenience.
					emptyFields += getMsg(firstElement);
					
				}
			}
		}
********************************************************************************/
