/*
 * Constructon defines available methods.  This object is used to validate
 * entries in textfields and text areas.  Validation includes checking for
 * content for mandatory fields and numeric ranges for ranged textfields.
 */
function ValidateTextField()
{
	// This constructor has no attributes

	// Function prototype defintions
	this.inRange = ValidateTextField_inRange;
	this.isNumeric = ValidateTextField_isNumeric;
	this.isBlank = ValidateTextField_isBlank;
	this.isEmpty = ValidateTextField_isEmpty;
	this.rangeErrType = ValidateTextField_rangeErrType;
}

/*
 * Check to see if current textfield/textarea have range qualifiers.
 * If there is a range qualifier, it checks the field element value to see
 * if it is in range.
 */
function ValidateTextField_inRange(txtFieldElem)
{
	var blnRange = false;	// Assume data is not within specified range
	var v = parseFloat(txtFieldElem.value);

	if (isNaN(v) ||
		((txtFieldElem.minVal != null) && (v < txtFieldElem.minVal)) ||
		((txtFieldElem.maxVal != null) && (v > txtFieldElem.maxVal)))
		blnRange = false;
	else
		blnRange = true;

	return (blnRange);
}

/*
 * Utility function that returns true if a string contians only
 * whitespace characters or leading whitespace characters.
 */
function ValidateTextField_isBlank(strTextValue)
{
	var blnWhiteSpace = false;  // Assume there are no whitespace
	var blnChar = false;   // Assume there are no actual characters

	// Check the string a character at a time
	for (var i = 0; i < strTextValue.length; i++)
	{
		var c = strTextValue.charAt(i);

		// White space are space, newline/return and tab
		if (( c == ' ') || ( c == '\n') || (c == '\t'))
		{
			blnWhiteSpace = true;
			break;
		}
		else
		{
			blnChar = true;
			break;
		}
	}

	// If we have a leading character in the data string then we don't have any
	// invalid white space.
	if (blnChar)
		blnWhiteSpace = false;

	return (blnWhiteSpace);
}

/*
 * Checks the passed in textfield/text area if it is empty by seeing if it is
 * null, have empty string or just using white-space characters.  Returns true
 * any condition is met otherwise, returns false.
 */
function ValidateTextField_isEmpty(textFieldElem)
{
	var blnIsEmpty = false;	// Assume textfield/area is not empty

	if ((textFieldElem.value == null) || (textFieldElem.value == "") 
		|| this.isBlank(textFieldElem.value))
		blnIsEmpty = true;
	else
		blnIsEmpty = false;
	return (blnIsEmpty);
}

/*
 * Checks the passed in widget area if it is numeric by seeing if it has the
 * numeric attribute or a min or max value limit.
 */
function ValidateTextField_isNumeric(txtFieldElem)
{
	var blnIsNumeric = false;  /* Assume textfield/area does not need to be
								* numeric.
								*/

	/*
	 * Check to see if current widget is supposed to be numeric
	 * base on the conditional attributes.
	 */
	if ((txtFieldElem.numeric != undefined)  && ((txtFieldElem.minVal != null) || 
		(txtFieldElem.maxVal != null)))
		blnIsNumeric = true;
	else
		blnIsNumeric = false;

	return (blnIsNumeric);
}

/*
 * This function assume the curremt textfield/textarea has range qualifiers
 * add will determine what type they are by the following:
 * 0 - no range qualifiers
 * 1 - min. qualifier only
 * 2 - max. qualifier only
 * 3 - both min and max qualifiers
 */
function ValidateTextField_rangeErrType(txtFieldElem)
{
	var intErrType = 0;	// No error just yet

	if (txtFieldElem.minVal != null)
		intErrType = 1;

	if ((txtFieldElem.minVal != null) && (txtFieldElem.maxVal != null))
		intErrType = 3;
	else if (txtFieldElem.maxVal != null)
		intErrType = 2;

	return (intErrType);
}