// form_validation.js 20050416 (c) 2005 cmonster .
// *depends* on previously declared array fieldList_ .
// entries are of the form [field_name,isRequired,type,nameOfFieldToCompare].
// possible values for field_name can be any field in the form to validate.
// possible values for isRequired: 'true' - field MUST not be empty, 'false' - can be empty
// possible values for (optional) type: 'int','email','ischecked','equalsfield'.
// possible values for (optional) nameOfFieldToCompare - any other field in the form to compare values with.
// typical entries:
//
// var fieldList_ = new Array();
// fieldList_.push( ['field_name_of_required_field',true] );
// fieldList_.push( ['name_of_nonrequired_int_field',false,'int'] );
// fieldList_.push( ['name_of_email_field',true,'email'] );
// fieldList_.push( ['name_of_checkbox',true,'ischecked'] );
// fieldList_.push( ['confirm',true,'equalsfield','password'] );
//
// usage: <FORM ACTION="some.php" METHOD="POST" onSubmit="return validateForm( this )">
//
//
var validateMissingFields = true; // set true for debug

function isInt( aField )
{
	if( isNaN( aField.value ) )
	{
		alert( aField.name.toUpperCase() + ' must be a number.' );
		aField.focus();
		return false;
	}
	return true;
}

function isEmail( aField )
{
	var em = aField.value;
	var re = /^.+\@.+\..+$/
	if( ! re.test( em ) )
	{
		alert( aField.name.toUpperCase() + ' appears to be incorrectly formatted.' );
		aField.focus();
		return false;
	}
	return true;
}

function isChecked( aField )
{
	if( ! aField.checked )
	{
		alert( aField.name.toUpperCase() + ' must be checked.' );
		aField.focus();
		return false;
	}
	return true;
}

function isEqualToField( aField, otherFieldName )
{
	var otherField = aField.form.elements[ otherFieldName ];
	if( aField.value != otherField.value )
	{
		alert( aField.name.toUpperCase() + ' must match ' + otherField.name.toUpperCase() );
		aField.focus();
		return false;
	}
	return true;
}	

function isValid( aField, fieldListEntry )
{
	var typ = fieldListEntry[2];
	if( typ )
	{
		if( typ == 'int' && ! isInt( aField ) ) return false;
		if( typ == 'email' && ! isEmail( aField ) ) return false;
		if( typ == 'ischecked' && ! isChecked( aField ) ) return false;
		if( typ == 'equalsfield' && ! isEqualToField( aField, fieldListEntry[3] ) ) return false;
	}
	return true;
}

function validateField( aField )
{
	var fld = aField;
	for( var i=0; i < fieldList_.length; i++ )
	{
		var entry = fieldList_[i];
		if( entry[0] != fld.name ) continue;
		if( ! isValid( fld, entry ) ) return;
	}
}

function validateForm( theForm )
{	
	var f = theForm;
	var fld;
	for( var i=0; i < fieldList_.length; i++ )
	{
		var entry = fieldList_[i];
		fld = f[ entry[0] ];
		if( ! fld ) // catch incorrect fieldList entries:
		{
			if( ! validateMissingFields ) continue; // skip if not debug.
			alert( entry[0] + ' is missing.' );
			return false;
		}
		var isRequired = entry[1];

		// 'null' check for unchosen SELECT
		if( isRequired && ( fld.value == '' || fld.value == 'null' ) )
		{
			alert( fld.name.toUpperCase() + ' is a required field.' );
			fld.focus();
			return false;
		}

		if( fld.value != '' )
		{
			if( ! isValid( fld, entry ) ) return false;
		}
	}
	return true;
}
