Imports System.Collections.Generic
Imports System.Web.Security
Imports System.Text.RegularExpressions
Imports CharterDotCom.CommonEntities.Library
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.CommonEntities.Library.Common
Imports CharterDotCom.WebSite.Library

Partial Class Controls_retrieveUserName
	Inherits System.Web.UI.UserControl

    Private blnFailedAttemptLockout As Boolean
	Public strSectionTitle As String = "" '"Retrieve Username Form"
	Public strSectionTitleV3 As String = ""	'"Retrieve Username Form"
	Private m_Steps As List(Of MyAccountStepList)

#Region " Properties "

	Public Property Mode() As LoginMode
		Get
			If ViewState(Me.ID & "_Mode") Is Nothing Then
				Return LoginMode.Unspecified
			Else
				Return CType(ViewState(Me.ID & "_Mode"), LoginMode)
			End If
		End Get

		Set(ByVal value As LoginMode)
			ViewState(Me.ID & "_Mode") = value
		End Set
	End Property

#End Region

#Region " Page Events "

	Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
		Dim bContinue As Boolean = True	'assume true
		Dim i As Integer
		Dim bRestart As Boolean = False	'this will be used in case the user tries to fake the url

        HidePanels()

        'hide Successstatus message, if it is not already
        lblSuccessStatus.Visible = False

		'JPIRKEY/SKANURI
        'check if number of attempts exceeded the  maximumm allowed attempts for Security questions
		'if so hide all panels show only the error panel message
		'currently error message is specific to Login attempts, make sure the message is accurate.

		Dim blSecretQAAttemptResult As Boolean
		blSecretQAAttemptResult = Utilities.LoginHandler.EvaluateFailedAttemptLockout(Me.Context, "MyAccount_SecretQA")
		If blSecretQAAttemptResult Then
            Me.pnlMyAccountV3.Visible = False
            Me.pnlAttemptLimitReached.Visible = True
            Dim cTxt As String = ContentManager.Instance.GetContent("myaccount:retuname:exceededSecretQAMaxTry", "1", OwnerType.Other)
            cTxt = cTxt.Replace("{{MYACCOUNT_SecretQA_FAILED_ATTEMPT_MAX_COUNT}}", Application("MyAccount_SecretQA_FAILED_ATTEMPT_MAX_COUNT"))
            pnlAttemptLimitReachedCustomContent.Text = cTxt.ToString()
			bContinue = False
		End If

		'now check to see if they've tried to log in unsuccessfully beyond the max attempts
		blnFailedAttemptLockout = Utilities.LoginHandler.EvaluateFailedAttemptLockout(Me.Context, "MyAccount_Login")
		If blnFailedAttemptLockout Then
            Me.pnlMyAccountV3.Visible = False
			Me.pnlAttemptLimitReached.Visible = True
			Dim cTxt As String = ContentManager.Instance.GetContent("myaccount:login:exceededMaxTry", "1", OwnerType.Other)
			cTxt = cTxt.Replace("{{MYACCOUNT_LOGIN_FAILED_ATTEMPT_MAX_COUNT}}", Application("MYACCOUNT_LOGIN_FAILED_ATTEMPT_MAX_COUNT"))
            pnlAttemptLimitReachedCustomContent.Text = cTxt.ToString()
			bContinue = False
		End If

		If bContinue AndAlso Not Request.QueryString(QueryStringName.MyAccountStep) Is Nothing Then
			'reset the continue flag - there could be a bogus querystring value here and we only continue
			'if we find a valid and expect querystring value
			bContinue = False
			For i = 0 To m_Steps.Count - 1
				If m_Steps(i).QueryName = Request.QueryString(QueryStringName.MyAccountStep) Then
					bContinue = True
					Exit For
				End If
			Next

			'JPIRKEY 4-2010
			'Depending on the panel we're showing, we need to set the "continue" button's imageURL property
			'Also, some panels will not have the Reset/Cancel button.
			If bContinue Then

				Select Case m_Steps(i).Sequence
					Case 1
						pnlFirstStepPanel.Visible = True
						ucIntroText.ContentKey = "myaccount:retuname:introtext:step1"
                        ucTips.ContentKey = "myaccount:retuname:tips:step1"
                        SetTipDivVisible(True)

                        btnContinue.ImageUrl = "../CMS_Images/2010_btn_next.ccom"
                        btnContinue.AlternateText = "Next"
                        btnContinue.ValidationGroup = "Step1"
                        btnContinue.TabIndex = 10005

                        btnReset.ImageUrl = "../CMS_Images/2010_btn_cancel.ccom"
                        btnReset.Visible = True
                        btnReset.AlternateText = "Cancel"
                        btnReset.TabIndex = 10006
					Case 2
						'make sure the session state is properly loaded, just in case someone tries to fake the url
						If Not AccountManager.Instance.GetMyAccountRetUNameAccount.ZipCode Is Nothing Then
							pnlSecondStepPanel.Visible = True
							ucIntroText.ContentKey = "myaccount:retuname:introtext:step2"
                            ucTips.ContentKey = "myaccount:retuname:tips:step2"

                            ' per Kim Blackmore issue 27678 6/2013
                            SetTipDivVisible(False)

                            btnContinue.ImageUrl = "../CMS_Images/2010_btn_next.ccom"
                            btnContinue.AlternateText = "Next"
                            btnContinue.ValidationGroup = "Step2"
                            btnContinue.TabIndex = 10002

                            btnReset.ImageUrl = "../CMS_Images/2010_btn_cancel.ccom"
                            btnReset.Visible = True
                            btnReset.AlternateText = "Cancel"
							btnReset.TabIndex = 10003
						Else
							bRestart = True
						End If

                        ' issue 27678
                    Case 3
                        'make sure the session state is properly loaded, just in case someone tries to fake the url
                        If Not AccountManager.Instance.GetMyAccountRetUNameAccount.UserName Is Nothing Then
                            pnlFourthStepPanel.Visible = True
                            ucIntroText.ContentKey = "myaccount:retuname:introtext:step4"
                            ucTips.ContentKey = "myaccount:retuname:tips:step4"
                            SetTipDivVisible(True)

                            btnContinue.ImageUrl = "../CMS_Images/2010_btn_loginNow.ccom"
                            btnContinue.AlternateText = "Login"
                            btnContinue.ValidationGroup = "Step3"
                            btnContinue.TabIndex = 10001

                            btnReset.Visible = False
                        Else
                            bRestart = True
                        End If
                End Select
			End If

		Else
			pnlFirstStepPanel.Visible = True
			ucIntroText.ContentKey = "myaccount:retuname:introtext:step1"
            ucTips.ContentKey = "myaccount:retuname:tips:step1"

            btnContinue.ImageUrl = "../CMS_Images/2010_btn_next.ccom"
            btnContinue.AlternateText = "Next"
            btnContinue.ValidationGroup = "Step1"
            btnContinue.TabIndex = 10005

            btnReset.ImageUrl = "../CMS_Images/2010_btn_cancel.ccom"
            btnReset.Visible = True
            btnReset.AlternateText = "Cancel"
            btnReset.TabIndex = 10006
		End If

		If bRestart Then
			Response.Redirect("retrieveusername.aspx", True)
		End If

	End Sub

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		LoadAccountSteps()
	End Sub

	Protected Sub btnContinue_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnContinue.Click
		Dim i, j As Integer
		Dim bContinue As Boolean
		Dim bFinished As Boolean

		'cycle through the Step collection and find the name of the current step and it's corresponding index
		'then find the next time in numeric sequence and append that name to the querystring
		If Not Request.QueryString(QueryStringName.MyAccountStep) Is Nothing Then
			Dim bFound As Boolean = False

			For i = 0 To m_Steps.Count - 1
				If m_Steps(i).QueryName = Request.QueryString(QueryStringName.MyAccountStep) Then
					Exit For
				End If
			Next
			'we found the current step, lets find the next one
			For j = 0 To m_Steps.Count - 1
				If m_Steps(j).Sequence = m_Steps(i).Sequence + 1 Then
					Exit For
				End If
			Next
		Else
			'first page, nothing supplied, go on to the 2nd step.
			For j = 0 To m_Steps.Count - 1
				If m_Steps(j).Sequence = 2 Then
					Exit For
				End If
			Next
		End If

		bContinue = False
		bFinished = False

		'process the step-specific functions
		Select Case True
			Case pnlFirstStepPanel.Visible
				bContinue = DoStepWork(ucStep1)
			Case pnlSecondStepPanel.Visible
				bContinue = DoStepWork(ucStep2)
            Case pnlFourthStepPanel.Visible
                bContinue = DoStepWork(ucStep4)
        End Select

		If bContinue Then
            'we found the next step, send it
            'if password is just changed just set the top message accordingly
            Response.Redirect("retrieveusername.aspx?" & QueryStringName.MyAccountStep & "=" & m_Steps(j).QueryName, True)
		Else
			'the sub-panel is in charge of outputting necessary "contingency" content, we don't need to do anything here, but stay on the page

			'WHERE DO GO AFTER THE PROCESS IS COMPLETE?!
			'The last panel's function will redirect the user to next location, we do not have to worry about it here.
		End If
	End Sub

#End Region

#Region " Subs and Functions "

	'JPirkey: Routine to load the account step name from the database config table
	Private Sub LoadAccountSteps()
		Dim _UseStaticNavigation As Boolean
		Dim stp As MyAccountStepList

		If Not Application(ApplicationKey.MyAccountRetUNameStepList) Is Nothing Then
			If TypeOf (Application(ApplicationKey.MyAccountRetUNameStepList)) Is List(Of MyAccountStepList) Then
				m_Steps = DirectCast(Application(ApplicationKey.MyAccountRetUNameStepList), List(Of MyAccountStepList))
				_UseStaticNavigation = False
			Else
				_UseStaticNavigation = True
			End If
		Else
			_UseStaticNavigation = True
		End If

		If _UseStaticNavigation Then
			'manually populate this list
			stp = New MyAccountStepList
            stp.QueryName = "PhoneAccount"
			stp.Sequence = 1
			m_Steps.Add(stp)

			stp = New MyAccountStepList
            stp.QueryName = "SecretQuestion"
			stp.Sequence = 2
			m_Steps.Add(stp)

            stp = New MyAccountStepList
            stp.QueryName = "ChangePwd"
            stp.Sequence = 3
            m_Steps.Add(stp)
        End If

		ucStep1.QueryKey = m_Steps(0).QueryName
		ucStep2.QueryKey = m_Steps(1).QueryName
        ' issue 27678
        ucStep4.QueryKey = m_Steps(2).QueryName

	End Sub

	Sub HidePanels()
		pnlFirstStepPanel.Visible = False
		pnlSecondStepPanel.Visible = False
        pnlFourthStepPanel.Visible = False
	End Sub

	Private Function DoStepWork(ByVal myAccountStepPage As IMyAccountStepPage) As Boolean
		Dim bReturn As Boolean
		bReturn = myAccountStepPage.DoWork()
		If Not myAccountStepPage.ErrorMessage Is Nothing Then
            lblStatusV3.Text = "<div class=""ValidationSummary"">" & _
                "<img src='../Images/error-icon_small.ccom' /> " & _
                myAccountStepPage.ErrorMessage & "</div>"
		End If
		Return bReturn
	End Function

#End Region

    Private Sub SetTipDivVisible(pbVisible As Boolean)
        ucTips.Visible = pbVisible
        tipsDiv.Visible = pbVisible
        tipBottom.Visible = pbVisible
        tipTop.Visible = pbVisible
    End Sub

    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnReset.Click
        Response.Redirect(Request.Url.ToString())
    End Sub
End Class