<?php
error_reporting(0);
session_start();
?>
<td valign="top" class="border">
<script language='javascript' src="js/popcalendar.js"></script>
<script language="javascript">
function gothatday()
{
	year = document.form1.y.value;
	month = document.form1.m.value;
	day = document.form1.d.value;
	window.location='fb.php?date='+year+'-'+month+'-'+day;
}
function ds(year, month, day)
{
	document.write('<select name=d>');
	for (i = 1; i < 32; i++)
	{
		if (i < 10) i = '0' + i;
		if (i == day*1) document.write('<option value="'+i+'" selected>'+i+"</option>");
		else document.write('<option value="'+i+'">'+i+"</option>");
	}
	document.write('</select>');
	document.write('<select name=m>');
	for (i = 1; i < 13; i++)
	{
		if (i < 10) i = '0' + i;
		if (i == month*1) document.write('<option value="'+i+'" selected>'+i+"</option>");
		else document.write('<option value="'+i+'">'+i+"</option>");
	}
	document.write('</select>');
	document.write('<select name=y>');
	for (i = year*1 - 1; i < year*1 + 10; i++)
	{
		if (i == year*1) document.write('<option value="'+i+'" selected>'+i+"</option>");
		else document.write('<option value="'+i+'">'+i+"</option>");
	}
	document.write('</select>');
	document.write('<input type=button value=Select onclick="gothatday();"');
}

function confsub()
{
	var conf = confirm("Are you sure you want to delete this booking");
	return conf;
}
</script>
<br>
	<?php
	$staffid = isloggedin(session_id());
	
	$act = $_REQUEST["act"];

	if ($staffid == -1)
	{
		echo "<script>window.location='staffarea.php'</script>";
	}
	else //display content
	{
		if ($act == "del")//delete facility booking
		{
			$res = mysql_query("select recep from staff where staffid='$staffid'");
	$recep = mysql_result($res, 0, 0);
	
			$bookid = $_GET["bookid"];
			$res = mysql_query("select count(*) from roombook where bookid='$bookid' and staffid='$staffid'");
			if ($recep == 1 || mysql_result($res, 0, 0))
			{
				mysql_query("delete from roombook where bookid='$bookid'");
				$date = $_GET["date"];
				?>
				<script language="javascript">window.location='fb.php?>date=<?php echo $date; ?>';</script>
				<?php
			}
			else
			{
				?>
				<script language="javascript">
				window.alert("You didn't book this room!");
				</script>
				<?php
			}
		}
		dispfb($staffid);
	}
	?>
</td>
<?php
if (!($staffid>0))
{
}
?>

<?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;
}

function fd($date)
{
	$split = explode("-", $date);
	$date = date("M jS, Y", mktime(0,0,0,$split[1], $split[2], $split[0]));
	return $date;
}

function dispfb($staffid)
{
	$date = $_GET["date"];
	if ( ! $date)
	{
		$date = date("Y-m-d");
	}
	
	
	$split = explode("-", $date);
	$y = $split[0];
	$m = $split[1];
	$d = $split[2];
	
	$start = mktime(0,0,0,$m, $d, $y);
	$stop = mktime(0,0,0,$m, $d + 1, $y);
	
	$yesterday = date("Y-m-d", mktime(0,0,0,$m, $d - 1, $y));
	$tomorrow = date("Y-m-d", $stop);
	?>
	<table border=0 width=100% cellspacing=0 cellpadding=5>
	  
	  <tr class="heading">
	    
    <td valign="top"><a href="index.php"><font color="#669900">Home</font></a> 
      <font color="#669900">></font> <a href="staffarea.php"><font color="#669900">Staff</font></a> <font color="#669900">> <a href="fb.php">Facility Booking</a></font></td>
	  </tr>
	  <tr><td><table cellspacing=0 cellpadding=0 border=0 width=100%>
	          <tr>
	          <td><form name=pro action="staffarea.php" method="post"><input type="hidden" name="act" value="profile"><table cellpadding=0 cellspacing=0><tr><td><input type="image" src="http://www.axonsg.com/app/img/folder_small.gif" height="42"></td><td onclick="pro.submit()" onmouseover="this.style.cursor='hand'"><u>Profile</u></td></tr></table></td></form>
	          <td><table cellspacing=0 cellpadding=0 border=0><tr><td><a href=fb.php><img border=0 src="http://www.axonsg.com/app/img/folder_small.gif" height="42"></a></td><td onclick="window.location='staffarea.php'" onmouseover="this.style.cursor='hand'"><u>Movements</u></td></tr></table></td>
	          <td><table cellspacing=0 cellpadding=0 border=0><tr><td><a href=fb.php><img border=0 src="http://www.axonsg.com/app/img/folder_small.gif" height="42"></a></td><td onclick="window.location='fb.php'" onmouseover="this.style.cursor='hand'"><u>Facility Booking</u></td></tr></table></td>
	          <td><table cellspacing=0 cellpadding=0 border=0><tr><td><a href=gen.php onclick="window.open('gen.php', '', 'width=600, height=300, resizable, scrollbars')"><img border=0 src="http://www.axonsg.com/app/img/folder_small.gif" height="42"></a></td><td onclick="window.open('gen.php', '', 'width=600, height=300, resizable, scrollbars')" onmouseover="this.style.cursor='hand'"><u>Report</u></td></tr></table></td>
	          <td><form name=log action="staffarea.php" method="post"><input type="hidden" name="act" value="logoff"><table cellpadding=0 cellspacing=0><tr><td><input type="image" src="http://www.axonsg.com/app/img/folder_small.gif" height="42"></td><td onclick="log.submit()" onmouseover="this.style.cursor='hand'"><u>Logoff</u></td></tr></table></td></form>
	          </tr>
	          </table></td></tr>
	  </table>
	  <p>
	  <?php
	  $res = mysql_query("select roomid, roomname from room order by roomname asc");
	  $tpa = array();
	  $sizes = 1;
	  while ($row = mysql_fetch_array($res))
	  {
		$roomid = $row[0];
		$roomname = $row[1];
		
		$room = array('roomid' => $roomid, 'roomname' => $roomname);
		
		array_push($tpa, $room);
		$sizes++;
	  }
	  ?>
	<table width=100% cellspacing=1 cellpadding=3 border=0 bgcolor="#e9e9e9">
	<form name=form1><tr bgcolor="#ffffff"><td colspan=<?php echo $sizes; ?>><table width=100%><tr><td width=10% align=center><a href=fb.php?date=<?php echo $yesterday; ?>><img src=http://www.axonsg.com/app/img/back.gif border=0 alt="Previouse day"></a></td><td width=80% align=center><script language="javascript">ds('<?php echo $y; ?>', '<?php echo $m; ?>', '<?php echo $d; ?>');</script></td><td><b><? echo date("l", mktime(0,0,0,$m, $d, $y)); ?></b></td><td width=10% align=center><a href=fb.php?date=<?php echo $tomorrow; ?>><img src=http://www.axonsg.com/app/img/forward.gif border=0 alt="Next day"></a></td></tr></table></td></tr></form>
	<tr bgcolor="#ffffff"><th width=1% nowrap>&nbsp;</th>
	<?php
	$pc = 100/($sizes-1);
	foreach($tpa as $roomarray)
	{
		$roomid = $roomarray['roomid'];
		$roomname = $roomarray['roomname'];
		echo "<th width=$pc%>$roomname</th>";
	}
	?>
	</tr>
	<?php
	for ($i = 8; $i < 18; $i++)
	{
		showbooking($y, $m, $d, $i, $tpa, $staffid);
	}
	?>
	</table>
	<?php
}

function showbooking($y, $m, $d, $h, $tpa, $staffid)
{
	$k = mktime($h,0,0,$m,$d,$y);
	$j = mktime($h+1,0,0,$m,$d,$y);
	echo "<tr bgcolor=#ffffff><th width=1% nowrap align=right valign=top>$h - " . ($h+1) . "</th>";
	
	foreach($tpa as $rooms)
	{
		$roomid = $rooms['roomid'];
		echo "<td valign=top>";
		echo "<table cellspacing=0 cellpadding=0 border=0 width=100%><tr><td valign=top>";
		showthis($k, $j, $roomid, $staffid);
		echo "</td><td valign=top width=1% align=right style=\"font-size: 1px;\"><a href=/ onclick=\"window.open('addfb.php?y=$y&m=$m&d=$d&h=$h&r=$roomid', '', 'width=500, height=375, resizable, scrollbars');return false\"><img src=http://www.axonsg.com/app/img/ico_new.gif border=0 align=right title=\"Add booking\"></a></td></tr></table>";
		
		echo "</td>";
	}
	echo "</tr>";
}

function showthis($k, $j, $roomid, $staffid) //display the events in one cell
{
	$res = mysql_query("select recep from staff where staffid='$staffid'");
	$recep = mysql_result($res, 0, 0);
	
	$res = mysql_query("select roombook.bookid, staff.firstname, staff.lastname, roombook.sd, roombook.staffid, kt, jt from roombook, staff where roombook.roomid='$roomid' and kt<'$j' and jt>'$k' and roombook.staffid=staff.staffid order by kt asc, jt asc") or die (mysql_error());
	$i = 0;
	$date = $_GET["date"];
	if (! $date) $date = date("Y-m-d");
	
	while ($row = mysql_fetch_array($res))
	{
		$bookid = $row[0];
		$staffname = $row[1] . ", " . $row[2];
		$sd = $row[3];
		$si = $row[4];
		$st = date("G:i", $row[5]);
		$stt = date("G:i", $row[6]);
		
		if ($i > 0)
		{
			echo "<p>";
			//echo "<br>";
		}
		echo "$staffname($st - $stt): $sd";
		if ($si == $staffid || $recep == 1)
		{
			echo "<a href=/ onclick=\"window.open('modfb.php?bookid=$bookid', '', 'width=500, height=375, resizable, scrollbars');return false\"><img src=http://www.axonsg.com/app/img/ico_pencil.gif border=0 alt='Modify'></a>&nbsp;<a href='fb.php?date=$date&act=del&bookid=$bookid' onclick=\"return confsub()\"><img src=http://www.axonsg.com/app/img/ico_trash.gif border=0 alt='Delete'></a>";
		}
		$i++;
	}
}
?>