// validation for online forms
/*
*** USAGE
in page header (path to javascript file may need to be adjusted):
	<script language="JavaScript" src="../js/formvalidation.js"></script>
in form onSubmit function (sample with one required field specified ['name']; also always verifies 'email' field if it exists; you may need to fix the response file name):
	<form name="form" method="post" onSubmit="this.name.required=true; return verify(this);" action="/cgi-win/mailpost.exe/mptemplates/parks/responsefile.txt">
*/

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;
}

function verify(f)
{
	var msg;
	var empty_fields = "";
	var errors = "";
	var emailerror = "";
	for(var i = 0; i < f.length; i++)
	{
		var e = f.elements[i];
		if ((e.name == "email") && ((e.type == "text") || (e.type == "textarea")))
		{
			// if there is a field named email, it must have min 6 chars including @ and .
		    if (e.length <6 || e.value.indexOf ('@',0) == -1 || e.value.indexOf ('.',0) == -1)
			{
				emailerror = "· Please enter a valid email address.\n";
				continue;
			}
		}
		if ((e.type == "checkbox") && (e.checked == false))
		{ 
		    e.value = " ";
		}
		if (((e.type == "text") || (e.type == "textarea")) && (e.required))
		{
			// check for blank field
			if ((e.value == null) || (e.value == "") || isblank(e.value))
			{
				empty_fields += "\n-- " + e.name;
				continue;
			}
			if ((e.numeric == true) || (e.getAttribute("min") != null) || (e.getAttribute("max") != null))
			{
				var v = parseFloat(e.value);
				if (isNaN(v) || ((e.min != null) && (v < e.min)) || ((e.max != null) && (v > e.max)))
				{
					errors += "- " + e.name + " must be a number";
					if (e.min != null)
						errors += " that is greater than " + e.min;
					if (e.max != null && e.min != null)
						errors += " and is less than " + e.max;
					else if (e.max != null)
						errors += " that is less than " + e.max;
						errors += ".\n";
				}
			}
		}
	}
	if (!empty_fields && !errors  && !emailerror) return true;
	msg = "This form was not submitted because of the following error(s).\n";
	msg += "Please correct the error(s) and re-submit.\n\n";
	if (empty_fields) {
		msg += "· The following required field(s) are empty:" + empty_fields + "\n";
		if (errors) msg += "\n";
	}
	msg += errors;
	msg += emailerror;
	alert(msg);
	return false;
}

function blankCheckBoxes(inputItem) {
		with (inputItem.form) {
			// process each of the different input types in the form.
			if (inputItem.type == "radio") { // process radio buttons
				// subtract the previously selected radio button value from the total
				calculatedTotal.value = eval(calculatedTotal.value) - eval(previouslySelectedRadioButton.value);
				// save the current radio selection value
				previouslySelectedRadioButton.value = eval(inputItem.value);
				// add the current radio button selection value to the total
				calculatedTotal.value = eval(calculatedTotal.value) + eval(inputItem.value);	
			} else { // process check boxes
				if (inputItem.checked == false) { // item was uncheck. subtract item value from total
				    calculatedTotal.value = eval(calculatedTotal.value) - eval(inputItem.value);
				} else { // item was checked. add the item value to the total
				    calculatedTotal.value = eval(calculatedTotal.value) + eval(inputItem.value); 
				}
			}

			// total value should never be less than 0
			if (calculatedTotal.value < 0) {
				InitForm();
			}

			// return total value
			return(formatCurrency(calculatedTotal.value));
		}
	}

