//-------------------NUMBER RELATED FUNCTIONS------------------------
// (C)2001 IFAA Written by Denise Taylor
//-------------------------------------------------------------------
// 
// Functions in this file
// ----------------------
// isIntegerNumber(numval)
// isDecimalNumber(numval)
// FormatIntegerNumber(passedValue)
// FormatDecimalNumber(sValue,numberPlaces)
// FormatMoney(passedValue)
// FormatIntegerMoney(passedValue)
//
//-------------------------------------------------------------------
// validate integer number
// input: string
// output: T or F
//
function isIntegerNumber(numval) {
  var allowed = "0123456789"; // if input is not matched in this sequence, then false returned 
  for (Ni=0;Ni < numval.length;Ni++) { 
  	if (allowed.indexOf(numval.charAt(Ni)) == -1) {
  		 return false;  
	}
  }
  return true; 
}
//-------------------------------------------------------------------
// validate a decimal number 
// input: string
// output: T or F 
//
function isDecimalNumber(numval) {
	var allowed = new String("0123456789."); 	// if input is not matched in this sequence, then false returned 
	var ndec = 0;								// number of decimal points in value entered 
	for (var Ni=0;Ni < numval.length;Ni++) {
	 	if (allowed.indexOf(numval.charAt(Ni)) == -1) {
			return false;  			// not a number 
		}
		if (numval.charAt(Ni) == ".") {
			 ndec = ndec + 1;		 // count the number of decimal points
		}
	}
	// check that the number has only one decimal point 
	if (ndec > 1) {
		return false; 
	}	
	
	return true; 
}
//-------------------------------------------------------------------
// Generates a display string of whole dollars
// Input: number to be formatted
// Output: whole number
// Any numbers after the decimal point are truncated.
// 
function FormatIntegerNumber(passedValue) {
	var sValue = new String(passedValue);
	var iDecimalPosition = sValue.indexOf(".");
	
	// If null, return null
	if (sValue == "") {
		return "";
	}
	// Use numbers before any decimal points
	if (iDecimalPosition > -1) {
		sValue = new String(sValue.substring(0, iDecimalPosition));
	}
	return sValue;
}
//-------------------------------------------------------------------
// Format a number with a specified number of decimal places
// Input: number to be formatted, number of decimal places required 
// Output: formatted number with correct number of decimal places and any commas removed 
// Zeros are added if needed after the decimal point; 
// excess numbers after the decimal point are truncated.
// If the number has no decimal point, a decimal point is added; 
// a decimal point in first place has a leading zero added.
//
// If the value to be processed is null, just return null.
//
function FormatDecimalNumber(sValue,numberPlaces) {
	if (sValue == "") {
		return "";
	}
	// remove any commas
	var newValue = "";										// value returned	
	var sTemp1 = sValue;
	var sTemp2 = "";	
	var nCommaPosition = sTemp1.indexOf(",");				// position of first comma, if any 
	while (nCommaPosition > -1) {
		sTemp2 = sTemp1.substring(0, nCommaPosition) + sTemp1.substring(nCommaPosition + 1, sTemp1.length);
		sTemp1 = sTemp2;
		nCommaPosition = sTemp1.indexOf(",");
	}
	//
	// build new string from sTemp1
	var nPointPosition = sTemp1.indexOf(".");				// position of decimal point
	var nPlaces = new Number(numberPlaces);					// number of decimal places required 
	//
	// build a string of zeros of length nPlaces
	var sZeros = "";
	for (var j=0; j<nPlaces; j++) {
		sZeros = sZeros + "0";
	}		
	// Concatenate string of zeros to value
	// If no decimal point, add a decimal point; get new position of decimal point
	if (nPointPosition < 0) {
		sTemp2 = "" + sTemp1 + "." + sZeros;
		nPointPosition = sTemp2.indexOf(".");
	}
	else {
			sTemp2 = "" + sTemp1 + sZeros;	
	}
	// If decimal point in first place, add a leading zero; get new position of decimal point	
	if (nPointPosition == 0) {
		sTemp1 = "" + "0" + sTemp2 + sZeros;
		nPointPosition = sTemp1.indexOf(".");	
	}
	else {
		sTemp1 = sTemp2;
	}
	//
	// have at least the correct number of places, so just take what is needed  
	newValue = sTemp1.substring(0, nPointPosition + nPlaces + 1);
	return newValue;
}
//-------------------------------------------------------------------
// Generates a money display string
// Input: number to be formatted
// Output: formatted number with dollar sign, commas, 2 decimal places
// Zeros are added if needed after the decimal point; 
// excess numbers after the decimal point are truncated.
// 
function FormatMoney(passedValue) {
	var theValue = new String(passedValue);
	var mValue = "";
	var mlength = theValue.length;
	var mdecimal = theValue.indexOf(".");
	if (mdecimal > -1) {
		if (mlength > (mdecimal + 2)) {
			mValue = theValue.substring(0, mdecimal + 3);
		}
		else if (mlength > (mdecimal + 1)) {
			mValue = theValue.substring(0, mdecimal + 2) + "0";
		}
		else {
			mValue = theValue.substring(0, mdecimal) + ".00";
		}
	}
	else if (mlength > 0) {
		mValue = theValue + ".00";
	}
	else {
		mValue = "0.00";
	}
	mlength = mValue.length;
	for (var i=(mlength-6); i>0; i=i-3) {
		mPart1 = mValue.substring(0, i);
		mPart2 = mValue.substring(i, mlength);
		mValue = mPart1 + "," + mPart2;
		mlength++;
	}
	mValue = new String("$" + mValue);
	return mValue;
}
//-------------------------------------------------------------------
// Generates a money display string of whole dollars
// Input: number to be formatted
// Output: formatted number commas
// Any numbers after the decimal point are truncated.
// 
function FormatIntegerMoney(passedValue) {
	var mValue = new String(passedValue);
	var mlength = mValue.length;
	var mdecimal = mValue.indexOf(".");
	var mPart1;
	var mPart2;
	
	if (mdecimal > -1) {
		mValue = new String(mValue.substring(0, mdecimal));
	}
	mlength = mValue.length;
	for (var i=(mlength-3); i>0; i=i-3) {
		mPart1 = mValue.substring(0, i);
		mPart2 = mValue.substring(i, mlength);
		mValue = mPart1 + "," + mPart2;
		mlength++;
	}
	mValue = new String("$" + mValue);
	return mValue;
}
//---------------- End of numeric validation functions --------------

