// JavaScript Document
/***************************
(c) 2008 www.TUFaT.com
All Rights Reserved. Please
do not re-sell/re-distribute
this software.
***************************/
function checkNumber(input, min, max, msg)
{
    msg = msg + " field has invalid data: " + input.value;
    var str = input.value;
    for (var i = 0; i < str.length; i++) {
        var ch = str.substring(i, i + 1)
        if ((ch < "0" || "9" < ch) && ch != '.') {
            alert(msg);
            return false;
        }
    }
    var num = 0 + str
    if (num < min || max < num) {
        alert(msg + " not in range [" + min + ".." + max + "]");
        return false;
    }
    input.value = str;
    return true;
}

function computeField(input)
{
    if (input.value != null && input.value.length != 0)
        input.value = "" + eval(input.value);
      computeForm(input.form);
}

function computeForm(form)
{
    if ((form.income.value == null || form.income.value.length == 0) ||
        (form.purchase_cost.value == null || form.purchase_cost.value.length == 0) ||
        (form.deposit.value == null || form.deposit.value.length == 0) ||
        (form.cpi.value == null || form.cpi.value.length == 0) ||
        (form.rental_rate.value == null || form.rental_rate.value.length == 0) ||
        (form.rental_growth.value == null || form.rental_growth.value.length == 0) ||
        (form.capital_growth.value == null || form.capital_growth.value.length == 0) ||
        (form.other_exp.value == null || form.other_exp.value.length == 0) ||
        (form.total_borrowed.value == null || form.total_borrowed.value.length == 0) ||
        (form.loan_interest.value == null || form.loan_interest.value.length == 0))
        {return;}
    if (!checkNumber(form.income, 0,9999999, "Your Income") ||
        !checkNumber(form.purchase_cost, 0,9999999, "Purchase Cost") ||
        !checkNumber(form.deposit, 0,9999999, "Deposit") ||
        !checkNumber(form.cpi, 1,15, "CPI Rate") ||
        !checkNumber(form.rental_rate, 0,9999999, "Rental Charged") ||
        !checkNumber(form.rental_growth, 1,15, "Rental Growth Rate") ||
        !checkNumber(form.capital_growth, 1,20, "Capital Growth Rate") ||
        !checkNumber(form.loan_interest, 1,100, "Loan Interest Rate"))
        {return;}
        var a1 = form.purchase_cost.value *1
        var a2 = form.deposit.value *1
        form.total_borrowed.value = parseInt ( a1 - a2 )
        <!-- calculate the payment -->
	var i = form.loan_interest.value;
	if (i > 1.0) {i = i / 100.0;}
	i /= 12;
    var pow = 1;
    for (var j = 0; j < (12*form.ly.value); j++)
        pow = pow * (1 + i);
    form.payment.value = parseInt((form.total_borrowed.value * pow * i) / (pow - 1))
<!-- done with payment calc -->
        var a9 = ( a1 - a2 )
        var b1=form.rental_rate.value;
        var b2 = form.rental_growth.value *1
        var b3 = form.cpi.value *1
		var b4 = form.loan_interest.value *1
        var b5 = form.capital_growth.value *1
        form.ri1.value = parseInt ( b1 * 12 )
        form.ri2.value = parseInt (( b1 * 12 ) * (1+(b2/100)))
        form.ri3.value = parseInt (( b1 * 12 ) * (1+(b2/100)) * (1+(b2/100)))
        form.ri4.value = parseInt (( b1 * 12 ) * (1+(b2/100)) * (1+(b2/100)) * (1+(b2/100)))
        form.ri5.value = parseInt (( b1 * 12 ) * (1+(b2/100)) * (1+(b2/100)) * (1+(b2/100)) * (1+(b2/100)))
        form.r1.value = form.hoi.value
        form.r2.value = parseInt (form.hoi.value * (1+(b3/100)))
        form.r3.value = parseInt (form.hoi.value * (1+(b3/100)) * (1+(b3/100)))
        form.r4.value = parseInt (form.hoi.value * (1+(b3/100)) * (1+(b3/100)) * (1+(b3/100)))
        form.r5.value = parseInt (form.hoi.value * (1+(b3/100)) * (1+(b3/100)) * (1+(b3/100)) * (1+(b3/100)))
        form.othe1.value = parseInt (form.other_exp.value * 12)
        form.othe2.value = parseInt ((form.other_exp.value * 12) * (1+(b3/100)))
        form.othe3.value = parseInt ((form.other_exp.value * 12) * (1+(b3/100)) * (1+(b3/100)))
        form.othe4.value = parseInt ((form.other_exp.value * 12) * (1+(b3/100)) * (1+(b3/100)) * (1+(b3/100)))
        form.othe5.value = parseInt ((form.other_exp.value * 12) * (1+(b3/100)) * (1+(b3/100)) * (1+(b3/100)) * (1+(b3/100)))
        form.fl1.value = form.yt.value
        form.fl2.value = parseInt (form.yt.value * (1+(b3/100)))
        form.fl3.value = parseInt (form.yt.value * (1+(b3/100)) * (1+(b3/100)))
        form.fl4.value = parseInt (form.yt.value * (1+(b3/100)) * (1+(b3/100)) * (1+(b3/100)))
        form.fl5.value = parseInt (form.yt.value * (1+(b3/100)) * (1+(b3/100)) * (1+(b3/100)) * (1+(b3/100)))
        <!-- hardcore the interest expense times number for rounding down each year - no amortization - est recalc principle -->
        form.ia1.value = parseInt ((a9 * ( b4 / 100 )) *1.0)
        form.prin1.value = parseInt ((form.payment.value *12)- (form.ia1.value*1))
        form.ia2.value = parseInt ((a9 * ( b4 / 100 )) *.985)
    	form.prin2.value = parseInt ((form.payment.value *12)- (form.ia2.value*1))
        form.ia3.value = parseInt ((a9 * ( b4 / 100 )) *.97)
        form.prin3.value = parseInt ((form.payment.value *12)- (form.ia3.value*1))
        form.ia4.value = parseInt ((a9 * ( b4 / 100 )) *.96)
        form.prin4.value = parseInt ((form.payment.value *12)- (form.ia4.value*1))
        form.ia5.value = parseInt ((a9 * ( b4 / 100 )) *.95)
        form.prin5.value = parseInt ((form.payment.value *12)- (form.ia5.value*1))
        form.to1.value = (form.r1.value *1) + (form.othe1.value * 1) + (form.fl1.value *1) + (form.ia1.value *1)
        form.to2.value = (form.r2.value *1) + (form.othe2.value * 1) + (form.fl2.value *1) + (form.ia2.value *1)
        form.to3.value = (form.r3.value *1) + (form.othe3.value * 1) + (form.fl3.value *1) + (form.ia3.value *1)
        form.to4.value = (form.r4.value *1) + (form.othe4.value * 1) + (form.fl4.value *1) + (form.ia4.value *1)
        form.to5.value = (form.r5.value *1) + (form.othe5.value * 1) + (form.fl5.value *1) + (form.ia5.value *1)
        form.rs1.value = (form.ri1.value *1)-(form.to1.value *1)
        form.rs2.value = (form.ri2.value *1)-(form.to2.value *1)
        form.rs3.value = (form.ri3.value *1)-(form.to3.value *1)
        form.rs4.value = (form.ri4.value *1)-(form.to4.value *1)
        form.rs5.value = (form.ri5.value *1)-(form.to5.value *1)
        <!-- straight line 27.5 year dep -->
        form.d1.value = parseInt (form.purchase_cost.value / 27.5)
        form.d2.value = parseInt (form.purchase_cost.value / 27.5)
        form.d3.value = parseInt (form.purchase_cost.value / 27.5)
        form.d4.value = parseInt (form.purchase_cost.value / 27.5)
        form.d5.value = parseInt (form.purchase_cost.value / 27.5)
        form.tc1.value = (form.d1.value *1) - (form.rs1.value *1)
        form.tc2.value = (form.d2.value *1) - (form.rs2.value *1)
        form.tc3.value = (form.d3.value *1) - (form.rs3.value *1)
        form.tc4.value = (form.d4.value *1) - (form.rs4.value *1)
        form.tc5.value = (form.d5.value *1) - (form.rs5.value *1)

        var c0=((form.income.value*1) * 1.017)
        if(c0<26250) {var tax0=(c0)*0.15}
        else if(c0<63550) {var tax0=((c0-26250)*0.28)+3937.50}
        else if(c0<132600) {var tax0=((c0-63550)*0.31)+14381.50}
        else if(c0<288351) {var tax0=((c0-132600)*0.36)+35787}
        else if(c0>288350) {var tax0=((c0-288351)*0.396)+91857};

        var c1=(((form.income.value*1) * 1.017) - (form.tc1.value *1))
        if(c1<26250) {var tax1=(c1)*0.15}
        else if(c1<63550) {var tax1=((c1-26250)*0.28)+3937.50}
        else if(c1<132600) {var tax1=((c1-63550)*0.31)+14381.50}
        else if(c1<288351) {var tax1=((c1-132600)*0.36)+35787}
        else if(c1<288350) {var tax1=((c1-288351)*0.396)+91857};

        var c2=(((form.income.value*1) * 1.017) - (form.tc2.value *1))
        if(c2<26250) {var tax2=(c2)*0.15}
        else if(c2<63550) {var tax2=((c2-26250)*0.28)+3937.50}
        else if(c2<132600) {var tax2=((c2-63550)*0.31)+14381.50}
        else if(c2<288351) {var tax2=((c2-132600)*0.36)+35787}
        else if(c2>288350) {var tax2=((c2-288351)*0.396)+91857};

        var c3=(((form.income.value*1) * 1.017) - (form.tc3.value *1))
        if(c3<26250) {var tax3=(c3)*0.15}
        else if(c3<63550) {var tax3=((c3-26250)*0.28)+3937.50}
        else if(c3<132600) {var tax3=((c3-63550)*0.31)+14381.50}
        else if(c3<288351) {var tax3=((c3-132600)*0.36)+35787}
        else if(c3>288350) {var tax3=((c3-288351)*0.396)+91857};

        var c4=(((form.income.value*1) * 1.017) - (form.tc4.value *1))
        if(c4<26250) {var tax4=(c4)*0.15}
        else if(c4<63550) {var tax4=((c4-26250)*0.28)+3937.50}
        else if(c4<132600) {var tax4=((c4-63550)*0.31)+14381.50}
        else if(c4<288351) {var tax4=((c4-132600)*0.36)+35787}
        else if(c4>288350) {var tax4=((c4-288351)*0.396)+91857};

        var c5=(((form.income.value*1) * 1.017) - (form.tc5.value *1))
        if(c5<26250) {var tax5=(c5)*0.15}
        else if(c5<63550) {var tax5=((c5-26250)*0.28)+3937.50}
        else if(c5<132600) {var tax5=((c5-63550)*0.31)+14381.50}
        else if(c5<288351) {var tax5=((c5-132600)*0.36)+35787}
        else if(c5>288350) {var tax5=((c5-288351)*0.396)+91857};

        form.tax1.value = parseInt (tax0 - tax1)
        form.tax2.value = parseInt (tax0 - tax2)
        form.tax3.value = parseInt (tax0 - tax3)
        form.tax4.value = parseInt (tax0 - tax4)
        form.tax5.value = parseInt (tax0 - tax5)
        form.at1.value = (form.tax1.value *1) + (form.ri1.value *1) - (form.to1.value *1)
        form.at2.value = (form.tax2.value *1) + (form.ri2.value *1) - (form.to2.value *1)
        form.at3.value = (form.tax3.value *1) + (form.ri3.value *1) - (form.to3.value *1)
        form.at4.value = (form.tax4.value *1) + (form.ri4.value *1) - (form.to4.value *1)
        form.at5.value = (form.tax5.value *1) + (form.ri5.value *1) - (form.to5.value *1)
        form.cg1.value = parseInt (a1 * (1+(b5/100)) )
        form.cg2.value = parseInt (a1 * (1+(b5/100)) * (1+(b5/100)) )
        form.cg3.value = parseInt (a1 * (1+(b5/100)) * (1+(b5/100)) * (1+(b5/100)) )
        form.cg4.value = parseInt (a1 * (1+(b5/100)) * (1+(b5/100)) * (1+(b5/100)) * (1+(b5/100)) )
        form.cg5.value = parseInt (a1 * (1+(b5/100)) * (1+(b5/100)) * (1+(b5/100)) * (1+(b5/100)) * (1+(b5/100)) )
        form.nw1.value = (form.at1.value *1) + ((form.cg1.value *1)-(form.purchase_cost.value *1) +(form.prin1.value*1))
        form.nw2.value = (form.at2.value *1) + ((form.cg2.value *1)-(form.cg1.value *1) +(form.prin2.value*1))
        form.nw3.value = (form.at3.value *1) + ((form.cg3.value *1)-(form.cg2.value *1) +(form.prin3.value*1))
        form.nw4.value = (form.at4.value *1) + ((form.cg4.value *1)-(form.cg3.value *1) +(form.prin4.value*1))
        form.nw5.value = (form.at5.value *1) + ((form.cg5.value *1)-(form.cg4.value *1) +(form.prin5.value*1))
}

function isWin(){
		if (navigator.appVersion.indexOf("Win") != -1)  return true;
		else return false;
}

function isIE(){
		if (navigator.appName == "Microsoft Internet Explorer") return true;
		else return false;
}

function isIEPC(){
		if (isWin() && isIE()) return true;
		else return false;
}

function popem(page,winname,width,height){
winname = window.open(page,winname,'width=' + width + ',height=' + height + ',toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=no');
		if (isIEPC() != 1) {
				winname.focus();
		}
}

function printThis()
        {
        if (window.print) { window.print() } else alert('To print this page press Ctrl-P on your keyboard.')
        }

function popUp(url) { sealWin = window.open
 (url, "win", 'toolbar=0, location=0, directories=0, status=1, menubar=1, scrollbars=1, resizable=1, width=550, height=450');
 self.name="mainWin";}
