// Filename: valid.js
// Project : Javascript Library
//
// Description:
// Validation routines for form processing.
//
// Revision History:
// 12FEB2001 - MJT - Initial development.
// 29JUL2001 - MJT - Added the email address validation function

// VAL_Required
// Verifies that the required fields has been filled out.
// strForm: name of the form to test
// strField: name of the form field to test
// returns: true if the required value is filled out, false if it is empty
function VAL_Required( strForm, strField )
{
	var f = document.forms[ strForm ];
	return ( f[ strField ].value.length > 0 );
}

// VAL_RequiredList
// Accepts a list of fields to be tested with the list as a CSV
// strForm: name of the for to test
// strFieldList: comma delimited list of fields
// blnNotify: true causes error alert box to be raised.
// returns: true if all elements are ok
function VAL_RequiredList( strForm, strFieldList, blnNotify )
{
	var arrFields = strFieldList.split( ',' );
	var i;
	for ( i=0; i < arrFields.length; i++ )
	{
		if ( arrFields[i] != '' )
		{
			if (!VAL_Required( strForm, arrFields[i] ) )
			{
				if (blnNotify)
				{
					alert( 'Please complete the required fields before submitting the form.' );
				}
				return false;
			}
		}
	}
	return true;
}

// VAL_EMailAddress
// Verifies that the supplied string appears to be a valid e-mail address with an
// optional message box if it doesn't
// An e-mail address needs to look roughly like:  name@domain.ext
// where name is not empty, @ is found and to the right of the @ is a domain and at least one
// extension that is not empty
// strValue: string to test
// blnNotify: if true show message box if fails
// returns: true if the string looks like a valid e-mail address
function VAL_EMailAddress( strValue, blnNotify )
{
	var err = 0;  		// figure out what went wrong
	var intAtPos;		// string position of the at sign
	var intDotPos;		// string position of the domain separator

	if ( strValue.length == 0 )
	{
		err = 1; // empty string
	}
	else
	{
		// at sign must not be the first character
		intAtPos = strValue.indexOf( '@', 1 );
		if ( intAtPos == -1 )
		{
			err = 2;
		}
		else
		{
			// dot must come after the @ sign
			intDotPos = strValue.indexOf( '.', intAtPos );
			if ( intDotPos == -1 )
			{
				err = 3;
			}
		}
	}
	// Handle errors 
	if ( err != 0 )
	{
		if ( blnNotify )
		{
			alert( 'Please enter a valid e-mail address in \'name@domain.ext\' format. ['  + err  + ']' );
		}
		return false;
	}
	else
	{
		return true;
	}
}

// VAL_Number
// Verifies that the supplied string is a number.  If min and max are different, it verifies that
// the number is in range min <= value <= max.  If min and max are the same, range checking is
// ignored.
// strValue: Value to check
// dblMin: Minimum value
// dblMax: Maximum value
// blnNotify: throw up a message box if validation fails
// returns: true if the value is a number
function VAL_Number( strValue, dblMin, dblMax, blnNotify )
{
	// check for the case where the field is empty
	if ( ( strValue == null ) || ( strValue == "" ) )
	{
		if ( blnNotify )
		{
			alert( 'Please complete the required fields.' );
		}
		return false;
	}

	// parse a float and check results
	var dblValue = parseFloat( strValue );
	if ( isNaN( dblValue ) )
	{
		if ( blnNotify )
		{
			alert( 'Please enter a valid number.' );
		}
		return false;
	}
	// make sure its in range - if range is active
	if ( dblMin != dblMax )
	{
		if ( ( dblValue < dblMin ) || ( dblValue > dblMax ) )
		{
			if ( blnNotify )
			{
				alert( 'Please enter a number between ' + dblMin + ' and ' + dblMax );
			}
			return false;
		}
	}
	return true;
}

// VAL_Integer
// Verifies that the supplied string is an integer.  If min and max are different, it verifies that
// the number is in range min <= value <= max.  If min and max are the same, range checking is
// ignored.
// strValue: Value to check
// intMin: Minimum value
// intMax: Maximum value
// blnNotify: throw up a message box if validation fails
// returns: true if the value is a number
function VAL_Integer( strValue, intMin, intMax, blnNotify )
{
	// check for the case where the field is empty
	if ( ( strValue == null ) || ( strValue == "" ) )
	{
		if ( blnNotify )
		{
			alert( 'Please complete the required fields.' );
		}
		return false;
	}

	// parse a float and check results
	var intValue = parseInt( strValue );
	if ( isNaN( intValue ) )
	{
		if ( blnNotify )
		{
			alert( 'Please enter a valid number.' );
		}
		return false;
	}
	// make sure its in range - if range is active
	if ( intMin != intMax )
	{
		if ( ( intValue < intMin ) || ( intValue > intMax ) )
		{
			if ( blnNotify )
			{
				alert( 'Please enter a number between ' + intMin + ' and ' + intMax );
			}
			return false;
		}
	}
	return true;
}

// VAL_FieldValue
// Get the value of a field on a form or NULL if does not exist.
// strForm: name of the for to test
// strField: checkbox to test
// returns: value or NULL on failure
function VAL_FieldValue( strForm, strField )
{
	var f = document.forms[ strForm ];
	var fld;
	
	if ( fld = f[ strField] )
	{
		return fld.value;
	}
	else
	{
		return NULL;
	}
}

// VAL_RadioRequired
// Verifies that one of a set of radio buttons is checked
// strForm: name of the for to test
// strField: checkbox to test
// returns: true if checkbox is selected
function VAL_RadioRequired( strForm, strField )
{
	var f = document.forms[ strForm ];
	var fld;
	var i;
	
	if ( fld = f[ strField ] )
	{
		for ( i=0; i < fld.length; i++ )
		{
			if ( fld[i].checked )
			{
				return true;
			}
		}
	}
	return false;
}

// VAL_CheckRequired
// Verifies that a checkbox is checked
// strForm: name of the for to test
// strField: checkbox to test
// returns: true if checkbox is selected
function VAL_CheckRequired( strForm, strField )
{
	var f = document.forms[ strForm ];
	var fld;
	var i;
	
	if ( fld = f[ strField ] )
	{
		if ( fld.checked )
		{
			return true;
		}
	}
	return false;
}

