// JavaScript Document

/////////////////////////////////////////////////////////////////////
//How much can I borrow?



function CalculateBorrowing(){
	
    var	App1Income		=	document.getElementById("borrowapp1").value;
	App1Income = parseFloat(App1Income.replace(/[^0-9.]/g, ''));
	var	App2Income		=	document.getElementById("borrowapp2").value;
	App2Income			=	parseFloat(App2Income.replace(/[^0-9.]/g, ''));
		if (isNaN(App1Income)){		App1Income = 0	}	
	if (isNaN(App2Income)){		App2Income = 0	}
	
	var income = App1Income +  App2Income;
	var loweramount = income * 3;
	var higheramount = income * 4.25;
	loweramount = addCommas(loweramount);
	higheramount = addCommas(higheramount);
	document.getElementById('loweramount').innerHTML = loweramount;
	document.getElementById('higheramount').innerHTML = higheramount;
	document.getElementById('borrowresult').style.display = "block";
	
	}
	
/////////////////////////////////////////////////////////////////////
//What will it cost?
function borrowingcosts(){

//declare key variables
	var	Loan		=	document.getElementById("costloan").value;
	Loan			=	parseFloat(Loan.replace(/[^0-9.]/g, ''));
	var	Rate		=	document.getElementById("costrate").value;
	Rate			=	parseFloat(Rate.replace(/[^0-9.]/g, ''));
	var	Term		=	new Number(document.getElementById("costterm").value);
	Loan			=	parseFloat(Loan);
	Term			=	parseFloat(Term);
	
//interest only amount
	var InterestOnly = (Loan * (Rate/100))/12
	InterestOnly = Math.round(InterestOnly);
	
//repayment amount
	var MonthlyRate = Rate/1200 
	var TermMonths = Term * -12
	var Multiplier = 1 + MonthlyRate
	Multiplier = Math.pow(Multiplier, TermMonths)
	Multiplier = 1 - Multiplier
	Multiplier = MonthlyRate / Multiplier
	var RepaymentAmount = Loan * Multiplier
	RepaymentAmount = Math.round(RepaymentAmount)
	document.getElementById("IOresults").innerHTML = addCommas(InterestOnly)
	document.getElementById("Rresults").innerHTML  = addCommas(RepaymentAmount)
	if (isNaN(Loan) | isNaN(Rate)  | isNaN(Term) ){ 
	document.getElementById('costresult').style.display = "none"; 
	document.getElementById('costerror').innerHTML = "Error: Please only enter numeric values";
	}
	else {document.getElementById('costresult').style.display = "block";
	document.getElementById('costerror').style.display = "none";}
}


