// validation.js
//
// SUMMARY
//
// whitspace Global variable contains all white space
// Functions:
// trim(strParam)				Right and Left trim the string
// ltrim(strParam)				Left trim the string	
// rtrim(strParam)				Right trim the string
// isEmpty(s)					Check whether string s is empty.
// isDate(intDay, intMonth, intYear)    		Check is Valid Date
// isInteger(intParam, intDigits)		Check whether the string intParam is an
//												integer and the number of digits is intDigits
// isDecimal(intFloat, intDigits, intDecimalPlaces)		Check whether the string 
//																		intFloat is a float, the
//																		number of digits is intDigits
//																		and the number of decimal
//																		places is intDecimalPlaces

	var whitespace = " \t\n\r";
//-----------------------------------------------------------------------------	
	/*
		Eliminate all left and right spaces
		Parameters Input: strParam string want to trim
		Return string after trim
	*/
	function trim(strParam)	{
		strParam = ltrim(strParam);
		strParam = rtrim(strParam);
		return strParam;
	}
	
//-----------------------------------------------------------------------------	
	/*
		Eliminate all left spaces
		Parameters Input: strParam string want to trim
		Return string after left trim
	*/
	function ltrim(strParam)
	{
		for(i=0;i<strParam.length;i++)
		{
			var c = strParam.charAt(i);
			if (whitespace.indexOf(c) == -1)
				break;
		}
		strParam=strParam.substring(i,strParam.length);
		return strParam;
	}
	
//-----------------------------------------------------------------------------
	/*
		Eliminate all right spaces
		Parameters Input: strParam string want to trim
		Return string after right trim
	*/
	function rtrim(strParam) {
		for (i = strParam.length - 1; i >= 0; i--) {
			var c = strParam.charAt(i);
			if (whitespace.indexOf(c) == -1)
				break;
		}
		if (i != strParam.length - 1)
			strParam = strParam.substring(0, i+1);
		return strParam;
	}

//-----------------------------------------------------------------------------	
	// Check whether string s is empty.
	function isEmpty(s) {   
		return ((s == null) || (s.length == 0));
	}
//-----------------------------------------------------------------------------	
 	/*
		validate Date
		Parameters Input: intDay day
						  intMonth Month as integer 0,1,2
						  intYear Year 
		return true if valid date
			   false if not valid
	*/
	function isDate(intDay, intMonth, intYear) {
		var varDate = new Date(intYear, intMonth, intDay);
		if (varDate.getDate() != intDay)
			return false;
		return true;
	}
//-----------------------------------------------------------------------------
	/*
		Check that the number is an integer and that the number of digits
		is within the allowed length.
		Parameters Input:
			intParam: the number (as a string).
			intDigits: maximum number of digits allowed for the integer.
		Returns:
			true if the number is an integer and within the allowed length;
			otherwise, false if not accepted.
	*/
	function isInteger(intParam, intDigits) {
		var intParamLength = intParam.length;
		// if the number of digits are greater than the allowed number
		if (intParamLength > intDigits)
			return false;
		// otherwise, the number of digits is within the allowed length
		else {
			if (intParam.indexOf(".") != -1)
				return false;
			else if (!isNaN(intParam))
				return true;
			else
				return false;
		}
	}
//-----------------------------------------------------------------------------
	/*
		Check that the number is a float, the number of digits is within
		the allowed length, and the number of decimal places are within
		the allowed length.
		Parameters Input:
			floatParam: the number (as a string).
			intDigits: maximum number of digits allowed for the float (before
						 the decimal point).
			intDecimalPlace: maximum number of digits after the decimal point.
		Returns:
			true if the number is a float, number of digits before the decimal
			point is within the allowed length, and the number of digits after
			the decimal point is within the allowed length;
			otherwise, false if not accepted.
	*/
	function isDecimal(floatParam, intDigits, intDecimalPlaces) {
		var floatParamLength = floatParam.length;
		var firstPart, secondPart;
		var decimalPos = floatParam.indexOf(".");

		// the number contains a decimal point
		if (decimalPos != -1) {
			// get the first part before the decimal point
			firstPart = floatParam.substring(0, decimalPos);
			var firstPartLength = firstPart.length;
			// if the number of digits in the first part (before the decimal point)
			// is greater than the allowed number OR no digits in the first part
			if ((firstPartLength > intDigits) || (firstPartLength == 0))
				return false;

			// get the second part before the decimal point
			secondPart = floatParam.substring(decimalPos + 1, floatParamLength);
			var secondPartLength = secondPart.length;
			// if the number of digits in the second part (after the decimal point)
			// is greater than the allowed number of decimal places
			if ((secondPartLength > intDecimalPlaces) || (secondPartLength == 0))
				return false;

			// if both the first and the second parts are integers
			if ( (!isNaN(firstPart)) && (!isNaN(secondPart)) )
				return true;
			// otherwise, the number is not valid
			else 
				return false;
		}
		// otherwise, the number does not contain a decimal point
		else {
			// if the number is an integer
			if (!isNaN(floatParam)) {
				// if the number of digits in the number is greater than the
				// allowed number
				if (floatParamLength > intDigits)
					return false;
				// otherwise, the number is valid
				else
					return true;
			}
			// otherwise, the number is not valid
			else
				return false;
		}
	}

	function addZeroForDecimalField(fieldObj, intDecimalPlaces) {
		var decimalField = eval(fieldObj);
		var decimalFieldValue = decimalField.value;
		var decimalFieldLength = decimalFieldValue.length;

		var decimalPos = decimalFieldValue.indexOf(".");
		if (decimalPos == 0)
			decimalField.value = "0" + decimalFieldValue;
		else if (decimalPos == decimalFieldLength - 1)
			if (intDecimalPlaces > 0)
				decimalField.value = decimalFieldValue + "0";
			else if (intDecimalPlaces == 0) {
				decimalField.value = decimalFieldValue.substring(0, decimalPos);
			}
	