// Copyright © 2004 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 LMI Calculator

// Rates should be entered as a decimal with two places + 1.00 
// A 10% Stamp Duty rate should be entered as 1.10 or a 8% Stamp Duty rate should be entered as 1.08
var LMIStampDuty_NSW = 1.05;
var LMIStampDuty_ACT = 1.11;
var LMIStampDuty_QLD = 1.09;
var LMIStampDuty_VIC = 1.11;
var LMIStampDuty_SA = 1.11;
var LMIStampDuty_WA = 1.11;
var LMIStampDuty_NT = 1.11;
var LMIStampDuty_TAS = 1.09;

// LMI premiums used are based on the BankWest premiums
// LVRValue relates to LVR tiers between 81.00 and 97.00
// loPremiumPI relates to LMI premiums for loan amounts equal or less than $300,000 and for Principal & Interest loans
// hiPremiumPI relates to LMI premiums for loan amounts greater than $300,000 and for Principal & Interest loans
// loPremiumInt relates to LMI premiums for loan amounts equal or less than $300,000 and for Interest Only loans
// hiPremiumInt relates to LMI premiums for loan amounts greater than $300,000 and for Interest Only loans
// LVR should be entered with two decimal places and premiums should be entered with three decimal places
var LVRValue = new Array(18);
var loPremiumPI = new Array(18);
var hiPremiumPI = new Array(18);
var loPremiumInt = new Array(18);
var hiPremiumInt = new Array(18);

LVRValue[0] = 80.00; loPremiumPI[0] = 0.000; hiPremiumPI[0] = 0.000; loPremiumInt[0] = 0.000; hiPremiumInt[0] = 0.000;
LVRValue[1] = 81.00; loPremiumPI[1] = 0.000; hiPremiumPI[1] = 0.000; loPremiumInt[1] = 0.000; hiPremiumInt[1] = 0.000;
LVRValue[2] = 82.00; loPremiumPI[2] = 0.510; hiPremiumPI[2] = 0.660; loPremiumInt[2] = 0.550; hiPremiumInt[2] = 0.710;
LVRValue[3] = 83.00; loPremiumPI[3] = 0.700; hiPremiumPI[3] = 0.990; loPremiumInt[3] = 0.750; hiPremiumInt[3] = 1.060;
LVRValue[4] = 84.00; loPremiumPI[4] = 0.770; hiPremiumPI[4] = 0.990; loPremiumInt[4] = 0.830; hiPremiumInt[4] = 1.060;
LVRValue[5] = 85.00; loPremiumPI[5] = 0.890; hiPremiumPI[5] = 1.250; loPremiumInt[5] = 0.960; hiPremiumInt[5] = 1.340;
LVRValue[6] = 86.00; loPremiumPI[6] = 0.960; hiPremiumPI[6] = 1.250; loPremiumInt[6] = 1.390; hiPremiumInt[6] = 1.340;
LVRValue[7] = 87.00; loPremiumPI[7] = 1.080; hiPremiumPI[7] = 1.410; loPremiumInt[7] = 1.580; hiPremiumInt[7] = 2.040;
LVRValue[8] = 88.00; loPremiumPI[8] = 1.150; hiPremiumPI[8] = 1.480; loPremiumInt[8] = 1.650; hiPremiumInt[8] = 2.040;
LVRValue[9] = 89.00; loPremiumPI[9] = 1.270; hiPremiumPI[9] = 1.660; loPremiumInt[9] = 1.840; hiPremiumInt[9] = 2.390;
LVRValue[10] = 90.00; loPremiumPI[10] = 1.330; hiPremiumPI[10] = 1.720; loPremiumInt[10] = 1.930; hiPremiumInt[10] = 2.390;
LVRValue[11] = 91.00; loPremiumPI[11] = 1.540; hiPremiumPI[11] = 2.520; loPremiumInt[11] = 2.230; hiPremiumInt[11] = 0.000;
LVRValue[12] = 92.00; loPremiumPI[12] = 1.590; hiPremiumPI[12] = 2.650; loPremiumInt[12] = 2.310; hiPremiumInt[12] = 0.000;
LVRValue[13] = 93.00; loPremiumPI[13] = 1.780; hiPremiumPI[13] = 2.730; loPremiumInt[13] = 2.590; hiPremiumInt[13] = 0.000;
LVRValue[14] = 94.00; loPremiumPI[14] = 1.850; hiPremiumPI[14] = 2.940; loPremiumInt[14] = 2.690; hiPremiumInt[14] = 0.000;
LVRValue[15] = 95.00; loPremiumPI[15] = 2.040; hiPremiumPI[15] = 3.150; loPremiumInt[15] = 2.810; hiPremiumInt[15] = 0.000;
LVRValue[16] = 96.00; loPremiumPI[16] = 2.490; hiPremiumPI[16] = 0.000; loPremiumInt[16] = 0.000; hiPremiumInt[16] = 0.000;
LVRValue[17] = 97.00; loPremiumPI[17] = 2.490; hiPremiumPI[17] = 0.000; loPremiumInt[17] = 0.000; hiPremiumInt[17] = 0.000;

function fLMIPremium(loanAmount, propertyValue, state, mortgageType, loanType)
{
	var LVR = 0;
	var LMIPremium = 0;
	var LVRCounter = 0;
	var LMIBase = 0;
	var LMIStampDuty = 0;
	
	// calculate the Loan to Value Ratio
	LVR = (loanAmount / propertyValue) * 10000;
	LVR = Math.round(LVR);
	LVR = LVR / 100;
	
	// loop through the array of LVR values until an LVR value that is less than or equal to the LVR value is found
	for (LVRCounter = 0; LVRCounter < LVRValue.length; LVRCounter++)
	{
		if (LVR <= LVRValue[LVRCounter])
		{
			break;
		}
	}
	
	// calculate the base LMI Premium amount for the specified LVR and loan type
	if (loanAmount <= 300000)
	{
		if (loanType == "P&I")
		{
			LMIBase = ((loanAmount * loPremiumPI[LVRCounter]) / 100);
		}
		else if (loanType == "INT")
		{
			LMIBase = ((loanAmount * loPremiumInt[LVRCounter]) / 100);
		}
	}
	else if ((loanAmount > 300000) && (loanAmount <= 500000))
	{
		if (loanType == "P&I")
		{
			LMIBase = ((loanAmount * hiPremiumPI[LVRCounter]) / 100);
		}
		else if (loanType == "INT")
		{
			LMIBase = ((loanAmount * hiPremiumInt[LVRCounter]) / 100);
		}
	}
	else
	{
		LMIBase = 0.00;
	}
	
	
	// LMI is usually not available for loans over $500,000 or LVR less than 80%
	if ((loanAmount > 500000) || ((LVR < 80) || (LVR > 95)))
	{
		// do nothing
	}
	else if ((loanAmount > 300000) && (loanType == "INT"))
	{
		// do nothing
	}
	else
	{
		// obtain the LMI Stamp Duty for the specified state
		LMIStampDuty = fGetStampDuty (state);

		LMIPremium = LMIBase * LMIStampDuty;
	}
		
	if (debug == 1)
	{
		alert (loanAmount);
		alert (propertyValue);
		alert (state);
		alert (mortgageType);
		alert (loanType);
		alert (LVR);
		alert (LMIPremium);
	}
	
	return LMIPremium;
}

function fGetStampDuty (state)
{
	var LMIStampDuty = 0;
	
	if (state == "NSW")
	{
		LMIStampDuty = LMIStampDuty_NSW;
	}
	else if (state == "ACT")
	{
		LMIStampDuty = LMIStampDuty_ACT;
	}
	else if (state == "QLD")
	{
		LMIStampDuty = LMIStampDuty_QLD;
	}
	else if (state == "VIC")
	{
		LMIStampDuty = LMIStampDuty_VIC;
	}
	else if (state == "SA")
	{
		LMIStampDuty = LMIStampDuty_SA;
	}
	else if (state == "WA")
	{
		LMIStampDuty = LMIStampDuty_WA;
	}
	else if (state == "NT")
	{
		LMIStampDuty = LMIStampDuty_NT;
	}
	else if (state == "TAS")
	{
		LMIStampDuty = LMIStampDuty_TAS;
	}
	else
	{
		LMIStampDuty = 0.00;
	}
		
	return LMIStampDuty;
}