<!--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;
  
  		if(mtgBal < 0)
			mtgBal = 0;
        return mtgBal;
}

function popup_value(theMenuObj) {
        return theMenuObj.options[theMenuObj.selectedIndex].value;
}

function requiredFieldsOK_mort1(fm) {
        //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(fm) {
        //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 requiredFieldsOK_mort3(fm) {
        //var fm = document.forms[0];
        
        if ((str_Empty(fm.loanAmt3)||str_Empty(fm.intRate3)||str_Empty(fm.amortPeriod3)||popup_Empty(fm.payPeriod3)||popup_Empty(fm.compPeriod3)))
                return false;

        return true;
}
function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}
function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}
function do_calc1_on_form(fm, doAlert) {
        clear_calculatedPmt(fm, "1");
        var pmt = 0;
        var pmtsToTerm = 0;
        var bal = 0;
		var newtotal= 0;
		//newtotal = fm.loanAmt1.value * 1.000;
        //var fm = document.forms[0];
        if (requiredFieldsOK_mort1(fm)) {
                if (fm.payPeriod1.selectedIndex == 3 || fm.payPeriod1.selectedIndex == 5) {
                        pmt = mtg_pmt((eval(filterNum(fm.loanAmt1.value) * 1.000)),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((eval(filterNum(fm.loanAmt1.value) * 1.000)),fm.intRate1.value,pmt,popup_value(fm.payPeriod1),popup_value(fm.compPeriod1),pmtsToTerm)
                } else {
                        pmt = mtg_pmt((eval(filterNum(fm.loanAmt1.value) * 1.000)),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((eval(filterNum(fm.loanAmt1.value) * 1.000)),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);
						fm.total_mortgage1_0.value =   round_decimals(eval(filterNum(fm.loanAmt1.value)), 2);
				}
				else {
                        fm.calculatedPmt1.value = mypmt;
						fm.total_mortgage1_0.value =   round_decimals(eval(filterNum(fm.loanAmt1.value)), 2);
					
				}		
                if (mybal.indexOf(".") > 0) 
                        fm.mtgBalance1.value = mybal.substring(0, mybal.indexOf(".") + 3);
                else 
                        fm.mtgBalance1.value = mybal;   
        }
        else if (doAlert) 
                alert(decodeMsg("COMPLETE_MORTGAGE_1"));
}

function do_calc2_on_form(fm, doAlert) {
        clear_calculatedPmt(fm, "2");
        var pmt = 0;
        var pmtsToTerm = 0;
        var bal = 0;
        // var fm = document.forms[0];
        if (requiredFieldsOK_mort2(fm)) {
                if (fm.payPeriod2.selectedIndex == 3 || fm.payPeriod2.selectedIndex == 5) {
                        pmt = mtg_pmt((eval(filterNum(fm.loanAmt2.value) * 1.000)),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((eval(filterNum(fm.loanAmt2.value) * 1.000)),fm.intRate2.value,pmt,popup_value(fm.payPeriod2),popup_value(fm.compPeriod2),pmtsToTerm)
                } else {
                        pmt = mtg_pmt((eval(filterNum(fm.loanAmt2.value) * 1.000)),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((eval(filterNum(fm.loanAmt2.value) * 1.000)),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_1.value = mypmt.substring(0, mypmt.indexOf(".") + 3);
						fm.total_mortgage2_0.value =   round_decimals(eval(filterNum(fm.loanAmt2.value)), 2);
			   }
			    else {
                        fm.calculatedPmt2_1.value = mypmt;
						fm.total_mortgage2_0.value =   round_decimals(eval(filterNum(fm.loanAmt2.value)), 2);
                
				}
                if (mybal.indexOf(".") > 0) 
                        fm.mtgBalance2_1.value = mybal.substring(0, mybal.indexOf(".") + 3);
                else 
                        fm.mtgBalance2_1.value = mybal;
                        
        }
}


/****************************************/
function do_calc3_on_form(fm, doAlert) {
        clear_calculatedPmt(fm, "2");
        var pmt = 0;
        var pmtsToTerm = 0;
        var bal = 0;
        // var fm = document.forms[0];
        if (requiredFieldsOK_mort3(fm)) {
                if (fm.payPeriod3.selectedIndex == 3 || fm.payPeriod3.selectedIndex == 5) {
                        pmt = mtg_pmt((eval(filterNum(fm.loanAmt3.value) * 1.000)),fm.intRate3.value,12,fm.amortPeriod3.value,popup_value(fm.compPeriod3))
                        if (fm.payPeriod3.options[fm.payPeriod3.selectedIndex].value == "26")
                                pmt = pmt / 2;
                        if (fm.payPeriod3.options[fm.payPeriod3.selectedIndex].value == "52")
                                pmt = pmt / 4;
                
                        pmtsToTerm = parseFloat(popup_value(fm.term3)) * parseInt(popup_value(fm.payPeriod3));

                        bal = balance_remaining((eval(filterNum(fm.loanAmt3.value) * 1.000)),fm.intRate3.value,pmt,popup_value(fm.payPeriod3),popup_value(fm.compPeriod3),pmtsToTerm)
                } else {
                        pmt = mtg_pmt((eval(filterNum(fm.loanAmt3.value) * 1.000)),fm.intRate3.value,popup_value(fm.payPeriod3),fm.amortPeriod3.value,popup_value(fm.compPeriod3))
                        pmtsToTerm = parseFloat(popup_value(fm.term3)) * parseInt(popup_value(fm.payPeriod3));
                        bal = balance_remaining((eval(filterNum(fm.loanAmt3.value) * 1.000)),fm.intRate3.value,pmt,popup_value(fm.payPeriod3),popup_value(fm.compPeriod3),pmtsToTerm)
                }
                var mypmt = String(pmt);
                var mybal = String(bal);
                if (mypmt.indexOf(".") > 0) {
                        fm.calculatedPmt3_1.value = mypmt.substring(0, mypmt.indexOf(".") + 3);
						fm.total_mortgage3_0.value =  round_decimals(eval(filterNum(fm.loanAmt3.value)), 2);
			   }
			    else {
                        fm.calculatedPmt3_1.value = mypmt;
						fm.total_mortgage3_0.value =  round_decimals(eval(filterNum(fm.loanAmt3.value)), 2);
                
				}
                if (mybal.indexOf(".") > 0) 
                        fm.mtgBalance3_1.value = mybal.substring(0, mybal.indexOf(".") + 3);
                else 
                        fm.mtgBalance3_1.value = mybal;
                        
        }
}

function clear_calculatedPmt (fm, which) {
        //var fm = document.forms[0];
        //if (which == "3") {
        //        fm.calculatedPmt2.value = '';
       //         fm.mtgBalance2.value = '';
        //} else {
        //        fm.calculatedPmt1.value = '';
        //        fm.mtgBalance1.value = '';
      //  }
}



