function isNumber(evt, element) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}

$("body").delegate("input.required, textarea.required", "keyup change", function (event) {
    if ($(this).val() == "") {
        $(this)
            .parent()
            .find(".invalid-feedback")
            .text("This field is required.")
            .addClass("show");
    } else {
        $(this)
            .parent()
            .find(".invalid-feedback")
            .text("")
            .hide();
    }
});

$(".btn-submit").click(function (e) {
    var formId = $(this).parents('form').attr('id');
    var formStatus = true;
        $('form#' + formId + ' input.required, form#' + formId + ' textarea.required').each(function () {
        if ($(this).val() == "") {
            formStatus = false;
            $(this)
                .parent()
                .find(".invalid-feedback")
                .text("This field is required.")
                .show();
        } else {
            $(this)
                .parent()
                .find(".invalid-feedback")
                .text("")
                .hide();
        }
    });

    if (formStatus) {
        captchaFormId = formId;
        grecaptcha.execute();
    }
});

function onSubmit(token) {
    let formId = captchaFormId;
    $('.modal-spinner').show();
    var form_data = $('form#' + formId).serializeArray();
    $.ajax({
        type: "POST",
        url: baseUrl + "contact-store",
        data: form_data,
        success: function (data) {
            if (data.status) {
                $('form#' + formId)[0].reset();
                $(".modal-spinner").hide();
                $('.hideAfterSubmit').hide();
                $('.successContactMsg').html('<iconify-icon icon="ooui:success" class="text-brand-green-5 !text-5xl success-icon"></iconify-icon> Thank you for reaching out to us. We will be in touch with you shortly.').show();
            } else {
                $(".modal-spinner").hide();
            }
        },
        error: function (error) {
            if (error.status === 422 && error.readyState == 4) {
                $(".modal-spinner").hide();
                $('form#' + formId)
                    .find(".invalid-feedback")
                    .each(function () {
                        $(this).empty().hide();
                    });
                var errors = $.parseJSON(error.responseText);
                $.each(errors.errors, function (key, val) {
                    $('form#' + formId)
                        .find("#" + key + "_err")
                        .empty()
                        .append(val).show();

                });
            }
        },
    });
}
