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 Utilities

Partial Class Errors_Loading
    Inherits CharterCMSPage


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetPageTitle("Loading Page")

        Dim targetPage As String = Request.Params(QueryStringName.Destination) & Querystring.BuildFeatureChoiceQueryString(Request.QueryString)
        Dim postBack As String = Session(QueryStringName.PostBack)
        If Not (postBack Is Nothing OrElse postBack.Equals(String.Empty)) Then
            Session(QueryStringName.PostBack) = Nothing
            If Not (targetPage Is Nothing OrElse targetPage.Equals(String.Empty)) Then
                Select Case targetPage
                    Case "SUBMIT"
                        '##################################################################################
                        'MODIFICATION   ROD COPELAND    FEB 01 2008
                        'THIS CHANGE WAS REQUESTED BY SHARON RENAUD, JOE KESSLER, LINDA LAMBERT.
                        '##################################################################################
                        'COUNT FLAG IS RESET IN THE CONTROLS/SHOPPINGCART.ASCX.VB AT THIS TIME.
                        'IN THE EVENT THE USER ENDS UP ON THIS PAGE BY HITTING F5 WHILE ITS LOADING 
                        'OR NAVIGATING DIRECTLY TO IT, THE INTENT IS TO NOT SUBMIT THE WEB SERVICE CALL 
                        'TWICE BY USING THIS FLAG.
                        'TO CLEAR THESE FLAGS, THE USER WILL NEED TO CHECKOUT AGAIN.
                        '##################################################################################
                        If Not (Session("SUBMIT_ORDER_COUNT") Is Nothing) Then
                            '###########################
                            'SKIP THE SUBMIT ORDER CALL
                            '###########################
                            If Not (Session("SUBMIT_ORDER_NEXT_URL") Is Nothing) Then
                                Response.Redirect(Session("SUBMIT_ORDER_NEXT_URL"))
                            Else
                                Response.Redirect("FinishOrder.aspx?" + QueryStringName.OrderComplete + "=true")
                            End If
                        Else
                            '###########################
                            'MAKE THE SUBMIT ORDER CALL
                            '###########################
                            Session("SUBMIT_ORDER_COUNT") = Session("SUBMIT_ORDER_COUNT") + 1
                            SubmitOrder()
                        End If

                    Case "AFFILIATESUBMIT"
                        AffiliateSubmitOrder()
                    Case Else
                        Response.Redirect(targetPage)
                End Select
            Else
                Response.Redirect("~/Visitors/Home.aspx")
            End If
        Else
            Session(QueryStringName.PostBack) = "true"

            '#################################################################
            'ISSUE ID 6146      ROD COPELAND    JUNE 16, 2009
            '#################################################################
            'THE INTENT OF THIS LITERAL IS TO DO A GET ON THE CMS RECORD 
            'THAT HAS THE KNOTICE JAVASCRIPT THAT PASSES ENTITYID AND HOUSEREFID 
            'TO KNOTICE SERVERS AFTER A PERSON HAS SUCCESSFULLY LOCALIZED ON 
            'CHARTER.COM
            '#################################################################
            ltrKnoticeTracking.Text = ContentManager.Instance.GetContent("Orderflow:HouseRefAndEntityID:Knotice", 1, OwnerType.Other)
            If ltrKnoticeTracking.Text IsNot Nothing Then
                If ltrKnoticeTracking.Text.Trim() <> "" Then
                    ltrKnoticeTracking.Text = ltrKnoticeTracking.Text.Replace("{entityid}", AccountManager.Instance.GetEntityID)
                    ltrKnoticeTracking.Text = ltrKnoticeTracking.Text.Replace("{houserefid}", AccountManager.Instance.GetHouseRefID)
                End If
            End If
            '#################################################################
        End If
    End Sub

    Private Sub SubmitOrder()
        Dim schedule1 As IOrder.ISchedule = Nothing
        Dim schedule2 As IOrder.ISchedule = Nothing

        If OrderManager.Instance.SubmitOrder() Then
            OrderManager.Instance.GetSchedule(schedule1, schedule2)
            Dim showInStall As Boolean = False

            If schedule1 IsNot Nothing Then
                If schedule1.ScheduleWindow.Count > 0 Then
                    showInStall = True
                End If
            End If

            If schedule2 IsNot Nothing Then
                If schedule2.ScheduleWindow.Count > 0 Then
                    showInStall = True
                End If
            End If

            If showInStall Then
                Session("SUBMIT_ORDER_NEXT_URL") = "ScheduleInstallation.aspx"
                Response.Redirect("ScheduleInstallation.aspx")
            Else
                Session("SUBMIT_ORDER_NEXT_URL") = "FinishOrder.aspx?" + QueryStringName.OrderComplete + "=true"
                Response.Redirect("FinishOrder.aspx?" + QueryStringName.OrderComplete + "=true")
            End If

        Else
            Session("SUBMIT_ORDER_NEXT_URL") = "FinishOrder.aspx?" + QueryStringName.OrderComplete + "=false"
            Response.Redirect("FinishOrder.aspx?" + QueryStringName.OrderComplete + "=false")
        End If
    End Sub

    Private Sub AffiliateSubmitOrder()
        Dim schedule1 As IAffiliateOrder.IAffiliateSchedule = Nothing
        Dim schedule2 As IAffiliateOrder.IAffiliateSchedule = Nothing

        If AffiliateOrderManager.Instance.SubmitOrder() Then
            AffiliateOrderManager.Instance.GetSchedule(schedule1, schedule2)
            Dim showInStall As Boolean = False

            If schedule1 IsNot Nothing Then
                If schedule1.ScheduleWindow.Count > 0 Then
                    showInStall = True
                End If
            End If

            If schedule2 IsNot Nothing Then
                If schedule2.ScheduleWindow.Count > 0 Then
                    showInStall = True
                End If
            End If

            If showInStall Then
                Response.Redirect("AffiliateScheduleInstallation.aspx")
            Else
                Response.Redirect("AffiliateFinishOrder.aspx?" + QueryStringName.OrderComplete + "=true")
            End If

        Else
            Response.Redirect("AffiliateFinishOrder.aspx?" + QueryStringName.OrderComplete + "=false")
        End If
    End Sub

End Class
