
function Calculate()
{
	
	
	var curTotalOwed;
	var curMonthlyOwed;
	var nMonths;
	var nAPR;
	var nMonthlyRate;
	var nMonthlyRepayment;
	var nTotalAmountPayable;
	var nSaving;
	
	curTotalOwed = eval(document.debtbusting.txtBankLoansAmount.value) + eval(document.debtbusting.txtCreditCardsAmount.value) + eval(document.debtbusting.txtCarFinanceAmount.value) + eval(document.debtbusting.txtFurnitureAmount.value) + eval(document.debtbusting.txtHPAmount.value) + eval(document.debtbusting.txtOtherAmount.value);
	curMonthlyOwed = eval(document.debtbusting.txtBankLoansMonthly.value) + eval(document.debtbusting.txtCreditCardsMonthly.value) + eval(document.debtbusting.txtCarFinanceMonthly.value) + eval(document.debtbusting.txtFurnitureMonthly.value) + eval(document.debtbusting.txtHPMonthly.value) + eval(document.debtbusting.txtOtherMonthly.value);
	nMonths = document.debtbusting.cboDebtBusterCalculator.value
	if ((isNaN(curTotalOwed)) || (isNaN(curMonthlyOwed)) || (isNaN(nMonths))){
		alert("You must enter valid values in both the Total AND Monthly payment fields and select a Repayment Period.");
		return false;
	}
	for (nIndex = 0;nIndex < m_arAmounts[0].length;nIndex++ ){
		if (m_arAmounts[0][nIndex] >= curTotalOwed){
			nAPR = m_arAmounts[1][nIndex]
			nIndex = m_arAmounts[0].length
		}
	}
	if (isNaN(nAPR)){
		alert("Failed to calculate the required APR.");
		return false;
	}
	nMonthlyRepayment = GetMonthlyRepaymentAmount(curTotalOwed, nAPR, nMonths);
	nTotalAmountPayable = nMonthlyRepayment * nMonths;
	document.debtbusting.txtTotalOwed.value = FormatCurrency(curTotalOwed);
	document.debtbusting.txtTotalMonthly.value = FormatCurrency(((Math.round(curMonthlyOwed  * 100)) / 100));
	document.debtbusting.txtNewMonthlyRepayment.value = FormatCurrency((Math.round(nMonthlyRepayment * 100)) / 100);
	nSaving = FormatCurrency((Math.round((curMonthlyOwed - nMonthlyRepayment) * 100)) / 100);
	if (nSaving > 0){
		document.debtbusting.txtSaving.value = nSaving;
	}else{
		document.debtbusting.txtSaving.value = 0.00;
	}
	
}

function FormatCurrency(vValue)
{
	var Pounds = Math.floor(vValue).toString();
	var Pence = Math.round((vValue - Math.floor(vValue))*100).toString();
	if (parseInt(Pence) < 10)
	{
		if (parseInt(Pence) == 0)
		{
			Pence = "00";
		}
		else
		{
			Pence = "0" + Pence;
		}
	}
	return(Pounds + "." + Pence);		
}