Imports System.Collections.Generic
Imports System.Data
Imports System.Text
Imports System.Web.UI.WebControls
Imports CharterDotCom.CommonEntities.Library
Imports CharterDotCom.CommonEntities.Library.Enumerations
Imports CharterDotCom.CommonEntities.Library.EntityMemberServices
Imports CharterDotCom.CommonEntities.Library.EntitySupport
Imports CharterDotCom.CommonEntities.Library.EntitySearch
Imports CharterDotCom.CommonEntities.Library.EntityWebConfig
Imports CharterDotCom.CommonEntities.Library.Common
Imports CharterDotCom.WebSite.Library

''' ----------------------------------------------------------------------------
''' <summary>
''' 
'''     Project:  UI
'''       Class:  Controls_RedeemCoupon
''' 
''' Description:  User control for redeeming coupons and gift cards, and for
'''               displaying coupon redemption history.
''' 
''' </summary>
''' <remarks></remarks>
''' <history>
''' 	[jnaes]		11/23/2010	Issue #10934.  Added gift card redemption.
''' 	[jnaes]		11/24/2010	Issue #10934.  Refactored coupon redemption.
''' 	[jnaes]		12/03/2010	Issue #11651.  Changed coupon code validation
'''                                            to allow 1-10 characters.
''' 	[jnaes]		01/05/2011	Issue #10934.  Added retrieval of billing
'''                                            address for gift card redemption.
''' 	[jnaes]		01/05/2011	Issue #10934.  Changed to use OSS-provided
'''                                            account number for redemptions
'''                                            and history.
''' </history>
''' ----------------------------------------------------------------------------
Partial Class Controls_RedeemCoupon
    Inherits System.Web.UI.UserControl

#Region "Account Property"

    Dim _account As IAccount

    Public Property Account() As IAccount
        Get
            Return _account
        End Get
        Set(ByVal value As IAccount)
            _account = value
        End Set
    End Property

#End Region

#Region "AccountNumber Property"

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''    Property:  AccountNumber
    ''' 
    ''' Description:  Returns the primary account number for the current
    '''               customer.
    ''' 
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Private ReadOnly Property AccountNumber() As String
        Get
            If Me.Account.OSSProvidedAccountNumber <= 16 Then
                Return Me.Account.OSSProvidedAccountNumber
            Else
                Return Left(Me.Account.OSSProvidedAccountNumber, 16)
            End If
        End Get
    End Property

#End Region

#Region "Page Event Handlers"

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

        If Not Page.IsPostBack Then

            If Not Application("CouponSystemEnabled") Is Nothing Then
                If Application("CouponSystemEnabled") = "1" Then
                    Me.SetCouponRedemptionEntry()
                Else
                    Me.pnlRedemptionEntry.Visible = False
                    Me.pnlCouponSystemUnavailable.Visible = True
                End If
            End If
        End If

        Me.txtCouponCode1.Focus()

    End Sub

    Protected Sub dgRedemptionDetails_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgRedemptionDetails.ItemDataBound

        Dim shortDate As String

        If (e.Item.Cells(1).Text.Split(" "c).Length < 2) Then
            e.Item.Cells(1).Text = "&nbsp;&nbsp;" & e.Item.Cells(1).Text & "&nbsp;&nbsp;"
        End If

        If (e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item) Then
            e.Item.Cells(2).Text = e.Item.Cells(2).Text & "&nbsp;"
        End If

        If IsDate(e.Item.Cells(3).Text) Then
            shortDate = Convert.ToDateTime(e.Item.Cells(3).Text).ToString("MM/dd/yyyy")
            If shortDate = "01/01/0001" Then
                e.Item.Cells(3).Text = ""
            Else
                e.Item.Cells(3).Text = shortDate
            End If
        End If

    End Sub

    Protected Sub dgRedemptionDetails_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgRedemptionDetails.PageIndexChanged
        dgRedemptionDetails.CurrentPageIndex = e.NewPageIndex
        BindData()
    End Sub

#End Region

#Region "Button Event Handlers"

#Region "btnCancelGiftCard_Click"

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  btnCancelGiftCard_Click
    ''' 
    ''' Description:  Event handler subroutine that executes when the cancel
    '''               gift card redemption button is clicked.
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Protected Sub btnCancelGiftCard_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelGiftCard.Click
        Me.ClearConfirmationValues()
        Me.ClearErrorMessages()
        Me.SetCouponRedemptionEntry()
    End Sub

#End Region

#Region "btnRedeemClone_Click"

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  btnRedeemClone_Click
    ''' 
    ''' Description:  Event handler subroutine that executes when the redeem
    '''               coupon button is clicked.
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Protected Sub btnRedeemClone_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRedeemClone.Click

        Me.ClearErrorMessages()
        Session("ctrl") = Nothing

        Try

            '-------------------------------------------------------------------
            ' Validate coupon-id and account information.
            '-------------------------------------------------------------------
            Me.Validate(Me.txtCouponCode1.Text)
            Me.Validate(Me.Account)

            '-------------------------------------------------------------------
            ' Check coupon type and redeem coupon.
            '-------------------------------------------------------------------
            Dim provider As New BLAccountManager
            Dim coupon As IRedeemCoupon

            Try

                '---------------------------------------------------------------
                ' Set page to prompt for mailing address to send gift card to.
                '---------------------------------------------------------------
                If provider.IsGiftCard(Me.AccountNumber, Me.txtCouponCode1.Text) Then

                    '-----------------------------------------------------------
                    ' Retrieve billing address, if available.
                    '-----------------------------------------------------------
                    If Me.Account.BillingAddress Is Nothing Then
                        AccountManager.Instance.SetBillingAddress(provider.GetBillingAddress(Me.AccountNumber))
                        Me.Account = AccountManager.Instance.GetAccount()
                    End If

                    '-----------------------------------------------------------
                    ' Show gift card redemption controls.
                    '-----------------------------------------------------------
                    Me.ValidateAddresses(Me.Account)
                    Me.SetGiftCardRedemptionEntry()
                    Me.BindData(Me.Account)

                    '-----------------------------------------------------------
                    ' Exit for additonal user input.
                    '-----------------------------------------------------------
                    Exit Sub

                End If

                '---------------------------------------------------------------
                ' Redeem coupon.
                '---------------------------------------------------------------
                coupon = provider.RedeemCoupon(accountNumber, Me.txtCouponCode1.Text)

            Catch ex As Exception
                lblErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> &nbsp;" & provider.LastCCOMDisplayFriendlyExceptionMessage(Nothing) & "</div>"
                Throw
            End Try

            '-------------------------------------------------------------------
            ' Validate and display redeemed coupon information.
            '-------------------------------------------------------------------
            Me.Validate(coupon)
            Me.BindData(coupon)

        Catch ex As Exception
            '-------------------------------------------------------------------
            ' Display error message.
            '-------------------------------------------------------------------
            Me.lblErrorMessage.Visible = True
            If String.IsNullOrEmpty(Me.lblErrorMessage.Text) Then
                Me.lblErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> &nbsp;" & "Sorry we are unable to process your request at this time. Please check back later." & "</div>"
            End If
        End Try

    End Sub

#End Region

#Region "btnSendGiftCard_Click"

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  btnSendGiftCard_Click
    ''' 
    ''' Description:  Event handler subroutine that executes when the send
    '''               gift card button is clicked.
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Protected Sub btnSendGiftCard_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSendGiftCard.Click

        Me.ClearErrorMessages()
        Session("ctrl") = Nothing

        Try

            '-------------------------------------------------------------------
            ' Validate account information.
            '-------------------------------------------------------------------
            Me.Validate(Me.Account)

            '-------------------------------------------------------------------
            ' Redeem gift card.
            '-------------------------------------------------------------------
            Dim provider As New BLAccountManager
            Dim coupon As IRedeemCoupon

            Try

                If Me.rblGiftCardAddress.SelectedValue = "SERVICE" Then
                    coupon = provider.RedeemGiftCard(Me.AccountNumber, Me.txtCouponCode1.Text, Me.Account.Address)
                Else
                    coupon = provider.RedeemGiftCard(Me.AccountNumber, Me.txtCouponCode1.Text, Me.Account.BillingAddress)
                End If

            Catch ex As Exception
                lblErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> &nbsp;" & provider.LastCCOMDisplayFriendlyExceptionMessage(Nothing) & "</div>"
                Throw
            End Try

            '-------------------------------------------------------------------
            ' Validate and display redeemed coupon information.
            '-------------------------------------------------------------------
            Me.Validate(coupon)
            Me.BindData(coupon)

        Catch ex As Exception
            '-------------------------------------------------------------------
            ' Display error message.
            '-------------------------------------------------------------------
            Me.lblErrorMessage.Visible = True
            If String.IsNullOrEmpty(Me.lblErrorMessage.Text) Then
                Me.lblErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> &nbsp;" & "Sorry we are unable to process your request at this time. Please check back later." & "</div>"
            End If
        End Try

    End Sub

#End Region

#Region "btnViewStatusClone_Click"

    Protected Sub btnViewStatusClone_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnViewStatusClone.Click

        btnViewStatusClone.Enabled = True
        Dim strCouponNumber As String
        Dim oBLAccountManager As New BLAccountManager
        Dim colCouponStatus As List(Of ICouponStatus)
        Dim objCouponStatus As ICouponStatus
        lblErrorMessage.Visible = False
        lblHistoryErrorMessage.Visible = False

        Try
            If (String.IsNullOrEmpty(txtCouponCode1.Text) OrElse txtCouponCode1.Text.Length > 10) Then
                lblErrorMessage.Visible = True
                lblErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & "&nbsp;Please enter a 1-10 digit coupon code." & "</div>"
            Else
                'strCouponNumber = txtCouponCode1.Text & txtCouponCode2.Text & txtCouponCode3.Text
                strCouponNumber = txtCouponCode1.Text
                If Not _account Is Nothing Then

                    If Not Session("ctrl") Is Nothing Then
                        Session("ctrl") = Nothing
                    End If
                    colCouponStatus = oBLAccountManager.GetCouponStatus(Me.AccountNumber, strCouponNumber)
                    If Not colCouponStatus Is Nothing Then
                        If colCouponStatus.Count > 0 Then
                            For Each objCouponStatus In colCouponStatus ' can there be more than one status?
                                With objCouponStatus
                                    lblStatusDescription.Text = .CouponDescription
                                    'lblStatusCouponNumber.Text = txtCouponCode1.Text & " " & txtCouponCode2.Text & " " & txtCouponCode3.Text
                                    lblStatusCouponNumber.Text = txtCouponCode1.Text

                                    Select Case .CouponStatus
                                        Case "3"
                                            lblStatusStatus.Text = "Redeemed"
                                        Case "0"
                                            lblStatusStatus.Text = "Active"
                                        Case "2"
                                            lblStatusStatus.Text = "Inactive"
                                        Case "1"
                                            lblStatusStatus.Text = "Expired"
                                        Case Else
                                            lblStatusStatus.Text = .CouponStatus
                                    End Select
                                    'lblStatusStatus.Text = .CouponStatus
                                    lblStatusOffer.Text = .CouponOffer.ToString("C2")
                                    lblStatusExpirationDate.Text = .CouponEndDate
                                    ' check redeemed events existence and iterate through them
                                    If Not .RedeemedEvents Is Nothing Then
                                        If .RedeemedEvents.Count > 0 Then
                                            plhRedeemedEventStatus.Visible = True
                                            plhQualifyingEventStatus.Visible = True
                                            For Each objRedeemedevent As ICouponRedeemedEvents In .RedeemedEvents
                                                If objRedeemedevent.IsRedeemed = "Y" Then
                                                    lblRedeemedEventsStatus.Text += objRedeemedevent.EventTitle + "<br>"
                                                Else
                                                    lblQualifyingEventsStatus.Text += objRedeemedevent.EventTitle + "<br>"
                                                End If
                                            Next
                                        Else

                                        End If
                                    Else

                                    End If
                                    pnlCouponStatus.Visible = True
                                    pnlRedemptionEntry.Visible = False
                                End With
                            Next
                        Else
                            lblErrorMessage.Visible = True
                            lblErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> &nbsp;" & "Invalid Coupon" & "</div>"
                        End If
                    Else
                        lblErrorMessage.Visible = True
                        lblErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> &nbsp;" & "No such coupon exists. Please recheck the coupon number." & "</div>"
                    End If
                Else
                    lblErrorMessage.Visible = True
                    lblErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> &nbsp;" & "No such coupon exists. Please recheck the coupon number." & "</div>"
                End If
            End If
        Catch ex As Exception
            Throw ex
        Finally
            Session("ctrl") = pnlCouponStatus
        End Try
    End Sub

#End Region

#End Region

#Region "Link Button Event Handlers"

    Protected Sub lbRedemptionHistory_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbRedemptionHistory.Click, lbRedemptionHistory2.Click
        BindData()
    End Sub

    Protected Sub lbRedeem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbRedeem.Click, lbRedeemFromHistory.Click
        Me.ClearConfirmationValues()
        Me.SetCouponRedemptionEntry()
    End Sub

#End Region

#Region "Address Methods"

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  DisplayBillToAddress
    ''' 
    ''' Description:  Returns True if the Bill-To address exists and is not the
    '''               same as the Service address.
    ''' 
    ''' </summary>
    ''' <param name="billingAccount"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Private Function DisplayBillToAddress(ByVal billingAccount As IAccount) As Boolean

        If billingAccount.BillingAddress IsNot Nothing Then

            If billingAccount.Address Is Nothing Then
                Return True
            End If

            With billingAccount.BillingAddress
                If billingAccount.Address.Street1 <> .Street1 OrElse _
                   billingAccount.Address.Street2 <> .Street2 OrElse _
                   billingAccount.Address.City <> .City OrElse _
                   billingAccount.Address.State <> .State OrElse _
                   billingAccount.Address.Zip <> .Zip Then
                    Return True
                Else
                    Return False
                End If
            End With

        Else
            Return False
        End If

    End Function

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  GetAddress
    ''' 
    ''' Description:  Returns the specified address as an HTML string.
    ''' 
    ''' </summary>
    ''' <param name="addressName"></param>
    ''' <param name="customerAddress"></param>
    ''' <param name="spacer"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Private Function GetAddress(ByVal addressName As String, _
                                ByVal customerAddress As IAddress, _
                                Optional ByVal spacer As String = "&nbsp;") As String

        Dim displayAddress As New StringBuilder

        If customerAddress IsNot Nothing Then

            '-------------------------------------------------------------------
            ' Assign header for display address.
            '-------------------------------------------------------------------
            displayAddress.AppendFormat("<b>{0} Address</b><br /><br />", addressName)

            '-------------------------------------------------------------------
            ' Assign street address.
            '-------------------------------------------------------------------
            If Not String.IsNullOrEmpty(customerAddress.Street1) Then
                displayAddress.AppendFormat("{0}{1}<br />", spacer, customerAddress.Street1)
            End If

            If Not String.IsNullOrEmpty(customerAddress.Street2) Then
                displayAddress.AppendFormat("{0}{1}<br />", spacer, customerAddress.Street2)
            End If

            '-------------------------------------------------------------------
            ' Assign city, state and zip.
            '-------------------------------------------------------------------
            displayAddress.Append(spacer)

            If Not String.IsNullOrEmpty(customerAddress.City) Then
                displayAddress.AppendFormat("{0}", customerAddress.City)
            End If

            If Not String.IsNullOrEmpty(customerAddress.State) Then
                displayAddress.AppendFormat(", {0}", customerAddress.State)
            End If

            If Not String.IsNullOrEmpty(customerAddress.Zip) Then
                If customerAddress.Zip.Length <= 5 OrElse customerAddress.Zip.Contains("-") Then
                    displayAddress.AppendFormat(" {0}", customerAddress.Zip)
                Else
                    displayAddress.AppendFormat(" {0}-{1}", _
                                                customerAddress.Zip.Substring(0, 5), _
                                                customerAddress.Zip.Substring(5))
                End If
            End If

            displayAddress.Append("<br />")

        End If

        '-----------------------------------------------------------------------
        ' Return address string.
        '-----------------------------------------------------------------------
        Return displayAddress.ToString

    End Function

#End Region

#Region "BindData Methods"

    Private Sub BindData()

        Dim oBLAccountManager As New BLAccountManager
        Dim colRedemptionHistory As List(Of ICustomerRedemptionHistory)
        Dim objRedemptionHistory As ICustomerRedemptionHistory
        lblErrorMessage.Visible = False
        lblHistoryErrorMessage.Visible = False
        plhRedemptionDetails.Visible = True
        plhQualifyingEventDetails.Visible = True

        Try
            If Not _account Is Nothing Then
                If Not Session("ctrl") Is Nothing Then
                    Session("ctrl") = Nothing
                End If
                colRedemptionHistory = oBLAccountManager.GetCustomerRedemptionHistory(Me.AccountNumber)

                ' check to see that there is a redemption history...
                If Not colRedemptionHistory Is Nothing Then
                    If colRedemptionHistory(0).ReturnCode = "NoDataFound" Then
                        lblHistoryErrorMessage.Visible = True
                        lblHistoryErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & "&nbsp;You have not redeemed any coupon." & "</div>"
                        dgQualifyingEvents.Visible = False
                        dgRedemptionDetails.Visible = False
                        lblQualifyingEvents.Visible = False
                        lblRedemptionHistoryTitle.Visible = False
                        plhRedemptionDetails.Visible = False
                        plhRedemptionDetails.Visible = False
                        plhQualifyingEventDetails.Visible = False

                    ElseIf colRedemptionHistory(0).ReturnCode = "InvalidXml" Or colRedemptionHistory(0).ReturnCode = "BusinessRuleNotMet" Or colRedemptionHistory(0).ReturnCode = "Exception" Or colRedemptionHistory(0).ReturnCode = "DatabaseError" Then
                        lblHistoryErrorMessage.Visible = True
                        lblHistoryErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & "&nbsp;Sorry we are unable to process your request at this time. Please check back later." & "</div>"
                        dgQualifyingEvents.Visible = False
                        dgRedemptionDetails.Visible = False
                        lblQualifyingEvents.Visible = False
                        lblRedemptionHistoryTitle.Visible = False
                        plhRedemptionDetails.Visible = False
                        plhQualifyingEventDetails.Visible = False

                    Else
                        If colRedemptionHistory(0).CouponStatus.Count > 0 Then
                            If (colRedemptionHistory(0).CouponStatus.Count > dgRedemptionDetails.PageSize) Then
                                dgRedemptionDetails.PagerStyle.Visible = True
                            Else
                                dgRedemptionDetails.PagerStyle.Visible = False
                            End If


                            For Each objRedemptionHistory In colRedemptionHistory
                                dgRedemptionDetails.DataSource = objRedemptionHistory.CouponStatus
                                dgRedemptionDetails.DataBind()
                                dgRedemptionDetails.Visible = True
                                Dim dtQualifyingEvents As New DataTable()
                                dtQualifyingEvents.Columns.Add("CouponID")
                                dtQualifyingEvents.Columns.Add("RedeemedTitle")
                                dtQualifyingEvents.Columns.Add("QualifyingTitle")
                                For Each objCouponStatus As ICouponStatus In objRedemptionHistory.CouponStatus
                                    If Not objCouponStatus Is Nothing Then
                                        If objCouponStatus.RedeemedEvents.Count > 0 Then
                                            Dim drQualifyingEvents As DataRow = dtQualifyingEvents.NewRow()
                                            drQualifyingEvents("CouponID") = objCouponStatus.CouponId
                                            Dim strRedeemedEvent As String = ""
                                            Dim strQualifyingEvent As String = ""
                                            For Each objRedeemedEvents As ICouponRedeemedEvents In objCouponStatus.RedeemedEvents
                                                If (objRedeemedEvents.IsRedeemed = "Y") Then
                                                    strRedeemedEvent += objRedeemedEvents.EventTitle
                                                Else
                                                    strQualifyingEvent += objRedeemedEvents.EventTitle + "<br>"
                                                End If
                                            Next
                                            drQualifyingEvents("RedeemedTitle") = strRedeemedEvent
                                            drQualifyingEvents("QualifyingTitle") = strQualifyingEvent
                                            dtQualifyingEvents.Rows.Add(drQualifyingEvents)
                                        End If
                                    Else
                                        lblQualifyingEvents.Visible = False
                                        dgQualifyingEvents.Visible = False
                                        plhQualifyingEventDetails.Visible = False
                                    End If
                                Next
                                If Not dtQualifyingEvents Is Nothing And dtQualifyingEvents.Rows.Count > 0 Then
                                    dgQualifyingEvents.DataSource = dtQualifyingEvents
                                    dgQualifyingEvents.DataBind()
                                    dgQualifyingEvents.Visible = True
                                    If (dtQualifyingEvents.Rows.Count > dgQualifyingEvents.PageSize) Then
                                        dgQualifyingEvents.PagerStyle.Visible = True
                                    Else
                                        dgQualifyingEvents.PagerStyle.Visible = False
                                    End If
                                    lblQualifyingEvents.Visible = True
                                    dgQualifyingEvents.Visible = True
                                    plhQualifyingEventDetails.Visible = True
                                Else
                                    lblQualifyingEvents.Visible = False
                                    dgQualifyingEvents.Visible = False
                                    plhQualifyingEventDetails.Visible = False

                                End If
                            Next
                            'Else
                            '    lblHistoryErrorMessage.Visible = True
                            '    lblHistoryErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & "&nbsp;You have not redeemed any coupon." & "</div>"
                            '    dgQualifyingEvents.Visible = False
                            '    dgRedemptionDetails.Visible = False
                            '    lblQualifyingEvents.Visible = False
                            '    lblRedemptionHistoryTitle.Visible = False
                            '    plhRedemptionDetails.Visible = False
                            '    plhRedemptionDetails.Visible = False
                            '    plhQualifyingEventDetails.Visible = False
                        End If
                    End If
                    'Else ' if col is nothing
                    '    lblHistoryErrorMessage.Visible = True
                    '    lblHistoryErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & "&nbsp;Sorry we are unable to process your request at this time. Please check back later." & "</div>"
                    '    dgQualifyingEvents.Visible = False
                    '    dgRedemptionDetails.Visible = False
                    '    lblQualifyingEvents.Visible = False
                    '    lblRedemptionHistoryTitle.Visible = False
                    '    plhRedemptionDetails.Visible = False
                    '    plhQualifyingEventDetails.Visible = False
                End If
                pnlRedemptionEntry.Visible = False
                pnlRedemptionHistory.Visible = True
                pnlRedemptionConfirm.Visible = False
                pnlRedemptionError.Visible = False
                pnlRedemptionConfirm.Visible = False

            Else
                '_account is nothing

            End If
        Catch ex As Exception
            Throw ex
        Finally
            Session("ctrl") = pnlRedemptionHistory
        End Try

    End Sub

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  BindData
    ''' 
    ''' Description:  Binds the addresses for the specified account to the Gift
    '''               Card address radio button list control.
    ''' 
    ''' </summary>
    ''' <param name="billingAccount"></param>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Private Sub BindData(ByVal billingAccount As IAccount)

        '-------------------------------------------------------------------
        ' Assign addresses to radio button list.
        '-------------------------------------------------------------------
        Me.rblGiftCardAddress.Items.Clear()

        If billingAccount.Address IsNot Nothing Then
            Me.rblGiftCardAddress.Items.Add(New ListItem(Me.GetAddress("Service", billingAccount.Address), "SERVICE"))
        End If

        If Me.DisplayBillToAddress(billingAccount) Then
            Me.rblGiftCardAddress.Items.Add(New ListItem(Me.GetAddress("Bill To", billingAccount.BillingAddress), "BILLTO"))
        End If

        Me.rblGiftCardAddress.Items(0).Selected = True

        '-------------------------------------------------------------------
        ' Assign header prompt for radio buttons.
        '-------------------------------------------------------------------
        If Me.rblGiftCardAddress.Items.Count = 1 Then
            Me.lblGiftCardAdressPrompt.Text = "<b>Your Gift Card will be sent to the address shown below:</b><br />"
        Else
            Me.lblGiftCardAdressPrompt.Text = "<b>Select an address below to which your Gift Card will ship:</b><br />"
        End If

    End Sub

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  BindData
    ''' 
    ''' Description:  Binds the specified redeemed coupon to the confirmation
    '''               display controls.
    ''' 
    ''' </summary>
    ''' <param name="coupon"></param>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Private Sub BindData(ByVal coupon As IRedeemCoupon)

        If coupon IsNot Nothing Then

            '-------------------------------------------------------------------
            ' Change page to display confirmation of coupon redemption.
            '-------------------------------------------------------------------
            Me.SetCouponRedemptionConfirm()

            With coupon

                '---------------------------------------------------------------
                ' Assign confirmation information to controls.
                '---------------------------------------------------------------
                Me.lblConfirmationText.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/sup_icon_successChange.ccom' />&nbsp;" & .ConfirmationMessage & "</div>"
                Me.lblConfirmationNumber.Text = .ConfirmationNumber
                Me.lblCouponStatus.Text = "Redeemed"
                Me.lblCouponNumber.Text = .CouponId
                Me.lblCouponOffer.Text = .AdjustmentAmount.ToString("C2")
                Me.lblCouponDescription.Text = .CouponDescription
                Me.lblRedemptionDate.Text = .RedemptionDate

                '---------------------------------------------------------------
                ' Assign redeemed event information to controls.
                '---------------------------------------------------------------
                If .RedeemedEvents IsNot Nothing Then
                    If .RedeemedEvents.Count > 0 Then
                        Me.plhRedeemedEventsConfirm.Visible = True
                        Me.plhQualifyingEventsConfirm.Visible = True
                        For Each inEvent As ICouponRedeemedEvents In .RedeemedEvents
                            If inEvent.IsRedeemed = "1" Then
                                Me.lblRedeemedventsConfirm.Text += inEvent.EventTitle
                            Else
                                Me.lblQualifyingEventsConfirm.Text += inEvent.EventTitle + "<br>"
                            End If
                        Next
                    End If
                End If

                '---------------------------------------------------------------
                ' Assign gift card mailing address location.
                '---------------------------------------------------------------
                If Me.plhGiftCardMailingAddressConfirm.Visible Then
                    If Me.rblGiftCardAddress.SelectedValue = "SERVICE" Then
                        Me.lblGiftCardMailingAddressConfirm.Text = "Service Address"
                    Else
                        Me.lblGiftCardMailingAddressConfirm.Text = "Bill To Address"
                    End If
                End If

            End With

            '-------------------------------------------------------------------
            ' Assign control for printing.
            '-------------------------------------------------------------------
            Session("ctrl") = Me.pnlRedemptionConfirm

        End If

    End Sub

#End Region

#Region "Clear Methods"

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  ClearConfirmationValues
    ''' 
    ''' Description:  Clears the text assigned to redeemed coupon confirmation
    '''               labels.
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Private Sub ClearConfirmationValues()

        Me.txtCouponCode1.Text = String.Empty
        Me.lblConfirmationNumber.Text = String.Empty
        Me.lblCouponStatus.Text = String.Empty
        Me.lblCouponNumber.Text = String.Empty
        Me.lblCouponOffer.Text = String.Empty
        Me.lblCouponDescription.Text = String.Empty
        Me.lblRedemptionDate.Text = String.Empty
        Me.lblRedeemedventsConfirm.Text = String.Empty
        Me.lblQualifyingEventsConfirm.Text = String.Empty
        Me.lblGiftCardMailingAddressConfirm.Text = String.Empty

    End Sub

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  ClearErrorMessages
    ''' 
    ''' Description:  Clears the text assigned to error message labels.
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Private Sub ClearErrorMessages()

        Me.lblErrorMessage.Visible = False
        Me.lblErrorMessage.Text = String.Empty
        Me.lblHistoryErrorMessage.Visible = False
        Me.lblHistoryErrorMessage.Text = String.Empty

    End Sub

#End Region

#Region "Set Methods"

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  SetCouponRedemptionEntry
    ''' 
    ''' Description:  Sets up the page for coupon redemption entry.
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Private Sub SetCouponRedemptionEntry()

        Me.pnlRedemptionEntry.Visible = True
        Me.pnlRedeemCouponView.Visible = True
        Me.pnlSendGiftCardView.Visible = False
        Me.txtCouponCode1.Enabled = True

        Me.pnlRedemptionConfirm.Visible = False
        Me.pnlRedemptionHistory.Visible = False
        Me.pnlRedemptionError.Visible = False

    End Sub

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  SetCouponRedemptionConfirm
    ''' 
    ''' Description:  Sets up the page to display a coupon or gift card
    '''               redemption confirmation.
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Private Sub SetCouponRedemptionConfirm()

        Me.pnlRedemptionConfirm.Visible = True
        Me.plhRedeemedEventsConfirm.Visible = False
        Me.plhQualifyingEventsConfirm.Visible = False
        Me.plhGiftCardMailingAddressConfirm.Visible = Me.pnlSendGiftCardView.Visible

        Me.pnlRedemptionEntry.Visible = False
        Me.pnlRedemptionHistory.Visible = False
        Me.pnlRedemptionError.Visible = False

    End Sub

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  SetGiftCardRedemptionEntry
    ''' 
    ''' Description:  Sets up the page to select an address for gift card
    '''               redemption.
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Private Sub SetGiftCardRedemptionEntry()

        Me.pnlRedemptionEntry.Visible = True
        Me.pnlRedeemCouponView.Visible = False
        Me.pnlSendGiftCardView.Visible = True
        Me.txtCouponCode1.Enabled = False

        Me.pnlRedemptionConfirm.Visible = False
        Me.pnlRedemptionHistory.Visible = False
        Me.pnlRedemptionError.Visible = False

    End Sub

#End Region

#Region "Validate Methods"

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  Validate
    ''' 
    ''' Description:  Validates the specified coupon-id.
    ''' 
    ''' </summary>
    ''' <param name="couponID"></param>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Private Sub Validate(ByVal couponID As String)

        If String.IsNullOrEmpty(couponID) Then
            Me.lblErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & "&nbsp;Please enter a coupon code." & "</div>"
            Throw New Exception()
        ElseIf couponID.Length > 10 Then
            Me.lblErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> " & "&nbsp;Coupon code cannot exceed 10 characters." & "</div>"
            Throw New Exception()
        End If

    End Sub

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  Validate
    ''' 
    ''' Description:  Validates the specified account object.
    ''' 
    ''' </summary>
    ''' <param name="billingAccount"></param>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Private Sub Validate(ByVal billingAccount As IAccount)

        If billingAccount Is Nothing Then
            Throw New Exception()
        End If

    End Sub

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  Validate
    ''' 
    ''' Description:  Validates the specified redeemed coupon object.
    ''' 
    ''' </summary>
    ''' <param name="coupon"></param>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Private Sub Validate(ByVal coupon As IRedeemCoupon)

        If coupon Is Nothing Then
            Throw New Exception()
        End If

        If coupon.RedemptionStatus <> "1" Then
            lblErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> &nbsp;" & coupon.ValidationErrorMessage & "</div>"
            Throw New Exception()
        End If

    End Sub

    ''' ------------------------------------------------------------------------
    ''' <summary>
    ''' 
    '''      Method:  ValidateAddresses
    ''' 
    ''' Description:  Validates the addresses in the specified account object.
    ''' 
    ''' </summary>
    ''' <param name="billingAccount"></param>
    ''' <remarks></remarks>
    ''' ------------------------------------------------------------------------
    Private Sub ValidateAddresses(ByVal billingAccount As IAccount)

        If billingAccount Is Nothing OrElse _
          (billingAccount.Address Is Nothing AndAlso billingAccount.BillingAddress Is Nothing) Then
            lblErrorMessage.Text = "<div class=""ValidationSummary"">" & "<img src='../Images/error-icon_small.ccom' /> &nbsp;We have no mailing address on file for your account.</div>"
            Throw New Exception()
        End If

    End Sub

#End Region

End Class
