Imports System.Web.Security
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
Imports System.Collections.Generic
Partial Class CBMS_UserControls_CBMyAccount
    Inherits System.Web.UI.UserControl

    Public lMinutes As Integer = 0

    Private Function ForceMyAccountUpdate(ByVal lRequestURL As String) As Boolean
        '###########################################################################################
        'MODIFICATION   ROD COPELAND    10-17-2007
        '###########################################################################################
        'THESE CONDITIONS OF FORCING AN UPDATE WITHOUT GOING TO ANY OTHER PAGE ONLY APPLIES 
        'TO MYACCOUNT PAGES, THE USER SHOULD BE ABLE TO NAVIGATE TO THE HOME PAGE AND SUPPORT 
        'PAGES FOR EXAMPLE.  UNTIL CARE SAYES OTHERWISE.
        '###########################################################################################
        Dim lResult As Boolean = False

        If lRequestURL.ToLower().Contains("account/charterepay.aspx") Then lResult = True
        If lRequestURL.ToLower().Contains("account/checkout.aspx") Then lResult = True
        If lRequestURL.ToLower().Contains("account/highspeedsecurity.aspx") Then lResult = True
        If lRequestURL.ToLower().Contains("cbaccount/manageemailaccounts.aspx") Then lResult = True
        If lRequestURL.ToLower().Contains("cbaccount/managelogin.aspx") Then lResult = True
        If lRequestURL.ToLower().Contains("account/managewebspace.aspx") Then lResult = True
        If lRequestURL.ToLower().Contains("cbaccount/cbmyaccount.aspx") Then lResult = True
        If lRequestURL.ToLower().Contains("account/mywebspace.aspx") Then lResult = True
        If lRequestURL.ToLower().Contains("account/quickpay.aspx") Then lResult = True
        If lRequestURL.ToLower().Contains("account/upgrades.aspx") Then lResult = True

        Return lResult

    End Function

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Dim member As IAccount.IMember
        Dim _account As IAccount = AccountManager.Instance.GetAccount()

        ucLogin.Visible = False
        pnlUnauthenticated.Visible = False
        'pnlUnauthenticated.Visible = True
        pnlAuthenticated.Visible = False
        pnlAuthenticatedHome.Visible = False
        'Exit Sub
        If Request.IsAuthenticated Then
            member = AccountManager.Instance.GetAccount().Person

            If Not (member Is Nothing) Then
                '###########################################################################################
                'MODIFICATION   ROD COPELAND    10-17-2007
                '###########################################################################################
                'THESE CONDITIONS OF FORCING AN UPDATE WITHOUT GOING TO ANY OTHER PAGE ONLY APPLIES 
                'TO MYACCOUNT PAGES, THE USER SHOULD BE ABLE TO NAVIGATE TO THE HOME PAGE AND SUPPORT 
                'PAGES FOR EXAMPLE.  UNTIL CARE SAYES OTHERWISE.
                '###########################################################################################
                If ForceMyAccountUpdate(Request.Url.ToString.ToLower()) Then

                    '#######################################################################
                    'CHECK THAT ACCOUNT CONTACT EMAIL HAS A VALUE. IF NOTHING THEN, 
                    'PER JOHN SPYERS 12-14-2006, REDIRECT THE USER TO THE PAGE THAT ASKS 
                    'THE USER TO UPDATE THE CONTACT EMAIL BEFORE THEY CONTINUE.
                    '#######################################################################
                    If Not (_account Is Nothing) Then
                        If Not (_account.Person Is Nothing) Then
                            If (_account.Person.ContactEmailAddress Is Nothing) Then
                                '################################
                                'FORCE TO UPDATE CONTACT EMAIL
                                '################################
                                If Not (Request.Url.ToString.ToLower().Contains("~/CBAccount/CBManagelogin.aspx")) Then
                                    Response.Redirect("~/CBAccount/CBManagelogin.aspx")
                                End If
                            Else
                                If _account.Person.ContactEmailAddress.Trim() = String.Empty Then
                                    If Not (Request.Url.ToString.ToLower().Contains("CBAccount/CBManagelogin.aspx")) Then
                                        Response.Redirect("~/CBAccount/CBManagelogin.aspx")
                                    End If
                                End If
                            End If
                        End If

                    End If
                    '################################################






                    '#######################################################################
                    '10/02/2007     ROD COPELAND
                    'PER DAVE COLE AND THE CPNI FOLKS, IF Security QUESTION OR ANSWER IS NOT 
                    'SET, FORCE TO PAGE TO UPDATE VALUES AS REQUIRED FIELDS.
                    '#######################################################################
                    If Not (_account Is Nothing) Then
                        If _account.SecurityQuestion Is Nothing Then
                            '################################
                            'FORCE TO UPDATE Security QUESTION
                            '################################
                            If Not (Request.Url.ToString.ToLower().Contains("CBAccount/CBManagelogin.aspx")) Then
                                Response.Redirect("~/CBAccount/CBManagelogin.aspx")
                            End If
                        Else
                            If _account.SecurityQuestion.Trim() = "" Then
                                '################################
                                'FORCE TO UPDATE Security QUESTION
                                '################################
                                If Not (Request.Url.ToString.ToLower().Contains("CBAccount/CBManagelogin.aspx")) Then
                                    Response.Redirect("~/CBAccount/CBManagelogin.aspx")
                                End If
                            Else
                                '#######################################################################
                                'SHOULD HAVE A VALUE OF SOMETYPE HERE, MAKE SURE IT EXISTS IN THE 
                                'APPROVED QUESTIONS LIST, IF NOT FORCE USER TO UPDATE
                                '##################################################################
                                'THE SECURITY QUESTIONS USED FOR ALL USERS WAS FIRST COLLECTED 
                                'IN THE SIGN IN PROCESS/BLACCOUNTMANAGER.
                                '#######################################################################
                                Dim _questions As List(Of String)
                                _questions = AccountManager.Instance.GetSecurityQAWithoutAccount()
                                If Not _questions Is Nothing Then
                                    If Not _questions.Contains(_account.SecurityQuestion) Then
                                        _account.SecurityQuestion = Nothing
                                        _account.SecurityAnswer = Nothing
                                        If Not (Request.Url.ToString.ToLower().Contains("CBAccount/CBManagelogin.aspx")) Then
                                            Response.Redirect("~/CBAccount/CBManagelogin.aspx")
                                        End If
                                    End If
                                End If
                            End If

                        End If
                        If _account.SecurityAnswer Is Nothing Then
                            '################################
                            'FORCE TO UPDATE Security ANSWER
                            '################################
                            If Not (Request.Url.ToString.ToLower().Contains("CBAccount/CBManagelogin.aspx")) Then
                                Response.Redirect("~/CBAccount/CBManagelogin.aspx")
                            End If
                        Else
                            If _account.SecurityAnswer.Trim() = "" Then
                                '################################
                                'FORCE TO UPDATE Security ANSWER
                                '################################
                                If Not (Request.Url.ToString.ToLower().Contains("CBAccount/CBManagelogin.aspx")) Then
                                    Response.Redirect("~/CBAccount/CBManagelogin.aspx")
                                End If
                            End If
                        End If

                    End If
                    '################################################

                End If


                '####################################################################################
                '10/02/2007     ROD COPELAND    
                'PER ISSUE TRACKER 2792, WE NEED TO FORCE PEOPLE TO LOGOUT AFTER SO MUCH INACTIVITY
                '####################################################################################
                Dim lapsedStartTime As New DateTime

                '####################################################################################
                '6/5/2008       MARK SNYDER 
                'INITIALIZE THE INACTIVITY MINUTES VALUE SO THAT THE CLIENT SCRIPT HAS AN INITIAL
                'VALUE TO USE RATHER THAN USING ZERO. THIS PREVENTS THE MYACCOUNT PAGE FROM  
                'IMMEDIATELY RELOADING ITSELF AFTER THE INITIAL LOAD.
                '####################################################################################
                lMinutes = 15

                If Not (Session("InActivityStartTime") Is Nothing) Then
                    lapsedStartTime = Session("InActivityStartTime")

                    If Application("MYACCOUNT_FORCEOUT_INACTIVITY_MINUTES") Is Nothing Then
                        lMinutes = 15
                    Else
                        lMinutes = CType(Application("MYACCOUNT_FORCEOUT_INACTIVITY_MINUTES"), Integer)
                    End If

                    If Now > (lapsedStartTime.AddMinutes(lMinutes)) Then
                        '################################################
                        'FORCE A LOGOUT OF SESSION BECAUSE OF INACTIVITY
                        '################################################
                        Session.Remove("InActivityStartTime")

                        ucLogin.Visible = False
                        pnlAuthenticated.Visible = False
                        pnlUnauthenticated.Visible = True

                        AccountManager.Instance.SignOut()
                        FormsAuthentication.SignOut()
                        Response.Redirect("~/Visitors/CBTimeout.aspx")
                    Else
                        '###########################################################
                        'RESET TIME LAPSE TO ZERO AND ALLOW THEM TO STAY LOGGED IN
                        '###########################################################\
                        lapsedStartTime = Now
                        Session("InActivityStartTime") = lapsedStartTime
                    End If
                Else
                    '###########################################################
                    'RESET TIME LAPSE TO ZERO AND ALLOW THEM TO STAY LOGGED IN
                    '###########################################################\
                    lapsedStartTime = Now
                    Session("InActivityStartTime") = lapsedStartTime
                End If









                If Request.Url.ToString.IndexOf("Visitors/Home.aspx") > -1 Then
                    If Not (member Is Nothing) Then
                        lblGreetingHome.Text = String.Format("{0} {1}", member.FirstName, member.LastName)
                        pnlAuthenticatedHome.Visible = True
                        pnlUnauthenticated.Visible = False
                    Else
                        ucLogin.Visible = False
                        pnlAuthenticated.Visible = False
                        pnlUnauthenticated.Visible = True
                    End If

                Else
                    lblGreeting.Text = String.Format("{0} {1}", member.FirstName, member.LastName)


                    If Request.Url.ToString.IndexOf("Visitors/Home.aspx") > 0 Then
                        ucLogin.Visible = False
                    End If

                    'pnlAuthenticated.Visible = True
                    'pnlUnauthenticated.Visible = False

                    pnlAuthenticated.Visible = True
                    ucLogin.Visible = False
                    pnlUnauthenticated.Visible = False

                End If
            Else
                '####################################################################
                'MEMBER IS NOTHING SO MAKE SURE THEY ARE SIGN OUT/UNAUTHENTICATED.
                '####################################################################
                ucLogin.Visible = False
                pnlAuthenticated.Visible = False
                pnlUnauthenticated.Visible = True

                AccountManager.Instance.SignOut()
                FormsAuthentication.SignOut()
                Response.Redirect("~/Order/CBLogin.aspx")

            End If

        Else

            ucLogin.Visible = False
            pnlUnauthenticated.Visible = True

            If Request.Url.ToString.ToLower().IndexOf("account/home.aspx") > 0 Then
                AccountManager.Instance.SignOut()
                FormsAuthentication.SignOut()
                Response.Redirect("http://www.charter.com/Visitors/cbLogin.aspx")
            End If


            'If Request.Url.ToString.IndexOf("Visitors/Home.aspx") > -1 Then
            '    'ucLogin.Visible = True
            '    ucLogin.Visible = False
            '    pnlUnauthenticated.Visible = False
            'Else
            '    pnlUnauthenticated.Visible = True
            'End If
        End If

        '####################################################################################
        '6/23/2008       MARK SNYDER
        'REGISTER THE TIMEOUT JAVASCRIPT FROM THE UTILITY CLASS
        '####################################################################################
        If pnlAuthenticated.Visible Or pnlAuthenticatedHome.Visible Then
            Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "LoginTimeoutScript", Utilities.Javascript.LoginTimeoutScript(lMinutes))

            If pnlAuthenticated.Visible Then
                ltlAuthenticated.Text = Utilities.Javascript.StartLoginTimeoutScript()
            ElseIf pnlAuthenticatedHome.Visible Then
                ltlAuthenticatedHome.Text = Utilities.Javascript.StartLoginTimeoutScript()
            End If

        End If
    End Sub

    'Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
    '    FormsAuthentication.RedirectToLoginPage()
    'End Sub

    Protected Sub btnLogOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogout.Click
        AccountManager.Instance.SignOut()
        FormsAuthentication.SignOut()
        Response.Redirect("~/Order/CBLogin.aspx")
    End Sub

    Protected Sub btnLogoutHome_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogoutHome.Click
        AccountManager.Instance.SignOut()
        FormsAuthentication.SignOut()
        Response.Redirect("~/Order/CBLogin.aspx")
    End Sub
End Class
