Imports System.Collections.Generic
Imports System.Text
Imports System.Xml
Imports CharterDotCom.CommonEntities.Library
Imports CharterDotCom.CommonEntities.Library.Common
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.Net

Partial Class Visitors_BuildYourOwn
    Inherits CharterCMSPage


    Private _IsExistingCustomer As Boolean

	Private _document As XmlDocument

	Public ReadOnly Property SelectedSubitemID() As Integer
		Get
			If Request.Form("txtAddSubitem") Is Nothing Or Request.Form("txtAddSubitem") = String.Empty Then
				Return 0
			Else
				Return CType(Request.Form("txtAddSubitem"), Integer)
			End If
		End Get
	End Property

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetPageTitle("Build Your Own")
        _document = New XmlDocument()
		_document.LoadXml(Application("BuildYourOwnXml"))

    If Not Page.IsPostBack Then
      Me.Master.OrphanPage = True
    End If

		If SelectedSubitemID > 0 Then
			CatalogManager.Instance.AddSubCartItemWithParent(SelectedSubitemID, Nothing, New Notify(AddressOf ucShoppingCart.OnNotification))
		End If
	End Sub

	Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
		Dim _existingServices As List(Of Category)
		Dim _existing As Boolean
		Dim features As Dictionary(Of String, ISubCartItem)
		Dim feature As KeyValuePair(Of String, ISubCartItem)
		Dim sb(3) As StringBuilder
    Dim i As Integer
    Dim noCableFeatures As Boolean = True
    Dim noHSDFeatures As Boolean = True
        Dim noTelephoneFeatures As Boolean = True

    Me.ucShoppingCart.HBXInfo_PageName = Me.Master.HBXPageName
    Me.ucShoppingCart.HBXInfo_MLC = Me.Master.HBXMLC

		_existingServices = AccountManager.Instance.GetAccount().ExistingService

    ucCustomContent.Visible = False
    If _existingServices Is Nothing Then
            _existingServices = New List(Of Category)
            _IsExistingCustomer = False

      'Else
      'ucCustomContent.ContentKey = "BuildYourOwn"
      'ucCustomContent.Visible = True
        Else
            _IsExistingCustomer = True
        End If
    Dim catalog As ICatalog
    catalog = CatalogManager.Instance.GetCatalog

    features = CatalogManager.Instance.GetFeatures()


    ' assign shopping cart's cart property
    ucShoppingCart.Cart = catalog.Cart

    For i = 0 To 2
      sb(i) = New StringBuilder()
    Next i

    For Each feature In features
      If _document.SelectSingleNode(String.Format("/buildYourOwn/features[@hidden='true']/feature[@id='{0}']", feature.Value.ID)) IsNot Nothing Then
        Continue For
      End If

      Select Case feature.Value.Category
        Case Category.Cable
          noCableFeatures = False
          RenderFeature(feature.Value, sb(0), _existingServices.Contains(Category.Cable))
        Case Category.HSD
          noHSDFeatures = False
          RenderFeature(feature.Value, sb(1), _existingServices.Contains(Category.HSD))
        Case Category.Telephoney
          noTelephoneFeatures = False
          RenderFeature(feature.Value, sb(2), _existingServices.Contains(Category.Telephoney))
      End Select
    Next

    For i = 0 To 2
      If sb(i).Length > 0 Then
        sb(i).Insert(0, "<table class='build' cellpadding='0' cellspacing='0'>")
        sb(i).Append("</table>")
      End If
    Next i

    ltrCableTV.Text = sb(0).ToString()
    ltrInternet.Text = sb(1).ToString()
    ltrTelephone.Text = sb(2).ToString()

    If noCableFeatures Then
      divCable.Visible = False
    Else
      divCable.Visible = True
    End If
    If noHSDFeatures Then
      divHSD.Visible = False
    Else
      divHSD.Visible = True
    End If
    If noTelephoneFeatures Then
      divTelephone.Visible = False
    Else
      divTelephone.Visible = True
    End If
  End Sub

	Private Sub RenderFeature(ByVal feature As ISubCartItem, ByVal sb As StringBuilder, ByVal _existing As Boolean)
		sb.Append("<tr>")

    sb.AppendFormat("<td>{0}</td>", feature.Name)
		sb.AppendFormat("<td><a href=""javascript:void(0)"" onClick=""window.open('../Visitors/ChoiceDetail.aspx?{0}={1}','mywindow','width=650,height=550,scrollbars=yes,resizeable=yes')"""">Learn More</a>", QueryStringName.ChoiceID, feature.ID)
    'sb.AppendFormat("<td style='text-align:right'>{0} /mo</td>", feature.ListPrice.ToString("$#0.00"))

		If feature.IsAvailable Then
			If _existing Then
                'Dim client As WebClient = New WebClient()
                'Dim category As String
                'Dim strBSN As String = "0"
                'Dim _catgr As StringBuilder = New StringBuilder
                'Dim _actualCategory As String
                'Dim i As Integer
                'Dim c As Char
                'category = client.DownloadString("https://charterlive.chartercom.com/wcsapp/weblib/charter/ProbQueueMapping.jsp?site=default&site=default&chat_type=SALES&bsn=" & strBSN & " &Format=CSV")
                'For i = 0 To category.Length - 1
                '	c = CType(category(i), Char)
                '	If Asc(c) >= 65 And Asc(c) <= 90 Or Asc(c) = 60 Or Asc(c) = 62 Then
                '		_catgr.Append(c)
                '	End If
                'Next
                '_actualCategory = _catgr.ToString()
                sb.Append("<td style='text-align:right'>")
                sb.Append(BuildChatLink())
                sb.Append("</td>")
            Else
                '####################################################################################################
                'M O D I F I C A T I O N   1/29/2008 ROD COPELAND
                '####################################################################################################
                'EPC EXTERNAL ID: 190
                '####################################################################################################
                'PER JIM GALLAGHER, THE WWW250 OPTION IS NOW BEING PRESENTED AS A 
                'SVC PLAN TO BETTER PROMOTE THE OPTION WHICH IS ACTUALLY A CUSTOMIZATION.
                'HOWEVER, WE ONLY WANT TO SHOW THE CHAT TO ORDER BUTTON FOR NEW AND EXISTING CUSTOMERS FOR THIS PLAN
                '####################################################################################################
                If feature.ID = 4995264 Then
                    sb.Append("<td style='text-align:right'>")
                    sb.Append(BuildChatLink())
                    sb.Append("</td>")
                Else
                    sb.AppendFormat("<td style='text-align:right'><a href='javascript:addSubitem({0})'><img src='../Images/but_orderNew24.gif' alt='Add to Cart'/></a></td>", feature.ID)
                End If

            End If
		ElseIf feature.IsSelected Then
			sb.Append("<td style='text-align:right'><img src='../Images/but_AlreadyInCart1.gif' alt='Add to Cart'/></td>")
		Else
			sb.Append("<td style='text-align:right'>Not Available</td>")
		End If

		sb.AppendFormat("</tr>")
    End Sub

  Protected Function BuildChatLink() As String

    Dim chatHTMLBuilder As New StringBuilder("<img alt='Chat To Oder' src='../Images/but_chatToOrder.gif' height='18' width='90' class='buttonSpacing' onclick=""")
        chatHTMLBuilder.Append("javascript:window.open('../Visitors/chatlive.aspx?Chattype=SALES','_charter_chat','width=550,height=660,left=50,top=15')")

    chatHTMLBuilder.Append(""" />")
    Dim chatHTML As String = chatHTMLBuilder.ToString()

    Return chatHTML

  End Function

End Class
