<?
/* 
   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>
   Copyright (C) 2007 David Grant <dave@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_once("common.inc.php");
 require_once("user.inc.php");
 require_once("fair.inc.php");


 if($_SESSION['embed'] == true) {
 	$u = user_load($_SESSION['embed_edit_id']);
 } else {
	user_auth_required('fair');
	$u = user_load($_SESSION['users_id']);
 }

 $fair_type = array('feeder' => 'Feeder Fair', 'sfiab' => 'SFIAB Upstream', 'ysf' => 'YSF/CWSF Upstream');

 if($_POST['action']=="save")
 {
	$id = $_POST['id'];
	if(trim($id) == '') {
		$q = mysql_query("INSERT INTO fairs(`id`,`name`) VALUES('','new entry')");
		$id = mysql_insert_id();
	} else {
		$id = intval($id);
	}

	$name = mysql_escape_string(stripslashes($_POST['name']));
	$abbrv = mysql_escape_string(stripslashes($_POST['abbrv']));
	$url = mysql_escape_string($_POST['url']);
	$type = array_key_exists($_POST['type'], $fair_type) ? $_POST['type'] : '';
	$username = mysql_escape_string(stripslashes($_POST['username']));
	$password = mysql_escape_string(stripslashes($_POST['password']));

	$q = mysql_query("UPDATE fairs SET `name`='$name',
				`abbrv`='$abbrv', `url`='$url',
				`type`='$type' , `username`='$username',
				`password`='$password'
				WHERE id=$id");

	$u['fairs_id'] = $id;
	user_save($u);
	message_push(notice(i18n("Fair Informaiton successfully updated")));
 }

/* update overall status */
fair_status_update($u);

if($_SESSION['embed'] != true) {
	//output the current status
	$newstatus=fair_status_info($u);
	if($newstatus!='complete')
		message_push(error(i18n("Fair Information Incomplete")));
	else
		message_push(happy(i18n("Fair Information Complete")));
}

if($_SESSION['embed'] == true) {
 	echo "<br />";
	display_messages();
	echo "<h3>".i18n('Fair Information')."</h3>";
 	echo "<br />";
} else {
	//send the header
	send_header("Fair Information", 
 		array("Science Fair Main" => "fair_main.php")
		);
}
 /* Load the fair info */
 $q = mysql_query("SELECT * FROM fairs WHERE id={$u['fairs_id']}");
 if(mysql_num_rows($q)) {
	 $f = mysql_fetch_assoc($q);
 } else {
	 $f = array();
 }

 $s = ($_SESSION['embed'] == true) ? $_SESSION['embed_submit_url'] : 'fair_info.php';
 echo "<form name=\"personalform\" method=\"post\" action=\"$s\">\n";
 echo "<input type=\"hidden\" name=\"action\" value=\"save\" />\n";
 echo "<input type=\"hidden\" name=\"id\" value=\"{$f['id']}\" />\n";
 echo "<table>\n";
 echo '<tr><td>'.i18n('Fair Name').'</td><td>';
 echo "<input type=\"text\" name=\"name\" value=\"{$f['name']}\" size=\"40\" />";
 echo '<tr><td>'.i18n('Fair Abbreviation').'</td><td>';
 echo "<input type=\"text\" name=\"abbrv\" value=\"{$f['abbrv']}\" size=\"7\" />";
 echo '<tr><td>'.i18n('Fair Type').'</td><td>';
 echo "<select name=\"type\">";
 foreach($fair_type as $k=>$o) {
	 $s = ($f['type'] == $k) ? 'selected="selected"' : '';
	 echo "<option value=\"$k\" $s >".i18n($o)."</option>";
 }
 echo "</select></td></tr>";
 echo '<tr><td>'.i18n('URL').'</td><td>';
 if($f['url'] == '') $f['url'] = 'http://';
 echo "<input type=\"text\" name=\"url\" value=\"{$f['url']}\" size=\"40\" />";
 echo '</td></tr>';
 echo '<tr><td>'.i18n('Username').'</td><td>';
 echo "<input type=\"text\" name=\"username\" value=\"{$f['username']}\" size=\"20\" />";
 echo '</td></tr>';
 echo '<tr><td>'.i18n('Password').'</td><td>';
 echo "<input type=\"text\" name=\"password\" value=\"{$f['password']}\" size=\"15\" />";
 echo '</td></tr>';

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

 echo "<br />";

 if($_SESSION['embed'] != true) send_footer();
?>