
//

// a utility function that returns true if a string contains only
//whitespace characters.  To validate required fields.
function isblank(s)
{
	for(var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

// This is the function that performs form verification.  It will
// be invoked from the onSubmit() event handler.  The handler should return whatever
// value this function returns.
function verify(f)
{
	var strmsg;
	var empty_fields = "";
	var empty_billing_fields = "";
	var strerrors = "";
	
	if ((!document.registration.samebilling.checked) &&
			(document.registration.billingFirstName.value == ""
			|| document.registration.billingLastName.value == ""
			|| document.registration.billingAddress.value == ""
			|| document.registration.billingCity.value == ""
			|| document.registration.billingState.value == ""
			|| document.registration.billingPostalCode.value == ""
			|| document.registration.billingCountry.value == "")) {
			
			if (document.registration.billingFirstName.value == "") {
				empty_billing_fields += "\n          " + document.registration.billingFirstName.name;
				}
			if (document.registration.billingLastName.value == "") {
				empty_billing_fields += "\n          " + document.registration.billingLastName.name;
				}
			if (document.registration.billingAddress.value == "") {
				empty_billing_fields += "\n          " + document.registration.billingAddress.name;
				}
			if (document.registration.billingCity.value == "") {
				empty_billing_fields += "\n          " + document.registration.billingCity.name;
				}
			if (document.registration.billingState.value == "") {
				empty_billing_fields += "\n          " + document.registration.billingState.name;
				}
			if (document.registration.billingPostalCode.value == "") {
				empty_billing_fields += "\n          " + document.registration.billingPostalCode.name;
				}
			if (document.registration.billingCountry.value == "") {
				empty_billing_fields += "\n          " + document.registration.billingCountry.name;
				}
	}
	
	// Loop through the elements of the form, looking for all
	// text and textarea elements that don't have an "optional" property
	// defined.  Then, check for fields that are empty and make a list of them.
	// Also, if any of these elements have a "min" or "max" property defined,
	// then verify that they are numbers and that they are in the right range.
	// Put together error messages for fields that are wrong.
	
	for(var i = 0; i < f.length; i++) {
		var e = f.elements[i];
		
		if (((e.type == "text") || (e.type == "textarea")) && (!e.optional))  { 
			
			// Now check if the field is empty
			if ((e.value == null) || (e.value == "") || isblank(e.value)) {
				empty_fields += "\n          " + e.name;
				continue;
			}
			
			// Now check for fields that are supposed to be numeric.
			if (e.numeric || (e.min != null) || (e.max != null)) { 
				var v = parseFloat(e.value);
				if (isNaN(v) ||
					((e.min != null) && (v < e.min)) ||
					((e.max != null) && (v > e.max))) {
					strerrors += "- The field " + e.name + " must be a number";
					if (e.min != null)
						strerrors += " that is greater than " + e.min;
					if (e.max != null && e.min != null)
						strerrors += " and less than " + e.max;
					else if (e.max != null)
						strerrors += " that is less than " + e.max;
					strerrors += ".\n";
				}
			}
		}
	}
	
	// Now, if there were any strerrors, display the messages, and
	// return false to prevent the form from being submitted.
	// Otherwise return true.
	if (!empty_fields && !strerrors) return true;
	
	strmsg  = "______________________________________________________\n\n"
	strmsg += "The form was not submitted because of the following error(s).\n";
	strmsg += "Please correct these error(s) and re-submit.\n";
	strmsg += "______________________________________________________\n\n"
	
	if (empty_fields) {
		strmsg += "- The following required field(s) are empty:" + empty_fields + "\n" + empty_billing_fields + "\n";

		if (strerrors) strmsg += "\n";
		}
		strmsg += strerrors;
		alert(strmsg);
		return false;
	}


