function validateMoney(moneyValue) {
	var dotLoc;
	
	//parse the value out - assume the last dot is the decimal point
	dotLoc = moneyValue.lastIndexOf(".");
	
	//remove everything to the right of the last dot
	if (dotLoc != -1) {
		moneyValue = moneyValue.substr(0, dotLoc);
	}
	
	//remove all '$', ',', and '.'
	
	moneyValue = moneyValue.replace(/[^0-9]/gi, "");	
	return moneyValue;
	
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function formatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
        if (isNaN(parseInt(num, 10))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum));
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num !== 0)  {
		if (num > 0) {
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		}
		else {
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		}
	}
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}

function formatCurrency(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.										
 
	RETVAL:
		The formatted number!		
 **********************************************************************/
{
	var tmpStr = new String(formatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas));

	if (tmpStr.indexOf("(") != -1 || tmpStr.indexOf("-") != -1) {
		// We know we have a negative number, so place '$' inside of '(' / after '-'
		if (tmpStr.charAt(0) == "(")
			tmpStr = "($"  + tmpStr.substring(1,tmpStr.length);
		else if (tmpStr.charAt(0) == "-")
			tmpStr = "-$" + tmpStr.substring(1,tmpStr.length);
			
		return tmpStr;
	}
	else
		return "$" + tmpStr;		// Return formatted string!
}

//Function to return ending 0's in place of ending "K", "M" or "B", "T"
function Kto000(moneystring)
{

	var lastChar = (moneystring.substr(moneystring.length - 1,1)).toLowerCase();
	var patternWithKM = /^\$?\d{1,}[km]$/;
		
	if (moneystring.match(patternWithKM) )
	{
		var allButLast = moneystring.substr(0, moneystring.length-1);
		 if ( lastChar== "k" )	 
		 {
				moneystring=allButLast + "000";
		 }
		 else if (lastChar=="m")
		 {
				moneystring=allButLast + "000000";
		 }
	}
	 return moneystring;
}

//Function to scrub the cash input and check it against a minimum and/or maximum value if desired
function cleanTheCash(moneyControlName, divName, inputRequired, minAmount, maxAmount, focusOnRejection)
{	
/*
	moneyControl: the control containing user input to be checked
	divName: the div whose innerHTML should be set to display the formatted amount
	inputRequired: true=value cannot be empty string; false=do no testing if moneyControl.value is empty string
	minAmount: if not empty string, the minimum acceptable amount
	maxAmount: if not empty string, the maximum acceptable amount
	focusOnRejection: true=put focus back on the moneycontrol if it doesn't pass the test
*/
	var patternCouldBeOkay = /^(\$?\d{1,3})(,?\d{3})*[k|m]?$/ ;//1 digit required, all else optional but must be well formed for number 
	var digitsOnly = /\d+/;   //optional string of digits (only), for integer minimum and maximum amounts
	var divFormattedValueDisplay;

	//test parameters
	var moneyControl = getObjectAnyBrowser(moneyControlName);
	if (moneyControl===null)
	{
		alert("Unable to find input field.");
		return false;
	}

	if (divName!=="")   //must exist if a name passed
	{
		divFormattedValueDisplay=getObjectAnyBrowser(divName);	
		if (divFormattedValueDisplay===null)
		{	
			alert("Cannot locate currency display are '" + divName + "'");
			return false;
		}
	}
	
	var moneystring = moneyControl.value.toLowerCase();

	//If it's required, is there something to test
	if (inputRequired === false) 
	{
		if (moneystring==="")	
		{
			return true;  //nothing to process		
		}
	}

	var tempAmount;
	if (minAmount!=="")  //must match if passed
	{
		tempAmount = minAmount.toString(10);
		if(tempAmount.match(digitsOnly)==null)
		{		
			alert("Minimum Amount '" + tempAmount + "' is not 'digits only'.");
			return false;
		}
	}
	if (maxAmount!=="")  //must match if passed
	{
		tempAmount = maxAmount.tostring(10);
		if(tempAmount.match(digitsOnly)!==true)
		{
			alert("Maximum Amount '" + tempAmount + "' is not 'digits only'.");
			return false;
		}
	}
	//set up pre-check display
	moneyControl.style.backgroundColor="#FFFF99";		//yellow background during check
	if (divName!=="")   
	{
		divFormattedValueDisplay.innerHTML= "&nbsp;";					//clear display area
		divFormattedValueDisplay.style.backgroundColor="transparent";				//show form background
	}

	//convert K to 000
	moneystring = Kto000(moneystring);
  
	if (!moneystring.match(patternCouldBeOkay))
	{
		alert("Please enter a single value, No ranges.\nUse only numbers and commas.\nMay end with K or M.");
		if (focusOnRejection===true) { movetocontrol(moneyControlName); }
		return false;
	}
	//remove all non-digit stuff
	moneystring=validateMoney(moneystring);

	moneyControl.value = moneystring;
	if (formatCurrency(moneyControl.value,0,false,false,true)	!= "$NaN")
	{
			if (minAmount!=="")
			{
				if (moneyControl.value < minAmount)
				{
					alert ("Value must be $" + minAmount + " or greater.");
					if (focusOnRejection===true) { movetocontrol(moneyControlName); }
					return false;
				}	
			}
			if (maxAmount!=="")
			{
				if (number(moneyControl.value) > number(maxAmount))
				{
					alert ("Value must be $" + maxAmount + " or less.");
					if (focusOnRejection===true) {movetocontrol(moneyControlName);}
					return false;
				}	
			}
			moneyControl.style.backgroundColor="#FFF";  //got good number, white background
			if (divName!=="")   
			{
				divFormattedValueDisplay.innerHTML= formatCurrency(moneyControl.value,0,false,false,true)	;
				divFormattedValueDisplay.style.backgroundColor="#0000FF";				
			}
			return true;
	}	
	else
	{
		alert("Please enter a number for the value.");
		if (focusOnRejection===true) {movetocontrol(moneyControlName);}
		return false;
	}
}

function movetocontrol(controlName)  //works even in firefox
{
		setTimeout("document.getElementById('" + controlName + "').focus();",1);
		setTimeout("document.getElementById('" + controlName+ "').select();",1);
		return true;
}