function fixform() 
{ for(i=0; i<document.packagecalculator.length-1; i++)
	{ if(isNaN(document.packagecalculator.elements[i].value)) { document.packagecalculator.elements[i].focus(); alert("Please enter numbers only."); return false; } } return true; }
function calculate() { x=document.packagecalculator
		if(!fixform()) { return; }
		b3=6.7/100;  b4=48.5/100;  b5=1.942/1;  b6=20/100;  c11=x.b11.value/1;  b14 = x.b14.value/100; b15 = x.b15.value/1; if(b14>0) c14=c11*b14;  else c14=b15; b17 = x.b17.value/100; b18 = x.b18.value/1; if(b17>0) c17=c11*b17;  else c17=b18; c20=x.b20.value/1; b22=x.b22.value/1; b24=x.b24.value/1; if(x.parking[0].checked) { c22=(228*b24*b5*b4)+b22; } else { c22=b22; } 
		b26=x.b26.value/1; if((b26>0)&&(b26<55134)) c26=(b26*0.227+3924)+(b26*b5*b4*0.2); else { if(b26>=55134) c26=(b26*0.227+3924+(0.084*(b26-55134)))+b26*b5*b4*0.2; else c26=0; }
		b28=x.b28.value/1; if((b28>0)&&(b28<55134)) c28=(b28*0.227+3924)+(b28*b5*b4*0.2); else { if(b28>=55134) c28=(b28*0.227+3924+(0.084*(b28-55134)))+b28*b5*b4*0.2; else c28=0; }
		b30=x.b30.value/100; b31=x.b31.value/1; if(b30>0) c30=b30*c11*4/52; else c30=b31; b32=x.b32.value/1; b34=x.b34.value/100;	c32=b32*(b3-b34)*(1+b4*b5)
		b36=x.b36.value/1; b38=x.b38.value/100; c36=b36*(b3-b38)*b4*b5
		b40=x.b40.value/1; c40=(b40*b5*b4)+b40; c43=x.b43.value/1; b47=c11+c14+c17+c20+c22+c26+c28+c30+c32+c36+c40+c43; b49=x.b49.value/1; b51=b47+b49; x.b47.value=Math.round(b47); x.b51.value=Math.round(b51); }
function select_item(name, value) { this.name = name; this.value = value; }
function get_selection(select_object) { contents = new select_item(); for(var i=0;i<select_object.options.length;i++) { if(select_object.options[i].selected == true) { contents.name = select_object.options[i].text; contents.value = select_object.options[i].value; } } return contents; }
        
function stampduty(formfield) {
var amount = formfield.property.value; var amount2 = formfield.loanamount.value; var state = get_selection(formfield.state);
if ((amount == "") && (amount2 == ""))  { window.alert("You have not entered any values! Please enter in a loan amount and a property value."); } if (amount == "") { amount = 0; }
if (amount2 == "") { amount2 = 0; }
if ((amount % 100) != 0 ) { var mod = eval(100-(amount % 100)); amount = -(-amount - mod); }
if ((amount2 % 100) != 0 ) { var mod2 = eval(100-(amount2 % 100)); amount2 = -(-amount2- mod2); }

if (state.value == "VIC") { if ((amount > 0) && (amount <= 500000)) { var xferfee = (amount / 1000) * 2.46 + 90; } else { var xferfee = 1320; }
var exnotes = "Exemption for first home up to $100,000. Exemption phases out at a limit of $111,000. Eligible pensioner exemption to $65,000. Partial exemption between $55,000 and $65,000.";
        if (amount <= 20000) {
                var duty_property = eval(((amount)/100)*1.4);

        }
        else if ((amount > 20000) && (amount <= 115000)) {
                var duty_property = eval((((amount - 20000)/100)*2.4) + 280);
        }

        else if ((amount > 115000) && (amount <= 870000)) {
                var duty_property = eval((((amount - 115000)/100)*6) + 2560);
        }
        
        else {
                var duty_property = eval(((amount)/100)*5.5) ;
        }

        if (amount2 <= 10000) {
                var duty_loan = 4;
        }
        else {
                var duty_loan = eval((((amount2 - 10000)/200)*0.8) + 4);
        }
}

else if (state.value == "NSW") {
        var xferfee = 56;
        var exnotes = "First home buyer is subject to Stamp duty. Deferred Payments Scheme up to an amount of $155,000. Property transfers between spouses - married or de facto - exempt.";    

        if (amount <= 14000) {
                var duty_property = eval((amount/100)*1.25);
        }

        else if ((amount > 14000) && (amount <= 30000)) {
                var duty_property = eval((((amount - 14000)/100)*1.5) + 175);
        }

        else if ((amount > 30000) && (amount <= 80000)) {
                var duty_property = eval((((amount - 30000)/100)*1.75) + 415);
        }

        else if ((amount > 80000) && (amount <= 300000)) {
                var duty_property = eval((((amount - 80000)/100)*3.5) + 1290);
        }

        else if ((amount > 300000) && (amount <= 1000000)) {
                var duty_property = eval((((amount - 300000)/100)*4.5) + 8990);
        }
        
        else {
                var duty_property = eval((((amount - 1000000)/100)*5.5) + 40490);
        }
        
        if (amount2 <= 16000) {
                var duty_loan = 5;
        }
        else {
                var duty_loan = eval((((amount2 - 16000)/1000)*4) + 5);
        }
        
}

else if (state.value == "ACT") {
        var xferfee = 140;
        var duty_loan = 0;
        var exnotes = "First home buyer exempt up to $116,000 phasing out at $140,000"; 

        if (amount <= 80000) {
                var duty_property = eval((amount/100)*2);
        }

        else if ((amount > 80000) && (amount <= 10000)) {
                var duty_property = eval((((amount - 80000)/100)*3) + 1600);
        }

        else if ((amount > 100000) && (amount <= 250000)) {
                var duty_property = eval((((amount - 100000)/100)*4.15) + 2200);
        }

        else if ((amount > 250000) && (amount <= 500000)) {
                var duty_property = eval((((amount - 250000)/100)*5.15) + 8425);
        }        
              
        else {
                var duty_property = eval((((amount - 500000)/100)*5.5) + 21300);
        }
}

else if (state.value == "QLD") {
        if (amount <=180000) {
                var xferfee = 87;
        }
        else {
                var xferfee = eval((((amount - 180000)/10000)*20)+87);
        }
        var exnotes = "For first homes: Below $80,000 -  Nil; $80,001 to $150,000 - 1% Less $400; $150,001 to $155,000 - 1% less $200; $155,001 to $160,000 - 1% less $100. 1% on principal place of residence for first $250,000. $4 on gift from spouse to spouse of their principal residence to be owned equally.";

        if (amount <= 20000) {
                var duty_property = eval((amount/100)*1.5);
        }
        
        if ((amount > 20000) && (amount <= 50000)) {
                var duty_property = eval((((amount - 20000)/100)*2.25) + 300);
        }

		else if ((amount > 50000) && (amount <= 100000)) {
                var duty_property = eval((((amount - 50000)/100)*2.75) + 975);
        }
        
        else if ((amount > 100000) && (amount <= 250000)) {
                var duty_property = eval((((amount - 100000)/100)*3.25) + 2350);
        }
        
        else if ((amount > 250000) && (amount <= 500000)) {
                var duty_property = eval((((amount - 250000)/100)*3.5) + 7225);
        }

        else {
                var duty_property = eval((((amount - 500000)/100)*3.75) + 15975);
        }
        var duty_loan = eval(amount2 * 0.004);
}


else if (state.value == "SAN") {
        if (amount <= 5000)
        xferfee = 80;
    if (amount > 5000 && amount <= 20000)
        xferfee = 90;
    if (amount > 20000 && amount <= 40000)
        xferfee = 100;
    if (amount > 40000 && amount <= 50000)
        xferfee = 142;
    if (amount > 50000) {
        var xferfee = eval((((amount - 50000) / 10000)*45) + 142);
        }
        var exnotes = "First Home (Owner Occupied) - First $80,000 exempt. Partial exemption between $80,000 and $130,000";
        if (amount <= 12000) {
                var duty_property = eval((amount/100)*1);
        }

        else if ((amount > 12000) && (amount <= 30000)) {
                var duty_property = eval((((amount - 12000)/100)*2) + 120);
        }

        else if ((amount > 30000) && (amount <= 50000)) {
                var duty_property = eval((((amount - 30000)/100)*3) + 480);
        }

        else if ((amount > 50000) && (amount <= 100000)) {
                var duty_property = eval((((amount - 50000)/100)*3.5) + 1080);
        }

        else if ((amount > 100000) && (amount <= 200000)) {
                var duty_property = eval((((amount - 100000)/100)*4) + 2830);
        }
        
        else if ((amount > 200000) && (amount <= 250000)) {
                var duty_property = eval((((amount - 200000)/100)*4.25) + 6830);
        }
        
        else if ((amount > 250000) && (amount <= 300000)) {
                var duty_property = eval((((amount - 250000)/100)*4.75) + 8955);
        }
        
        else if ((amount > 300000) && (amount <= 500000)) {
                var duty_property = eval((((amount - 300000)/100)*5) + 11330); }
        
        else { var duty_property = eval((((amount - 500000)/100)*5.5) + 21330); }
        if (amount2 <= 400) { var duty_loan = 0; }
        else if ((amount2 > 400) && (amount2 <= 4000)) { var duty_loan = 10; }
        else if ((amount2 > 4000) && (amount2 <= 10000)) { var duty_loan = eval((((amount2 - 4000)/100)*0.25) + 10); }
        else { var duty_loan = eval((((amount2 - 10000)/100)*0.35) + 25); } }

else if (state.value == "TAS") { var xferfee = 130; var exnotes = "First home to $120,000. Interest free loan for any duty on purchase price and any xferfee repayable over 2 years.";
        if (amount <= 1300) { var duty_property = 20; }
        else if ((amount > 1300) && (amount <= 10000)) { var duty_property = eval((amount/100)*1.5); }
        else if ((amount > 10000) && (amount <= 30000)) { var duty_property = eval((((amount - 10000)/100)*2) + 150); }
        else if ((amount > 30000) && (amount <= 75000)) { var duty_property = eval((((amount - 30000)/100)*2.5) + 550); }
        else if ((amount > 75000) && (amount <= 150000)) { var duty_property = eval((((amount - 75000)/100)*3) + 1675); }
        else if ((amount > 150000) && (amount <= 225000)) { var duty_property = eval((((amount - 150000)/100)*3.5) + 3925); }
        else { var duty_property = eval((((amount - 225000)/100)*4) + 6550); }   
        if (amount2 <= 8000) { var duty_loan = 20; }
        else if ((amount2 > 8000) && (amount2 <= 10000)) { var duty_loan = eval((((amount2 - 8000)/100)*0.25) + 20); } 
		else { var duty_loan = eval((((amount2 - 10000)/100)*0.35) + 25); } }
		else if (state.value == "WAN") { if (amount <= 85000) {  var xferfee = 64; }
        else if ((amount > 85000) && (amount <= 120000)) { var xferfee = 74; }
        else if ((amount > 120000) && (amount <= 200000)) { var xferfee = 94; }
        else { var xferfee = eval((((amount - 200000)/100000)*20)+94); }
        var exnotes = "Transfer of property between spouses from single to joint ownership exempt. First home owner below 26 parallel to $85,000. Above 26 parallel to $127,500 rebate. Max. $500.";
        if (amount <= 80000) { var duty_property = eval((amount/100)*2.00); }
        else if ((amount > 80000) && (amount <= 100000)) { var duty_property = eval((((amount - 80000)/100)*3.00) + 1600); }
        else if ((amount > 100000) && (amount <= 250000)) { var duty_property = eval((((amount - 100000)/100)*4.15) + 2200); }
		else if ((amount > 250000) && (amount <= 500000)) { var duty_property = eval((((amount - 250000)/100)*5.15) + 8425); }
		else { var duty_property = eval((((amount - 500000)/100)*5.50) + 21300); }
        if (amount2 >= 35000) { var duty_loan = eval((amount2 - 35000) * 0.0025 - 87.50); }
		else { var duty_loan = eval(amount2 * 0.0025); } }
		else if (state.value == "NT") {
        var xferfee = 90;
        var duty_loan = 0;
        var exnotes = "First $80,000 of first home exempt. Property transfers between spouses - married or de facto - exempt where consideration is NIL.";
        if (amount <=500000) { newamount = eval(amount / 1000); var duty_property = eval((0.065 * Math.pow(newamount,2)) + (21 * newamount));  }
		else { var duty_property = eval(5.4 * (amount/100)); } }
		
total = eval(duty_property + duty_loan + xferfee);
total = Math.round(total);
duty_property = Math.round(duty_property);
duty_loan = Math.round(duty_loan);
xferfee = Math.round(xferfee);
formfield.duty_property.value = duty_property;
formfield.duty_loan.value = duty_loan;
formfield.xferfee.value = xferfee;
formfield.total.value = total;
//formfield.notes.value = exnotes;
document.all.Comment.innerText = exnotes;
}


function IncomeTotal()
{
	//total income calcs
	var a = new Number(document.frmBorrowLimits.urMonthlySalary.value);
	var b = new Number(document.frmBorrowLimits.partnersMonthlySalary.value);
	var c = new Number(document.frmBorrowLimits.InvestIncome.value);
	var d = a+b+c;
	document.all.TotalIncome.innerText = formatCurrency(d);

	//total expenses calcs
	var e = new Number(document.frmBorrowLimits.CarLoanPays.value);
	var f = new Number(document.frmBorrowLimits.CreditCardsPay.value);
	var g = new Number(document.frmBorrowLimits.Other.value);
	var h = e+f+g;
	document.all.TotalExpense.innerText = formatCurrency(h);

	var i = new Number(d);//document.all.TotalIncome.innerText);
	var j = new Number(h);//document.all.TotalExpense.innerText);
	var DSR = new Number(document.frmBorrowLimits.DSR.value);

	var k = (i-j)*(DSR/100);

	//max monthly repaymentS
	document.all.MaxMonthlyRepay.innerText = formatCurrency(k);

	var Rate = new Number(document.frmBorrowLimits.InterestRate.value);
	//number of payment periods
	var np = new Number(document.frmBorrowLimits.TermOfLoan.value);

	Rate = Rate/1200	
	np = np*12;

	pv = PV(Rate,np,k)
	
	document.all.MaxLoanAmt.innerText = pv
	document.frmBorrowLimits.ProposedLoanAmt.value = pv.toString().replace(/\$|\,/g,'');
	
	//Maximum propsed Value
	var LoanValueRatio = new Number(document.frmBorrowLimits.LVR.value);
	
	var PrpLoanAmt = document.frmBorrowLimits.ProposedLoanAmt.value;
	//PrpLoanAmt = PrpLoanAmt.toString().replace(/\$|\,/g,'');
	
	var MPV = PrpLoanAmt/(LoanValueRatio/100)
	
	document.all.MaxPropVal.innerText = formatCurrency(MPV);
	
	//Your deposit
	document.all.UrDeposit.innerText = formatCurrency(MPV-PrpLoanAmt);

}

function formatCurrency(num) { num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num)) { num = "0"; }
cents = Math.floor((num*100+0.5)%100); num = Math.floor((num*100+0.5)/100).toString();
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) { num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3)); }
return ('$' + num); }
function PV(Rate, np, pmt) { var q1;var q2; q1 = Math.pow(1+Rate,-np); q2 = Math.pow(1+Rate,np);
if(Rate == 0) { pv = (pmt*np); } else { pv = (q1*(Rate-pmt+q2*pmt))/Rate; }
return(formatCurrency(pv)); }



function calculatesavings(obj) {
a=obj.monthly.value; b=obj.interest.value; c=obj.term.value; f=Math.abs(obj.initial.value);
i=eval((b/100)/12); n=eval(c*12); v=eval(1/(1+i)); z=Math.pow((1+i),n); w=Math.pow(v,n);
totala=eval((f+a*((1-w)/i)*(1+i))*z); totalamt = eval(f+(a*n)); totalint = eval(totala - totalamt);
obj.totalone.value=rounding(totala); obj.totalamt.value=rounding(totalamt); obj.totalint.value=rounding(totalint); }
function rounding(n){ 
pennies = n * 100 ; pennies = Math.round(pennies) ; strPennies = "" + pennies ; len = strPennies.length ; return strPennies.substring(0, len - 2) + "." + strPennies.substring((len - 2), len); }
function computeyeld(obj) {
 	x=obj.Amount.value
	y=obj.Rate.value
	z = (obj.Term.value/12)
totalann = eval((x*(Math.pow(1 + ((y*.01)/1),z)))-x)
totalsemi = eval((x*(Math.pow(1 + ((y*.01)/2),(z*2))))-x)
totalqtr = eval((x*(Math.pow(1 + ((y*.01)/4),(z*4))))-x)
totalmth = eval((x*(Math.pow(1 + ((y*.01)/12),(z*12))))-x)
yieldann = eval((((x*(Math.pow(1 + ((y*.01)/1),1)))-x)/x)*100)
yieldsemi = eval((((x*(Math.pow(1 + ((y*.01)/2),(2))))-x)/x)*100)
yieldqtr = eval((((x*(Math.pow(1 + ((y*.01)/4),(4))))-x)/x)*100)
yieldmth = eval((((x*(Math.pow(1 + ((y*.01)/12),(12))))-x)/x)*100)
obj.TotalAnn.value = rounding(totalann)
obj.TotalSemi.value = rounding(totalsemi)
obj.TotalQtr.value = rounding(totalqtr)
obj.TotalMth.value = rounding(totalmth)
obj.YieldAnn.value = rounding(yieldann)
obj.YieldSemi.value = rounding(yieldsemi)
obj.YieldQtr.value = rounding(yieldqtr)
obj.YieldMth.value = rounding(yieldmth) }
function InitInf() { calcInf(document.infcalc) 
document.infcalc.amount.focus() }
function calcInf(form) { var str; var num;
var amount = form.amount.value
var rate = form.rate.options[form.rate.selectedIndex].value
var years = form.years.options[form.years.selectedIndex].value
var inf = amount * Math.pow((1.0 + rate/100), years)
inf = Math.round(inf)
str = Math.floor(inf/1000000);
str += inf%1000
form.result.value = str; }
function computeloan(obj) {
x=obj.LoanAmount.value
y=obj.Rate.value
z = obj.Term.value
if ((x=="") || (y=="") || (z=="")) { alert("You have not entered a value in all three fields"); } else { month = eval(pmt(x,(y*.01)/12,z*12)); fnight = eval(pmt(x,(y*.01)/26,z*26)); week = eval(pmt(x,(y*.01)/52,z*52)); obj.Monthly.value = rounding(month) } }
function pmt(ppl,rate,mths) { var x=eval((ppl*rate)/(eval(1-Math.pow(eval(rate+1),eval(0-mths))))) 
return x }
function nper(obj) { fv = 0; origterm = parseFloat(obj.Term.value); origpay = parseFloat(obj.Monthly.value); extpay = parseFloat(obj.Extra.value); pv = parseFloat(obj.LoanAmount.value); per = 12; pv = -pv; pay = origpay + extpay; rate = obj.Rate.value; if (obj.Extra.value == "") { alert("Please enter how much extra you would like to pay off your loan!"); } else { rate = eval((rate)/(per * 100)); if ( rate == 0 ) { nper_value = - (fv + pv)/pay; } else { nper_value = Math.log((-fv * rate + pay)/(pay+ rate * pv))/ Math.log(1 + rate); } new_nper =  rounding(nper_value); orig_total_amt = origpay * origterm * 12; new_total_amt = pay * new_nper; save = orig_total_amt - new_total_amt; obj.NewTerm.value = rounding(nper_value/12); obj.Saving.value = Math.round(save); } }

