var PageIndex = 1;
function BindAccessionRegisterLIst() {
    $('[id$=MainUpdateProgress]').show();

    $.ajax({
        type: "POST",
        url: "/Pages/Library/AccessionRegisterSearch.aspx/BindAccessionRegisterLIst",
        data: '{"Code":"' + $('[id$=CodeTextBox]').val() + '","Title":"' + $('[id$=TitleTextBox]').val() + '","Supplier":"' + $('[id$=SupplierTextBox]').val() + '","Publisher":"' + $('[id$=PublisherTextBox]').val() + '","Author":"' + $('[id$=AuthorTextBox]').val() + '","Subject":"' + $('[id$=SubjectTextBox]').val() + '","Location":"' + $('[id$=LocationTextBox]').val() + '","Language":"' + $('[id$=LanguageTextBox]').val() + '","Edition":"' + $('[id$=EditionTextBox]').val() + '","ISBNNumber":"' + $('[id$=ISBNNumberTextBox]').val() + '","BookTag":"' + $('[id$=BookTagTextBox]').val() + '","BookCustomTag":"' + $('[id$=BookCustomTagTextBox]').val() + '","BookAgeGroupId":"' + $('[id$=AgeGroupDropDownList]').val() + '","ResourceTypeId":"' + $('[id$=ResourceTypeDropDownList]').val() + '","StatusId":"' + $('[id$=BookStatusDropDownList]').val() + '","BookDepartment":"' + $('[id$=BookDepartmentDropDownList]').val() + '","ClassificationNo":"' + $('[id$=ClassificationNoTextBox]').val() + '","AccessionNo":"' + $('[id$=AccessionNoTextBox]').val() + '","Paging":"' + PageIndex + '"}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            var datas = $.parseJSON(data.d);
            if (datas.Grid != '') {
                $('#tblList').html('');
                $('#trHeader').html('');
                $('#tblListExport_N').html('');
                $('#AccessionGridDiv').show();
                $('#trHeader').html(datas.Header);
                $('#tblList').html(datas.Grid);
                $('#pagging').html(datas.Paging);
               // $('#tblListExport_N').html(datas.ExpprtGrid);
              //  $('#trHeaderExport_N').html(datas.ExportHeader);
                window.setTimeout(function () { $('[id$=MainUpdateProgress]').hide(); }, 300);
            }
            else {
                $('#AccessionGridDiv').show();
                $('#trHeader').html("<tr><td>No Records found !</td></tr>");
                $('#tblList').html("");
                $('[id$=MainUpdateProgress]').hide();
            }
        },
        error: function (xhr, status, error) {
            $('[id$=MainUpdateProgress]').hide();
            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}
function ChangePage(pi) {
    PageIndex = pi;
    BindAccessionRegisterLIst();
}
$(document).ready(function () {
    function exportTableToCSV($table, filename) {
        var $headers = $table.find('tr:has(th)'), $rows = $table.find('tr:has(td)'), tmpColDelim = String.fromCharCode(11), tmpRowDelim = String.fromCharCode(0), colDelim = '","', rowDelim = '"\r\n"';
        var csv = '"';
        csv += formatRows($headers.map(grabRow));
        csv += rowDelim;
        csv += formatRows($rows.map(grabRow)) + '"';
        var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
        $(this).attr({
            'download': filename,
            'href': csvData
            //,'target' : '_blank' //if you want it to open in a new window
        });
        function formatRows(rows) {
            return rows.get().join(tmpRowDelim).split(tmpRowDelim).join(rowDelim).split(tmpColDelim).join(colDelim);
        }
        function grabRow(i, row) {
            var $row = $(row);
            var $cols = $row.find('td');
            if (!$cols.length) $cols = $row.find('th');
            return $cols.map(grabCol).get().join(tmpColDelim);
        }
        function grabCol(j, col) {
            var $col = $(col),
            $text = $col.text();
            return $text.replace('"', '""'); // escape double quotes	
        }
    }
    $("#ExportButton").click(function (event) {
        var outputFile = 'Accession-register'
        outputFile = outputFile.replace('.xls', '') + '.xls'
        exportTableToCSV.apply(this, [$('#dvExports_N>table'), outputFile]);
    });
});
