$(document).ready(function (e) {
    $('.cshtmlFormContainer .outside-form button[type=\"submit\"]').unbind().click(function (event) {
        event.preventDefault();
        event.stopPropagation();

        if ($(this).closest(".cshtmlFormContainer").find(".inside-form").find("form").length > 0) {
            $(this).closest(".cshtmlFormContainer").find(".inside-form").find("form").submit();
        }
        else if ($(this).closest(".cshtmlFormContainer").find("form[method=\"post\"]").length > 0) {
            $(this).closest(".cshtmlFormContainer").find("form[method=\"post\"]").submit();
        }
        else {
            console.error("Formulář neexistuje");
        }
    });

    $('.cshtmlFormContainer select.submit').unbind().change(function (event) {
        event.preventDefault();
        event.stopPropagation();

        $(this).closest("form").submit();
    });

    $('.cshtmlFormContainer input.checkbox-all').unbind().change(function (event) {
        event.preventDefault();
        event.stopPropagation();

        $(this).closest("table").find("input[type=\"checkbox\"]").prop("checked", $(this).is(':checked'));
    });

    $('.cshtmlFormContainer input.submit-checkbox').unbind().click(function (event) {
        event.preventDefault();
        event.stopPropagation();

        $(this).closest("form").submit();
    });

    $(".cshtmlFormContainer .threeCheckbox label").unbind().click(function (event) {
        event.preventDefault();
        event.stopPropagation();

        var input = $(this).find("input");

        if (input.attr("disabled") === "disabled") return;

        if (input.val() === "true") {
            input.attr("value", false);
        } else if (input.val() === "false") {
            input.attr("value", null);
        } else if (input.val() === "") {
            input.attr("value", true);
        }
    });

    $(".cshtmlFormContainer table i.fad").click(function (event) {
        var input = $(this);

        if (input.hasClass("fa-plus-circle")) {
            input.removeClass("fa-plus-circle");
            input.addClass("fa-minus-circle");

            input.closest("tr").addClass("activeRow");
            input.closest("tr").next().addClass("activeRow");
        } else {
            input.removeClass("fa-minus-circle");
            input.addClass("fa-plus-circle");

            input.closest("tr").removeClass("activeRow");
            input.closest("tr").next().removeClass("activeRow");
        }
    });

    $(".cshtmlFormContainer table.table.clickableRows tbody tr").click(function (event) {
        window.location = $(this).attr("data-href");
    });
});