var format_currency = function(currency) {
	currency += '';
	x = currency.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 cwCalc(showAmort) {

    cwBalance = document.getElementById('cwBalance');
    cwRate = document.getElementById('cwRate');
    cwMonthlyAmount = document.getElementById('cwMonthlyAmount');
    cwDesiredMonths = document.getElementById('cwDesiredMonths');

    if (cwBalance.value == '') {
        alert('Please enter your credit card balance.'); 
        return;
    }
    if (cwRate.value == '') {
        alert('Please enter your credit card\'s interest rate.'); 
        return;
    }
    if (cwMonthlyAmount.value == '' && cwDesiredMonths.value == '') {
        alert('Please enter either a payment amount or desired months.'); 
        return;
    }

    var mRate = (cwRate.value / 100) / 12;
    if (cwMonthlyAmount.value == '') {
        var payment = cwBalance.value * (mRate) / (1 - Math.pow((1 + mRate), (-cwDesiredMonths.value)));
        cwMonthlyAmount.value = Math.round(payment * 100) / 100 + 0.01;
    }

    var remainingBalance = cwBalance.value;
    var minPayment = mRate * cwBalance.value;
    var months = 0;
    if (minPayment > cwMonthlyAmount.value) {
        alert('Your monthly payment is less than the monthly interest charged by this card.');
        return;
    }

    var amort = '<table class="Table_Amort"><tr>' +
    	'<td class="Cell_Amort_Header">Month</td>' +
    	'<td class="Cell_Amort_Header">Principal</td>' +
    	'<td class="Cell_Amort_Header">Interest</td>' +
    	'<td class="Cell_Amort_Header">Balance</td></tr>';
    var totalPaid = 0.1 - 0.1;
    while (remainingBalance > 0) {
        months++;
        var interest = remainingBalance * mRate;
        var principal = cwMonthlyAmount.value - interest;
        if (principal > remainingBalance) principal = remainingBalance;
        totalPaid = totalPaid + principal + interest;
    
        remainingBalance = remainingBalance - principal;
    
        amort += '<tr><td class="Cell_Amort">' + months + '</td>';
        amort += '<td class="Cell_Amort">$' + format_currency(Number(principal).toFixed(2)) + '</td>';
        amort += '<td class="Cell_Amort">$' + format_currency(Number(interest).toFixed(2)) + '</td>';
        amort += '<td class="Cell_Amort">$' + format_currency(Number(remainingBalance).toFixed(2)) + '</td></tr>';
    }

    cwDesiredMonths.value = months;
    document.getElementById('cwResult').innerHTML = "At $" + format_currency(Number(cwMonthlyAmount.value).toFixed(2)) + " a month, it will take " + months + " months to pay off this card and will cost you a total of $" + format_currency(Number(totalPaid).toFixed(2)) + ".";
    if (showAmort) {
        document.getElementById('cwResult').innerHTML += '<br /><br />Amortization Schedule<br /><div style="overflow: scroll;height: 200px;">' + amort + '</table></div>';
    } else {
        document.getElementById('cwResult').innerHTML += ' <a href="javascript:cwCalc(true);" class="Link_Amort">show amortization</a>';
    }

}