// Validate Form Script
//
// ********************************************************************
//
// This script is designed to perform a simple validation of a form.
// To use the form, call the method validateform() on the onSubmit event
// of the form.
// Any input boxes labelled as required must have a value or the method
// will return false and the form will not be submitted. It will also
// fire an alert box with a list of the fields which have been omitted.
//
// ********************************************************************

// When script is first included, determine if browser is compatible with
// running the validation. If not, just return true and use only server
// side validation
var compatible = (
	document.getElementById && document.getElementsByTagName 
	);

// variables to hold the error reports
// errorReport
var errorReport;
var requiredReport;
var lengthReport;

function validateForm()
{
	if (!compatible) return true;
	errorReport = "";
	requiredReport = "";
	lengthReport = "";
	
	var returnValue = true;
	
	var boxes = document.getElementsByTagName('input');
	for (var i=0;i<boxes.length;i++)
	{
		var check = boxes[i].getAttribute('validate');
		
		if(check=='required')
		{
			if(!checkRequired(boxes[i]))
			{
				returnValue = false;
			}
		}
		if(check=='length3')
		{
			if(!checkLength(boxes[i]))
			{
				returnValue = false;
			}
		}
	}
	
	if( !returnValue )
	{
		//errorReport = "The following errors have been found in the form:\n"
		if(requiredReport!="")
			errorReport += "The following required fields have not been filled in:\n" + requiredReport;
		if(lengthReport!="")
			errorReport += "\nThe following fields require at least three characters\n" + lengthReport;
		alert(errorReport);
	}
	return returnValue;
}

function checkRequired(field)
{
	if(field.value=="")
	{
		requiredReport += field.getAttribute('validateerror') + "\n";
		return false;
	}
	return true;
}

function checkLength(field)
{
	if(field.value.length<3)
	{
		lengthReport += field.getAttribute('validateerror') + "\n";
		return false;
	}
	return true
}