
function startAjaxAction(url) {
    globalStopProgress = false;
    var data = $("#addressbook-form").serialize();
    var nothingSelected = true;
    if ($('#selectAll').is(":checked")) {
        nothingSelected = false;
    } else {
        $("#addresses-grid")
                .find("input[type='checkbox']")
                .each(function (i, e) {
                    if ($(e).is(":checked"))
                    {
                        nothingSelected = false;
                    }
                });
    }
    if (nothingSelected === false)
    {
        $.ajax({
            type: "POST",
            url: url,
            data: data,
            dataType: "json",
            success: function (data) {
                setupProgressModal(data);
                makeRequestToAjaxAction(data);
            },
            error: function (e) {
                console.log("error");
            }
        });
    } else {
        $('#function').val(0);
    }
}

function makeRequestToAjaxAction(data) {
    $.ajax({
        type: "POST",
        url: data.requestUrl,
        data: data,
        dataType: "json",
        success: function (data) {
            updateProgressModal(data);
            if (data.stopProcess === "false" && globalStopProgress === false) {
                makeRequestToAjaxAction(data);
            } else {
                if (data.operation === "csvExport" && globalStopProgress === false) {
                    downloadCsvFile(data);
                }
                resetModal();
            }
        },
        error: function () {
            console.log("error in makeRequestToAjaxAction");
        }
    });
}

function setupProgressModal(data) {

    setProgressModalHeader(0, data.count);
    $("#progressBar .bar").width("0%");
    $("#progressBar > div").text("0%");
    $("#progressModal").modal("show");
}

function resetModal() {
    $('#function').prop('selectedIndex', 0)
    $("#progressBar .bar").width("0%");
    $("#progressBar > div").text("0%");
    $("#progressModal").modal("hide");
    $.fn.yiiGridView.update("addresses-grid");
}

function updateProgressModal(data) {
    if (data.operation == "copy" && data.sourceBookId == data.targetBookId) {
        setProgressModalHeader(Math.round((data.offset / 2)), (data.count / 2));
    } else {
        setProgressModalHeader(data.offset, data.count);
    }

    var progress = calculateProgress(data.offset, data.count);
    setProgressbarValue(progress);
}

function setProgressModalHeader(offset, count) {
    $("#progressBarModalHeader").text(offset + " von " + count); // FIXME localisation
}

function calculateProgress(offset, count) {
    var progress = (offset / count) * 100;
    return Math.round(progress);
}

function setProgressbarValue(progress) {
    $("#progressBar .bar").width(progress + "%");
    $("#progressBar > div").text(progress + "%");
}

$("#progressModal").on("hide", function () {
    console.log("Abbruch");
    globalStopProgress = true;

});


function stopProgress() {
    globalStopProgress = true;
}

function downloadCsvFile(data) {
    var downloadLink = data.downloadLink + "&startTime=" + data.startTime;
    window.location = downloadLink;
}