/*
	validation routines
	Charles Feduke
	March 9, 2001
	
	performs form validation based on an array passed
*/

function sTrim(sString)
{
	// trims off trailing spaces on right side
	if (sString.length > 0)
	{
		for (var i = sString.length; i >= 0; i--)
		{
			if (sString.charAt(i) != " ")
			{
				return sString.substring(0, i);
			}
		}
	}
	return sString;
}

function bIsValid(objName)
{
	// checks to see if the data in a single object is not blank
	var sString = new String(sTrim(eval("document." + objName + ".value")));
	if (sString.length > 0)
	{
		// success!
		return true;
	}
	else
	{
		// failure
		return false;
	}
}

function bIsNumber(objName)
{
	// checks to see if a currency is a number
	var sString = eval("document." + objName + ".value");
	// replace
	sString = sString.replace(/\$/, '');
	sString = sString.replace(/,/, '');
	sString = sString.replace(/ /, '');
	if (isNaN(sString))
	{
		return false;
	}
	return true;
}

function bValidate(sArray)
{
	/* validates a list of items.  The array should appear in the following format:
		formName.objectName:actual name (i.e. "user.last:last name")
		will return false if it fails, set focus on the object and popup an alert box
	*/
	// loop thru each element in the array
	var sCurrent = new Array();
	for (var i = 0; i <= sArray.length - 1; i++)
	{
		sCurrent = sArray[i].split(":");
		if (bIsValid(sCurrent[0]) == false)
		{
			// failure
			alert("You must complete the " + sCurrent[1] + " field before submitting this form.");
			eval("document." + sCurrent[0] + ".focus()");
			return false;
		}
	}
	// success
	return true;
}

function bValidate2(sArray1, sArray2)
{
	/* validates a list of items, similiar to bValidate but processes sArray2 with bIsNumber */
	var sCurrent = new Array();
	
	// loop thru each element in the array looking for missing required fields
	for (var i = 0; i <= sArray1.length - 1; i++)
	{
		sCurrent = sArray1[i].split(":");
		if (bIsValid(sCurrent[0]) == false)
		{
			// failure
			alert("You must complete the " + sCurrent[1] + " field before submitting this form.");
			eval("document." + sCurrent[0] + ".focus()");
			return false;
		}
	}
	// loop looking for NaNs
	for (var i = 0; i <= sArray2.length - 1; i++)
	{
		sCurrent = sArray2[i].split(":");
		if (bIsNumber(sCurrent[0]) == false)
		{
			// failure
			alert("You must enter a valid number in the " + sCurrent[1] + " field before submitting this form.");
			eval("document." + sCurrent[0] + ".focus()");
			return false;
		}
	}
	// success
	return true;
}
