Imports System.Collections.Generic
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.WebSite.Library

Partial Class Controls_ManageEmailAccounts
    Inherits System.Web.UI.UserControl
    Dim _account As IAccount

    Public strSectionTitle As String = "My Email Accounts"
    Public strEmailDomainName As String = "@charter.net"

    Public Property Account() As IAccount
        Get
            Return _account
        End Get
        Set(ByVal value As IAccount)
            _account = value
        End Set
    End Property

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

    Protected Sub renderPanel()
        '########################################
        'CLEAR DISPLAY MESSAGES FOR WHOLE PAGE.
        '########################################
        ClearDisplayMessagesForPage()
        pnlPackageList.Visible = True
        'lblNewEmailDescription1.Text = "* The parent account controls the email addresses for all addresses."

        '#################################################
        'ADD MEMBER SERVICES PACKAGE LIST TO DROPDOWN LIST
        '#################################################
        If Not (ddlMSPackage Is Nothing) Then
            If Not (ddlMSPackage.Items Is Nothing) Then
                If ddlMSPackage.Items.Count < 1 Then
                    If Not (_account Is Nothing) Then
                        If Not (_account.Person Is Nothing) Then
                            If Not (_account.Person.MSPackageInfo Is Nothing) Then
                                For Each MSPackage As IAccount.IMember.IMemberServicePackageInfo In _account.Person.MSPackageInfo
                                    If Not (MSPackage Is Nothing) Then
                                        If Not (MSPackage.descriptionField() Is Nothing) Then
                                            If MSPackage.descriptionField.IndexOf(",") > 0 Then
                                                Dim lDisplayName As String = MSPackage.descriptionField.Substring(0, MSPackage.descriptionField.IndexOf(","))
                                                ddlMSPackage.Items.Add(New ListItem(lDisplayName, MSPackage.provisioningIdField & MSPackage.descriptionField()))
                                            Else
                                                ddlMSPackage.Items.Add(New ListItem(MSPackage.descriptionField, MSPackage.provisioningIdField & MSPackage.descriptionField()))
                                            End If
                                        End If
                                    End If
                                Next
                                txtProvID.Text = ""
                                txtAddress.Text = ""
                                lblResultMessage.Text = ""
                                txtPassword.Text = ""
                                txtPassword2.Text = ""
                                txtAddress.Text = ""

                                pnlCreateNewEmailAccount.Visible = True

                                '##################################################
                                'PRESELECT THE FIRST PACKAGE IN THE DROPDOWN LIST
                                '##################################################
                                If ddlMSPackage.Items.Count > 0 Then
                                    ddlMSPackage.SelectedIndex = 0
                                End If
                            End If
                        End If
                    End If

                End If
            End If

            '####################################################################
            'REDISPLAY THE MANAGE EMAIL LANDING PAGE WITH THE UPDATED EMAIL LIST
            '####################################################################
            If ddlMSPackage.Items.Count > 0 Then
                If Not (_account Is Nothing) Then
                    If Not (_account.Person Is Nothing) Then
                        If Not (_account.Person.MSPackageInfo Is Nothing) Then
                            Dim intSelectedValue As Integer = ddlMSPackage.SelectedIndex
                            Dim lPackage As IAccount.IMember.IMemberServicePackageInfo = _account.Person.MSPackageInfo(intSelectedValue)

                            'plhAccounts.Visible = True
                            Dim intMaxEmailsAllowed As Integer = 0
                            Dim intEmailsCount As Integer = 0

                            If Not (lPackage Is Nothing) Then

                                '####################################################
                                'PER JOHN SPYERS 12-14-2006, THE MOST COMMON COUNT 
                                'FOR EMAIL ADDRESSES PER PACKAGE IS 10
                                '####################################################
                                If lPackage.MaxEmailsAllowed > 0 Then
                                    intMaxEmailsAllowed = lPackage.MaxEmailsAllowed
                                End If

                                Select Case intMaxEmailsAllowed
                                    Case 0
                                        pnlCannotShowEmail.Visible = True
                                        plhAccounts.Visible = False
                                    Case Else
                                        plhAccounts.Visible = True
                                        pnlCannotShowEmail.Visible = False
                                End Select

                                If Not (lPackage.emailsField Is Nothing) Then
                                    intEmailsCount = lPackage.emailsField.Count
                                    If lPackage.emailsField.Count >= intMaxEmailsAllowed Then
                                        '####################################################################
                                        'TELL THE USER THAT THEY HAVE THE MAX EMAILS AVAIL FOR THIS PACKAGE.
                                        'HIDE THE BUTTON TO ADD A NEW EMAIL.
                                        '####################################################################
                                        pnlCreateNewEmailAccount.Visible = False
                                        btnCreateNewEmailAddress.Visible = False
                                    Else
                                        '#####################################################
                                        'CONTINUE ON, ALL IS GOOD TO SHOW THE NEW EMAIL BUTTON
                                        '#####################################################
                                        pnlCreateNewEmailAccount.Visible = True
                                        btnCreateNewEmailAddress.Visible = True
                                    End If
                                End If
                            End If

                            Call buildEmailList(_account.Person.MSPackageInfo(intSelectedValue))

                            If intMaxEmailsAllowed > 0 Then
                                '#####################################################
                                'THIS MAY BE THE CASE WHERE THEY ARE PROVISIONED FOR 
                                'EMAIL BUT DONT HAVE ANY EMAIL ACCOUNTS ASSIGNED TO 
                                'THEIR ACCOUNT YET.  IN THIS CASE, WE SHOULD SHOW THE 
                                'BUTTON TO CREATE NEW EMAIL ACCOUNTS.
                                '#####################################################
                                pnlCreateNewEmailAccount.Visible = True
                                ' btnCreateNewEmailAddress.Visible = True
                                btnCreateNewEmailAddress.Visible = True

                                '##################################################################
                                'JUST CLEAR THE DESCRIPTION FIELD THAT WOULD NORMALLY DESCRIBE THE 
                                'PARENT EMAIL ACCOUNT CONCEPT, SHOULDN'T BE NEEDED SINCE THERE ARE 
                                'NO EMAIL ACCOUNTS TO MAKE REFERENCE TO IN THE DESCRIPTION.
                                '##################################################################
                                If intEmailsCount = 0 Then
                                    lblNewEmailDescription1.Text = ""
                                End If
                            End If

                        Else
                            Call ClearEmailTable()
                        End If
                    Else
                        Call ClearEmailTable()
                    End If
                Else
                    Call ClearEmailTable()
                End If
            Else
                Call ClearEmailTable()
            End If
        End If

        '####################
        'SET DEFAULT VALUE 
        '####################
        strSectionTitle = "Manage Email Accounts"

        '#################################################################
        'MODIFICATION   ROD COPELAND    FEB 4, 2008
        '#################################################################
        'THIS CHANGE WAS REQUESTED BY DIANA TO ALLOW THE RIGHT DOMAIN 
        'FOR CHARTER BUSINESS CUSTOMERS WHO USE MYACCOUNT.  RESIDENTIAL 
        'CUSTOMERS SHOULD KEEP THE CHARTER.NET DOMAIN FOR ALL EMAIL MAINT.
        '#################################################################
        If Not (_account Is Nothing) Then
            If Not (_account.AccountType Is Nothing) Then
                Select Case _account.AccountType.ToUpper().Trim()
                    Case "RES"
                        strEmailDomainName = "@charter.net" 'CHARTER RESIDENTIAL
                    Case "BUS"
                        strEmailDomainName = "@charterinternet.com" 'CHARTER BUSINESS
                    Case Else
                        strEmailDomainName = "@charter.net"
                End Select
            Else
                '##############
                'DEFAULT
                '##############
                strEmailDomainName = "@charter.net"
            End If
        Else
            '##############
            'DEFAULT
            '##############
            strEmailDomainName = "@charter.net"
        End If
        '#################################################################

        '#######################################################
        'MAKE MS PACKAGE DROPDOWN PANEL NOT VISIBLE SINCE THERE 
        'IS ONLY ONE PACKAGE AND NO MORE TO SELECT FROM
        '#######################################################
        If Not (ddlMSPackage Is Nothing) Then
            If Not (ddlMSPackage.Items Is Nothing) Then
                If ddlMSPackage.Items.Count = 1 Then
                    pnlPackageList.Visible = False
                End If
            End If
        End If

        pnlPackageList.Visible = False  ' Naqib - 2/5/2009 Diana wanted the drop down turned off
    End Sub


    Private Sub ClearDisplayMessagesForPage()
        '########################################
        'CLEAR DISPLAY MESSAGES FOR WHOLE PAGE.
        '########################################
        lblResultMessage.Text = ""
        lblCreateEmailMessage.Text = "" ' v3
        lblChangePasswordMessage.Text = ""
    End Sub

    Private Sub ClearEmailTable()
        '#############################
        'CLEAR THE TABLE OF EMAILS
        '#############################
        plhAccounts.Controls.Clear()
        pnlCreateNewEmailAccount.Visible = False
        plhAccounts.Visible = False
        btnCreateNewEmailAddress.Visible = False
        pnlNewEmailAddress.Visible = False
    End Sub

    ''' <summary>
    ''' New
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub btnAddNew_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnAddNew.Click
        '#######################################################################
        'MODIFICATION   MARK SNYDER    JUNE 20, 2008  ISSUE ID 3069
        'DO NOT FORCE THE EMAIL ADDRESS TO LOWER CASE; JUST TRIM IT.
        '#######################################################################
        Dim blnResponse As Boolean = AccountManager.Instance.AddEmailAccount(txtAddress.Text.Trim() & strEmailDomainName, txtPassword.Text, txtProvID.Text)

        If ddlMSPackage.Items.Count > 0 Then
            If Not (_account Is Nothing) Then
                If Not (_account.Person Is Nothing) Then
                    If Not (_account.Person.MSPackageInfo Is Nothing) Then
                        If blnResponse Then
                            '#################################
                            'BUILD EMAIL LIST FOR THE PACKAGE
                            '#################################
                            Dim intSelectedValue2 As Integer = ddlMSPackage.SelectedIndex
                            Call buildEmailList(_account.Person.MSPackageInfo(intSelectedValue2))

                            plhAccounts.Visible = True
                            pnlCreateNewEmailAccount.Visible = True
                            pnlNewEmailAddress.Visible = False

                            '#############
                            'CLEAR VALUES
                            '#############
                            txtAddress.Text = ""

                            '####################################################################
                            'REDISPLAY THE MANAGE EMAIL LANDING PAGE WITH THE UPDATED EMAIL LIST
                            '####################################################################
                            If Not (_account Is Nothing) Then
                                If Not (_account.Person Is Nothing) Then
                                    If Not (_account.Person.MSPackageInfo Is Nothing) Then
                                        Dim intSelectedValue As Integer = ddlMSPackage.SelectedIndex
                                        Dim lPackage As IAccount.IMember.IMemberServicePackageInfo = _account.Person.MSPackageInfo(intSelectedValue)

                                        If Not (lPackage Is Nothing) Then
                                            '####################################################
                                            'PER JOHN SPYERS 12-14-2006, THE MOST COMMON COUNT 
                                            'FOR EMAIL ADDRESSES PER PACKAGE IS 10
                                            '####################################################
                                            Dim intMaxEmailsAllowed As Integer = 0
                                            If lPackage.MaxEmailsAllowed > 0 Then
                                                intMaxEmailsAllowed = lPackage.MaxEmailsAllowed
                                            End If

                                            Select Case intMaxEmailsAllowed
                                                Case 0
                                                    pnlCannotShowEmail.Visible = True
                                                    plhAccounts.Visible = False
                                                Case Else
                                                    plhAccounts.Visible = True
                                                    pnlCannotShowEmail.Visible = False
                                            End Select

                                            If lPackage.emailsField.Count >= intMaxEmailsAllowed Then
                                                '####################################################################
                                                'TELL THE USER THAT THEY HAVE THE MAX EMAILS AVAIL FOR THIS PACKAGE.
                                                'HIDE THE BUTTON TO ADD A NEW EMAIL.
                                                '####################################################################
                                                pnlCreateNewEmailAccount.Visible = False
                                                btnCreateNewEmailAddress.Visible = False
                                            Else
                                                '#####################################################
                                                'CONTINUE ON, ALL IS GOOD TO SHOW THE NEW EMAIL BUTTON
                                                '#####################################################
                                                pnlCreateNewEmailAccount.Visible = True
                                                btnCreateNewEmailAddress.Visible = True
                                            End If

                                        End If

                                        Call buildEmailList(_account.Person.MSPackageInfo(intSelectedValue))
                                    Else
                                        Call ClearEmailTable()
                                    End If
                                Else
                                    Call ClearEmailTable()
                                End If
                            Else
                                Call ClearEmailTable()
                            End If

                        Else
                            '#############################
                            'CLEAR THE TABLE OF EMAILS
                            '#############################
                            plhAccounts.Visible = False
                            pnlCreateNewEmailAccount.Visible = False
                            pnlNewEmailAddress.Visible = True
                            'pnlCreateEmailDisplayMessage.Visible = True 'shows the message on the page that theres a prob

                            '##################################################################################
                            '##########     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
                                    lblCreateEmailMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage & "</div>"
                                    AccountManager.Instance.ClearCCOMDisplayFriendlyExceptionMessage()
                                Case Else
                                    lblCreateEmailMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & "Sorry, we're unable to take your request at this time." & "</div>"
                            End Select
                            '##################################################################################
                        End If
                    Else
                        Call ClearEmailTable()
                    End If
                Else
                    Call ClearEmailTable()
                End If
            Else
                Call ClearEmailTable()
            End If
        Else
            Call ClearEmailTable()
        End If

    End Sub  ' end of  add email

    ''' <summary>
    ''' NEW
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Sub OnChangeIsPrimary(ByVal sender As Object, ByVal e As EventArgs)
        '######################
        'ADD THE EMAIL ACCOUNT 
        '######################
        Dim _radioButton As RadioButton
        Dim _accountID As String
        _radioButton = CType(sender, RadioButton)
        _accountID = Server.HtmlDecode(_radioButton.ID.Substring(_radioButton.ID.IndexOf("_") + 1))
        Dim blnResponse As Boolean = AccountManager.Instance.ChangePrimaryEmail(_accountID)

        If ddlMSPackage.Items.Count > 0 Then
            If Not (_account Is Nothing) Then
                If Not (_account.Person Is Nothing) Then
                    If Not (_account.Person.MSPackageInfo Is Nothing) Then
                        If blnResponse Then
                            '#################################
                            'BUILD EMAIL LIST FOR THE PACKAGE
                            '#################################
                            Dim intSelectedValue As Integer = ddlMSPackage.SelectedIndex
                            Call buildEmailList(_account.Person.MSPackageInfo(intSelectedValue))

                            plhAccounts.Visible = True
                            pnlCreateNewEmailAccount.Visible = True
                            pnlNewEmailAddress.Visible = False

                            '#############
                            'CLEAR VALUES
                            '#############
                            txtAddress.Text = ""
                        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
                                    lblResultMessage.Text = "<div class=""ValidationSummary"">" & AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage & "</div>"
                                    AccountManager.Instance.ClearCCOMDisplayFriendlyExceptionMessage()
                                Case Else
                                    lblResultMessage.Text = "<div class=""ValidationSummary"">" & "Sorry, we're unable to take your request at this time." & "</div>"
                            End Select
                            '##################################################################################
                        End If

                    Else
                        Call ClearEmailTable()
                    End If
                Else
                    Call ClearEmailTable()
                End If
            Else
                Call ClearEmailTable()
            End If
        Else
            Call ClearEmailTable()
        End If


    End Sub


    ''' <summary>
    ''' NEW
    ''' </summary>
    ''' <param name="source"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub OnChangePassword(ByVal source As Object, ByVal e As EventArgs)
        strSectionTitle = "Edit Email Account"
        Dim _linkButton As LinkButton = CType(source, LinkButton)
        Dim _EmailID As String = Server.HtmlDecode(_linkButton.ID.Substring(_linkButton.ID.IndexOf("_") + 1))

        txtEmailAddress_EditPW.Text = _EmailID

        pnlPackageList.Visible = False
        plhAccounts.Visible = False
        pnlCreateNewEmailAccount.Visible = False
        pnlNewEmailAddress.Visible = False
        pnlEditPassword.Visible = True

        txtPassword_EditPW.Focus()

    End Sub

    ''' <summary>
    ''' NEW
    ''' </summary>
    ''' <param name="source"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub OnDeleteAccount(ByVal source As Object, ByVal e As EventArgs)
        Dim _linkButton As LinkButton
        Dim _accountID As String

        _linkButton = CType(source, LinkButton)
        _accountID = Server.HtmlDecode(_linkButton.ID.Substring(_linkButton.ID.IndexOf("_") + 1))
        Dim blnResponse As Boolean = AccountManager.Instance.DeleteEmailAccount(_accountID)

        If ddlMSPackage.Items.Count > 0 Then
            If Not (_account Is Nothing) Then
                If Not (_account.Person Is Nothing) Then
                    If Not (_account.Person.MSPackageInfo Is Nothing) Then

                        If blnResponse Then
                            '#################################
                            'BUILD EMAIL LIST FOR THE PACKAGE
                            '#################################
                            Dim intSelectedValue As Integer = ddlMSPackage.SelectedIndex
                            Call buildEmailList(_account.Person.MSPackageInfo(intSelectedValue))

                            plhAccounts.Visible = True
                            pnlCreateNewEmailAccount.Visible = True
                            pnlNewEmailAddress.Visible = False

                            '#############
                            'CLEAR VALUES
                            '#############
                            txtAddress.Text = ""

                            '####################################################################
                            'REDISPLAY THE MANAGE EMAIL LANDING PAGE WITH THE UPDATED EMAIL LIST
                            '####################################################################
                            If Not (_account Is Nothing) Then
                                If Not (_account.Person Is Nothing) Then
                                    If Not (_account.Person.MSPackageInfo Is Nothing) Then
                                        Dim intSelectedValue2 As Integer = ddlMSPackage.SelectedIndex
                                        Dim lPackage As IAccount.IMember.IMemberServicePackageInfo = _account.Person.MSPackageInfo(intSelectedValue2)

                                        If Not (lPackage Is Nothing) Then
                                            '####################################################
                                            'PER JOHN SPYERS 12-14-2006, THE MOST COMMON COUNT 
                                            'FOR EMAIL ADDRESSES PER PACKAGE IS 10
                                            '####################################################
                                            Dim intMaxEmailsAllowed As Integer = 0
                                            If lPackage.MaxEmailsAllowed > 0 Then
                                                intMaxEmailsAllowed = lPackage.MaxEmailsAllowed
                                            End If

                                            Select Case intMaxEmailsAllowed
                                                Case 0
                                                    pnlCannotShowEmail.Visible = True
                                                    plhAccounts.Visible = False
                                                Case Else
                                                    plhAccounts.Visible = True
                                                    pnlCannotShowEmail.Visible = False
                                            End Select

                                            If lPackage.emailsField.Count >= intMaxEmailsAllowed Then
                                                '####################################################################
                                                'TELL THE USER THAT THEY HAVE THE MAX EMAILS AVAIL FOR THIS PACKAGE.
                                                'HIDE THE BUTTON TO ADD A NEW EMAIL.
                                                '####################################################################
                                                pnlCreateNewEmailAccount.Visible = False
                                                btnCreateNewEmailAddress.Visible = False
                                            Else
                                                '#####################################################
                                                'CONTINUE ON, ALL IS GOOD TO SHOW THE NEW EMAIL BUTTON
                                                '#####################################################
                                                pnlCreateNewEmailAccount.Visible = True
                                                btnCreateNewEmailAddress.Visible = True
                                            End If

                                        End If

                                        Call buildEmailList(_account.Person.MSPackageInfo(intSelectedValue2))
                                    Else
                                        Call ClearEmailTable()
                                    End If
                                Else
                                    Call ClearEmailTable()
                                End If
                            Else
                                Call ClearEmailTable()
                            End If

                        Else
                            ''#############################
                            ''CLEAR THE TABLE OF EMAILS
                            ''#############################
                            'plhAccounts.Visible = False
                            'pnlCreateNewEmailAccount.Visible = False
                            'pnlNewEmailAddress.Visible = True
                            'pnlCreateEmailDisplayMessage.Visible = True 'shows the message on the page that theres a prob


                            '##################################################################################
                            '##########     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
                                    lblResultMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage & "</div>"
                                    AccountManager.Instance.ClearCCOMDisplayFriendlyExceptionMessage()
                                Case Else
                                    lblResultMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & "Sorry, we're unable to take your request at this time." & "</div>"
                            End Select
                            '##################################################################################
                        End If

                    Else
                        Call ClearEmailTable()
                    End If
                Else
                    Call ClearEmailTable()
                End If
            Else
                Call ClearEmailTable()
            End If
        Else
            Call ClearEmailTable()
        End If
    End Sub


    Protected Sub OnSaved()
        'RenderAccounts()
    End Sub

    Protected Sub OnCancelled()
        'RenderAccounts()
    End Sub

    ''' <summary>
    ''' NEW
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub btnChangePassword_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnChangePassword.Click
        Dim lEmailID As String = txtEmailAddress_EditPW.Text
        Dim lPW As String = txtPassword_EditPW.Text
        Dim blnResponse As Boolean = AccountManager.Instance.ChangeAccountEmailPassword(lEmailID, lPW)

        If ddlMSPackage.Items.Count > 0 Then
            If Not (_account Is Nothing) Then
                If Not (_account.Person Is Nothing) Then
                    If Not (_account.Person.MSPackageInfo Is Nothing) Then
                        If blnResponse Then
                            '#################################
                            'BUILD EMAIL LIST FOR THE PACKAGE
                            '#################################
                            Dim intSelectedValue As Integer = ddlMSPackage.SelectedIndex
                            Call buildEmailList(_account.Person.MSPackageInfo(intSelectedValue))

                            plhAccounts.Visible = True
                            pnlCreateNewEmailAccount.Visible = True
                            pnlNewEmailAddress.Visible = False
                            pnlEditPassword.Visible = False

                            '#############
                            'CLEAR VALUES
                            '#############
                            txtAddress.Text = ""

                        Else
                            ''#############################
                            ''CLEAR THE TABLE OF EMAILS
                            ''#############################
                            plhAccounts.Visible = False
                            pnlCreateNewEmailAccount.Visible = False

                            '##################################################################################
                            '##########     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
                                    lblChangePasswordMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage & "</div>"
                                    AccountManager.Instance.ClearCCOMDisplayFriendlyExceptionMessage()
                                Case Else
                                    lblChangePasswordMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & "Sorry, we're unable to take your request at this time." & "</div>"
                            End Select
                            '##################################################################################
                        End If
                    Else
                        Call ClearEmailTable()
                    End If
                Else
                    Call ClearEmailTable()
                End If
            Else
                Call ClearEmailTable()
            End If
        Else
            Call ClearEmailTable()
        End If
    End Sub


    ''' <summary>
    '''  Drop Down lister
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub ddlMSPackage_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMSPackage.SelectedIndexChanged
        '#############################
        'CLEAR THE TABLE OF EMAILS
        '#############################
        pnlNewEmailAddress.Visible = False
        pnlEditPassword.Visible = False
        txtAddress.Text = ""
    End Sub

    Protected Sub btnCancelEditPassword_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnCancelEditPassword.Click
        '#####################################################################
        '2/12/2008  SIVA DOLA
        'Redirecting to charter or charter business pages based on user type
        '#####################################################################
        _account = AccountManager.Instance.GetAccount()

        '###############################################################
        'MODIFICATION   SIVA DOLA   CHARTER BUSINESS    11/02/2008
        '###############################################################
        Dim blnCBMyAccountUse As String = Application("ENABLE_CHARTER_BUSINESS_MYACCOUNT_SECTION")
        If Not (blnCBMyAccountUse Is Nothing) Then
            Select Case blnCBMyAccountUse.Trim().ToUpper()
                Case "T"
                    '#######################################################################
                    '11/02/2008     Siva Dola   
                    'Users are directed based on account type(Residential or Bussiness) 
                    '#######################################################################
                    If Not _account Is Nothing Then
                        If Not (_account.AccountType Is Nothing) Then
                            Select Case True
                                Case _account.AccountType.ToLower().Trim() = "commercial"
                                    Response.Redirect("~/CBAccount/ManageEmailAccounts.aspx")
                                Case _account.AccountType.ToLower().Trim() = "bus"
                                    Response.Redirect("~/CBAccount/ManageEmailAccounts.aspx")
                                Case Else
                                    '#################################################################
                                    'CONSIDERED TO BE RESEDENTIAL CUSTOMERS, SEND TO THE RESIDENTIAL 
                                    'MY ACCOUNT LANDING PAGE.
                                    '#################################################################
                                    Response.Redirect("~/Account/InternetTools.aspx")
                            End Select
                        Else
                            Response.Redirect("~/Account/InternetTools.aspx")
                        End If
                    Else
                        Response.Redirect("~/Account/InternetTools.aspx")
                    End If
                    '#######################################################################
                Case Else
                    '###############################################################
                    'ASSUME THE CHARTER BUSINESS MYACCOUNT SECTION IS TURNED OFF, 
                    'JUST SHOW THE NORMAL RESEDENTIAL.
                    '###############################################################
                    Response.Redirect("~/Account/InternetTools.aspx")
            End Select
        Else
            Response.Redirect("~/Account/InternetTools.aspx")
        End If
        '###############################################################
        '###############################################################


    End Sub

    ''' <summary>
    ''' NEW
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub btnCancelCreateEmail_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnCancelCreateEmail.Click
        '#####################################################################
        '2/12/2008  SIVA DOLA
        'Redirecting to charter or charter business pages based on user type
        '#####################################################################
        _account = AccountManager.Instance.GetAccount()


        '###############################################################
        'MODIFICATION   SIVA DOLA   CHARTER BUSINESS    11/02/2008
        '###############################################################
        Dim blnCBMyAccountUse As String = Application("ENABLE_CHARTER_BUSINESS_MYACCOUNT_SECTION")
        If Not (blnCBMyAccountUse Is Nothing) Then
            Select Case blnCBMyAccountUse.Trim().ToUpper()
                Case "T"
                    '#######################################################################
                    '11/02/2008     Siva Dola   
                    'Users are directed based on account type(Residential or Bussiness) 
                    '#######################################################################
                    If Not _account Is Nothing Then
                        If Not (_account.AccountType Is Nothing) Then
                            Select Case True
                                Case _account.AccountType.ToLower().Trim() = "commercial"
                                    Response.Redirect("~/CBAccount/ManageEmailAccounts.aspx")
                                Case _account.AccountType.ToLower().Trim() = "bus"
                                    Response.Redirect("~/CBAccount/ManageEmailAccounts.aspx")
                                Case Else
                                    '#################################################################
                                    'CONSIDERED TO BE RESEDENTIAL CUSTOMERS, SEND TO THE RESIDENTIAL 
                                    'MY ACCOUNT LANDING PAGE.
                                    '#################################################################
                                    Response.Redirect("~/Account/InternetTools.aspx")
                            End Select
                        Else
                            Response.Redirect("~/Account/InternetTools.aspx")
                        End If
                    Else
                        Response.Redirect("~/Account/InternetTools.aspx")
                    End If
                    '#######################################################################
                Case Else
                    '###############################################################
                    'ASSUME THE CHARTER BUSINESS MYACCOUNT SECTION IS TURNED OFF, 
                    'JUST SHOW THE NORMAL RESEDENTIAL.
                    '###############################################################
                    Response.Redirect("~/Account/InternetTools.aspx")
            End Select
        Else
            Response.Redirect("~/Account/InternetTools.aspx")
        End If
        '###############################################################
        '###############################################################


    End Sub

    ''' <summary>
    ''' NEW  Create New Email Button
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub btnCreateNewEmailAddress_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnCreateNewEmailAddress.Click

        Panel.Visible = True

        strSectionTitle = "Create Email Address"
        plhAccounts.Visible = False
        pnlCreateNewEmailAccount.Visible = False
        pnlPackageList.Visible = False
        pnlNewEmailAddress.Visible = True

    End Sub

    ''' <summary>
    ''' NEW  EMAIL LIST BUILDER
    ''' </summary>
    ''' <param name="lPackage"></param>
    ''' <remarks></remarks>
    ''' 
    Private Sub buildEmailList(ByVal lPackage As IAccount.IMember.IMemberServicePackageInfo)
        '#################################
        'BUILD EMAIL LIST FOR THE PACKAGE
        '#################################
        Dim _table As Table
        Dim _row As TableRow
        Dim _cell As TableCell
        Dim _radio As RadioButton
        Dim _link As LinkButton
        Dim _control As Controls_ManageEmailAccount

        If Not (lPackage Is Nothing) Then
            '########################################################
            'SET THE PROVISIONING ID TO A HIDDEN FIELD, THIS IS USED 
            'WHEN A USER WANTS TO CREATE A NEW EMAIL ACCOUNT.
            '########################################################
            If Not (lPackage.provisioningIdField Is Nothing) Then
                txtProvID.Text = lPackage.provisioningIdField
            End If

            '#################################
            'LOOP THROUGH THE EMAILS
            '#################################
            If Not (lPackage.emailsField Is Nothing) Then

                _table = New Table()
                _table.ID = "tblAccounts"
                _table.CellPadding = 2
                _table.CellSpacing = 0
                _table.GridLines = GridLines.None
                _table.Style("width") = "95%"
                _table.BorderWidth = 1
                _table.BorderColor = System.Drawing.ColorTranslator.FromHtml("#78c4de")  'Drawing.Color.Gray

                '#################################
                'WRITE PACKAGE HEADER INFO 
                '#################################
                _row = New TableHeaderRow()
                _row.Style.Value = "background-color:White; font-weight:bold; color:Black"
    
                _cell = New TableHeaderCell()
                _cell.Wrap = False
                _cell.Text = lPackage.descriptionField '"Email Address"
                _cell.Attributes.Add("scope", "col")
                _row.Cells.Add(_cell)

                _cell = New TableHeaderCell()
                _cell.Text = "Parent"
                _cell.Style.Add("text-align", "center")
                _cell.Wrap = False
                _cell.Attributes.Add("scope", "col")
                _row.Cells.Add(_cell)

                _cell = New TableHeaderCell()
                _cell.Wrap = False
                _cell.Text = "Email Address Changes"
                _cell.Attributes.Add("scope", "col")
                _cell.ColumnSpan = "2"
                _cell.Style.Add("text-align", "center")

                _row.Cells.Add(_cell)
                _table.Rows.Add(_row)

                Dim intCnt As Integer = 0

                For Each EmailAccount As IAccount.IMember.IMemberServiceEmailInfo In lPackage.emailsField
                    '#################################
                    'WRITE EMAIL DETAIL LINE TO PAGE 
                    '#################################
                    If Not (EmailAccount Is Nothing) Then
                        _row = New TableRow()
                        '##############################################################
                        _cell = New TableCell()
                        If intCnt Mod 2 = 0 Then
                            _cell.Style.Value = "background-color:#F1FAFD;color:Black;line-height:26px"
                        Else
                            _cell.Style.Value = "background-color:White;color:Black;line-height:26px"
                        End If
                        If Not (EmailAccount.FullEmailAddressField Is Nothing) Then
                            _cell.Text = "&nbsp; &nbsp;" & EmailAccount.FullEmailAddressField.ToString() & ""
                        End If
                        _row.Cells.Add(_cell)
                        '##############################################################

                        '##############################################################
                        _cell = New TableCell()
                        If intCnt Mod 2 = 0 Then
                            _cell.Style.Value = "background-color:#F1FAFD;color:Black;text-align: center;"
                        Else
                            _cell.Style.Value = "background-color:White;color:Black;text-align: center;"
                        End If
                        _radio = New RadioButton()
                        If Not (EmailAccount.FullEmailAddressField Is Nothing) Then
                            _radio.ID = "rdoIsPrimary_" & Server.HtmlEncode(EmailAccount.FullEmailAddressField.ToString())
                        End If
                        _radio.AutoPostBack = True
                        _radio.CausesValidation = False
                        _radio.Checked = EmailAccount.IsParentEAddressField
                        _radio.GroupName = "IsPrimary"
                        _radio.Text = ""

                        AddHandler _radio.CheckedChanged, AddressOf Me.OnChangeIsPrimary
                        _cell.Controls.Add(_radio)
                        _row.Cells.Add(_cell)
                        '##############################################################

                        '##############################################################
                        _cell = New TableCell()
                        _cell.Style("align") = "center"
                        If intCnt Mod 2 = 0 Then
                            _cell.Style.Value = "background-color:#F1FAFD;color:Black;text-align: center;"
                        Else
                            _cell.Style.Value = "background-color:White;color:Black;text-align: center;"
                        End If
                        'Dim s As Style = New Style
                        's.Font.Bold = False
                        '_cell.Style.Add("border-spacing", "15pt")

                        _link = New LinkButton()
                        If Not (EmailAccount.FullEmailAddressField Is Nothing) Then
                            _link.ID = "lnkPassword_" & Server.HtmlEncode(EmailAccount.FullEmailAddressField.ToString())
                        End If
                        _link.CausesValidation = False
                        _link.Text = "Edit Email"
                        AddHandler _link.Click, AddressOf Me.OnChangePassword

                        '#########################################################
                        'ADD A LITTLE LINE SPACING TO BETTER FORMAT THE TABLE.
                        '#########################################################
                        Dim x As New Label()
                        x.Text = "&nbsp;"
                        _cell.Controls.Add(x)

                        _cell.Controls.Add(_link)
                        _row.Cells.Add(_cell)
                        '##############################################################

                        '##############################################################
                        _cell = New TableCell()
                        If intCnt Mod 2 = 0 Then
                            _cell.Style.Value = "background-color:#F1FAFD;color:Black;text-align: center;"
                        Else
                            _cell.Style.Value = "background-color:White;color:Black;text-align: center;"
                        End If
                        _link = New LinkButton()
                        If Not (EmailAccount.FullEmailAddressField Is Nothing) Then
                            _link.ID = "lnkDelete_" & Server.HtmlEncode(EmailAccount.FullEmailAddressField.ToString())
                        End If
                        _link.CausesValidation = False
                        _link.Text = "Delete Email"
                        If EmailAccount.IsParentEAddressField Then
                            _link.Attributes.Add("onclick", "alert('You cannot delete the Parent Email Address.');return false;")
                        Else
                            If Not (EmailAccount.FullEmailAddressField Is Nothing) Then
                                _link.Attributes.Add("onclick", "return confirm('Deleting """ & EmailAccount.FullEmailAddressField.ToString() & """ will immediately remove all inbox / sent mail items stored on the server and remove all address book entries for this mailbox in webmail.  Are you sure you wish to delete this mailbox?');")
                            Else
                                _link.Attributes.Add("onclick", "return confirm('Deleting this email will immediately remove all inbox / sent mail items stored on the server and remove all address book entries for this mailbox in webmail.  Are you sure you wish to delete this mailbox?');")
                            End If
                            AddHandler _link.Click, AddressOf Me.OnDeleteAccount
                        End If
                        _cell.Controls.Add(_link)
                        _row.Cells.Add(_cell)
                        '##############################################################

                        '#########################################
                        'ADD ROW JUST CREATED TO THE HTML TABLE
                        '#########################################
                        _table.Rows.Add(_row)

                        '##############################################################
                        'INVISIBLE ROW FOR EDITING THE ACCOUNT SELECTED 
                        '##############################################################
                        _control = CType(LoadControl("../Controls/ManageEmailAccount.ascx"), Controls_ManageEmailAccount)
                        If Not (EmailAccount.FullEmailAddressField Is Nothing) Then
                            _control.AccountID = EmailAccount.FullEmailAddressField.ToString()
                        End If
                        AddHandler _control.Saved, AddressOf Me.OnSaved
                        AddHandler _control.Cancelled, AddressOf Me.OnCancelled
                        _row = New TableRow()
                        _row.Visible = False
                        _cell = New TableCell()
                        _cell.Style.Value = "background-color: #D9E4EE;"
                        _cell.ColumnSpan = 4
                        _cell.Controls.Add(_control)
                        _row.Cells.Add(_cell)
                        '##############################################################

                        '#########################################
                        'ADD ROW JUST CREATED TO THE HTML TABLE
                        '#########################################
                        _table.Rows.Add(_row)

                    End If

                    '###############################################################################
                    'THIS IS USED TO KEEP TRACK OF WHAT ROW TO HIGHLIGHT FOR COSMETIC REASONS ONLY.
                    '###############################################################################
                    intCnt += 1
                Next


                plhAccounts.Controls.Clear()
                '#########################
                'ADD EMAIL TABLE TO PAGE
                '#########################
                plhAccounts.Controls.Add(_table)
            Else
                '#####################################################################
                'FOR SOME REASON THIS PERSON GOT HERE WITH THE EMAILS IN THE PACKAGE 
                'BEING SET TO NOTHING.  THIS SHOULDNT HAPPEN SINCE THE LEFT NAVIGATION 
                'SHOULDNT SHOW A LINK TO THIS PAGE IF THERE ARE NO EMAILS.
                '#####################################################################
                Call ClearEmailTable()
            End If
        End If
    End Sub
End Class
