$(document).ready(function () {
    $('.input-validation-error').parents('.form-group').addClass('has-error');
    $('.field-validation-error').addClass('text-danger');
});

function clearValidationErrors() {
    $(".field-validation-error").text("");
    $(".field-validation-error").removeClass(".field-validation-error").addClass(".field-validation-valid");
    $(".field-validation-error").closest(".form-group").removeClass("has-error");
}

function displayResponseValidationErrors(response, form = null, displayErrorsModal = true) {
    let errorMessage = "";
    for (var i = 0; i < response.errors.length; i++) {
        var id = "#" + response.errors[i].Key;

        if (form !== null) {
            id = "#" + form + " #" + response.errors[i].Key;
        }

        $(id + "-validation").closest(".form-group").addClass("has-error");
        $(id + "-validation").removeClass("field-validation-valid").addClass("field-validation-error");
        $(id + "-validation").text(response.errors[i].Value);

        errorMessage += "<b>" + $(id + "-validation").closest(".form-group").find("label.control-label").text() + "</b>: " + response.errors[i].Value + "<br/>";
    }

    if (displayErrorsModal) {
        $().customModal({
            destroyOnClose: true,
            showCloseButton: true,
            type: "error",
            text: errorMessage,
            buttons: [
                {
                    icon: 'fa fa-check',
                    text: $("#custom-modal-confirm").val(),
                    closeOnAction: true,
                    type: 'primary'
                }
            ]
        });
    }
}