// Function to format a numeric value using currency formatting
function formatCurrency(number, decimalPlaces, currencyCode, leadingDigit, trailingCode, useParens)
{
/*
ARGUMENTS:
number
  Required. Number to be formatted.
decimalPlaces
  Optional. Numeric value indicating how many places to the right of the decimal are displayed.
  Default value is 2.
currencyCode
  Optional. Character value that specifies which country's currency to use.
  Default value is USD.
leadingDigit
  Optional. Boolean value that indicates whether or not a leading zero is displayed for fractional values.
  Default value is False.
trailingCode
  Optional. Boolean value that indicates whether or not the trailing currency code is displayed.
  Default value is False.
useParens
  Optional. Boolean value that indicates whether or not to place negative values within parentheses.
  Default value is False.
*/
	var sign;
	var currency;
	var dollar;
	var cents;
	
	if (isNaN(decimalPlaces))
	{
		number = "0";
	}
	else
	{
		decimalPlaces = Math.abs(decimalPlaces)
	}
	
	switch (currencyCode)
	{
		case "CAD":
			currency = "$";
			break;
		case "EUR":
			currency = "€";
			break;
		case "GBP":
			currency = "£";
			break;
		default:
			currencyCode = "USD";
			currency = "$";
			break;	
	}
	
	if (!leadingDigit)
	{
		leadingDigit = false;
	}
	
	if (!trailingCode)
	{
		trailingCode = false;
	}
	
	if (!useParens)
	{
		useParens = false;
	}
	
	number = number.toString().replace(/\$|\£|\€/,'');
	number = number.toString().replace(/\,/g,'');
	if (isNaN(number))
	{
		number = "0";
	}
	sign = (number != (number = Math.abs(number)));
	number = Math.floor(number * 100 + 0.50000000001);
	dollar = Math.floor(number / 100).toString();
	cents = (number % 100).toString();
	if (cents < 10)
	{
		cents = '0' + cents;
	}
    while (cents.length < decimalPlaces)
    {
        cents += '0';
	}
	
	for (var i = 0; i < Math.floor((dollar.length - (1 + i)) / 3); i++)
	{
		dollar = dollar.substring(0, dollar.length - (4 * i + 3)) + ',' + dollar.substring(dollar.length - (4 * i + 3));
	}
	
	if (sign)
	{
		number = ((useParens)?'(':'-') + currency + ((dollar == 0 && !leadingDigit)?'':dollar) + '.' + cents + ((useParens)?')':'') + ((trailingCode)?' '+currencyCode:'');
	}
	else
	{
		number = currency + ((dollar == 0 && cents != 0 && !leadingDigit)?'':dollar) + '.' + cents + ((trailingCode)?' '+currencyCode:'');
	}
	
	return number;
}

// Function to round number
function round(number, decimalPlaces)
{
/*
ARGUMENTS:
number
  Required. Number to be formatted.
decimalPlaces
  Optional. Numeric value indicating how many places to the right of the decimal are rounded.
  Default value is 0.
*/
	if (isNaN(number))
	{
		number = "0";
	}
	
	if (decimalPlaces == undefined)
	{
		number = Math.round(number);
	}
	else
	{
		number = Math.round(number * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces);
	}
	return number;
}

