var w;
var LTV_MIN = 0;
var LTV_MID = 80;
var LTV_MAX = 90;  // WWW0021, change Max LTV from 95% to 90%
var LOAN_MIN = 200000;
var LOAN_MID = 5000000;
//var LOAN_MAX = 20000000;      // WWW0017
var LOAN_MAX = 5400000;         // WWW0017, WWW0021, WWW0022, WWW0024
var LOAN_MAX_LTV90 = 5400000;   // WWW0017, WWW0021, WWW0022, WWW0024
var LOAN_MAX_LTV95 = 6000000;   // WWW0017
var LOAN_MAX_NOO = 5400000;     // WWW0017, WWW0022, WWW0024
var YEAR_MIN = 5;
var YEAR_MAX = 40;
var YEAR_MAX_LTV95 = 30;      // 2008/12/29, EN
var PV_MAX = 6000000;         // WWW0022, WWW0024

var boolValidate = false;
var lastInputValue;
var ChangeSelectBoxValue = '';

/* error messages */
var MSG_INVALID_PV = "Please enter property value.";
var MSG_INVALID_LOANAMOUNT = "The calculated loan amount should be within the range from HK$200,000 to HK$5,400,000.\n\nPlease enter again.";  // WWW0017, WWW0021, WWW0022, WWW0024
var MSG_INVALID_LTV = "The LTV ratio should be within the range from 1% to 90%.\n\nPlease enter again.";  // WWW0021
var MSG_INVALID_AMRATE = "The interest rate should be within the range from 0.001% to 60.000% per annum.\n\nPlease enter again.";
var MSG_INVALID_YEAR = "The fixed rate period should be within the range from 1 month to 60 months.\n\nPlease enter again.";
var MSG_NO_AMRATE = "Please enter fixed interest rate.";
var MSG_MITYPE_GT70 = "Please select 'Premium Payment Option' as LTV>70%.";
var MSG_MITYPE_LT70 = "No need to select 'Premium Payment Option' as LTV<=70%.";
var MSG_MITYPE_LT75_AN = "Annual Premium Option is not offered for LTV<=75% applications."; // LTV75
var MSG_MITYPE_NOO_AN = "Annual Premium Option is not offered for Non Owner-occupied Residential Properties."; // NOO
var MSG_MITYPE_NOO_LTV = "LTV ratio must not exceed 85% for Non Owner-occupied Residential Properties."; // NOO
var MSG_MITYPE_NOO_LOANAMT = "Loan amount must not exceed HK$5,4000,000."; // NOO, WWW0022, WWW0024
var MSG_INVALID_LOANAMT_LTV90 = "For 90% LTV ratio, the maximum loan amount is HK$5,400,000.";  // WWW0017, WWW0021, WWW0022, WWW0024
var MSG_INVALID_LOANAMT_LTV95 = "For 95% LTV ratio, the maximum loan amount is HK$6,000,000.";  // 2008/12/29, EN, WWW0017
var MSG_INVALID_TENOR_LTV95 = "For 95% LTV ratio, the maximum loan tenor is 30 years.";         // 2008/12/29, EN
var MSG_INVALID_PV_MAX = "Property value must not exceed HK$6,000,000.";         // WWW0022, WWW0024

function ChangeSelectBox(o, e) {
    var k = window.event ? e.keyCode: e.which;
    if (k < 48 || k > 57) return;
    ChangeSelectBoxValue += String.fromCharCode(k);
    if (ChangeSelectBoxValue.length > 2) ChangeSelectBoxValue = String.fromCharCode(k);
    if (ChangeSelectBoxValue != '') o.value = ChangeSelectBoxValue;
}

function originalChangeSelectBoxNS(obj) {
    // Accumulate digit
    var k = window.event ? event.keyCode: event.which;
    ChangeSelectBoxValue += String.fromCharCode(k);

    // Reset the digit
    if(ChangeSelectBoxValue.length == 3){
      ChangeSelectBoxValue = String.fromCharCode(k);
    }

    // Accept range
    if(ChangeSelectBoxValue < YEAR_MIN || ChangeSelectBoxValue > YEAR_MAX){
        //ChangeSelectBoxValue = '';
        return;
    }

    // Handle the second digit
    if(ChangeSelectBoxValue.length == 2){
      obj.value = ChangeSelectBoxValue;
    }

    // Handle the first digit
    if(ChangeSelectBoxValue.length == 1){
      obj.value = ChangeSelectBoxValue;
    }

}

function getFocus(obj){
  lastInputValue = obj.value;
}

function toggleField(name, bool) {
    var e = MM_findObj(name, document);
    if (!e) return;
    e.style.display = (bool ? '': 'none');
}

function Checkfarm_y1(f){
    if(f.value=='')return;
    if(f.value <= 0 || f.value > 60) {
        if(f.value <= 0){
            alert(MSG_INVALID_YEAR);
        }
        if(f.value > 60){
            alert(MSG_INVALID_YEAR);
        }
        f.value = '';
        setTextboxStyle('farm_y1', '');
        f.focus();
        return;
    }else{
        if(f.value <= 12){
            f.value = 12;
        }else if(f.value <= 24){
            f.value = 24;
        }else if(f.value <= 36){
            f.value = 36;
        }else if(f.value <= 48){
            f.value = 48;
        }else if(f.value <= 60){
            f.value = 60;
        }
        setTextboxStyle('farm_y1', '#ffffff');
    }
}

function checkmortgagetype() {
    var f = document.forms['calc'];
    var e = document.forms['calc'].elements['mtype'];
    if (parseInt(e.options[e.selectedIndex].value) == 1) {
        if (!document.layers) {
            toggleField('float_rate1', false);
            toggleField('float_rate2', false);
            toggleField('farm_rate1', true);
            toggleField('farm_rate2', true);
            f.mitype.options[3] = new Option('Annual (Non-financed)', '2');  // WWW0017, WWW0021
        }
    } else {
        if (!document.layers) {
            toggleField('float_rate1', true);
            toggleField('float_rate2', true);
            toggleField('farm_rate1', false);
            toggleField('farm_rate2', false);
            f.mitype.options[3] = new Option('Annual (Non-financed)', '2');  // WWW0017
        }
    }
}

function reopen(name, features) {
    if (w.name != name) {
        w = window.open('', name, 'width=1,height=1,top=0,left=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');
        w.close();
        w = window.open('', name, features);
    }
    if (!w) return null;
    if (!w.opener) w.opener = self;
    if (window.focus) w.focus();
    w.document.open('text/html');
    return w;
}

function pparseInt(v) {
    return parseInt(str_replace(',', '', v));
}

function pparseFloat(v) {
    return parseFloat(str_replace(',', '', v));
}

function isInt(e, minv, maxv) {
    if (e.value == '') return;
    var v = pparseInt(e.value);
    if (isNaN(v)) {
        return false;
    } else if (v < minv) {
        return false;
    } else if (v > maxv) {
        return false;
    } else {
        e.value = v;
        return true;
    }
}


function isDouble(e, minv, maxv) {
    if (e.value == '') return;
    var v = pparseFloat(e.value);
    if (isNaN(v)) {
        alert('Please provide a valid amount.');
        return false;
    } else if (v < minv) {
        return false;
    } else if (v > maxv) {
        return false;
    } else {
        e.value = v;
        return true;
    }
}

function changePropertyValue(f) {
    if (lastInputValue == f.pptyvalue.value) return;
    var ltv = f.ltv;
    var pv = f.pptyvalue;
    var la = f.loanamount;

    if (pv.value == '') return;

    // WWW0022 start
    if(getNumeric(pv.value) > PV_MAX) {
        alert(MSG_INVALID_PV_MAX);
        pv.value = '';
        la.value = '';
        pv.focus();
        return false;
    }
    // WWW0022 end

    if (getNumeric(pv.value) < LOAN_MIN || getNumeric(pv.value) > 999999999) {
        alert(MSG_INVALID_LOANAMOUNT);
        pv.value = '';
        pv.focus();
        return false;  // WWW0021
    }

    setTextboxStyle('pptyvalue','#ffffff');
    result = (getNumeric(pv.value) * (ltv.value)/ 100) ;

    if (ltv.value >0){
        if ( result == 0 ) return;
        if ( result >= LOAN_MIN && result <= LOAN_MAX){
            f.loanamount.value = result;
            NumObjRoundUp(f.loanamount,0);
            currencyFormat(f.loanamount);
            return;
        } else {
            // Error msg
            if (result < LOAN_MIN) {
                alert(MSG_INVALID_LOANAMOUNT);
                setTextboxStyle('pptyvalue', '');
                f.pptyvalue.value = '';
                f.loanamount.value = '';   // WWW0017
                return false;              // WWW0021
            } else if (result > LOAN_MAX) {   // WWW0017
                if (Math.floor(getNumeric(pv.value) * LTV_MAX / 100) > LOAN_MAX) {   // WWW0017, WWW0021
                    alert(MSG_INVALID_LOANAMOUNT);
                    f.loanamount.value = '';
                    return false;          // WWW0021
                } else {
                    f.ltv.value = LTV_MAX;  // WWW0021
                    f.loanamount.value = getNumeric(pv.value) * LTV_MAX / 100;  // WWW0021
                    NumObjRoundUp(f.loanamount,0);  // WWW0021
                    currencyFormat(f.loanamount);   // WWW0021
                }
            }
            return;
        }
    }
}

function changeLTV(f){
    if(lastInputValue==f.ltv.value) return;
    var ltv = f.ltv;
    var pv = f.pptyvalue;
    var la = f.loanamount;
    var result = 0;

    if (ltv.value == '') return;

    if (pv.value == '') {
        alert(MSG_INVALID_PV);
        ltv.value = '';
        return false;
    }

    ltv = parseFloat(ltv.value);
    if (isNaN(ltv) || ltv <= LTV_MIN) {
        alert(MSG_INVALID_LTV);
        f.ltv.value = '';
        setTextboxStyle('ltv', '');
        return false;        // WWW0021
    }
    if (ltv > LTV_MAX) {
        alert(MSG_INVALID_LTV);
        f.ltv.value = '';
        setTextboxStyle('ltv', '');
        return false;        // WWW0021
    }

    // 2008/12/29, EN
    if (ltv > 90) {
        changeTenorOption(f, YEAR_MIN, YEAR_MAX_LTV95);
    } else {
        changeTenorOption(f, YEAR_MIN, YEAR_MAX);
    }
    // End 2008/12/29

    result = (getNumeric(pv.value) * ltv / 100) ;

    if ( result >= LOAN_MIN && result <= LOAN_MAX){

        f.loanamount.value = result;
        NumObjRoundUp(f.loanamount,0);
        currencyFormat(f.loanamount);
//      f.ltv.value = NumRoundUp(f.ltv.value,2);
        setTextboxStyle('ltv', '#ffffff');

    } else {
        if (result < LOAN_MIN) {
            alert(MSG_INVALID_LOANAMOUNT);
            f.ltv.value = '';                  // WWW0021
            f.loanamount.value = '';           // WWW0021
            setTextboxStyle('ltv', '');
            return false;                      // WWW0021
        } else if (result > LOAN_MAX) {   // WWW0017
            if (Math.floor(getNumeric(pv.value) * LTV_MAX / 100) > LOAN_MAX) {   // WWW0017, WWW0021
                alert(MSG_INVALID_LOANAMOUNT);
                f.ltv.value = '';                   // WWW0021
                f.loanamount.value = '';            // WWW0021
                setTextboxStyle('ltv', '');         // WWW0021
                return false;                       // WWW0021
            } else {
                ltv.value = LTV_MAX;  // WWW0021
                la.value = Math.floor(getNumeric(pv.value) * LTV_MAX / 100);  // WWW0021
            }
        }
        return;
    }
}

// 2008/12/29, EN
function changeTenorOption(f, rangeFrom, rangeTo) {
    var length = f.year.length;
    var selected = false;
    var selectedValue = f.year.options[f.year.selectedIndex].value;
    for (j = length - 1; j >= 0; j--) {
        f.year.options[j] = null;
    }
    var index = 0;
    for (i = rangeFrom; i <= rangeTo; i++) {
        f.year.options[index++]= new Option(i, i);
        if (i==selectedValue) {
            f.year.options[index-1].selected = true;
            selected = true
        }
    }
    if (!selected) {
        f.year.options[index-1].selected = true;
    }
}
// End 2008/12/29

function change_Floating_InterestRate(f) {
    if(f.value == '')return;
    if(f.value <= 0 || f.value > 60){
        if (f.value == 0) {
            alert(MSG_INVALID_AMRATE);
        } else if (f.value <= 0) {
            alert(MSG_INVALID_AMRATE);
        } else if (f.value > 60){
            alert(MSG_INVALID_AMRATE);
        }
        f.value = '';
        setTextboxStyle('amrate','');
        return;
    }else{
        setTextboxStyle('amrate','#ffffff');
        NumObjRoundUp(f,3);
    }
}

function change_Fixed_InterestRate_r1(f) {
    if(f.value == '')return;
    if(f.value <= 0 || f.value > 60){
        if(f.value == 0){
            alert(MSG_INVALID_AMRATE);
        }else if(f.value <= 0){
            alert(MSG_INVALID_AMRATE);
        }else if(f.value > 60){
            alert(MSG_INVALID_AMRATE);
        }
        f.value = '';
        setTextboxStyle('farm_r1','');
        return;
    }else{
        setTextboxStyle('farm_r1','#ffffff');
        NumObjRoundUp(f,3);
    }
}

function change_Fixed_InterestRate_r2(f) {
    if(f.value == '')return;
    if(f.value <= 0 || f.value > 60){
        if(f.value == 0){
            alert(MSG_INVALID_AMRATE);
        }else if(f.value <= 0){
            alert(MSG_INVALID_AMRATE);
        }else if(f.value > 60){
            alert(MSG_INVALID_AMRATE);
        }
        f.value = '';
        setTextboxStyle('farm_r2','');
        return;
    }else{
        setTextboxStyle('farm_r2','#ffffff');
        NumObjRoundUp(f,3);
    }

}

function changeLoanAmount(f) {
    if (lastInputValue == f.loanamount.value) return;
    var ltv = f.ltv;
    var pv = f.pptyvalue;
    var la = f.loanamount;

    //
    if (la.value == '') return;

    //
    if (pv.value == ''){
        la.value = '';
        alert(MSG_INVALID_PV);
        return false;
    // WWW0022 start
    }else if(getNumeric(pv.value) > PV_MAX) {
        pv.value = '';
        la.value = '';
        alert(MSG_INVALID_PV_MAX);
        return false;
    // WWW0022 end
    }

    if (getNumeric(la.value) < LOAN_MIN) {
        alert(MSG_INVALID_LOANAMOUNT);
        la.value = '';
        setTextboxStyle('loanamount', '');
        return false;   // WWW0021
    } else if (getNumeric(la.value) > LOAN_MAX) {   // WWW0017
        if (Math.floor(getNumeric(pv.value) * LTV_MAX / 100) > LOAN_MAX) {   // WWW0017, WWW0021
            alert(MSG_INVALID_LOANAMOUNT);
            la.value = '';
            setTextboxStyle('loanamount', '');  // WWW0021
            return false;                       // WWW0021
        } else {
            ltv.value = LTV_MAX;  // WWW0021
            la.value = Math.floor(getNumeric(pv.value) * LTV_MAX / 100);  // WWW0021
        }
        currencyFormat(la);
        return;
    // WWW0021, start
    } else {
        if (getNumeric(la.value) > getNumeric(pv.value) * LTV_MAX / 100) {
            ltv.value = LTV_MAX;
            la.value = Math.floor(getNumeric(pv.value) * LTV_MAX / 100);
            currencyFormat(la);
            return;
        }
    // WWW0021, end
    }

    var ltvresult = 0 ;
    ltvresult = (getNumeric(la.value) / getNumeric(pv.value)) * 100 ;

    if(ltvresult > LTV_MAX){  // WWW0021
        alert(MSG_INVALID_LOANAMOUNT+':');
        la.value = '';
        setTextboxStyle('loanamount', '');
        return false;  // WWW0021
    }

    setTextboxStyle('loanamount', '#ffffff');

    ltv.value = ltvresult;

    NumObjRoundUp(la,0);

    currencyFormat(la);

    NumObjRoundUp(ltv,2);
    mitype_check(f);
}

function validate(f) {
    if (f.pptyvalue.value == '') {
        setTextboxStyle('pptyvalue', '');
        alert(MSG_INVALID_PV);
        f.pptyvalue.focus();
        return false;
    // WWW0022 start
    }else if(getNumeric(f.pptyvalue.value) > PV_MAX) {
        setTextboxStyle('pptyvalue', '');
        alert(MSG_INVALID_PV_MAX);
        f.pptyvalue.focus();
        return false;
    // WWW0022 end
    }else{setTextboxStyle('pptyvalue', '#ffffff');}

    if (f.ltv.value == '') {
        setTextboxStyle('ltv', '');
        alert(MSG_INVALID_LTV);
        f.ltv.focus();
        return false;
    }else{
       setTextboxStyle('ltv', '#ffffff');
       // WWW0021
       if (f.ltv.value <= 70 && f.mitype.selectedIndex > 0) {
            f.mitype.selectedIndex = 0
        }
       // End WWW0021
    }

    // WWW0021
    if (f.pptyvalue.value != '' && f.pptyvalue.value != '') {
      var calLoanAmount = (getNumeric(f.pptyvalue.value) * f.ltv.value / 100);
      if (f.loanamount.value != calLoanAmount) {
        f.loanamount.value = calLoanAmount;
        NumObjRoundUp(f.loanamount,0);
        currencyFormat(f.loanamount);
      }
    }
    // End WWW0021

    if (f.loanamount.value == '' || f.loanamount.value < LOAN_MIN || f.loanamount.value > LOAN_MAX) {  // WWW0021
        setTextboxStyle('loanamount', '');
        alert(MSG_INVALID_LOANAMOUNT);
        f.loanamount.focus();
        return false;
    }else{setTextboxStyle('loanamount', '#ffffff');}

    isFARM = (parseInt(f.mtype.options[f.mtype.selectedIndex].value) == 1);

    if (isFARM) {
        if (f.farm_y1.value == '') {
            setTextboxStyle('farm_y1', '');
            alert(MSG_INVALID_YEAR);
            f.farm_y1.focus();
            return false;
        }else{setTextboxStyle('farm_y1', '#ffffff');}

        if (f.farm_r1.value == '') {
            setTextboxStyle('farm_r1', '');
            alert(MSG_INVALID_AMRATE);
            f.farm_r1.focus();
            return false;
        }else{setTextboxStyle('farm_r1', '#ffffff');}

        if (f.farm_r2.value == '') {
            setTextboxStyle('farm_r2', '');
            alert(MSG_INVALID_AMRATE);
            f.farm_r2.focus();
            return false;
        }else{setTextboxStyle('farm_r2', '#ffffff');}

        if (12 * parseInt(f.year.options[f.year.selectedIndex].value) - parseInt(f.farm_y1.value <= 0)) {
            setTextboxStyle('farm_y1', '');
            alert(MSG_INVALID_YEAR);
            f.farm_y1.focus();
            return false;
        }else{setTextboxStyle('farm_y1', '#ffffff');}

    } else {
        if (f.amrate.value == '') {
            setTextboxStyle('amrate', '#ffffff');
            alert(MSG_NO_AMRATE);
            f.amrate.focus();
            return false;
        } else {
            setTextboxStyle('amrate', '#ffffff');
        }
        if (f.ltv.value > 70 && f.mitype.selectedIndex <= 0) {
            alert(MSG_MITYPE_GT70);
            f.mitype.focus();
            return false;
        }
    }

    // NOO begins
    if (f.useofppty[f.useofppty.selectedIndex].value == 'Non Owner-occupied') {
       if (f.ltv.value > 85) {
           alert(MSG_MITYPE_NOO_LTV);
           f.ltv.focus();
           return false;
       }
       if (pparseFloat(f.loanamount.value) > LOAN_MAX_NOO) {  // WWW0017
           alert(MSG_MITYPE_NOO_LOANAMT);
           f.loanamount.focus();
           return false;
       }
       if (f.mitype.selectedIndex == 3) {
           alert(MSG_MITYPE_NOO_AN);
           f.mitype.selectedIndex = 1;
           f.mitype.focus();
           return false;
       }
    }
    // NOO ends

    // LTV75
    if (f.mitype.selectedIndex == 3 && f.ltv.value > 70 && f.ltv.value <= 75) {
        alert(MSG_MITYPE_LT75_AN);
        f.mitype.selectedIndex = 1;
        f.mitype.focus();
        return false;
    }

    // WWW0017 begin
    /*  WWW0021
    if (f.ltv.value <= 90) {
        if (pparseFloat(f.loanamount.value) > LOAN_MAX_LTV90) {
            alert(MSG_INVALID_LOANAMT_LTV90);
            setTextboxStyle('pptyvalue', '');
            f.pptyvalue.focus();
            return false;
        }
    }
    */
    // WWW0017 end

    // WWW0021 begin
    if (isNaN(f.ltv.value) || f.ltv.value <= LTV_MIN) {
        alert(MSG_INVALID_LTV);
        f.ltv.value = '';
        setTextboxStyle('ltv', '');
        return false;
    }
    if (f.ltv.value > LTV_MAX) {
        alert(MSG_INVALID_LTV);
        f.ltv.value = '';
        setTextboxStyle('ltv', '');
        return false;
    }
    // WWW0021 end

    // 2008/12/29, EN
    /* WWW0021
    if (f.ltv.value > 90) {
        if (pparseFloat(f.loanamount.value) > LOAN_MAX_LTV95) {  // WWW0017
            alert(MSG_INVALID_LOANAMT_LTV95);
            setTextboxStyle('pptyvalue', '');
            f.pptyvalue.focus();
            return false;
        }
        if (pparseFloat(f.year.value) > 30) {
            alert(MSG_INVALID_TENOR_LTV95);
            f.year.selectedIndex = 0;
            f.year.focus();
            return false;
        }
    }
    */
    // End 2008/12/29, EN

    boolValidate = true;
    return true;
}

function calculate(f) {
    w = reopen('mortgagecalc_res', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=590,height=540,top=0,left=0');
    var d = w.document;
    writeHeader(d);

    /* inputs */
    d.writeln('<table width="530" border="0" cellspacing="1" cellpadding="5" class="framered">');
    d.writeln('<tr class="arial12">');
    d.writeln('<th align="center" colspan="2" class="arial12" bgcolor="#8b2942"><font color="#ffffff"><b>Mortgage Calculator</th>');
    d.writeln('</tr>');
    // NOO begins
    d.writeln('<tr class="arial12">');
    d.writeln('<td width="66%" class="arial12" bgcolor="#dedddc"><b>Use of Property</b></td>');
    d.writeln('<td bgcolor="#dedddc" align="right">' + f.useofppty.options[f.useofppty.selectedIndex].value + '</td></tr>');
    // NOO ends
    d.writeln('<tr class="arial12">');
    d.writeln('<td width="66%" class="arial12" bgcolor="#f2f1f0"><b>Property Value</b></td>');
    d.writeln('<td width="34%" bgcolor="#f2f1f0" align="right">HK$' + number_format(f.pptyvalue.value, 2) + '</td></tr>');
    var tmp = (f.ltv.value == '' ? pparseFloat(f.loanamount.value) / pparseFloat(f.pptyvalue.value) * 100: f.ltv.value);
    d.writeln('<tr class="arial12" >');
    d.writeln('<td class="arial12" bgcolor="#dedddc"><b>Loan-To-Value Ratio</b></td>');
    d.writeln('<td bgcolor="#dedddc" align="right">' + number_format(tmp, 2) + '%</td></tr>');
    d.writeln('<tr class="arial12">');
    d.writeln('<td class="arial12" bgcolor="#f2f1f0"><b>Loan Amount</b></td>');
    d.writeln('<td bgcolor="#f2f1f0" align="right">HK$' + number_format(f.loanamount.value, 2) + '</td></tr>');
    d.writeln('<tr class="arial12">');
    d.writeln('<td class="arial12" bgcolor="#dedddc"><b>Loan Tenor</b></td>');
    d.writeln('<td bgcolor="#dedddc" align="right">' + number_format(f.year.options[f.year.selectedIndex].value, 0) + ' years</td></tr>');
    d.writeln('<tr class="arial12">');
    d.writeln('<td class="arial12" bgcolor="#f2f1f0"><b>Mortgage Type</b></td>');
    d.writeln('<td bgcolor="#f2f1f0" align="right">');
    if (parseInt(f.mtype.options[f.mtype.selectedIndex].value) == 0) {
        d.write('Floating Rate');
    } else {
        d.write('Fixed Adjustable Rate under the HKMC FARM Programme');
    }
    d.writeln('</td></tr>');
    d.writeln('</table>');

    /* calculate total loan amount */
    var pv = pparseFloat(f.pptyvalue.value);
    var principal = pparseFloat(f.loanamount.value);
    var ltv = (f.ltv.value == '' ? principal / pv: f.ltv.value / 100);
    var loanamount = principal;
    var miclass = getMIClass(parseInt(f.year.options[f.year.selectedIndex].value));

    if (f.mitype.options[f.mitype.selectedIndex].value != 2) {
        // NOO begins
        if (f.useofppty[f.useofppty.selectedIndex].value == 'Non Owner-occupied') {
           var spp = getNooSPP(ltv, miclass);
           var sppfix = getNooSPPFix(ltv, miclass);
        } else {
           var spp = getSPP(ltv, miclass);
           var sppfix = getSPPFix(ltv, miclass);
        }
        // NOO ends
        var paccount = Math.round(spp / 100 * principal * 100) / 100; // NOO
        var paccountfix = Math.round(sppfix / 100 * principal * 100) / 100; // NOO
        if (parseInt(f.mtype.options[f.mtype.selectedIndex].value) == 1) {
            spp = sppfix;
            paccount = paccountfix;
        }
        if (parseInt(f.mitype.options[f.mitype.selectedIndex].value) == 1) principal += paccount;
    }
    // NOO begins
    if (f.useofppty[f.useofppty.selectedIndex].value == 'Non Owner-occupied') {
       var app1 = 0;
       var app2 = 0;
       var appfix1 = 0;
       var appfix2 = 0;
    } else {
       var app1 = getAPP1(ltv, miclass);
       var app2 = getAPP2(ltv, miclass);
       var appfix1 = getAPPFix1(ltv, miclass);
       var appfix2 = getAPPFix2(ltv, miclass);
    }
    // NOO ends
    if (parseInt(f.mtype.options[f.mtype.selectedIndex].value) == 1) {
        app1 = appfix1;
        app2 = appfix2;
    }
    var instalment = 0;
    var instalment1 = 0;
    var instalment2 = 0;
    if (isFARM) {
        instalment1 = pmt(pparseFloat(f.farm_r1.value) / 100 / 12, 12 * parseInt(f.year.options[f.year.selectedIndex].value), -1 * principal);
        instalment2 = pmt(pparseFloat(f.farm_r2.value) / 100 / 12, 12 * parseInt(f.year.options[f.year.selectedIndex].value) - parseInt(f.farm_y1.value), -1 * repayment_total(pparseFloat(f.farm_r1.value) / 100 / 12, parseInt(f.farm_y1.value), principal, instalment1));
    } else {
        instalment = pmt(pparseFloat(f.amrate.value) / 100 / 12, 12 * parseInt(f.year.options[f.year.selectedIndex].value), -1 * principal);
    }

    /* Mortgage Insurance Programme Premium Table */
    if(f.ltv.value>=70){
        d.writeln('<p></p><p></p>');
        d.writeln('<table width="530" border="0" cellspacing="1" cellpadding="5">');
        switch (parseInt(f.mitype.options[f.mitype.selectedIndex].value)) {
            case 1:
            case 3:
                d.writeln('<tr class="arial12" bgcolor="#8B2942" align="center">');
                d.writeln('<td colspan="2" class="arial12">');
                d.writeln(' <table width="510" border="0" cellspacing="0" cellpadding="0">');
                d.writeln('  <tr class="arial12" bgcolor="#8B2942" align="center">');
                d.writeln('   <td width="20%"> </td>');
                d.writeln('   <td width="60%" class="arial12"><b><font color="#ffffff">Mortgage Insurance Programme Premium Table<br/>');
                if (parseInt(f.mtype.options[f.mtype.selectedIndex].value) == 0) {
                    d.write('Floating Rate');
                } else {
                    d.write('Fixed Adjustable Rate under the HKMC FARM Programme');
                }
                d.write(' - ');
                switch (parseInt(f.mitype.options[f.mitype.selectedIndex].value)) {
                    case 1: d.write('Single Financed'); break;
                    case 3: d.write('Single Non-financed'); break;
                }
                d.writeln('     <br>(Based on 70% LTV Threshold)');   // 2008/12/29, EN
                d.writeln('     </b></font></td>');
                // NOO begins
                if (f.useofppty[f.useofppty.selectedIndex].value == 'Owner-occupied') {
                   d.writeln('   <td width="20%" align=left><a href="../../ourbusiness/mip-pd.html" target="mip-pd"><img src="../image/mip-pd-50.gif"></a></td>');
                }
                else {
                   d.writeln('   <td width="20%" align=left>&nbsp;</td>');
                }
                // NOO ends
                d.writeln('  </tr>');
                d.writeln(' </table>');
                d.writeln('</td></tr>');
                d.writeln('<tr class="arial12" bgcolor="#FDC076">');
                d.writeln('<td width="50%" class="arial12" align="center"><b>% of loan amount at origination</b></td>');
                d.writeln('<td width="50%" align="center"><b>Amount</b></td>');
                d.writeln('</tr>');
                d.writeln('<tr class="arial12" align="center">');
                d.writeln('<td class="arial12" width="50%" bgcolor="#f2f1f0">' + number_format(spp, 2) + '%</td>');
                d.writeln('<td bgcolor="#f2f1f0" width="50%">HK$' + number_format(paccount, 2) + '</td>');
                d.writeln('</tr>');
                break;
            case 2:
                d.writeln('<tr class="arial12" bgcolor="#8B2942" align="center">');
                d.writeln('<td colspan="4" class="arial12">');
                d.writeln(' <table width="510" border="0" cellspacing="0" cellpadding="0">');
                d.writeln('  <tr class="arial12" bgcolor="#8B2942" align="center">');
                d.writeln('   <td width="20%"> </td>');
                d.writeln('   <td width="60%" class="arial12"><b><font color="#ffffff">Mortgage Insurance Programme Premium Table<br />');
                if (parseInt(f.mtype.options[f.mtype.selectedIndex].value) == 0) {
                    d.write('Floating Rate');
                } else {
                    d.write('Fixed Adjustable Rate under the HKMC FARM Programme');
                }
                d.write(' - ');
                d.write('Annual Non-Financed');
                d.writeln('     <br>(Based on 70% LTV Threshold)');   // 2008/12/29, EN
                d.writeln('     </b></font></td>');                   // 2008/12/29, EN
                // NOO begins
                if (f.useofppty[f.useofppty.selectedIndex].value == 'Owner-occupied') {
                   d.writeln('   <td width="20%" align=left><a href="../../ourbusiness/mip-pd.html" target="mip-pd"><img src="../image/mip-pd-50.gif"></a></td>');
                }
                else {
                   d.writeln('   <td width="20%" align=left>&nbsp;</td>');
                }
                // NOO ends
                d.writeln('  </tr>');
                d.writeln(' </table>');
                d.writeln('</td></tr>');
                d.writeln('<tr class="arial12" bgcolor="#FDC076" align="center">');
                d.writeln('<td colspan="2" class="arial12"><b>1st Year</b></td>');
                d.writeln('<td colspan="2"><b>Renewal</b></td>');
                d.writeln('</tr>');
                d.writeln('<tr class="arial12" bgcolor="#FDC076">');
                d.writeln('<td width="25%" class="arial12" align="center"><b>% of loan amount at origination</b></td>');
                d.writeln('<td width="25%" align="center"><b>Amount</b></td>');
                d.writeln('<td width="25%" align="center"><b>% of loan amount at origination</b></td>');
                d.writeln('<td width="25%" align="center"><b>Amount</b></td>');
                d.writeln('</tr>');
                d.writeln('<tr class="arial12" bgcolor="#f2f1f0" align="center">');
                d.writeln('<td class="arial12" width="25%">' + number_format(app1, 2) + '%</td>');
                d.writeln('<td class="arial12" width="25%">HK$' + number_format(Math.round(principal * app1) / 100, 2) + '</td>');
                d.writeln('<td class="arial12" width="25%">' + number_format(app2, 2) + '%</td>');
                d.writeln('<td class="arial12" width="25%">HK$' + number_format(Math.round(principal * app2) / 100, 2) + '</td></tr>');
                break;
            default: break;
        }
        d.writeln('</table>');
    }
    /* end Mortgage Insurance Programme Premium Table */

    d.writeln('<p></p><p></p>');
    /* Mortgage Summary Table */
    d.writeln('<table width="530" border="0" cellspacing="1" cellpadding="5">');
    d.writeln('<tr class="arial12" bgcolor="#8B2942">');
    d.writeln('<td colspan="4" class="arial12" align="center"><b><font color="#ffffff">Calculation Result</font></b></td>');
    d.writeln('</tr>');
    d.writeln('<tr class="arial12" bgcolor="#FDC076">');
    d.writeln('<td width="33%" class="arial12" align="center"><b>Total Loan Amount</b></td>');
    d.writeln('<td width="33%" align="center"><b>Interest Rate per annum</b></td>');
    d.writeln('<td width="34%" align="center"><b>Monthly Instalment Amount</b></td>');
    d.writeln('<tr class="arial12" bgcolor="#f2f1f0">');
    //principal = number_format(NumRoundUp(principal,2),2);
    d.writeln('<td class="arial12" width="33%" align="center">HK$' + number_format(principal,2) + '</td>');
    d.writeln('<td bgcolor="#f2f1f0" width="33%" align="center">');
    if (isFARM) {
        d.writeln('First ' + f.farm_y1.value + ' months ' + number_format(f.farm_r1.value,3) + ' %<br />');
        d.writeln('Thereafter ' + number_format(f.farm_r2.value,3) + '%');
    } else {
        d.writeln(number_format(f.amrate.value,3) + '%');
    }
    d.writeln('</td>');
    d.writeln('<td align="center" width="34%" bgcolor="#f2f1f0">');
    if (isFARM) {
        d.writeln('First ' + f.farm_y1.value + ' months HK$' + number_format(instalment1, 2) + '<br />');
        d.writeln('Thereafter HK$' + number_format(instalment2, 2));
    } else {
        d.writeln('HK$' + number_format(instalment, 2));
    }
    d.writeln('</td>');
    //d.writeln('<td width="25%" align="center">TBC</td>');
    d.writeln('</tr>');
    d.writeln('</table>');
    /* end Mortgage Summary Table */

    d.writeln('<p></p><p></p>');
    /* monthly payment table */
    d.writeln('<table width="530" border="0" cellspacing="1" cellpadding="5" class="framered">');
    d.writeln('<thead>');
    d.writeln('<tr bgcolor="#8B2942" align="center"><td colspan="7" class="arial12"><font color="#FFFFFF"><b>Repayment Schedule</b></td></tr>');
    d.writeln('<tr class="arial12" bgcolor="#FDC076" valign="bottom">');
    d.writeln('<td class="arial12" width="10%" bgcolor="#E27F37"><font color="#FFFFFF"><b>Term</b></font></td>');
    d.writeln('<th class="arial12" width="18%">Opening<br />Balance<br />(HK$)</th>');
    d.writeln('<th class="arial12" width="18%">Monthly<br />Instalment<br />(HK$)</th>');
    d.writeln('<th class="arial12" width="18%">Interest<br />Payment<br />(HK$)</th>');
    d.writeln('<th class="arial12" width="18%">Principal<br />Repayment<br />(HK$)</th>');
    d.writeln('<th class="arial12" width="18%">Outstanding<br />Balance<br />(HK$)</th>');
    d.writeln('</tr>');
    d.writeln('</thead>');
    d.writeln('<tbody>');
    var j = 12 * parseInt(f.year.options[f.year.selectedIndex].value);
    for (var i = 1; i <= j; i++) {
        if (isFARM) {
            if (i <= parseInt(f.farm_y1.value)) {
                instalment = instalment1;
                mipayment = principal * pparseFloat(f.farm_r1.value) / 100 / 12;
            } else {
                instalment = instalment2;
                mipayment = principal * pparseFloat(f.farm_r2.value) / 100 / 12;
            }
        } else {
            mipayment = principal * pparseFloat(f.amrate.value) / 100 / 12;
        }
        mppayment = instalment - mipayment;
        outstand = principal - mppayment;
        if (outstand > pv * 0.7) lastappterm = i + 1; // barrier
        if (outstand < 0) outstand = 0;
        d.write('<tr bgcolor="#');
        d.write(i % 2 == 0 ? 'DEDDDC': 'F2F1F0');
        d.writeln('">');
        d.writeln('<td class="arial12" bgcolor="#999999" align="right"><font color="#FFFFFF"><b>' + i + '</b></font></td>');
        d.writeln('<td class="arial12" align="right">' + number_format(principal, 2) + '</td>');
        d.writeln('<td class="arial12" align="right">' + number_format(instalment, 2) + '</td>');
        d.writeln('<td class="arial12" align="right">' + number_format(mipayment, 2) + '</td>');
        d.writeln('<td class="arial12" align="right">' + number_format(mppayment, 2) + '</td>');
        d.writeln('<td class="arial12" align="right">' + number_format(outstand, 2) + '</td>');
        d.writeln('</tr>');
        principal = outstand;
    }
    d.writeln('</tbody>');
    d.writeln('<tfoot>');
    if (f.mitype.options[f.mitype.selectedIndex].value == 2) d.writeln('<tr bgcolor="#8B2942" align="center"><td colspan="6" class="arial12"><font color="#FFFFFF"><b>MIP Cover Period will expire after the instalment payment of Term ' + lastappterm + '</b></td></tr>');
    d.writeln('</tfoot>');
    d.writeln('</table>');
    /* end monthly payment table */
    writeFooter(d);
    boolValidate = false;
}

function writeHeader(d) {
    /* header */
    with (d) {
        open('text/html', 'replace');
        writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
        writeln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">');
        writeln('<head>');
        writeln('<meta http-equiv="Content-Language" content="iso-8859-1" />');
        writeln('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />');
        writeln('<meta http-equiv="imagetoolbar" content="false" />');
        writeln('<meta name="mssmarttagspreventparsing" content="true" />');
        writeln('<title>The Hong Kong Mortgage Corporation Limited</title>');
        writeln('<link rel="stylesheet" href="../../../hkmc.css" type="text/css" />');
        writeln('</head>');
        writeln('<body bgcolor="#ffffff" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">');
        writeln('<table width="100%" border="0" cellspacing="0" cellpadding="0">');
        writeln('<tr bgcolor="#fedca2">');
        writeln('<td width="100%" bgcolor="#fedca2" height="83"><img src="../../../image/common/logo_hkmc.gif" width="371" height="83"></td>');
        writeln('</tr>');
        writeln('<tr bgcolor="#ffffff">');
        writeln('<td>&nbsp;</td>');
        writeln('</tr>');
        writeln('<tr bgcolor="#ffffff">');
        writeln('<td width="100%" align="right">');
        writeln('<a href="javascript:;" onclick="window.print();"><img src="../../../image/common/btn_print.gif" width="59" height="26" border="0" /></a> <a href="javascript:;" onClick="window.close();"><img src="../../../image/common/btn_close.gif" width="59" height="26" border="0" /></a>');
        writeln('</td>');
        writeln('</tr>');
        writeln('</table>');
        writeln('<div style="margin:0px 0px 0px 20px;">');
    }
}

function writeFooter(d) {
    /* footer and buttons */
    with (d) {
        writeln('<p class="arial9">The above information is for reference only. For easy reference, all figures are rounded up to 2 decimal places.</p>');
        writeln('<p class="arial12bold">Related Document:</p>');
        writeln('<ul>');
        writeln('<li class="arial12"><a href="../../ourbusiness/Annex-1-English.pdf" target="_blank">Premium Rate Sheet</a></li>');
        writeln('</ul>');
        writeln('<p class="arial12">&copy; Copyright The Hong Kong Mortgage Corporation Limited<br />All rights reserved</p>');
        writeln('</div>');
        writeln('<table width="100%" border="0" cellspacing="0" cellpadding="0">');
        writeln('<tr bgcolor="#ffffff">');
        writeln('<td width="100%" align="right">');
        writeln('<a href="javascript:;" onclick="window.print();"><img src="../../../image/common/btn_print.gif" width="59" height="26" border="0" /></a> <a href="javascript:;" onClick="window.close();"><img src="../../../image/common/btn_close.gif" width="59" height="26" border="0" /></a>');
        writeln('</td>');
        writeln('</tr>');
        writeln('</table>');
        writeln('</body>');
        writeln('</html>');
        close();
    }
}

function SetKeyEvent(e) {
    var k = window.event ? e.keyCode: e.which;
    if (k == 13) {
        if (!validate(document.forms['calc'])) return false;
        w = window.open('','mortgagecalc_res', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=590,height=540,top=0,left=0');
        calculate(document.forms['calc']);
        currencyFormat(document.forms['calc'].elements['pptyvalue']);
    }
    return true;
}

function SetKeyEventSubmit(){
    var k = window.event ? event.keyCode: event.which;
    if (k == 13 || k == 32) {
        validate(document.forms['calc']);if (boolValidate == false)return;win = window.open('','', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=590,height=540,top=0,left=0');calculate(document.forms['calc']);currencyFormat(document.all.pptyvalue);
        return false;
    }
}

function getMIClass(y) {
    if (y < YEAR_MIN || y > YEAR_MAX) return 0;
    if (y <= 10) {
        return 1;
    } else if (y <= 15) {
        return 2;
    } else if (y <= 20) {
        return 3;
    } else if (y <= 25) {
        return 4;
    } else if (y <= 30) {
        return 5;
    } else if (y <= 35) {
        return 6;
    } else if (y <= 40) {
        return 7;
    } else {
        return 0;
    }
}

// NOO begins
function getNooSPP(ltv, miclass) {
    if (ltv <= LTV_MIN / 100 || ltv > LTV_MAX / 100) return 0;

    if (ltv <= 0.75) {
        return Array(0, 0.95, 1.00, 1.05, 1.10, 1.15, 1.20, 1.25)[miclass];
    } else if (ltv <= 0.80) {
        return Array(0, 1.60, 1.75, 2.00, 2.10, 2.25, 2.35, 2.45)[miclass];
    } else if (ltv <= 0.85) {
        return Array(0, 2.55, 2.80, 3.15, 3.30, 3.40, 3.50, 3.60)[miclass];
    } else {
        return 0;
    }
}

function getNooSPPFix(ltv, miclass) {
    if (ltv <= LTV_MIN / 100 || ltv > LTV_MAX / 100) return 0;

    if (ltv <= 0.75) {
        return Array(0, 0.93, 0.98, 1.03, 1.08, 1.13, 1.17, 1.22)[miclass];
    } else if (ltv <= 0.80) {
        return Array(0, 1.55, 1.70, 1.95, 2.05, 2.15, 2.25, 2.35)[miclass];
    } else if (ltv <= 0.85) {
        return Array(0, 2.40, 2.70, 2.95, 3.05, 3.20, 3.30, 3.40)[miclass];
    } else {
        return 0;
    }
}
//NOO ends

function getSPP(ltv, miclass) {
    if (ltv < LTV_MIN / 100 || ltv > LTV_MAX / 100) return 0;

    // LTV75
    if (ltv <= 0.75) {
        return Array(0, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85)[miclass];
    } else if (ltv <= 0.80) {
        return Array(0, 1.00, 1.15, 1.40, 1.50, 1.65, 1.75, 1.85)[miclass];
    } else if (ltv <= 0.85) {
        return Array(0, 1.55, 1.80, 2.15, 2.30, 2.40, 2.50, 2.60)[miclass];
    } else if (ltv <= 0.90) {
        return Array(0, 2.15, 2.50, 2.98, 3.35, 3.55, 3.75, 3.95)[miclass];
    } else if (ltv <= 0.95) {
        return Array(0, 2.48, 2.88, 3.38, 3.78, 3.98, 4.28, 4.58)[miclass];
    } else {
        return 0;
    }
}

function getSPPFix(ltv, miclass) {
    if (ltv < LTV_MIN / 100 || ltv > LTV_MAX / 100) return 0;

    // LTV75
    if (ltv <= 0.75) {
        return Array(0, 0.53, 0.58, 0.63, 0.68, 0.73, 0.77, 0.82)[miclass];
    } else if (ltv <= 0.80) {
        return Array(0, 0.95, 1.10, 1.35, 1.45, 1.55, 1.65, 1.75)[miclass];
    } else if (ltv <= 0.85) {
        return Array(0, 1.40, 1.70, 1.95, 2.05, 2.20, 2.30, 2.40)[miclass];
    } else if (ltv <= 0.90) {
        return Array(0, 2.01, 2.34, 2.84, 3.18, 3.38, 3.58, 3.78)[miclass];
    } else if (ltv <= 0.95) {
        return Array(0, 2.32, 2.70, 3.27, 3.66, 3.89, 4.19, 4.44)[miclass];
    } else {
        return 0;
    }
}

function getAPP1(ltv, miclass) {
    //LTV75: if (ltv < LTV_MIN / 100 || ltv > LTV_MAX / 100) return 0;
    if (ltv <= 0.75 || ltv > LTV_MAX / 100) return 0; // LTV75
    if (ltv <= 0.80) {
        return Array(0, 0.50, 0.60, 0.70, 0.75, 0.85, 0.95, 1.05)[miclass];
    } else if (ltv <= 0.85) {
        return Array(0, 0.70, 0.80, 0.90, 1.00, 1.10, 1.20, 1.30)[miclass];
    } else if (ltv <= 0.90) {
        return Array(0, 0.90, 1.09, 1.28, 1.46, 1.65, 1.85, 2.05)[miclass];
    } else if (ltv <= 0.95) {
        return Array(0, 1.04, 1.26, 1.48, 1.68, 1.90, 2.10, 2.30)[miclass];
    } else {
        return 0;
    }
}

function getAPP2(ltv, miclass) {
    //LTV75: if (ltv < LTV_MIN / 100 || ltv > LTV_MAX / 100) return 0;
    if (ltv <= 0.75 || ltv > LTV_MAX / 100) return 0; // LTV75
    if (ltv <= 0.80) {
        return 0.24;
    } else if (ltv <= 0.85) {
        return 0.45;
    } else if (ltv <= 0.90) {
        return 0.63;
    } else if (ltv <= 0.95) {
        return 0.73;
    } else {
        return 0;
    }
}

function getAPPFix1(ltv, miclass) {
    //LTV75: if (ltv < LTV_MIN / 100 || ltv > LTV_MAX / 100) return 0;
    if (ltv <= 0.75 || ltv > LTV_MAX / 100) return 0; // LTV75
    if (ltv <= 0.80) {
        return Array(0, 0.45, 0.55, 0.65, 0.70, 0.80, 0.85, 0.95)[miclass];
    } else if (ltv <= 0.85) {
        return Array(0, 0.65, 0.75, 0.85, 0.95, 1.05, 1.15, 1.25)[miclass];
    } else if (ltv <= 0.90) {
        return Array(0, 0.85, 1.03, 1.20, 1.31, 1.50, 1.70, 1.90)[miclass];
    } else if (ltv <= 0.95) {
        return Array(0, 0.98, 1.19, 1.38, 1.51, 1.73, 1.93, 2.15)[miclass];
    } else {
        return 0;
    }
}

function getAPPFix2(ltv, miclass) {
    //LTV75: if (ltv < LTV_MIN / 100 || ltv > LTV_MAX / 100) return 0;
    if (ltv <= 0.75 || ltv > LTV_MAX / 100) return 0; // LTV75
    if (ltv <= 0.80) {
        return 0.24;
    } else if (ltv <= 0.85) {
        return 0.40;
    } else if (ltv <= 0.90) {
        return 0.59;
    } else if (ltv <= 0.95) {
        return 0.68;
    } else {
        return 0;
    }
}

function repayment_total(rate, nper, pv, repay) {
    for (var i = 0; i < nper; i++) {
        pv *= (1 + rate);
        pv -= repay;
    }
    return pv;
}

/*
    Returns a Double specifying the payment for an annuity based on periodic, fixed payments and a fixed interest rate.
    Parameters:
    rate: interest rate per period
    nper: total number of payment periods in the annuity.
    pv: present value (or lump sum) that a series of payments to be paid in the future is worth now
*/
function pmt(rate, nper, pv) {
    if (rate == 0 || rate == -1) return false;
    var Z = 1 / (1 + rate);
    return -1 * ((1 - Z) * pv) / (Z * (1 - Math.pow(Z, nper)));
}

function setTextboxStyle(name, bool) {
    var e = MM_findObj(name);
    if (!e) return;
    if (bool == false || bool == '') {
        e.style.backgroundColor = '#fedca2';
        e.focus();
    } else {
        e.style.backgroundColor = bool;
    }
}

function resetAllElement(f){
    for (i = 0 ; i < f.elements.length ; i++){
        if(f.elements[i].type == 'text'){
            setTextboxStyle(f.elements[i].name,"#ffffff");
        }
    }
}

function mitype_check(f) {
    var e = f.mitype;
    if (f.ltv.value == '') return;
    var ltv = parseFloat(f.ltv.value);
    if (isNaN(ltv) || ltv < 0 || ltv > 100) {
        ltv = 0;
    }

    if (e.selectedIndex == 0 && ltv > 70) {
        alert(MSG_MITYPE_GT70);
        e.selectedIndex = 1;
    } else if (e.selectedIndex > 0 && ltv <= 70) {
        alert(MSG_MITYPE_LT70);
        e.selectedIndex = 0;
    } else if (e.selectedIndex == 3) {
       // NOO begins
       if (f.useofppty[f.useofppty.selectedIndex].value == 'Non Owner-occupied') {
           alert(MSG_MITYPE_NOO_AN);
           e.selectedIndex = 1;
       // NOO ends
       // LTV75 begins
       } else if (ltv > 70 && ltv <= 75) {
           alert(MSG_MITYPE_LT75_AN);
           e.selectedIndex = 1;
       }
       // LTV75 ends
    }
}

