/* Minification failed. Returning unminified contents.
(1,17): run-time error CSS1031: Expected selector, found '='
(1,17): run-time error CSS1025: Expected comma or open brace, found '='
(4,27): run-time error CSS1031: Expected selector, found '='
(4,27): run-time error CSS1025: Expected comma or open brace, found '='
(6,10): run-time error CSS1031: Expected selector, found 'OpenPopupDodajKontrahentaPartial('
(6,10): run-time error CSS1025: Expected comma or open brace, found 'OpenPopupDodajKontrahentaPartial('
(23,10): run-time error CSS1031: Expected selector, found 'OpenPopupEdytujKontrahentaPartial('
(23,10): run-time error CSS1025: Expected comma or open brace, found 'OpenPopupEdytujKontrahentaPartial('
(45,10): run-time error CSS1031: Expected selector, found 'OpenPopupDodajZlecenieElementPartial('
(45,10): run-time error CSS1025: Expected comma or open brace, found 'OpenPopupDodajZlecenieElementPartial('
(69,10): run-time error CSS1031: Expected selector, found 'OpenPopupZlecenieElementModalPartial('
(69,10): run-time error CSS1025: Expected comma or open brace, found 'OpenPopupZlecenieElementModalPartial('
(87,10): run-time error CSS1031: Expected selector, found 'OpenPopupZdjecieSzczegolyByIdModalPartial('
(87,10): run-time error CSS1025: Expected comma or open brace, found 'OpenPopupZdjecieSzczegolyByIdModalPartial('
(106,10): run-time error CSS1031: Expected selector, found 'OpenPopupZdjecieSzczegolyByLpModalPartial('
(106,10): run-time error CSS1025: Expected comma or open brace, found 'OpenPopupZdjecieSzczegolyByLpModalPartial('
(125,10): run-time error CSS1031: Expected selector, found 'OpenPopupZdjecieWzorcoweSzczegolyModalPartial('
(125,10): run-time error CSS1025: Expected comma or open brace, found 'OpenPopupZdjecieWzorcoweSzczegolyModalPartial('
(143,10): run-time error CSS1031: Expected selector, found 'UploadZdjecie('
(143,10): run-time error CSS1025: Expected comma or open brace, found 'UploadZdjecie('
(228,10): run-time error CSS1031: Expected selector, found 'DeleteZdjecie('
(228,10): run-time error CSS1025: Expected comma or open brace, found 'DeleteZdjecie('
(300,10): run-time error CSS1031: Expected selector, found 'OpenPopupDodajZlecenieNotatkaPartial('
(300,10): run-time error CSS1025: Expected comma or open brace, found 'OpenPopupDodajZlecenieNotatkaPartial('
(316,10): run-time error CSS1031: Expected selector, found 'OpenPopupZlecenieNotatkaModalPartial('
(316,10): run-time error CSS1025: Expected comma or open brace, found 'OpenPopupZlecenieNotatkaModalPartial('
(334,10): run-time error CSS1031: Expected selector, found 'OpenPopupZlecenieNotatkaOdpKlientaModalPartial('
(334,10): run-time error CSS1025: Expected comma or open brace, found 'OpenPopupZlecenieNotatkaOdpKlientaModalPartial('
(353,10): run-time error CSS1031: Expected selector, found 'SavePopupKontrahentModalPartial('
(353,10): run-time error CSS1025: Expected comma or open brace, found 'SavePopupKontrahentModalPartial('
(432,15): run-time error CSS1031: Expected selector, found '='
(432,15): run-time error CSS1025: Expected comma or open brace, found '='
(433,15): run-time error CSS1031: Expected selector, found '='
(433,15): run-time error CSS1025: Expected comma or open brace, found '='
(435,10): run-time error CSS1031: Expected selector, found 'SetKodNabywca('
(435,10): run-time error CSS1025: Expected comma or open brace, found 'SetKodNabywca('
(453,10): run-time error CSS1031: Expected selector, found 'SetKodOdbiorca('
(453,10): run-time error CSS1025: Expected comma or open brace, found 'SetKodOdbiorca('
(471,10): run-time error CSS1031: Expected selector, found 'SavePopupZlecenieElementModalPartial('
(471,10): run-time error CSS1025: Expected comma or open brace, found 'SavePopupZlecenieElementModalPartial('
(561,10): run-time error CSS1031: Expected selector, found 'DeletePopupZlecenieElementModalPartial('
(561,10): run-time error CSS1025: Expected comma or open brace, found 'DeletePopupZlecenieElementModalPartial('
(628,10): run-time error CSS1031: Expected selector, found 'SavePopupZlecenieNotatkaModalPartial('
(628,10): run-time error CSS1025: Expected comma or open brace, found 'SavePopupZlecenieNotatkaModalPartial('
(716,10): run-time error CSS1031: Expected selector, found 'DeletePopupZlecenieNotatkaModalPartial('
(716,10): run-time error CSS1025: Expected comma or open brace, found 'DeletePopupZlecenieNotatkaModalPartial('
(782,10): run-time error CSS1031: Expected selector, found 'SavePopupZlecenieNotatkaOdpKlientaModalPartial('
(782,10): run-time error CSS1025: Expected comma or open brace, found 'SavePopupZlecenieNotatkaOdpKlientaModalPartial('
(868,10): run-time error CSS1031: Expected selector, found 'ShowPopupPartial('
(868,10): run-time error CSS1025: Expected comma or open brace, found 'ShowPopupPartial('
(876,10): run-time error CSS1031: Expected selector, found 'ClosePopupPartial('
(876,10): run-time error CSS1025: Expected comma or open brace, found 'ClosePopupPartial('
(880,10): run-time error CSS1031: Expected selector, found 'UpdateDaneTowar('
(880,10): run-time error CSS1025: Expected comma or open brace, found 'UpdateDaneTowar('
(913,10): run-time error CSS1031: Expected selector, found 'UpdateWartosc('
(913,10): run-time error CSS1025: Expected comma or open brace, found 'UpdateWartosc('
(932,10): run-time error CSS1031: Expected selector, found 'ShowLoadingModal('
(932,10): run-time error CSS1025: Expected comma or open brace, found 'ShowLoadingModal('
(938,10): run-time error CSS1031: Expected selector, found 'HideLoadingModal('
(938,10): run-time error CSS1025: Expected comma or open brace, found 'HideLoadingModal('
(945,10): run-time error CSS1031: Expected selector, found 'GetLoadingImgModal('
(945,10): run-time error CSS1025: Expected comma or open brace, found 'GetLoadingImgModal('
 */
var sizeModalMd = '600'
var sizeModalLg = '1100'
var dataModalLoadingTemp = '';
var modal_TwrIdOptimaLoad = false;

function OpenPopupDodajKontrahentaPartial() {
    $.ajax({
        type: "GET",
        async: false,
        url: urlNowyKontrahentModalPartial,
        contentType: "application/json; charset=utf-8",
        data: {},
        datatype: "json",
        success: function (data) {
            ShowPopupPartial(data, sizeModalLg);
        },
        error: function () {
            alert("Nie udało się załadować okna.");
        }
    });
}

function OpenPopupEdytujKontrahentaPartial() {

    var kntId = PodIdOptimaCmb.GetValue();

    $.ajax({
        type: "GET",
        async: false,
        url: urlEdytujKontrahentModalPartial,
        contentType: "application/json; charset=utf-8",
        data: {
            "idKnt": kntId,
        },
        datatype: "json",
        success: function (data) {
            ShowPopupPartial(data, sizeModalLg);
        },
        error: function () {
            alert("Nie udało się załadować okna.");
        }
    });
}

function OpenPopupDodajZlecenieElementPartial(dodatkowe) {
    $.ajax({
        type: "GET",
        url: urlDodajZlecenieElementPartial,
        contentType: "application/json; charset=utf-8",
        data: {
            "dodatkowe": dodatkowe
        },
        datatype: "json",
        success: function (data) {
            try {
                ShowPopupPartial(data, sizeModalMd);
                UpdateDaneTowar();
            }
            catch (e) {
                alert(e)
            }
        },
        error: function () {
            alert("Nie udało się załadować okna.");
        }
    });
}

function OpenPopupZlecenieElementModalPartial(lp) {
    $.ajax({
        type: "GET",
        url: urlZlecenieElementModalPartial,
        contentType: "application/json; charset=utf-8",
        data: {
            "lp": lp
        },
        datatype: "json",
        success: function (data) {
            ShowPopupPartial(data, sizeModalMd);
        },
        error: function () {
            alert("Nie udało się załadować okna.");
        }
    });
}

function OpenPopupZdjecieSzczegolyByIdModalPartial(idZdj, editable) {
    $.ajax({
        type: "GET",
        url: urlZdjecieSzczegolyByIdModalPartial,
        contentType: "application/json; charset=utf-8",
        data: {
            "idZdj": idZdj,
            "editable": editable
        },
        datatype: "json",
        success: function (data) {
            ShowPopupPartial(data, sizeModalMd);
        },
        error: function () {
            alert("Nie udało się załadować okna.");
        }
    });
}

function OpenPopupZdjecieSzczegolyByLpModalPartial(lp, editable) {
    $.ajax({
        type: "GET",
        url: urlZdjecieSzczegolyByLpModalPartial,
        contentType: "application/json; charset=utf-8",
        data: {
            "lp": lp,
            "editable": editable
        },
        datatype: "json",
        success: function (data) {
            ShowPopupPartial(data, sizeModalMd);
        },
        error: function () {
            alert("Nie udało się załadować okna.");
        }
    });
}

function OpenPopupZdjecieWzorcoweSzczegolyModalPartial(idDab) {
    $.ajax({
        type: "GET",
        url: urlZdjecieWzorcoweSzczegolyModalPartial,
        contentType: "application/json; charset=utf-8",
        data: {
            "dabId": idDab
        },
        datatype: "json",
        success: function (data) {
            ShowPopupPartial(data, sizeModalMd);
        },
        error: function () {
            alert("Nie udało się załadować okna.");
        }
    });
}

function UploadZdjecie(typ) {
    try {
        var idZlecenie = document.getElementById('IdZlecenie').value;

        var f = $("#File" + typ);

        var formData = new FormData();

        if (f.prop('files').length == 0) {
            alert('Ne wybrano plików');
            return;
        }

        var length = 0;
        var max_length = 10737400;

        for (var i = 0; i < f.prop('files').length; i++) {
            length = length + f.prop('files')[i].size;
            formData.append('files', f.prop('files')[i]);
        }

        formData.append('typ', typ);
        formData.append('idZlecenie', idZlecenie);

        if (length > max_length) {
            alert("Za duży rozmiar plików (" + length + "/" + max_length + " b)!");
            return;
        }

        $.ajax({
            type: "POST",
            url: urlDodajZdjeciePartial,
            data: formData,
            processData: false,
            contentType: false,
            cache: false,
            success: function (message) {
                if (message != null && message != '') {
                    showAjaxError('#errorMessage', message, true);
                    return;
                }

                $.ajax({
                    type: "GET",
                    url: urlZlecenieZdjeciaPartial,
                    contentType: "application/json; charset=utf-8",
                    data: {
                        "typ": typ,
                        "editable": true
                    },
                    datatype: "json",
                    success: function (data) {
                        if (typ == 2 || typ == 'ZPrzyjecia')
                            $('#zdjeciaPrzyjecie').html(data);
                        if (typ == 3 || typ == 'ZNaprawy')
                            $('#zdjeciaNaprawa').html(data);
                        if (typ == 4 || typ == 'PrzedSpakowaniem')
                            $('#zdjeciaPrzedSpakowaniem').html(data);
                        if (typ == 5 || typ == 'PoSpakowaniu')
                            $('#zdjeciaPoSpakowaniu').html(data);
                    },
                    error: function () {
                        alert("Nie udało się załadować listy zdjęć.");
                    }
                });
            },
            error: function (xhr, status, error) {
                var message = xhr.responseText;

                if (message == null || message == '') {
                    alert("Nie udało się usunąć zdjęcia.");
                }
                else {
                    showAjaxError('#errorMessage', message, true);
                }

                return;
            }
        });
    }
    catch (err) {
        alert('UploadZdjecie: ' + err)
    }
}

function DeleteZdjecie() {
    try {
        if (confirm('Czy na pewno chcesz usunąć to zdjęcie?') == false)
            return;

        var lp = document.getElementById('modal_Lp').value;
        var typ = document.getElementById('modal_Typ').value;
        var idZlecenie = document.getElementById('IdZlecenie').value;

        var form = $('#mainForm');
        var token = $('input[name="__RequestVerificationToken"]', form).val();

        $.ajax({
            type: "POST",
            url: urlUsunZdjeciePartial,
            data: {
                "lp": lp,
                "idZlecenie": idZlecenie,
                "__RequestVerificationToken": token
            },
            success: function (message) {

                if (message != null && message != '') {
                    showAjaxError('#errorMessageModal', message, true);
                    return;
                }

                $.ajax({
                    type: "GET",
                    url: urlZlecenieZdjeciaPartial,
                    contentType: "application/json; charset=utf-8",
                    data: {
                        "typ": typ,
                        "editable": true
                    },
                    datatype: "json",
                    success: function (data) {
                        if (typ == 2 || typ == 'ZPrzyjecia')
                            $('#zdjeciaPrzyjecie').html(data);
                        if (typ == 3 || typ == 'ZNaprawy')
                            $('#zdjeciaNaprawa').html(data);
                        if (typ == 4 || typ == 'PrzedSpakowaniem')
                            $('#zdjeciaPrzedSpakowaniem').html(data);
                        if (typ == 5 || typ == 'PoSpakowaniu')
                            $('#zdjeciaPoSpakowaniu').html(data);

                        ClosePopupPartial();
                    },
                    error: function () {
                        alert("Nie udało się załadować listy zdjęć.");
                    }
                });
            },
            error: function (xhr, status, error) {
                var message = xhr.responseText;

                if (message == null || message == '') {
                    alert("Nie udało się usunąć zdjęcia.");
                }
                else {
                    showAjaxError('#errorMessage', message, true);
                }

                return;
            }
        });
    }
    catch (err) {
        alert('DeleteZdjecie: ' + err)
    }
}

function OpenPopupDodajZlecenieNotatkaPartial() {
    $.ajax({
        type: "GET",
        url: urlDodajZlecenieNotatkaPartial,
        contentType: "application/json; charset=utf-8",
        data: {},
        datatype: "json",
        success: function (data) {
            ShowPopupPartial(data, sizeModalMd);
        },
        error: function () {
            alert("Nie udało się załadować okna.");
        }
    });
}

function OpenPopupZlecenieNotatkaModalPartial(lp) {
    $.ajax({
        type: "GET",
        url: urlZlecenieNotatkaModalPartial,
        contentType: "application/json; charset=utf-8",
        data: {
            "lp": lp
        },
        datatype: "json",
        success: function (data) {
            ShowPopupPartial(data, sizeModalMd);
        },
        error: function () {
            alert("Nie udało się załadować okna.");
        }
    });
}

function OpenPopupZlecenieNotatkaOdpKlientaModalPartial(lp) {
    $.ajax({
        type: "GET",
        url: urlZlecenieNotatkaOdpKlientaModalPartial,
        contentType: "application/json; charset=utf-8",
        data: {
            "lp": lp
        },
        datatype: "json",
        success: function (data) {
            ShowPopupPartial(data, sizeModalMd);
        },
        error: function () {
            alert("Nie udało się załadować okna.");
        }
    });
}


function SavePopupKontrahentModalPartial() {
    try {
        var errormessage = KontrahentValidate()

        if (errormessage != null && errormessage != '') {
            showAjaxError('#errorMessageModal', errormessage, true);
            return;
        }

        var idKnt = document.getElementById('modal_KntId').value;
        var kod = document.getElementById('modal_Kod').value;
        var nazwa = document.getElementById('modal_Nazwa').value;
        var nip = document.getElementById('modal_NIP').value;
        var telefon = document.getElementById('modal_Telefon').value;
        var email = document.getElementById('modal_Email').value;
        var ulica = document.getElementById('modal_Ulica').value;
        var nrDomu = document.getElementById('modal_NrDomu').value;
        var nrLokalu = document.getElementById('modal_NrLokalu').value;
        var kodPocztowy = document.getElementById('modal_KodPocztowy').value;
        var miasto = document.getElementById('modal_Miasto').value;
        var kraj = document.getElementById('modal_Kraj').value;
        var typKnt = document.getElementById('modal_TypKontrahentaOF').checked ? 1 : 0;

        if (idKnt == null || idKnt == "") idKnt = 0;

        $('#errorMessageModal').html('');

        ShowLoadingModal();

        var form = $('#mainForm');
        var token = $('input[name="__RequestVerificationToken"]', form).val();

        $.ajax({
            type: "POST",
            url: urlSaveKontrahentPartial,
            data: {
                "idKnt": idKnt,
                "kod": kod,
                "nazwa": nazwa,
                "nip": nip,
                "telefon": telefon,
                "email": email,
                "ulica": ulica,
                "nrDomu": nrDomu,
                "nrLokalu": nrLokalu,
                "kodPocztowy": kodPocztowy,
                "miasto": miasto,
                "kraj": kraj,
                "typKnt": typKnt,
                "__RequestVerificationToken": token
            },
            datatype: "json",
            success: function (message) {

                if (message != null && message != '') {
                    HideLoadingModal();
                    showAjaxError('#errorMessageModal', message, true);
                    return;
                }

                kodKntNab = kod;
                kodKntOdb = kod;

                PodIdOptimaCmb.PerformCallback();
                OdbIdOptimaCmb.PerformCallback();

                ClosePopupPartial();
            },
            error: function () {
                HideLoadingModal();
                alert("Nie udało się załadować okna.");
            }
        });
    } catch (e) {
        HideLoadingModal();
        alert(e);
    }
}

var kodKntNab = '';
var kodKntOdb = '';

function SetKodNabywca() {
    if (kodKntNab != '') {
        for (var i = 0; i < PodIdOptimaCmb.GetItemCount(); i++) {
            var item = PodIdOptimaCmb.GetItem(i);
            if (item.GetColumnText('Kod').toLowerCase() == kodKntNab.toLowerCase()) {
                var id = item.GetColumnText('KntId');
                PodIdOptimaCmb.SetValue(id);
                document.getElementById('PodIdOptima').value = id;

                break;
            }
        }

        UpdateNabywca();
        kodKntNab = '';
    }
}

function SetKodOdbiorca() {
    if (kodKntOdb != '') {
        for (var i = 0; i < OdbIdOptimaCmb.GetItemCount(); i++) {
            var item = OdbIdOptimaCmb.GetItem(i);
            if (item.GetColumnText('Kod').toLowerCase() == kodKntOdb.toLowerCase()) {
                var id = item.GetColumnText('KntId');
                OdbIdOptimaCmb.SetValue(id);
                document.getElementById('OdbIdOptima').value = id;

                break;
            }
        }

        UpdateOdbiorca();
        kodKntOdb = '';
    }
}

function SavePopupZlecenieElementModalPartial() {
    try {
        var errormessage = ZlecenieElementValidate()

        if (errormessage != null && errormessage != '') {
            showAjaxError('#errorMessageModal', errormessage, true);
            return;
        }

        var idZlecenie = document.getElementById('IdZlecenie').value;
        var dodatkowe = document.getElementById('modal_Dodatkowy').value;
        var lp = document.getElementById('modal_Lp').value;
        var twrId = document.getElementById('modal_TwrIdOptima').value;
        var ilosc = document.getElementById('modal_Ilosc').value;
        var iloscPocz = document.getElementById('modal_IloscPocz').value;
        var cena = document.getElementById('modal_Cena').value;
        var pdsId = document.getElementById('modal_PdSIdOptima').value;

        if (ilosc.includes('.')) ilosc = ilosc.replace('.', ',');
        if (iloscPocz.includes('.')) iloscPocz = iloscPocz.replace('.', ',');
        if (cena.includes('.')) cena = cena.replace('.', ',');

        $('#errorMessageModal').html('');
        ShowLoadingModal();

        var form = $('#mainForm');
        var token = $('input[name="__RequestVerificationToken"]', form).val();

        $.ajax({
            type: "POST",
            url: urlSaveZlecenieElementPartial,
            data: {
                "lp": lp,
                "idZlecenie": idZlecenie,
                "twrId": twrId,
                "ilosc": ilosc,
                "iloscPocz": iloscPocz,
                "cenaBrutto": cena,
                "dodatkowe": dodatkowe,
                "pdsId": pdsId,
                "__RequestVerificationToken": token
            },
            datatype: "json",
            success: function (message) {

                if (message != null && message != '') {
                    HideLoadingModal();
                    showAjaxError('#errorMessageModal', message, true);
                    return;
                }

                $.ajax({
                    type: "GET",
                    url: urlZlecenieElementyPartial,
                    contentType: "application/json; charset=utf-8",
                    data: {
                        "dodatkowe": dodatkowe,
                        "editable": true
                    },
                    datatype: "json",
                    success: function (data) {
                        if (dodatkowe == 'True') {
                            $('#elementyDodatkowe').html(data);
                            UpdateWartoscZlecenie();
                        }
                        else {
                            $('#elementyPodstawowe').html(data);
                        }

                    },
                    error: function () {
                        HideLoadingModal();
                        alert("Nie udało się załadować listy pozycji.");
                    }
                });

                ClosePopupPartial();
            },
            error: function () {
                HideLoadingModal();
                alert("Nie udało się załadować okna.");
            }
        });
    } catch (e) {
        HideLoadingModal();
        alert(e);
    }

}

function DeletePopupZlecenieElementModalPartial() {
    try {
        var idZlecenie = document.getElementById('IdZlecenie').value;
        var lp = document.getElementById('modal_Lp').value;
        var dodatkowe = document.getElementById('modal_Dodatkowy').value;

        if (confirm('Czy na pewno chcesz usunąć tę pozycję?')) {

            ShowLoadingModal();

            var form = $('#mainForm');
            var token = $('input[name="__RequestVerificationToken"]', form).val();

            $.ajax({
                type: "POST",
                url: urlUsunZlecenieElementPartial,
                data: {
                    "lp": lp,
                    "dodatkowe": dodatkowe,
                    "idZlecenie": idZlecenie,
                    "__RequestVerificationToken": token
                },
                datatype: "json",
                success: function (message) {

                    if (message != null && message != '') {
                        HideLoadingModal();
                        showAjaxError('#errorMessageModal', message, true);
                        return;
                    }
                    $.ajax({
                        type: "GET",
                        url: urlZlecenieElementyPartial,
                        contentType: "application/json; charset=utf-8",
                        data: {
                            "dodatkowe": dodatkowe,
                            "editable": true
                        },
                        datatype: "json",
                        success: function (data) {
                            if (dodatkowe == 'True') {
                                $('#elementyDodatkowe').html(data);
                                UpdateWartoscZlecenie();
                            }
                            else {
                                $('#elementyPodstawowe').html(data);
                            }
                        },
                        error: function () {
                            alert("Nie udało się załadować listy pozycji.");
                        }
                    });

                    ClosePopupPartial();
                },
                error: function () {
                    HideLoadingModal();
                    alert("Nie udało się załadować okna.");
                }
            });
        }
    } catch (e) {
        HideLoadingModal();
        alert(e);
    }
}

function SavePopupZlecenieNotatkaModalPartial() {
    try {
        var errormessage = ZlecenieNotatkaValidate()

        if (errormessage != null && errormessage != '') {
            showAjaxError('#errorMessageModal', errormessage, true);
            return;
        }

        var idZlecenie = document.getElementById('IdZlecenie').value;
        var lp = document.getElementById('modal_Lp').value;
        var tresc = document.getElementById('modal_Tresc').value;
        var decKl = document.getElementById('modal_WymaganaDecyzjaKlienta').checked;
        var odpKl = document.getElementById('modal_OdpowiedzKlienta').value;

        $('#errorMessageModal').html('');

        ShowLoadingModal();

        var form = $('#mainForm');
        var token = $('input[name="__RequestVerificationToken"]', form).val();

        $.ajax({
            type: "POST",
            url: urlSaveZlecenieNotatkaPartial,
            data: {
                "lp": lp,
                "idZlecenie": idZlecenie,
                "tresc": tresc,
                "decKl": decKl,
                "odpKl": odpKl,
                "__RequestVerificationToken": token
            },
            datatype: "json",
            success: function (message) {

                if (message != null && message != '') {
                    HideLoadingModal();
                    showAjaxError('#errorMessageModal', message, true);
                    return;
                }

                $.ajax({
                    type: "GET",
                    url: urlZlecenieNotatkiPartial,
                    contentType: "application/json; charset=utf-8",
                    data: {
                        "editable": true,
                    },
                    datatype: "json",
                    success: function (data) {
                        $('#notatki').html(data);
                        var elems = document.getElementsByTagName("button");

                        for (var i = 0; i < elems.length; i++) {
                            elems[i].addEventListener("click", function (event) {
                                event.stopPropagation();
                            }, true);
                        }
                    },
                    error: function () {
                        alert("Nie udało się załadować listy notatek.");
                    }
                });

                if (decKl) {
                    if (document.getElementById('Status') != null) {
                        document.getElementById('Status').selectedIndex = 2;
                    }
                    if (document.getElementById('StatusString') != null) {
                        document.getElementById('StatusString').value = 'Czeka na decyzję klienta';
                    }
                }

                ClosePopupPartial();
            },
            error: function () {
                HideLoadingModal();
                alert("Nie udało się załadować okna.");
            }
        });
    } catch (e) {
        HideLoadingModal();
        alert(e);
    }

}

function DeletePopupZlecenieNotatkaModalPartial() {
    try {
        var idZlecenie = document.getElementById('IdZlecenie').value;
        var lp = document.getElementById('modal_Lp').value;

        if (confirm('Czy na pewno chcesz usunąć tę notatkę?')) {

            ShowLoadingModal();

            var form = $('#mainForm');
            var token = $('input[name="__RequestVerificationToken"]', form).val();

            $.ajax({
                type: "POST",
                url: urlUsunZlecenieNotatkaPartial,
                data: {
                    "lp": lp,
                    "idZlecenie": idZlecenie,
                    "__RequestVerificationToken": token
                },
                datatype: "json",
                success: function (message) {

                    if (message != null && message != '') {
                        HideLoadingModal();
                        showAjaxError('#errorMessageModal', message, true);
                        return;
                    }

                    $.ajax({
                        type: "GET",
                        url: urlZlecenieNotatkiPartial,
                        contentType: "application/json; charset=utf-8",
                        data: {
                            "editable": true,
                        },
                        datatype: "json",
                        success: function (data) {
                            $('#notatki').html(data);
                            var elems = document.getElementsByTagName("button");

                            for (var i = 0; i < elems.length; i++) {
                                elems[i].addEventListener("click", function (event) {
                                    event.stopPropagation();
                                }, true);
                            }
                        },
                        error: function () {
                            alert("Nie udało się załadować listy notatek.");
                        }
                    });

                    ClosePopupPartial();
                },
                error: function () {
                    HideLoadingModal();
                    alert("Nie udało się załadować okna.");
                }
            });
        }
    } catch (e) {
        HideLoadingModal();
        alert(e);
    }
}

function SavePopupZlecenieNotatkaOdpKlientaModalPartial() {
    try {
        var errormessage = ZlecenieNotatkaOdpKlValidate()

        if (errormessage != null && errormessage != '') {
            showAjaxError('#errorMessageModal', errormessage, true);
            return;
        }

        var idZlecenie = document.getElementById('IdZlecenie').value;
        var lp = document.getElementById('modal_Lp').value;
        var odpKl = document.getElementById('modal_OdpowiedzKlienta').value;
        var zmienWNaprawie = document.getElementById('modal_ZmienNaWNaprawie').checked

        $('#errorMessageModal').html('');

        ShowLoadingModal();

        var form = $('#mainForm');
        var token = $('input[name="__RequestVerificationToken"]', form).val();

        $.ajax({
            type: "POST",
            url: urlSaveZlecenieNotatkaOdpKlientaPartial,
            data: {
                "lp": lp,
                "idZlecenie": idZlecenie,
                "odpKl": odpKl,
                "zmienWNaprawie": zmienWNaprawie,
                "__RequestVerificationToken": token
            },
            datatype: "json",
            success: function (message) {

                if (message != null && message != '') {
                    HideLoadingModal();
                    showAjaxError('#errorMessageModal', message, true);
                    return;
                }

                $.ajax({
                    type: "GET",
                    url: urlZlecenieNotatkiPartial,
                    contentType: "application/json; charset=utf-8",
                    data: {
                        "editable": true,
                    },
                    datatype: "json",
                    success: function (data) {
                        $('#notatki').html(data);
                        var elems = document.getElementsByTagName("button");

                        for (var i = 0; i < elems.length; i++) {
                            elems[i].addEventListener("click", function (event) {
                                event.stopPropagation();
                            }, true);
                        }
                    },
                    error: function () {
                        alert("Nie udało się załadować listy notatek.");
                    }
                });

                if (zmienWNaprawie) {
                    if (document.getElementById('Status') != null) {
                        document.getElementById('Status').selectedIndex = 1;
                    }
                    if (document.getElementById('StatusString') != null) {
                        document.getElementById('StatusString').value = 'W naprawie';
                    }
                }

                ClosePopupPartial();
            },
            error: function () {
                HideLoadingModal();
                alert("Nie udało się załadować okna.");
            }
        });
    } catch (e) {
        HideLoadingModal();
        alert(e);
    }

}

function ShowPopupPartial(html, width) {
    popup.SetContentHtml(html);
    MVCxClientUtils.FinalizeCallback();
    popup.SetWidth(width);
    //popup.SetHeaderText(header);
    popup.Show();
}

function ClosePopupPartial() {
    popup.Hide()
}

function UpdateDaneTowar() {
    try {
        var twrId = document.getElementById('modal_TwrIdOptima').value;

        if (twrId != 0) {
            $.ajax({
                cache: false,
                type: "GET",
                url: urlGetDaneTowar,
                data: { "twrId": twrId },
                success: function (data) {
                    var cena = parseFloat(data.Cena).toFixed(2).replace('.', ',');
                    document.getElementById('modal_TwrNazwa').value = data.Nazwa;
                    document.getElementById('modal_Cena').value = cena;
                    document.getElementById('modal_JM').value = data.Twr_JM;
                    document.getElementById('modal_JM1').innerHTML = data.Twr_JM;
                    document.getElementById('modal_JM2').innerHTML = data.Twr_JM;
                    document.getElementById('modal_Waluta').value = data.Waluta;
                    document.getElementById('modal_Waluta1').innerHTML = data.Waluta;
                    document.getElementById('modal_Waluta2').innerHTML = data.Waluta;
                    UpdateWartosc();
                },
                error: function () {
                    alert("UpdateDaneTowar: błąd pobierania danych");
                }
            });
        }
    }
    catch (err) {
        alert('UpdateDaneTowar: ' + err)
    }
}

function UpdateWartosc() {
    var ilosc = document.getElementById('modal_Ilosc').value;
    var cena = document.getElementById('modal_Cena').value;

    if (ilosc.includes(',')) ilosc = ilosc.replace(',', '.');
    if (cena.includes(',')) cena = cena.replace(',', '.');

    var wartosc = ilosc * cena;

    if (isNaN(wartosc)) {
        document.getElementById('modal_Wartosc').value = ''
    }
    else {
        document.getElementById('modal_Wartosc').value = wartosc.toFixed(2).replace('.', ',');
    }


}

function ShowLoadingModal() {
    dataModalLoadingTemp = popup.GetContentHtml();
    var height = popup.GetHeight();
    popup.SetContentHtml(GetLoadingImgModal(height));
}

function HideLoadingModal() {
    if (dataModalLoadingTemp != '') {
        popup.SetContentHtml(dataModalLoadingTemp);
        dataModalLoadingTemp = '';
    }
}

function GetLoadingImgModal(height) {
    var margin = height / 2 - 25;
    return '<div><div class="loader" style="margin-top:' + margin + 'px;margin-bottom:' + margin + 'px"></div></div>';
}
