
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 ValidateForm(f)
{
	var msg;
	var empty_fields = "";
	var errors = "";
	var prevel = "";
	var chkd = false;
	var dummy = 0;
// Loop thru 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 a "max" property defined, then verify that they
// are numbers and 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.id != prevel)
		{
			if ((prevel != "") && !chkd)
			{
				empty_fields += "\n          " + prevel;
			}
			if ((e.type == "checkbox") && !e.optional)
			{
				prevel = e.id;
			}
			else
			{
				prevel = "";
			}
			chkd = false;
		}
		
		if ((e.type == "text") || (e.type == "textarea"))
		{
			if ((e.optional) || (e.id.slice(0, 3) == "opt"))
			{
				dummy = 0;
			}
			else
			{
				// check if field is empty
				if ((e.value == null) || (e.value == "") || isblank(e.value))
				{
					empty_fields += "\n          " + e.id;
					continue;
				}
			}
			// next check field length
			if ((e.maxlength > 0) && (e.value.length > e.maxlength))
			{
				errors += "- The field " + e.id + " is limited to ";
				errors += e.maxlength + " or fewer characters";
				errors += ".\n";
				continue;
			}
			// then ensure no apostrophe in text
			if (e.value.indexOf("'") > -1)
			{
				errors += "- The field " + e.id + " may not ";
				errors += "contain an apostrophe";
				errors += ".\n";
				continue;
			}
			// now check numeric fields
			if (e.numberonly)
			{
				var v = parseFloat(e.value);
				if (isNaN(v) && !isblank(e.value))
				{
					errors += "- The field " + e.id + " must be a number.";
					errors += ".\n";
					continue;
				}
				if (((e.min != null) && (v < e.min)) || ((e.max != null) && (v > e.max)))
				{
					errors += "- The field " + e.id + " must be a number";
					if (e.min != null) errors += " that is greater than " + e.min;
					if (e.max != null && e.min !=null) errors += " and less than " + e.max;
					else if (e.max != null) errors += " that is less than " + e.max;
					errors += ".\n";
				}
			}
		}
		else if ((e.type == "select-one") && !e.optional)
		{
			if ((e.value == "0") || (e.value == " "))
			{
				empty_fields += "\n          " + e.id;
			}
		}
		else if (e.type == "checkbox")
		{
			if (e.checked)
			{
				chkd = true;
			}
		}
	}
	// if any errors were found, display message and return false
	if (!empty_fields && !errors) return true;
	
	msg  = "______________________________________________________\n\n";
	msg += "The form was not submitted because of the following error(s).\n";
	msg += "Please correct the error(s) and re-submit.\n";
	msg += "______________________________________________________\n\n";
	
	if (empty_fields)
	{
		msg += "- The following required field(s) are empty :" + empty_fields + "\n\n";
	}
	msg += errors;
	alert(msg);
	return false;		
}
