/******************************************************************************
*
*  usage: formValidate(<object name>, <validation type>
*					[, <object name>, <validation type> ...])
*
*  validation types: First char is "R" if field is required - "N" otherwise.
*    "isEmail" or "isNum" may be appended if field is email or numeric. If the
*    field is a combo, and the first item is non-selectable, use "Rselect". If
*    the field is required only when a specific drop-down item is selected, use
*    "Cname=value" where name is the control name, and value is the property in
*    the value attribute of the option.
*
*  return: does not return a value, but sets document.fv_returnValue.
*
*******************************************************************************/

var gstrERROR_REQUIRED			= ' is a required field';
var gstrERROR_EMAIL					= ' must contain an email address';
var gstrERROR_NUMERIC				= ' must be numeric';
var gstrERROR_SELECT				= ' must be selected';

var gstrERROR_INTRO					= 'The form you submitted has the following error(s):\n\n';

var bCheckingLength					= false;

function errCat()
{
	var lngIndex,
			astrArgs	= errCat.arguments,
			strReturn	= astrArgs[0];

	for (lngIndex = 1; lngIndex < astrArgs.length; lngIndex++)
		strReturn	+= ' and' + astrArgs[lngIndex];

	return strReturn;
}

function findObj(strName)
{
	var lngIndex, objFound;

	if(!(objFound = document[strName]) && document.all)
		objFound	= document.all[strName];

	for (lngIndex = 0; !objFound && (lngIndex < document.forms.length); lngIndex++)
		objFound	= document.forms[lngIndex][strName];

	if (!objFound && document.getElementById)
		objFound	= document.getElementById(strName);

	return objFound;
}

function formValidate()
{
	var lngIndex, strName, strType, strError, lngPos, strCondValue,
			strErrors	= '',
			astrArgs	= formValidate.arguments;

	for (lngIndex = 0; lngIndex < (astrArgs.length - 1); lngIndex += 3)
	{
		strName					= astrArgs[lngIndex];
		objFormElement	= findObj(strName);

		if (objFormElement)
		{
			strType		= astrArgs[lngIndex + 1];
			strError	= astrArgs[lngIndex + 2];
			strValue	= trim(objFormElement.value);

			if (strValue != "")
			{
				if (strType.indexOf('select') != -1)
				{
					if (1 * objFormElement.value == 0)
						strErrors	+= '- ' + strError + '\n';
				}
				else if (strType.substr(1) == 'isEmail')
				{
					lngPos	= strValue.indexOf('@');
					if ((lngPos < 1) || (lngPos == (strValue.length - 1)))
						strErrors	+= '- ' + strError + '\n';
				}
				else if (strType.substr(1) == 'isNum')
				{
					if (isNaN(strValue))
						strErrors	+= '- ' + strError + '\n';
				}
			}
			else if (strType.charAt(0) == 'R')
				strErrors	+= '- ' + strError + '\n';
			else if (strType.charAt(0) == 'C')
			{
				strName					= strType.substr(1, strType.indexOf('=') - 1);
				strCondValue		= strType.substr(strType.indexOf('=') + 1);
				objFormElement	= findObj(strName);

				if (objFormElement)
				{
					strValue	= trim(objFormElement.value);
					if (strValue == strCondValue)
						strErrors	+= '- ' + strError + '\n';
				}
			}
		}
	}

	if (strErrors)
		alert(gstrERROR_INTRO + strErrors);

	document.fv_returnValue	= (strErrors == '');
}

function simpleError(strName, strConstant)
{
	return strName + strConstant + '.\n';
}

// Removes leading and trailing spaces from the string.
function trim(InputString)
{
	var Trimmed	= InputString;

	// Check for spaces at the beginning of the string
	while (Trimmed.length > 0 && Trimmed.charCodeAt(0) <= 32)
		Trimmed	= Trimmed.substring(1);

	// Check for spaces at the end of the string
	while (Trimmed.length > 0 && Trimmed.charCodeAt(Trimmed.length - 1) <= 32)
		Trimmed	= Trimmed.substring(0, Trimmed.length - 1);

	return Trimmed;
}

/*
 * The following functions are not used by the formValidate function, but are
 * useful on their own.
 */

// Check the length of a textarea.
function checkMaxLength(textAreaName, textAreaLabel)
{
	if (!bCheckingLength)
	{
		bCheckingLength	= true;

		var textArea	= findObj(textAreaName);

		if (textArea.value.length > textArea.maxlength)
		{
			alert(textAreaLabel + ' has a maximum length of ' + textArea.maxlength +
				' characters. Your entry was ' + textArea.value.length +
				' characters long, so it will be truncated.');

			textArea.value	= textArea.value.substr(0, textArea.maxlength);

			// If the last character is a carriage return, its line feed was chopped
			// off. If the user clicks a key, the browser can add the line feed back
			// in automatically, causing the alert to be shown again (and again...).
			if (textArea.value.substr(textArea.maxlength - 1, 1) == '\r')
				textArea.value	= textArea.value.substr(0, textArea.maxlength - 1);
		}

		bCheckingLength	= false;
	}
}

// Enable or disable a text field based on the value of a drop-down box.
function updateTextField(dropDownName, dropDownValue, textName)
{
	var objDropDown	= findObj(dropDownName);
	var objText			= findObj(textName);

	if (objDropDown.value != dropDownValue)
		objText.value			= '';

	objText.disabled							= (objDropDown.value != dropDownValue);
	objText.style.backgroundColor	= (objText.disabled ? '#eeeeee' : '#ffffff');
}