<?php
session_start();
$staffid = isloggedin(session_id());
if ($staffid == -1)
{
	echo "<script>window.close();</script>";
	exit;
}
$res = mysql_query("select recep from staff where staffid='$staffid'");
$recep = mysql_result($res, 0, 0);

if ($_POST["act"] == "mod")
{
	$bookid = $_POST["bookid"];
	if ($recep == 1)
	$res = mysql_query("select roomid from roombook where bookid='$bookid'");
	else
	$res = mysql_query("select roomid from roombook where bookid='$bookid' and staffid='$staffid'");
	if ($row = mysql_fetch_array($res))
	{
	}
	else
	{
		?>
		<script language="javascript">
		window.alert("You didn't book this room! ");
		window.close();
		</script>
		<?php
		exit;
	}
	$date = $_POST["date"];
	$kt = trim($_POST["kt"]);
	$jt = trim($_POST["jt"]);
	$roomid = $_POST["roomid"];
	$bookid = $_POST["bookid"];
	$sd = trim($_POST["sd"]);
	$ld = trim($_POST["ld"]);
	
	$k = strtotime($date . " " . $kt . ":00");
	$j = strtotime($date . " " . $jt . ":00");
	
	if ((!$kt) || (! $jt))
	{
		$err = "Please enter start and stop time";
	}
	elseif (($k == -1) || ($j == -1))
	{
		$err = "Please make sure you enter the correct time format";
	}
	elseif ($k >= $j)
	{
		$err = "Start time cannot be later than stop time";
	}
	elseif (! $sd)
	{
		$err = "Please enter the use for booking";
	}
	else
	{
		$res = mysql_query("select staff.firstname, staff.lastname from roombook, staff where bookid<>'$bookid' and roomid='$roomid' and kt<'$j' and jt>'$k' and roombook.staffid=staff.staffid") or die (mysql_error());
		if ($row = mysql_fetch_array($res))
		{
			$staffname = $row[0] . ", " . $row[1];
			$err = "your time slot has confliction with $staffname";
		}
	}
	
	if (! $err)
	{
		mysql_query("update roombook set roomid='$roomid', kt='$k', jt='$j', sd='$sd', ld='$ld' where bookid='$bookid'");
		?>
		<script language="javascript">
		window.opener.location='fb.php?date=<?php echo $date; ?>';
		window.close();
		</script>
		<?php
	}
}
else
{
	$bookid = $_GET["bookid"];
	
	if ($recep == 1)
	{
		$res = mysql_query("select roomid, kt, jt, sd, ld from roombook where bookid='$bookid'");
	}
	else
	$res = mysql_query("select roomid, kt, jt, sd, ld from roombook where bookid='$bookid' and staffid='$staffid'");
	if ($row = mysql_fetch_array($res))
	{
		$roomid = $row[0];
		$kt = $row[1];
		$jt = $row[2];
		$sd = $row[3];
		$ld = $row[4];

		$date = date("Y-m-d", $kt);
		$kt = date("H:i", $kt);
		$jt = date("H:i", $jt);
	}
	else
	{
		?>
		<script language="javascript">
		window.alert("You didn't book this room! ");
		window.close();
		</script>
		<?php
		exit;
	}
}
?>
<html>
<head>
<title>Facility Booking</title>
<script language='javascript' src="js/popcalendar.js"></script>
</head>
<body>


<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" name=mainform>
<table width=80% cellspacing=0 cellpadding=3 border=0 align=center>
<?php
if ($err) echo "<tr><td colspan=2 align=center><font color=red>$err</font></td></tr>";
?>
<tr><td colspan=2><b>Date</b><br><input type="text" name=date value="<?php echo "$date"; ?>" size=12 readonly onfocus='popUpCalendar(this, mainform.date, "yyyy-mm-dd")'></td></tr>
<tr>
<td width=50%><b>Start Time</b>(24 hour format 12:34)<br><input size=5 maxlength=5 type="text" name="kt" value="<?php echo $kt; ?>"></td>
<td width=50%><b>Stop Time</b>(24 hour format 12:34)<br><input size=5 maxlength=5 type="text" name="jt" value="<?php echo $jt; ?>"></td>
</tr>
<tr><td colspan=2><b>Room</b><br><select name=roomid>
<?php
$res = mysql_query("select roomid, roomname from room order by roomname asc");
while ($row = mysql_fetch_array($res))
{
	if ($roomid == $row[0])
	{
		echo "<option value=$row[0] selected>$row[1]</option>";
	}
	else
	{
		echo "<option value=$row[0]>$row[1]</option>";
	}
}
?>
</select></td></tr>
<tr><td colspan=2><b>Use</b><br><input type="text" name="sd" value="<?php echo $sd; ?>"></td></tr>
<!--
<tr><td colspan=2><b>Detail</b><br><textarea name="ld" rows=5 cols=30 wrap=virtual><?php echo $sd; ?></textarea></td></tr>
-->
<tr><td colspan=2 align=center><input type=hidden name=act value=mod><input type=hidden name=bookid value="<?php echo $bookid; ?>"><input type=submit value="Modify Booking"></td></tr>
</table>
</form>
</body>
</html>

<?php
function isloggedin($sessid)
{
	include_once "conn.php";
	
	$query = "select loginstaff.staffid from loginstaff, staff where loginstaff.sessid='$sessid' and loginstaff.staffid=staff.staffid";
	$result = mysql_query($query) or die (mysql_error());
	if ($row = mysql_fetch_array($result))
	{
		$staffid = $row[0];
		return $staffid;
	}
	
	return -1;
}
?>