<!--#include file="../owFunctions/I_GeneralFunctions.asp"-->
<%sPath = "../"%>
<!--#include file="../owTemplates/Template.asp"-->
<%
	'************************************************************************************
	'*																					*
	'*	MODIFICATION TO THIS SCRIPT OR ANY FILES INCLUDED WITH 							*
	'*		ONEWORLDSTORE IS DONE AT YOUR OWN RISK!										*
	'*	Product  : OneWorldStore														*
	'*	Version  : 9.0000																*
	'*	Modified : June 2004															*
	'*	Copyright:	Copyright (C) 2004 OneWorldStore.Com								*
	'*				Registration Number: 1024507										*
	'*				http://strategis.ic.gc.ca/											*
	'*																					*
	'************************************************************************************

	Dim sKeywords, sIDSearch, sInitialPrice, sFinalPrice
	Dim sError, SearchsqlText, iEndPos, iStartPos, dLastDate, sTemp
	Dim sIDSupplier, sIDCategory

	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Sub ActionSubroutine()
	
		sKeywords		= Trim(Request("sKeywords"))
		sInitialPrice	= Trim(Request("sInitialPrice"))
		sFinalPrice		= Trim(Request("sFinalPrice"))
		sIDSupplier		= Trim(Request("sIDSupplier"))
		sIDCategory		= Trim(Request("sIDCategory"))
		
		If ( sFinalPrice = "0" ) Then
			sFinalPrice = ""
		End If

		If ( sKeywords = "" ) Then
			%>
			<table width=<%=sOWStoreTableWidth%> border=0 style='border: 1px solid #000000;' cellspacing=0 cellpadding=5>
				<tr>
					<td align=center>
						<br><br>
						<%=LANG_DT( "SEARCHP_ONEKEY", "Please enter at least one keyword before performing your search." )%>
						<br><br>
					</td>
				</tr>
			</table>
			<%
		Else
			%>
			<table width=<%=sOWStoreTableWidth%> border=0 style='border: 1px solid #000000;' cellspacing=0 cellpadding=5>
				<tr>
					<td align=center>
						<br>
						<%=LANG_DT( "SEARCHP_PREPARING", "Preparing Results<br><br>Please Wait..." )%>
						<br><br>
					</td>
				</tr>
			</table>
			<%
			Response.Flush
			Call SearchConf()	' get the search results
		End If
		
	End Sub
	'************************************************************************************

	
	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Sub SearchConf()

		Randomize
		iRandNum = CInt(9999 * Rnd) + 1000
		Call DB_updateDatabase ("INSERT INTO owSearches (idSession, DateChanged, RandNum, Keywords, InStock, InitialPrice, FinalPrice, IDSupplier, IDCategory) VALUES ('" & Session.SessionID & "', #" & CONVERSION_FormatDate(Now(), "L") & "#, '" & iRandNum & "', '" & SQLEncode(sKeywords) & "', 'Y', '" & SQLEncode(sInitialPrice) & "', '" & SQLEncode(sFinalPrice) & "', '" & SQLEncode(sIDSupplier) & "', '" & SQLEncode(sIDCategory) & "')")

		Set rsStoreData = CreateObject("ADODB.Recordset")
		sSQLText = "SELECT idSearch FROM owSearches WHERE RandNum = '" & iRandNum & "' AND idSession = '" & Session.SessionID & "'"	' get the just inserted SearchID for use in the owSearchResults table
		Call DB_getFromDatabase(sSQLText, rsStoreData,"")
		If ( NOT rsStoreData.EOF ) Then
			sIDSearch = rsStoreData("idSearch")
		End If
		DB_closeRecordset(rsStoreData)
		
		If ( UCAse(Trim(sKeywords)) = "OW_NEW" ) Then
			Call NewProductSearch()
		Else
			Call NormalSearch()
		End If

	End Sub
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*	This searches for the newest products											*
	'*																					*
	'************************************************************************************
	Sub NewProductSearch()

		dLastDate = CONVERSION_FormatDate(DateAdd("d", -3, NOW), "L")	' search for the past three days
		Call DB_updateDatabase ("INSERT INTO owSearchResults (idSearch, idProduct, idCategory) SELECT " & sIDSearch & ", P.idProduct, C.idCategory  FROM Products P, Categories C, CatProdMap CPM WHERE (P.DateAdded >= #" & CONVERSION_FormatDate(dLastDate, "L") & "#) AND P.idProduct = CPM.idProduct AND CPM.idCategory = C.idCategory ORDER BY P.DateAdded Desc")
		%>
		<script language='JavaScript1.2' type='text/javascript'>
	
			document.location = "DisplayResults.asp?sIDSearch=<%=sIDSearch%>";

		</script>
		<%
	End Sub
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*	This searches normally															*
	'*																					*
	'************************************************************************************
	Sub NormalSearch()

		SearchsqlText = "INSERT INTO owSearchResults (idSearch, idProduct, idCategory) SELECT " & sIDSearch & ", P.idProduct, C.idCategory  FROM Products P, Categories C, CatProdMap CPM WHERE P.idProduct = CPM.idProduct AND CPM.idCategory = C.idCategory AND P.showInHome = -1 AND P.active = -1"
		
		' Parse the keywords out
		sKeywords = Replace(sKeywords, " ,", ",")
		sKeywords = Replace(sKeywords, ", ", ",")
		sKeywords = Replace(sKeywords, """", "")
		sKeywords = SQLEncode(sKeywords) & ","
		
		iEndPos = InStr(sKeywords, ",")
		iStartPos = 1
		if iEndPos > 0 then
			while iEndPos > 0
				if iStartPos = 1 then
					SearchsqlText = SearchsqlText & "AND ((P.SearchKeywords LIKE '" & "%" & mid(sKeywords, iStartPos, iEndPos - iStartPos) & "%" & "' OR P.SKU LIKE '" & "%" & mid(sKeywords, iStartPos, iEndPos - iStartPos) & "%" & "' OR P.Description LIKE '" & "%" & mid(sKeywords, iStartPos, iEndPos - iStartPos) & "%" & "' OR P.Details LIKE '" & "%" & mid(sKeywords, iStartPos, iEndPos - iStartPos) & "%" & "' OR C.CategoryDesc LIKE '" & "%" & mid(sKeywords, iStartPos, iEndPos - iStartPos) & "%" & "')"
				else
					SearchsqlText = SearchsqlText & " OR (P.SearchKeywords LIKE '" & "%" & mid(sKeywords, iStartPos, iEndPos - iStartPos) & "%" & "' OR P.SKU LIKE '" & "%" & mid(sKeywords, iStartPos, iEndPos - iStartPos) & "%" & "' OR P.Description LIKE '" & "%" & mid(sKeywords, iStartPos, iEndPos - iStartPos) & "%" & "' OR P.Details LIKE '" & "%" & mid(sKeywords, iStartPos, iEndPos - iStartPos) & "%" & "' OR C.CategoryDesc LIKE '" & "%" & mid(sKeywords, iStartPos, iEndPos - iStartPos) & "%" & "')"
				end if
				if iEndPos < len(sKeywords) then
					iStartPos = iEndPos + 1
					iEndPos = InStr(iStartPos, sKeywords, ",")
				else
					iEndPos = 0
				end if
			wend
		else
			' there's just one word with no spaces
			SearchsqlText = SearchsqlText & "AND ((P.SearchKeywords LIKE '" & "%" & mid(sKeywords, iStartPos, iEndPos - iStartPos) & "%" & "' OR P.SKU LIKE '" & "%" & mid(sKeywords, iStartPos, iEndPos - iStartPos) & "%" & "' OR P.Description LIKE '" & "%" & mid(sKeywords, iStartPos, iEndPos - iStartPos) & "%" & "' OR P.Details LIKE '" & "%" & mid(sKeywords, iStartPos, iEndPos - iStartPos) & "%" & "' OR C.CategoryDesc LIKE '" & "%" & mid(sKeywords, iStartPos, iEndPos - iStartPos) & "%" & "')"
		end if
		SearchsqlText = SearchsqlText & " )"
		
		If ( sInitialPrice <> "" ) And ( sFinalPrice <> "" ) And ( IsNumeric(sInitialPrice) ) And ( IsNumeric(sFinalPrice) ) Then
			SearchsqlText = SearchsqlText & " AND P.Price BETWEEN " & sInitialPrice & " AND " & sFinalPrice
		End If
		
		If ( sIDSupplier <> "" ) And ( IsNumeric(sIDSupplier) ) Then
			SearchsqlText = SearchsqlText & " AND P.idSupplier = " & sIDSupplier
		End If
		
		If ( sIDCategory <> "" ) And ( IsNumeric(sIDCategory) ) Then
			SearchsqlText = SearchsqlText & " AND CPM.idCategory = " & sIDCategory
		End If
		
		SearchsqlText = SearchsqlText & " ORDER BY C.idCategory, P.SKU"
		
		Call DB_updateDatabase (SearchsqlText)

		%>
		<script language='JavaScript1.2' type='text/javascript'>
	
			document.location = "DisplayResults.asp?sIDSearch=<%=sIDSearch%>";
			
		</script>
		<%
		
	End Sub
	'************************************************************************************
%>