
// =======
// Sign In
// =======

$(document).ready(function () {

    var $application = $("#APPLICATION_MNEMONIC").val(), $sessionStorage = JSON.parse(sessionStorage.getItem($application) || "{}");

    // =================
    // Process Enter Key
    // =================

    $(document).on('keypress', function (e) {
        if (e.which == 13) {
            e.preventDefault();
            $("#signIn").submit();
        }
    });

    // -----------
    // Save Button
    // -----------

    $(".btnSignIn").on("click", function (e) {
        e.preventDefault();
        $("#signIn").submit();
    });

    // ---------------
    // Validate Inputs
    // ---------------

    $("#signIn").submit(function (e) {

        var user_name = $("#user_name"), user_password = $("#user_password");

        clearInputErrors();

        user_name.val(user_name.val().replace(/\s+/g, " ").trim());

        if (isBlank(user_name)) {
            createInputError(user_name, "User Name/Email is required");
            return false;
        }

        if (isBlank(user_password)) {
            createInputError(user_password, "Password is required");
            return false;
        }

        return true;

    });

    $(".btnForgot").on("click", function (e) {

        e.preventDefault();
        $("#reset-password").submit();

    });

    // ---------------
    // Validate Inputs
    // ---------------

    $("#reset-password").submit(function (e) {

        clearInputErrors();

        var user_name = $("#user_name");

        user_name.val(user_name.val().replace(/\s+/g, " ").trim());

        if (isBlank(user_name)) {
            createInputError(user_name, "User Name/Email is required to reset password");
            return false;
        }

        $("#reset_user_name").val(user_name.val());

        return true;

    });

});

// --------------------------------
// Clear the Error State for Inputs
// --------------------------------

function clearInputErrors() {

    $(".is-invalid").removeClass("is-invalid");
    $(".invalid-feedback").remove();

}

// --------------------
// Test for Blank Value
// --------------------

function isBlank(object) {

    if ($.trim(object.val()).length == 0) {
        return true;
    } else {
        return false;
    }

}

// -----------------------------------
// Create an Error State for an Input.
// -----------------------------------

function createInputError(input, message = "", scroll = false) {

    if ($(input).hasClass("search-dropdown")) { // Select Picker

        $(input).closest(".bootstrap-select")
                .find(".bs-placeholder")
                .addClass("is-invalid")
                .after("<div class='invalid-feedback fs-875'>" + message + "</div>")
                .focus();

    } else {

        $(input).focus().addClass("is-invalid");

        if ($(input).is(":last-child")) {
            $(input).after("<div class='invalid-feedback text-center'>" + message + "</div>");
        } else {
            $(input).siblings(":last").after("<div class='invalid-feedback'>" + message + "</div>");
        }

    }

    if (scroll === true) {

        var element = document.getElementById(input.attr("id"));
        var header_offset = 130;
        var element_position = element.getBoundingClientRect().top;
        var offset_position = element_position + window.pageYOffset - header_offset;

        window.scrollTo({
            top: offset_position,
            behavior: "smooth"
        });

}
}