
Partial Class order_centre_DefaultMasterPage
    Inherits System.Web.UI.MasterPage
    Private _webWS As New WebWS
    Private _goPrint2Adapter As New com.racadtech.services.GoPrint2Client()
    Private _interfaceSettings As New com.racadtech.services.PrintDriverInterface()
    Private _baseImageURL As String = "img/"
    Private _currentTheme As String = "green"
    Protected _selectedLanguage As String = System.Threading.Thread.CurrentThread.CurrentUICulture.Name

    Public ReadOnly Property MasterKey() As String
        Get
            Return "877k31hg333-d03423332wq-312dkazlfs-433398j543"
        End Get
    End Property
    Public ReadOnly Property GoPrint2Adapter() As com.racadtech.services.GoPrint2Client
        Get
            Return _goPrint2Adapter
        End Get
    End Property
    Public Property InterfaceSettings() As com.racadtech.services.PrintDriverInterface
        Get
            Return _interfaceSettings
        End Get
        Set(ByVal value As com.racadtech.services.PrintDriverInterface)
            _interfaceSettings = value
        End Set
    End Property
    Public ReadOnly Property CurrentActivationId() As String
        Get
            If Not Session("StoreId") Is Nothing Then
                Return _webWS.GetActivationIdByStoreId(Session("StoreId")).ToString()
            Else
                Return "null"
            End If
        End Get
    End Property
    Public Property BaseImageURL() As String
        Get
            Return _baseImageURL
        End Get
        Set(ByVal value As String)
            _baseImageURL = value
        End Set
    End Property
    Public Property CurrentTheme() As String
        Get
            If Not Session("CurrentTheme") Is Nothing Then
                Return Session("CurrentTheme")
            Else
                Return _currentTheme
            End If
        End Get
        Set(ByVal value As String)
            _currentTheme = value
            Session("CurrentTheme") = value
        End Set
    End Property

    Public ReadOnly Property CmsAdapter() As CmsAdapter
        Get
            Return New CmsAdapter()
        End Get
    End Property

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        '
        ' ORDER CENTRE REDIRECT PROCEDURE.
        '
        Dim _subDomain As String = Request.Url.Host.ToString().Split(".")(0)

        If _subDomain.Equals("localhost") Then
            Session("StoreId") = 1 ' DEBUG ONLY.
        End If


        If Request.Url.Host.ToString().Split(".").Length > 1 Then
            If Request.Url.Host.ToString().Split(".")(1) = "goprint2" Then
                'Response.Write("<br/>condition1")
                If Not _subDomain.Equals("www") And Not _subDomain.Equals("") And Not _subDomain.Equals("localhost") Then
                    If (Session("Subdomain") Is Nothing) Then
                        Dim _storeId As Integer = 0
                        _storeId = _webWS.GetStoreIdFromDomainName(_subDomain)
                        If _storeId = 0 Then
                            Response.Redirect("https://www.goprint2.com/home.aspx")
                        Else
                            Session("StoreId") = _storeId
                            Session("Subdomain") = _subDomain
                        End If
                    End If
                End If
            Else
                ' For customer own domains (not .goprint2.com)
                'Response.Write("<br/>condition2")
                Dim _storeId As Integer = 0
                _storeId = _webWS.GetStoreIdFromDomainName(Request.Url.Host.ToString())
                If _storeId = 0 Then
                    Response.Redirect("https://www.goprint2.com/home.aspx")
                Else
                    Session("StoreId") = _storeId
                End If
            End If
        Else
            'Response.Write("<br/>condition3")
            If Not _subDomain.Equals("www") And Not _subDomain.Equals("") And Not _subDomain.Equals("localhost") Then
                If (Session("Subdomain") Is Nothing) Then
                    Dim _storeId As Integer = 0
                    _storeId = _webWS.GetStoreIdFromDomainName(_subDomain)
                    If _storeId = 0 Then
                        Response.Redirect("https://www.goprint2.com/home.aspx")
                    Else
                        Session("StoreId") = _storeId
                        Session("Subdomain") = _subDomain
                    End If
                End If
            End If
        End If



        If CurrentActivationId.Equals("null") Then
            Dim _defaultInterfaceSettings As New com.racadtech.services.PrintDriverInterface()
            _defaultInterfaceSettings.Theme = "green"
            InterfaceSettings = _defaultInterfaceSettings
        Else
            InterfaceSettings = GoPrint2Adapter.GetPrintDriverInterface(MasterKey, CurrentActivationId)
        End If

        Select Case InterfaceSettings.Theme
            Case "lightblue"
                BaseImageURL = "themes/lightblue/"
            Case "orange"
                BaseImageURL = "themes/orange/"
            Case "red"
                BaseImageURL = "themes/red/"
            Case "brown"
                BaseImageURL = "themes/brown/"
            Case "originalgreen"
                BaseImageURL = "themes/originalgreen/"
            Case "navyblue"
                BaseImageURL = "themes/navyblue/"
            Case "burgundy"
                BaseImageURL = "themes/burgundy/"
            Case "lightburgundy"
                BaseImageURL = "themes/lightburgundy/"
            Case "lightgreen"
                BaseImageURL = "themes/lightgreen/"
            Case "lightpink"
                BaseImageURL = "themes/lightpink/"
            Case "lightpurple"
                BaseImageURL = "themes/lightpurple/"
            Case "pink"
                BaseImageURL = "themes/pink/"
            Case "purple"
                BaseImageURL = "themes/purple/"
            Case "lightyellow"
                BaseImageURL = "themes/lightyellow/"
            Case "blue"
                BaseImageURL = "themes/blue/"
            Case "yellow"
                BaseImageURL = "themes/yellow/"
            Case "green"
                BaseImageURL = "themes/green/"
            Case Else
                BaseImageURL = "themes/orange/"

        End Select
        Session("BaseImageURL") = BaseImageURL
        ' 

        Me.cssLink.Href = BaseImageURL & "style.css"

		ECardBuilderHyperLink.NavigateUrl = "http://" + Request.Url.Host.ToString() + "/order_centre/ecard_builder.aspx"
    End Sub


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Session("StoreId") Is Nothing Then
            'Response.Write("StoreId " & Session("StoreId"))
            'Response.Write("<br/>number of records " & _webWS.GetDomainName(Convert.ToInt32(Session("StoreId"))).Tables(0).Rows.Count)

            If _webWS.GetDomainName(Convert.ToInt32(Session("StoreId"))).Tables(0).Rows.Count > 0 Then
                'Response.Write("<br/>domain active: " & _webWS.GetDomainName(Convert.ToInt32(Session("StoreId"))).Tables(0).Rows(0).Item("IsActive"))
                If _webWS.GetDomainName(Convert.ToInt32(Session("StoreId"))).Tables(0).Rows(0).Item("IsActive") = False Then
                    Response.Redirect("https://www.goprint2.com/domain_activation.aspx?key=0")
                End If
            Else
                Response.Redirect("https://www.goprint2.com/domain_activation.aspx?key=0")
            End If
        End If

        Dim urlArr() As String = Request.Url.ToString().Split("//")
        Dim url As String = urlArr(urlArr.Length - 1).ToString()

        Dim _parameter As String = ""
        If url.IndexOf("?") > -1 Then _parameter = "?" & url.Split("?")(1)

        Dim _pageId As Integer = 0
        Dim _customContentExists As Boolean = False

        If CmsAdapter.GetAllPagesByStoreId(Session("StoreId")).Count > 0 Then
            _customContentExists = True
            _pageId = CmsAdapter.GetAllPagesByStoreId(Session("StoreId")).Item(0).PageId
            HomeHyperLink.NavigateUrl = "~/order_centre/page_loader.aspx?pageId=" & _pageId
        End If

        If _customContentExists Then
            tab_1.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
            tab_2.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
            tab_3.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")
        Else
            tab_1.Visible = False
            tab_2.Visible = False
            tab_3.Visible = False
        End If

        If Me.CmsAdapter.GetECardBuilderByStoreId(Session("StoreId")).Tables(0).Rows.Count < 1 Then
            tab_10.Visible = False
            tab_11.Visible = False
            tab_12.Visible = False
        Else
            If Me.CmsAdapter.GetECardBuilderByStoreId(Session("StoreId")).Tables(0).Rows(0).Item("IsEnabled") = False Then
                tab_10.Visible = False
                tab_11.Visible = False
                tab_12.Visible = False
            End If
        End If

        Select Case url
            Case "error_page.aspx"

                tab_100.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_101.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_102.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                tab_4.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_5.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_6.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                tab_7.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_8.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_9.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                tab_10.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_11.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_12.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

            Case "getting_started.aspx"

                tab_100.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_color_left_part.gif') no-repeat")
                tab_101.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_color_main_part_bg.gif') repeat-x")
                tab_102.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_color_right_part.gif') no-repeat")

                tab_4.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_5.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_6.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                tab_7.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_8.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_9.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                tab_10.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_11.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_12.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

            Case "login.aspx", "login.aspx" & _parameter, "customer_contact_info.aspx", "customer_password_recovery.aspx"

                tab_100.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_101.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_102.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                tab_4.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_color_left_part.gif') no-repeat")
                tab_5.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_color_main_part_bg.gif') repeat-x")
                tab_6.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_color_right_part.gif') no-repeat")

                tab_7.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_8.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_9.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                tab_10.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_11.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_12.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

            Case "job_options.aspx", "upload.aspx", "process_document.aspx", "thank_you.aspx", "upload_applet.aspx"

                tab_100.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_101.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_102.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                tab_4.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_5.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_6.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                tab_7.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_color_left_part.gif') no-repeat")
                tab_8.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_color_main_part_bg.gif') repeat-x")
                tab_9.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_color_right_part.gif') no-repeat")

                tab_10.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_11.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_12.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

            Case "page_loader.aspx" & _parameter

                tab_1.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_color_left_part.gif') no-repeat")
                tab_2.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_color_main_part_bg.gif') repeat-x")
                tab_3.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_color_right_part.gif') no-repeat")

                tab_100.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_101.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_102.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                tab_4.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_5.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_6.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                tab_7.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_8.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_9.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                tab_10.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_11.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_12.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                _pageId = Convert.ToInt32(Request.QueryString("pageId"))
                Dim _pageDetails As PageDetails
                _pageDetails = Me.CmsAdapter.GetPageByPageId(_pageId)

                If _pageDetails.StoreId.ToString = Session("StoreId").ToString Then
                    Dim _newMetaTag As HtmlMeta
                    Dim _metaTagsList As System.Collections.Generic.List(Of MetaTagDetails) = _pageDetails.MetaTagDetailsList
                    If _metaTagsList.Count > 0 Then
                        Dim _metaTag As MetaTagDetails
                        For Each _metaTag In _metaTagsList
                            _newMetaTag = New HtmlMeta
                            _newMetaTag.Attributes.Add(_metaTag.MetaName, _metaTag.MetaContent)
                            Me.headId.Controls.Add(_newMetaTag)
                        Next
                    End If
                    _newMetaTag = New HtmlMeta
                    If _pageDetails.AllowRobots Then
                        _newMetaTag.Attributes.Add("robots", "all")
                    Else
                        _newMetaTag.Attributes.Add("robots", "noindex, nofollow")
                    End If
                    Me.headId.Controls.Add(_newMetaTag)
                End If

            Case "ecard_builder.aspx" & _parameter

                tab_100.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_101.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_102.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                tab_4.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_5.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_6.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                tab_7.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_left_part.gif') no-repeat")
                tab_8.Style.Add("background", "#a2afb7 url('" & BaseImageURL & "tab_grey_main_part_bg.gif') repeat-x")
                tab_9.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_grey_right_part.gif') no-repeat")

                tab_10.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_color_left_part.gif') no-repeat")
                tab_11.Style.Add("background", "#86ad08 url('" & BaseImageURL & "tab_color_main_part_bg.gif') repeat-x")
                tab_12.Style.Add("background", "#ffffff url('" & BaseImageURL & "tab_color_right_part.gif') no-repeat")

        End Select

        LogoImage.ImageUrl = ""
        Try
            Dim _dataSet As New System.Data.DataSet
            _dataSet = _webWS.GetPrintDriverInterfaceByStoreId(Session("StoreId"))
            ContactInfoLabel.Text = " <b>" & _dataSet.Tables(0).Rows(0).Item("CompanyName") & "</b> <br />" & _
            _dataSet.Tables(0).Rows(0).Item("CompanyAddress") & "<br />" & _
            _dataSet.Tables(0).Rows(0).Item("CompanyPhone") & "<br /> " & _dataSet.Tables(0).Rows(0).Item("CompanyEmail")
            LogoImage.ImageUrl = _dataSet.Tables(0).Rows(0).Item("CompanyLogo")
        Catch ex As Exception
        End Try
        If LogoImage.ImageUrl = "" Then LogoImage.Visible = False
		
		If LogoImage.Visible Then
			LogoImage.ImageUrl = LogoImage.ImageUrl.Replace("http://","https://")
		End If

        Dim _pagesList As System.Collections.Generic.List(Of PageDetails) = CmsAdapter.GetAllPagesByStoreId(Convert.ToInt32(Session("StoreId")))
        Dim _page As PageDetails
        If _pagesList.Count > 0 Then
            For Each _page In _pagesList
                Dim _img As New HtmlImage
                _img.Src = "img/icon_arrow.gif"
                _img.Alt = ""
                _img.Style.Add("vertical-align", "middle")
                _img.Style.Add("padding-bottom", "15px")
                Dim _link As New HyperLink
                _link.NavigateUrl = "~/order_centre/page_loader.aspx?pageId=" & _page.PageId
                _link.Style.Add("vertical-align", "middle")
                _link.Text = _page.PageTitle
                _link.CssClass = "bold_hyperlink"
                _link.Style.Add("padding-bottom", "15px")
                Dim _break As HtmlControl = New HtmlGenericControl("br")
                Me.LeftMenuDiv.Controls.Add(_img)
                Me.LeftMenuDiv.Controls.Add(_link)
                Me.LeftMenuDiv.Controls.Add(_break)
            Next
            If url <> "ecard_builder.aspx" Then LeftMenu.Visible = True
        End If

        Dim _ds As System.Data.DataSet = _webWS.SelectWithSQLQuery("Select * From PI_StoreOrderCentreSettings Where StoreId='" & Session("StoreId") & "'")
        If _ds.Tables(0).Rows.Count > 0 Then
            If Not _ds.Tables(0).Rows(0)("ShowTopMenu") Is DBNull.Value Then
                top_2.Visible = _ds.Tables(0).Rows(0)("ShowTopMenu")
                top_3_outside.Visible = _ds.Tables(0).Rows(0)("ShowTopMenu")
                top_5_outside.Visible = _ds.Tables(0).Rows(0)("ShowTopMenu")
            End If
        End If

        If Session("StoreId") = "1146" Then
            top_2.Visible = False
            top_3_outside.Visible = False
            top_5_outside.Visible = False
        End If
        'Response.Write(Session("StoreId"))
    End Sub

    Protected Sub FlagEnButton_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles FlagEnButton.Click
        _selectedLanguage = "en"
        ChangeLang(_selectedLanguage)
    End Sub

    Public Sub ChangeLang(ByVal selectedLanguage As String)
        Dim cookiename As String = "lang"
        Dim cookie As HttpCookie = New HttpCookie(cookiename)
        Response.Cookies.Clear()
        Response.Cookies.Add(cookie)
        cookie.Value = _selectedLanguage
        Dim dtExpiry As DateTime = DateTime.Now.AddDays(50)
        Response.Cookies(cookiename).Expires = dtExpiry

        Me.Response.Redirect(Request.Url.ToString())
    End Sub

    Protected Sub FlagEsButton_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles FlagEsButton.Click
        _selectedLanguage = "es"
        ChangeLang(_selectedLanguage)
    End Sub

    Protected Sub FlagFrButton_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles FlagFrButton.Click
        _selectedLanguage = "fr"
        ChangeLang(_selectedLanguage)
    End Sub

End Class

