Imports EDSL.Entities
Imports EDSL.DAL
Imports System
Imports System.Web

Partial Public Class contactusAddressList
    Inherits BasePage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim objMainPage As New dbMainPage
        Dim objMainPageEntity As New entMainPage

        objMainPageEntity = objMainPage.GetPageDetails("ContactUsAddressList")

        lblCatPageTitle.Text = objMainPageEntity.PageTitle
        divContent.InnerHtml = objMainPageEntity.PageContent
        Me.MetaTitle = ConfigurationManager.AppSettings("BrowserTitleCompanyName") & objMainPageEntity.MetaTitle

        Me.MetaName = objMainPageEntity.PageMetaName
        Me.MetaContent = objMainPageEntity.PageMetaContent
       
        btnSubmit.PostBackUrl = Request.RawUrl.ToString()

        If Request.QueryString("cid") <> "" Then
            BuildBreadCrumb(objMainPageEntity)
            'Don't display drop down list
            lstType.Visible = False
            'label is visible as it's direct from an product page
            lblTypeOfEnquiry.Visible = True
        End If


        If Not (IsPostBack) Then
            GetEnquiryList()

        End If

        If Request.QueryString("t") = "Thankyou" Then
            Dim dbMessage As New dbMainMessage
            divThankyou.InnerHtml = dbMessage.GetMessage(2).ToString()
            divThankyou.Visible = True
            enquiryform.Visible = False
        End If

    End Sub
    Private Sub BuildBreadCrumb(ByVal objMainPageEnt As entMainPage)
        Dim pageInfo As HyperLink = breadcrumb.FindControl("lnkPage")
        pageInfo.Text = objMainPageEnt.PageTitle
        Dim categoryList As String
        Dim lbl As Label = breadcrumb.FindControl("lblContact")
        lbl.Visible = True
        'Build the bread crumbtrail parent
        Dim objMainCategory As New entmainCategory
        Dim objMain As New dbMainCategory
        objMainCategory = objMain.GetItem(Request.QueryString("cid").ToString())
        'Find div on master page
        Dim masterPage As main = DirectCast(Me.Page.Master, main)
        masterPage.BodyTag.Attributes.Add("class", objMainCategory.DropDownValue.ToString())
        categoryList = objMainCategory.CategoryID & "," & objMainCategory.CategoryName

        'Get the type of enquiry for category
        lblTypeOfEnquiry.Text = objMainCategory.CategoryName.ToString()
        If Request.QueryString("srv") <> "" Then
            lblTypeOfEnquiry.Text = "General IT Services"
        End If
        'Sub categories have been removed 23-2-2011, keep in just in case
        'If Request.QueryString("catid") <> "" Or Request.QueryString("catid") <> 0 Then
        '    'Build the bread crumbtrail sub category
        '    Dim objSubCategory As New entmainCategory
        '    objSubCategory = objMain.GetItem(Request.QueryString("catID").ToString())
        '    categoryList += "|" & objSubCategory.CategoryID & "," & objSubCategory.CategoryName
        'End If

        If Request.QueryString("pid") <> "" Then

            ''Build the bread crumbtrail product
            Dim objMainProduct As entMainProduct
            objMainProduct = GetProduct(Request.QueryString("pid"), Request.QueryString("cid"))

            categoryList += "|" & objMainProduct.ProductID & "," & objMainProduct.ProductName
            breadcrumb.SetProductLinks(pageInfo.Text, categoryList)
            lblTypeOfEnquiry.Text = objMainProduct.ProductName.ToString()
        Else

            breadcrumb.SetProductLinks(pageInfo.Text, categoryList)
        End If

    End Sub


    Private Function CheckStringValue(ByVal strInput As String) As String
        Dim strOutput As String = ""
        If Not strInput Is Nothing AndAlso strInput <> "" Then
            strOutput = strInput & "<br />"
        End If
        Return strOutput
    End Function
    Private Function MakeMapLink(ByVal strMapLink As String, ByVal strMapImageName As String) As String
        Dim strOutput As String = ""
        If Not strMapLink Is Nothing AndAlso strMapLink <> "" Then
            strOutput += "<a href=""" & strMapLink & """ alt=""Map link"" target=""_blank"">"
        End If
        If Not strMapImageName Is Nothing AndAlso strMapImageName <> "" Then
            strOutput += "<img src=""uploads/images/" & strMapImageName & """ alt=""Map link"" class=""imgMapImageName""/>"
        Else
            strOutput += strMapLink
        End If
        If Not strMapLink Is Nothing AndAlso strMapLink <> "" Then
            strOutput += "</a>"
        End If
        Return strOutput
    End Function

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
        If (Page.IsValid) Then


            ' SUBMIT enquiry to database
            Dim enquiry As New entMainEnquiry
            Dim enquiryDB As New dbMainEnquiry
            Dim successful As New Integer
            'Dim enquiryPreferences As New ArrayList
            Dim areasOfInterest As String

            With enquiry
                .EnquiryName = txtName.Text.ToString().Trim()
                .BusinessName = txtCompany.Text.ToString().Trim()
                .EnquiryEmail = txtEmail.Text.ToString().Trim()
                .EnquiryTelephone = txtPhone.Text.ToString().Trim()
                .EnquiryMessage = txtMessage.Text.ToString().Trim()
                .EnquiryFaxNo = txtFax.Text.ToString().Trim

                If Request.QueryString("cid") <> "" Then
                    areasOfInterest = lblTypeOfEnquiry.Text.ToString().Trim()
                Else
                    areasOfInterest = lstType.SelectedValue.ToString()
                End If

                'Removed multi select box 23-2-2011, keep just in case
                'For Each item As ListItem In lstEnquiryType.Items
                '    If item.Selected = True Then
                '        enquiryPreferences.Add(item.Value)
                '        areasOfInterest = item.Text.ToString & "<br>"
                '    End If
                'Next

                successful = enquiryDB.SaveItem(0, .EnquiryName, .BusinessName, "", "", "", .EnquiryEmail, .EnquiryTelephone, .EnquiryFaxNo, .EnquiryMessage, "", "S", 0, areasOfInterest)
            End With

            If successful = 0 Then
                Dim emailTemplate As String = ""
                emailTemplate = "Enquiry.htm"


                Dim strFilePath As String = Server.MapPath("")
                strFilePath = Replace(strFilePath, "pagesmain", "")
                Dim strVar As String = strFilePath & "\" & ConfigurationManager.AppSettings("EmailTemplate")
                Dim emailDirectory As String = ConfigurationManager.AppSettings("EmailImageDirectory")
                Dim mainEmail As String = ConfigurationManager.AppSettings("emailAddress")
                Dim messageText As String
                Dim messageSubject As String

                Dim objReader As New System.IO.StreamReader(strVar + emailTemplate)
                messageText = objReader.ReadToEnd()
                messageSubject = "Enquiry sent from EDSL website"
                With enquiry
                    messageText = messageText.Replace("[#DateReceived]", FormatDateTime(Now(), DateFormat.LongDate))
                    messageText = messageText.Replace("[#ContactName]", .EnquiryName)
                    messageText = messageText.Replace("[#Company]", .BusinessName)
                    messageText = messageText.Replace("[#Telephone]", .EnquiryTelephone)
                    messageText = messageText.Replace("[#Fax]", .EnquiryFaxNo)
                    messageText = messageText.Replace("[#Email]", .EnquiryEmail)
                    messageText = messageText.Replace("[#Message]", .EnquiryMessage)
                    messageText = messageText.Replace("[#AOI]", areasOfInterest)

                End With

                Dim newEmail As New dbMailHelper
                newEmail.SendMailMessage(mainEmail, mainEmail, "", "", messageSubject, messageText)
                Response.Redirect("~/general/Contactus-Thankyou")

            Else
                divThankyou.InnerText = "There was an error with your enquiry, please try again"
                divThankyou.Visible = True
                enquiryform.Visible = True
            End If
        End If
    End Sub

    Protected Sub GetEnquiryList()
        Dim enquiryType As New List(Of entMainDropDown)
        Dim enquiryObj As New dbMainDropDownFunctions
        enquiryType = enquiryObj.GetDropDownValues(4)

        For Each enqType In enquiryType
            lstType.Items.Add(New ListItem(enqType.TextDisplay, enqType.DropDownValue))
        Next

        'Sub categories have been removed 23-2-2011, keep in just in case
        'Removed multi select box 23-2-2011, keep just in case
        'Dim matchEnquiryType As String

        'If Request.QueryString("cid") <> "" Then
        '    matchEnquiryType = Request.QueryString("cid")
        'End If

        'If Request.QueryString("catid") <> "" Then
        '    matchEnquiryType = Request.QueryString("catID") & "|" & Request.QueryString("cid")
        'Else
        '    matchEnquiryType += "|0"
        'End If

        'If Request.QueryString("pid") <> "" Then
        '    matchEnquiryType += "|" & Request.QueryString("pid")
        'Else
        '    matchEnquiryType += "|0"
        'End If



        'enquiryType = objList.GetListForEnquiryForm()
        'For Each Type In enquiryType

        '    If Type.DropDownValue.ToString() = matchEnquiryType Then
        '        lstEnquiryType.Items.Add(New ListItem(Type.TextDisplay, Type.DropDownValue))
        '        lstEnquiryType.Items.FindByValue(matchEnquiryType).Selected = True
        '    Else
        '        lstEnquiryType.Items.Add(New ListItem(Type.TextDisplay, Type.DropDownValue))
        '    End If

        'Next
    End Sub
    Private Function GetProduct(ByVal intProductID As Integer, ByVal intCategoryID As Integer) As entMainProduct
        Dim objMain As New dbMainProduct
        Dim objMainProduct As entMainProduct = objMain.GetItemShowOnWebsite(intProductID, intCategoryID)
        Return objMainProduct
    End Function
End Class
