$(document).ready(function(){
    $("body").on('blur', '.init', checkInit);
    $("body").on("keyup", ".csearch", getSearchData);
    $("body").on("click", ".cload", getSearchData);
    $("body").on("click", "#search", getSearchData);
    
    function checkInit(){
        var init = $('.init').val();
        var accode = $('.accode').val();
        var type = "cash"
        $.post('check_init.php',{init: init, type: type, accode: accode},function(data){
            if(data == 1){
                alert("Init Already Exist");
                $(".init").val('');
            }
        });
    }
    
    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".cnoRecord").hide();
    $(".cloader").hide();

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

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".bloader").show();

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

        var series = $(".series").val(),
                vouchno = $(".vouchno").val(),
                isearch = $(".csearch").val(),
                ifield = $(".cfield").val(),
                ioperator = $(".coperator").val(),
                data = {isearch: isearch, ifield: ifield, ioperator: ioperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
        values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "cash_search.php",
            data: data,
            success: function (response) {
                $.each(response, function () {
                    values += "<tr class='clickable-row' data-href='" + this.action + "'>" +
                            "<td align='left'>" + this.name + "</td>" +
                            "<td align='left'>" + this.init + "</td>" +
                            "<td align='left'>" + this.openbal + "</td>" +
                            "<td align='left'><a href='" + this.action + "' ><i class='fa fa-pencil'></i></a></td>" +
                            "</tr>";
                });
                $(".cloader").hide();
                $(".cTbody").empty();
                $(".cTbody").html(values);
                if (response) {
                    $(".search-action-bar").show();
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }
    $("#csearch").click(function () {
        getSearchData();
    });
    $(".cprevious").click(function () {
        getSearchData('prev');
    });
    $(".cnext").click(function () {
        getSearchData('next');
    });


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

    
    
});