Imports System.Collections.Generic
Imports CharterDotCom.CommonEntities.Library
Imports CharterDotCom.CommonEntities.Library.Common
Imports CharterDotCom.CommonEntities.Library.Enumerations
Imports CharterDotCom.CommonEntities.Library.EntityMemberServices
Imports CharterDotCom.CommonEntities.Library.EntityEBSServices
Imports CharterDotCom.CommonEntities.Library.EntitySupport
Imports CharterDotCom.CommonEntities.Library.EntitySearch
Imports CharterDotCom.CommonEntities.Library.EntityWebConfig
Imports CharterDotCom.CommonEntities.Library.EntityG2BServices
Imports CharterDotCom.WebSite.Library
Imports CharterDotCom.WebSite.Library.BLCommon

Partial Class Visitors_Forgot
    Inherits CharterCMSPage

    Public strSectionTitle As String = "Forgot Username & Password"
    Private blnFailedAttemptLockout As Boolean

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetPageTitle("Forgotten Password")
        Dim _questions As List(Of String)
        Dim _question As String

        '###########################################################################
        'MODIFICATION   ROD COPELAND    ISSUE ID 4376   OCT 22 2008
        '########################################################################### 
        If Not (Application("USE_ZIP_VERIFICATION") Is Nothing) Then
            If Application("USE_ZIP_VERIFICATION").ToString().Trim().ToUpper() = "T" Then
                pnlZipVerification.Visible = True
            Else
                pnlZipVerification.Visible = False
            End If
        Else
            pnlZipVerification.Visible = False
        End If

        '#####################################################################
        '6/21/2007  ROD COPELAND
        'LOCKOUT THE USER IF THEY HAVE FAILED TOO MANY TIMES FOR THIS SESSION.
		'#####################################################################
		'JPIRKEY/SKANURI 4-2010 Moved the EvaluationFailedAttemptLockout to a central location
		blnFailedAttemptLockout = Utilities.LoginHandler.EvaluateFailedAttemptLockout(Me.Context, "MyAccount_SecretQA")

        If blnFailedAttemptLockout Then
            pnlForgotten.Visible = False
            pnlReset.Visible = False
            pnlAttemptLimitReached.Visible = True
        Else
            pnlForgotten.Visible = True
            pnlReset.Visible = False
            pnlAttemptLimitReached.Visible = False
        End If

        Select Case True
            Case pnlForgotten.Visible
                strSectionTitle = "Forgotten Password"
            Case pnlReset.Visible
                strSectionTitle = "Password Reset"
            Case pnlAttemptLimitReached.Visible
                strSectionTitle = "Reset Password Failed"
            Case Else
                strSectionTitle = "Forgotten Password"
        End Select

        If Not Page.IsPostBack Then
            vldUserNameSpecialChar.ValidationGroup = "Forgotten"
            vldUserNameRequired.ValidationGroup = "Forgotten"
            vldQuestionRequired.ValidationGroup = "Forgotten"
            vldAnswerRequired.ValidationGroup = "Forgotten"
            btnSubmit.ValidationGroup = "Forgotten"

            vldPasswordSpecialChar.ValidationGroup = "Reset"
            vldPassword2SpecialChar.ValidationGroup = "Reset"
            vldPwdRequired.ValidationGroup = "Reset"
            vldConfirmPwdRequired.ValidationGroup = "Reset"
            vldPasswordCompare.ValidationGroup = "Reset"
            btnResetPwd.ValidationGroup = "Reset"
            _questions = AccountManager.Instance.GetSecurityQAWithoutAccount()

            For Each _question In _questions
                ddlQuestion.Items.Add(_question)
            Next

            If pnlAttemptLimitReached.Visible Then
                Dim cTxt As String = ContentManager.Instance.GetContent("Myaccount:ForgotPass:exceededMaxTry", "1", OwnerType.Other)
                cTxt = cTxt.Replace("{{MYACCOUNT_LOGIN_FAILED_ATTEMPT_MAX_COUNT}}", Application("MYACCOUNT_LOGIN_FAILED_ATTEMPT_MAX_COUNT"))
                pnlAttemptLimitReachedCustomContent.Text = cTxt.ToString()
            End If
        End If
    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSubmit.Click
        '#####################################################################
        '6/21/2007  ROD COPELAND
        'LOCKOUT THE USER IF THEY HAVE FAILED TOO MANY TIMES FOR THIS SESSION.
		'#####################################################################
		'JPIRKEY/SKANURI 4-2010 Moved the EvaluationFailedAttemptLockout to a central location
		blnFailedAttemptLockout = Utilities.LoginHandler.EvaluateFailedAttemptLockout(Me.Context, "MyAccount_SecretQA")

        If Not blnFailedAttemptLockout Then
            If Page.IsValid Then
                Dim blnResult As Boolean = False
                '###########################################################################
                'MODIFICATION   ROD COPELAND    ISSUE ID 4376   OCT 22 2008
                '########################################################################### 
                If Not (Application("USE_ZIP_VERIFICATION") Is Nothing) Then
                    If Application("USE_ZIP_VERIFICATION").ToString().Trim().ToUpper() = "T" Then
                        blnResult = AccountManager.Instance.AuthenticateLoginSecretQA(txtUserName.Text, ddlQuestion.SelectedValue, txtAnswer.Text, txtZipCode.Text)
                    Else
                        blnResult = AccountManager.Instance.AuthenticateLoginSecretQA(txtUserName.Text, ddlQuestion.SelectedValue, txtAnswer.Text)
                    End If
                Else
                    blnResult = AccountManager.Instance.AuthenticateLoginSecretQA(txtUserName.Text, ddlQuestion.SelectedValue, txtAnswer.Text)
                End If

                If blnResult Then
                    pnlForgotten.Visible = False
                    pnlReset.Visible = True
                    lblStatusV3.Text = String.Empty
                Else
                    'stay on current panel, display feedback msg
                    '##################################################################################
                    '##########     E R R O R   C O D E   M A P P I N G    S O L U T I O N      #######
                    '#################          MODIFIED BY ROD COPELAND, 5/2/2007     ################ 
                    '##################################################################################
                    'WRITE THE (IDEALLY) CHARTER FRIENDLY ERROR MESSAGES TO THE PAGE.  
                    'THESE MESSAGES ARE MANAGED IN THE CHARTERDOTCOM.ADMIN.ERROR_CODE_MAPPING TABLE.
                    'THE UNDERLYING LOGIC WILL DISPLAY THE DATASOURCE ERROR MESSAGE IF A 
                    'CHARTERDOTCOM FRIENDLY ERROR MESSAGE IS NOT PRESENT ON THE RECORD.
                    'THERE MAY BE MORE THAN ONE ERROR MESSAGE THAT COMES BACK, DISPLAY 
                    'ACCORDINGLY.
                    '##################################################################################
                    Select Case True
                        Case Not AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage Is Nothing
                            lblStatusV3.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage & "</div>"
                            AccountManager.Instance.ClearCCOMDisplayFriendlyExceptionMessage()
                        Case Else
                            lblStatusV3.Text = "<div class=""ValidationSummary"">Could not authenticate username/Security question/Security answer combination at this time.</div>"
                    End Select
                    '##################################################################################
                End If

            End If
        End If


        Select Case True
            Case pnlForgotten.Visible
                strSectionTitle = "Forgotten Password"
            Case pnlReset.Visible
                strSectionTitle = "Password Reset"
            Case pnlAttemptLimitReached.Visible
                strSectionTitle = "Reset Password Failed"
            Case Else
                strSectionTitle = "Forgotten Password"
        End Select

    End Sub


    Public ReadOnly Property IsAuthor() As Boolean
        Get
            If Server.UrlDecode(Request.Url.Query).IndexOf("/Authors/") > -1 Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property


    Protected Sub btnResetPwd_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnResetPwd.Click
        Dim cookie As HttpCookie
        Dim ticket As FormsAuthenticationTicket
        Dim encryptedText As String
        Dim roleName As String

        If Page.IsValid Then
            Dim blnResult As Boolean = AccountManager.Instance.ChangeAccountPassword(txtPwd.Text.Trim())

            If blnResult Then
                Dim lAccount As IAccount = AccountManager.Instance.GetAccount()
                Dim lResult As Boolean

                If Not lAccount Is Nothing Then
                    If Not lAccount.LoginID Is Nothing Then
                        'call signin to fully auth user
                        lResult = AccountManager.Instance.SignIn(lAccount.LoginID, txtPwd.Text.Trim(), 0)
                    End If
                End If

                If lResult Then
                    If IsAuthor Then
                        roleName = "author"
                    Else
                        roleName = "member"
                    End If

                    ticket = New FormsAuthenticationTicket(1, lAccount.LoginID, DateTime.Now, DateTime.Now.AddMinutes(60), False, roleName)
                    encryptedText = FormsAuthentication.Encrypt(ticket)
                    cookie = New HttpCookie(FormsAuthentication.FormsCookieName, encryptedText)
                    Response.Cookies.Add(cookie)


                    ' issue 26553
                    CookieHandler.StoreCookies(lAccount.LoginID, _
                                               lAccount.OSSProvidedAccountNumber, _
                                               Response, Server, _
                                               Application("COOKIE_DOMAIN"))

                    'Naqib Quarishi - Dec 16 08
                    'Response.Redirect("../account/MyAccount.aspx")
                    'Show the confirm change panel
                    pnlForgotten.Visible = False
                    pnlReset.Visible = False
                    pnlConfirm.Visible = True
                    pnlAttemptLimitReached.Visible = False
                    lblStatusV3.Text = String.Empty
                Else
                    '##################################################################################
                    '##########     E R R O R   C O D E   M A P P I N G    S O L U T I O N      #######
                    '#################          MODIFIED BY ROD COPELAND, 5/2/2007     ################ 
                    '##################################################################################
                    'WRITE THE (IDEALLY) CHARTER FRIENDLY ERROR MESSAGES TO THE PAGE.  
                    'THESE MESSAGES ARE MANAGED IN THE CHARTERDOTCOM.ADMIN.ERROR_CODE_MAPPING TABLE.
                    'THE UNDERLYING LOGIC WILL DISPLAY THE DATASOURCE ERROR MESSAGE IF A 
                    'CHARTERDOTCOM FRIENDLY ERROR MESSAGE IS NOT PRESENT ON THE RECORD.
                    'THERE MAY BE MORE THAN ONE ERROR MESSAGE THAT COMES BACK, DISPLAY 
                    'ACCORDINGLY.
                    '##################################################################################
                    Select Case True
                        Case Not AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage Is Nothing
                            lblStatusV3.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage & "</div>"
                            AccountManager.Instance.ClearCCOMDisplayFriendlyExceptionMessage()
                        Case Else
                            lblStatusV3.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' />Could not change password at this time." & "</div>"
                    End Select
                    '##################################################################################
                End If
            Else
                pnlForgotten.Visible = False
                pnlReset.Visible = True

                '##################################################################################
                '##########     E R R O R   C O D E   M A P P I N G    S O L U T I O N      #######
                '#################          MODIFIED BY ROD COPELAND, 5/2/2007     ################ 
                '##################################################################################
                'WRITE THE (IDEALLY) CHARTER FRIENDLY ERROR MESSAGES TO THE PAGE.  
                'THESE MESSAGES ARE MANAGED IN THE CHARTERDOTCOM.ADMIN.ERROR_CODE_MAPPING TABLE.
                'THE UNDERLYING LOGIC WILL DISPLAY THE DATASOURCE ERROR MESSAGE IF A 
                'CHARTERDOTCOM FRIENDLY ERROR MESSAGE IS NOT PRESENT ON THE RECORD.
                'THERE MAY BE MORE THAN ONE ERROR MESSAGE THAT COMES BACK, DISPLAY 
                'ACCORDINGLY.
                '##################################################################################
                Select Case True
                    Case Not AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage Is Nothing
                        lblStatusV3.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage & "</div>"
                        AccountManager.Instance.ClearCCOMDisplayFriendlyExceptionMessage()
                    Case Else
                        lblStatusV3.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' />Could not change password at this time." & "</div>"
                End Select
                '##################################################################################
            End If
        End If
    End Sub

    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnLogin.Click
        Response.Redirect("../order/Login.aspx")
    End Sub
End Class
