// ====================================== STAMP DUTY CALCULATOR START ================================================
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 StripChars(strIn) {
        var newstr = "";
        var Chars = "0123456789.";
        for (var i = 0; i < strIn.length; i++) {
                if (Chars.indexOf(strIn.charAt(i)) == -1) {}
                else {
                newstr = newstr + strIn.charAt(i);
                }
        }
        return newstr;
}

function stampduty(formfield) {
        var amount = StripChars(formfield.amount.value);
        var loanamount = StripChars(formfield.loanamount.value);
        var state = get_selection(formfield.state);
        var type = "OO"
        for (i=0; i< formfield.elements.length; i++) {
                if (formfield.elements[i].name == 'Type') {
                        for (s=0;s < 2; s++) {
                                if (formfield.Type[s].checked == true) {
                                        type = formfield.Type[s].value;
                                }
                        }
                }
        }
        if ((amount == "") && (loanamount == "")) {
                window.alert("You have not entered any values!");
                return;
        }
        if ((amount % 100) != 0 ) {
                        var mod = eval(100-(amount % 100));
                        amount = -(-amount - mod);
        }
        
        if ((loanamount % 100) != 0 ) {
                        var mod2 = eval(100-(loanamount % 100));
                        loanamount = -(-loanamount- mod2);
        }
//********************** Victoria *************************
        if (state.value == "VIC") {
                if ((amount > 0) && (amount <= 500000)) {
                        var transfer = (amount / 1000) * 2.46 + 90;
                }
                else {
                        var transfer = 1320;
                }
                mortgage = 59;
                var exnotes = "First Home Buyers with Families\n- Full stamp duty exemption for properties valued up to $115,000\n- Stamp duty concessions on a reducing scale for properties between $115,000 to $165,000\n- Have one or more dependent children and gross combined taxable income not exceeding $39,000 (1 child) or $40,000 (2 or more children)\n\nHome Buyers with Concession Cards\n- Full stamp duty exemption for properties valued up to $100,000\n- Stamp duty concessions on a reducing scale for properties between $100,000 to $130,000\n- Have a Pensioner Concession Card (Department of Social Security or Department of Veterans' Affairs), Veterans' Affairs Gold Card, Health Benefits Card, or Health Care Card.";
                if (amount <= 20000) {
                        var duty = eval(((amount)/100)*1.4);
        
                }
                else if ((amount > 20000) && (amount <= 115000)) {
                        var duty = eval((((amount - 20000)/100)*2.4) + 280);
                }
        
                else if ((amount > 115000) && (amount <= 870000)) {
                        var duty = eval((((amount - 115000)/100)*6) + 2560);
                }
                
                else {
                        var duty = eval(((amount)/100)*5.5) ;
                }
        
                if (loanamount <= 10000) {
                        var loanduty = 4;
                }
                else {
                        var loanduty = eval((((loanamount - 10000)/200)*0.8) + 4);
                }
        }
//********************** New South Wales *************************
        else if (state.value == "NSW") {
                var mortgage = 60;
                var transfer = 60;
                var exnotes = "First Home Plus Scheme\n- Full stamp duty exemption for properties valued up to $200,000 for metropolitan areas and $175,000 in country NSW\n- Stamp duty concessions on a reducing scale for properties between $200,000 and $300,000 in metropolitan areas\n- Stamp duty concessions on a reducing scale for properties between $175,000 and $200,000 in country NSW"; 
        
                if (amount <= 14000) {
                        var duty = eval((amount/100)*1.25);
                }
        
                else if ((amount > 14000) && (amount <= 30000)) {
                        var duty = eval((((amount - 14000)/100)*1.5) + 175);
                }
        
                else if ((amount > 30000) && (amount <= 80000)) {
                        var duty = eval((((amount - 30000)/100)*1.75) + 415);
                }
        
                else if ((amount > 80000) && (amount <= 300000)) {
                        var duty = eval((((amount - 80000)/100)*3.5) + 1290);
                }
        
                else if ((amount > 300000) && (amount <= 1000000)) {
                        var duty = eval((((amount - 300000)/100)*4.5) + 8990);
                }
                
                else {
                        var duty = eval((((amount - 1000000)/100)*5.5) + 40490);
                }
                
                if (loanamount <= 16000) {
                        var loanduty = 5;
                }
                else {
                        var loanduty = eval((((loanamount - 16000)/1000)*4) + 5);
                }
                
        }
//********************** ACT *************************
        else if (state.value == "ACT") {
                var mortgage = 76;
                var transfer = 151;
                var loanduty = 0;
                var exnotes = "Stamp Duty Relief Scheme\n- Full stamp duty exemption for properties valued up to $116,000\n- Stamp duty concessions on a reducing scale for properties between $116,000 and $140,000 in metropolitan areas\n- Property must be for principal residence and may be first home or re-entering home ownership (not held property for last two years)\n- Total household income must be less than $45,000"; 
        
                if (amount <= 14000) {
                        var duty = eval((amount/100)*1.25);
                        if (duty < 20) {
                                duty = 20;
                        }
                }
                else if ((amount > 14000) && (amount <= 30000)) {
                        var duty = eval((((amount - 14000)/100)*1.5) + 175);
                }
        
                else if ((amount > 30000) && (amount <= 60000)) {
                        var duty = eval((((amount - 30000)/100)*2) + 415);
                }
        
                else if ((amount > 60000) && (amount <= 100000)) {
                        var duty = eval((((amount - 60000)/100)*2.5) + 1015);
                }
                else if ((amount > 100000) && (amount <= 300000)) {
                        var duty = eval((((amount - 100000)/100)*3.5) + 2015);
                }
        
                else if ((amount > 300000) && (amount <= 1000000)) {
                        var duty = eval((((amount - 300000)/100)*4.5) + 9015);
                }
                
                else {
                        var duty = eval((((amount - 1000000)/100)*5.5) + 40515);
                }
        }
//********************** Queensland *************************
        else if (state.value == "QLD") {
                if (amount <=180000) {
                        var transfer = 88;
                }
                else {
                        var transfer = eval((((amount - 180000)/10000)*20)+88);
                }
                var mortgage = 91;
                
                if (type == 'OO') {
                        var exnotes = "Owner Occupied Concession:\nThese rates are applicable only where property is not being gifted or transferred between spouses. In these cases, investment rates are applicable.\n\nFirst Home Buyer Rebate:\nFull stamp duty exemption for properties valued up to $80,000\n- Stamp duty concessions on a reducing scale for properties between $80,000 and $160,000 in metropolitan areas\n- Property must be first home and intended as principal residence";
                
                        if (amount <= 250000) {
                                var duty = eval((amount/100)*1);
                        }
                        else if ((amount > 250000) && (amount <= 500000)) {
                                var duty = eval((((amount - 250000)/100)*3.5) + 2500);
                        }
                        else {
                                var duty = eval((((amount - 500000)/100)*3.75) + 11250);
                        }
                        if(loanamount > 70000){
                                loanduty = (loanamount - 70000) * 0.004;
                        }
                        else{
                                loanduty = 0;
                        }
                }
                else {
                        var exnotes = "";
                        if (amount <= 20000) {
                                var duty = eval((amount/100)*1.5);
                        }
                        else if ((amount > 20000) && (amount <= 50000)) {
                                var duty = eval((((amount - 20000)/100)*2.25) + 300);
                        }
                        else if ((amount > 50000) && (amount <= 100000)) {
                                var duty = eval((((amount - 50000)/100)*2.75) + 975);
                        }
                        else if ((amount > 100000) && (amount <= 250000)) {
                                var duty = eval((((amount - 100000)/100)*3.25) +2350);
                        }
                        else if ((amount > 250000) && (amount <= 500000)) {
                                var duty = eval((((amount - 250000)/100)*3.5) + 7225);
                        }
                        else {
                                var duty = eval((((amount - 500000)/100)*3.75) + 15975);
                        }
                        loanduty = loanamount * 0.004
                }
                
        }
//********************** South Australia *************************
        else if (state.value == "SAN") {
                if (amount <= 5000)
                        transfer = 87;
                if (amount > 5000 && amount <= 20000)
                        transfer = 97;
                if (amount > 20000 && amount <= 40000)
                        transfer = 108;
                if (amount > 40000 && amount <= 50000)
                        transfer = 155;
                if (amount > 50000) {
                        var transfer = eval((((amount - 50000)/10000)*48) + 155);
                }
                mortgage = 91;
                var exnotes = "First Home Buyers Concession\n- Full stamp duty exemption for properties valued up to $100,000\n- Stamp duty concessions on a reducing scale for properties between $100,000 and $150,000 in metropolitan areas\n- Property must be first home and intended as principal residence";
                if (amount <= 12000) {
                        var duty = eval((amount/100)*1);
                }
        
                else if ((amount > 12000) && (amount <= 30000)) {
                        var duty = eval((((amount - 12000)/100)*2) + 120);
                }
        
                else if ((amount > 30000) && (amount <= 50000)) {
                        var duty = eval((((amount - 30000)/100)*3) + 480);
                }
        
                else if ((amount > 50000) && (amount <= 100000)) {
                        var duty = eval((((amount - 50000)/100)*3.5) + 1080);
                }
        
                else if ((amount > 100000) && (amount <= 500000)) {
                        var duty = eval((((amount - 100000)/100)*4) + 2830);
                }
                else if ((amount > 500000) && (amount <= 1000000)) {
                        var duty = eval((((amount - 100000)/100)*4.5) + 18830);
                }
                else {
                        var duty = eval((((amount - 1000000)/100)*5) + 41330);
                }
                
                if (loanamount <= 400) {
                        var loanduty = 0;
                }
                else if ((loanamount > 400) && (loanamount <= 4000)) {
                        var loanduty = 10;
                }
                else if ((loanamount > 4000) && (loanamount <= 10000)) {
                        var loanduty = eval((((loanamount - 4000)/100)*0.25) + 10);
                }
                else {
                        var loanduty = eval((((loanamount - 10000)/100)*0.35) + 25);
                }
        }
//********************** Tasmania *************************
        else if (state.value == "TAS") {
                var mortgage = 85.50;
                var transfer = 130;
                var exnotes = "Stamp Duty Concession\n- Interest free loan of total stamp duty payable over a span of two years\n- Applicable to properties valued up to $120,000\n- Property must be first home and intended as principal residence";
                if (amount <= 1300) {
                        var duty = 20;
                }
                else if ((amount > 1300) && (amount <= 10000)) {
                        var duty = eval((amount/100)*1.5);
                }
        
                else if ((amount > 10000) && (amount <= 30000)) {
                        var duty = eval((((amount - 10000)/100)*2) + 150);
                }
        
                else if ((amount > 30000) && (amount <= 75000)) {
                        var duty = eval((((amount - 30000)/100)*2.5) + 550);
                }
        
                else if ((amount > 75000) && (amount <= 150000)) {
                        var duty = eval((((amount - 75000)/100)*3) + 1675);
                }
        
                else if ((amount > 150000) && (amount <= 225000)) {
                        var duty = eval((((amount - 150000)/100)*3.5) + 3925);
                }
                
                else {
                        var duty = eval((((amount - 225000)/100)*4) + 6550);
                }
                
                if (loanamount <= 8000) {
                        var loanduty = 20;
                }
                else if ((loanamount > 8000) && (loanamount <= 10000)) {
                        var loanduty = eval((((loanamount - 8000)/100)*0.25) + 20);
                }
                else {
                        var loanduty = eval((((loanamount - 10000)/100)*0.35) + 25);
                }
        }
//********************** Western Australia *************************
        else if (state.value == "WAN") {
                if (amount <= 85000) {
                        var transfer = 75;
                }
                else if ((amount > 85000) && (amount <= 120000)) {
                        var transfer = 85;
                }
                else if ((amount > 120000) && (amount <= 200000)) {
                        var transfer = 105;
                }
                else {
                        tempProperty = (amount - 200000) / 100000;
                        for (var i=0, k=1; tempProperty >i && tempProperty > k ; i++, k++);
                        var transfer = (k * 20) + 105;
                        //var mortgage = eval((((amount - 200000)/100000)*20)+105);
                }
                mortgage = 75;
                var exnotes = "Concessional Rate\n-Applies to purchase of principal place of residence or business undertaking with a dutiable value not exceeding $135,000.\nThe concessional rate is $1.50 per $100 (or part thereof) up to $100,000 and $5.50 per $100 (or part thereof) for that amount between $100,000 and $135,000.\nApplication forms are available on request. Full details are contained in section 75AE of the Stamp Act 1921.\n\nFirst Home Owners Rebate\n- Rebate of $500 or amount of duty payable, whichever is lesser\n- Vacant land valued up to, $52,000 and property (house and land) up to $135,000\n- Property must be first home and intended as principal residence.\nFull details are contained in section 75AG of the Stamp Act 1921.";
                if (type == 'OO' && amount <= 135000) {
                        if (amount <= 100000) { 
                                var duty = eval((amount/100)*1.5);
                        }
                        else {
                                var duty = eval((((amount - 100000)/100)*5.5) + 1500);
                        }
                }
                else {
                        if (amount <= 80000) {  
                                var duty = eval((amount/100)*1.95);
                        }
                
                        else if ((amount > 80000) && (amount <= 100000)) {
                                var duty = eval((((amount - 80000)/100)*2.85) + 1560);
                        }
                
                        else if ((amount > 100000) && (amount <= 250000)) {
                                var duty = eval((((amount - 100000)/100)*3.7) + 2130);
                        }
                
                        else if ((amount > 250000) && (amount <= 500000)) {
                                var duty = eval((((amount - 250000)/100)*4.55) + 7680);
                        }
                
                        else {
                                var duty = eval((((amount - 500000)/100)*4.85) + 19055);
                        }
                }
                if (loanamount <= 35000) {
                        var loanduty = eval(loanamount * 0.0025);
                }
                else {
                        var loanduty = eval((((loanamount - 35000)/100)*0.4) + 87.5);
                }
                
        }
//********************** Northern Territory  *************************
        else if (state.value == "NT") {
                var mortgage = 90;
                var transfer = 90;
                var loanduty = 0;
                var exnotes = "First Home Rebate Scheme\n- A rebate of up to $2780 off the total stamp duty payable\n- Property must be first home and intended as principal residence";
        
                if (amount <=500000) {
                        newamount = eval(amount / 1000);
                        var duty = eval((0.065 * Math.pow(newamount,2)) + (21 * newamount));
                }
        
                else {
                        var duty = eval(5.4 * (amount/100));
                }
        }
        total = eval(duty + loanduty + mortgage + transfer);
        total = Math.round(total);
        duty = Math.round(duty);
        loanduty = Math.round(loanduty);
        mortgage = Math.round(mortgage);
        transfer = Math.round(transfer);
        
        formfield.duty.value = duty;
        formfield.loanduty.value = loanduty;
        formfield.mortgage.value = mortgage;
        formfield.transfer.value = transfer;
        formfield.total.value = total;
        formfield.notes.value = exnotes;
}

// =====================================STAMP DUTY CALCULATOR END =================================================



// =====================================EARLY PAYMENTS CALCULATOR START =================================================
function compute(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 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 ppmt(amt,term,subterm,rate) {
        // Term sent as number of payments
        parseFloat(amt);
        parseFloat(term);
        parseFloat(subterm);
        parseFloat(rate);
        if (rate > 1) {
                rate = rate * 0.01;
        }
        Monthly = pmt(amt, rate/12, term);
        MthInterest = rate/12;
        principal = amt;
        for(i=1; i<= subterm; i++) {
                intPay = principal * MthInterest;
                priPay = Monthly - intPay;
                principal -= priPay;
        }
        return(principal);
}

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;
        rate = obj.Rate.value;
        subterm = obj.Subterm.value * 12;
        alreadyPaid = origpay * subterm;
        term = origterm * 12;
        pv2 = -(ppmt(-pv,origterm * 12,subterm,rate)) + extpay;
        //alert("Present value: " + pv + " Principal Left:" + pv2);

        
   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 )    // Interest rate is 0
   {
         nper_value = - (fv + pv2)/origpay; 
   }
   else 
   {
         nper_value = Math.log((-fv * rate + origpay)/(origpay+ rate * pv2))/ Math.log(1 + rate);
   }
        new_nper =  rounding(nper_value);
        orig_total_amt = origpay * origterm * 12;
        new_total_amt = (origpay * subterm) + (origpay * new_nper);
        save = orig_total_amt - new_total_amt;
        obj.NewTerm.value = rounding((nper_value + subterm)/12);
        obj.Saving.value = Math.round(save);
   }
}
// =====================================EARLY PAYMENTS CALCULATOR END =================================================