$(document).ready(function () {
    $('body').on('change', '#bankname', getRecpNo);
    $('body').on('blur', '.roe, .feamt,#amount', getamtwords);

    $('body').on('change blur', '.ratelakhs', calAmt);
    $('body').on('change', '.curr', getAmt);
    getAmt();
    function getAmt() {
        var curr = $(".curr").val();

        if (curr == "AED") {
            $(".aeddiv").show();
        } else {
            $(".aeddiv").hide();
        }
    }

    $("body").on("change keyup", ".feamt, .roe,.ratelakhs.commperc", calAmount);
    function calAmount() {
        var roe = $(".roe").val(),
                commmperc = $(".commperc").val(),
                feamt = $(".feamt").val(),
                amount = (roe * feamt).toFixed(2),
                commamt = (commmperc * amount / 100).toFixed(2);
        $(".amount").val(amount);
        $(".commamt").val(commamt);
    }

    function calAmt() {
        var ratelakhs = $(".ratelakhs").val();
        if (ratelakhs > 0) {
            var roe = (100000 / ratelakhs).toFixed(9);
            $(".roe").val(roe);
        }
        calAmount();
    }

//    ----------------------------------------------------------
    $('body').on('click', '.narr1, .narr2', FillNarrVal);

    function getamtwords() {
        var amount = $("#amount").val();
        if (amount > 0) {
            $.get('getamt_words.php',
                    {amount: amount}, function (data) {
                $('.amtinwords').val(data);
            });
        }

    }

    function getRecpNo() {
        var bankname = $("#bankname").val(),
                cashdb = "cashpay",
                vouchno = $("#vouchno").val();

        $.get('getRecpNo.php',
                {bankname: bankname, cashdb: cashdb, vouchno: vouchno}, function (data) {
            $('.recpno').val(data);

        });
    }
    //    -------------------------------------------------------

    $("#narrsrch").click(function () {
        $.get('getnarr.php', function (data) {
            $('.modalbody').html(data);
        });
        $("#NarrModal").modal('show');
    });


    $("#remsrch").keyup(function () {
        var soption = $(".remoptn").val(),
                ssearch = $("#remsrch").val();

        if (ssearch != '') {
            $.get('getnarr.php', {soption: soption, ssearch: ssearch}, function (data) {
                $('.modalbody').html(data);
            });
        } else {
            $.get('getnarr.php', function (data) {
                $('.modalbody').html(data);
            });
        }
    });


    function FillNarrVal() {
        var tr = $(this).closest("tr"),
                narr1 = tr.find(".narr1").text(),
                narr2 = tr.find(".narr2").text();
        $("#NarrModal").modal('hide');
        $(".remark1").val(narr1);
        $(".remark2").val(narr2);
    }
});