<?
/* 
   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("register_participants.inc.php");
 require_once("user.inc.php");
 user_auth_required('participant');
 $u=user_load($_SESSION['users_id']);
 
 //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=studentsStatus($u['registrations_id']);
 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") {
 	$message = saveProjectData($_POST,$u['registrations_id']);
	if($message == 'success'){
		echo notice(i18n("Project information successfully updated"));
	}else{
		echo error($message);
	}
 }

 //now lets find out their MAX grade, so we can pre-set the Age Category
 $q=mysql_query("SELECT MAX(grade) AS maxgrade FROM users WHERE registrations_id='".$u['registrations_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 WHERE conferences_id='".$conference['id']."' 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='".$u['registrations_id']."' AND conferences_id='".$conference['id']."'");
 //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,conferences_id) VALUES ('".$u['registrations_id']."','$projectcategories_id','".$conference['id']."')"); 
	//now query the one we just inserted
 	$q=mysql_query("SELECT * FROM projects WHERE registrations_id='".$u['registrations_id']."' AND conferences_id='".$conference['id']."'");
 }
 $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($u['registrations_id']);
if($newstatus!="complete")
{
	echo error(i18n("Project Information Incomplete"));
}
else if($newstatus=="complete")
{
	echo happy(i18n("Project Information Complete"));

}
?>

<script language="javascript" type="text/javascript">
function countwords()
{
	var wordmax=<?=$config['participant_project_summary_wordmax'];?>;
	var wordmin=<?=$config['participant_project_summary_wordmin'];?>;
	var summaryobj=document.getElementById('summary');
	var wordcountobj=document.getElementById('wordcount');
	var wordcountmessageobj=document.getElementById('wordcountmessage');

	var wordarray=summaryobj.value.replace(/\s+/g," ").split(" ");
	var wordcount=wordarray.length;

	if(wordcount>wordmax || wordcount<wordmin)
		wordcountmessageobj.className="incomplete";
	else
		wordcountmessageobj.className="complete";
		
	wordcountobj.innerHTML=wordcount;
}

$(document).ready(function() {
	countwords();
	});

</script>
<?


 echo "<form name=\"projectform\" method=\"post\" action=\"register_participants_project.php\">\n";
 echo "<input type=\"hidden\" name=\"action\" value=\"save\">\n";
 echo "<input type=\"hidden\" name=\"project_id\" value=\"$projectinfo->id\">\n";
 echo "<table>\n";
 echo "<tr><td>".i18n("Project Title").": </td><td><input type=\"text\" name=\"title\" size=\"50\" value=\"".htmlspecialchars($projectinfo->title)."\" />".REQUIREDFIELD;
 if($config['participant_project_title_charmax'])
 	echo i18n("(Max %1 characters)",array($config['participant_project_title_charmax']));
 echo "</td></tr>\n";
 if($config['participant_short_title_enable'] == 'yes') {
 	 echo "<tr><td>".i18n("Short Project Title").": </td><td><input type=\"text\" name=\"shorttitle\" size=\"50\" value=\"".htmlspecialchars($projectinfo->shorttitle)."\" />".REQUIREDFIELD;
	 if($config['participant_short_title_charmax'])
 		echo i18n("(Max %1 characters)",array($config['participant_short_title_charmax']));
	 echo "</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>";

//###### Feature Specific - filtering divisions by category
 if($config['filterdivisionbycategory']=="yes"){
	$q=mysql_query("SELECT projectdivisions.* FROM projectdivisions,projectcategoriesdivisions_link WHERE projectdivisions.id=projectdivisions_id AND projectcategories_id=".$projectcategories_id." AND projectdivisions.conferences_id='".$conference['id']."' AND projectcategoriesdivisions_link.conferences_id='".$conference['id']."' ORDER BY division"); 
	echo mysql_error();
//###
}else
 	$q=mysql_query("SELECT * FROM projectdivisions WHERE conferences_id='".$conference['id']."' 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>".REQUIREDFIELD;
 if($config['usedivisionselector']=="yes")
 {
 ?>
 	<script language="javascript" type="text/javascript">

	function openDivSelWindow()
	{
		divselwin=window.open('register_participants_project_divisionselector.php','divsel','width=500,height=220,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no')
		if(divselwin.opener==null) divselwin.opener=self;
		return false;
	}
	</script>
	<?

	echo "&nbsp;<a href=\"#\" onClick=\"openDivSelWindow(); return false;\">".i18n("Division Selector")."</a>";	
 }
 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>".REQUIREDFIELD;
 echo " ".i18n("This is the language you wish to be judged in!")."</td></tr>";

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

	if($config['participant_project_table']=="no")
	{
		//if we arent asking them if they want a table or not, then we set it to 'yes' assuming everyone will get a table
		echo " <input type=\"hidden\" name=\"req_table\" value=\"yes\" />";
	}
	else
	{
		echo "<tr>";
		echo " <td>".i18n("Table").REQUIREDFIELD."</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>";
	}

	if($config['participant_project_electricity']=="no")
	{
		//if we arent asking them if they want electricity or not, then we set it to 'yes' assuming everyone will get electricity
		echo " <input type=\"hidden\" name=\"req_electricity\" value=\"yes\" />";
	}
	else
	{
		echo "<tr>";
		echo " <td>".i18n("Electricity").REQUIREDFIELD."</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 onchange='countwords()' onkeypress='countwords()' cols=\"60\" rows=\"12\" id=\"summary\" name=\"summary\">".htmlspecialchars($projectinfo->summary)."</textarea>".REQUIREDFIELD."<br />";

 $summarywords=preg_split("/[\s,]+/",trim($projectinfo->summary));
 $summarywordcount=count($summarywords);
 if($summarywordcount>$config['participant_project_summary_wordmax'])
	 echo "<div id=\"wordcountmessage\" class=\"incomplete\">";
 else
	 echo "<div id=\"wordcountmessage\" class=\"complete\">";

 echo "<span id=\"wordcount\">$summarywordcount</span>/";
 echo i18n("%1 words maximum",array($config['participant_project_summary_wordmax']));
 if($config['participant_project_summary_wordmin'] > 0)
 {
 	echo i18n(", %1 words minimum", array($config['participant_project_summary_wordmin']));
 }
 echo "</div>";

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

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


 send_footer();
?>