// Copyright © 2002 Australasian Management and Technology (AMAT) PTY LTD, Suite 8, 340 Darling Street, Balmain NSW 2041, Australia. All rights reserved. 
// Warning: This computer program is protected by copyright law and international treaties. Unauthorized reproduction or distribution of this program, or any portion of it, may result in severe civil and criminal penalties, and will be prosecuted to the maximum extent possible under the law.

// AMAT MortgageFirst Comparison Rate Calculator

function doComparisonRateCalculations(n)
{
	var comprate = calculateComparisonRate()
	
	document.getElementById("message").innerHTML = '<h5>The comparison rate for this scenario is <b>' + numval(comprate, 2) + '%</b> p.a.</h5>';
	document.getElementById("disclaimer").innerHTML = strComparisonRate;
	document.getElementById("spacer").innerHTML = '<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>';
}

// Function to calculate the Comparison Rate

function calculateComparisonRate()
{
	var principal = parseFloat(document.Q6.principal.value);
	var term = parseFloat(document.Q6.year.value);
	var rate = parseFloat(document.Q6.interest.value);
	var comparisonrate = rate;
	var upfrontCost = parseFloat(document.Q6.upfrontcost.value);
	var monthlyCost = parseFloat(document.Q6.monthlycost.value);
	var quarterlyCost = parseFloat(document.Q6.quarterlycost.value);
	var annualCost = parseFloat(document.Q6.annualcost.value);
	var dischargeCost = parseFloat(document.Q6.dischargecost.value);
	var fixedrate = parseFloat(document.Q6.fixedrate.value);
	var fixedmonths = parseFloat(document.Q6.fixedmonths.value);
	
	var stepSize = 2;			// Initial Stepping
	var maxGuess = 20; 			// Max no. of guesses
	var errorMargin = 0.0001; 	// Acceptable error margin
	
	var monthlyRepaymentFixed = getMonthlyRepayment(principal, term, fixedrate);
	var futureValue = getFutureValue(principal, fixedmonths, fixedrate, monthlyRepaymentFixed);
	var monthlyRepayment = getMonthlyRepayment(futureValue, ((term*12)-fixedmonths)/12, rate);

	var i = 0
	while (i < maxGuess) {
		var curNPV = getNPV(principal, monthlyRepaymentFixed, monthlyRepayment, term, comparisonrate, fixedrate, fixedmonths, upfrontCost, monthlyCost, quarterlyCost, annualCost, dischargeCost);

		if (stepSize < errorMargin) {
			break;
		}

		if (curNPV > 0) {
			comparisonrate += stepSize;
		} else {
			comparisonrate -= stepSize;
		}

		stepSize /= 2;

		i++;
	}

	return comparisonrate;
}

function getFutureValue(principal, term, rate, repayment) {
	var r = rate/100/12;
	var balance = principal;
	for (var i = 0; i < term; i++) {
		balance += (balance*r - repayment);
	}
	return balance;
}

function getMonthlyRepayment(principal, term, rate) {
	var r = rate/100/12;
	return (principal*r)/(1 - Math.pow(1+r,-term*12));
}

function getNPV(principal, monthlyRepaymentFixed, monthlyRepayment, term, comparisonrate, fixedrate, fixedmonths, upfrontCost, monthlyCost, quarterlyCost, annualCost, dischargeCost) {

	var r = comparisonrate/100/12;		// % monthly
	var result = 0;

	result -= principal;
	result += upfrontCost;


	for (i = 1; i <= term * 12; i++) 
	{
		// Base monthly cashflow
		if (i <= fixedmonths) {
			var cashflow = monthlyRepaymentFixed;
		
		} else {
			var cashflow = monthlyRepayment;
		}
		
		// Add monthly cost
		if (i % 1 == 0) { cashflow += monthlyCost; }

		// Add quarterly cost
		if (i % 3 == 0) { cashflow += quarterlyCost; }

		// Add annual cost
		if (i % 12 == 0) { cashflow += annualCost; }

		// Add discharge cost
		if (i == term * 12) { cashflow += dischargeCost; }

		var denom = Math.pow(1+r, i);

		result += (cashflow/denom);
	}

	return result;
}