function initializeReportsReceivedDocuments() {
    var getSearchData = function () {
        return {
            DateFrom: reformatDate($("#Filter_DateFrom").val()),
            DateTo: reformatDate($("#Filter_DateTo").val()),
            PartnerId: $("#Filter_PartnerId").val(),
            UserId: $("#Filter_UserId").val(),
            DocType: $("#Filter_DocType").val(),
        };
    };

    var select2AcDocType = $("#Filter_DocType").select2({
        allowClear: true,
        theme: "bootstrap",
        placeholder: $('#documentTypePlaceholder').val(),
        language: $('#i18n').val()
    }).on('change', function () {
        $('#grid-data').bootgrid('reload');
    }).on("select2:unselecting", function (e) {
        $(this).select2("val", "");
        e.preventDefault();
    });

    var select2Partner = $("#Filter_PartnerId").select2({
        ajax: {
            url: $("#Filter_PartnerId").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) {
                // parse the results into the format expected by Select2
                // since we are using custom formatting functions we do not need to
                // alter the remote JSON data, except to indicate that infinite
                // scrolling can be used
                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: $('#partnerPlaceholder').val(),
        theme: "bootstrap",
        escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
        minimumInputLength: 1,
        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 () {
        $('#grid-data').bootgrid('reload');
    }).on("select2:unselecting", function (e) {
        $(this).select2("val", "");
        e.preventDefault();
    });

    var select2User = $("#Filter_UserId").select2({
        ajax: {
            url: $("#Filter_UserId").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) {
                // parse the results into the format expected by Select2
                // since we are using custom formatting functions we do not need to
                // alter the remote JSON data, except to indicate that infinite
                // scrolling can be used
                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: $('#userPlaceholder').val(),
        theme: "bootstrap",
        escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
        minimumInputLength: 1,
        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 () {
        $('#grid-data').bootgrid('reload');
    }).on("select2:unselecting", function (e) {
        $(this).select2("val", "");
        e.preventDefault();
    });

    var grid = $("#grid-data").bootgrid({
        ajax: true,
        type: 'POST',
        formatters: {
            "InvoiceRef": function (column, row) {
                if (row["ExchangePackageId"] !== null)
                    return "<a href=\"Documents/EditLinked?id=" + row.ExchangePackageId + "\">" + row.InvoiceRef + "</a>";
                else
                    return row.InvoiceRef;
            }
        },
        converters: {
            decimalComma: decimalCommaBootgridConverter
        },
        templates: {
            search: "",
            header: ""
        },
        labels: {
            noResults: $("#bootgrid-noResults").val(),
            infos: $("#bootgrid-infos").val(),
            loading: $("#bootgrid-loading").val()

        },
        post: function () {
            /* To accumulate custom parameter with the request object */
            return getSearchData();
        },
    }).on("loaded.rs.jquery.bootgrid", function () {
        $(this).find("a").on("click", function (e) {
            interceptLinksForAjaxTabs(e, $(this));
        });
    });

    $("#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');
            $('#grid-data').bootgrid('reload');
        });
}

function reformatDate(dateString) {
    var dateElements = dateString.split('.');
    return dateElements[2] + '-' + dateElements[1] + '-' + dateElements[0];
}

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