<script language="javascript">
window.location = '../joblist.php';
</script>

<?php
session_start();

include "../conn.php";
include "../functions.php";

checkAdmin(session_id());

$title = "Job Post Listing";
include "header.php";

$page = $_GET["page"];
$key = $_GET["key"];
$jobcatid = $_GET["jobcatid"];
$range = $_GET["range"];
?>

<center>
<table width=80%><tr><td align="right">
<table border=0 cellspacing=1 cellpadding=5>

<tr>
<td>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="get">
<!--
<select name="range">
<option value="0">- All Files -</option>
<option value="1" <?php if ($range == 1) echo "selected"; ?>>- Past 1 week -</option>
<option value="2" <?php if ($range == 2) echo "selected"; ?>>- Past 1 month -</option>
<option value="3" <?php if ($range == 3) echo "selected"; ?>>- Past 3 months -</option>
<option value="4" <?php if ($range == 4) echo "selected"; ?>>- Past 6 months -</option>
<option value="5" <?php if ($range == 5) echo "selected"; ?>>- Past 1 year -</option>
</select>
-->
<select name="jobcatid">
<option value="0">-All Categories-</option>
<?php
$query = "select jobcatid, catname from jobcat";
$result = mysql_query($query) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
	if ($row[0] == $jobcatid)
	{
		echo "<option value=\"$row[0]\" selected>$row[1]</option>";
	}
	else
	{
		echo "<option value=\"$row[0]\">$row[1]</option>";
	}
}
?>
</select>
</td>
<td>
<input type="text" name="key" value="<?php echo $key; ?>">
</td>
<td>
<input type="submit" value="search">
</td>
</tr>
</table>
</td>
</form>
</tr>
</table>


<table width=80% border=0 cellspacing=1 cellpadding=5>
<?php

if (! isset($page))
{
	$page = 1;
}

$step = 30;

$start = ($page - 1) * $step;

echo "<tr bgcolor=\"a0a0a0\"><th>Job Title</th><th>Job Description</th><th>End Date</th><th>Applied Number</th><th colspan=2>Options</th></tr>";

if ($jobcatid > 0)
{
	$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";
	
	if (isset($key))
	{
		$query .= " and (job.jobtitle like '%$key%' or job.jobdescrip like '%$key%')";
	}
}
else
{
	$query = "select jobid, jobtitle, jobdescrip, enddate, appliednumber from job"; 

	if (isset($key))
	{
		$query .= " where (jobtitle like '%$key%' or jobdescrip like '%$key%')";
	}
}

$query .= " order by enddate desc, jobid desc limit $start, $step";

$result = mysql_query ($query) or die (mysql_error());

$trigger = 1;

while ($row = mysql_fetch_array($result))
{
	$jobid = $row[0];
	$jobtitle = $row[1];
	$jobdescrip = $row[2];
	$enddate = $row[3];
	$appliednumber = $row[4];
	
	$trigger = ($trigger + 1) % 2;
	
	if ($trigger == 1)
	{
		echo "<tr bgcolor=\"#c9c9c9\">";
	}
	else
	{
		echo "<tr bgcolor=\"#e9e9e9\">";
	}
	
	echo "<td>$jobtitle</td><td>$jobdescrip</td>";
	echo "<td>$enddate</td>";
	echo "<td><a title=\"view report\" href=\"application.php?jobid=$jobid\">$appliednumber</a></td>";
	echo "<td width=1 align=\"center\"><form action=\"modjob.php\" method=\"post\"><input type=\"hidden\" name=\"jobid\" value=\"$jobid\"><input type=\"submit\" value=\"Modify\"></td></form>";
	echo "<td width=1 align=\"left\"><form action=\"deljob.php\" method=\"post\"><input type=\"hidden\" name=\"jobid\" value=\"$jobid\"><input type=\"submit\" value=\"Delete\"></td></form>";
	echo "</tr>";
}
?>
</table>
</center>


<?php
echo "<center><table border=0 width=80%><tr><td align=right width=100% nowrap>";
if ($jobcatid > 0)
{
	$query = "select count(*) from job, job_cat where job_cat.jobcatid='$jobcatid' and job.jobid=job_cat.jobid";
	
	if (isset($key))
	{
		$query .= " and (job.jobtitle like '%$key%' or job.jobdescrip like '%$key%')";
	}
}
else
{
	$query = "select count(*) from job"; 

	if (isset($key))
	{
		$query .= " where (jobtitle like '%$key%' or jobdescrip 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);
}

if ($pages > 1)
{
	echo "<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>";
}
echo "</td></tr></table><center>";
include "footer.php";
?>