<?
 require("common.inc.php");
 include "register_participants.inc.php";
 
 //authenticate based on email address and registration number from the SESSION
 if(!$_SESSION['email'])
 {
 	header("Location: register_participants.php");
	exit;
 }
 if(!$_SESSION['registration_number'])
 {
 	header("Location: register_participants.php");
	exit;
 }

 $q=mysql_query("SELECT registrations.id AS regid, students.id AS studentid, students.firstname FROM registrations,students ".
 	"WHERE students.email='".$_SESSION['email']."' ".
	"AND registrations.num='".$_SESSION['registration_number']."' ". 
	"AND registrations.id='".$_SESSION['registration_id']."' ".
	"AND students.registrations_id=registrations.id ".
	"AND registrations.year=".$config['FAIRYEAR']." ".
	"AND students.year=".$config['FAIRYEAR']);
echo mysql_error();

 if(mysql_num_rows($q)==0)
 {
 	header("Location: register_participants.php");
	exit;
 
 }
 $authinfo=mysql_fetch_object($q);

 //send the header
 send_header("Participant Registration - Project Information");

 echo "<a href=\"register_participants_main.php\">&lt;&lt; ".i18n("Back to Participant Registration Summary")."</a><br />";
 echo "<br />";

 $studentstatus=studentStatus();
 if($studentstatus!="complete")
 {
	echo error(i18n("Please complete the <a href=\"register_participants_students.php\">Student Information Page</a> first"));
	send_footer();
	exit;
 }


 if($_POST['action']=="save")
 {
 	//first, lets make sure this project really does belong to them
	$q=mysql_query("SELECT * FROM projects WHERE id='".$_POST['id']."' AND registrations_id='".$_SESSION['registration_id']."' AND year='".$config['FAIRYEAR']."'");
	if(mysql_num_rows($q)==1)
	{
		mysql_query("UPDATE projects SET ".
				"title='".mysql_escape_string(stripslashes($_POST['title']))."', ".
				"projectdivisions_id='".$_POST['projectdivisions_id']."', ".
				"language='".mysql_escape_string(stripslashes($_POST['language']))."', ".
				"req_table='".mysql_escape_string(stripslashes($_POST['req_table']))."', ".
				"req_electricity='".mysql_escape_string(stripslashes($_POST['req_electricity']))."', ".
				"req_special='".mysql_escape_string(stripslashes($_POST['req_special']))."', ".
				"summary='".mysql_escape_string(stripslashes($_POST['summary']))."' ".
				"WHERE id='".$_POST['id']."'");
				echo mysql_error();
		echo notice(i18n("Project information successfully updated"));
	}
	else
	{
		echo error(i18n("Invalid project to update"));
	}
 }


 //now lets find out their MAX grade, so we can pre-set the Age Category
 $q=mysql_query("SELECT MAX(grade) AS maxgrade FROM students WHERE registrations_id='".$_SESSION['registration_id']."'");
 $gradeinfo=mysql_fetch_object($q);

 //now lets grab all the age categories, so we can choose one based on the max grade
 $q=mysql_query("SELECT * FROM projectcategories ORDER BY id");
 while($r=mysql_fetch_object($q))
 {
 	//save these in an array, just incase we need them later (FIXME: remove this array if we dont need it)
	$agecategories[$r->id]['category']=$r->category;
	$agecategories[$r->id]['mingrade']=$r->mingrade;
	$agecategories[$r->id]['maxgrade']=$r->maxgrade;

	if($gradeinfo->maxgrade >= $r->mingrade && $gradeinfo->maxgrade <= $r->maxgrade)
	{
		$projectcategories_id=$r->id;
	}
 }
 //now select their project info
 $q=mysql_query("SELECT * FROM projects WHERE registrations_id='".$_SESSION['registration_id']."' AND year='".$config['FAIRYEAR']."'");
 //check if it exists, if we didnt find any record, lets insert one
 if(mysql_num_rows($q)==0)
 {
 	mysql_query("INSERT INTO projects (registrations_id,projectcategories_id,year) VALUES ('".$_SESSION['registration_id']."','$projectcategories_id','".$config['FAIRYEAR']."')"); 
	//now query the one we just inserted
 	$q=mysql_query("SELECT * FROM projects WHERE registrations_id='".$_SESSION['registration_id']."' AND year='".$config['FAIRYEAR']."'");
 }
 $projectinfo=mysql_fetch_object($q);

 //make sure that if they changed their grade on the student page, we update their projectcategories_id accordingly
 if($projectcategories_id && $projectinfo->projectcategories_id!=$projectcategories_id)
 {
 	echo notice(i18n("Age category changed, updating to %1",array($agecategories[$projectcategories_id]['category'])));
	mysql_query("UPDATE projects SET projectcategories_id='$projectcategories_id' WHERE id='$projectinfo->id'");
 }



//output the current status
$newstatus=projectStatus();
if($newstatus!="complete")
{
	echo error(i18n("Project Information Incomplete"));
}
else if($newstatus=="complete")
{
	echo happy(i18n("Project Information Complete"));

}



 echo "<form method=\"post\" action=\"register_participants_project.php\">\n";
 echo "<input type=\"hidden\" name=\"action\" value=\"save\">\n";
 echo "<input type=\"hidden\" name=\"id\" value=\"$projectinfo->id\">\n";
 echo "<table>\n";
 echo "<tr><td>".i18n("Project Title").": </td><td><input type=\"text\" name=\"title\" size=\"50\" value=\"$projectinfo->title\" /></td></tr>\n";
 echo "<tr><td>".i18n("Age Category").": </td><td>";
	echo i18n($agecategories[$projectcategories_id]['category']);
	echo " (".i18n("Grades %1-%2",array($agecategories[$projectcategories_id]['mingrade'],$agecategories[$projectcategories_id]['maxgrade'])).")";
 echo "</td></tr>";
 echo "<tr><td>".i18n("Division").": </td><td>";
 $q=mysql_query("SELECT * FROM projectdivisions ORDER BY division");
 echo "<select name=\"projectdivisions_id\">";
 echo "<option value=\"\">".i18n("Select a division")."</option>\n";
 while($r=mysql_fetch_object($q))
 {
 	if($r->id == $projectinfo->projectdivisions_id) $sel="selected=\"selected\""; else $sel="";
	echo "<option $sel value=\"$r->id\">".htmlspecialchars(i18n($r->division))."</option>\n";
	
 }
 echo "</select>";
 echo "</td></tr>";

 echo "<tr><td>".i18n("Language").": </td><td>";
 echo "<select name=\"language\">\n";

 if($projectinfo->language)
 	$currentlang=$projectinfo->language;
 else
	$currentlang=$_SESSION['lang'];

 foreach($config['languages'] AS $key=>$val)
 { 
	if($currentlang==$key) $selected="selected=\"selected\""; else $selected="";

	echo "<option $selected value=\"$key\">$val</option>";
 }
 echo "</select>";
 echo "</td></tr>";

 echo "<tr><td>".i18n("Requirements").": </td><td>";
	echo "<table>";

	echo "<tr>";
	echo " <td>".i18n("Table")."</td>";
	if($projectinfo->req_table=="yes") $check="checked=\"checked\""; else $check="";
	echo " <td><input $check type=\"radio\" name=\"req_table\" value=\"yes\" />Yes</td>";
	echo " <td width=\"20\">&nbsp;</td>";
	if($projectinfo->req_table=="no") $check="checked=\"checked\""; else $check="";
	echo " <td><input $check type=\"radio\" name=\"req_table\" value=\"no\" />No</td>";
	echo "</tr>";

	echo "<tr>";
	echo " <td>".i18n("Electricity")."</td>";
	if($projectinfo->req_electricity=="yes") $check="checked=\"checked\""; else $check="";
	echo " <td><input $check type=\"radio\" name=\"req_electricity\" value=\"yes\" />Yes</td>";
	echo " <td width=\"20\">&nbsp;</td>";
	if($projectinfo->req_electricity=="no") $check="checked=\"checked\""; else $check="";
	echo " <td><input $check type=\"radio\" name=\"req_electricity\" value=\"no\" />No</td>";
	echo "</tr>";

	echo "<tr>";
	echo " <td>".i18n("Special")."</td>";
	echo " <td colspan=\"3\"><input type=\"text\" name=\"req_special\" value=\"$projectinfo->req_special\" /></td>";
	echo "</tr>";

	echo "</table>";

 echo "</td></tr>";

 echo "<tr><td>".i18n("Summary").": </td><td><textarea cols=\"60\" rows=\"12\" name=\"summary\">$projectinfo->summary</textarea><br />";
 echo i18n("100 words maximum");
 echo "</td></tr>";

 echo "</table>";
 echo "<input type=\"submit\" value=\"".i18n("Save Project Information")."\" />\n";
 echo "</form>";


 send_footer();
?>