
function cwCalc()
{
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=='') || (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)) );
	payment=Math.round(payment*100)/100;
	cwResult.innerHTML="It will cost $" + payment.toFixed(2) + " a month to pay off this card and will cost you a total of $" + (payment*cwDesiredMonths.value).toFixed(2) + ".";
} else {
	var remainingBalance=cwBalance.value;
	var minPayment=mRate*cwBalance.value;
	var months=0;
	var lastPayment;
	if (minPayment>cwMonthlyAmount.value) {alert ('Your monthly payment is less than the monthly interest charged by this card.');return;}
	while (remainingBalance>0)
	{
		months++;
		remainingBalance=remainingBalance*(1 + mRate)-cwMonthlyAmount.value;
	}
	cwResult.innerHTML="It will take " + months + " months to pay off this card and will cost you a total of $" + (cwMonthlyAmount.value*months).toFixed(2) + ".";
}
}

