<table width=100% border=0 cellspacing=0 cellpadding=0 valign="top">

<?php
include_once "conn.php";

$categoryID = $_GET["categoryID"];
$key = $_GET["key"];
$step = 10;
$page = $_GET["page"];

if (!($page > 1))
{
	$page = 1;
}
$start = ($page - 1) * $step;

if ($key)
{
	echo "<tr class=\"heading\"><td height=20 valign=\"top\"><a href=\"index.php\">Home</a> > <a href=profile.php>Profile</a> > <a href=\"news.php\">News</a></td></tr>";
	echo "<tr><td align=\"right\"><form action=\"" . $_SERVER["PHP_SELF"] . "\"><input size=10 type=\"text\" name=\"key\" value=\"$key\">&nbsp;<input type=\"submit\" value=\"Search\"></td></form></tr>";
	
	$query = "select id, date, subject from news where subject like '%$key%' or summary like '%$key%' or body like '%$key%' limit $start, $step";
	
	echo "<tr><td valign=\"top\">";
	printnews($query);
	echo "</td></tr>";
	
	pages($step, 1, $key);
}

elseif ($categoryID > 0)
{
	$query = "select categoryName from news_category where categoryID='$categoryID'";
	$result = mysql_query($query) or die (mysql_error());
	while ($row = mysql_fetch_array($result))
	{
		$categoryName = $row[0];
	}
	echo "<tr class=\"heading\"><td height=20 valign=\"top\"><a href=\"index.php\">Home</a> > <a href=profile.php>Profile</a> > <a href=\"news.php\">News</a> > <a href=\"news.php?categoryID=$categoryID\">$categoryName</a></td></tr>";
	echo "<tr><td align=\"right\"><form action=\"" . $_SERVER["PHP_SELF"] . "\"><input size=10 type=\"text\" name=\"key\" value=\"$key\">&nbsp;<input type=\"submit\" value=\"Search\"></td></form></tr>";
	
	$day = date ("d");
	$month = date ("m");
	$year = date ("Y");
	$startfrom = date("Ymd", mktime(0,0,0, ($month-3), $day, $year));
		
	$query = "select id, date, subject from news where categoryID='$categoryID'";
	/*
	if ($_GET["arch"] == 1)
	{
		$query .= " and date<'$startfrom'";
	}
	else
	{
		$query .= " and date>'$startfrom'";
	}
	*/
	$query .= " limit $start, $step";
	
	echo "<tr><td valign=\"top\">";
	printnews($query);
	echo "</td></tr>";
	
	pages($step, 2, '', $categoryID, $_GET["arch"]);
	
}
else
{
	echo "<tr class=\"heading\"><td height=20 valign=\"top\"><a href=\"index.php\">Home</a> > <a href=profile.php>Profile</a> > <a href=\"news.php\">News</a></td></tr>";
	
	echo "<tr><td align=\"right\"><form action=\"" . $_SERVER["PHP_SELF"] . "\"><input size=10 type=\"text\" name=\"key\" value=\"$key\">&nbsp;<input type=\"submit\" value=\"Search\"></td></form></tr>";
	
	$query = "select categoryID, categoryName from news_category order by categoryName asc";
	$result = mysql_query($query) or die (mysql_error());
	
	while ($row = mysql_fetch_array($result))
	{
		$categoryID = $row[0];
		$categoryName = $row[1];
		
		echo "<tr><td><a style=\"font-weight:bold;\" title=\"View news in this category\" href=\"" . $_SERVER["PHP_SELF"] . "?categoryID=$categoryID\">$categoryName</a></td></tr>";
		$query = "select id, date, subject from news where categoryID='$categoryID'";
		/*
		if ($_GET["arch"] == 1)
		{
			$query .= " and date<'$startfrom'";
		}
		else
		{
			$query .= " and date>'$startfrom'";
		}
		*/
		$query .= " order by date desc, id desc limit 3";
		
		echo "<tr><td valign=\"top\">";
		printnews($query);
		echo "</td></tr>";
		echo "<tr><td>&nbsp;</td></tr>";
	}
}

function printnews($query)
{
	//echo $query;
	$result = mysql_query($query) or die (mysql_error());
	
	echo "<table border=0 cellpadding=5 cellspacing=0>";
	while ($row = mysql_fetch_array($result))
	{
		$id = $row[0];
		$date = $row[1];
		$year = substr($date, 0, 4);
		$month = substr($date, 4, 2);
		$day = substr($date, 6, 2);
		$showday = date("M jS Y", mktime(0,0,0, $month, $day, $year));
		$subject = $row[2];
		echo "<tr>";
		echo "<td width=\"100\" valign=\"top\">$showday</td>";
		echo "<td><a href=\"news/detail.php?id=$id\" onClick=return(openSmallWindow('news/detail.php?id=$id'))>$subject</a></td>";
		echo "</tr>";
	}
	echo "</table>";
}

function pages($step, $mode, $key='', $categoryID='', $arch=0) //$mode = 1 search, $mode=2 browse single category
{
	if ($mode == 1)
	{
		$query = "select count(*) from news where subject like '%$key%' or summary like '%$key%' or body like '%$key%'";
		$tail = "key=$key";
	}
	else
	{
		$query = "select count(*) from news where categoryID='$categoryID'";
		if ($arch)
		{
			$query .= " and date<'$startfrom'";
			$tail = "categoryID=$categoryID&arch=1";
		}
		else
		{
			$query .= " and date>'$startfrom'";
			$tail = "categoryID=$categoryID";
		}
	}
	
	$result = mysql_query($query) or die (mysql_error());
	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 align=\"left\">";
		echo "<table cellpadding=0><tr>";
		if ($pages <= 7)
		{
			for ($i = 1; $i <= $pages; $i++)
			{
				echo "<td align=left><a href=$PHP_SELF?$tail&page=$i>$i</a></td>";
			}
		}
	
		else
		{
			for ($i = 1; $i <= 7; $i++)
			{
				echo "<td align=left><a href=$PHP_SELF?$tail&page=$i>$i</a></td>";
			}
			echo "<td align=left>...</td><td><a href=$PHP_SELF?$tail&page=$pages>$pages</a></td>";
		}
		echo "<td align=left><form action=\"" . $_SERVER["PHP_SELF"] . "\" method=get>";
		if ($mode == 1)
		{
			echo "<input type=\"hidden\" name=\"key\" value=\"$key\">";
		}
		else
		{
			echo "<input type=\"hidden\" name=\"categoryID\" value=\"$categoryID\">";
		}
		echo "<input type=text size=3 name=page><input type=submit value=Go></td></form>";
		echo "</tr></table>";
		echo "</td></tr>";
	}
}

function searchform()
{
}
?>
</table>