<?php
session_start();

include "../functions.php";
checkAdmin(session_id());

include "header.php";

$step = 20;
$page = $_GET["page"];
$key = $_GET["key"];

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

$query = "select staffid, firstname, lastname, staffname from staff order by staffname limit $start, $step";
if ($key)
{
	$key = trim($key);
	$query = "select staffid, firstname, lastname, staffname from staff where staffname like '%$key%' order by staffname limit $start, $step";
}
printstaff($query, $key);

$query = "select count(*) from staff";
if ($key)
{
	$query = "select count(*) from staff where staffname like '%$key%'";
	$tail = "key=$key";
}
pages ($query, $step, $tail);
?>

<?php
include "footer.php";
?>

<?php
function printstaff($query, $key)
{
	echo "<center>";
	echo "<table width=80% cellspacing=1 cellpadding=3>";
	searchform($key);
	$result = mysql_query($query) or die (mysql_error());
	if (mysql_num_rows($result) < 1)
	{
		echo "<tr><td>no staff found</td></tr>";
	}
	else
	{
		echo "<tr bgcolor=\"#a0a0a0\"><th>First Name</th><th>Last Name</th><th>Access Name</th><th colspan=2>Options</th></tr>";
		
		$trigger = 1;
		
		while ($row = mysql_fetch_array($result))
		{
			$staffid = $row[0];
			$firstname = $row[1];
			$lastname = $row[2];
			$staffname = $row[3];
			
			$trigger = ($trigger + 1) % 2;
			
			if ($trigger)
			{
				echo "<tr bgcolor=\"c9c9c9\">";
			}
			else
			{
				echo "<tr bgcolor=\"e9e9e9\">";
			}
			echo "<td>$firstname</td>";
			echo "<td>$lastname</td>";
			echo "<td><a href=\"move.php?staffid=$staffid\" title=\"View this staff's activity\">$staffname</a></td>";
			echo "<td align=\"right\"><form action=\"modstaff.php\" method=\"post\"><input type=\"hidden\" name=\"staffid\" value=$staffid><input type=\"submit\" value=\"Modify\"></td></form>";
			echo "<td><form action=\"delstaff.php\" method=\"post\"><input type=\"hidden\" name=\"staffid\" value=$staffid><input type=\"submit\" value=\"Delete\"></td></form>";
			echo "</tr>";
		}
	}
	echo "</table>";
	echo "</center>";
}

function pages($query, $step, $tail='')
{
	$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 "<center><table width=80%>";
		echo "<tr><td align=\"right\">";
		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>";
		echo "</table></center>";
	}
}

function searchform($key)
{
	echo "<tr><td colspan=5 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>";
}
?>