function initializeReportsReceivedCashDocuments() {
    $(document).ready(function () {
        $("#Filter_DocType").trigger("change");

        $("select").off("change");
        $("select").on("change", function () {
            $.fn.dataTable.reload($dataTable);
        });

        var typingTimer;
        var doneTypingInterval = 600;
        $('#Keywords').off('keyup');
        $('#Keywords').on('keyup', function () {
            clearTimeout(typingTimer);
            typingTimer = setTimeout(function () { $.fn.dataTable.reload($dataTable); }, doneTypingInterval);
        });
    });

    var getSearchData = function () {
        return {
            DateFrom: reformatDate($("#Filter_DateFrom").val()),
            DateTo: reformatDate($("#Filter_DateTo").val()),
            DocType: $("#Filter_DocType").val(),
            PaymentTypeId: $("#Filter_PaymentTypeId").val(),
            ItemDisplayFilter: $("#CustomDateValue").val()
        };
    };

    $dataTable = $('#mainGrid');
    $dataTable.DataTable({
        ajax: {
            url: $dataTable.attr('data-url'),
            data: function (d) {
                d.Keywords = $("#Keywords").val();
                d.DateFrom = reformatDate($("#Filter_DateFrom").val());
                d.DateTo = reformatDate($("#Filter_DateTo").val());
                d.ItemDisplayFilter = $("#CustomDateValue").val();
                //d.DocType = $("#Filter_DocType").val();
                //d.PaymentTypeId = $("#Filter_PaymentTypeId").val();
            }
        },
        columns: [
            { data: "JournalRef", name: "CashReg.AcKey", filtertype: "text", render: $.fn.dataTable.render.link("JournalId") },
            { data: "JournalIdView", name: "CashReg.Ackey", filtertype: "text", render: $.fn.dataTable.render.link("JournalId") },
            { data: "Subject", name: "CashReg.AcSubject", filtertype: "text" },
            { data: "AcDocTypeExt", name: "CashReg.Acdoctype", filtertype: "text" },
            { data: "CashregClose", name: "CashReg.Addate", filtertype: "dateTime" },
            { data: "Amount", name: "CashReg.Anvalue", filtertype: "number", render: $.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()), className: 'dt-body-right' }
        ]
    }).on('xhr.dt', function (e, settings, json, xhr) {
        if (json && json.prop) {
            $('#grid-data-total-sum').html($.fn.dataTable.render.number('.', ',', $("#DefaultDecimalPlacesPrice").val()).display(json.prop));
            $('#grid-data-start-items').html(json.recordsTotal === 0 ? '0' : '1');
            $('#grid-data-total-items').html(json.recordsTotal);
        }
    });

    var select2DocType = $("#Filter_DocType").select2({
        ajax: {
            url: $("#Filter_DocType").data("url"),
            dataType: 'json',
            type: "POST",
            delay: 250,
            data: function (params) {
                return {
                    filter: params.term, // search term
                    page: params.page
                };
            },
            id: function (data) {
                return data.Value;
            },
            processResults: function (data, params) {
                params.page = params.page || 1;

                var select2Data = $.map(data.Items, function (obj) {
                    obj.id = obj.Value;
                    obj.text = obj.Text;

                    return obj;
                });

                return {
                    results: select2Data,
                    pagination: {
                        more: (params.page * 10) < data.TotalItems
                    }
                };
            },
            cache: true
        },
        allowClear: true,
        placeholder: $('#documentTypePlaceholder').val(),
        theme: "bootstrap",
        escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
        minimumInputLength: 0,
        templateResult: format, // omitted for brevity, see the source of this page
        templateSelection: function (item) { return item.name || item.text; }, // omitted for brevity, see the source of this page
        language: $('#i18n').val()
    }).on('change', function () {
        $.fn.dataTable.reload($dataTable);
    });

    var select2PaymentType = $("#Filter_PaymentTypeId").select2({
        ajax: {
            url: $("#Filter_PaymentTypeId").data("url"),
            dataType: 'json',
            type: "POST",
            delay: 250,
            data: function (params) {
                return {
                    filter: params.term, // search term
                    page: params.page
                };
            },
            id: function (data) {
                return data.Value;
            },
            processResults: function (data, params) {
                params.page = params.page || 1;

                var select2Data = $.map(data.Items, function (obj) {
                    obj.id = obj.Value;
                    obj.text = obj.Text;

                    return obj;
                });

                return {
                    results: select2Data,
                    pagination: {
                        more: (params.page * 10) < data.TotalItems
                    }
                };
            },
            cache: true
        },
        allowClear: true,
        placeholder: $('#paymentMethodPlaceholder').val(),
        theme: "bootstrap",
        escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
        minimumInputLength: 0,
        templateResult: format, // omitted for brevity, see the source of this page
        templateSelection: function (item) { return item.name || item.text; }, // omitted for brevity, see the source of this page
        language: $('#i18n').val()
    }).on('change', function () {
        $.fn.dataTable.reload($dataTable);
    });

    $("#downloadIssuedBills").off("click");
    $("#downloadIssuedBills").on("click", function () {
        var href = $(this).attr("href");
        var params = getSearchData();

        var form = $('<form id="exportForm" method="POST" style="display:none;"></form>');
        form.attr("action", $("#ExportUrl").val());
        var form_data = new FormData();

        for (var key in params) {
            var input = $("<input>")
                .attr("name", key).val(params[key]);
            form.append(input);
        }

        $("body").append(form);
        form.submit();
        $("#exportForm").remove();
    });

    $('.datepicker').datepicker()
        .on('click', function (e) {
            $(this).select();
        }).on('changeDate', function () {
            $(this).datepicker('hide');
            $.fn.dataTable.reload($dataTable);
        });
}

function format(item) { return item.Text; };