<%@ Application Language="VB" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Security.Principal" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="CommonBLEntities" %>
<%@ Import Namespace="CharterDotCom.WebSite.Library" %>
<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="CharterDotCom.CommonEntities.Library" %>

<script RunAt="server">	                                                        
    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
       
        '###############################################################
        'THE ERROR QUEUE IS A MESSAGE QUEUE THAT HOLDS ALL THE ERRORS 
        'FOR THE APPLICATION.  YOU COULD VIEW THE ERRORS (AT THIS STAGE) 
        'BY USING THE root/LOAD_TEST/ERRORLOGREPORT.ASPX PAGE.
        '########################################
        'LOAD THE ERROR QUEUEING LOCATION TO A APPLICATION VARIABLE 
        'SO IT CAN BE USED (AT THIS STAGE) BY THE COMMON.VB IN 
        'RemotingServices.DLL
        '###############################################################
        Application("ErrorQueueLocation") = ConfigurationManager.AppSettings("ErrorQueueLocation")
        
        Dim wsConfigProvider As New ConfigProvider.Service
        Dim ds As New DataSet
		
        Dim d As DateTime = CachedControls.getExpireDate()
        HttpContext.Current.Cache.Insert(CacheKey.OutputCacheDurationKey, DateTime.Now, Nothing, d, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing)
		
        HttpContext.Current.Application.Add("startTimeKey", "00:00:00")
        HttpContext.Current.Application.Add("endTimeKey", "05:00:00")
		
        wsConfigProvider.Url = ConfigurationManager.AppSettings("ConfigProvider.service")
        Dim configLocation As String = ConfigurationManager.AppSettings("ConfigLocation")
        ds = wsConfigProvider.GetWebConfig(configLocation)
        
        Dim appService As New CharterDotComServices
        Call appService.setInitialApplicationConfigurationValues(ds)
        Call Me.addStateCollectionToApplicationObject()

        CharterDotCom.WebSite.Library.Common.RegisterChannels()
        
        ' add support folders to cache
        Dim supportDataset As DataSet = wsConfigProvider.GetAllFolders()
        Dim supportList As New List(Of CachedFolder)
        Dim folderTable As Data.DataTable
        Dim folder As CachedFolder
        If supportDataset IsNot Nothing AndAlso supportDataset.Tables.Count > 0 Then
            folderTable = supportDataset.Tables(0)
            For Each row As DataRow In folderTable.Rows
                folder.ID = CInt(row("DocumentFolderID"))
                folder.Name = CStr(row("DocumentFolderName"))
                folder.ParentID = CInt(row("ParentFolderID"))
                supportList.Add(folder)
            Next
        End If
      
        HttpContext.Current.Cache.Add(CharterDotCom.CommonEntities.Library.Common.m_SUPPORT_CACHE, supportList, Nothing, d, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing)

        'Loads all template header information into the 
        'application state object on key templateHeaderInfo.  The key
        'is a xmlnode 
        Dim manager As BLContentManager = New BLContentManager()
        manager.LoadTemplateHeaderInfo()
       
    End Sub
	
    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim identity As FormsIdentity
        Dim roles As String()
        Dim principal As GenericPrincipal
		        
        If Request.IsAuthenticated Then
            identity = CType(Context.User.Identity, FormsIdentity)
            roles = identity.Ticket.UserData.Split(";"c)
            principal = New GenericPrincipal(Context.User.Identity, roles)
            Context.User = principal
        End If
    End Sub
	
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        Dim _error As Exception = Server.GetLastError()
        Dim _time As DateTime = DateTime.Now
	
        Try
            
            Try
                Dim tempErrorQLoc As String = ConfigurationManager.AppSettings("ErrorQueueLocation")
                Dim ErrorQueueLocation As String = Environment.MachineName & tempErrorQLoc
                Dim lMessageQComponent As New MessageQComponent
                '###########################################
                'LOGG THE ERROR MESSAGE TO THE ERROR QUEUE
                '###########################################
				
                ' Dim lMessageQComponent As New Charter.comErrorMessageQueing.MessageQComponent
				
                Dim appError As New ErrorLog
				
                appError.Err = _error.Message
                If Not _error.InnerException Is Nothing Then
                    appError.InnerError = _error.InnerException.ToString()
                Else
                    appError.InnerError = "no inner exception"
                End If
                
                appError.ErrorDate = _time.ToString()
                lMessageQComponent.AddAMessage(ErrorQueueLocation, appError.ToString())
				
            Catch ex As Exception
                Dim tempErrorQLoc As String = ConfigurationManager.AppSettings("ErrorQueueLocation")
                Dim ErrorQueueLocation As String = Environment.MachineName & tempErrorQLoc
                Dim lMessageQComponent As New MessageQComponent
                lMessageQComponent.AddAMessage(ErrorQueueLocation, ex.Message)
                lMessageQComponent.AddAMessage(ErrorQueueLocation, _error.Message)
                '##########################################
                'IF THIS FAILS, JUST SKIP THE LOGGING PART
                '##########################################
            End Try
            
            If TypeOf _error.InnerException Is MissingCatalogException Then
                Server.Transfer("~/Errors/MissingCatalog.aspx")
                Server.ClearError()
            End If
                        
        Catch ex As Exception
	
        End Try
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        
        Dim d As DateTime = CachedControls.getExpireDate()
        Session("sortindex") = False
        ' if cache don't have dependency key add it to cache
        If HttpContext.Current.Cache.Item(CacheKey.OutputCacheDurationKey) Is Nothing Then
            HttpContext.Current.Cache.Insert(CacheKey.OutputCacheDurationKey, DateTime.Now, Nothing, d, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing)
        End If
        
        '##############################################################
        '10/03/2007     ROD COPELAND
        'THIS CONDITION EXISTS FOR THE PURPOSES OF RUNNING FASTER FOR 
        'DEBUGING ON MY LOCALHOST. USUALY DONT NEED ALL THE SUPPORT 
        'DOCS SITTING IN MEMORY FOR MOST OF THE DEBUGGING SITUATIONS.
        '##############################################################
        Select Case Environment.MachineName.ToUpper()
            Case "KSTLINTC64V6855" 'JOHN CORTRIGHT    
            Case "KSTLINTC20V5109" ' Jude Naes
            Case "KSTLUSRS64V09YS" ' Jim Gilbert
            Case "KSTLMWEB65"      'CHARTER.COM DEV BOX    
            Case Else
                ' support cache
                If HttpContext.Current.Cache.Item(CharterDotCom.CommonEntities.Library.Common.m_SUPPORT_CACHE) Is Nothing Then
                    Dim wsConfigProvider As New ConfigProvider.Service
                    Dim supportDataset As DataSet = wsConfigProvider.GetAllFolders()
                    Dim supportList As New List(Of CachedFolder)
                    Dim folderTable As Data.DataTable
                    Dim folder As CachedFolder
                    If supportDataset IsNot Nothing AndAlso supportDataset.Tables.Count > 0 Then
                        folderTable = supportDataset.Tables(0)
                        For Each row As DataRow In folderTable.Rows
                            folder.ID = CInt(row("DocumentFolderID"))
                            folder.Name = CStr(row("DocumentFolderName"))
                            folder.ParentID = CInt(row("ParentFolderID"))
                            supportList.Add(folder)
                        Next
                    End If
                    HttpContext.Current.Cache.Add(CharterDotCom.CommonEntities.Library.Common.m_SUPPORT_CACHE, supportList, Nothing, d, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing)
        
                End If
        End Select
               
    End Sub
	
    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        'CatalogManager.Instance.PersistShoppingcart()
        ' issue 26553
        'CookieHandler.ClearCookies(HttpContext.Current.Response, _
        '                            HttpContext.Current.Request, _
        '                            Application("COOKIE_DOMAIN").ToString())
    End Sub
    
    Private Sub addStateCollectionToApplicationObject()
        Dim states As New DataTable()
        
        states.Columns.Add(New DataColumn("Abbv"))
        states.Columns.Add(New DataColumn("Name"))
		
        Dim mydataRow1 As DataRow
        mydataRow1 = states.NewRow()
        mydataRow1("Abbv") = "AL"
        mydataRow1("Name") = "Alabama"
        states.Rows.Add(mydataRow1)
		
        Dim mydataRow2 As DataRow
        mydataRow2 = states.NewRow()
        mydataRow2("Abbv") = "AK"
        mydataRow2("Name") = "Alaska"
        states.Rows.Add(mydataRow2)
		
        Dim mydataRow3 As DataRow
        mydataRow3 = states.NewRow()
        mydataRow3("Abbv") = "AZ"
        mydataRow3("Name") = "Arizona"
        states.Rows.Add(mydataRow3)
		
        Dim mydataRow4 As DataRow
        mydataRow4 = states.NewRow()
        mydataRow4("Abbv") = "AR"
        mydataRow4("Name") = "Arkansas"
        states.Rows.Add(mydataRow4)
		
        Dim mydataRow5 As DataRow
        mydataRow5 = states.NewRow()
        mydataRow5("Abbv") = "CA"
        mydataRow5("Name") = "California"
        states.Rows.Add(mydataRow5)
		
        Dim mydataRow6 As DataRow
        mydataRow6 = states.NewRow()
        mydataRow6("Abbv") = "CO"
        mydataRow6("Name") = "Colorado"
        states.Rows.Add(mydataRow6)
		
        Dim mydataRow7 As DataRow
        mydataRow7 = states.NewRow()
        mydataRow7("Abbv") = "CT"
        mydataRow7("Name") = "Connecticut"
        states.Rows.Add(mydataRow7)
		
        Dim mydataRow8 As DataRow
        mydataRow8 = states.NewRow()
        mydataRow8("Abbv") = "DE"
        mydataRow8("Name") = "Delaware"
        states.Rows.Add(mydataRow8)
		
        Dim mydataRow9 As DataRow
        mydataRow9 = states.NewRow()
        mydataRow9("Abbv") = "FL"
        mydataRow9("Name") = "Florida"
        states.Rows.Add(mydataRow9)
		
        Dim mydataRow10 As DataRow
        mydataRow10 = states.NewRow()
        mydataRow10("Abbv") = "GA"
        mydataRow10("Name") = "Georgia"
        states.Rows.Add(mydataRow10)
		
        Dim mydataRow11 As DataRow
        mydataRow11 = states.NewRow()
        mydataRow11("Abbv") = "HI"
        mydataRow11("Name") = "Hawaii"
        states.Rows.Add(mydataRow11)
		
        Dim mydataRow12 As DataRow
        mydataRow12 = states.NewRow()
        mydataRow12("Abbv") = "ID"
        mydataRow12("Name") = "Idaho"
        states.Rows.Add(mydataRow12)
		
        Dim mydataRow13 As DataRow
        mydataRow13 = states.NewRow()
        mydataRow13("Abbv") = "IL"
        mydataRow13("Name") = "Illinois"
        states.Rows.Add(mydataRow13)
		
        Dim mydataRow14 As DataRow
        mydataRow14 = states.NewRow()
        mydataRow14("Abbv") = "IN"
        mydataRow14("Name") = "Indiana"
        states.Rows.Add(mydataRow14)
		
        Dim mydataRow15 As DataRow
        mydataRow15 = states.NewRow()
        mydataRow15("Abbv") = "IA"
        mydataRow15("Name") = "Iowa"
        states.Rows.Add(mydataRow15)
		
        Dim mydataRow16 As DataRow
        mydataRow16 = states.NewRow()
        mydataRow16("Abbv") = "KS"
        mydataRow16("Name") = "Kansas"
        states.Rows.Add(mydataRow16)
		
        Dim mydataRow17 As DataRow
        mydataRow17 = states.NewRow()
        mydataRow17("Abbv") = "KY"
        mydataRow17("Name") = "Kentucky"
        states.Rows.Add(mydataRow17)
		
        Dim mydataRow18 As DataRow
        mydataRow18 = states.NewRow()
        mydataRow18("Abbv") = "LA"
        mydataRow18("Name") = "Louisiana"
        states.Rows.Add(mydataRow18)
		
        Dim mydataRow19 As DataRow
        mydataRow19 = states.NewRow()
        mydataRow19("Abbv") = "ME"
        mydataRow19("Name") = "Maine"
        states.Rows.Add(mydataRow19)
		
        Dim mydataRow20 As DataRow
        mydataRow20 = states.NewRow()
        mydataRow20("Abbv") = "MD"
        mydataRow20("Name") = "Maryland"
        states.Rows.Add(mydataRow20)
		
        Dim mydataRow21 As DataRow
        mydataRow21 = states.NewRow()
        mydataRow21("Abbv") = "MA"
        mydataRow21("Name") = "Massachusetts"
        states.Rows.Add(mydataRow21)
		
        Dim mydataRow22 As DataRow
        mydataRow22 = states.NewRow()
        mydataRow22("Abbv") = "MI"
        mydataRow22("Name") = "Michigan"
        states.Rows.Add(mydataRow22)
			
        Dim mydataRow23 As DataRow
        mydataRow23 = states.NewRow()
        mydataRow23("Abbv") = "MN"
        mydataRow23("Name") = "Minnesota"
        states.Rows.Add(mydataRow23)
	
		
        Dim mydataRow24 As DataRow
        mydataRow24 = states.NewRow()
        mydataRow24("Abbv") = "MS"
        mydataRow24("Name") = "Mississippi"
        states.Rows.Add(mydataRow24)
		
        Dim mydataRow25 As DataRow
        mydataRow25 = states.NewRow()
        mydataRow25("Abbv") = "MO"
        mydataRow25("Name") = "Missouri"
        states.Rows.Add(mydataRow25)
		
        Dim mydataRow26 As DataRow
        mydataRow26 = states.NewRow()
        mydataRow26("Abbv") = "MT"
        mydataRow26("Name") = "Montana"
        states.Rows.Add(mydataRow26)
		
        Dim mydataRow27 As DataRow
        mydataRow27 = states.NewRow()
        mydataRow27("Abbv") = "NE"
        mydataRow27("Name") = "Nebraska"
        states.Rows.Add(mydataRow27)
		
        Dim mydataRow28 As DataRow
        mydataRow28 = states.NewRow()
        mydataRow28("Abbv") = "NV"
        mydataRow28("Name") = "Nevada"
        states.Rows.Add(mydataRow28)
		
        Dim mydataRow29 As DataRow
        mydataRow29 = states.NewRow()
        mydataRow29("Abbv") = "NH"
        mydataRow29("Name") = "New Hampshire"
        states.Rows.Add(mydataRow29)
		
        Dim mydataRow30 As DataRow
        mydataRow30 = states.NewRow()
        mydataRow30("Abbv") = "NJ"
        mydataRow30("Name") = "New Jersey"
        states.Rows.Add(mydataRow30)
		
        Dim mydataRow31 As DataRow
        mydataRow31 = states.NewRow()
        mydataRow31("Abbv") = "NM"
        mydataRow31("Name") = "New Mexico"
        states.Rows.Add(mydataRow31)
		
        Dim mydataRow32 As DataRow
        mydataRow32 = states.NewRow()
        mydataRow32("Abbv") = "NY"
        mydataRow32("Name") = "New York"
        states.Rows.Add(mydataRow32)
		
		
        Dim mydataRow33 As DataRow
        mydataRow33 = states.NewRow()
        mydataRow33("Abbv") = "NC"
        mydataRow33("Name") = "North Carolina"
        states.Rows.Add(mydataRow33)
		
        Dim mydataRow34 As DataRow
        mydataRow34 = states.NewRow()
        mydataRow34("Abbv") = "ND"
        mydataRow34("Name") = "North Dakota"
        states.Rows.Add(mydataRow34)
		
        Dim mydataRow35 As DataRow
        mydataRow35 = states.NewRow()
        mydataRow35("Abbv") = "OH"
        mydataRow35("Name") = "Ohio"
        states.Rows.Add(mydataRow35)
		
        Dim mydataRow36 As DataRow
        mydataRow36 = states.NewRow()
        mydataRow36("Abbv") = "OK"
        mydataRow36("Name") = "Oklahoma"
        states.Rows.Add(mydataRow36)
		
        Dim mydataRow37 As DataRow
        mydataRow37 = states.NewRow()
        mydataRow37("Abbv") = "OR"
        mydataRow37("Name") = "Oregon"
        states.Rows.Add(mydataRow37)
		
        Dim mydataRow38 As DataRow
        mydataRow38 = states.NewRow()
        mydataRow38("Abbv") = "PA"
        mydataRow38("Name") = "Pennsylvania"
        states.Rows.Add(mydataRow38)
		
        Dim mydataRow39 As DataRow
        mydataRow39 = states.NewRow()
        mydataRow39("Abbv") = "RI"
        mydataRow39("Name") = "Rhode Island"
        states.Rows.Add(mydataRow39)
		
        Dim mydataRow40 As DataRow
        mydataRow40 = states.NewRow()
        mydataRow40("Abbv") = "SC"
        mydataRow40("Name") = "South Carolina"
        states.Rows.Add(mydataRow40)
		
        Dim mydataRow41 As DataRow
        mydataRow41 = states.NewRow()
        mydataRow41("Abbv") = "SD"
        mydataRow41("Name") = "South Dakota"
        states.Rows.Add(mydataRow41)
		
        Dim mydataRow42 As DataRow
        mydataRow42 = states.NewRow()
        mydataRow42("Abbv") = "TN"
        mydataRow42("Name") = "Tennessee"
        states.Rows.Add(mydataRow42)
		
        Dim mydataRow43 As DataRow
        mydataRow43 = states.NewRow()
        mydataRow43("Abbv") = "TX"
        mydataRow43("Name") = "Texas"
        states.Rows.Add(mydataRow43)
		
        Dim mydataRow44 As DataRow
        mydataRow44 = states.NewRow()
        mydataRow44("Abbv") = "UT"
        mydataRow44("Name") = "Utah"
        states.Rows.Add(mydataRow44)
		
        Dim mydataRow45 As DataRow
        mydataRow45 = states.NewRow()
        mydataRow45("Abbv") = "VT"
        mydataRow45("Name") = "Vermont"
        states.Rows.Add(mydataRow45)
		
        Dim mydataRow46 As DataRow
        mydataRow46 = states.NewRow()
        mydataRow46("Abbv") = "VA"
        mydataRow46("Name") = "Virginia"
        states.Rows.Add(mydataRow46)
		
        Dim mydataRow47 As DataRow
        mydataRow47 = states.NewRow()
        mydataRow47("Abbv") = "WA"
        mydataRow47("Name") = "Washington"
        states.Rows.Add(mydataRow47)
		
        Dim mydataRow48 As DataRow
        mydataRow48 = states.NewRow()
        mydataRow48("Abbv") = "WV"
        mydataRow48("Name") = "West Virginia"
        states.Rows.Add(mydataRow48)
		
        Dim mydataRow49 As DataRow
        mydataRow49 = states.NewRow()
        mydataRow49("Abbv") = "WI"
        mydataRow49("Name") = "Wisconsin"
        states.Rows.Add(mydataRow49)
		
        Dim mydataRow50 As DataRow
        mydataRow50 = states.NewRow()
        mydataRow50("Abbv") = "WY"
        mydataRow50("Name") = "Wyoming"
        states.Rows.Add(mydataRow50)
  
        Application.Add(ApplicationKey.StateList, states)
    End Sub
</script>