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

<?php
include_once "conn.php";

$jobcatid = $_GET["cat"];
$key = $_GET["key"];
$step = 10;
$page = $_GET["page"];

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

if ($key)
{
	$today = date ("Y-m-d");
	
	$query = "select jobid, jobtitle, jobdescrip, enddate, appliednumber from job where (jobtitle like '%$key%' or jobdescrip like '%$key%') and (enddate > '$today' or enddate='0000-00-00')";
	
	echo "<tr><td valign=\"top\">";
	printjob($query);
	echo "</td></tr>";
	
	pages($step, 1, $key);
}

elseif ($jobcatid > 0)
{
	$query = "select catname from jobcat where jobcatid = '$jobcatid'";
	$result = mysql_query($query) or die (mysql_error());
	while ($row = mysql_fetch_array($result))
	{
		$catname = $row[0];
	}
	$today = date ("Y-m-d");
	
	$query = "select job.jobid, job.jobtitle, job.jobdescrip, job.enddate, job.appliednumber from job, job_cat where job_cat.jobcatid = '$jobcatid' and job_cat.jobid=job.jobid and (enddate > '$today' or enddate='0000-00-00')";
	$query .=" order by job.enddate desc";
	$query .= " limit $start, $step";
	
	echo "
	<tr><td valign=\"top\"><b>$catname</b></td></tr>
	<tr><td>&nbsp;</td></tr>
	<tr><td valign=\"top\">";
	printjob($query);
	echo "</td></tr>";
	
	pages($step, 2, '', $categoryID);
	
}
else
{
	$query = "select jobcatid, catname, catdescrip from jobcat order by catname asc";
	
	$result = mysql_query($query) or die (mysql_error());
	
	while ($row = mysql_fetch_array($result))
	{
		$jobcatid = $row[0];
		$catname = $row[1];
		$catdescrip = $row[2];
		
		$today = date ("Y-m-d");
		$query = "select job.jobid, job.jobtitle, job.jobdescrip, job.enddate, job.appliednumber from job, job_cat where job_cat.jobcatid='$jobcatid'  and job.jobid=job_cat.jobid and (enddate > '$today' or enddate='0000-00-00')";
		$query .= " order by enddate desc, jobid desc limit 3";
		
		$res = mysql_query($query);
		if (mysql_num_rows($res))
		{
			echo "<tr><td>Category: <a style=\"font-weight:bold;\" title=\"View job posting in this category\" href=\"" . $_SERVER["PHP_SELF"] . "?cat=$jobcatid\">$catname</a></td></tr>";
		
			echo "<tr><td valign=\"top\">";
			printjob($query);
			echo "</td></tr>";
			echo "<tr><td>&nbsp;</td></tr>";
		}
	}
}

function printjob($query)
{
	//echo $query;
	$result = mysql_query($query) or die (mysql_error());
	if (mysql_num_rows($result))
	{
		echo "<table border=0 cellpadding=5 cellspacing=0>
			<tr><td colspan=2>&nbsp;</td></tr>
			<tr><th align=left>Closing Date</th><th align=left>Positions Available</th></tr>";
		while ($row = mysql_fetch_array($result))
		{
			$jobid = $row[0];
			$jobtitle = $row[1];
			$jobdescrip = $row[2];
			$enddate = $row[3];
			$appliednumber = $row[4];
		
			$year = substr($enddate, 0, 4);
			$month = substr($enddate, 5, 2);
			$day = substr($enddate, 8, 2);
			if ($enddate == '0000-00-00')
			{
				$showday = "Not specified";
			}
			else
			{
				$showday = date("M jS Y", mktime(0,0,0, $month, $day, $year));
			}
			echo "<tr>";
			echo "<td width=\"100\" valign=\"top\">$showday</td>";
			echo "<td><a href=\"job/jobdetail.php?jobid=$jobid\" onClick=return(openSmallWindow('job/jobdetail.php?jobid=$jobid'))>$jobtitle</a></td>";
			echo "</tr>";
		}
		echo "</table>";
	}
}

function pages($step, $mode, $key='', $jobcat='') //$mode = 1 search, $mode=2 browse single category
{
	if ($mode == 1)
	{
		$query = "select count(*) from job where jobtitle like '%$key%' or jobdescrip like '%$key%'";
		$tail = "key=$key";
	}
	else
	{
		$query = "select count(*) from job_cat where jobcatid='$jobcatid'";
	}
	
	$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($key='')
{
	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>";
}
?>
</table>