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 Controls_MyAccountHeaderLogin
    Inherits System.Web.UI.UserControl

    Public lMinutes As Integer = 0

    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()

        pnlUnauthenticated.Visible = False
        pnlAuthenticated.Visible = False

        If Request.IsAuthenticated Then
            member = AccountManager.Instance.GetAccount().Person

            If Not (member Is Nothing) Then

                '####################################################################################
                '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")

                        pnlAuthenticated.Visible = False
                        pnlUnauthenticated.Visible = True
                        ' issue 26553
                        SignOut()

                        Response.Redirect("~/Visitors/Timeout.aspx")
                    Else
                        '###########################################################
                        'RESET TIME LAPSE TO ZERO AND ALLOW THEM TO STAY LOGGED IN
                        '###########################################################\
                        lapsedStartTime = Now
                        Session("InActivityStartTime") = lapsedStartTime
                        CookieHandler.ResetCookieTimer(Response, _
                                                        Request, _
                                                        Application("COOKIE_DOMAIN"))
                    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.ToLower().IndexOf("visitors/home.aspx") > -1 Then
                    If Not (member Is Nothing) Then
                        '-----------------------------------------------------------------------------------------------------
                        ' ISSUE #11983  01-07-2011  JLN
                        '-----------------------------------------------------------------------------------------------------
                        ' Description:  On the My Account pages, the welcome name was not displaying for CB customers.
                        '      Change:  Check for missing first name or an asterisk (which indicates a company) and omit
                        '               the name accordingly.
                        '-----------------------------------------------------------------------------------------------------
                        If Not String.IsNullOrEmpty(member.FirstName) AndAlso member.FirstName.Trim <> "*" Then
                            notUserName.Text = String.Format("Welcome, {0}!", member.FirstName.Trim)
                        Else
                            notUserName.Text = "Welcome!"
                        End If
                        pnlAuthenticated.Visible = True
                        pnlUnauthenticated.Visible = False
                    Else
                        pnlAuthenticated.Visible = False
                        pnlUnauthenticated.Visible = True
                    End If

                Else
                    '-----------------------------------------------------------------------------------------------------
                    ' ISSUE #11983  01-07-2011  JLN
                    '-----------------------------------------------------------------------------------------------------
                    ' Description:  On the My Account pages, the welcome name was not displaying for CB customers.
                    '      Change:  Check for missing first name or an asterisk (which indicates a company) and omit
                    '               the name accordingly.
                    '-----------------------------------------------------------------------------------------------------
                    If Not String.IsNullOrEmpty(member.FirstName) AndAlso member.FirstName.Trim <> "*" Then
                        notUserName.Text = String.Format("Welcome, {0}!", member.FirstName.Trim)
                    Else
                        notUserName.Text = "Welcome!"
                    End If
                    pnlAuthenticated.Visible = True
                    pnlUnauthenticated.Visible = False

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

                ' issue 26553
                SignOut()

                Response.Redirect("~/Visitors/Login.aspx")

            End If

        Else
            pnlUnauthenticated.Visible = True
            If Request.Url.ToString.ToLower().IndexOf("account/home.aspx") > 0 Then
                ' issue 26553
                SignOut()

                Response.Redirect("http://www.charter.com/Visitors/Login.aspx")
            End If

        End If


        '###########################################################################################
        '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 localRedirectionUtility As New Utilities.CPNIPageRedirection
        If localRedirectionUtility.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("account/managelogin.aspx")) Then
                            Response.Redirect("~/account/managelogin.aspx")
                        End If
                    Else
                        If _account.Person.ContactEmailAddress.Trim() = String.Empty Then
                            If Not (Request.Url.ToString.ToLower().Contains("account/managelogin.aspx")) Then
                                Response.Redirect("~/account/managelogin.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 _account IsNot Nothing AndAlso _account.OSSProvidedAccountNumber IsNot Nothing Then
                '###########################################################################
                '6/22/2010  ROD COPELAND    SYCH PROJECT
                'THIS SECTION OF CODE IS MEANT TO BE USED ONLY FOR AN AUTHENTICATED STATE.
                '###########################################################################
                If _account.SecurityQuestion Is Nothing Then
                    '################################
                    'FORCE TO UPDATE Security QUESTION
                    '################################
                    If Not (Request.Url.ToString.ToLower().Contains("account/managelogin.aspx")) Then
                        Response.Redirect("~/account/managelogin.aspx")
                    End If
                Else
                    If _account.SecurityQuestion.Trim() = "" Then
                        '################################
                        'FORCE TO UPDATE Security QUESTION
                        '################################
                        If Not (Request.Url.ToString.ToLower().Contains("account/managelogin.aspx")) Then
                            Response.Redirect("~/account/managelogin.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("account/managelogin.aspx")) Then
                                    Response.Redirect("~/account/managelogin.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("account/managelogin.aspx")) Then
                        Response.Redirect("~/account/managelogin.aspx")
                    End If
                Else
                    If _account.SecurityAnswer.Trim() = "" Then
                        '################################
                        'FORCE TO UPDATE Security ANSWER
                        '################################
                        If Not (Request.Url.ToString.ToLower().Contains("account/managelogin.aspx")) Then
                            Response.Redirect("~/account/managelogin.aspx")
                        End If
                    End If
                End If

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

        End If


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

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

        End If
    End Sub

    Protected Sub btnLogOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogout.Click
        ' issue 26553
        SignOut()

        Response.Redirect("~/Visitors/Login.aspx")
    End Sub

    Protected Sub btnNotUser_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNotUser.Click
        ' issue 26553
        SignOut()

        Response.Redirect("~/Visitors/Login.aspx")
    End Sub

    Protected Sub SignOut()
        AccountManager.Instance.SignOut()
        FormsAuthentication.SignOut()

        ' issue 26553
        CookieHandler.ClearCookies(Response, _
                                                        Request, _
                                                        Application("COOKIE_DOMAIN"))

    End Sub
End Class