function save_pricelist() {
    var t = $("#pricelist_detail_table input, select").serialize(),
        e = $("#main_page").val(),
        a = $("#main_type").val(),
        o = $("#main_action").val(),
        n = $("#main_id").val(),
        l = $("#pricelist_cash_discount").val(),
        i = $("#pricelist_terms").val(),
        r = $("#pricelist_terms_discount").val(),
        s = $("#current_project").val(),
        p = $("#current_model").val(),
        c = $("#pricelist_remarks").val(),
        //c = tinymce.get("pricelist_remarks").getContent(),
        d = $("#pricelist_updated_date").val(),
        m = $("#pricelist_reservation_fee").val().replace(/,/g, ""),
        u = $(".phase").val(),
        v = $(".block").val(),
        _ = $(".lot").val();
    if ("" != u && "" != v && "" != _) {
        var h = t + "&current_project=" + encodeURIComponent(s) +
            "&current_model=" + encodeURIComponent(p) +
            "&pricelist_remarks=" + encodeURIComponent(c) +
            "&pricelist_updated_date=" + encodeURIComponent(d) +
            "&pricelist_reservation_fee=" + encodeURIComponent(m) +
            "&action=" + encodeURIComponent(o) + "&mainid=" +
            encodeURIComponent(n) + "&cash_discount=" + encodeURIComponent(
                l) + "&terms=" + encodeURIComponent(i) + "&terms_discount=" +
            encodeURIComponent(r);
        $.post("./ajax/pricelist.php", h, function(t) {
            var o = $.parseJSON(t);
            1 == o.output ? (bootbox.alert("Successfully Saved ..."),
                window.setTimeout(function() {
                    window.location.href = "index.php?pg=" +
                        e + "&type=" + a
                }, 1e3)) : bootbox.alert("Failed in saving.")
        })
    } else bootbox.alert("Fields must not be blank")
}


function load_pricelist() {
    var t = ($("#main_page").val(), $("#main_type").val(), $("#main_action")
            .val()),
        e = $("#main_id").val(),
        a = "action=" + encodeURIComponent(t) + "&mainid=" +
        encodeURIComponent(e);
    $.post("./ajax/load_pricelist.php", a, function(e) {
        var a = $.parseJSON(e),
            o = (a.data[0].project, Comma(a.data[0].cash_discount)),
            n = a.data[0].terms,
            l = Comma(a.data[0].terms_discount),
            i = (a.data[0].model, a.data[0].pricelist_date),
            r = Comma(a.data[0].reservation_fee),
            s = a.data[0].remarks; 
            //s = a.data[0].remarks.replace("\n", "<br />");  
        $("#pricelist_cash_discount").val(o), $(
            "#pricelist_terms_discount").val(l), $(
            "#pricelist_terms").val(n), $(
            "#pricelist_updated_date").val(i), $(
            "#pricelist_reservation_fee").val(r), $(
            "#pricelist_remarks").text(s);
        for (var p = "", c = $.parseJSON(a.data[0].details), d = 0; d <
            c.details.length; d++) {
            var m = c.details[d].pricelist_status,
                u = c.details[d].detail_terms,
                v = c.details[d].lot_type,
                _ = c.details[d].phase,
                h = c.details[d].block,
                f = c.details[d].lot,
                x = c.details[d].lot_size,
                g = c.details[d].floor_area,
                y = c.details[d].contract_price,
                b = c.details[d].loanable_amount,
                //N = c.details[d].pf_net,
				N = Comma(parseFloat(c.details[d].pf_net.replace(/,/g, '')).toFixed(2)), 
                C = c.details[d].pf_payable,
                I = c.details[d].required_gross,
                k = c.details[d].amortization,
                R = "",
                O = "",
                F = "",
                E = "",
                j = "",
                G = "";
            "RFO" == m ? O = "selected" : "ON-GOING" == m ? F =
                "selected" : "UP-COMING" == m ? E = "selected" :
                "PRE-SELLING" == m && (R = "selected"), "INNER" ==
                v ? j = "selected" : "END" == v && (G = "selected"),
                loadSelect("detail_terms2", ".div_detail_terms" + d,
                    "", u), p += "<tr>", p +=
                '<td><span onClick="removeCurrentRow(this);" class="btn btn-xs btn-danger glyphicon glyphicon-trash"></span></td>',
                p +=
                '<td><select class="form-control text-left left pricelist_status" name="pricelist_status[]" onchange="computePF();"><option ' +
                R +
                ' value="PRE-SELLING">PRE-SELLING</option><option ' +
                O + ' value="RFO">RFO</option><option ' + F +
                ' value="ON-GOING">ON-GOING</option> </select></td>"', p +=
                '<td style="width:200px"><div class="div_detail_terms' +
                d + '"></div></td>', p +=
                '<td><input maxlength="6" type="text" class="form-control text-right phase" name="phase[]" value="' +
                _ + '"></td>', p +=
                '<td><input maxlength="10" type="text" class="form-control text-right block" name="block[]"  value="' +
                h + '"></td>', p +=
                '<td><input maxlength="10" type="text" class="form-control text-right lot" name="lot[]" value="' +
                f + '"></td>', p +=
                '<td><input maxlength="6" type="text" class="form-control text-right" name="lot_size[]" value="' +
                x +
                '" onkeypress="return isNumberKey(event)"></td>', p +=
                '<td><select class="form-control text-left left" name="lot_type[]"><option value="">-</option><option value="INNER" ' +
                j + ">Inner</option><option " + G +
                ' value="END">End</option></select></td>', p +=
                '<td><input maxlength="12"  type="text" class="form-control text-right" name="floor_area[]" value="' +
                g +
                '" onkeypress="return isNumberKey(event)"></td>', p +=
                '<td><input maxlength="12"  type="text" class="form-control text-right pricelist_contract_price" value="' +
                y +
                '" name="contract_price[]" onkeypress="return isNumberKey(event)" onblur = "javascript:this.value=Comma(this.value);"  onchange="computePF()"></td>',
                p +=
                '<td><input maxlength="12"  type="text" class="form-control text-right pricelist_loanable_amount" name="loanable_amount[]" value="' +
                b +
                '" onkeypress="return isNumberKey(event)" onblur = "javascript:this.value=Comma(this.value);computePF()" onchange="computePF()"></td>',
                p +=
                '<td><input maxlength="12"  type="text" class="form-control text-right pricelist_pf_net" name="pf_net[]" value="' +
                N +
                '" onkeypress="return isNumberKey(event)" onblur = "javascript:this.value=Comma(this.value);"></td>',
                p +=
                '<td><input maxlength="12" type="text" class="form-control text-right pf_payable" name="pf_payable[]" value="' +
                C +
                '" onkeypress="return isNumberKey(event)" onblur = "javascript:this.value=Comma(this.value);"></td>',
                p +=
                '<td><input maxlength="12" type="text" class="form-control text-right required_gross" name="required_gross[]" value="' +
                I +
                '" onkeypress="return isNumberKey(event)" onblur = "javascript:this.value=Comma(this.value);"></td>',
                p +=
                '<td><input maxlength="12" type="text" class="form-control text-right amortization" name="amortization[]" value="' +
                k +
                '" onkeypress="return isNumberKey(event)" onblur = "javascript:this.value=Comma(this.value);"></td>',
                p += "</tr>"
        }
		
		
        $("#pricelist_detail_table tbody").html(p), "view" == t &&
            ($("input").attr("disabled", !0), $("textarea").attr(
                    "disabled", !0), $("select").attr("disabled", !
                    0), $("#current_project").select2("disable"), $(
                    "#current_model").select2("disable"), $(
                    ".glyphicon-trash").hide(), $(".glyphicon-plus")
                .hide())
    })
}

function delete_pricelist(t) {
    var e = "pricelist",
        a = $("#main_page").val(),
        o = $("#main_type").val(),
        n = "mainid=" + encodeURIComponent(t) + "&transaction=" +
        encodeURIComponent(e);
    $.post("./ajax/delete_transaction.php", n, function(t) {
        var e = $.parseJSON(t);
        1 == e.output ? (bootbox.alert("Successfully Deleted ..."),
            window.setTimeout(function() {
                window.location.href = "index.php?pg=" + a +
                    "&type=" + o
            }, 1e3)) : (bootbox.alert("Failed in Deleting ..."),
            window.setTimeout(function() {
                window.location.href = "index.php?pg=" + a +
                    "&type=" + o
            }, 1e3))
    })
}

function getDefaultModelValues() {
    var t = $("#current_model").val(),
        e = "current_model=" + t;
    $.post("./ajax/load_default_model_values.php", e, function(t) {
        var e = $.parseJSON(t),
            a = e.data[0].remarks,
            o = e.data[0].reservation_fee;
			
        //tinyMCE.activeEditor.setContent(a), 
		$("#pricelist_remarks").val(a),
		$("#pricelist_reservation_fee").val(o)
    })
}

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

function computePF() {
    $("#pricelist_detail_table > tbody  > tr").each(function() {
        var t = $(this).find(".detail_terms option:selected").val(),
            e = $(this).find(".pricelist_contract_price").val().replace(
                /,/g, ""),
            a = $(this).find(".pricelist_loanable_amount").val().replace(
                /,/g, ""),
            o = $("#pricelist_reservation_fee").val().replace(/,/g,
                "");
        "" == e && (e = 0);
        var n = 0,
            l = $("#pricelist_terms").val();
        if (l == t) var n = $("#pricelist_terms_discount").val().replace(
            /,/g, "");
        if ("cash" == t) var n = $("#pricelist_cash_discount").val()
            .replace(/,/g, ""),
            i = 0;
        else var i = t; if (!t) var i = 0;
        "" == a && (a = 0);
        var r = parseFloat(e) - parseFloat(a) - o - n,
            s = Comma(r);
        $(this).find(".pricelist_pf_net").val(s);
        $(this).find(".pricelist_status option:selected").val();
        if ("cash" == t) var p = parseFloat(0);
        else var p = parseFloat(r) / parseInt(i); if (!t) var p =
            parseFloat(0);
        $(this).find(".pf_payable").val(Comma(p.toFixed(2)))
    })
}
$(document).ready(function() {
    var t = $("#main_page").val(),
        e = $("#main_type").val(),
        a = $("#main_action").val();
    "pricelist" != t || "pricelist" != e || "edit" != a && "view" != a ||
        load_pricelist(), $("#current_model").change(function() {
            getDefaultModelValues()
        }), $("#pricelist_detail_table_add").click(function(t) {
            t.preventDefault(), "create" == a &&
                getDefaultModelTableValues();
            var e = Math.floor(100 * Math.random() + 1);
            loadSelect("detail_terms", ".select_random" + e);
            var o = "";
			//<option value="UP-COMING">UP-COMING</option>
            o +=
                '<tr><td><span onClick="removeCurrentRow(this);" class="btn btn-xs btn-danger glyphicon glyphicon-trash"></span></td><td><select onchange="computePF();" class="form-control text-left left pricelist_status select2" name="pricelist_status[]"><option value="PRE-SELLING">PRE-SELLING</option><option value="RFO">RFO</option><option value="ON-GOING">ON-GOING</option></select></td><td><div class="div_detail_terms select_random' +
                e +
                '"></div></td><td><input maxlength="6" type="text" class="phase form-control text-right" name="phase[]"></td><td><input maxlength="10" type="text" class="form-control text-right block" name="block[]"></td><td><input maxlength="10" type="text" class="form-control text-right lot" name="lot[]"></td><td><input maxlength="6" type="text" class="form-control text-right" name="lot_size[]" onkeypress="return isNumberKey(event)"></td><td><select class="form-control text-left left" name="lot_type[]"><option value="">-</option><option value="INNER">Inner</option><option value="END">End</option></select></td><td><input maxlength="12"  type="text" class="form-control text-right" name="floor_area[]" onkeypress="return isNumberKey(event)"></td><td><input maxlength="12"  type="text" class="form-control text-right pricelist_contract_price" name="contract_price[]" onkeypress="return isNumberKey(event)" onblur = "javascript:this.value=Comma(this.value);"  onchange="computePF()"></td><td><input maxlength="12"  type="text" class="form-control text-right pricelist_loanable_amount" name="loanable_amount[]"  onchange="computePF()" onblur = "javascript:this.value=Comma(this.value);computePF()" onkeypress="return isNumberKey(event)"></td><td><input maxlength="12"  type="text" class="form-control text-right pricelist_pf_net" name="pf_net[]" onkeypress="return isNumberKey(event)" onblur = "javascript:this.value=Comma(this.value);"></td><td><input maxlength="12" type="text" class="form-control text-right pf_payable" name="pf_payable[]" onkeypress="return isNumberKey(event)" onblur = "javascript:this.value=Comma(this.value);"></td><td><input maxlength="12" type="text" class="form-control text-right required_gross" name="required_gross[]" onkeypress="return isNumberKey(event)" onblur = "javascript:this.value=Comma(this.value);"></td><td><input maxlength="12" type="text" class="form-control text-right amortization" name="amortization[]" onkeypress="return isNumberKey(event)" onblur = "javascript:this.value=Comma(this.value);"></td></tr>';
            $("#pricelist_detail_table tr").length;
            $("#pricelist_detail_table").append(o)
        })
});