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_RetrieveUsernameStep3
	Inherits System.Web.UI.UserControl
	Implements IMyAccountStepPage

	Private m_sError As String
	Dim m_MyAccount As IRetUNameAccount
	Dim m_myQueryString As String
	Dim m_NextStepKey As String
	Dim m_ChangedPasswordKey As String

#Region " Properties "

	Public Property ChangedPasswordKey() As String
		Get
			Return m_ChangedPasswordKey
		End Get
		Set(ByVal value As String)
			m_ChangedPasswordKey = value
		End Set
	End Property

	Public Property NextStepKey() As String
		Get
			Return m_NextStepKey
		End Get
		Set(ByVal value As String)
			m_NextStepKey = value
		End Set
	End Property

	Public Property QueryKey() As String
		Get
			Return m_myQueryString
		End Get
		Set(ByVal value As String)
			m_myQueryString = value
		End Set
	End Property

	Public ReadOnly Property ErrorMessage() As String Implements IMyAccountStepPage.ErrorMessage
		Get
			Return m_sError
		End Get
	End Property

#End Region

#Region " Page Events "

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		'load the Account object we've been building from the previous 2 steps and display the Username

		'This complicated check keeps the amount of work done on ever other step's load from running.  Because this is a control
		'being loaded up from a parent page, each page load will try and instantiate and run this event.
		'by checking the querystring value to make sure it's "our turn" we can avoid unnecessarily running the
		'LoadAccount routine for all the other sub-controls "turns".
		If (Not IsPostBack And (Not Request.QueryString(QueryStringName.MyAccountStep) Is Nothing)) AndAlso _
		  (Request.QueryString(QueryStringName.MyAccountStep) = Me.QueryKey Or Request.QueryString(QueryStringName.MyAccountStep) = Me.ChangedPasswordKey) Then
			If LoadAccount() Then

				lblUserName.Text = m_MyAccount.UserName
                'There is no reason for this check. In case of success, we are setting 
                'the success message at the top control that holds this one with success message.
                'this label is obsolete. 
                '
                'If Request.QueryString(QueryStringName.MyAccountStep) = Me.ChangedPasswordKey Then
                '                'lblPasswordChanged.Visible = True
                'Else
                lblPasswordChanged.Visible = False
                'End If
            Else
                lblUserName.Text = "N/A"
            End If
        Else
            'if they're tried signing in at all, hide this label to be sure
            lblPasswordChanged.Visible = False
        End If

	End Sub

#End Region

#Region " Subs and Functions "

	Public Function DoWork() As Boolean Implements IMyAccountStepPage.DoWork
		If txtPassword.Text.Length >= 8 Then
			If LoadAccount() Then
				If Utilities.LoginHandler.Login(Me.Context, True, lblUserName.Text, txtPassword.Text, m_MyAccount.ZipCode, Nothing, False) Then
					Return True
				Else
					m_sError = AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage
					AccountManager.Instance.ClearCCOMDisplayFriendlyExceptionMessage()
					Return False
				End If
			Else
				m_sError = AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage
				AccountManager.Instance.ClearCCOMDisplayFriendlyExceptionMessage()
			End If
		Else
			m_sError = "Please supply a valid password."
			Return False
		End If
	End Function

	Private Function LoadAccount() As Boolean
		Try
			m_MyAccount = AccountManager.Instance.GetMyAccountRetUNameAccount()
			If Not (m_MyAccount Is Nothing) Then
				Return True
			Else
				Return False
			End If
		Catch ex As Exception
			m_sError = ex.Message
		End Try
	End Function

#End Region

End Class
