<?php
session_start();

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

include "header.php";

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

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

$query = "select moveid, movetitle, startdate, stopdate from move limit $start, $step";
if ($key)
{
	$key = trim($key);
	$query = "select moveid, movetitle, startdate, stopdate from move where movetitle like '%$key%' or movedetail like '%$key%' limit $start, $step";
}

if ($staffid)
{
	$query = "select move.moveid, move.movetitle, move.startdate, move.stopdate from move, move_staff where move_staff.staffid='$staffid' and move_staff.moveid=move.moveid";
}

printmove($query, $key);

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

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

<?php
function printmove($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 movement found</td></tr>";
	}
	else
	{
		echo "<tr bgcolor=\"#a0a0a0\"><th width=1% nowrap>Start Date</th><th width=1% nowrap>Stop Date</th><th>Activity</th><th width=1% nowrap>Viw Detail</th></tr>";
		
		$trigger = 1;
		
		while ($row = mysql_fetch_array($result))
		{
			$moveid = $row[0];
			$movetitle = $row[1];
			$movedate = $row[2];
			$stopdate = $row[3];
			
			$movedate = fd($movedate);
			$stopdate = fd($stopdate);
		
			$trigger = ($trigger + 1) % 2;
			
			if ($trigger)
			{
				echo "<tr bgcolor=\"c9c9c9\">";
			}
			else
			{
				echo "<tr bgcolor=\"e9e9e9\">";
			}
			echo "<td width=1% nowrap>$movedate</td>";
			echo "<td width=1% nowrap>$stopdate</td>";
			echo "<td>$movetitle</td>";
			echo "<td align=center><input type=\"button\" value=\"View\" onclick=\"showmd($moveid)\"></td>";
			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>";
}

function fd($date)
{
	$split = explode("-", $date);
	$date = date("M jS, Y", mktime(0,0,0,$split[1], $split[2], $split[0]));
	return $date;
}
?>
<script language="javascript">
function showmd(moveid)
{
	window.open('movedetail.php?moveid='+moveid,"smallWindow","width=500,height=375,scrollbars,resizable"); 
	return false; 
}
</script>