/*****************************/
/* Data validation functions */
/*****************************/
function Require ( field, descr )
{
	//Handle drop downs
	if ( field.type == 'select-one' )
	{
		if ( SelectedValue ( field ) == '' )
		{
			field.focus ( );
			alert ( descr + ' is a required field.' );
			return -1;
		}
	}
	else
	{
		//Trim the value
		eval ( field ).value = Trim ( eval ( field ).value );
	
		//Be sure the field is not empty
		if ( eval ( field ).value == '' || eval ( field ).value == null )
		{
			field.focus ( );
			alert ( descr + ' is a required field.' );
			return -1;
		}
	}

	//Return good
	return 1
}

function IsNumber ( field, descr )
{
	//Trim the value
	eval ( field ).value = Trim ( eval ( field ).value );
	
	//If the field is empty, then return good
	if ( eval ( field ).value == '' ) return true;

	//If not a number, then return
	if ( isNaN ( eval ( field ).value ) )
	{
		field.focus ( );
		field.select ( );
		if ( descr != '' ) alert ( descr + ' must be a number.' );
		return false;
	}
	
	//Return true
	return true;
}
function IsNumber2 ( field, descr ) //Allows commas
{
	//Trim the value
	eval ( field ).value = Trim ( eval ( field ).value );
	
	//If the field is empty, then return good
	if ( eval ( field ).value == '' ) return true;

	//Loop thru the characters
	ndx = 0
	while ( ndx < eval ( field ).value.length )
	{
		if ( eval ( field ).value.substr ( ndx, 1 ) != ',' && isNaN ( eval ( field ).value.substr ( ndx, 1 ) ) )
		{
			field.focus ( );
			field.select ( );
			if ( descr != '' ) alert ( descr + ' must be a number.' );
			return false;
		}
		ndx++;
	}
	
	//Return true
	return true;
}

function IsNumber3 ( field, descr ) //Allows commas
{
	//Trim the value
	eval ( field ).value = Trim ( eval ( field ).value );
	
	//If the field is empty, then return good
	if ( eval ( field ).value == '' ) return true;

	//Loop thru the characters
	ndx = 0
	while ( ndx < eval ( field ).value.length )
	{
		if ( eval ( field ).value.substr ( ndx, 1 ) != ',' && isNaN ( eval ( field ).value.substr ( ndx, 1 ) ) )
		{
			field.focus ( );
			field.select ( );
			if ( descr != '' ) alert ( descr + ': ONLY NUMBERS IN THIS BOX.  NO DOLLAR SIGNS OR DECIMAL SIGNS.' );
			return false;
		}
		ndx++;
	}
	
	//Return true
	return true;
}

function IsEmail ( field, descr )
{
	//Trim the value
	eval ( field ).value = Trim ( eval ( field ).value );
	
	//If the field is empty, then return good
	if ( eval ( field ).value == '' ) return true;

	//Be sure there is an "@" and a "."
	if ( eval ( field ).value.indexOf ( '@' ) == -1 || eval ( field ).value.indexOf ( '.' ) == -1 )
	{
		field.focus ( );
		field.select ( );
		if ( descr != '' ) alert ( descr + ' is not a valid email address.' );
		return false;
	}
	
	//Be sure there are no disallowed characters
	if (	eval ( field ).value.indexOf ( '\\' ) != -1 ||
		eval ( field ).value.indexOf ( '/' ) != -1 ||
		eval ( field ).value.indexOf ( ',' ) != -1 ||
		eval ( field ).value.indexOf ( '\'' ) != -1 ||
		eval ( field ).value.indexOf ( '"' ) != -1 ||
		eval ( field ).value.indexOf ( '?' ) != -1 ||
		eval ( field ).value.indexOf ( '*' ) != -1 )
	{
		field.focus ( );
		field.select ( );
		if ( descr != '' ) alert ( descr + ' is not a valid email address.' );
		return false;
	}
	
	//Return true
	return true;
}


/***********************************************/
/* Validate dates.  Verifies leap year as well */
/***********************************************/
function IsDate ( year, month, day, descr )
{
	//Trim the values
	eval ( year ).value = Trim ( eval ( year ).value );
	eval ( month ).value = Trim ( eval ( month ).value );
	eval ( day ).value = Trim ( eval ( day ).value );

	//If the fields are all empty, then return good
	if (	eval ( year ).value + eval ( month ).value + eval ( day ).value == '' ) return true;

	//Be sure all fields are numbers
	if ( isNaN ( eval ( month ).value ) )
	{
		month.focus ( );
		month.select ( );
		alert ( descr + ' is not a valid date.\nThe month is not a number.' );
		return false;
	}

	if ( isNaN ( eval ( day ).value ) )
	{
		day.focus ( );
		day.select ( );
		alert ( descr + ' is not a valid date.\nThe day is not a number.' );
		return false;
	}

	if ( isNaN ( eval ( year ).value ) )
	{
		year.focus ( );
		year.select ( );
		alert ( descr + ' is not a valid date.\nThe year is not a number.' );
		return false;
	}
	
	//Since some of the data is there, be sure that ALL is there
	if ( eval ( month ).value == '' )
	{
		month.focus ( );
		month.select ( );
		alert ( descr + ' is not a valid date.\nThe month can not be blank.' );
		return false;
	}
	if ( eval ( day ).value == '' )
	{
		day.focus ( );
		day.select ( );
		alert ( descr + ' is not a valid date.\nThe day can not be blank.' );
		return false;
	}
	if ( eval ( year ).value == '' )
	{
		year.focus ( );
		year.select ( );
		alert ( descr + ' is not a valid date.\nThe year can not be blank.' );
		return false;
	}

	//Covert strings to numbers
	iDay   = parseInt ( eval ( day   ).value, 10 );
	iMonth = parseInt ( eval ( month ).value, 10 );
	iYear  = parseInt ( eval ( year  ).value, 10 );

	//Validate the month
	if ( iMonth < 1 || iMonth > 12 )
	{
		month.focus ( );
		month.select ( );
		alert ( descr + ' is not a valid date.\nMonth must be a number between 1 and 12.' );
		return false;
	}

	//Validate the year
	if ( eval ( year ).value.length != 4 )
	{
		year.focus ( );
		year.select ( );
		alert ( descr + ' is not a valid date.\nYear must be 4 digits.' );
		return false;
	}

	//Figure out number of days in month
	if ( iMonth == 1 ) iDays = 31;
	if ( iMonth == 3 ) iDays = 31;
	if ( iMonth == 4 ) iDays = 30;
	if ( iMonth == 5 ) iDays = 31;
	if ( iMonth == 6 ) iDays = 30;
	if ( iMonth == 7 ) iDays = 31;
	if ( iMonth == 8 ) iDays = 31;
	if ( iMonth == 9 ) iDays = 30;
	if ( iMonth == 10 ) iDays = 31;
	if ( iMonth == 11 ) iDays = 30;
	if ( iMonth == 12 ) iDays = 31;

	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	if ( iMonth == 2 )
	{
		iDays = 28;
		if ( ( (iYear % 4) == 0 ) && (((iYear % 100) != 0) || ((iYear % 400) == 0))) iDays = 29;
	}

	//Validate the day
	if ( iDay < 1 || iDay > iDays )
	{
		day.focus ( );
		day.select ( );
		alert ( descr + ' is not a valid date.\nDay is not valid for month ' + eval ( month ).value + ' in year ' + eval ( year ).value + '.' );
		return false;
	}

	//Return true
	return true;
}

function YearFix ( yrField )
{
	ls_value = yrField.value
	if ( isNaN ( ls_value ) ) return;
	iYear = parseInt ( ls_value, 10 );
	if ( iYear < 100 )
	{
		if ( iYear > 89 ) iYear += 1900;
		if ( iYear < 90 ) iYear += 2000;
	}
	yrField.value = iYear;
}



/********************/
/* IsTime ( time )  */
/* Validates times. */
/********************/
function IsTime ( time )
{
	//Trim the value
	eval ( time ).value = Trim ( eval ( time ).value );

	//If the field is empty, then return good
	if (	eval ( time ).value == '' ) return true;

	//Get the value
	ls_time = eval ( time ).value
	
	//Be sure the value is atleast 4 characters
	if ( ls_time.length < 4 )
	{
		time.focus ( );
		time.select ( );
		alert ( ls_time + ' is not a valid time.' );
		return false;
	}

	//Pad the value with a zero if it's 4 chars long
	if ( ls_time.length == 4 )
	{
		ls_time = '0' + ls_time;
	}

	//Be sure the third character is a colon
	if ( ls_time.substr ( 2, 1 ) != ':' )
	{
		time.focus ( );
		time.select ( );
		alert ( ls_time + ' is not a valid time.' );
		return false;
	}
	
	//Be sure first two are a number
	if ( isNaN ( ls_time.substr ( 0, 2 ) ) )
	{
		time.focus ( );
		time.select ( );
		alert ( ls_time + ' is not a valid time.' );
		return false;
	}

	//Be sure last two are a number
	if ( isNaN ( ls_time.substr ( 3, 2 ) ) )
	{
		time.focus ( );
		time.select ( );
		alert ( ls_time + ' is not a valid time.' );
		return false;
	}

	//Extract hours and minutes as numbers
	hour = parseInt ( ls_time.substr ( 0, 2 ), 10 );
	min  = parseInt ( ls_time.substr ( 3, 2 ), 10 );

	//Validate the hour
	if ( hour < 0 || hour > 23 )
	{
		time.focus ( );
		time.select ( );
		alert ( ls_time.substr ( 0, 2 ) + ' is not a valid hour.' );
		return false;
	}

	//Validate the minute
	if ( min < 0 || min > 59 )
	{
		time.focus ( );
		time.select ( );
		alert ( ls_time.substr ( 3, 2 ) + ' is not a valid minute.' );
		return false;
	}

	//Return true
	return true;
}



/********************************************/
/* FormatPhone ( field )                    */
/* Formats a phone number as (###) ###-#### */
/********************************************/
function FormatPhone ( field )
{
	//Get the entered value
	temp = field.value;

	//Strip out anything other than the numbers
	temp = Replace ( temp, '-', '' );
	temp = Replace ( temp, ' ', '' );
	temp = Replace ( temp, '(', '' );
	temp = Replace ( temp, ')', '' );
	temp = Replace ( temp, ',', '' );

	//If not ten digits, then return
	if ( temp.length != 10 ) return;

	//Format the data
	temp = '(' + temp.substr ( 0, 3 ) + ') ' + temp.substr ( 3, 3 ) + '-' + temp.substr ( 6 );

	//Place the formatted data into the field
	field.value = temp;
}


function SelectedValue ( objSelect )
{
	lsReturn = '0';
	if ( objSelect.options.length > 0 ) lsReturn = objSelect.options[objSelect.selectedIndex].value;
	return lsReturn;
}
