<?php
include_once "conn.php";

function printSubCategory($parentID) //for normal users
{
	$query = "select categoryID, categoryName, categoryDescrip, categoryPic from categories where parentID='$parentID' and categoryID <> 0";
	$result = mysql_query($query);
	
	$iCounter = 0;
	
	echo "<table border=1 width=80%>";
	
	while ($row = mysql_fetch_array($result))
	{
		$categoryID = $row[0];
		$categoryName = $row[1];
		$categoryDescrip = $row[2];
		$categoryPic = $row[3];
		
		if ($iCounter % 3 == 0)
		{
			echo "<tr>";
		}
		?>
		
		<td width=33% align="center">
		  <table border=1>
		    <tr>
		      <td colspan=2 align=center>
		        <img src="admin/<?php echo $categoryPic; ?>">
		      </td>
		    </tr>
		    <tr>
		      <td align="center">
			<a title="show subcategories" href=category.php?parentID=<?php echo $categoryID; ?>><?php echo $categoryName; ?></a>
		      </td>
		      <td align="center">
			<a title="show products" href=product.php?categoryID=<?php echo $categoryID; ?>><?php echo $categoryName; ?></a>
		      </td>
		    </tr>
		  </table>
		</td>
		
		<?php
		if ($iCounter % 3 == 2)
		{
			echo "</tr>";
		}
		
		
		$iCounter++;
	}
	
	if ($iCounter % 3 == 1) //we need add 2 empty cells
	{
		echo "<td bgcolor=\"#FFFFFF\"></td><td bgcolor=\"#FFFFFF\"></td></tr>";
	}
	elseif($iCounter % 3 == 2) //we need add 1 empty cell
	{
		echo "<td bgcolor=\"#FFFFFF\"></td></tr>";
	}
	echo "</table>";
}

function printProduct($categoryID, $page=1) //for normal users
{
	$step = 15;
	
	$start = ($page - 1) * $step;
	
	$query = "select products.productID, products.productName, products.productPic, products.productDescrip from products, products_categories where products_categories.categoryID='$categoryID' and products.productID=products_categories.productID limit $start, $step";
	//echo $query;
	$result = mysql_query($query);
	
	$iCounter = 0;
	echo "<table border=0 width=100% cellpadding=5 cellspacing=1>";
	
	while ($row = mysql_fetch_array($result))
	{
		
		if ($iCounter % 3 == 0)
		{
			echo "<tr>";
		}
		
		$productID = $row[0];	
		$productName = $row[1];
		$productPic = $row[2];
		$productDescrip = $row[3];
		echo "<td width=33% align=\"center\">";
		echo "<img src=\"admin/$productPic\" width=80 height=80>";
		echo "<br>";
		echo "<a title=\"show details about this product\" href=productdetail.php?productID=$productID onClick=return(openSmallWindow('productdetail.php?productID=$productID'))>$productName</a>";
		echo "</td>";
		
		if ($iCounter % 3 == 2)
		{
			echo "</tr>";
		}	
		
		$iCounter++;
	}
	
	if ($iCounter % 3 == 1) //we need add 2 empty cells
	{
		echo "<td bgcolor=\"#FFFFFF\"></td><td bgcolor=\"#FFFFFF\"></td></tr>";
	}
	elseif($iCounter % 3 == 2) //we need add 1 empty cell
	{
		echo "<td bgcolor=\"#FFFFFF\"></td></tr>";
	}
	
	$query = "select count(*) from products, products_categories where products_categories.categoryID='$categoryID' and products.productID=products_categories.productID";
	$result = mysql_query($query);
	
	while ($row = mysql_fetch_array($result))
	{
		$total = $row[0];
	}
	
	if ($total % $step == 0)
	{
		$pages = $total / $step;
	}
	else
	{
		$pages = ceil($total / $step);
	}
	
	if ($pages > 1)
	{
		echo "<tr><td colspan=3 align=\"right\"><table width=1 border=0 height=1><tr><td valign=bottom nowrap>Page:</td>";
	
		if ($pages <= 7)
		{
			for ($i = 1; $i <= $pages; $i++)
			{
				echo "<td align=left valign=bottom><a href=$PHP_SELF?page=$i&key=$key&groupID=$groupID>$i</a></td>";
			}
		}
	
		if ($pages > 7)
		{
			for ($i = 1; $i <= 7; $i++)
			{
				echo "<td align=left valign=bottom><a href=$PHP_SELF?page=$i&key=$key&groupID=$groupID>$i</a></td>";
			}
			echo "<td align=left valign=bottom>...<a href=$PHP_SELF?page=$pages&key=$key&groupID=$groupID>$pages</a></td>";
		}
	
		echo "<td align=left valign=bottom><form action=$PHP_SELF method=get><input type=text size=3 name=page><input type=submit value=\"Go\"></td></form>";
		echo "</tr></table></td></tr>";
	}
	
	echo "</table>";
	
	
	
	
}

function printCategoryTree($parentID, $lastColor) //for admin
{
	echo "<table border=0 cellspacing=0 width=100%>";
	
	$query = "select categoryID, categoryName, categoryPic from categories where parentID='$parentID' and categoryID <> 0";
	
	$result = mysql_query($query);
	
	while ($row = mysql_fetch_array($result))
	{
		if ($lastColor == 1)
		{
			echo "<tr bgcolor=\"#c9c9c9\">";
			$lastColor = 0;
		}
		else
		{
			echo "<tr bgcolor=\"#e9e9e9\">";
			$lastColor = 1;
		}
		
		
		echo "<td colspan=2><table width=100% border=0 cellspacing=0><tr><td><a title=\"Show the products under this category\" href=\"product.php?categoryID=$row[0]\">$row[1]</a></td><td align=right weidth=60><img src=$row[2] width=50 height=50></td><td width=60 align=right><form action=modCategory.php method=post><input type=hidden name=categoryID value=$row[0]><input type=submit value=modify></td></form><td width=60 align=right><form action=delCategory.php method=post><input type=hidden name=categoryID value=$row[0]><input type=submit value=Delete></td></form></tr></table></td></tr>";
		
		
		$query = "select categoryID from categories where parentID=$row[0] and categoryID <> 0";
		$newresult = mysql_query($query);
		if (mysql_num_rows($newresult) > 0)
		{
			echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td>";
			printCategoryTree($row[0], $lastColor);
			if ($lastcolor == 1)
			{
				$lastColor = 0;
			}
			else
			{
				$lastColor = 1;
			}
			echo "</td></tr>";
		}
	}
	
	echo "</table>";
}

function delCategory($categoryID) //for admin
{
	$query = "select categoryID from categories where parentID='$categoryID'";
	$result = mysql_query($query);
	while ($row = mysql_fetch_array($result))
	{
		delCategory($row[0]);
	}
	
	$query = "delete from products_categories where categoryID='$categoryID'";
	mysql_query($query);
	
	$query = "delete from categories where categoryID='$categoryID'";
	mysql_query($query);
	
	
}

function delProduct($productID) //for admin
{
	$query = "delete from products_categories where productID='$productID'";
	mysql_query($query);
	
	$query = "delete from products where productID='$productID'";
	mysql_query($query);
}

function printProducts($range, $categoryID, $key='', $page=1, $step=30) //for admin, have bug now
{
	$day = date("d");
	$month = date("m");
	$year = date("Y");
	
	if ($range == 1) // 1 week
	{
		$startfrom = date("Y-m-d", mktime(0,0,0, $month, ($day - 7), $year));
	}
	elseif ($range == 2) // 1 month
	{
		$startfrom = date("Y-m-d", mktime(0,0,0, ($month-1), $day, $year));
	}
	elseif ($range == 3) //3 months
	{
		$startfrom = date("Y-m-d", mktime(0,0,0, ($month-3), $day, $year));
	}
	elseif ($range == 4) // 6 months
	{
		$startfrom = date("Y-m-d", mktime(0,0,0, ($month-6), $day, $year));
	}
	elseif ($range == 5) // 1 year
	{
		$startfrom = date("Y-m-d", mktime(0,0,0, $month, $day, ($year-1)));
	}
	
	$start = ($page - 1) * $step;
	
	if ($categoryID == -1)
	{
		$query = "select productID, productName, productPic, productDescrip, addDate from products where (productName like '%$key%' or productDescrip like '%$key%' or productDDescrip like '%$key%')";
		
		if (isset($startfrom))
		{
			$query .= " and addDate>'$startfrom'";
		}
		
		$query .= " limit $start, $step";
	}
	else
	{
		$query = "select products.productID, products.productName, products.productPic, products.productDDescrip, products.addDate from products, products_categories where (products_categories.categoryID='$categoryID' and products.productID=products_categories.productID and (productName like '%$key%' or productDescrip like '%$key%' or productDDescrip like '%$key%'))";
		
		if (isset($startfrom))
		{
			$query .= " and products.addDate>'$startfrom'";
		}
		
		$query .= " limit $start, $step";
	}
	
	//echo "$query";
	
	$result = mysql_query($query) or die (mysql_error());
	
	echo "<table border=0 width=100%>";
	echo "<tr bgcolor=\"#a0a0a0\" height=\"30\"><th>Date</th><th>Product Name</th><th>Product Description</th><th>Product Picture</th><th colspan=2>Options</th></tr>";
	
	$lastColor = 1;
	
	while ($row = mysql_fetch_array($result))
	{
		$productID = $row[0];
		$productName = $row[1];
		$productPic = $row[2];
		$productDDescrip = $row[3];
		$addDate = $row[4];
		
		if ($lastColor == 1)
		{
			$lastColor = 0;
		}
		else
		{
			$lastColor = 1;
		}
		
		altBG($lastColor);
		
		$tmp = stripslashes($productDDescrip);
		$tmp = ereg_replace("\"", "&quote;", $tmp);
		
		if (strlen($tmp) > 60)
		{
			//$tmp = substr($tmp, 0, 57) . "...";
		}
		echo "<td nowrap valign=top>$addDate</td>";
		echo "<td valign=top>$productName</td>";
		echo "<td valign=top>$tmp</td>";
		echo "<td valign=top><img src=$productPic width=50 height=50></td>";
		echo "<td align=\"right\" valign=top><form action=\"modProduct.php\" method=\"post\"><input type=\"hidden\" name=\"productID\" value=\"$productID\"><input type=\"submit\" value=\"Modify\"></td></form>";
		echo "<td align=\"left\" valign=top><form action=\"delProduct.php\" method=\"post\"><input type=\"hidden\" name=\"productID\" value=\"$productID\"><input type=\"submit\" value=\"Delete\"></td></form>";
		echo "</tr>";
	}
	
	if ($categoryID == -1)
	{
		$query = "select count(*) from products where productName like '%$key%' or productDescrip like '%$key%' or productDDescrip like '%$key%'";
	}
	else
	{
		$query = "select count(*) from products, products_categories where products_categories.categoryID='$categoryID' and products.productID=products_categories.productID and (productName like '%$key%' or productDescrip like '%$key%' or productDDescrip like '%$key%')";
	}
	
	$result = mysql_query($query) or die (mysql_error());
	$row = mysql_fetch_array($result);
	
	$total = $row[0];
	
	if ($total % $step == 0)
	{
		$pages = $total / $step;
	}
	else
	{
		$pages = ceil($total / $step);
	}
	//echo "total=$total step=$step pages=$pages";
	if ($pages > 1)
	{
		echo "<tr><td width=100% colspan=6 align=\"right\"><table width=1 border=0 height=1><tr><td valign=bottom nowrap>Page:</td>";
	
		if ($pages <= 7)
		{
			for ($i = 1; $i <= $pages; $i++)
			{
				echo "<td align=left valign=bottom><a href=$PHP_SELF?page=$i&key=$key&groupID=$groupID>$i</a></td>";
			}
		}
	
		if ($pages > 7)
		{
			for ($i = 1; $i <= 7; $i++)
			{
				echo "<td align=left valign=bottom><a href=$PHP_SELF?page=$i&key=$key&groupID=$groupID>$i</a></td>";
			}
			echo "<td align=left valign=bottom>...</td><td align=left valign=bottom><a href=$PHP_SELF?page=$pages&key=$key&groupID=$groupID>$pages</a></td>";
		}
	
		echo "<td align=left valign=bottom><form action=$PHP_SELF method=get><input type=text size=3 name=page><input type=submit value=\"Go\"></td></form>";
		echo "</tr></table></td></tr>";
	}
	
	echo "</table>";
}

function altBG($lastColor)
{
	if ($lastColor == 1)
	{
		echo "<tr bgcolor=\"#c9c9c9\">";
	}
	else
	{
		echo "<tr bgcolor=\"#e9e9e9\">";
	}
}

function checkAdmin($sessionID)
{
	//return 1;
	
	$query = "select userID from loginusers where sessionID='$sessionID'";
	//echo "$query";
	$result = mysql_query($query) or die (mysql_error());
	
	while ($row = mysql_fetch_array($result))
	{
		$userID = $row[0];
	}
	
	if (! isset($userID))
	{
		header("location: ../login.php");
	}
	return $userID;//return $userID;
}

function dropdownList($id=0, $parentID=0, $space=1)
{
	$query = "select categoryID, categoryName from categories where parentID='$parentID' and categoryID <> 0";
	
	$result = mysql_query($query);
	
	while ($row = mysql_fetch_array($result))
	{
		if ($row[0] == $id)
		{
			echo "<option value=$row[0] selected>";
		}
		else
		{
			echo "<option value=$row[0]>";
		}
		
		for ($i = 0; $i < 2*$space; $i++)
		{
			echo "&nbsp;";
		}
		
		echo "$row[1]</option>";
		
		
		$query = "select categoryID from categories where parentID=$row[0] and categoryID <> 0";
		$newresult = mysql_query($query);
		if (mysql_num_rows($newresult) > 0)
		{
			$space++;
			dropdownList($id, $row[0], $space);
			$space--;
		}
	}
}

function getSubCategories($parentID=0)
{
	include_once "conn.php";
	
	$query = "select categoryID, categoryName from categories where parentID='$parentID'";
              
        $result = mysql_query($query);
              
        $iCounter = 0;
	echo "<table border=0 width=100% cellpadding=10 cellspacing=1>";
	
	while ($row = mysql_fetch_array($result))
	{
		
		if ($iCounter % 2 == 0)
		{
			echo "<tr>";
		}
		
		$categoryID = $row[0];	
		$categoryName = $row[1];
		echo "<td width=50% align=\"center\">";
		echo "<a href=categories.php?categoryID=$categoryID>$categoryName</a>";
		echo "</td>";
		
		if ($iCounter % 2 == 1)
		{
			echo "</tr>";
		}	
		
		$iCounter++;
	}
	
	if ($iCounter % 2 == 1) //we need add 1 empty cells
	{
		echo "<td></td></tr>";
	}
	
	echo "</table>";
}
?>