function CFNumberToAmount(pNumber, pGroupSeparator, pDecimalSeparator, pDecPlaces) {
	
	var lSourceAmount;
	var lResult;
	var lTestParam;
	var lREDec;
	var lDecPos;
	var lRENum;
	var lReplStr;
	var i;
	var n;
	var lDecPart
	var lDecNumber
	
	//Initialization
	lResult = '';
	lTestParam = true;
	
	if (pDecPlaces == null) {
		pDecPlaces = 2;
	}
			
	if (lTestParam) {
		if (typeof(pGroupSeparator) == 'undefined') {
			pGroupSeparator = ','
		} else {
			if (typeof(pGroupSeparator) != 'string') {
				lTestParam = false
			}
		}
	};

	if (lTestParam) {
		if (typeof(pDecimalSeparator) == 'undefined') {
			pDecimalSeparator = '.'
		} else {
			if (typeof(pDecimalSeparator) != 'string') {
				lTestParam = false
			}
		}
	};

	if (!lTestParam) {
		return("");
	}

	if (typeof(pNumber) != 'number') {
		pNumber = CFAmountToNumber(pNumber, pGroupSeparator, pDecimalSeparator)	
	}
		
	//Round decimal part
	pNumber = Math.round(pNumber * Math.pow(10,pDecPlaces)) / Math.pow(10,pDecPlaces)
	//Convert in string
	lSourceAmount = "" + pNumber;
	//Restore decimal separator
	lSourceAmount = lSourceAmount.replace(".", pDecimalSeparator)
			
	
	
	
	//Locates the decimal separator.
	lREDec = new RegExp('\\' + pDecimalSeparator);
	lDecPos = lSourceAmount.search(lREDec);
	if (lDecPos!=-1) {
		
		//Stores the decimal part of the amount into the result string
		lDecPart = lSourceAmount.substr(lDecPos+1);
		
		if (lDecPart.length > pDecPlaces) {
			lDecNumber = new Number(lDecPart)
					
			if (isNaN(lDecNumber)) {
				lDecPart = "0"
			} else {
				n = Math.pow(10, lDecPart.length - pDecPlaces)
				lDecNumber = Math.round(lDecNumber/n)
				lDecPart = "" + lDecNumber
			}
		}
		
		lResult = pDecimalSeparator + lDecPart
		lSourceAmount = lSourceAmount.substring(0, lDecPos)
	} else {
		lResult = pDecimalSeparator;
	}
	
	if (pDecPlaces > 0) {
		n = lResult.length - 1
		for(i=1;i<=pDecPlaces-n;i++) {
			lResult = lResult + "0"
		}
	} else {
		lResult = "";
	}
		
	//Inserts the group separator in the integer part of the amount.
	//The following code is taken from an example by Danny Goodman (D. Goodman, 
	//JavaScript Bible, IDGBooks)
	if (pGroupSeparator!='') {
		lRENum = /(-?\d+)(\d{3})/;
		lReplStr = '$1' + pGroupSeparator + '$2';
		while (lRENum.test(lSourceAmount)) {
			lSourceAmount = lSourceAmount.replace(lRENum, lReplStr)
		}
	};

	//Builds the result string and terminates
	if(lSourceAmount != 'NaN') {
		lResult = lSourceAmount + lResult;
	}
	return(lResult); 
}

function CFAmountToNumber(pNumber, pGroupSeparator, pDecimalSeparator) {

var str = new String(pNumber);
var num

if (pGroupSeparator == "")	{
	pGroupSeparator = "\s"
}
var re = new RegExp("["+pGroupSeparator+"]","g")
str = str.replace(re, "")
str = str.replace(pDecimalSeparator, ".")
num = parseFloat(str)

if (isNaN(num)) {
	num = 0
}
return num
}

function CFClientFormatAmount (pAmount, pGroupSeparator, pDecimalSeparator, pDecimalPlaces) {
/*-------------------------------------------------------------------------*\
| Formats a field representing an amount into a string like the following:  |
| x,xxx,xxx.xx                                                              |
| where the separators and decimal places are provided as parameters.       |
|																																						|
| In case of errors, the source amount is returned.                         |
\*-------------------------------------------------------------------------*/

	lSourceAmount = pAmount.value;
	
	pAmount.value = CFNumberToAmount(lSourceAmount, pGroupSeparator, pDecimalSeparator, pDecimalPlaces)
	return;

}


function CFClientUnformatAmount(pAmount, pGroupSeparator) {
/*-------------------------------------------------------------------------*\
| Drops any group separator from an amount field, and leaves the decimal    |
| part unchanged.                                                           |
| In case of errors, the source amount is returned.                         |
\*-------------------------------------------------------------------------*/

	var lSourceAmount;
	var lResult;
	var lTestParam;
	var lRENum;

	
	//Initialization
	lResult = '';
	lTestParam = true;
	lSourceAmount = pAmount.value;
	
	//Parameters check.
	if (typeof(lSourceAmount) != 'string') {
		lTestParam = false
	} else {
		if (lSourceAmount=='') {
			return;
		}
	};

	if (lTestParam) {
		if (typeof(pGroupSeparator) == 'undefined') {
			pGroupSeparator = ','
		} else {
			if (typeof(pGroupSeparator) != 'string') {
				lTestParam = false
			}
		}
	};

	if (!lTestParam) {
		return;
	};

	if (pGroupSeparator == "")	{
		pGroupSeparator = "\s"
	}

	//Drops the grouping separator from the amount
	lRENum = new RegExp('\\' + pGroupSeparator, 'g');
	lResult = lSourceAmount.replace(lRENum, '');
	pAmount.value = lResult;
	return;

}
