Imports System.Collections.Generic
Imports CharterDotCom.CommonEntities.Library
Imports CharterDotCom.CommonEntities.Library.Enumerations
Imports CharterDotCom.CommonEntities.Library.EntityMemberServices
Imports CharterDotCom.CommonEntities.Library.EntitySupport
Imports CharterDotCom.CommonEntities.Library.EntitySearch
Imports CharterDotCom.CommonEntities.Library.EntityWebConfig
Imports CharterDotCom.CommonEntities.Library.Common
Imports CharterDotCom.WebSite.Library
Imports System.Web.Security
Imports System.Text.RegularExpressions
Imports CharterDotCom.CommonEntities.Library.EntityEBSServices

Partial Class Controls_Register
    Inherits System.Web.UI.UserControl

    Public strSectionTitle As String = "Create Login"
    Public strPlaceholderQuestion As String = "Choose One..."

    Private _isPanel3 As Boolean = False
    Public Property IsPanel3() As Boolean
        Get
            Return _isPanel3
        End Get
        Set(ByVal value As Boolean)
            _isPanel3 = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.PanelV3.Visible = True
    End Sub

    Protected Sub Load_RegisterScreen()
        ' instead of server redirect do the followings
        Me.MACreateAcctPanel2.Visible = True
        Me.MACreateAcctPanel1.Visible = False
        pnlSSN.Visible = False
        pnlAccount.Visible = True
        pnlComplete.Visible = False
        pnlChoice.Visible = True

        vldSecurityCodeRegEx.ValidationGroup = Me.ID
        vldSecurityCodeRequired.ValidationGroup = Me.ID
        vldAccountNumberRequired.ValidationGroup = Me.ID
        vldAddress1Required.ValidationGroup = Me.ID
        vldAnswerRequired.ValidationGroup = Me.ID
        vldEmailRegEx.ValidationGroup = Me.ID
        vldEmailRequired.ValidationGroup = Me.ID
        vldPassword2Compare.ValidationGroup = Me.ID
		vldPassword2Required.ValidationGroup = Me.ID
		revLength.ValidationGroup = Me.ID
		revLowercase.ValidationGroup = Me.ID
		revNumber.ValidationGroup = Me.ID
		revUppercase.ValidationGroup = Me.ID

        vldPassword2SpecialChar.ValidationGroup = Me.ID
        vldQuestionRequired.ValidationGroup = Me.ID
        vldSSNRegEx.ValidationGroup = Me.ID
        vldSSNRequired.ValidationGroup = Me.ID
        vldUserNameRequired.ValidationGroup = Me.ID
        vldUserNameSpecialChar.ValidationGroup = Me.ID
        vldZipCodeRegEx.ValidationGroup = Me.ID
        vldZipCodeRequired.ValidationGroup = Me.ID
        btnRegister.ValidationGroup = Me.ID

        '#######################################################################
        'MODIFICATION   ROD COPELAND    MARCH 20, 2008  ISSUE ID 2976
        'CHANGE ADDED TO INSERT THE VAULE OF 'CHOOSE ONE' IN THE QUESTION LIST.
        '#######################################################################
        Dim tempQuestions As List(Of String)
        Dim outQuestions As New List(Of String)
        outQuestions.Add(strPlaceholderQuestion)
        tempQuestions = AccountManager.Instance.GetSecurityQAWithoutAccount()
        For Each strQuestion As String In tempQuestions
            outQuestions.Add(strQuestion)
        Next
        ddlQuestion.DataSource = outQuestions
        ddlQuestion.DataBind()

        ddlState.DataSource = CType(Application(ApplicationKey.StateList), System.Data.DataTable)
        ddlState.DataTextField = "Name"
        ddlState.DataValueField = "Abbv"
        ddlState.DataBind()
        ddlState.Items.Insert(0, New ListItem(String.Empty, String.Empty))

        '11/12/2007 Brad Miller - populate form controls with data from previous screen
        '12/15/08 Naqib Quarishi - populate the conttrols with the collected values
        txtSecurityCode.Text = txtHorWideSecurityCodeV3.Text
        txtAccountNumber.Text = txtHorWideAccountNumberV3.Text

        '#######################################################################
        'MODIFICATION   MARK SNYDER    JUNE 17, 2008  ISSUE ID 3604
        'IF SECURITY CODE AND ACCOUNT NUMBER ARE POPULATED, DO NOT ALLOW EDIT.
        'THEY HAVE ALREADY BEEN THROUGH THE CPNI VERIFICATION ON THE PREVIOUS
        'LOGIN PAGE.
        '#######################################################################
        txtSecurityCode.ReadOnly = txtSecurityCode.Text <> String.Empty
        txtAccountNumber.ReadOnly = txtAccountNumber.Text <> String.Empty
        If txtSecurityCode.ReadOnly And txtAccountNumber.ReadOnly Then
            txtUserName.Focus()
        End If

        strSectionTitle = "Create Login"

    End Sub

    Protected Sub btnRegister_Click1(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnRegister.Click
        If Page.IsValid Then
            Dim _successCPNI As Boolean = False

            '#######################################################################
            'MODIFICATION   MARK SNYDER    JULY 21, 2008  ISSUE ID 4254
            '#######################################################################
            'SECURITY CODE AND ACCOUNT NUMBER MAY BE SET TO READ-ONLY IN THE PAGE
            'LOAD ABOVE.
            'IF SECURITY CODE AND ACCOUNT NUMBER ARE READ-ONLY, DO NOT CALL CPNI.
            'THEY HAVE ALREADY BEEN THROUGH THE CPNI VERIFICATION ON THE PREVIOUS
            'LOGIN PAGE.
            '#######################################################################
            If txtSecurityCode.ReadOnly And txtAccountNumber.ReadOnly Then
                _successCPNI = True
            Else
                _successCPNI = AccountManager.Instance.GetCPNIVerification(txtAccountNumber.Text, txtSecurityCode.Text)
            End If

            If _successCPNI Then
                Dim _successMS As Boolean = False
                Dim strPotentialICOMSToCSGConvertedAcctNum As String = ""

                If txtAccountNumber.Text.Trim().Length > 16 Then

                    If Not (Application("ICOMS_TO_CSG_REGISTRATION_MAPPING_ENABLED") Is Nothing) Then
                        If Application("ICOMS_TO_CSG_REGISTRATION_MAPPING_ENABLED") = "T" Then
                            '###########################################################################
                            'PROJECT BIG SKY, JULY 2009     ROD COPELAND    ISSUE ID 6724
                            '###########################################################################
                            'CONVERT THE ICOMS ACCOUNT NUMBER AND PASS THE DATA INTO THE PROVISIONING 
                            'SYSTEM AS A CSG CUSTOMER THAT HAS BEEN CONVERTED FROM ICOMS.
                            '###########################################################################
                            Dim lAccount As IAccount = AccountManager.Instance.GetAccount()
                            If Not lAccount Is Nothing Then
                                If Not lAccount.ConvertedBillingAccountsSummary Is Nothing Then
                                    '###########################################################################
                                    'PROJECT BIG SKY, JULY 2009     ROD COPELAND    ISSUE ID 6724
                                    '###########################################################################
                                    'AS PART OF THE BIG SKY PROJECT JULY 2009, EAI, OSS, IT HAS BEEN CONCLUDED 
                                    'THAT IN THE EVENT A CUSTOMER HAS MORE THAN ONE HSI ACCOUNT POST ICOMS TO 
                                    'CSG CONVERSION, WE WILL THROW AN EXCEPTION TO AVIOD CREATING A MS ON THE 
                                    'WRONG ACCOUNT AS WE WOULDN'T KNOW WHICH ONE TO ASSOCIATE TO.  CURRENT 
                                    'NUMBERS SUGGEST THAT THE NUMBER OF CUSTOMERS IMPACTED BY THIS SITUATION 
                                    'IN SITE 100 FOR ICOMS IS AROUND 30.  DIDNT HAVE THE BUSINESS JUSTIFICATION 
                                    'TO CODE A SOLUTION TO ADDRESS THIS, SO JUST THROW EXCEPTION IT THIS CASE.
                                    '############################################################################
                                    Select Case lAccount.ConvertedBillingAccountsSummary.Count()
                                        Case 1
                                            '###########################################################################
                                            'PROJECT BIG SKY, JULY 2009     ROD COPELAND    ISSUE ID 6724
                                            '###########################################################################
                                            'THIS IS A ICOMS CUSTOMER THAT HAS BEEN CONVERTED TO CSG, GO AHEAD AND REGISTER 
                                            'THEM AS A CSG CUSTOMER IN THE OSS PROVISIONING SYSTEM.
                                            '################################################################################
                                            strPotentialICOMSToCSGConvertedAcctNum = lAccount.ConvertedBillingAccountsSummary(0).AccountID 'SHOULD BE CSG ACCOUNT NUMBER
                                        Case Else
                                            Throw New Exception("Customer has multiple CSG HSI accounts after ICOMS conversion. We dont know which HSI account to register the MS under.")
                                    End Select
                                Else
                                    '####################################################################
                                    'THIS IS A NORMAL ICOMS CUSTOMER, REGISTER THEM AS A ICOMS CUSTOMERS.
                                    '####################################################################
                                    strPotentialICOMSToCSGConvertedAcctNum = txtAccountNumber.Text.Trim().Substring(0, 16) & "003"
                                End If
                            Else
                                '####################################################################
                                'THIS IS A NORMAL ICOMS CUSTOMER, REGISTER THEM AS A ICOMS CUSTOMERS.
                                '####################################################################
                                strPotentialICOMSToCSGConvertedAcctNum = txtAccountNumber.Text.Trim().Substring(0, 16) & "003"
                            End If

                            '#######
                            'ICOMS
                            '#######
                            _successMS = AccountManager.Instance.RegisterUser(txtUserName.Text.ToLower(), strPotentialICOMSToCSGConvertedAcctNum, txtPassword.Text, ddlQuestion.SelectedItem.Text, txtAnswer.Text, txtEmail.Text)
                        Else
                            '####################################################################
                            'THIS IS A NORMAL ICOMS CUSTOMER, REGISTER THEM AS A ICOMS CUSTOMERS.
                            '####################################################################
                            strPotentialICOMSToCSGConvertedAcctNum = txtAccountNumber.Text.Trim().Substring(0, 16) & "003"
                            _successMS = AccountManager.Instance.RegisterUser(txtUserName.Text.ToLower(), strPotentialICOMSToCSGConvertedAcctNum, txtPassword.Text, ddlQuestion.SelectedItem.Text, txtAnswer.Text, txtEmail.Text)
                        End If
                    Else
                        '####################################################################
                        'THIS IS A NORMAL ICOMS CUSTOMER, REGISTER THEM AS A ICOMS CUSTOMERS.
                        '####################################################################
                        strPotentialICOMSToCSGConvertedAcctNum = txtAccountNumber.Text.Trim().Substring(0, 16) & "003"
                        _successMS = AccountManager.Instance.RegisterUser(txtUserName.Text.ToLower(), strPotentialICOMSToCSGConvertedAcctNum, txtPassword.Text, ddlQuestion.SelectedItem.Text, txtAnswer.Text, txtEmail.Text)
                    End If
                Else
                    '#######
                    'CSG
                    '#######
                    _successMS = AccountManager.Instance.RegisterUser(txtUserName.Text.ToLower(), txtAccountNumber.Text, txtPassword.Text, ddlQuestion.SelectedItem.Text, txtAnswer.Text, txtEmail.Text)
                End If

                If _successMS Then
                    ' issue 28648
                    ' send user to my account page, in a logged in state
                    If AccountManager.Instance.SignIn(txtUserName.Text, txtPassword.Text, False) Then
                        Dim accountInfo As IAccount
                        accountInfo = AccountManager.Instance.GetAccount()
                        If accountInfo IsNot Nothing Then
                            Utilities.LoginHandler.Login(Me.Context, True, _
                                                         txtUserName.Text, txtPassword.Text, _
                                                         accountInfo.Address.Zip, _
                                                         Nothing, False)
                            Return
                        End If
                    End If

                    pnlChoice.Visible = False
                    pnlSSN.Visible = False
                    pnlAccount.Visible = False
                    pnlComplete.Visible = True
                    strSectionTitle = "My Account Login Successfully Created"
                    lblStatusV3.Visible = False
                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' /> " & "Your registration cannot be completed." & "</div>"
                    End Select
                    '##################################################################################
                End If
            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' /> " & "The account number and security code combination you entered cannot be validated." & "</div>"
                End Select
                '##################################################################################
            End If
        End If
    End Sub

    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnReset.Click
        '#############################################################################################
        'MODIFICATION   SIVA DOLA   3-13-2008
        'cookie variable is used to navigate users(Cb.com Vs Charter.com) to respective login pages.
        'This variable is defined in login.ascx.vb file.
        '#############################################################################################
        If Not Request.Cookies("MyAccountUserType") Is Nothing Then
            If Request.Cookies("MyAccountUserType").Value.ToString.ToUpper.Trim.Equals("BUS") Then
                Response.Redirect("../Visitors/cblogin.aspx")
            Else
                Response.Redirect("~/Visitors/Login.aspx")
            End If
        Else
            Response.Redirect("~/Visitors/Login.aspx")
        End If
    End Sub

    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnLogin.Click
        '#############################################################################################
        'MODIFICATION   SIVA DOLA   3-13-2008
        'cookie variable is used to navigate users(Cb.com Vs Charter.com) to respective login pages.
        'This variable is defined in login.ascx.vb file.
        '#############################################################################################
        If Not Request.Cookies("MyAccountUserType") Is Nothing Then
            If Request.Cookies("MyAccountUserType").Value.ToString.ToUpper.Trim.Equals("BUS") Then

                Response.Redirect("../order/cblogin.aspx")
            Else
                Response.Redirect("../order/Login.aspx")
            End If
        Else
            Response.Redirect("../order/Login.aspx")
        End If
    End Sub

    ''' <summary>
    ''' NEW
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub btnRegisterV3_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnRegisterV3.Click
        If Page.IsValid Then
            Dim _successCPNI As Boolean = False
            _successCPNI = AccountManager.Instance.GetCPNIVerification(txtHorWideAccountNumberV3.Text, txtHorWideSecurityCodeV3.Text)

            If _successCPNI Then
                '###########################################################################
                'PROJECT BIG SKY, JULY 2009     ROD COPELAND    ISSUE ID 6724
                '###########################################################################
                'CONVERT THE ICOMS ACCOUNT NUMBER AND PASS THE DATA INTO THE PROVISIONING 
                'SYSTEM AS A CSG CUSTOMER THAT HAS BEEN CONVERTED FROM ICOMS.
                '###########################################################################
                If Not (Application("ICOMS_TO_CSG_REGISTRATION_MAPPING_ENABLED") Is Nothing) Then
                    If Application("ICOMS_TO_CSG_REGISTRATION_MAPPING_ENABLED").ToString().ToUpper() = "T" Then
                        Dim strTempICOMSAcctNum As String = txtHorWideAccountNumberV3.Text.Trim()
                        If Not (strTempICOMSAcctNum Is Nothing) Then
                            Select Case strTempICOMSAcctNum.Substring(0, 1)
                                Case "1", "2", "3", "4", "5"
                                    '##############################################################################
                                    'THIS IS ASSUMED TO BE ONLY ICOMS NUMBERS COMING ACROSS HERE. NO CSG NUMBERS.
                                    '##############################################################################
                                    If strTempICOMSAcctNum.Length() > 16 Then
                                        strTempICOMSAcctNum = strTempICOMSAcctNum.Substring(0, 16)
                                    End If
                                    '##############################################################################
                                    'N O T A B L E  ALTHOUGH THIS CALL IS GOING THROUGH EAI AND THEY NORMALLY TREAT 
                                    'ICOMS ACCOUNT NUMBERS LIKE .Substring(3, 9).  THIS CALL ON THE OTHER SIDE OF 
                                    'EAI IS REALLY GOING TO AN OSS DATABASE SO WE NEED TO PASS IN THE FULL 16 DIGITS 
                                    'FOR NORMAL OSS INTERACTIONS.
                                    '##############################################################################
                                    AccountManager.Instance.FindCSGAccountNumber(strTempICOMSAcctNum)
                            End Select
                        End If
                    End If
                End If

                lblStatusV3.Text = ""
                Load_RegisterScreen()
            Else
                '##################################################################################
                'M O D I F I C A T I O N    MARK SNYDER   6/13/2008
                '##################################################################################
                'ADDED ROD'S ERROR CODE MAPPING SOLUTION FOR HANDLING AN UNSUCCESSFUL CALL TO THE 
                'CPNI VERIFICATION.
                '##################################################################################
                '##########     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' /> The account number and security code combination you entered cannot be validated." & "</div>"
                End Select
            End If
        End If
    End Sub
End Class