Imports Microsoft.VisualBasic
Imports System.Configuration
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 CharterDotCom.WebSite.Library.BLCommon
Imports System.Xml
Imports System.Collections.Generic
Imports Utilities
Imports System.OverflowException
' Imports CharterDotCom.Common.Logging


Public Class CharterCMSPage
    Inherits Page

    Dim _pageContentLibrary As XmlNode                                  'xmlnode containing all content on page
    Dim _contentProvider As IContentProvider   'provider object (used only if content is not found in cache

    Public Sub New()

    End Sub

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Try
			Dim sTempURL As String

			If Not IsPostBack Then	'we will never redirect during a post-back, don't waste the processing.

				'JPIRKEY 4/2010 MODIFICATION FOR SYNCHRONOSS
				'All the URLS trapped below will be examined before redirecting the user.  As the Sync
				'project continues more and more urls will need to be pointed to their servers.
				'First we will catch the URL the app wants to redirect to, then we will check
				'to see if that url exists in the list of defined urls and if it does, redirect that.

				'#####################################################################################################
				'ROD COPELAND   MODIFICATION    1-21-2009   CL PROJECT - GRANDFATHER OLD URLS
				'#####################################################################################################
				'FOR REDIRECTS FROM AND TO DIFFERENT PAGES AS OPPOSED TO REDIRECTING TO THE SAME PAGE, 
				'LOOK AT \APP_CODE\Handlers\RedirectHandler.VB
				'#####################################################################################################
				'THE THOUGHT OF PUTTING THIS LOGIC IN THE REDIRECT HANDLER WAS LOOKED AT.  
				'HOWEVER, WHEN DEFINING <add verb="*" path="*/visitors/products.aspx" type="RedirectHandler"/> 
				'IN THE WEB CONFIG, IT MAKES THE REQUEST FAIL OUT WHEN TRING TO REDIRECT TO THE SAME PAGE WITH 
				'A DIFFERENT QUERYSTRING PARM.  FOR EXAMPLE GOING FROM  
				'Products.aspx?MenuItem=2 TO Products.aspx?MenuItem=30.  THE Products.aspx?MenuItem=30 WOULD 
				'GET CAUGHT BY THE HANDLER AGAIN SINCE YOU CAN ONLY DEFINE THE HANDLER AT THE FILE LEVEL, 
				'NOT AT THE QUERYSTRING PARM LEVEL.  PUTTING THE REDIRECTING LOGIC FROM ONE QUERYSTRING 
				'PARM TO ANOTHER IN THE SAME PAGE NEEDS TO BE ON A PAGE BY PAGE BASIS.
				'#####################################################################################################
				If HttpContext.Current.Request.RawUrl.ToString.ToLower().Contains("/visitors/") Then
					Select Case True
						Case HttpContext.Current.Request.RawUrl.ToString.ToLower().Contains("/visitors/products.aspx?productitem=5".ToLower())
							sTempURL = "../Visitors/Products.aspx?MenuItem=2"
						Case HttpContext.Current.Request.RawUrl.ToString.ToLower().Contains("/Visitors/Products.aspx?ProductItem=5".ToLower())
							sTempURL = "../Visitors/Products.aspx?MenuItem=2"
						Case HttpContext.Current.Request.RawUrl.ToString.ToLower().Contains("/Visitors/Products.aspx?ProductItem=9".ToLower())
							sTempURL = "../Visitors/Products.aspx?MenuItem=16"
						Case HttpContext.Current.Request.RawUrl.ToString.ToLower().Contains("/Visitors/Products.aspx?ProductItem=8".ToLower())
							sTempURL = "../Visitors/Products.aspx?MenuItem=17"
						Case HttpContext.Current.Request.RawUrl.ToString.ToLower().Contains("/Visitors/Products.aspx?ProductItem=10".ToLower())
							sTempURL = "../Visitors/Products.aspx?MenuItem=15"
						Case HttpContext.Current.Request.RawUrl.ToString.ToLower().Contains("/Visitors/Products.aspx?ProductItem=6".ToLower())
							sTempURL = "../Visitors/Products.aspx?MenuItem=19"
						Case HttpContext.Current.Request.RawUrl.ToString.ToLower().Contains("/Visitors/Products.aspx?ProductItem=7".ToLower())
							sTempURL = "../Visitors/Products.aspx?MenuItem=18"
						Case HttpContext.Current.Request.RawUrl.ToString.ToLower().Contains("/Visitors/Products.aspx?ProductItem=4".ToLower())
							sTempURL = "../Visitors/Products.aspx?MenuItem=2"
					End Select
				End If
				'########################################################################################################


				'#####################################################################################################
				' ISSUE ID #6717   07-27-2009  JLN
				'#####################################################################################################
				' Description:  Link to page was being made from external sites, but page did not exist.
				'      Change:  Added following if-case structure to redirect missing page to the "root" page.
				'               Case structure was used in anticipation of similar requests in the future.
				'#####################################################################################################
				'#####################################################################################################
				' ISSUE ID #6914   08-10-2009  JLN
				'#####################################################################################################
				' Description:  Change made for issue 6717 inadvertently included MenuItems 50-59.
				'      Change:  Added QueryString check to be more specific.
				'#####################################################################################################
				If HttpContext.Current.Request.RawUrl.ToString.ToLower().Contains("/customers/") Then
					Select Case True
						Case HttpContext.Current.Request.RawUrl.ToString.ToLower().Contains("/Customers/Support.aspx?MenuItem=5".ToLower()) AndAlso Request.QueryString("MenuItem") = "5"
							sTempURL = "../Customers/Support.aspx"
					End Select
				End If
				'#####################################################################################################


				'######################################################################################
				'MODIFICATION   ROD COPELAND    JUNE 30, 2008   ISSUE ID 2366
				'######################################################################################
				'ON CERTAIN MYACCOUNT PAGES WE NEED TO BLOCK CERTAIN IPS FROM CHANGING ANYTHING IN 
				'THE MYACCOUNT SECTION.  THE ONLY EXCEPTION TO THIS AT THIS POINT IS THE LINK TO 
				'THE BILL VENDOR SITE (CHARTEREPAY.APSX).  PEOPLE MAY BE IN A COUNTRY THAT IS 
				'BLACKLISTED, CHARTER STILL WANTS TO COLLECT THEIR PAYMENT MONEY, JUST NOT ALLOW 
				'THE TO CHANGE ANY OF THE MYACCOUNT SETTINGS.
				'######################################################################################
				'IF BY CHANCE THE USER NAVIGATES DIRECTLY TO THE PAGE IN QUESTION, THIS CONDITION WILL BLOCK THEM.
				'######################################################################################
				Dim ipChecker As New Utilities.IPRestriction
				Select Case True
					Case HttpContext.Current.Request.RawUrl.ToString().Trim().ToLower().Contains("account/manageemailaccounts.aspx")
						If ipChecker.BlockIPFromMyAccountAccess() Then
							sTempURL = "~/Account/MyAccount.aspx"
						End If
					Case HttpContext.Current.Request.RawUrl.ToString().Trim().ToLower().Contains("account/mywebspace.aspx")
						If ipChecker.BlockIPFromMyAccountAccess() Then
							sTempURL = "~/Account/MyAccount.aspx"
						End If
					Case HttpContext.Current.Request.RawUrl.ToString().Trim().ToLower().Contains("account/newkeyactivated.aspx")
						If ipChecker.BlockIPFromMyAccountAccess() Then
							sTempURL = "~/Account/MyAccount.aspx"
						End If
					Case HttpContext.Current.Request.RawUrl.ToString().Trim().ToLower().Contains("account/managelogin.aspx")
						If ipChecker.BlockIPFromMyAccountAccess() Then
							sTempURL = "~/Account/MyAccount.aspx"
						End If
					Case HttpContext.Current.Request.RawUrl.ToString().Trim().ToLower().Contains("account/checkout.aspx")
						If ipChecker.BlockIPFromMyAccountAccess() Then
							sTempURL = "~/Account/MyAccount.aspx"
						End If
					Case HttpContext.Current.Request.RawUrl.ToString().Trim().ToLower().Contains("account/couponprint.aspx")
						If ipChecker.BlockIPFromMyAccountAccess() Then
							sTempURL = "~/Account/MyAccount.aspx"
						End If
					Case HttpContext.Current.Request.RawUrl.ToString().Trim().ToLower().Contains("account/highspeedsecurity.aspx")
						If ipChecker.BlockIPFromMyAccountAccess() Then
							sTempURL = "~/Account/MyAccount.aspx"
						End If
					Case HttpContext.Current.Request.RawUrl.ToString().Trim().ToLower().Contains("account/internettools.aspx")
						If ipChecker.BlockIPFromMyAccountAccess() Then
							sTempURL = "~/Account/MyAccount.aspx"
						End If
					Case HttpContext.Current.Request.RawUrl.ToString().Trim().ToLower().Contains("account/managewebspace.aspx")
						If ipChecker.BlockIPFromMyAccountAccess() Then
							sTempURL = "~/Account/MyAccount.aspx"
						End If
					Case HttpContext.Current.Request.RawUrl.ToString().Trim().ToLower().Contains("account/newkeyactivated.aspx")
						If ipChecker.BlockIPFromMyAccountAccess() Then
							sTempURL = "~/Account/MyAccount.aspx"
						End If
					Case HttpContext.Current.Request.RawUrl.ToString().Trim().ToLower().Contains("account/quickpay.aspx")
						If ipChecker.BlockIPFromMyAccountAccess() Then
							sTempURL = "~/Account/MyAccount.aspx"
						End If
					Case HttpContext.Current.Request.RawUrl.ToString().Trim().ToLower().Contains("account/redeemcoupon.aspx")
						If ipChecker.BlockIPFromMyAccountAccess() Then
							sTempURL = "~/Account/MyAccount.aspx"
						End If
					Case HttpContext.Current.Request.RawUrl.ToString().Trim().ToLower().Contains("account/upgrades.aspx")
						If ipChecker.BlockIPFromMyAccountAccess() Then
							sTempURL = "~/Account/MyAccount.aspx"
						End If
					Case HttpContext.Current.Request.RawUrl.ToString().Trim().ToLower().Contains("cbaccount/managelogin.aspx")
						If ipChecker.BlockIPFromMyAccountAccess() Then
							sTempURL = "~/CBAccount/CBMyAccount.aspx"
						End If
					Case HttpContext.Current.Request.RawUrl.ToString().Trim().ToLower().Contains("cbaccount/manageemailaccounts.aspx")
						If ipChecker.BlockIPFromMyAccountAccess() Then
							sTempURL = "~/CBAccount/CBMyAccount.aspx"
						End If
					Case Else
						sTempURL = HttpContext.Current.Request.RawUrl.ToString()
				End Select

				If sTempURL Is Nothing Then sTempURL = HttpContext.Current.Request.RawUrl.ToString()

				sTempURL = sTempURL.ToLower()

				'If we're running on Localhost, remove the /ui from the check
				If sTempURL.Substring(0, 4).Contains("/ui/") Then sTempURL = sTempURL.Replace("/ui/", "/")
				'####################################################################################
				'JPIRKEY 4-2010 Synchronoss Project - capture all urls and redirect those marked.
				'####################################################################################
				Try
					If Not AccountManager.Instance.GetAccount() Is Nothing Then
						SyncHandler.CheckSyncRedirectURL(sTempURL, AccountManager.Instance.GetAccount())
					Else
						SyncHandler.CheckSyncRedirectURL(sTempURL, Nothing)
					End If

				Catch ex As System.NullReferenceException
					'The check for GetAccount will throw an error if the session isn't built
					'Object reference error.  Check for that because it means that an un-authenticated user
					'clicked on a product link.
					SyncHandler.CheckSyncRedirectURL(sTempURL, Nothing)

				Catch ex As System.Threading.ThreadAbortException
					'if the CheckSyncRedirectURL actually does a redirect, we'll get an error here.
					'Property Evaulation Failed.  Just exit the sub, the remainder of this page
					'is no longer valid.
					Exit Sub
				End Try
			End If

			Dim _defaultSiteTitle As String = ConfigurationManager.AppSettings("SiteTitle")

			Dim WebCrawledPageCollection As Dictionary(Of String, WebCrawledPage) = Application("WEB_CRAWLED_PAGE_COLLECTION")
			Dim url As String = Request.RawUrl.Trim().ToLower()
			If url.Substring(0, 4).Contains("/ui/") Then
				url = url.Replace("/ui/", "") '/UI/ value will exist if running on local machine, non PROD, UAT, Or DEV stuff
			Else
				url = url.Substring(1) ' THIS IS AN ATTEMPT TO REMOVE THE FORWARD SLASH FROM THE URL
			End If



			If Not (WebCrawledPageCollection Is Nothing) Then
				If WebCrawledPageCollection.ContainsKey(url) Then
					If Not (WebCrawledPageCollection(url) Is Nothing) Then
						Dim lRecord As WebCrawledPage = WebCrawledPageCollection(url)
						If Not (lRecord.Title Is Nothing) Then
							Me.Page.Header.Title = lRecord.Title
						Else
							'###############################
							'DO THE NORMAL TITLE ASSIGNMENT
							'###############################
							Me.Page.Header.Title = _defaultSiteTitle
						End If
					Else
						'###############################
						'DO THE NORMAL TITLE ASSIGNMENT
						'###############################
						Me.Page.Header.Title = _defaultSiteTitle
					End If
				Else
					'###############################
					'DO THE NORMAL TITLE ASSIGNMENT
					'###############################
					Me.Page.Header.Title = _defaultSiteTitle
				End If
			Else
				'###############################
				'DO THE NORMAL TITLE ASSIGNMENT
				'###############################
				Me.Page.Header.Title = _defaultSiteTitle
			End If
		Catch ex As Exception

			'###############################################################################
			'ROD COPELAND JAN 31 2007, ALTHOUGH NO ONE HAS CONFIRMED THIS THOUGHT YET. 
			'I WOULD IMAGE WE WOULDN'T WANT TO SHOW THE REQUEST FOR FOLLOW UP PAGE 
			'JUST BECAUSE WE CANT LOAD THE PAGE TITLE CORRECTLY. SO FOR NOW, IF AN 
			'EXCEPTION OCCURS, JUST RETURN NOTHING AND CONTINUE ON TO LOAD THE REST 
			'OF THE PAGE.
			'###############################################################################

		End Try
    End Sub

    Sub SetTemplate(ByVal templateId As Int32)
        Try
            Dim temp As Object = Me.Cache.Item("Template." & templateId)
            If Not temp Is Nothing Then
                _pageContentLibrary = CType(temp, XmlNode)
            Else
                Dim providerURL As String = CStr(HttpContext.Current.Application("RemoteURL_ContentProvider"))
                _contentProvider = CType(Activator.GetObject(GetType(IContentProvider), providerURL), IContentProvider)
                Dim templateContent As String = _contentProvider.GetTemplateContent(templateId)
                Dim xdoc As XmlDocument = New XmlDocument()
                xdoc.LoadXml(templateContent)
                Me.Cache.Insert("Template." & templateId, xdoc.DocumentElement)
                _pageContentLibrary = xdoc.DocumentElement
                HttpContext.Current.Application("templateHeaderInfo") = _pageContentLibrary
            End If

            'Logger.WebLog("\UI\App_Code\CharterCMSPage\SetTemplate")

            SetPageTitle()
            SetHeaderContent()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    

    Sub SetPageTitle()
        'Try
        '    Dim _defaultSiteTitle As String = ConfigurationManager.AppSettings("SiteTitle")
        '    If _pageContentLibrary("Header").Attributes("Title").Value.Length = 0 Then
        '        Me.Page.Header.Title = _defaultSiteTitle
        '    Else
        '        Me.Page.Header.Title = _defaultSiteTitle & " - " & _pageContentLibrary("Header").Attributes("Title").Value
        '    End If
        'Catch ex As Exception

        '    '###############################################################################
        '    'ROD COPELAND JAN 31 2007, ALTHOUGH NO ONE HAS CONFIRMED THIS THOUGHT YET. 
        '    'I WOULD IMAGE WE WOULDN'T WANT TO SHOW THE REQUEST FOR FOLLOW UP PAGE 
        '    'JUST BECAUSE WE CANT LOAD THE PAGE TITLE CORRECTLY. SO FOR NOW, IF AN 
        '    'EXCEPTION OCCURS, JUST RETURN NOTHING AND CONTINUE ON TO LOAD THE REST 
        '    'OF THE PAGE.
        '    '###############################################################################

        'End Try
    End Sub

    Protected Sub SetPageTitle(ByVal title As String)
        'Try
        '    Dim _defaultSiteTitle As String = ConfigurationManager.AppSettings("SiteTitle")
        '    Me.Page.Header.Title = _defaultSiteTitle & " " & title
        'Catch ex As Exception
        '    '###############################################################################
        '    'ROD COPELAND JAN 31 2007, ALTHOUGH NO ONE HAS CONFIRMED THIS THOUGHT YET. 
        '    'I WOULD IMAGE WE WOULDN'T WANT TO SHOW THE REQUEST FOR FOLLOW UP PAGE 
        '    'JUST BECAUSE WE CANT LOAD THE PAGE TITLE CORRECTLY. SO FOR NOW, IF AN 
        '    'EXCEPTION OCCURS, JUST RETURN NOTHING AND CONTINUE ON TO LOAD THE REST 
        '    'OF THE PAGE.
        '    '###############################################################################
        'End Try

    End Sub

    Sub SetHeaderContent()
        Try
            For Each meta As XmlNode In _pageContentLibrary("Header")("Metas")
                Dim _htmlMeta As HtmlMeta = New HtmlMeta
                _htmlMeta.Name = meta.Attributes("Name").Value
                _htmlMeta.Content = meta.Attributes("Content").Value
                Me.Page.Header.Controls.Add(_htmlMeta)
            Next
        Catch ex As Exception
            '###############################################################################
            'ROD COPELAND JAN 31 2007, ALTHOUGH NO ONE HAS CONFIRMED THIS THOUGHT YET. 
            'I WOULD IMAGE WE WOULDN'T WANT TO SHOW THE REQUEST FOR FOLLOW UP PAGE 
            'JUST BECAUSE WE CANT LOAD THE PAGE TITLE CORRECTLY. SO FOR NOW, IF AN 
            'EXCEPTION OCCURS, JUST RETURN NOTHING AND CONTINUE ON TO LOAD THE REST 
            'OF THE PAGE.
            '###############################################################################
        End Try

    End Sub

End Class
