function FormatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    
    if(isNaN(num))
	    num = "0";
				
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	num = Math.floor(num / 100).toString();
	
	for (var i = 0; i < Math.floor((num.length-(1 + i)) / 3); i++)
		num = num.substring(0, num.length-(4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
	
	return '$' + ((sign) ? '' : '-') + num;
}

function FormatNumberWithDecimalPlaces(num, decimalPlaces) {
    var defaultVal;
    
    if (decimalPlaces <= 0)
        defaultVal = 0;
    else {
        defaultVal = "0.";
        
        for (var i = 1; i < decimalPlaces; i++)
            defaultVal += "0";
    }
     
    try {
	    if (num == null || isNaN(num) || num == "")
	        return defaultVal;
	    else {
	        var sValue = num;
	        sValue = parseFloat(sValue);
	        sValue = sValue.toFixed(decimalPlaces);
	        num = sValue;
	        return (num);
	    }
	}
	catch(exception) {
		return defaultVal;
	}
}

// DecimalPalces ignored for currency, months and years
function FormatSliderNumber(SliderValue, FormattingStyle, DecimalPlaces) {
    var ValueToDisplay;
    
    if (FormattingStyle == "Currency")
        ValueToDisplay = FormatCurrency(SliderValue);
    else if (FormattingStyle == "Years") {
        if (SliderValue == 0)
            ValueToDisplay = "Less Than 1 Year";
        else if (SliderValue == 1)
            ValueToDisplay = "1 Year";
        else
            ValueToDisplay = SliderValue + " Years";
    }
    else if (FormattingStyle == "Months") {
        if (SliderValue == 0)
            ValueToDisplay = "Less Than 1 Month";
        else if (SliderValue == 1)
            ValueToDisplay = "1 Month";
        else if (SliderValue < 12)
            ValueToDisplay = SliderValue + " Months";
        else if (SliderValue == 12)
            ValueToDisplay = "1 Year";
        else if (SliderValue % 12 == 0)
            ValueToDisplay = Math.floor(SliderValue / 12) + " Years";
        else
            ValueToDisplay = Math.floor(SliderValue / 12) + " Years, " + (SliderValue % 12) + " Months";
    }
    else {
        if (DecimalPlaces > 0) {
            var divVal = 10;
            
            for (var i = 1; i < DecimalPlaces; i++)
            {
                divVal *= 10;
            }
            
            SliderValue *= (1 / divVal);
        }

        // Decimal or Percent
        ValueToDisplay = FormatNumberWithDecimalPlaces(SliderValue, DecimalPlaces);
        
        if (FormattingStyle == "Percent")
            ValueToDisplay += "%";
    }

    return String(ValueToDisplay);
}
