// Written by alden taylor (aldent@thesustainablevillage.com)

// This script works in conjunction with the formmail.pl program
// used to email contents of a form to a given recipient.  This 
// function simply checks for required fields, verifies that those
// fields are not blank, prompts the user for missing fields, and if
// all fields are complete, allows the form to be submitted.  

// PRECONDITION:  CheckFormMail is passed the form in the following manner:
// <form name="foo" onSubmit="return checkFormMail(this);">
// Both the onSubmit event handler and the name attribute are required for the
// function to work.  

// checks to see if a field contains spaces tabs or newlines but no
// characters
function isBlank(s) {
	for (var i =0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != " ") && (c != "\t") && (c != "\n")) return false;
	}
	return true;
}

function checkFormMail (f) {
	var reqFields = f.required.value;
	var reqFieldArray = reqFields.split(",");
	var required = "";
	for ( var ind = 0; ind < reqFieldArray.length; ind++) {
		var e = reqFieldArray[ind];
		// remove any whitespace

		var fieldname = eval("document." + f.name + "." + e);
		if (fieldname.value == "" || fieldname.value == null || isBlank(fieldname.value)) {
			required += e + "\n";
		}
	}
	if (required != "") {
		var s = "The following fields are required for completion ";
		s += "of this form and have not been filled out.\n\n";
		s += required + "\n";
		s += "Please fill out these fields before proceeding.\n";
		window.alert(s);
		return false;
	}
	return true;
}
