/* Reset the fields on the payments page */
function ResetPayments(){
	var PandIPayment = document.getElementById("pandipayment");
	var InterestOnlyPayment = document.getElementById("interestonlypayment");
	var divOutputText = document.getElementById("divOutputText")
	
	PandIPayment.value = "Not Calculated";
	InterestOnlyPayment.value = "Not Calculated";
	divOutputText.innerHTML = "<p> Enter the loan details above and press calculate.</p>";
}

/* The main payment calculator function. */
function CalculatePayment(){
	/* Retrieve all the information from the form */
	/* We should validate it really but I have not written that yet*/
	var LoanAmount = parseFloat(document.getElementById("loanamount").value);
	var LoanTerm = 12 * parseInt(document.getElementById("loanterm").value);
	var InterestRate = parseFloat(document.getElementById("loanrate").value);
	var PandIPayment = document.getElementById("pandipayment");
	var InterestOnlyPayment = document.getElementById("interestonlypayment");
	var divOutputText = document.getElementById("divOutputText")
	
	/* Now calculate the P & I payment */	
	var CalculatedPandIPayment = IterateUntilSolved(LoanAmount, LoanTerm, InterestRate);
	
	/* Now update the P&I payment on the form */ 
	PandIPayment.value=CalculatedPandIPayment;
	
	/* Now update the interest only payment */
	InterestOnlyPayment.value=Math.round(LoanAmount * InterestRate / 12)/100;
	
	/* Update the text in the form */
	divOutputText.innerHTML = "<p>Your monthly payments are shown below.</p>";}
		
/* This is the function that finds the payment */
function IterateUntilSolved(LoanAmount, LoanTerm, InterestRate){
	
	/* Set the Solved variable to False */
	var Solved = new Boolean(false)
	
	/* Cap the number of attempts to 30 */
	for (i = 1; i <= 30; i++) {
		
		/* If it is our first attempt calculate the first guess */
		if (i == 1) 
			{MyGuess1 = FirstGuess(LoanAmount, LoanTerm, InterestRate);
			MyBalance1 = OutstandingCapitalCalculation(LoanAmount, LoanTerm, InterestRate, MyGuess1);
			MyBalance = MyBalance1;
			MyGuess = MyGuess1;}
		
		/* If it is our first attempt calculate the first guess */
		if (i == 2) 
			{MyGuess2 = SecondGuess(MyGuess1,LoanTerm);
			MyBalance2 = OutstandingCapitalCalculation(LoanAmount, LoanTerm, InterestRate, MyGuess2);
			MyBalance = MyBalance2;
			MyGuess = MyGuess2;}
		
		if (i >= 3)
			{if (MyBalance2 == MyBalance1) 
				{MyGuess = MyGuess1 + (MyBalance1 / LoanTerm);}
			else 
				{MyGuess = CalculateNextGuess(MyGuess1, MyBalance1, MyGuess2, MyBalance2);}
				
			MyBalance = OutstandingCapitalCalculation(LoanAmount, LoanTerm, InterestRate, MyGuess);}
		
		if (MyBalance >= (-1 - 0.01 * LoanTerm) && MyBalance <= (1 + 0.01 * LoanTerm)) {
			Solved = true;
			MyAnswer = Math.round(100* MyGuess) / 100;
			break;
		}
		else {
			if (i >= 3) {
				MyGuess1 = MyGuess2;
				MyBalance1 = MyBalance2;
				MyGuess2 = MyGuess;
				MyBalance2 = MyBalance;
			}
		}
	}
	if (Solved == false)
		{MyAnswer = "Calculation failed.";}
	
	return (MyAnswer);}	
	

function FirstGuess(LoanAmount, LoanTerm, InterestRate){
		var EstimatedPayment = Math.floor(100*((LoanAmount / LoanTerm) + (LoanAmount * InterestRate / 1200)))/100;
		return(EstimatedPayment);}		

function SecondGuess(FirstGuess,LoanTerm){
		var EstimatedPayment2 = Math.floor(FirstGuess * 3 / 4);
		return(EstimatedPayment2);
		}
		
function OutstandingCapitalCalculation(LoanAmount, LoanTerm, InterestRate, EstimatedPayment){
		var FinishBal = LoanAmount;
		
		for (j = 1; j <= LoanTerm; j++) {
			FinishBal = Math.round((100 * FinishBal * (1 + InterestRate / 1200) - (100 * EstimatedPayment))) / 100;}	

		return(FinishBal);}

/***********************************************************************************************************************/
		
function CalculateNextGuess(Payment1, Balance1, Payment2, Balance2){
		NextGuess = ((-Balance1 * (Payment2 - Payment1)) / (Balance2 - Balance1)) + Payment1;
		return(NextGuess);}


		
		