Imports System.Web.Security
Imports System.Text.RegularExpressions
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 CBMS_UserControls_CBLogin
    Inherits System.Web.UI.UserControl
    Private blnFailedAttemptLockout As Boolean
    Public strSectionTitle As String = "Login to My Account"
    Private CBcookie As HttpCookie

    Public Property Mode() As LoginMode
        Get
            If ViewState(Me.ID & "_Mode") Is Nothing Then
                Return LoginMode.Unspecified
            Else
                Return CType(ViewState(Me.ID & "_Mode"), LoginMode)
            End If
        End Get

        Set(ByVal value As LoginMode)
            ViewState(Me.ID & "_Mode") = value
        End Set
    End Property

    'Public Property Title() As String
    '	Get
    '		If ViewState(Me.ID & "_Title") Is Nothing Then
    '			Return String.Empty
    '		Else
    '			Return CType(ViewState(Me.ID & "_Title"), String)
    '		End If
    '	End Get

    '	Set(ByVal value As String)
    '		ViewState(Me.ID & "_Title") = value
    '	End Set
    'End Property

    Public ReadOnly Property IsAuthor() As Boolean
        Get
            If Server.UrlDecode(Request.Url.Query).IndexOf("/Authors/") > -1 Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblStatus.Text = ""


        '#####################################################################
        '5/19/2008  ROD COPELAND
        'To show My account landing page directly if user is already authenticated
        'in different(CB or Charter) domain.
        '#####################################################################

        If Not (Request.QueryString("un") Is Nothing) Then
            If Not (Request.QueryString("pwd") Is Nothing) Then

                SignIn(Request.QueryString("un").ToString().ToLower(), Request.QueryString("pwd").ToString(), chkHorWideRememberMe.Checked, Nothing)
            Else
                'THIS IS A NORMAL VALUE JUST SKIP AND ALLOW THE NORMAL LOGIN PAGE FLOW, NO PRE AUTHENTICATE STEP
            End If
        Else
            'THIS IS A NORMAL VALUE JUST SKIP AND ALLOW THE NORMAL LOGIN PAGE FLOW, NO PRE AUTHENTICATE STEP
        End If




        If Not IsPostBack Then
            'below cookie in not used anymore in timeout control
            'based on domain, timeout control displays login page.
            '##########################################################################
            'MODIFICATION   SIVA DOLA   3-13-2008
            'used in Timeout user control(time out user control is common for ch & cb)

            '##########################################################################

            CBcookie = New HttpCookie("MyAccountUserType", "BUS")
            CBcookie.Expires = DateTime.Now.AddYears(1)
            Response.Cookies.Add(CBcookie)

            '##########################################################################

            If Request.Cookies(CookieName.m_UserName) Is Nothing Then
                txtHorUserName.Text = String.Empty
                txtVerUserName.Text = String.Empty
                txtHorWideUserName.Text = String.Empty
                chkVerRememberMe.Checked = False
                chkHorWideRememberMe.Checked = False
                '	chkHorRememberMe.Checked = False
            Else
                txtHorUserName.Text = Request.Cookies(CookieName.m_UserName).Value
                txtVerUserName.Text = Request.Cookies(CookieName.m_UserName).Value
                txtHorWideUserName.Text = Request.Cookies(CookieName.m_UserName).Value
                chkVerRememberMe.Checked = True
                chkHorWideRememberMe.Checked = True
                '	chkHorRememberMe.Checked = True
            End If

        End If

    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

        '#####################################################################
        '6/21/2007  ROD COPELAND
        'LOCKOUT THE USER IF THEY HAVE FAILED TOO MANY TIMES FOR THIS SESSION.
        '#####################################################################
        blnFailedAttemptLockout = EvaluateFailedAttemptLockout("MyAccount_Login")

        If blnFailedAttemptLockout Then
            btnLogin.Visible = False
            strSectionTitle = "Login Failed"
            pnlVertical.Visible = False
            pnlHorizontal.Visible = False
            pnlHorizontalWide.Visible = False
            pnlAttemptLimitReached.Visible = True
        Else
            btnLogin.Visible = True
            strSectionTitle = "Login to My Account"
            pnlVertical.Visible = False
            pnlHorizontal.Visible = False
            pnlHorizontalWide.Visible = False
            pnlAttemptLimitReached.Visible = False

            Select Case Mode
                Case LoginMode.Vertical
                    pnlVertical.Visible = True
                Case LoginMode.Horizontal
                    pnlHorizontal.Visible = True
                Case LoginMode.HorizontalWide
                    pnlHorizontalWide.Visible = True
                    txtHorWideUserName.Focus()
            End Select
        End If



        '###############################################################
        'MODIFICATION   ROD COPELAND   05/08/2008
        '###############################################################
        'THIS CONDITION IS USED TO DISABLE THE MYACCOUNT SECTION OF THE 
        'SITE TO FROM BEING USED WHILE THE ISP RELEASE IS BEING DONE, 
        'WE DONT WANT PEOPLE TRYING TO LOGIN WITH ERRORS SINCE THE CORE 
        'OF THE MS AREA IS BEING CHANGED OUT.  WHILE HIDING THE LOGIN 
        'CONTROLS, WE CAN SHOW A PAGE THAT EXPLAINS WE ARE DOING SOME 
        'MAINTANENCE RIGHT NOW AND SERVICES WILL BE TEMPORARILY 
        'UNAVAILABLE AND TO PLEASE CHECK BACK IN A FEW HOURS.
        '###############################################################
        ltrMessages.Text = ""
        Dim blnDisableMyAccountLogin As String = Application("DISABLE_CHARTERDOTCOM_MYACCOUNT_LOGIN")
        If Not (blnDisableMyAccountLogin Is Nothing) Then
            Select Case blnDisableMyAccountLogin.Trim().ToUpper()
                Case "T"
                    '############################
                    'DISABLE THE LOGIN PAGE
                    '############################
                    btnLogin.Visible = False
                    strSectionTitle = "My Account Availability Status"
                    pnlVertical.Visible = False
                    pnlHorizontal.Visible = False
                    pnlHorizontalWide.Visible = False
                    pnlAttemptLimitReached.Visible = False
                    pnlMSLoginIsUnavailable.Visible = True

                    ltrMessages.Text = ContentManager.Instance.GetContent("Login:Unavailable", "1", OwnerType.Other)

                Case Else
                    '############################
                    'DONT DISABLE THE LOGIN PAGE
                    '############################
                    pnlMSLoginIsUnavailable.Visible = False
            End Select
        End If
        '###############################################################
        '###############################################################




    End Sub

    Protected Sub btnHorLogin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnHorLogin.Click
        SignIn(txtHorUserName.Text.ToLower(), txtHorPassword.Text, False, vldHorLoginCustom)
    End Sub

    Protected Sub btnVerLogin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnVerLogin.Click
        SignIn(txtVerUserName.Text.ToLower(), txtVerPassword.Text, chkVerRememberMe.Checked, vldVerLoginCustom)
    End Sub

    Protected Sub btnHorWideLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHorWideLogin.Click
        SignIn(txtHorWideUserName.Text.ToLower(), txtHorWidePassword.Text, chkHorWideRememberMe.Checked, Nothing)
    End Sub

    Protected Sub btnRegister_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRegister.Click
        '11/6/2007 Brad Miller - Added for CPNI
        If Page.IsValid Then
            Server.Transfer("CBRegister.aspx")
        End If
    End Sub

    Private Function EvaluateFailedAttemptLockout(ByVal lTypeName As String) As Boolean
        Try
            '#################################################################
            '6/21/2007  ROD COPELAND
            'SHOW THE PLEASE CONTACT CUST SERVICE PANEL, THEY MAY HAVE EXCEEDED THE
            'MAX ERRORS FOR QUICK PAY ATTEMPTS AS DEFINED BY CUST CARE 
            'DEPARTMENT AND DIANA
            '#################################################################

            '#############################################################################
            'IF DATE IS STILL ACTIVE, SHOW CALL BACK PANEL IF MAX COUNT HAS BEEN EXCEEDED
            '#############################################################################
            If Not (Session(lTypeName & "_FAILED_TotalAttemptCount") Is Nothing) Then
                If Session(lTypeName & "_FAILED_TotalAttemptCount") >= Application(lTypeName & "_FAILED_ATTEMPT_MAX_COUNT") Then


                    'we continue to lock them out until the time limit runs out then we give option to make payments again.
                    If (Session(lTypeName & "_FAILED_LockDownStartTime") Is Nothing) Then
                        Session(lTypeName & "_FAILED_LockDownStartTime") = New Date(Now.AddMinutes(CType(Application(lTypeName & "_FAILED_LOCKOUT_WAIT_MINUTES"), Integer)).Ticks())
                    End If

                    If Session(lTypeName & "_FAILED_LockDownStartTime") < Now Then
                        '################################
                        'ALLOW ACCESS TO MAKE A PAYMENT
                        '################################

                        Session.Remove(lTypeName & "_FAILED_LockDownStartTime")
                        Session.Remove(lTypeName & "_FAILED_TotalAttemptCount")
                        Return False
                    Else
                        '#########################################
                        'CONTINUE TO LOCK THEM OUT FROM PAYMENTS
                        '#########################################
                        Return True
                    End If
                Else
                    '####################################
                    'UPDATE COUNT AND LET THEM CONTINUE
                    '####################################
                    'Session(lTypeName & "_FAILED_TotalAttemptCount") = (CType(Session(lTypeName & "_FAILED_TotalAttemptCount"), Integer) + 1).ToString()
                    Return False
                End If
            End If


            Return False

        Catch ex As Exception
            Return True
        End Try
    End Function

    Private Function ValidateUsername(ByVal strTemp As String) As Boolean
        Dim strRegExp As String = ""

        If strTemp IsNot Nothing Then
            strRegExp = "^[a-zA-Z0-9._-]+$"
            Return Regex.IsMatch(strTemp, strRegExp)
        Else
            Return True
        End If

    End Function

    Private Function ValidatePassword(ByVal strTemp As String) As Boolean
        If strTemp IsNot Nothing Then
            If strTemp.IndexOf("\") > 0 OrElse strTemp.IndexOf("""") > 0 Then
                Return False
            Else
                Return True
            End If
        End If

    End Function
    '#####################################################################
    '5/19/2008  ROD COPELAND
    'If the user is not in the correct domain,
    'below function redirects the user an appropriate domain MyAccount Page 

    '#####################################################################
    Private Sub GetRedirectsurl(ByVal sURL As String, ByVal UserName As String, ByVal Password As String, ByVal AcctType As String)

        If AcctType Is Nothing Then

            Response.Redirect("~/Account/MyAccount.aspx")
        Else
            If (AcctType.Trim = String.Empty) Then
                Response.Redirect("~/Account/MyAccount.aspx")
            End If
        End If

        Select Case True

            '###########################################################################
            'The following section is for charter.com Commercial
            '###########################################################################

            Case sURL.Contains("://business.charter.com"), sURL.Contains("://www.business.charter.com")
                'CB PROD
                'WE NEED TO LEAVE THE USER IN THIS DOMAIN
                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://www.charter.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If


            Case sURL.Contains("://business.cc2uatx")
                'CB TEST
                'WE NEED TO LEAVE THE USER IN THIS DOMAIN
                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://cc2uatx.chartercom.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If


            Case sURL.Contains("://kstlmweb65:81")
                'CB DEV
                'WE NEED TO LEAVE THE USER IN THIS DOMAIN
                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")

                Else
                    Response.Redirect("https://kstlmweb65/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If

            Case sURL.Contains("://localhost/business")
                'CB LOCALHOST
                'WE NEED TO LEAVE THE USER IN THIS DOMAIN
                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("http://localhost/UI/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If





                '###########################################################################
                'The following section is for charter.com Residential

                '###########################################################################



                '###########################################################################

            Case sURL.Contains("://charter.com"), sURL.Contains("://www.charter.com")
                'CHARTER.COM PROD
                'WE NEED TO REDIRECT THE USER IN THE APPROPRIATE BUSINESS DOMAIN
                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("https://www.business.charter.com/order/CBLogin.aspx?un=" + UserName + "&pwd=" + Password)
                Else
                    Response.Redirect("~/Account/MyAccount.aspx")
                End If


            Case sURL.Contains("://cc2uatx.chartercom.com"), sURL.Contains("://cc2uatw")
                'CHARTER.COM TEST
                'WE NEED TO REDIRECT THE USER IN THE APPROPRIATE BUSINESS DOMAIN
                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("https://business.cc2uatx.chartercom.com/order/CBLogin.aspx?un=" + UserName + "&pwd=" + Password)
                Else
                    Response.Redirect("~/Account/MyAccount.aspx")
                End If


            Case sURL.Contains("://kstlmweb65/")
                'CHARTER.COM DEV
                'WE NEED TO REDIRECT THE USER IN THE APPROPRIATE BUSINESS DOMAIN
                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("https://kstlmweb65:81/order/CBLogin.aspx?un=" + UserName + "&pwd=" + Password)
                Else
                    Response.Redirect("~/Account/MyAccount.aspx")
                End If


            Case sURL.Contains("://localhost/ui")
                'CHARTER.COM LOCALHOST
                'WE NEED TO REDIRECT THE USER IN THE APPROPRIATE BUSINESS DOMAIN
                'Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("http://localhost/business/Order/CBLogin.aspx?un=" + UserName + "&pwd=" + Password)
                Else
                    Response.Redirect("~/Account/MyAccount.aspx")
                End If





                '###########################################################################
                '06-24-08   SDOLA   
                'Conditions added to handle UAT & PROD BOXES individually
                '###########################################################################

                '###########################################################################
                'The following section is for UAT BOXES
                '###########################################################################

            Case sURL.Contains("://kstlmweb66/")
                'CHARTER.COM UAT BOX

                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://cc2uatx.chartercom.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If
            Case sURL.Contains("://kstlmweb67/")
                'CHARTER.COM UAT BOX

                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://cc2uatx.chartercom.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If
            Case sURL.Contains("://kstlmccws01/")
                'CHARTER.COM UAT BOX

                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://cc2uatx.chartercom.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If
            Case sURL.Contains("://kstlmccws02/")
                'CHARTER.COM UAT BOX

                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://cc2uatx.chartercom.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If
            Case sURL.Contains("://kstlmccws03/")
                'CHARTER.COM UAT BOX

                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://cc2uatx.chartercom.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If
            Case sURL.Contains("://kstlmccws04/")
                'CHARTER.COM UAT BOX

                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://cc2uatx.chartercom.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If
            Case sURL.Contains("://kstlmccws05/")
                'CHARTER.COM UAT BOX

                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://cc2uatx.chartercom.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If


                '###########################################################################
                'The following section is for PROD BOXES
                '###########################################################################

            Case sURL.Contains("://kstlmcc2p01"), sURL.Contains("://www.charter.com")
                'CHARTER.COM PROD OX

                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://www.charter.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If

            Case sURL.Contains("://kstlmcc2p02"), sURL.Contains("://www.charter.com")
                'CHARTER.COM PROD OX

                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://www.charter.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If
            Case sURL.Contains("://kstlmccwp01"), sURL.Contains("://www.charter.com")
                'CHARTER.COM PROD OX

                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://www.charter.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If
            Case sURL.Contains("://kstlmccwp02"), sURL.Contains("://www.charter.com")
                'CHARTER.COM PROD OX

                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://www.charter.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If
            Case sURL.Contains("://kstlmccwp03"), sURL.Contains("://www.charter.com")
                'CHARTER.COM PROD OX

                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://www.charter.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If
            Case sURL.Contains("://kstlmccwp04"), sURL.Contains("://www.charter.com")
                'CHARTER.COM PROD OX

                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://www.charter.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If
            Case sURL.Contains("://kstlmccwp05"), sURL.Contains("://www.charter.com")
                'CHARTER.COM PROD OX
                If AcctType.ToLower = "bus" Or AcctType.ToLower = "commercial" Then
                    Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                Else
                    Response.Redirect("https://www.charter.com/order/login.aspx?un=" + UserName + "&pwd=" + Password)
                End If





            Case Else
                'JUST SEND TO THE NORMAL CHARTER.COM RESEDENTIAL MYACCOUNT LANDING PAGE.
                Response.Redirect("~/Account/MyAccount.aspx")
        End Select
    End Sub
   
    Private Sub SignIn(ByVal userName As String, ByVal password As String, ByVal rememberMe As Boolean, ByVal validator As CustomValidator)
        Dim cookie As HttpCookie
        Dim ticket As FormsAuthenticationTicket
        Dim encryptedText As String
        Dim roleName As String

        Dim blnFlag As Boolean = False

        If Not (Request.QueryString("un") Is Nothing) Then
            If Not (Request.QueryString("pwd") Is Nothing) Then

                blnFlag = True
            Else
                blnFlag = False
            End If
        Else
            blnFlag = False
        End If
        '#####################################################################
        '6/21/2007  ROD COPELAND
        'LOCKOUT THE USER IF THEY HAVE FAILED TOO MANY TIMES FOR THIS SESSION.
        '#####################################################################
        blnFailedAttemptLockout = EvaluateFailedAttemptLockout("MyAccount_Login")

        If Not blnFailedAttemptLockout Then


            If Not (blnFlag) Then

                If Page.IsValid Then
                    If txtHorWideUserName.Text.Trim().Length = 0 Or txtHorWidePassword.Text.Trim().Length = 0 Then
                        lblStatus.Text = "<img src='../Images/error-icon_small.gif' /><b>Username</b> and <b>Password</b> are required."
                        'ElseIf Not ValidateUsername(txtHorWideUserName.Text.Trim()) Then
                        '    lblStatus.Text = "<img src='../Images/error-icon_small.gif' />The only special characters allowed for <b>Username</b> are .(dot), -(dash) or _(underscore)."
                        'ElseIf Not ValidatePassword(txtHorWidePassword.Text.Trim()) Then
                        '    lblStatus.Text = "<img src='../Images/error-icon_small.gif' /><b>Password</b> does not allow \(back slash) or &quot;(quotes)."
                        blnFlag = False
                    Else
                        blnFlag = True
                    End If
                Else
                    blnFlag = True
                End If
            End If
            If (blnFlag) Then
                If AccountManager.Instance.SignIn(userName, password, IsAuthor) Then

                    If rememberMe Then
                        cookie = New HttpCookie(CookieName.m_UserName, userName)
                        cookie.Expires = DateTime.Now.AddYears(1)
                        Response.Cookies.Add(cookie)
                    Else
                        Response.Cookies(CookieName.m_UserName).Expires = DateTime.Now.AddYears(-1)
                    End If


                    If Not (validator Is Nothing) Then
                        validator.IsValid = True
                    End If


                    If IsAuthor Then
                        roleName = "author"
                    Else
                        roleName = "member"
                    End If

                    ticket = New FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(60), False, roleName)
                    encryptedText = FormsAuthentication.Encrypt(ticket)
                    cookie = New HttpCookie(FormsAuthentication.FormsCookieName, encryptedText)
                    Response.Cookies.Add(cookie)

                    ' default redirect path (home page)
                    Dim defaultRedirectPath As String = "~/CBAccount/CBMyAccount.aspx"

                    ' a few pages need special handling
                    If Request.Url.ToString().IndexOf("PayYourBill.aspx") > -1 Then
                        Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                    ElseIf Request.Url.ToString().IndexOf("Home.aspx") > -1 Then
                        Response.Redirect("~/CBAccount/CBMyAccount.aspx")
                    ElseIf Request.Url.ToString().ToLower().IndexOf("order/cblogin.aspx") > -1 Then
                        '#######################################################################
                        '10/02/2007     ROD COPELAND    
                        'TO SATISFY ISSUE ID 2718 AND ALLOW THE CHARTER.NET FOLKS TO REDIRECT TO 
                        'SPECIFIC PAGES AFTER LOGIN, READ THE QUERYSTRING VALUE FOR THE TARGET 
                        'URL AND REDIRECT THE USER TO THE LOCATION.
                        '#######################################################################
                        Dim returnurl As String = Request.QueryString("returnurl")
                        Dim _account As IAccount = AccountManager.Instance.GetAccount()
                        If returnurl Is Nothing OrElse returnurl.Equals(String.Empty) Then
                            '###################################################
                            'DO THE NORMAL REDIRECT TO THE LANDING PAGE LIKELY
                            '###################################################
                            '###############################################################
                            'MODIFICATION   SIVA DOLA   CHARTER BUSINESS    11/02/2008
                            'To enable CB MyAccount section
                            '###############################################################
                            Dim blnCBMyAccountUse As String = Application("ENABLE_CHARTER_BUSINESS_MYACCOUNT_SECTION")
                            Dim url As String = Request.Url.ToString().ToLower()
                            If Not (blnCBMyAccountUse Is Nothing) Then
                                Select Case blnCBMyAccountUse.Trim().ToUpper()
                                    Case "T"

                                        '#############################################################################
                                        '5/19/2008     Siva Dola  
                                        'To enable CB MYAccount section based on EXPOSE_CB_TO_INTERNAL_TRAFFIC_ONLY value
                                        'To enable CB MYAccount section for internal users who are Commercial or Business

                                        '#############################################################################
                                        Dim strIPAddress As String = ""
                                        If Not _account Is Nothing Then
                                            If Not (_account.AccountType Is Nothing) Then
                                                Select Case _account.AccountType.ToLower().Trim()
                                                    Case "commercial", "bus"
                                                        Dim blnExposeCBToInternalTrafficOnly As String = Application("EXPOSE_CB_TO_INTERNAL_TRAFFIC_ONLY")
                                                        Select Case blnExposeCBToInternalTrafficOnly.Trim().ToUpper()
                                                            Case "T"


                                                                '#############################################################################
                                                                '6/11/2008     Siva Dola  
                                                                'To enable CB MYAccount section for internal users(based on mathced IP ADDRESS)
                                                                ' 
                                                                '#############################################################################

                                                                Dim strUserIP As String = ""
                                                                Dim arrIPAddress() As String
                                                                Dim intLoop As Integer
                                                                Dim strIp As String
                                                                Dim blnCBShow As Boolean



                                                                '########################################################################
                                                                'ROD COPELAND   MODIFICATION JULY 9, 2008
                                                                'NEED A BETTER WAY TO OBTAIN THE SOURCE IP WHEN SITTING BEHIND THE F5.
                                                                '########################################################################
                                                                Dim ipChecker As New Utilities.IPRestriction
                                                                strUserIP = ipChecker.getF5SourceIP()
                                                                If (strUserIP Is Nothing) Then
                                                                    '######################################################################
                                                                    'IF THE F5 DERIVED SOURCE IP IS NOT AVAILABLE, THEN ATTEMPT TO USE THE 
                                                                    'DEFAULT WAY OF OBTAINING THE SOURCE IP.
                                                                    '######################################################################
                                                                    strUserIP = Request.ServerVariables("REMOTE_ADDR")
                                                                End If
                                                                '########################################################################





                                                                If strUserIP.Trim() <> "" Then
                                                                    strIPAddress = Application("CB_MYACCOUNT_ALLOWED_IP_ADDRESS_LIST")
                                                                    If strIPAddress.Length > 0 Then
                                                                        arrIPAddress = strIPAddress.Split(",")
                                                                        For intLoop = 0 To arrIPAddress.Length - 1
                                                                            strIp = arrIPAddress(intLoop)
                                                                            If (0 = String.Compare(strUserIP, strIp)) Then
                                                                                blnCBShow = True
                                                                            End If
                                                                        Next
                                                                    End If
                                                                End If

                                                                If blnCBShow Then
                                                                    Call GetRedirectsurl(url, userName, password, _account.AccountType.ToLower().Trim())
                                                                Else
                                                                    Response.Redirect("~/Account/MyAccount.aspx")
                                                                End If


                                                                '#############################################################################
                                                                '' ''strIPAddress = Request.ServerVariables("REMOTE_ADDR")
                                                                ' ''If (strIPAddress Is Nothing) Then
                                                                ' ''    strIPAddress = ""
                                                                ' ''End If
                                                                ' ''If strIPAddress.Trim() <> "" Then
                                                                ' ''    If strIPAddress.Contains("192.") Or strIPAddress.Contains("127.") Then
                                                                ' ''        'IF THE DOMAIN CHARTER.COM AND THIS IS A COMMERCIAL CUSTOMER, 
                                                                ' ''        'THEN REDIRECT TO BUSINESS.CHARTER.COM AND PASS THE LOGIN ID AND PASSWORD (ENCRYPT)
                                                                ' ''        Call GetRedirectsurl(url, userName, password, _account.AccountType.ToLower().Trim())
                                                                ' ''    Else
                                                                ' ''        Response.Redirect("~/Account/MyAccount.aspx")
                                                                ' ''    End If
                                                                ' ''Else
                                                                ' ''    Response.Redirect("~/Account/MyAccount.aspx")
                                                                ' ''End If
                                                            Case Else
                                                                'ALLOW ALL PEOPLE TO USE THE CB SECTION, INTERNAL AND EXTERNAL, FULL GOLIVE.
                                                                Call GetRedirectsurl(url, userName, password, _account.AccountType.ToLower().Trim())
                                                        End Select
                                                    Case Else
                                                        '#################################################################
                                                        'CONSIDERED TO BE RESEDENTIAL CUSTOMERS, SEND TO THE RESIDENTIAL 
                                                        'MY ACCOUNT LANDING PAGE.
                                                        '#################################################################
                                                        Call GetRedirectsurl(url, userName, password, _account.AccountType.ToLower().Trim())
                                                End Select
                                            Else
                                                Response.Redirect("~/Account/MyAccount.aspx")
                                            End If
                                        Else
                                            Response.Redirect("~/Account/MyAccount.aspx")
                                        End If
                                        '#######################################################################
                                    Case Else
                                        '###############################################################
                                        'ASSUME THE CHARTER BUSINESS MYACCOUNT SECTION IS TURNED OFF, 
                                        'JUST SHOW THE NORMAL RESEDENTIAL.
                                        '###############################################################
                                        Response.Redirect("~/Account/MyAccount.aspx")
                                End Select
                            Else
                                Response.Redirect("~/Account/MyAccount.aspx")
                            End If
                            '###############################################################
                            '###############################################################








                        Else
                            '#########################################################################
                            'SEND THEM DIRECTLY TO THE REDIRECT PATH THEY HAVE SENT VIA QUERYSTRING
                            '#########################################################################
                            Response.Redirect(returnurl)
                        End If
                    Else
                        ' was a redirect page passed?
                        Dim formsAuthenticationRedirectUrl As String = FormsAuthentication.GetRedirectUrl(userName, False)
                        If formsAuthenticationRedirectUrl.Length = 0 Then
                            Response.Redirect(defaultRedirectPath)
                        Else
                            Response.Redirect(formsAuthenticationRedirectUrl)
                        End If
                    End If
                Else
                    '##################################################################################
                    '##########     E R R O R   C O D E   M A P P I N G    S O L U T I O N      #######
                    '#################          MODIFIED BY ROD COPELAND, 5/2/2007     ################ 
                    '##################################################################################
                    'WRITE THE (IDEALLY) CHARTER FRIENDLY ERROR MESSAGES TO THE PAGE.  
                    'THESE MESSAGES ARE MANAGED IN THE CHARTERDOTCOM.ADMIN.ERROR_CODE_MAPPING TABLE.
                    'THE UNDERLYING LOGIC WILL DISPLAY THE DATASOURCE ERROR MESSAGE IF A 
                    'CHARTERDOTCOM FRIENDLY ERROR MESSAGE IS NOT PRESENT ON THE RECORD.
                    'THERE MAY BE MORE THAN ONE ERROR MESSAGE THAT COMES BACK, DISPLAY 
                    'ACCORDINGLY.
                    '##################################################################################
                    Select Case True
                        Case Not AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage Is Nothing
                            lblStatus.Text = "<img src='../Images/error-icon_small.gif' />" & AccountManager.Instance.GetLastCCOMDisplayFriendlyExceptionMessage
                            AccountManager.Instance.ClearCCOMDisplayFriendlyExceptionMessage()
                        Case Else
                            lblStatus.Text = "<img src='../Images/error-icon_small.gif' />Sorry, we're unable to take your request at this time."
                    End Select
                    '##################################################################################

                    If Not (validator Is Nothing) Then
                        validator.IsValid = False
                    End If
                End If
            End If
            End If





    End Sub

    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnLogin.Click
        Response.Redirect("../Order/CBLogin.aspx")
    End Sub
End Class
