Imports System.Collections.Generic
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

Partial Class Controls_AffiliateSubmitOrder
  Inherits System.Web.UI.UserControl

  Dim _cart As ICart

  Public Property Cart() As ICart
    Get
      Return _cart
    End Get
    Set(ByVal value As ICart)
      _cart = value
    End Set
  End Property

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
      btnSubmit.ValidationGroup = "SubmitOrderGroup"
      
    End If
    ' assign shopping cart's cart property
    ucShoppingCart.Cart = Cart
  End Sub

  Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBack.Click
    Response.Redirect("AffiliateEnterInformation.aspx")
  End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSubmit.Click

        '10/24/2007 Brad Miller - Make Authenticate call
        Dim iProvider As IG2BServicesProvider = CType(Activator.GetObject(GetType(IG2BServicesProvider), CStr(HttpContext.Current.Application("RemoteURL_G2BServicesProvider"))), IG2BServicesProvider)
        Dim providerRequest As EntityG2BServices.G2BAuthenticateRequest
        providerRequest = New EntityG2BServices.G2BAuthenticateRequest()

        Dim lDoAuthenticateCall As Boolean
        Dim lAccount As IAccount = AccountManager.Instance.GetAccount()
        If lAccount IsNot Nothing Then
            With providerRequest
                .FirstName = lAccount.Person.FirstName
                .LastName = lAccount.Person.LastName
                .ServiceAddressPhone = lAccount.Phone
                .ServiceAddress.AddressLine = lAccount.Address.Street1
                .ServiceAddress.Suite = lAccount.Address.Street2
                .ServiceAddress.City = lAccount.Address.City
                .ServiceAddress.State = lAccount.Address.State
                .ServiceAddress.Zip = lAccount.Address.Zip
                .SocialSecurityNumber = lAccount.SSN
                .MSOID = lAccount.MSOID
                .TrackingID = lAccount.TrackingID


                If lAccount.AuthQuestion IsNot Nothing Then
                    For Each item As BLG2BAuthQuestion In lAccount.AuthQuestion.Values
                        If Not (item Is Nothing) Then
                            Dim lProviderQuestion As New G2BAuthFieldInput
                            If item.UserResponded Then
                                lProviderQuestion.ID = item.QuestionID
                                lProviderQuestion.Value = item.AnswerFromUser
                                .AuthFieldInput.Add(lProviderQuestion)
                                lDoAuthenticateCall = True
                            End If
                        End If

                    Next
                End If

                If lAccount.IsExistingUser Then
                    .NewCustomer = False
                Else
                    .NewCustomer = True
                End If

            End With
        End If

        '#######################################################################
        'PART OF THE 4.1 PROJECT 10/30/2007
        'PER STEPHANIE BOONE, ITS OK TO JUST DO THE AUTHENTICATE CALL AND 
        'REGARDLESS OF THE RESPONSE (ERROR OR SUCCESS), WE JUST CONTINUE 
        'ON AND G2B ASSOCIATES THE TRACKING ID WITH THE ORDER AFTER THE FACT.
        '#######################################################################
        If lDoAuthenticateCall Then
            Dim providerResponse As EntityG2BServices.G2BAuthenticateResponse
            providerResponse = CType(iProvider.Authenticate(providerRequest), EntityG2BServices.G2BAuthenticateResponse)
        End If

        'Submit the order
        If Page.IsValid Then
            Response.Redirect("Loading.aspx?" & QueryStringName.Destination + "=AFFILIATESUBMIT")
            '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 If
    End Sub
End Class
