Imports System.Collections.Generic
Imports System.Text
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_HighSpeedSecurity
    Inherits System.Web.UI.UserControl
    Dim _account As IAccount

    Public strSectionTitle As String = "Charter Security Suite&reg;"
    Public strIndex As String = "1"

    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
        pnlActivateKey.Visible = True

        If Not IsPostBack Then
            If Not (_account Is Nothing) Then
                If Not (_account.Person Is Nothing) Then
                    If Not (_account.Person.MSPackageInfo Is Nothing) Then

                        If Not (ddlMSPackage Is Nothing) Then
                            If Not (ddlMSPackage.Items Is Nothing) Then

                                If ddlMSPackage.Items.Count < 1 Then
                                    'ddlMSPackage.Items.Add(New ListItem("-- Choose One --", "")) 'ADD BLANK ROW

                                    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

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


                                    '#######################################################
                                    'ASSIGN THE EMAIL ADDRESS THE SECURITY KEY CONFIRMATION 
                                    'EMAILS WILL BE SENT TO
                                    '#######################################################
                                    If Not (_account.Person.ContactEmailAddress Is Nothing) Then
                                        lblPreferredEmail.Text = "<a href=""mailto:" & _account.Person.ContactEmailAddress & """>" & _account.Person.ContactEmailAddress & "</a>"
                                    End If

                                End If
                            End If
                        End If

                    End If
                End If
            End If
        End If

        Call LoadPageValues()



        '#######################################################
        '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
                    pnlMSPackageDropDown.Visible = False
                End If
            End If
        End If


    End Sub

    Private Sub LoadPageValues()
        Dim lAccount As IAccount = _account

        lblKey1.Text = ""
        txtprovID.Text = ""
        lblKeyMessage.Text = ""




        If ddlMSPackage.Items.Count > 0 Then

            strIndex = ddlMSPackage.SelectedIndex.ToString()

            If Not (lAccount Is Nothing) Then
                If Not (lAccount.Person Is Nothing) Then
                    If Not (lAccount.Person.MSPackageInfo Is Nothing) Then
                        If (lAccount.Person.MSPackageInfo.Count > 0) Then

                            Dim lSecurityKeyCnt As Integer
                            If Not (_account.Person.MSPackageInfo(ddlMSPackage.SelectedIndex.ToString()).SecurityKeys Is Nothing) Then
                                lSecurityKeyCnt = _account.Person.MSPackageInfo(ddlMSPackage.SelectedIndex.ToString()).SecurityKeys.Count()
                            Else
                                '##################
                                'DEFAULTS TO ZERO
                                '##################
                                lSecurityKeyCnt = 0
                            End If
                            Dim lMaxSecurityKeysAllowed As Integer
                            If Not (_account.Person.MSPackageInfo(ddlMSPackage.SelectedIndex.ToString()).MaxSecurityKeysAllowed = 0) Then
                                lMaxSecurityKeysAllowed = _account.Person.MSPackageInfo(ddlMSPackage.SelectedIndex.ToString()).MaxSecurityKeysAllowed
                            Else
                                '##################
                                'DEFAULTS TO ZERO
                                '##################
                                lMaxSecurityKeysAllowed = 0
                            End If
                            Select Case True
                                Case lSecurityKeyCnt >= lMaxSecurityKeysAllowed
                                    '###########################################################################
                                    'You have the maximum amount of security keys available"
                                    '###########################################################################
                                    pnlActivateKey.Visible = False
                                Case Else
                                    '###########################################################################
                                    'THIS COULD BE WHEN NO KEYS ARE ASSIGNED YET AND THEY HAVE AVAILABLE KEYS.
                                    '###########################################################################
                                    pnlActivateKey.Visible = True
                            End Select








                            If lAccount.Person.MSPackageInfo(ddlMSPackage.SelectedIndex.ToString()).MaxSecurityKeysAllowed > 0 Then
                                pnlCanShowSecurityKeys.Visible = True
                                pnlCannotShowSecurityKeys.Visible = False

                                If Not (lAccount.Person.MSPackageInfo(ddlMSPackage.SelectedIndex.ToString()).SecurityKeys Is Nothing) Then
                                    For Each SK As String In lAccount.Person.MSPackageInfo(ddlMSPackage.SelectedIndex.ToString()).SecurityKeys
                                        lblKey1.Text = lblKey1.Text.Trim() & "<a href=""javascript:opendownload('" & SK & "')"">Click Here To Install With Key " & SK & "</a>" & "<br>"
                                    Next
                                    txtprovID.Text = lAccount.Person.MSPackageInfo(ddlMSPackage.SelectedIndex.ToString()).provisioningIdField
                                Else
                                    lblKey1.Text = ""
                                    txtprovID.Text = ""
                                End If
                            Else
                                pnlCanShowSecurityKeys.Visible = False
                                pnlCannotShowSecurityKeys.Visible = True

                                lblKey1.Text = ""
                                txtprovID.Text = ""
                            End If
                        Else
                            lblKey1.Text = ""
                            txtprovID.Text = ""
                        End If
                    Else
                        lblKey1.Text = ""
                        txtprovID.Text = ""
                    End If
                End If
            Else
                lblKey1.Text = ""
                txtprovID.Text = ""
            End If

            '#############################################################################
            'SHOW THE KEYS ON THE PAGE ONLY IF THE KEYS ARE PRESENT IN THE ACCOUNT OBJECT
            '#############################################################################
            If lblKey1.Text.Trim() <> "" Then
                pnlLicenseKeys.Visible = True
            Else
                pnlLicenseKeys.Visible = False
            End If
        Else
            pnlCanShowSecurityKeys.Visible = False
            pnlCannotShowSecurityKeys.Visible = True

            lblKey1.Text = ""
            txtprovID.Text = ""
        End If

    End Sub
End Class
