<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/master.Master" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<%@ Import Namespace="MKInsight.Online.Core.Security" %>
<%@ Import Namespace="AuditVision.Online.DataServices" %>
<%@ Import Namespace="MKInsight.Online.Shared" %>
<%@ Import Namespace="MKInsight.Online.Login" %>
<%@ Import Namespace="MK.Core" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <meta name="robots" content="noindex,nofollow" />

    <script runat="server" language="vb">

        Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
            Page.ViewStateUserKey = Session.SessionID
        End Sub

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                StringTable.Start()
                Localise()
            End If

            lbl_Header.Text = StringTable.Text("ResetPassword")

        End Sub

        Protected Function CheckSecureToken() As Boolean

            Dim authKey As String = txt_AuthKey.Text.Trim()

            If authKey.Length < 1 Then Return False
            Dim tk As SecuredFeatureToken = SecuredFeatureToken.Authenticate(SecuredFeature.ResetPassword, txt_FeatureKey.Text, txt_AuthKey.Text)

            If Not tk.IsValidToken(SecuredFeature.ResetPassword) Then

                Helper.AlertMessage(dv_Alert, Helper.AlertType.Warning, StringTable.Text("InvalidAuthKeySpecified"))

                Return False
            Else
                Session("token") = tk
            End If
            Return True
        End Function

        Private Function ValidateForm() As Boolean
            If String.IsNullOrEmpty(txt_Username.Text) Then
                Helper.AlertMessage(dv_Alert, Helper.AlertType.Warning, StringTable.Text("InvalidUserNamePleaseCheckYourLogindetailsAndTryAgain"))
                Return False

            End If

            If Not txt_Password1.Text.Equals(txt_Password2.Text) Then
                Helper.AlertMessage(dv_Alert, Helper.AlertType.Warning, StringTable.Text("PasswordCheck"))
                Return False

            End If

            Return True

        End Function

        Private Sub OnAuthorisedResetPassword()

            Try
                Dim token As SecuredFeatureToken = TryCast(Session("token"), SecuredFeatureToken)
                If token Is Nothing Then Throw New ArgumentNullException("token")
                If Not token.IsValidToken(SecuredFeature.ResetPassword) Then Throw New InvalidOperationException("The specified token is invalid")

                If String.IsNullOrEmpty(txt_AccountName.Text) Then Throw New ArgumentNullException("account")

                If String.IsNullOrEmpty(txt_Password1.Text) Then Throw New ArgumentNullException("password")

                If String.IsNullOrEmpty(txt_Username.Text) Then Throw New ArgumentNullException("username")

                ' NOTE: reset only applies a subset of the policies, since the user has to reset pw after next use anyway
                If txt_Password1.Text.Length < AC.ItemInt("MinPassword") Then Throw New Exception(StringTable.Text("InvalidPassword_LengthPolicy", AC.ItemInt("MinPassword").ToString()))
                If Not txt_Password1.Text.Equals(txt_Password2.Text) Then Throw New Exception(StringTable.Text("PasswordCheck"))

                Dim pwhash As String = MK.Core.PasswordEncoding.CreateSHA512(txt_Password1.Text)

                AuthorisedPasswordResetAction.Execute(token, txt_AccountName.Text, txt_Username.Text, pwhash)

                Helper.AlertMessage(dv_Alert, Helper.AlertType.OK, StringTable.Text("PasswordChangeSuccessful"))

            Catch ex As Exception
                Helper.AlertMessage(dv_Alert, Helper.AlertType.Warning, ex.ToString())
            End Try

            ClearAllData()

        End Sub

        Protected Sub OnUpdateActiveStepChanged(sender As Object, e As EventArgs) Handles w.ActiveStepChanged

            Try
                Select Case w.ActiveStepIndex
                    Case 0 'account details
                        If String.IsNullOrEmpty(txt_AccountName.Text) Then Return

                    Case 1
                        WebAppSecurity.AuthenticateAnonymous(txt_AccountName.Text, SoftwareSection.OnlineLogin, New Guid)
                        txt_FeatureKey.Text = SecuredFeatureToken.CreateRequestKey(SecuredFeature.ResetPassword)

                    Case 2
                        If Not CheckSecureToken() Then w.ActiveStepIndex = 0

                End Select
            Catch ex As Exception
                Helper.AlertMessage(dv_Alert, Helper.AlertType.Warning, ex.ToString())
                ClearAllData()
            End Try

        End Sub

        Protected Sub OnUpdateWizardFinish(sender As Object, e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles w.FinishButtonClick
            OnAuthorisedResetPassword()
        End Sub

        Private Sub ClearAllData()
            txt_AccountName.Text = ""
            txt_Username.Text = ""
            txt_AuthKey.Text = ""
            SessionEx.Abandon()
            Session.Abandon()
            w.ActiveStepIndex = 0
        End Sub

        Private Sub Localise()
            Dim st As StringTableDelegate = StringTable.CreateDelegate

            lbl_Info.Text = st("AuthResetPasswordFormInfo")

            lbl_Account.Text = st("AccountName:")
            lbl_Username.Text = st("User_Name:")
            lbl_Password1.Text = st("PlanetMKNewPassword:")
            lbl_Password2.Text = st("PlanetMKNewPassword:")

            lbl_MS.Text = st("AdditionalAuthenticationRequired")
            lbl_Info.Text = st("RequestedFeatureRequiresMorganKaiAuthentication")
            lbl_FeatureKey.Text = st("FeatureRequestKey_c")
            lbl_AuthKey.Text = st("FeatureAuthenticationKey_c")
            btn_POK.Text = st("OK")
            btn_PCancel.Text = st("Cancel")

        End Sub
    </script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="cm" runat="server">

    <div style="padding-left: 20px">
        <h2>
            <asp:Label ID="lbl_Header" runat="server" ForeColor="White"></asp:Label></h2>
        <div>

            <asp:Button ID="btn_pHandler" runat="server" Text="Button" Style="display: none;" />
            <asp:Panel ID="P" runat="server" CssClass="frame" Style="display: none;">
                <div class="overlay">
                    <asp:Panel ID="pnlHeader" runat="server" CssClass="header">
                        <asp:Label ID="lbl_MS" runat="server" CssClass="msg" Text="Additional Authentication Required" />
                    </asp:Panel>
                    <asp:Panel ID="PB" runat="server" CssClass="body">
                        <div>
                        </div>
                    </asp:Panel>
                    <div class="footer">
                        <asp:Button ID="btn_POK" runat="server" Text="Ok" Width="70px" />
                        <asp:Button ID="btn_PCancel" runat="server" Text="Cancel"
                            CausesValidation="False" />
                    </div>
                </div>

            </asp:Panel>
            <cc1:ModalPopupExtender ID="mdp" runat="server" TargetControlID="btn_pHandler" PopupControlID="P"
                BackgroundCssClass="modalBackground">
            </cc1:ModalPopupExtender>
            <cc1:DragPanelExtender ID="dpE" runat="server" DragHandleID="pnlHeader" TargetControlID="p" />
        </div>
        <br />
        <div role="alert" id="dv_Alert" runat="server" style="margin-top: 5px;">
        </div>

        <h3>

            <asp:Label ID="lbl_Info" runat="server" ForeColor="White"></asp:Label>
        </h3>


        <asp:Wizard ID="w" runat="server" ActiveStepIndex="0" BackColor="White" BorderColor="#CCCCCC"
            BorderStyle="Solid" BorderWidth="1px" DisplaySideBar="False" EnableTheming="True"
            Font-Names="Verdana" Width="90%" Height="100%">
            <StepStyle BorderWidth="0px" />

            <HeaderStyle ForeColor="#585880" Font-Size="11pt" BackColor="#F8F7F4" BorderColor="#CCCCCC" BorderWidth="1" Height="24"></HeaderStyle>
            <SideBarStyle ForeColor="#585880" Font-Size="11pt" CssClass="theme_fadeblue" />
            <SideBarButtonStyle ForeColor="#000066" Font-Size="11pt" BackColor="transparent" BorderColor="#000066" />
            <StepStyle ForeColor="#585880" Font-Size="11pt" BackColor="#FFFFFF" BorderColor="#585880" BorderWidth="1pt" />
            <NavigationStyle ForeColor="#585880" Font-Size="11pt" BackColor="#F8F7F4" BorderColor="#585880" />
            <NavigationButtonStyle ForeColor="#585880" Font-Size="11pt" BackColor="#F8F7F4" BorderColor="#585880" />

            <WizardSteps>
                <asp:WizardStep ID="w_account" runat="server">
                    <table>
                        <tr>
                            <td>
                                <asp:Label ID="lbl_Account" runat="server"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_AccountName" runat="server" Width="400"></asp:TextBox>
                            </td>
                        </tr>

                    </table>

                </asp:WizardStep>

                <asp:WizardStep ID="w_authKey" runat="server">

                    <table>
                        <tr>
                            <td colspan="2">
                                <asp:Label ID="Label1" runat="server" Text="The requested feature cannot be accessed without additional authentication from the MKInsight support team. Please contact Morgan Kai for additional information, or click Cancel to exit."></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <hr class="ee" />
                            </td>

                        </tr>
                        <tr>
                            <td>
                                <asp:Label runat="server" ID="lbl_FeatureKey" Text="Feature request key to pass to MKInsight Support:"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_FeatureKey" runat="server" Width="500"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label runat="server" ID="lbl_AuthKey" Text="Authentication key to access secured feature:"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_AuthKey" runat="server" Width="500"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="rfv_AuthKey" runat="server"
                                    ControlToValidate="txt_AuthKey" ErrorMessage="*"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                    </table>
                </asp:WizardStep>
                <asp:WizardStep ID="w_finish" runat="server">
                    <table>

                        <tr>
                            <td>
                                <asp:Label ID="lbl_Username" runat="server"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_Username" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="lbl_Password1" runat="server"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_Password1" runat="server" TextMode="Password"></asp:TextBox>
                            </td>
                        </tr>

                        <tr>
                            <td>
                                <asp:Label ID="lbl_Password2" runat="server"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="txt_Password2" runat="server" TextMode="Password"></asp:TextBox>
                            </td>
                        </tr>

                    </table>

                </asp:WizardStep>
            </WizardSteps>

        </asp:Wizard>

    </div>
</asp:Content>
