var crdBalance;
var intRate;
var mnthRepay;
var monthsClearDebt;
var yearsClearDebt;

function UpdateRepay()
{
	//document.frmCard.resultRepay.value="test";
	// get the forms variables
	getValsRepay();
	
	//alert('Please enter a valid card balance');
	if (isNaN(crdBalance) || crdBalance=="")
	{
		//document.frmCard.resultRepay.value=crdBalance;
		alert('Please enter a valid card balance');
		document.frmCard.crdBalanceRepay.focus();
	}
	else if (isNaN(intRate) || intRate=="")
	{
		alert('Please enter a valid interest rate');
		document.frmCard.intRateRepay.focus();
	}
	else if (isNaN(mnthRepay) || mnthRepay=="")
	{	
		alert('Please enter a valid monthly repayment');
	    document.frmCard.mnthRepay.focus();
	}
	else
	{
		CalcResultsRepay(true);
	}
}

function CalcResultsRepay(clickCalc)
{	
	var remainingBalance = 0; 
	var mnIntRate = null;
	var monthsPaid = 0;
	var monthlyIntPay = null;
	var totIntPaid = 0;
	var paidOff = true; // can the card be paid off based on the repayment and interest amounts
	var yearsStr = 'years';
	var monthsStr = 'months';
	
	// calc the monthly interest rate
	mnIntRate = GetMnthIntRate();
	
	// simulate making payments each month
	remainingBalance = crdBalance;
	
	while (remainingBalance > 0)
	{
		monthsPaid++;
		
		// calc monthly int payment
		monthlyIntPay = remainingBalance * mnIntRate;
		
		// add int payment and subtract payment from remaining balance
		remainingBalance = remainingBalance + monthlyIntPay - mnthRepay;
					
		// keep runnning total of interested paid
		totIntPaid = totIntPaid + monthlyIntPay;
		
		// is the base amount going up instead of down because of interest
		if (remainingBalance >= crdBalance)
		{
			paidOff = false;
			
			if (clickCalc)
			{
				alert('Your monthly payment is less than your monthly interest fee');
			}
			break;
		}
	}
	
	// if the card was able to be paid off, update the result boxes
	if (!paidOff)
	{
		ResetFieldsRepay();
	}
	else
	{
		if (monthsPaid >= 12)
		{
			var years = Math.floor(monthsPaid/12);
			var months = monthsPaid-(years*12);
			
			if(years==1){
				yearsStr = 'year';
			}
			if(months==1){
				monthsStr = 'month';
			}
			
			document.frmCard.resultRepay.value = years+' '+yearsStr+', '+months+' '+monthsStr;
			totIntPaid = totIntPaid.toFixed(2);
			document.frmCard.totIntPaid.value = totIntPaid;
		}
		else
		{
			if(monthsPaid==1){
				monthsStr = 'month';
			}
			document.frmCard.resultRepay.value = monthsPaid+' '+monthsStr;
			totIntPaid = totIntPaid.toFixed(2);
			document.frmCard.totIntPaid.value = totIntPaid;
		}
		// total cost -> document.frmCard.resultBox2.value = (crdBalance+totIntPaid).toFixed(2);
	}
}

function getValsRepay()
{
   //document.frmCard.resultRepay.value="getValsRepay";
   crdBalance = new Number(document.frmCard.crdBalanceRepay.value);
   intRate = new Number(document.frmCard.intRateRepay.value);
   mnthRepay = new Number(document.frmCard.mnthRepay.value);
   //document.frmCard.resultRepay.value=crdBalance;
}

function GetMnthIntRate()
{
	return (parseFloat(intRate)/100)/12;
}

function ResetFieldsRepay()
{
	document.frmCard.resultRepay.value = '';
}


function PopCardBalRepay()
{
	if (document.frmCard.crdBalanceMonths.value == null || document.frmCard.crdBalanceMonths.value == ''){
		document.frmCard.crdBalanceMonths.value = document.frmCard.crdBalanceRepay.value;
	}
}

function UpdateMonths()
{
	// get the forms variables
	getValsMonths();
	
	if (isNaN(crdBalance) || crdBalance=="")
	{
		alert('Please enter a valid card balance');
		document.frmCard.crdBalanceMonths.focus();
	}
	else if (isNaN(intRate) || intRate=="")
	{
		alert('Please enter a valid interest rate');
		document.frmCard.intRateMonths.focus();
	}
	else if (monthsClearDebt == 0 && yearsClearDebt == 0)
	{
		alert('Please select your desired time frame');
		document.frmCard.yearsSelect.focus();
	}
	else
	{
		CalcResultsMonths(true);
	}
}

function getValsMonths()
{
   crdBalance = new Number(document.frmCard.crdBalanceMonths.value);
   intRate = new Number(document.frmCard.intRateMonths.value);
   monthsClearDebt = document.frmCard.monthsSelect[document.frmCard.monthsSelect.selectedIndex].value;
   yearsClearDebt = document.frmCard.yearsSelect[document.frmCard.yearsSelect.selectedIndex].value;
}

function CalcResultsMonths ()
{
	// want card paid off in X months, work out required monthly payment and total cost
	var mnIntRate = null;
	
	// calc the monthly interest rate
	mnIntRate = GetMnthIntRate();
	
	var totalMonths = ((parseInt(yearsClearDebt)*12) + parseInt(monthsClearDebt));

	var mnthlyIntCost = (crdBalance*mnIntRate);
	var change = 1-Math.pow((1+mnIntRate),-totalMonths);
	var reqPayment = (mnthlyIntCost / change).toFixed(2);
	
	document.frmCard.resultMonths.value = reqPayment; 
	// total cost -> document.frmCard.resultBox2.value = (reqPayment*totalMonths).toFixed(2);
}

function calculate()
{
	var amt;
	var apr;
	var months;
	var topay;
	var intrate;
	var pow;
	var temp;
	
	with(document.frmCard)
	{ 
	if (amount.value=="")
		{
				
		alert("Please enter the amount you plan to borrow");
		amount.focus();
								
	    }
	else if (aprate.value=="")
		{
		alert("Please enter the APR value");
		aprate.focus();
		}
	else if (nomonth.value=="")
		{
		alert("Please enter the number of months");
		nomonth.focus()
		
		}
	else if (isNaN(amount.value))
		{
		
		alert("Please enter a number");
		amount.focus();
		}
	else if (isNaN(aprate.value))
		{
		
		alert("Please enter a number");
		aprate.focus();
		}
	else if (isNaN(nomonth.value))
		{
		
		alert("Please enter a number");
		nomonth.focus();
		}
	else
		{
		
		amt=amount.value;
		apr=aprate.value;
		months=nomonth.value;
		
		amt=parseFloat(amt);
		apr=parseFloat(apr);
		months=parseFloat(months);
		
		
		intrate=((apr/100)/12);
		temp=intrate+1;
		pow=Math.pow(temp,months);
		
		topay=((amt*intrate)*pow)/(pow-1);//monthly payment including interest
		totpay=topay*months;//total payment during the entire period including interest
		totinterest=totpay-amt;//total interest during the entire period
		
		
		//rounding the value to 2 decimal places
		var resultm1=topay*Math.pow(10,2);
		var resultm2=Math.round(resultm1);
		var resultm3=resultm2/Math.pow(10,2);
		
		var resultt1=totpay*Math.pow(10,2);
		var resultt2=Math.round(resultt1);
		var resultt3=resultt2/Math.pow(10,2);
		
		var resultint1=totinterest*Math.pow(10,2);
		var resultint2=Math.round(resultint1);
		var resultint3=resultint2/Math.pow(10,2);
		
		result.value=resultm3;
		total.value=resultt3;
		interest.value=resultint3;
		
		}
			
	}
}