 

function cloneMRI(){
	var mri = $('#bpm_mri').val();
	$('#bpm_movein_mri').val(mri);
}


function computeNetMRI2(a) {
    var e = 0,
        t = 0,
        m = 0;
    e = $("#bpm_loanable_amount").val().replace(/,/g, ""), t = $("#bpm_takeout_amount").val().replace(/,/g, ""), m = $("#bpm_mri").val().replace(/,/g, ""), (null == m || 0 == m) && (m = 0), (null == t || 0 == t) && (t = 0), (null == a || 0 == a) && (a = 0);
    var o = parseFloat(e) - parseFloat(t) - parseFloat(m) - parseFloat(a);
    $("#bpm_net_of_mri").val(Comma(o.toFixed(2)))
}

function computeNetMRI() {
    var a = 0,
        e = 0,
        t = 0,
        m = 0;
    a = $("#bpm_loanable_amount").val().replace(/,/g, ""), e = $("#bpm_takeout_amount").val().replace(/,/g, ""), t = $("#bpm_mri").val().replace(/,/g, ""), m = $("#bpm_adjustment").val().replace(/,/g, ""), (null == t || 0 == t || "" == t) && (t = 0), (null == e || 0 == e || "" == e) && (e = 0), (null == m || 0 == m || "" == m) && (m = 0);
    var o = parseFloat(a) - parseFloat(e) - parseFloat(m) - parseFloat(t);
    $("#bpm_net_of_mri").val(Comma(o.toFixed(2)))
}

function computeNetEquity() {
    var a = $("#bpm_tcp").val().replace(/,/g, ""),
        e = $("#bpm_discount").val().replace(/,/g, ""),
        t = $("#bpm_loanable_amount").val().replace(/,/g, "");
    getReservationFee();
    var m = $("#reservation_fee").val().replace(/,/g, ""),
        o = $("#bpm_payment_mode").val();
    if ("cash" == o) var _ = parseFloat(a) - parseFloat(e) - parseFloat(t);
    else var _ = parseFloat(a) - parseFloat(e) - parseFloat(t) - parseFloat(m);
    var l = Comma(_.toFixed(2));
    $("#bpm_net_equity").val(l), computeMonthlyEquity()
}

function getReservationFee() {
    var a = $("#current_model").val(),
        e = "current_model=" + a;
    $.post("./ajax/load_default_model_values.php", e, function(a) {
        var e = $.parseJSON(a),
            t = (e.data[0].remarks, e.data[0].reservation_fee);
        $("#reservation_fee").val(t)
    })
}

function computeDifferenceApprovedLoanable() {
    var a = $("#bpm_pagibig_approved_loanable_amount").val().replace(/,/g, ""),
        e = $("#bpm_pagibig_loanable_amount").val().replace(/,/g, "");
    (0 == a || "" == a) && (a = 0);
    var t = parseFloat(a) - parseFloat(e),
        m = Comma(t.toFixed(2));
    $("#bpm_difference_of_approved_vs_loanable").val(m)
}

function computeMonthlyEquity() {
    var a = $("#bpm_net_equity").val().replace(/,/g, ""),
        e = $("#bpm_terms_of_payment").val().replace(/,/g, ""),
        t = parseFloat(a) / parseFloat(e),
        m = Comma(t.toFixed(2));
    $("#bpm_monthly_equity").val(m)
}

function save_buyer_payment_monitoring() {
    var a = $("#buyer_payment_monitoring_form").serialize(),
        e = $("#main_page").val(),
        t = ($("#main_type").val(), $("#main_action").val()),
        m = $("#main_id").val(),
        o = a + "&action=" + encodeURIComponent(t) + "&mainid=" + encodeURIComponent(m);
    $.post("./ajax/buyer_payment_monitoring.php", o, function(a) {
        var t = $.parseJSON(a);
        1 == t.output ? (bootbox.alert("Successfully Saved ..."), window.setTimeout(function() {
            window.location.href = "index.php?pg=" + e + "&type=buyer_payment_monitoring"
        }, 1e3)) : bootbox.alert("Failed in saving.")
    })
}

function load_buyer_payment_monitoring() {
    var ctype = $('#current_user_type').val();
    
    var a = ($("#main_page").val(), $("#main_type").val(), $("#main_action").val()),
        e = $("#main_id").val(),
        t = "action=" + encodeURIComponent(a) + "&mainid=" + encodeURIComponent(e);
    $.post("./ajax/load_buyer_payment_monitoring.php", t, function(a) {
        var e = $.parseJSON(a),
            t = e.data[0].full_name,
            m = e.data[0].model,
            o = e.data[0].date_rf_deposited,
            _ = e.data[0].reservation_date,
            l = e.data[0].start_of_equity,
            n = e.data[0].tcp,
            p = e.data[0].loanable_amount,
            i = Comma(parseFloat(e.data[0].net_equity.replace(/,/g, "")).toFixed(2)),
            r = e.data[0].discount,
            b = e.data[0].pdc_submitted,
            v = e.data[0].payment_terms,
            d = (e.data[0].interest, e.data[0].lacking_pdc),
            u = e.data[0].monthly_equity,
            c = e.data[0].remarks_on_equity,
            s = e.data[0].payments_made,
            g = e.data[0].takeout_amount,
            y = e.data[0].takeout_date,
            f = e.data[0].mri,
            F = e.data[0].difference,
            C = e.data[0].misc,
            q = e.data[0].adjustment,
            h = (e.data[0].pagibig_loanable_amount, e.data[0].movein_mri),
            k = e.data[0].movein_others,
            x = e.data[0].refilling_fee,
            N = e.data[0].pagibig_upgrading,
            R = e.data[0].pagibig_loanupdate,
            j = e.data[0].collected_date,
            E = e.data[0].deposited_date,
            I = e.data[0].movein_amount,
            w = e.data[0].approved_equity_date,
            ph = e.data[0].phase,
            bl = e.data[0].block,
            lo = e.data[0].lot,
            fa = e.data[0].floor_area,
            ls = e.data[0].lot_size,
            S = e.data[0].approved_loanable,
            M = e.data[0].payment_mode;
            pd = e.data[0].pricelist_date;
            
            

            var financing = e.data[0].financing;
            $('#bpm_financing').val(financing); 

			$('#bpm_pricelist_date').val(pd);
        "01/01/1970" == w && (w = ""), "01/01/1970" == l && (l = ""), "01/01/1970" == _ && (_ = ""), "01/01/1970" == o && (o = ""), "01/01/1970" == y && (y = ""), "01/01/1970" == j && (j = ""), "01/01/1970" == E && (E = ""), $("#current_model").val(m), $("#bpm_pagibig_approved_loanable_amount").val(S), $("#bpm_approved_equity").val(w), 
        $("#bpm_payment_mode").val(M), computeNetEquity(), $("#bpm_discount_temp").val(Comma(r)), $("#bpm_loanable_amount_temp").val(Comma(p)), $("#bpm_pagibig_loanable_amount_temp").val(Comma(p)), $("#bpm_terms_of_payment_temp").val(v), $("#bpm_fullname").val(t), $("#bpm_tcp").val(Comma(n)), $("#bpm_discount").val(Comma(r)), $("#bpm_remarks_on_equity").val(c), $("#bpm_date_rf_deposited").val(o), $("#bpm_phase").val(ph), $("#bpm_block").val(bl), $("#bpm_lot").val(lo), $("#bpm_lot_size").val(ls), $("#bpm_floor_area").val(fa), $("#bpm_loanable_amount").val(Comma(p)), $("#bpm_reservation_date").val(_), $("#bpm_start_of_equity").val(l), $("#bpm_pdc_submitted").val(b), $("#bpm_lacking_pdc").val(d), $("#bpm_net_equity").val(i), $("#bpm_terms_of_payment").val(v), $("#bpm_monthly_equity").val(Comma(u)), $("#bpm_payments_made").val(s), $("#bpm_takeout_amount").val(Comma(g)), $("#bpm_mri").val(Comma(f)), $("#bpm_net_of_mri").val(Comma(C)), $("#bpm_adjustment").val(Comma(q)), $("#bpm_takeout_date").val(y), $("#bpm_difference_of_approved_vs_loanable").val(Comma(F)), $("#bpm_pagibig_loanable_amount").val(Comma(p)), $("#bpm_movein_mri").val(Comma(h)), $("#bpm_others").val(Comma(k)), $("#bpm_refilling_fee").val(x), $("#bpm_pagibig_upgrading").val(N), $("#bpm_pagibig_loan_update").val(R), $("#bpm_collected_date").val(j), $("#bpm_deposited_date").val(E), $("#bpm_movein_amount").val(Comma(I))
    });

    var m = $("#current_user_type").val();
    "accountant" == m ? ($("#bpm_tcp").attr("readonly", 1), $("#bpm_loanable_amount").attr("readonly", 1), $("#bpm_discount").attr("readonly", 1)) : "assistant_accountant" == m && ($("#bpm_pagibig_approved_loanable_amount").attr("readonly", false), $("#bpm_adjustment").attr("readonly", !1))	 
	if(m=="admin"){
        $("#bpm_discount").attr("readonly", false); $("#bpm_pagibig_approved_loanable_amount").attr("readonly", false); 
     }

        if(m=="accountant"){
            $("#bpm_pagibig_approved_loanable_amount").attr("readonly", false);
        }

}
var mods = $("#main_page").val(),
    types = $("#main_type").val(),
    action = $("#main_action").val();
"payment" != mods || "buyer_payment_monitoring" != types || "edit" != action && "view" != action || load_buyer_payment_monitoring(), $("#bpm_loanable_amount").change(function() {
    var a = $("#bpm_loanable_amount").val();
    $("#bpm_pagibig_loanable_amount").val(Comma(a))
}), $("#bpm_payment_mode").change(function() {
    var a = $("#bpm_payment_mode").val();
    /*if ("cash" == a) {
        var e = $("#bpm_discount").val(),
            t = $("#bpm_loanable_amount").val(),
            m = $("#bpm_pagibig_loanable_amount").val(),
            o = $("#bpm_terms_of_payment").val();
        $("#bpm_discount_temp").val(e), $("#bpm_loanable_amount_temp").val(t), $("#bpm_pagibig_loanable_amount_temp").val(m), $("#bpm_terms_of_payment_temp").val(o), $("#bpm_discount").val(0), $("#bpm_loanable_amount").val(0), $("#bpm_pagibig_loanable_amount").val(0), $("#bpm_terms_of_payment").val(1), computeNetEquity()
    } else if ("bank" == a || "pag-ibig" == a || "in-house" == a) {
        var e = $("#bpm_discount_temp").val(),
            t = $("#bpm_loanable_amount_temp").val(),
            m = $("#bpm_pagibig_loanable_amount_temp").val(),
            o = $("#bpm_terms_of_payment_temp").val();
        $("#bpm_discount").val(e), $("#bpm_loanable_amount").val(t), $("#bpm_pagibig_loanable_amount").val(m), $("#bpm_terms_of_payment").val(o), computeNetEquity()
    }*/

    

    if ("cash" == a) {
        var e = $("#bpm_discount").val(),
            t = $("#bpm_loanable_amount").val(),
            m = $("#bpm_pagibig_loanable_amount").val(),
            o = $("#bpm_terms_of_payment").val();
        $("#bpm_discount_temp").val(e), $("#bpm_loanable_amount_temp").val(t), $("#bpm_pagibig_loanable_amount_temp").val(m), $("#bpm_terms_of_payment_temp").val(o), $("#bpm_discount").val(0), $("#bpm_loanable_amount").val(0), $("#bpm_pagibig_loanable_amount").val(0), $("#bpm_terms_of_payment").val(1), computeNetEquity()
    } else if ("instalments" == a) {
        var e = $("#bpm_discount_temp").val(),
            t = $("#bpm_loanable_amount_temp").val(),
            m = $("#bpm_pagibig_loanable_amount_temp").val(),
            o = $("#bpm_terms_of_payment_temp").val();
        $("#bpm_discount").val(e), $("#bpm_loanable_amount").val(t), $("#bpm_pagibig_loanable_amount").val(m), $("#bpm_terms_of_payment").val(o), computeNetEquity()


    }

});



