/*

Purpose:
	Given a form reference, a field name, and a "label", return if the field has a value or not

Parameters:
	frmToCheck:		reference to the form to use
	sFieldName:		name/id of the text field to use
	sFieldLabel:	the text caption of the field, used for the error message

Return values:
	true/false:		true if the field had a value, false if error or if the field had no value
*/
//-------------------------------------------------------------------------
function validateRequiredTextField(frmToCheck, sFieldName, sFieldLabel)
{
	// check the reference to the form, return if not valid
	if(!frmToCheck)
		return false;
	
	// get a reference to the field, return if the field is not valid
	var txtFieldToCheck = frmToCheck[sFieldName];
	if(!txtFieldToCheck)
		return false;

	
	// retrieve the value from the field, return if not valid
	var sFieldValue = txtFieldToCheck.value;
	
	// if the field didn't have a value (0 length string)
	if(sFieldValue.length == 0)
	{
		// send an error message to the user
		alert("You must enter a value for " + sFieldLabel + ".");
		
		// set the focus to the field
		if (txtFieldToCheck.type != "hidden")
			txtFieldToCheck.focus();
		return false;
	}

	return true;
}

/*
Purpose:
	Given a form reference, a field name, and a confirmation field name: validates the password values. does not do length checking.

Parameters:
	frmToCheck:			reference to the form to use
	sFieldName:			name/id of the password field to use
	sFieldNameConfirm:	name/id of the confirmation password field

Return values:
	true/false:		true if the validation passed, false if error or validation failed
*/
function validatePassword(frmToCheck, sFieldName, sFieldNameConfirm)
{
	// check the reference to the form, return if not valid
	if(!frmToCheck)
		return false;

	// get a reference to the field, return if the field is not valid
	var txtFieldToCheck = frmToCheck[sFieldName];
	if(!txtFieldToCheck)
		return false;

	// get a reference to the field, return if the field is not valid
	var txtFieldConfirmToCheck = frmToCheck[sFieldNameConfirm];
	if(!txtFieldConfirmToCheck)
		return false;
	
	// if the password value doesn't match the confirm password value, alert an error
	if(txtFieldToCheck.value != txtFieldConfirmToCheck.value)
	{
		// report error to user
		alert("Password must match password confirmation value.");
		
		// reset field values
		txtFieldToCheck.value = "";
		txtFieldConfirmToCheck.value = "";
		
		// set input focus to password field
		txtFieldToCheck.focus();
		return false;
	}
	
	return true;
}

/*
Purpose:
	Given a form reference, a field name, and a "label", return if the field has a value or not

Parameters:
	frmToCheck:			reference to the form to use
	sFieldName:			name/id of the password field to use
	sFieldNameConfirm:	name/id of the confirmation password field

Return values:
	true/false:		true if the validation passed, false if error or validation failed
*/
//-----------------------------------------------------------------------------
function validateEmail(frmToCheck, sFieldName)
{
	// check the reference to the form, return if not valid
	if(!frmToCheck)
		return false;

	// get a reference to the field, return if the field is not valid
	var txtEmail = frmToCheck[sFieldName];
	if(!txtEmail)
		return false;
	
	// get the actual e-mail address value
	var sEmail = txtEmail.value;
	if(sEmail.length == 0)
	{
		alert("You must enter a value for the e-mail address.");
		txtEmail.focus();
		return false;
	}
	
	/*
	Form a regular expression.  
	The reg. exp. makes sure that you have characters/slashes, 
		then a "@", then characters/slashes, then a ".", then any characters
	*/

	var objRegExp = /(\w|\-)*@(\w|\-)*\.\w\w.*/;
	
	if(!objRegExp.test(sEmail))
	{
		alert("Email Address must have valid format.  Ex: webmaster@ecea-online.com");
		txtEmail.focus();
		return false;
	}

	return true;
}

/*
Purpose:
	Given a form reference, a field name, and a "label", return if the field has a value or not

Parameters:
	frmToCheck:			reference to the form to use
	sFieldName:			name/id of the email field to use
	sFieldNameConfirm:	name/id of the confirmation email field

Return values:
	true/false:		true if the validation passed, false if error or validation failed
*/
//-----------------------------------------------------------------------------
function validateEmailConfirm(frmToCheck, sFieldName, sFieldNameConfirm)
{
	// check the reference to the form, return if not valid
	if(!frmToCheck)
		return false;

	// get a reference to the field, return if the field is not valid
	var txtFieldToCheck = frmToCheck[sFieldName];
	if(!txtFieldToCheck)
		return false;
	
	// get the actual e-mail address value
	var sEmail = txtFieldToCheck.value;
	if(sEmail.length == 0)
	{
		alert("You must enter a value for the email address.");
		txtFieldToCheck.focus();
		return false;
	}
	
	/*
	Form a regular expression.  
	The reg. exp. makes sure that you have characters/slashes, 
		then a "@", then characters/slashes, then a ".", then any characters
	*/

	var objRegExp = /(\w|\-)*@(\w|\-)*\.\w\w.*/;
	
	if(!objRegExp.test(sEmail))
	{
		alert("Email address must have valid format, ie: johnsmith@abcd.com");
		txtFieldToCheck.focus();
		return false;
	}
	
	// get a reference to the field, return if the field is not valid
	var txtFieldConfirmToCheck = frmToCheck[sFieldNameConfirm];
	if(!txtFieldConfirmToCheck)
		return false;
		
	// if the email value doesn't match the confirm email value, alert an error
	if(txtFieldToCheck.value != txtFieldConfirmToCheck.value)
	{
		// report error to user
		alert("Email address must match the email confirmation value.");
		
		// set input focus to email field
		txtFieldToCheck.focus();
		return false;
	}
	return true;
}

/*
Purpose:
	Given a form reference, a field name, and a "label", return if the field has a value or not

Parameters:
	frmToCheck:			reference to the form to use
	sFieldName:			name/id of the password field to use
	sFieldLabel:		caption for the field (for error messages)
	nMinLength:			the minimum length of the text field
	nMaxLength:			the maximum length of the text field

Return values:
	true/false:		true if the validation passed, false if error or validation failed
*/
//-----------------------------------------------------------------------------
function validateMinMaxTextFieldLen(frmToCheck, sFieldName, sFieldLabel, nMinLength, nMaxLength)
{
	// check the reference to the form, return if not valid
	if(!frmToCheck)
		return false;

	// get a reference to the field, return if the field is not valid
	var txtFieldToCheck = frmToCheck[sFieldName];
	if(!txtFieldToCheck)
		return false;
	
	// get the actual e-mail address value
	var sFieldValue = txtFieldToCheck.value;
	
	// if the minimum field length should be checked
	if(nMinLength > -1)
	{
		// if the field value's length is < min value, report an error
		if(sFieldValue.length < nMinLength)
		{
			alert("The " + sFieldLabel + " should be at least " + nMinLength + " characters long.");
			txtFieldToCheck.focus();
			return false;
		}
	}
	
	// if the maximum field length should be checked
	if(nMaxLength > -1)
	{
		// if the field value's length is < min value, report an error
		if(sFieldValue.length > nMaxLength)
		{
			alert("The " + sFieldLabel + " should not be more than " + nMaxLength + " characters long.");
			txtFieldToCheck.focus();
			return false;
		}
	}
	
	return true;
}

/*
Purpose:
	Alert the user that we will be trimming the length of their input to x characters if they entered too much data
	If they entered too much data, we also confirm with them that it's ok to trim it.

Parameters:
	frmToCheck:			reference to the form to use
	sFieldName:			name/id of the password field to use
	sFieldLabel:				caption for the field (for error messages)
	nMaxLength:			the maximum length of the text field

Return values:
	true/false:			true if the validation passed, false if error or validation failed
*/
//-----------------------------------------------------------------------------
function trimTextBoxForSubmit(frmToCheck, sFieldName, sFieldLabel, nMaxLength)
{
	// check the reference to the form, return if not valid
	if(!frmToCheck)
		return false;

	// get a reference to the field, return if the field is not valid
	var txtFieldToCheck = frmToCheck[sFieldName];
	if(!txtFieldToCheck)
		return false;
	
	// get the actual e-mail address value
	var sFieldValue = txtFieldToCheck.value;
	
	if(sFieldValue.length > nMaxLength)
	{
		if(!window.confirm("The value you entered for " + sFieldLabel + " is greater than " + nMaxLength + " characters long.\nIt will be trimmed to " + nMaxLength + " characters if you continue.  Do you wish to continue?"))
			return false;
		
		sFieldValue = sFieldValue.substr(0, nMaxLength);
		
		txtFieldToCheck.value = sFieldValue;
	}
	return true;
}

/*
Purpose:
	Given a form reference, a field name, and a "label", return if the select field has a value or not

Parameters:
	frmToCheck:			reference to the form to use
	sFieldName:			name/id of the <select> field to check
	sFieldLabel:				caption for the field (for error messages)

Return values:
	true/false:		true if the validation passed, false if error or validation failed
*/
//-----------------------------------------------------------------------------
function validateRequiredSelectField(frmToCheck, sFieldName, sFieldLabel)
{
	// check the reference to the form, return if not valid
	if(!frmToCheck)
		return false;

	// get a reference to the select field, return if the field is not valid
	var selField = frmToCheck[sFieldName];
	if(!selField)
		return false;
		
	// get the actual selected value
	var sFieldValue = selField.options[selField.selectedIndex].value;
	
	// which should not be null
	if (sFieldValue == "" || sFieldValue == "NULL")
	{
		// send an error message to the user
		alert("You must enter a value for " + sFieldLabel + ".");
		
		// set the focus to the field
		selField.focus();
		return false;
	}

	return true;
}
/*

Purpose:
	Given a form reference, a field name, and a "label", return if the field has an integer value or not

Parameters:
	frmToCheck:		reference to the form to use
	sFieldName:		name/id of the text field to use
	sFieldLabel:	the text caption of the field, used for the error message

Return values:
	true/false:		true if the field had a value, false if error or if the field had no value
*/
//-------------------------------------------------------------------------
function validateIntegerTextField(frmToCheck, sFieldName, sFieldLabel)
{
	// check the reference to the form, return if not valid
	if(!frmToCheck)
		return false;
	
	// get a reference to the field, return if the field is not valid
	var txtFieldToCheck = frmToCheck[sFieldName];
	if(!txtFieldToCheck)
		return false;
	
	// retrieve the value from the field, return if not valid integer
	var sFieldValue = txtFieldToCheck.value;
	
	// Search through string's characters one by one
    // until we find a non-numeric character.
    for (i = 0; i < sFieldValue.length; i++)
    {   
        // Check that current character is number.
        var c = sFieldValue.charAt(i);

        if (! (c >= "0" && c <= "9") )
        {
			// send an error message to the user
			alert("You must enter an integer value for " + sFieldLabel + ".");
		
			// set the focus to the field
			if (txtFieldToCheck.type != "hidden")
				txtFieldToCheck.focus();
		
			return false;
		}
    }
	
	return true;
}

//-----------------------------------------------------------------------------
function validateRequiredPhoneField(frmToCheck, sFieldName, sFieldLabel)
{
	// check the reference to the form, return if not valid
	if(!frmToCheck)
		return false;
	
	// get a reference to the field, return if the field is not valid
	var txtFieldToCheck = frmToCheck[sFieldName];
	if(!txtFieldToCheck)
		return false;
	
	// retrieve the value from the field, return if not valid integer
	var sFieldValue = txtFieldToCheck.value;
	
	var sDigitNumber = stripNonDigits(sFieldValue);	
	
	var sFormattedPhone = "";
	switch(sDigitNumber.length)
	{
	case 0:
		break;
	case 10:
		// it's a US phone number w/ no "1" @ the beginning
		sFormattedPhone = "(" + sDigitNumber.substr(0, 3) + ") " + sDigitNumber.substr(3, 3) + "-" + sDigitNumber.substr(6, 4);
		break;
	case 11:
		// check if the first char is a 1
		var nFirstDigit = parseInt(sDigitNumber.substr(0, 1));
		if(nFirstDigit == 1)
		{
			// it's a US phone number w/ "1" @ the beginning
			sFormattedPhone = "(" + sDigitNumber.substr(1, 3) + ") " + sDigitNumber.substr(4, 3) + "-" + sDigitNumber.substr(7, 4);
		}
	}	
		
	// if the field didn't have a value (0 length string)
	if(sFormattedPhone.length == 0)
	{
		// send an error message to the user
		alert("You must enter a telehone number (xxx-xxx-xxxx) for " + sFieldLabel + ".");
		
		// set the focus to the field
		if (txtFieldToCheck.type != "hidden")
			txtFieldToCheck.focus();
		return false;
	}
	else if(sFormattedPhone != sFieldValue)
		txtFieldToCheck.value = sFormattedPhone;
	
	return true;
}

//-----------------------------------------------------------------------------
function stripNonDigits(sValueToStrip)
{
	var sStripped = "";
	for(var niCurrentChar = 0; niCurrentChar < sValueToStrip.length; niCurrentChar++)
	{
		var sCurrentChar = sValueToStrip.charAt(niCurrentChar);
		if(!isNaN(sCurrentChar) && (sCurrentChar != " "))
		{
			var nCurrentNumber = parseInt(sCurrentChar, 10);
			sStripped += nCurrentNumber;
		}
	}
	return sStripped;
}
    
//-----------------------------------------------------------------------------
function is_nav4()
{
	// convert all characters to lowercase to simplify testing
	var agt=navigator.userAgent.toLowerCase();

	// *** BROWSER VERSION ***
	// Note: On IE5, these return 4, so use is_ie5up to detect IE5.
	//var is_major = parseInt(navigator.appVersion);
	//var is_minor = parseFloat(navigator.appVersion);

	// Note: Opera and WebTV spoof Navigator.  We do strict client detection.
	// If you want to allow spoofing, take out the tests for opera and webtv.
	var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));

	//return (is_nav && (is_major == 4));
	
	return is_nav;
}


//-----------------------------------------------------------------------------
/*	Copyright 2000 SiteExperts.com/ InsideDHTML.com, LLC
	This code can be reusedas long as the above copyright notice is not removed
	See http://www.siteexperts.com/tips/elements/ts34/page1.asp for details.
	By Scott Isaacs
*/
//-----------------------------------------------------------------------------
function checkTab(el) {
	// Run only in IE and if tab key is pressed
	if ((document.all) && (9==event.keyCode)) {
		// Cache the selection
		el.selection=document.selection.createRange(); 
		el.selection.text=String.fromCharCode(9)
		
		// Cancel the event
		event.returnValue = false;
	}
}