<?
/* 
   This file is part of the 'Science Fair In A Box' project
   SFIAB Website: http://www.sfiab.ca

   Copyright (C) 2005 Sci-Tech Ontario Inc <info@scitechontario.org>
   Copyright (C) 2005 James Grant <james@lightbox.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation, version 2.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/
?>
<?
 require("common.inc.php");
 require_once("schedule.inc.php");

if($_SESSION['schoolid'] && $_SESSION['schoolaccesscode']){
	// we're logged in, make sure the school is good
	$q=mysql_query("SELECT * FROM schools WHERE id='".$_SESSION['schoolid']."' AND accesscode='".$_SESSION['schoolaccesscode']."'");
	echo mysql_error();
	$school=mysql_fetch_object($q);
	if($school) {
		switch($_GET['action']) {
			case "load":
					$id=intval($_GET['id']);
					$q=mysql_query("SELECT schedule.*,
							events.eventtype,
							events.website,
							events.summary,
							events.name,
							locations.name AS location
							FROM schedule 
							JOIN events ON schedule.events_id=events.id
							JOIN locations ON schedule.locations_id=locations.id
							WHERE schedule.conferences_id='{$conference['id']}' 
							AND schedule.id='{$id}'");
					echo mysql_error();
					$r=mysql_fetch_object($q);
					if($r->title!=$r->name) {
						$name="($r->name)";
					}
					echo "<h3>$r->title $name</h3>";

				switch($_GET['tab']) {
					case "summary":
					echo "<table width=\"95%\" class=\"tableview\">";
					echo "<tr>";
					echo "<th>".i18n("Time")."</th>\n";
					echo "<th>".i18n("Duration")."</th>\n";
					echo "<th>".i18n("Location")."</th>\n";
					echo "</tr>\n";
					echo "<tr>";
					echo "<td>".format_time(strtotime($r->hour.":".$r->minute))."</td>\n";
					echo "<td>$r->duration minutes</td>\n";
					echo "<td>$r->location</td>\n";
					echo "</tr></table>\n";
					echo "<br />";
					echo "<h4>".i18n("Summary")."</h4>\n";
					echo nl2br(trim($r->summary));
					echo "<br />\n";
					echo "<br />\n";

					if($r->website) {
						echo "<a target=\"_blank\" href=\"$r->website\">".i18n("Click here for the full event description")."</a> <br />\n";
					}

					break;
					case "register":
						$regteams=getNumRegistrations($r->id);
						echo i18n("Current capacity: %1 of %2 Teams",array($regteams,$r->somaxteams));
						echo "<br />";
						echo "<br />";
						echo i18n("Your school's teams").": <br />";
						echo "<table class=\"summarytable\" style=\"width: 90%;\">";
						echo "<tr>";
						echo " <th>".i18n("Team Name")."</th>";
						echo " <th>".i18n("Current Status")."</th>";
						echo " <th>".i18n("Action")."</th>";
						echo "</tr>";

						$query="SELECT * FROM so_teams WHERE schools_id='{$school->id}' AND conferences_id='{$conference['id']}'";
						$tq=mysql_query($query);

						echo mysql_error();
						while($team=mysql_fetch_object($tq)) {
							$status=getTeamEventStatus($team->id,$r->id);
							if($status=="registered") {
								$cl="class=\"happy\"";
								$statusmsg=i18n("Registered");
								$buttonmsg=i18n("Un-Register");
								$action="unregister";
							} else {
								$cl="class=\"error\"";
								$statusmsg=i18n("Not Registered");
								$buttonmsg=i18n("Register");
								$action="register";
							}
							echo "<tr><td>$team->name</td>";
							echo "<td $cl>$statusmsg</td>";
							echo "<td style=\"text-align: center;\"><input type=\"button\" value=\"$buttonmsg\" onclick=\"return registerSchedule($team->id,$r->id,'$action')\"></td>";
							echo "</tr>\n";
						}
						echo "</table>\n";
					break;
				}

			break;
			case "register":
				$teamid=intval($_POST['teamid']);
				$scheduleid=intval($_POST['scheduleid']);
				$conferenceid=$conference['id'];

					$q=mysql_query("SELECT schedule.*,
							events.eventtype,
							events.website,
							events.summary,
							events.name,
							locations.name AS location
							FROM schedule 
							JOIN events ON schedule.events_id=events.id
							JOIN locations ON schedule.locations_id=locations.id
							WHERE schedule.conferences_id='{$conference['id']}' 
							AND schedule.id='{$scheduleid}'");
					$schedule=mysql_fetch_object($q);
					echo mysql_error();


				switch($_POST['regaction']) {
					case "register": 
					$q=mysql_query("SELECT * FROM schedule_registrations WHERE so_teams_id=$teamid AND schedule_id=$scheduleid AND conferences_id=$conferenceid");
					if(mysql_num_rows($q)) {
						error_("This team is already registered for this scheduled event");
					}
					else {
						$regteams=getNumRegistrations($scheduleid);
						if($regteams<$schedule->somaxteams) {
							mysql_query("INSERT INTO schedule_registrations (schedule_id, so_teams_id, conferences_id, dt) VALUES ('$scheduleid','$teamid','$conferenceid',NOW())");
							happy_("Successfully registered the team for this scheduled event");
						}
						else {
							error_("This event has reached its capacity and can no longer be registered for");
						}
					}
					break;

					case "unregister":
						$q=mysql_query("SELECT * FROM schedule_registrations WHERE so_teams_id=$teamid AND schedule_id=$scheduleid AND conferences_id=$conferenceid");
						if(mysql_num_rows($q)) {
							mysql_query("DELETE FROM schedule_registrations WHERE so_teams_id='$teamid' AND schedule_id='$scheduleid' AND conferences_id='$conferenceid'");
							happy_("Successfully unregistered the team from this scheduled event");
						}
						else {
							error_("This team is not registered for this scheduled event, and thus, cannot be unregistered");
						}
					break;
				}
			break;
		}


	}
	else {
		echo "Invalid school";
	}
}
else {
	echo "Invalid school id or access code";
}

?>
