<!--SCRIPT LANGUAGE="JAVASCRIPT"-->

function mtg_pmt(loanAmt,intRate,payPeriod,amortPeriod,compPeriod) {
        if (intRate < 0 || intRate > 100)
                intRate=10;
        if (loanAmt <= 0 )
                loanAmt=1000;
        if (amortPeriod <= 0)
                amortPeriod=1;

        intRate = intRate / 100;

        var top = 1 + (intRate / compPeriod);
        var factor = compPeriod / payPeriod;
        var EIRate = Math.pow(top,factor) - 1;

        var NPayP = amortPeriod * payPeriod;
        var PMT = (Math.pow((1+EIRate),NPayP)-1) / (EIRate*Math.pow(1+EIRate,NPayP));
        PMT = loanAmt/PMT;

        PMT = Math.round(PMT*100)/100;
        
        return PMT;
}

function balance_remaining(amt,rate, pmt, pmtsPerYr, cmpdsPerYr, pmtsToTerm) {
        var L = 0.0;
        var B = 0.0;
        var mtgBal = 0.0;
        var pmtsPerCmpd = 0;
  
        pmtsPerCmpd = pmtsPerYr / cmpdsPerYr;
        L = Math.pow(1 + (rate / (100 * cmpdsPerYr)),(1 / pmtsPerCmpd)) - 1;
        B = 1 - Math.pow(1 + (rate / (100 * cmpdsPerYr)), -(pmtsToTerm / pmtsPerCmpd) );
        B = (1 / L) * B;
        mtgBal = (amt - pmt * B) / (1 - L * B);
        mtgBal = Math.round(mtgBal*100)/100;
  
        return mtgBal;
}

function popup_value(theMenuObj) {
        return theMenuObj.options[theMenuObj.selectedIndex].value;
}

function requiredFieldsOK_mort1() {
        var fm = document.forms[0];
        if (str_Empty(fm.loanAmt1)||str_Empty(fm.intRate1)||str_Empty(fm.amortPeriod1)||popup_Empty(fm.payPeriod1)||popup_Empty(fm.compPeriod1))
                return false;
        
        return true;
}
function requiredFieldsOK_mort2() {
        var fm = document.forms[0];
        
        if ((str_Empty(fm.loanAmt2)||str_Empty(fm.intRate2)||str_Empty(fm.amortPeriod2)||popup_Empty(fm.payPeriod2)||popup_Empty(fm.compPeriod2)))
                return false;

        return true;
}

function do_calc1(doAlert) {
        clear_calculatedPmt("1");
        var pmt = 0;
        var pmtsToTerm = 0;
        var bal = 0;
        var fm = document.forms[0];
        if (requiredFieldsOK_mort1()) {
                if (fm.payPeriod1.selectedIndex == 3 || fm.payPeriod1.selectedIndex == 5) {
                        pmt = mtg_pmt(filterNum(fm.loanAmt1.value),fm.intRate1.value,12,fm.amortPeriod1.value,popup_value(fm.compPeriod1))
                        if (fm.payPeriod1.options[fm.payPeriod1.selectedIndex].value == "26")
                                pmt = pmt / 2;
                        if (fm.payPeriod1.options[fm.payPeriod1.selectedIndex].value == "52")
                                pmt = pmt / 4;
                
                        pmtsToTerm = parseFloat(popup_value(fm.term1)) * parseInt(popup_value(fm.payPeriod1));

                        bal = balance_remaining(filterNum(fm.loanAmt1.value),fm.intRate1.value,pmt,popup_value(fm.payPeriod1),popup_value(fm.compPeriod1),pmtsToTerm)
                } else {
                        pmt = mtg_pmt(filterNum(fm.loanAmt1.value),fm.intRate1.value,popup_value(fm.payPeriod1),fm.amortPeriod1.value,popup_value(fm.compPeriod1))
                        pmtsToTerm = parseFloat(popup_value(fm.term1)) * parseInt(popup_value(fm.payPeriod1));
                        bal = balance_remaining(filterNum(fm.loanAmt1.value),fm.intRate1.value,pmt,popup_value(fm.payPeriod1),popup_value(fm.compPeriod1),pmtsToTerm)
                }
                var mypmt = String(pmt);
                var mybal = String(bal);
                if (mypmt.indexOf(".") > 0) 
                        fm.calculatedPmt1.value = mypmt.substring(0, mypmt.indexOf(".") + 3);
                else 
                        fm.calculatedPmt1.value = mypmt;
                
                if (mybal.indexOf(".") > 0) 
                        fm.mtgBalance1.value = mybal.substring(0, mybal.indexOf(".") + 3);
                else 
                        fm.mtgBalance1.value = mybal;   
        }
        else if (doAlert) 
                alert("Please complete all fields for 'Mortgage 1' before calculating.");
}

function do_calc2(doAlert) {
        clear_calculatedPmt("2");
        var pmt = 0;
        var pmtsToTerm = 0;
        var bal = 0;
        var fm = document.forms[0];
        if (requiredFieldsOK_mort2()) {
                if (fm.payPeriod2.selectedIndex == 3 || fm.payPeriod2.selectedIndex == 5) {
                        pmt = mtg_pmt(filterNum(fm.loanAmt2.value),fm.intRate2.value,12,fm.amortPeriod2.value,popup_value(fm.compPeriod2))
                        if (fm.payPeriod2.options[fm.payPeriod2.selectedIndex].value == "26")
                                pmt = pmt / 2;
                        if (fm.payPeriod2.options[fm.payPeriod2.selectedIndex].value == "52")
                                pmt = pmt / 4;
                
                        pmtsToTerm = parseFloat(popup_value(fm.term2)) * parseInt(popup_value(fm.payPeriod2));

                        bal = balance_remaining(filterNum(fm.loanAmt2.value),fm.intRate2.value,pmt,popup_value(fm.payPeriod2),popup_value(fm.compPeriod2),pmtsToTerm)
                } else {
                        pmt = mtg_pmt(filterNum(fm.loanAmt2.value),fm.intRate2.value,popup_value(fm.payPeriod2),fm.amortPeriod2.value,popup_value(fm.compPeriod2))
                        pmtsToTerm = parseFloat(popup_value(fm.term2)) * parseInt(popup_value(fm.payPeriod2));
                        bal = balance_remaining(filterNum(fm.loanAmt2.value),fm.intRate2.value,pmt,popup_value(fm.payPeriod2),popup_value(fm.compPeriod2),pmtsToTerm)
                }
                var mypmt = String(pmt);
                var mybal = String(bal);
                if (mypmt.indexOf(".") > 0) 
                        fm.calculatedPmt2.value = mypmt.substring(0, mypmt.indexOf(".") + 3);
                else 
                        fm.calculatedPmt2.value = mypmt;
                
                if (mybal.indexOf(".") > 0) 
                        fm.mtgBalance2.value = mybal.substring(0, mybal.indexOf(".") + 3);
                else 
                        fm.mtgBalance2.value = mybal;
                        
        }
//      else if (doAlert) 
//              alert("Please complete all fields for 'Mortgage 2' before calculating, or check 'Ignore'.");
}


function clear_calculatedPmt (which) {
        var fm = document.forms[0];
        if (which == "2") {
                fm.calculatedPmt2.value = '';
                fm.mtgBalance2.value = '';
        } else {
                fm.calculatedPmt1.value = '';
                fm.mtgBalance1.value = '';
        }
}

function submit_for_amort () {
  if (requiredFieldsOK_mort1() && requiredFieldsOK_mort2() ) 
    document.forms[0].submit();
  else
    alert("Please complete all fields for 'Mortgage 1' \n or 'Mortgage 2' before requesting a Amortization Schedule.");
}

