$(document).ready(function () {
    var elems = Array.prototype.slice.call(document.querySelectorAll('.switch-wizard-modal'));
    elems.forEach(function (html) {
        var switchery = new Switchery(html, { size: 'small' });
    });
});

function onCreatePriceChange() {
    $("#PriceChangeDateFrom").datepicker('setDate', new Date());
    $("#PriceChangeDateTo").datepicker('setDate', new Date());
    $("#PriceChangeFromDocTypes").val($("#DocType").val());
    $("#PriceChangeFromDocTypes").selectpicker('refresh');
    $("#wizardModal").modal('show');
}

function onStartPriceChange() {
    var dateFrom = $("#PriceChangeDateFrom").datepicker("getDate");
    var dateTo = $("#PriceChangeDateTo").datepicker("getDate");
    var dateMatOpen = $("#MoveNonMatOpenDate").datepicker("getDate");

    if (dateFrom < dateMatOpen || dateTo < dateMatOpen) {
        $().customModal({
            destroyOnClose: true,
            showCloseButton: true,
            type: "alert",
            text: stringFormat($("#TheIntangiblePeriodIsAlreadyOverLoc").val(), $("#MoveNonMatOpenDate").val()),
            buttons: [
                {
                    icon: 'fa fa-check',
                    text: $("#ConfirmLoc").val(),
                    closeOnAction: true,
                    type: 'primary'
                }
            ]
        });
        return;
    }

    if (!$("#PriceChangeDocType option:selected").val() || !$("#PriceChangeFromDocTypes").val()) {
        $().customModal({
            destroyOnClose: true,
            showCloseButton: true,
            type: "alert",
            text: $("#YouMustEnterCriteriaLoc").val(),
            buttons: [
                {
                    icon: 'fa fa-check',
                    text: $("#ConfirmLoc").val(),
                    closeOnAction: true,
                    type: 'primary'
                }
            ]
        });
        return;
    }

    var docTypesList = $("#PriceChangeFromDocTypes").val().toString().split(',');
    for (var i = 0; i < docTypesList.length; i++) {
        while (docTypesList[i].length < 4)
            docTypesList[i] = docTypesList[i] + '0';
    }

    $.post($("#CreatePriceChangeDoc").val(), {
        DateFrom: $("#PriceChangeDateFrom").val(),
        DateTo: $("#PriceChangeDateTo").val(),
        FromDocTypes: docTypesList.toString(),
        PCDocType: $("#PriceChangeDocType option:selected").val(),
        Cancel: $("#PriceChangeStornoCb").is(':checked')
    }).done(function (response) {
        if (response.AcKey !== "")
            location.href = $("#EditPriceChange").val() + '?id=' + response.AcKey;
        else if (response.AcKey === "")
            $().customModal({
                destroyOnClose: true,
                showCloseButton: true,
                type: "alert",
                text: $("#OperationFinishedLoc").val(),
                buttons: [
                    {
                        icon: 'fa fa-check',
                        text: $("#ConfirmLoc").val(),
                        closeOnAction: true,
                        type: 'primary'
                    }
                ]
            });
    });
}