<!--#include file="../../owFunctions/I_GeneralFunctions.asp"-->
<!--#include file="../header.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 IdCategory, CategoryDesc
	Dim sSearchText, sSearch, sSQLCount, bResetTtlNumItems, bResetDBLoc
	Dim iBeginPagination, sURL, iTtlNumItems, iDBLoc, sQuerystring
	Dim sTarget, iPaginationCount, sSort, sSuccess, sQueryString2
	Dim sSupplier, sTempSearch

	sAction				= Trim(Request("sAction"))
	sSort				= Trim(Request("sSort"))
	sSearchText			= Trim(Request("sSearchText"))
	sSearch				= Trim(Request("sSearch"))
	bResetDBLoc			= False
	sURL				= "owListCategories.asp"
	sQuerystring		= "sAction=" & sAction & "&sSearchText=" & sSearchText & "&sSearch=" & sSearch & "&sSort=" & sSort
	sTarget				= "owMain"

	If ( sAction = "Delete" ) Then
		For Each sCheckbox In Request.Form
			If ( InStr(sCheckbox, "sDelete") > 0 ) Then
				sTemp = Replace(sCheckbox, "sDelete", "")
				sSuccess = DeleteCategories(sTemp)
			End If
		Next
		bResetDBLoc = True
	End If
	
	If ( sSearchText <> "" ) Then
		If ( sSearch = "FirstChar" ) Then
			sTempSearch = " AND categoryDesc LIKE '" & sSearchText & "%'"
		Else
			sTempSearch = " AND " & sSearch & " LIKE '%" & sSearchText & "%'"
		End If
	Else
		sTempSearch = ""
	End If

	sSQLText	= "SELECT * FROM categories WHERE idCategory > 1" & sTempSearch
	sSQLCount	= "SELECT Count(*) FROM categories WHERE idCategory > 1" & sTempSearch

	If ( sSort <> "" ) Then
		sSQLText = sSQLText & " ORDER BY " & sSort
	Else
		sSQLText = sSQLText  & " ORDER BY idparentcategory, displayOrder, categoryDesc"
	End If
	
	iBeginPagination = SetupPagination( sSQLCount, bResetTtlNumItems, bResetDBLoc )
	Call PaginationQuery( sSQLText, rsStoreData, iBeginPagination )
	
	%>
	<script language = "javascript">
		parent.owBodyFooter.location="owBodyFooter.asp?sAction=<%=sAction%>&sSearchText=<%=sSearchText%>&sSort=<%=sSort%>&sSearch=<%=sSearch%>&iOWNumPerPage=<%=iOWNumPerPage%>&iTtlNumItems=<%=iTtlNumItems%>&iDBLoc=<%=iDBLoc%>"
	</script>
	<%
	
	If ( sSuccess <> "" ) Or ( rsStoreData.EOF ) Then
		If ( sSuccess <> "" ) Then
			%>
			<script language = "javascript">
				parent.owBodyTopNav.location="owBodyTopNav.asp?sMessage=<%=Server.URLEncode(sSuccess)%>"
			</script>
			<%
		End If
		If ( rsStoreData.EOF ) Then
			%>
			<script language = "javascript">
				parent.owBodyTopNav.location="owBodyTopNav.asp?sMessage=<%=Server.URLEncode(LANG_DT( "NOCATEGORIESFOUND", "** No categories found **" ))%>"
			</script>
			<%
		End If
	Else
		' just refresh the top bar to remove any "Save" buttons or old messages
		%>
		<script language = "javascript">
			parent.owBodyTopNav.location="owBodyTopNav.asp?sMessage="
		</script>
		<%
	End If
	sQueryString = "sSearchText=" & Server.URLEncode(sSearchText) & "&sSearch=" & Server.URLEncode(Trim(sSearch)) & "&sSort="
	%>
	<script language = "javascript">
		
		function ColumnOn(sStyle)
		{
			sStyle.background	= "#F5C058";
		}
		function ColumnOff(sStyle)
		{
			sStyle.background	= "#C3DAF9";
		}
		
		function ColumnDataOn(sStyle)
		{
			sStyle.background	= "#EAF1F9";
		}
		function ColumnDataOff(sStyle)
		{
			sStyle.background	= "#FFFFFF";
		}
		
	</script>
	<body bgcolor="C3DAF9" text="#000000" topmargin="0" leftmargin="0">
		<table width=100% border="0" cellpadding="2" cellspacing="0" style="font-size: 11px" bgcolor=FFFFFF>
		<form action='owListCategories.asp' method='post' name='CategoryList'>
			<input type='hidden' name='sAction' value=''>
			<input type='hidden' name='sSort' value=''>
			<tr> 
				<td class="ColumnHead" align="center">Select</td>
				<td onClick="location.href='owListCategories.asp?<%=sQueryString%>idCategory'" onMouseOver="ColumnOn(this.style);" onMouseOut="ColumnOff(this.style);" class="ColumnHead"><%=LANG_DT( "ID", "ID" )%></td>
				<td onClick="location.href='owListCategories.asp?<%=sQueryString%>categoryDesc'" onMouseOver="ColumnOn(this.style);" onMouseOut="ColumnOff(this.style);" class="ColumnHead"><%=LANG_DT( "CATEGORY", "Category" )%></td>
				<td onClick="location.href='owListCategories.asp?<%=sQueryString%>idParentCategory'" onMouseOver="ColumnOn(this.style);" onMouseOut="ColumnOff(this.style);" class="ColumnHead"><%=LANG_DT( "PARENT", "Parent" )%></td>
				<td onClick="location.href='owListCategories.asp?<%=sQueryString%>DisplayOrder'" onMouseOver="ColumnOn(this.style);" onMouseOut="ColumnOff(this.style);" class="ColumnHead" align="center"><%=LANG_DT( "ORDER", "Order" )%></td>
				<td onClick="location.href='owListCategories.asp?<%=sQueryString%>Featured'" onMouseOver="ColumnOn(this.style);" onMouseOut="ColumnOff(this.style);" class="ColumnHead" align="center"><%=LANG_DT( "FEATURED", "Featured?" )%></td>
				<td onClick="location.href='owListCategories.asp?<%=sQueryString%>Active'" onMouseOver="ColumnOn(this.style);" onMouseOut="ColumnOff(this.style);" class="ColumnHead" align="center"><%=LANG_DT( "ACTIVEQUEST", "Active?" )%></td>
			</tr>
			<%
			sSQLCount = 0
			While ( ( NOT rsStoreData.EOF ) And ( sSQLCount < iOWNumPerPage ) )
				IdCategory			= rsStoreData("idCategory")
				CategoryDesc		= rsStoreData("categoryDesc")   
				IdParentCategory	= rsStoreData("idParentCategory")
				DisplayOrder		= rsStoreData("DisplayOrder")
				sActive				= rsStoreData("Active")
				sFeatured			= rsStoreData("Featured")
				sSQLText = "SELECT categoryDesc FROM categories WHERE idCategory = " & IdParentCategory
				Call DB_getFromDatabase (sSQLText, rsStoreData2, "")
				If ( NOT rsStoreData2.EOF ) Then
					sParentCat = Trim(rsStoreData2("categoryDesc"))
					If ( UCase(sParentCat) = LANG_DT( "ROOT", "ROOT" ) ) Then
						sParentCat = "&lt;Root&gt;"
					End If
				Else
					sParentCat = "&lt;Root&gt;"
				End If
				Call DB_closeRecordset(rsStoreData2)
				sQueryString2 = sQueryString & sSort & "&idCategory=" & IdCategory
				%>
				<tr onMouseOver="ColumnDataOn(this.style);" onMouseOut="ColumnDataOff(this.style);"> 
					<td width=5% class='ColumnData' align="center"><input size="1" type="checkbox" style="font-size: 6px;" name="sDelete<%=idCategory%>" value="46"></td>
					<td width=5% onClick="location.href='owModifyCategories.asp?<%=sQueryString2%>'" class='ColumnData' align="left"><%=IdCategory%>&nbsp;</td>
					<td onClick="location.href='owModifyCategories.asp?<%=sQueryString2%>'" class='ColumnData' align="left"><%=CategoryDesc%>&nbsp;</td>
					<td width=15% onClick="location.href='owModifyCategories.asp?<%=sQueryString2%>'" class='ColumnData' align="left"><%=sParentCat%>&nbsp;</td>
					<td width=5% onClick="location.href='owModifyCategories.asp?<%=sQueryString2%>'" class='ColumnData' align="center"><%=DisplayOrder%>&nbsp;</td>
					<td width=5% onClick="location.href='owModifyCategories.asp?<%=sQueryString2%>'" class='ColumnData' align="center">
						<%
						If ( sFeatured = -1 ) Then
							Response.Write LANG_DT( "YES", "Yes" )
						Else
							Response.Write LANG_DT( "NO", "No" )
						End If
						%>
					</td>
					<td width=5% onClick="location.href='owModifyCategories.asp?<%=sQueryString2%>'" class='ColumnData' align="center">
						<%
						If ( sActive = -1 ) Then
							Response.Write LANG_DT( "YES", "Yes" )
						Else
							Response.Write LANG_DT( "NO", "No" )
						End If
						%>
					</td>
				</tr>
				<%
				sSQLCount = sSQLCount + 1
				rsStoreData.moveNext
			Wend
			Call DB_closeRecordset(rsStoreData)
			%>
    		</form>
  		</table>
	<!--#include file="../footer.asp"-->
	<%
	'************************************************************************************


	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Function DeleteCategories(pIdCategory)

		Dim sTemp
		
		If ( NOT bOWBackOfficeDemoMode ) Then	' just do a cursory check in case hackers try to replicate the forms on their computers (return no error)
			' Verify assignment products
			sSQLText = "SELECT P.idProduct FROM Products P, CatProdMap CP WHERE P.idProduct = CP.idProduct AND CP.idCategory=" & pIdCategory
			Call DB_getFromDatabase(sSQLText, rsStoreData, "") 
			If ( NOT rsStoreData.EOF ) Then
			  DeleteCategories = LANG_DT( "CATEGORIES_CANT_DELETE", "** Category assigned, can't delete **" )
			  Call DB_closeRecordset(rsStoreData)
			Else
				' Verify categories assigned
				Call DB_closeRecordset(rsStoreData)
				sSQLText = "SELECT idCategory from categories WHERE idParentCategory = " & pIdCategory	
				Call DB_getFromDatabase(sSQLText, rsStoreData, "") 
				If ( NOT rsStoreData.EOF ) Then
					DeleteCategories = LANG_DT( "CATEGORIES_DEL_ISPARENT", "Please delete this categories subcategories first." )
				Else				
					Call DB_closeRecordset(rsStoreData)
					sSQLText = "SELECT imageCategory from categories WHERE IdCategory=" &pIdCategory	
					Call DB_getFromDatabase(sSQLText, rsStoreData, "") 
					if not rsStoreData.eof then
						sTemp = Trim(rsStoreData("imageCategory"))
						If ( sTemp <> "" ) Then
							Call DeleteCategoryImage(sTemp)
						End If
					End If
					
					Call DB_updateDatabase("DELETE FROM CatProdMap WHERE idCategory = " & pIdCategory	)
					Call DB_updateDatabase("DELETE FROM categories WHERE idCategory = " & pIdCategory	)

					DeleteCategories = LANG_DT( "CATEGORIES_SUCCESS_DEL", "** Category(s) successfully deleted **" )
				End If
				Call DB_closeRecordset(rsStoreData)
			End If
		End If

	End Function
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*	This saves the file (if any). 													*
	'*																					*
	'************************************************************************************
	Sub DeleteCategoryImage(sFileName)
	
		Dim sFilePath, lf, f
	
		Set lf = server.createObject("Scripting.FileSystemObject")
		sFilePath	= "../../" & sOWCategoryImage
		sFilePath	= Replace(sFilePath, "\", "/")
		sFilePath	= Replace(sFilePath, "//", "/")
		sFilePath	= server.mappath(sFilePath) & "\" & sFileName
		If ( lf.FileExists(sFilePath) ) Then	
			Set f = lf.GetFile(sFilePath)
			f.Delete
			Set f = Nothing
		End If
		Set lf = Nothing
	
	End Sub
	'************************************************************************************
%>