function cwCalc()
{
	
var balance = document.getElementById('cwBalance').value;
var rate = document.getElementById('cwRate').value;
var monthlyAmount = document.getElementById('cwMonthlyAmount').value;
var monthlyProportion = document.getElementById('cwMonthlyProportion').value;
var minimum = document.getElementById('cwMinimum').value;


if (balance=='') {alert('Please enter your credit card balance.'); return;}
if (rate=='') {alert('Please enter your credit card interest rate.'); return;}

if ( (monthlyAmount=='' && monthlyProportion =='') || (monthlyAmount!='' && monthlyProportion!='')) {alert('Please enter either a percentage or a fixed payment amount.'); return;}

if ((minimum=='' && monthlyProportion!='')) {alert('This calculation requires that you enter both the percentage you wish to pay and the minimum repayment required.'); return;}

if (monthlyAmount > balance) {
	alert ('The payment amount you have entered is higher than your balance. If you regularly pay off your balance in full you should not incur any interest charges.');
	return;
}

var payAmount=monthlyAmount;
var apr=rate;

var mRate=(rate/100)/12;
var x = 1+(rate/100);
var y = 1/12;
var power=Math.pow(x,y);
var mPercent=(power-1)*100;

//this section does the percentage calculation
	
if (monthlyProportion!=''){
	
	var proportion=(monthlyProportion/100);
	var remainingBalance=balance;
	var minPayment=minimum;
	var months = 0;
	var monthlyInterest=0;
	var mCharge1=0;
	var payAmount1 = 0;
	var paymentAmount = 0;

	payAmount1 = (balance*proportion);	

	if (payAmount1 > balance) {alert('If you always pay your bill in full you should not incur any interest.'); return;}
	
	if (minPayment > payAmount1) {
		alert ('This percentage you have entered is less than the minimum repayment required.');return;
	}

	if (minimum.value=='') {
		alert('Please enter your minimum payment amount - typically £5 or £10.'); return;
	}
	mCharge1 = (balance - payAmount1)*(mPercent/100);
	remainingBalance = remainingBalance - payAmount1 + mCharge1;
	months++;
			
	while(remainingBalance >= 0) {		
        if(eval(remainingBalance * proportion) < minPayment) {
			paymentAmount = minPayment; 
			} 
		else { 
			paymentAmount = proportion * remainingBalance; 
			}	
		
		var mCharge = (remainingBalance - paymentAmount)*(mPercent/100);
		if (paymentAmount <= remainingBalance){
			monthlyInterest = monthlyInterest + mCharge;
		}
		remainingBalance = remainingBalance - paymentAmount + mCharge;
		months++;
		if(months > 1200) { 
			alert ('If you do not increase the payment amount you may never pay off the balance; it would take over 100 years at this rate.')
		return; } else { continue;}
	}
	
	monthlyInterest = monthlyInterest + mCharge1;	
	monthlyInterest = Math.round(monthlyInterest*100)/100;
	monthlyInterest = monthlyInterest.toFixed(2);
	
	var years = months / 12;
	years=Math.floor(years);
	months = months - (years * 12)
	  
	if (years==1) {document.getElementById('cwResult').innerHTML="It will take " + years + " year and " + months + " months to pay off the balance, and it will cost a total of £" +  monthlyInterest + " in interest. ";
	}
	if (years!=1) { 
document.getElementById('cwResult').innerHTML="It will take " + years + " years and " + months + " months to pay off the balance, and it will cost a total of £" +  monthlyInterest + " in interest.";
	}
} 
	
	
//this section does the fixed calculation
else {
	var remainingBalance=balance;
	var minPayment=mRate*balance;
	var months=0;
	var monthlyInterest=0;
	var mCharge1=0;



	mCharge1 = (remainingBalance - monthlyAmount)*(mPercent/100);
	
	if (monthlyAmount < mCharge1) {
		alert ('Your payments are less than the interest charged by this card, meaning that your balance will increase rather than reduce.');return;}
		
	remainingBalance = remainingBalance - monthlyAmount + mCharge1;
	months++;

	while (remainingBalance >= 0){

		var mCharge = (remainingBalance - monthlyAmount)*(mPercent/100);

		if (monthlyAmount <= remainingBalance){		
			monthlyInterest = monthlyInterest + mCharge;
		}

		remainingBalance = remainingBalance - monthlyAmount + mCharge;
		months++;
			if(months > 1200) { 
				alert ('If you do not increase the payment amount you may never pay off the balance; it would take over 100 years at this rate.')
			return; } else { continue;}
	
	}
	monthlyInterest = monthlyInterest + mCharge1;	
	monthlyInterest = Math.round(monthlyInterest*100)/100;
	monthlyInterest = monthlyInterest.toFixed(2);
   
    var years = months / 12;
	years=Math.floor(years);
	months = months - (years * 12)
	
	if (years==1) {cwResult.innerHTML="It will take " + years + " year and " + months + " months to pay off the balance, and it will cost a total of £" +  monthlyInterest + " in interest.";
	}
	if (years!=1) { 
cwResult.innerHTML="It will take " + years + " years and " + months + " months to pay off the balance, and it will cost a total of £" +  monthlyInterest + " in interest.";
	}
	}
}
//  End -->
