$(document).ready(function () {
    $('body').on('change', '#bankname', getRecpNo);
    $("body").on("keyup", ".crsearch", getSearchData);
    $("body").on("click", ".crload", getSearchData);
    $("body").on("click", "#search", getSearchData);

    var addflag = $(".addflag").val(),
            par = $(".par").val();
    if (addflag == 1) {
        $(".chqretn").hide();
    } else {
        $(".chqretn").show();

    }

    $("form").submit(function () {
        var bankname = $(".bankname").val(),
                party = $(".party").val(),
                amount = $(".amount").val();
        if (bankname == '') {
            alert('Bank Name Cannot Be Empty');
            event.preventDefault();
        }
        if (party == '') {
            alert('A/c. Name Cannot Be Empty');
            event.preventDefault();

        }
        if (amount <= 0) {
            alert('Amount Cannot Be Zero');
            event.preventDefault();

        }
    });

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

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

        });
    }
    $(".tranmesg").hide();
//    var trnflag = $(".trnflag").val();
//    if (trnflag != "") {
//        $("#hedit").hide();
//        $("#hdelete").hide();
//        $(".tranmesg").show();
//        $(".trnmsg").val("Edit / Delete Not Allowed For this Receipt");

//    }







//////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".crnoRecord").hide();

    $(".searchModal").on('shown.bs.modal', function () {
        $(".crsearch").focus();
        $(".crsearch").val("");
        $(".crfield").val("1");
        getSearchData();
    });

    var smrangeFrom = 0,
            smrangeTo = 40;

    function getSearchData(pagination) {

        $(".crloader").show();

        if (pagination == 'prev') {
            if (smrangeFrom > 0) {
                smrangeFrom = (smrangeFrom - 40);
                smrangeTo = (smrangeTo - 40);
            }
        }
        if (pagination == 'next') {
            smrangeFrom = (smrangeFrom + 40);
            smrangeTo = (smrangeTo + 40);
        }

        var series = $(".series").val(),
                para = $(".para").val(),
                crsearch = $(".crsearch").val(),
                crfield = $(".crfield").val(),
                croperator = $(".croperator").val(),
                data = {crsearch: crsearch, crfield: crfield, croperator: croperator, para: para,
                    series: series, rangeFrom: smrangeFrom, rangeTo: smrangeTo},
                values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "bankpay_search.php",
            data: data,
            success: function (response) {
                $.each(response, function () {
                    values += "<tr class='clickable-row' data-href='" + this.action + "'>" +
                            "<td align='left'>" + this.vouchno + "</td>" +
                            "<td align='left'>" + this.vouchdt + "</td>" +
                            "<td align='left'>" + this.paidto + "</td>" +
                            "<td align='left'>" + this.chqno + "</td>" +
                            "<td align='left'>" + this.rtgsno + "</td>" +
                            "<td align='left'>" + this.name + "</td>" +
                            "<td align='left'>" + this.remark1 + "</td>" +
                            "<td align='left'><a href='" + this.action + "'><i class='fa fa-pencil' style='font-size:1.3em;cursor:pointer;'></i></a></td>" +
                            "</tr>";
                });
                $(".crloader").hide();
                $(".crTbody").empty();
                $(".crTbody").html(values);
                if (response) {
                    $(".search-action-bar").show();
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }
    $("#search").click(function () {
        getSearchData();
    });
    $(".crprev").click(function () {
        getSearchData('prev');
    });
    $(".crprev").click(function () {
        getSearchData('next');
    });


    //////////////////////////////////////////////Search Part - End  //////////////////////////////////////////////

});