Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports RewriteModule
Partial Class SearchProduct
    Inherits System.Web.UI.Page
    Shared a As String
    Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session("three") = "yes"
        If Not IsPostBack Then
            Dim cmd As New SqlCommand
            Dim dr As SqlDataReader
            Dim str As String
            Dim title As String = ""
            Dim keywor As String = ""
            Dim description As String = ""
            Dim strarr(3) As String
            Dim i As Integer = 1

            'checkqrstr(Request.QueryString("Product_Id"))
            'checkqrstr(Request.QueryString("Brand_kId"))

            Try

                'If Request.QueryString("Product_HId") <> Nothing Then

                'End If

                If Request.QueryString("Product_Id") <> Nothing Or Request.QueryString("Product_DId") <> Nothing Or Request.QueryString("Product_HId") <> Nothing Then
                    Dim catstr As String

                    If Request.QueryString("Product_Id") <> Nothing Then
                        catstr = RemoveLiterals(DecodeURL(Request.QueryString("Product_Id")))
                    ElseIf Request.QueryString("Product_DId") <> Nothing Then
                        catstr = RemoveLiterals(DecodeURL(Request.QueryString("Product_DId")))
                    ElseIf Request.QueryString("Product_HId") <> Nothing Then
                        catstr = RemoveLiterals(DecodeURL(Request.QueryString("Product_HId")))
                    End If
                    con.Open()
                    str = "Select  Product_Kid,Product_Name,Product_Description from Product_Master where Product_CategoryId ='" & catstr & "' and Product_Approved='Y' and Product_Isdeleted='0' order by Product_Name"
                    cmd = New SqlCommand(str, con)
                    dr = cmd.ExecuteReader()
                    'Dim dtTitle As New DataTable
                    'dtTitle.Load(dr)
                    If dr.HasRows Then
                        While dr.Read()
                            If i = 1 Then
                                strarr = FgenerateTags((RemoveLiterals(dr(0).ToString)), dr(1).ToString)
                                'strarr(0) = limitWords(strarr(3).ToString(), 10, 90, " ")    ' For Title including  category herarchy

                                i = 2
                            End If
                            keywor = dr(1).ToString() + " " + keywor
                            description = dr(2).ToString + "," + description
                        End While
                        If Not dr.HasRows Then
                            strarr(0) = ReturnValue("Select Category_Name from CategoryDistinct where Category_KId='" & RemoveLiterals(catstr) & "'") & "," & Session("Title").ToString.ToLower
                        End If
                        strarr(1) = limitWords(description, 0, 170, " ")    ' For meta description Content 
                        strarr(2) = limitWords(description.ToString() + " " + keywor, 20, 900, ", ") ' For Meta Keyword Content Product Name
                        dr.Close()

                    Else
                        dr.Close()
                        CategoryList = ""
                        CatCount = 0
                        FillCategoryName(RemoveLiterals(catstr))
                        description = CategoryList
                        strarr(0) = ReturnValue("Select Category_Name from CategoryDistinct where Category_KId='" & RemoveLiterals(catstr) & "'") & "-" & Convert.ToString(Session("Title"))
                        strarr(0) = strarr(0).Remove(strarr(0).LastIndexOf("-"))
                        'strarr(0) = limitWords(description.ToString(), 10, 90, " ")    ' For Title including  category herarchy
                        strarr(1) = limitWords(description, 0, 170, " ")    ' For meta description Content 
                        strarr(2) = limitWords(description.ToString() + "" + keywor, 20, 900, ", ") ' For Meta Keyword Content Product Name
                    End If
                    dr.Close()
                    con.Close()
                ElseIf Request.QueryString("Product_Kid") <> Nothing Then

                    Dim strCat As String
                    strCat = Request.QueryString("Product_CatID").ToString
                    Try
                        strCat = strCat.Remove(0, strCat.IndexOf("/") + 1)
                    Catch ex As Exception
                        strCat = Request.QueryString("Product_CatID").ToString
                    End Try

                    Dim PName As String
                    PName = ReturnValue("Select OriginalProduct_Name from Product_Distinct where Product_Name ='" & Request.QueryString("Product_KID").ToString & "'and Category_Name ='" & strCat & "'")

                    'Dim PName As String
                    'PName = Request.QueryString("Product_KId").Replace("-", " ")

                    con.Open()
                    str = "Select Product_Kid,Product_Name,Product_Description from Product_Master where Product_Name ='" & PName & "' and Product_Approved='Y' and Product_Isdeleted='0' order by Product_Description desc"
                    cmd = New SqlCommand(str, con)
                    dr = cmd.ExecuteReader()

                    While dr.Read()
                        If i = 1 Then
                            strarr = FgenerateTags((RemoveLiterals(dr(0).ToString)), dr(1).ToString)
                            'strarr(0) = limitWords(strarr(0), 10, 90, " ")    ' For Title including productname 
                            i = 2
                        End If
                        description = dr(2).ToString + ", " + description

                    End While
                    strarr(1) = limitWords(description, 0, 170, " ")    ' For meta description Content Category Herarchy
                    strarr(2) = limitWords(strarr(3).ToString() + " " + description, 20, 900, ", ") ' For Meta Keyword Content From Product_Keyword Value of Keyword_Auto Field


                    dr.Close()
                    con.Close()

                ElseIf Request.QueryString("Brand_kId") <> Nothing Then
                    con.Open()
                    Dim brandname As String = ReturnValue("SELECT    Brand_Name    FROM Brand_Master WHERE     Brand_Kid ='" & RemoveLiterals(Request.QueryString("Brand_kId")) & "'")

                    str = "Select top(5) Product_Name,Product_Description from Product_Master where Product_Brandid = '" & RemoveLiterals(Request.QueryString("Brand_kId")) & "' and Product_Approved='Y' and Product_Isdeleted='0'"
                    cmd = New SqlCommand(str, con)
                    dr = cmd.ExecuteReader()
                    While dr.Read()
                        description = dr(1) + "," + description

                    End While
                    strarr(1) = limitWords(description, 0, 170, " ")    ' For meta description Content of product description
                    dr.Close()
                    con.Close()
                    ''==================================Title========================================
                    str = "Select distinct Product_Name from Product_Master where Product_Brandid = '" & RemoveLiterals(DecodeURL(Request.QueryString("Brand_kId")) & "' and Product_Approved='Y' and Product_Isdeleted='0' order by Product_Name desc")
                    cmd = New SqlCommand(str, con)
                    con.Open()
                    dr = cmd.ExecuteReader()

                    While dr.Read()
                        title = dr(0).ToString + "," + title.Trim
                    End While
                    strarr(2) = limitWords(title.Trim, 20, 900, ", ") ' For Meta Keyword Content From Product_Keyword Value of Keyword_Auto Field
                    title = brandname.ToString + "-" + title.Trim
                    'strarr(0) = limitWords(title.ToString.Trim, 10, 90, " ") ' For Title including Brand Name
                    strarr(0) = title
                    dr.Close()
                    con.Close()
                Else
                    strarr(0) = "Search Result"
                End If

                Dim str1 As String
                str1 = StrConv(strarr(0), VbStrConv.ProperCase)
                Me.Header.Title = str1 + " - " + "Atcomaart Industrial Supply"



                Dim mt1 As New HtmlMeta
                mt1.Name = "description"
                mt1.Content = strarr(1)
                Me.Header.Controls.Add(mt1)

                Dim mt2 As New HtmlMeta
                mt2.Name = "keywords"
                mt2.Content = strarr(2).Trim.Remove(strarr(2).Length - 2).ToLower
                Me.Header.Controls.Add(mt2)
                Me.Header.Controls.Add(New LiteralControl("<script type='text/javascript' src='" & ResolveUrl("~/App_Script/JScript.js") & "'></script>"))




            Catch ex As Exception

            End Try





        End If

    End Sub

    Public Sub checkqrstr(ByVal qsrt As String)
        If qsrt <> Nothing Then
            If qsrt.Length > 9 Or qsrt.Length < 7 Then
                Response.Redirect("home.aspx")
            End If
        End If
       

    End Sub

    Public Function limitWords(ByVal str As String, ByVal words As Integer, ByVal chars As Integer, ByVal delimiter As String) As String
        If (chars) Then
            If (str.Length() > chars) Then
                str = str.Substring(0, chars)
            End If
        End If

        If (words) Then
            Dim temp() As String
            temp = str.Split(" ")

            If (temp.Length > words) Then
                Array.Resize(temp, words)

            End If
            Dim i As Integer = 0
            str = ""
            While (i < temp.Length)
                ' If temp(i) <> "" Then
                str += temp(i) & delimiter
                i = i + 1
                ' End If

            End While
        End If
        Return str
    End Function
   
End Class

