lang", preferredLang);
            }, function () { });
            let dirPromise = bridge.invoke("CloudExperienceHost.Globalization.Language.getReadingDirection").then(function (dirVal) {
                _htmlRoot.setAttribute("dir", dirVal);
            }, function () { });
            let navFlowPromise = bridge.invoke("CloudExperienceHost.getContext").then(function (result) {
                navFlow = result.host;
            }, function () { });

            return WinJS.Promise.join({ pagePromise: pagePromise, cssPromise: cssPromise, languagePromise: languagePromise, dirPromise: dirPromise, navFlowPromise: navFlowPromise });
        },

        ready: function (element, options) {
            let processingFlag = false;

            // Load string resources in HTML elements
            document.title = resources.rdxTitle;
            securityTitle.textContent = resources.securityTitle;
            securityText.textContent = resources.securityText;
            passwordManagmentLegend.textContent = resources.passwordManagmentLegend;
            timeoutLegend.textContent = resources.timeoutLegend;
            passwordLegend.textContent = resources.passwordLegend;

            // start the toggle out disabled until the RDAM request has completed
            passwordManagementToggle.disabled = true;

            // Check the flow
            if (navFlow === "FRXINCLUSIVE") {
                nextButton.textContent = resources.nextButton;
            } else {
                nextButton.textContent = resources.finishButton;
            }

            passwordInputCheck.addEventListener("change", passwordCheck);
            passwordInput.addEventListener("change", passwordCheck);

            passwordManagementToggle.addEventListener("change", function (eventInfo) {
                eventInfo.preventDefault();
                if (passwordManagementToggle.winControl.checked) {
                    timeoutElements.style.display = "inline";
                    passwordElements.style.display = "none";
                    allowUserToContinue();
                    if (passwordError.firstChild) {
                        passwordError.removeChild(passwordError.firstChild);
                    }
                } else {
                    timeoutElements.style.display = "none";
                    passwordElements.style.display = "inline";
                    passwordCheck();
                }
            });

            // we allow a max of 21 days before we disable the admin account
            for (let i = 0; i < 22; i++) {
                let option = document.createElement("option");
                option.value = i * 24;
                if (i == 0) {
                    option.text = resources.immediatelyText;
                } else if (i == 1) {
                    option.text = resources.dayText.replace("{0}", i);
                } else {
                    option.text = resources.daysText.replace("{0}", i);
                }

                timeoutSelect.appendChild(option);
            }

            timeoutSelect.selectedIndex = 3;

            let racValue;
            if (Windows.System.Profile.RetailInfo.properties.hasKey("RetailAccessCode")) {
                racValue = Windows.System.Profile.RetailInfo.properties.lookup("RetailAccessCode");
            }

            // before the RDAM request, prevent the user from continuing
            preventContinue();
            let racOption = {
                url: "https://api.retaildemo.microsoft.com/v1/attributeconfig/rac/" + racValue,
                responseType: "json"
            };
            WinJS.Promise.timeout(30000, bridge.invoke("WinJS.xhr", racOption)).then(
                function (result) {
                    rdamJson = result.response;
                    // populate the password if the rac has one
                    passwordInput.value = rdamJson.RetailerAccessCodes[0].AdminPassword;
                    passwordInputCheck.value = rdamJson.RetailerAccessCodes[0].AdminPassword;
                    passwordManagementToggle.disabled = false
                    allowUserToContinue();
                },
                function (error) {
                    if (error.message === "Canceled") {
                        bridge.invoke("CloudExperienceHost.Telemetry.logEvent", "RdamRequestTimeout");
                    } else {
                        bridge.invoke("CloudExperienceHost.Telemetry.logEvent", "InvalidRac", racValue);
                    }
                    passwordManagementToggle.disabled = false
                    allowUserToContinue();
                }
            );

            nextButton.addEventListener("click", function (eventInfo) {
                eventInfo.preventDefault();
                if (!processingFlag) {
                    if (!blockNext) {
                        processingFlag = true;
                        bridge.fireEvent(CloudExperienceHost.Events.showProgressWhenPageIsBusy);

                        if (passwordManagementToggle.winControl.checked) {
                            bridge.invoke("CloudExperienceHost.Telemetry.logEvent", "DisableAdminSelected", timeoutSelect[timeoutSelect.selectedIndex].value);
                            bridge.invoke("RetailDemo.Internal.RetailInfoSetter.setDWORDAsync", "DelayDisableAdminAccess", timeoutSelect[timeoutSelect.selectedIndex].value)
                            .done(() => {
                                // The CXH Done event is fired from SetupPageSuccessNavigation. Failure in SetupPageSuccessNavigation fails CXH.
                                retailDemoShared.SetupPageSuccessNavigation(rdamJson ? rdamJson.RetailerAccessCodes[0].AdminPassword : null, navFlow, bridge);
                            });
                        } else {
                            bridge.invoke("CloudExperienceHost.Telemetry.logEvent", "CustomPasswordSelected");
                            bridge.invoke("RetailDemo.Internal.RetailInfoSetter.setDWORDAsync", "DelayDisableAdminAccess", 4294967295) // max dword
                            .done(() => {
                                // The CXH Done event is fired from SetupPageSuccessNavigation. Failure in SetupPageSuccessNavigation fails CXH.
                                retailDemoShared.SetupPageSuccessNavigation(passwordInput.value, navFlow, bridge);
                            });
                        }
                    } else if (passwordInput.value.length == 0) {
                        showPasswordError(resources.enterPassword);
                    } else if (passwordInputCheck.value.length == 0) {
                        showPasswordError(resources.retypePassword);
                    }
                }
            });

            // If the disable admin account feature is not enabled, do not show this page.
            if (!CloudExperienceHost.FeatureStaging.isOobeFeatureEnabled("RDX_DisableAdminAccount")) {
                retailDemoShared.SetupPageSuccessNavigation(null, navFlow, bridge);
            } else {
                bridge.fireEvent(CloudExperienceHost.Events.visible, true);
            }

            // Show an error message under the RAC input
            function showPasswordError(message) {
                if (passwordError.firstChild) {
                    passwordError.removeChild(passwordError.firstChild);
                }
                passwordErrorExists = true;
                passwordError.classList.add("inputState_error");
                let text = document.createElement("p");
                text.textContent = message;
                text.setAttribute("aria-hidden", "true");
                let tooltip = document.createElement("div");
                tooltip.className = "errorDialog-dialogRoot template-tooltip tooltipType_error";
                tooltip.appendChild(text);

                passwordError.appendChild(tooltip);
                passwordError.setAttribute("aria-label", message);
                passwordError.style.display = 'inline';
            }

            // Enable all text entry fields and the next button
            function allowUserToContinue() {
                nextButton.classList.add("button_primary");
                blockNext = false;
            }

            function preventContinue() {
                nextButton.classList.remove("button_primary");
                blockNext = true;
            }

            function passwordCheck() {
                if (passwordErrorExists) {
                    passwordError.style.display = 'none';
                    passwordError.setAttribute("aria-label", null);
                    passwordError.classList.remove("inputState_error");
                    if (passwordError.firstChild) {
                        passwordError.removeChild(passwordError.firstChild);
                    }
                    passwordErrorExists = false;
                }

                // Check if the passwords match
                if ((passwordInput.value.length == 0) || (passwordInputCheck.value.length == 0)) {
                    preventContinue();
                } else if (passwordInput.value != passwordInputCheck.value) {
                    preventContinue();
                    showPasswordError(resources.passwordError);
                } else {
                    if (blockNext) {
                        allowUserToContinue();
                    }
                }
            }
        }
    });
})();
