<?
/* 
   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.
*/
?>
<?

//////echo phpinfo();
header("Content-Type: text/html; charset=utf8"); 
include_once("helper.inc.php");
//set error reporting to not show notices, for some reason some people's installation dont set this by default
//so we will set it in the code instead just to make sure
error_reporting(E_ALL);
#error_reporting( E_ALL ^ E_WARNING ); 
#error_reporting( E_ALL ^ E_WARNING ^ E_NOTICE ^ E_DEPRECATED ); 

define('REQUIREDFIELD','<span class="requiredfield">*</span>');

//figure out the directory to prepend to directoroy names, depending on if we are in a subdirectory or not
// Dennis Fix so works in windows servers.
// Windows based servers use '\' in directories. This code works for WIN servers and or *nix servers.
if(substr(getcwd(),-6)=="/admin" || substr(getcwd(),-6)=="\\admin")
	$prependdir="../";
else if(substr(getcwd(),-6)=="/super" || substr(getcwd(),-6)=="\\super")
	$prependdir="../";
else if(substr(getcwd(),-7)=="/config" || substr(getcwd(),-7)=="\\config")
	$prependdir="../";
else if(substr(getcwd(),-3)=="/db" || substr(getcwd(),-3)=="\\db")
	$prependdir="../";
else if(substr(getcwd(),-8)=="/scripts" || substr(getcwd(),-8)=="\\scripts")
	$prependdir="../";
else if(substr(getcwd(),-4)=="/app" || substr(getcwd(),-4)=="\\app")
	$prependdir="../";
else
	$prependdir="";

$sfiabversion=@file($prependdir."version.txt");
$config['version']=trim($sfiabversion[0]);

//make sure the data subdirectory is writable, if its not, then we're screwed, so make sure it is!
if(!is_writable($prependdir."data"))
{
	echo "<html><head><title>SFIAB ERROR</title></head><body>";
	echo "<h1>Science Fair In A Box - ERROR</h1>";
	echo "data/ subdirectory is not writable by the web server";
	echo "<br>";
	echo "<h2>Details</h2>";
	echo "The data/ subdirectory is used to store files uploaded through the SFIAB software.  The web server must have write access to this directory in order to function properly.  Please contact your system administrator (if you are the system administrator, chown/chmod the data directory appropriately).";
	echo "<br>";
	echo "</body></html>";
	exit;
}

if(file_exists($prependdir."data/config.inc.php"))
{
	require_once($prependdir."data/config.inc.php");
}
else
{
	echo "<html><head><title>SFIAB</title></head><body>";
	echo "<h1>Science Fair In A Box - Installation</h1>";
	echo "It looks like this is a new installation of SFIAB, and the database has not yet been configured.  Please choose from the following options: <br />";
	echo "<br />";
	echo "<a href=\"install.php\">Proceed with Fresh SFIAB Installation</a>";
	echo "<br />";
	echo "</body></html>";
	exit;
}

$dsn = "mysql:host=db;dbname=$DBNAME;charset=utf8mb4";

$pdo = new PDO($dsn,$DBUSER,$DBPASS);

if(!$pdo)
{
	echo "<html><head><title>SFIAB ERROR</title></head><body>";
	echo "<h1>Science Fair In A Box - ERROR</h1>";
	echo "Cannot connect to database!";
	echo "</body></html>";
	exit;
}

//find out the fair year and any other 'year=0' configuration parameters (things that dont change as the years go on)


//we might get an error if installation step 2 is not done (ie, the config table doesnt even exist)

//if we have 0 (<1) then install2 is not done, which would get caught above, 
//if we have 1 (<2) then insatll3 is not done (no entries for FAIRYEAR and SFIABDIRECTORY)
$q = $pdo->prepare("SELECT * FROM config WHERE year='0'");
$q->execute();

if($pdo->errorInfo()[0] != '00000')
{
	echo "<html><head><title>SFIAB ERROR</title></head><body>";
	echo "<h1>Science Fair In A Box - ERROR</h1>";
	echo "SFIAB installation is not complete.  Please go to <A href=\"install2.php\">Installer Step 2</a> to complete the installation process";
	echo "<br>";
	echo "</body></html>";
	exit;
}


if($q->rowCount()<2)
{
	echo "<html><head><title>SFIAB ERROR</title></head><body>";
	echo "<h1>Science Fair In A Box - ERROR</h1>";
	echo "SFIAB installation is not complete.  Please go to <A href=\"install3.php\">Installer Step 3</a> to complete the installation process";
	echo "<br>";
	echo "</body></html>";
	exit;

}
else
{	
	while($r=$q->fetch())
	{
		
		$config[$r['var']]=$r['val'];
	}
}

$dbdbversion=$config['DBVERSION'];
$dbcodeversion=@file($prependdir."db/db.code.version.txt");
$dbcodeversion=trim($dbcodeversion[0]);


if(!$dbdbversion)
{
	echo "<html><head><title>SFIAB ERROR</title></head><body>";
	echo "<h1>Science Fair In A Box - ERROR</h1>";
	echo "SFIAB installation is not complete.  Please go to <A href=\"install2.php\">Installer Step 2</a> to complete the installation process";
	echo "<br>";
	echo "</body></html>";
	exit;
}

if($dbcodeversion!=$dbdbversion)
{
	echo "<html><head><title>SFIAB ERROR</title></head><body>";
	echo "<h1>Science Fair In A Box - ERROR</h1>";
	echo "SFIAB database and code are mismatched";
	echo "<br>";
	echo "Please run the db_update.php script in order to update";
	echo "<br>";
	echo "your database to the same version as the code";
	echo "<br>";
	echo "<br>";
	echo "<br>";
	echo "<h2>Details</h2>";
	echo "Current SFIAB codebase requires DB version: ".$dbcodeversion;
	echo "<br>";
	echo "Current SFIAB database is detected as version: ".$dbdbversion;
	echo "<br>";
	echo "</body></html>";
	exit;
}

//now pull the rest of the configuration
$q = $pdo->prepare("SELECT * FROM config WHERE year='".$config['FAIRYEAR']."'");
$q->execute();
while($r=$q->fetch())
{
	$config[$r['var']]=$r['val'];
}

//now pull the dates
$q = $pdo->prepare("SELECT * FROM dates WHERE year='".$config['FAIRYEAR']."'");
$q->execute();
while($r=$q->fetch())
{
	$config['dates'][$r['name']]=$r['date'];
}

//and now pull the theme
require_once("theme/{$config['theme']}/theme.php");
require_once("theme/{$config['theme_icons']}/icons.php");

require_once("committee.inc.php");

if($config['SFIABDIRECTORY'] == '') {
	session_name("SFIABSESSID");
	session_set_cookie_params(0,'/');
} else {
	session_name("SFIABSESSID".preg_replace("/[^A-Za-z]/","_",$config['SFIABDIRECTORY']));
	session_set_cookie_params(0,$config['SFIABDIRECTORY']);
}

session_start();

//detect the browser first, so we know what icons to use - we store this in the config array as well
//even though its not configurable by the fair
if(stristr($_SERVER['HTTP_USER_AGENT'],"MSIE"))
	$config['icon_extension']="gif";
else
	$config['icon_extension']="png";



//now get the languages, and make sure we have at least one active language

$q=$pdo->prepare("SELECT * FROM languages WHERE active='Y' ORDER BY langname");
$q->execute();
if($q->rowCount()==0)
{
	echo "No active languages defined, defaulting to English";
	$config['languages']['en']="English";
}
else
{	while($r=$q->fetch())
	{
		$config['languages'][$r['lang']]=$r['langname'];
	}
}
//now if no language has been set yet, lets set it to the default language

if(!$_SESSION['lang'])
{
	//first try the default language, if that doesnt work, use "en"
	if($config['default_language'])
		$_SESSION['lang']=$config['default_language'];
	else
		$_SESSION['lang']="en";
}

//only allow debug to get set if we're using a development version (odd numbered ending)
if(substr($config['version'], -1) % 2 != 0)
	if(get_value_from_array($_GET, 'debug')) $_SESSION['debug']=$_GET['debug'];

//if the user has switched languages, go ahead and switch the session variable
if(array_key_exists('switchlanguage', $_GET))
{
	//first, make sure its a valid language:
	if($config['languages'][$_GET['switchlanguage']])
	{
		$_SESSION['lang']=$_GET['switchlanguage'];
	}
	else
	{
		//invalid language, dont do anything
	}
}

function i18n($str,$args=array(),$argsdesc=array(),$forcelang="")
{	global $pdo;
	if(!$str)
		return "";

	if($forcelang)
	{
		$savelang=$_SESSION['lang'];
		$_SESSION['lang']=$forcelang;
	}

	if($_SESSION['lang'])
	{
		if($_SESSION['lang']=="en")
		{
			for($x=1;$x<=count($args);$x++)
			{
				$str=str_replace("%$x",$args[$x-1],$str);
			}
			if($forcelang) $_SESSION['lang']=$savelang;
			return $str;
		}
		else
		{
			
			$q = $pdo->prepare("SELECT * FROM translations WHERE lang='".$_SESSION['lang']."' AND strmd5='".md5($str)."'");
			$q->execute();
			if($r = $q->fetch())
		
			{
				if($r["val"])
				{
					$ret=$r["val"];

					for($x=1;$x<=count($args);$x++)
					{
						$ret=str_replace("%$x",$args[$x-1],$ret);
					}
					if($forcelang) $_SESSION['lang']=$savelang;
					return $ret;
				}
				else
				{
					for($x=1;$x<=count($args);$x++)
					{
						$str=str_replace("%$x",$args[$x-1],$str);
					}
					if($forcelang) $_SESSION['lang']=$savelang;
					return "{{".$str."}}";
				}
					
			}
			else
			{
				if(count($argsdesc))
				{
					$argsdescstring="";
					$n=1;
					foreach($argsdesc AS $ad)
					{
						$argsdescstring.="%$n=$ad, ";
						$n++;
					}
					$argsdescstring=substr($argsdescstring,0,-2);
					$argsdescstring=pdo->quote($argsdescstring)."'";
				}
				else
					$argsdescstring="null";

				$stmt = $pdo->prepare("INSERT INTO translations (lang,strmd5,str,argsdesc) VALUES (?,?,?,?)");
				$stmt->execute([$_SESSION['lang'], md5($str), $pdo->quote($str), $argsdescstring]);
				for($x=1;$x<=count($args);$x++)
				{
					$str=str_replace("%$x",$args[$x-1],$str);
				}
				if($forcelang) $_SESSION['lang']=$savelang;
				return "{{".$str."}}";
			}
		}
	}
	else
	{
		//no language set, assume english
		if($forcelang) $_SESSION['lang']=$savelang;
		return $str;
	}
}

function error($str,$type="normal")
{
	if($type=="normal")
		return "<div class=\"error\">$str</div><br />";
	else if($type=="inline")
		return "<span class=\"error\">$str</span><br />";

}

function notice($str,$type="normal")
{
	if($type=="normal")
		return "<div class=\"notice\">$str</div><br />";
	else if($type=="inline")
		return "<span class=\"notice\">$str</span><br />";
}

function happy($str,$type="normal")
{
	if($type=="normal")
		return "<div class=\"happy\">$str</div><br />";
	else if($type=="inline")
		return "<span class=\"happy\">$str</span><br />";
}

function display_messages()
{
	/* Dump any messages in the queue */
	if(is_array($_SESSION['messages'])) {
		foreach($_SESSION['messages'] as $m) echo $m;
	}
	$_SESSION['messages'] = array();
}

$HEADER_SENT=false;
function send_header($title="", $nav=null, $icon=null, $titletranslated=false)
{
	global $HEADER_SENT;
	global $config;
	global $prependdir;

	//do this so we can use send_header() a little more loosly and not worry about it being sent more than once.
	if($HEADER_SENT) return;
	else $HEADER_SENT=true;
?>
<!DOCTYPE html> 
<html>
<head>
<meta charset="utf-8" />
<title><? //if($title && !$titletranslated) echo i18n($title); else if($title) echo $title; else echo i18n($config['fairname']); ?></title>
<link rel="stylesheet" href="<?=$config['SFIABDIRECTORY']?>/theme/<?=$config['theme']?>/jquery-ui-1.7.2.custom.css" type="text/css" media="all" />
<link rel="stylesheet" href="<?=$config['SFIABDIRECTORY']?>/theme/<?=$config['theme']?>/sfiab.css" type="text/css" media="all" />
<link rel="stylesheet" href="<?=$config['SFIABDIRECTORY']?>/tableeditor.css" type="text/css" media="all" />
</head>
<body>
<!-- <? if($title && !$titletranslated) echo i18n($title); else if($title) echo $title; else echo i18n($config['fairname']); ?> -->
<script type="text/javascript" src="<?=$config['SFIABDIRECTORY']?>/js/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="<?=$config['SFIABDIRECTORY']?>/js/jqueryui/1.7.2/jquery-ui.min.js"></script>
<script type="text/javascript" src="<?=$config['SFIABDIRECTORY']?>/js/sfiab.js"></script>
<script type="text/javascript" src="<?=$config['SFIABDIRECTORY']?>/js/tablesorter/jquery.tablesorter.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
        $('.tableview').tablesorter();
	});
</script>
<?
//if we're under /admin or /config we also want the translation editor
if(substr(getcwd(),-6)=="/admin" || substr(getcwd(),-7)=="/config" || substr(getcwd(),-6)=="\\admin" || substr(getcwd(),-7)=="\\config")
	require_once("../translationseditor.inc.php");
?>

<div id="notice_area" class="notice_area"></div>
<div id="header">
<?
if(file_exists($prependdir."data/logo-100.gif"))
	echo "<img align=\"left\" height=\"50\" src=\"".$config['SFIABDIRECTORY']."/data/logo-100.gif\">";

echo "<h1>".i18n($config['fairname'])."</h1>";
echo "<div align=\"right\" style=\"font-size: 0.75em;\">";
if(isset($_SESSION['users_type'])) {
	$types = array('volunteer' => 'Volunteer', 'judge' => 'Judge', 
		'student'=>'Participant','committee'=>'Committee Member',
		'fair'=>'Science Fair');
	if($_SESSION['users_type'] != false) {
		echo i18n($types[$_SESSION['users_type']]);
	}
	echo " {$_SESSION['email']}: ";
	if($_SESSION['multirole'] == true) {
		echo "<a href=\"{$config['SFIABDIRECTORY']}/user_multirole.php\">[".i18n('Switch Roles')."]</a> ";
	}
	echo "<a href=\"{$config['SFIABDIRECTORY']}/user_login.php?action=logout\">[".i18n("Logout")."]</a>";
	
} else if(isset($_SESSION['email'])) {
	/* Backwards compatible login settings */
	if(isset($_SESSION['registration_id'])) {
		echo i18n('Participant');
		echo " {$_SESSION['email']}: ";
		echo "<a href=\"{$config['SFIABDIRECTORY']}/register_participants.php?action=logout\">[".i18n("Logout")."]</a>";
	} else {
		echo "&nbsp;";
	}

} else {
	echo i18n('Not Logged In');
}
echo "</div>";
?>
<hr />
</div>
<table cellpadding="5" width="100%">
<tr><td width="175">
<?
	global $pdo;
	//if the date is greater than the date/time that the confirmed participants gets posted,
	//then we will show the registration confirmation page as a link in the menu,
	$registrationconfirmationlink="";

	//only display it if a date is set to begin with.
	if($config['dates']['postparticipants'] && $config['dates']['postparticipants']!="0000-00-00 00:00:00")
	{
		$q= $pdo->prepare("SELECT (NOW()>'".$config['dates']['regclose']."') AS test");
		$q->execute();
		$r=$q->fetch(PDO::FETCH_OBJ);
		if($r->test==1)
		{
			$registrationconfirmationlink="<li><a href=\"".$config['SFIABDIRECTORY']."/confirmed_participants.php\">".i18n("Confirmed Participants")."</a></li>";
		}
	}
?>

<div id="left">
<?
if(is_array($nav)) {
		$navkeys=array_keys($nav);
		if (isset($navkeys[2]) && $navkeys[2] == "Fundraising") {
			echo "<ul class=\"mainnav\">\n";
			echo "<li><h4 style=\"text-align: center;\">".i18n("Fundraising")."</h4></li>\n";
			echo "<li><a href=\"{$config['SFIABDIRECTORY']}/admin/fundraising.php\">".i18n("Fundraising Dashboard").'</a></li>';
			echo "<li><a href=\"{$config['SFIABDIRECTORY']}/admin/fundraising_setup.php\">".i18n("Fundraising Setup").'</a></li>';
			echo "<li><a href=\"{$config['SFIABDIRECTORY']}/admin/fundraising_campaigns.php\">".i18n("Manage Appeals").'</a></li>';
			echo "<li><a href=\"{$config['SFIABDIRECTORY']}/admin/donors.php\">".i18n("Manage Donors/Sponsors").'</a></li>';
			echo "<li><a href=\"{$config['SFIABDIRECTORY']}/admin/fundraising_reports.php\">".i18n("Fundraising Reports").'</a></li>';
			echo "</ul><br />\n";
		}		
}
?>
<ul class="mainnav">
<?
 echo "<li><a href=\"{$config['SFIABDIRECTORY']}/index.php\">".i18n("Home Page").'</a></li>';
 echo "<li><a href=\"{$config['SFIABDIRECTORY']}/important_dates.php\">".i18n("Important Dates").'</a></li>';

 echo $registrationconfirmationlink;
 /*
 echo "<li><a href=\"{$config['SFIABDIRECTORY']}/register_participants.php\">".i18n("Participant Registration").'</a></li>';
 echo "<li><a href=\"{$config['SFIABDIRECTORY']}/user_login.php?type=judge\">".i18n("Judges Registration").'</a></li>';
 if($config['volunteer_enable'] == 'yes') {
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/user_login.php?type=volunteer\">".i18n("Volunteer Registration").'</a></li>';
 }
 */
 echo "<li><a href=\"{$config['SFIABDIRECTORY']}/committees.php\">".i18n("Committee").'</a></li>';
 echo "<li><a href=\"{$config['SFIABDIRECTORY']}/winners.php\">".i18n("Winners").'</a></li>';
 echo '</ul>';
?>
<br />
<ul class="mainnav">
<?
if(get_value_from_session('users_type') == 'committee') {
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/user_personal.php\">".i18n("My Profile").'</a></li>';
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/committee_main.php\">".i18n("Committee Home").'</a></li>';
	if(committee_auth_has_access("admin")){ 
		echo "<li><a href=\"{$config['SFIABDIRECTORY']}/admin/\">".i18n("Fair Administration").'</a></li>';
	} 
	if(committee_auth_has_access("config")){
		echo "<li><a href=\"{$config['SFIABDIRECTORY']}/config/\">".i18n("Configuration").'</a></li>';
	} 
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/user_login.php?action=logout\">".i18n("Logout").'</a></li>';
} else if(get_value_from_session('users_type') == "judge") {
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/user_personal.php\">".i18n("My Profile").'</a></li>';
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/judge_main.php\">".i18n("Judge Home").'</a></li>';
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/user_login.php?action=logout\">".i18n("Logout").'</a></li>';
} else if(get_value_from_session('users_type') == "volunteer") {
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/user_personal.php\">".i18n("My Profile").'</a></li>';
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/volunteer_main.php\">".i18n("Volunteer Home").'</a></li>';
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/user_login.php?action=logout\">".i18n("Logout").'</a></li>';
} else if(get_value_from_session('users_type') == "sponsor") {
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/user_personal.php\">".i18n("My Profile").'</a></li>';
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/sponsor_main.php\">".i18n("Sponsor Home").'</a></li>';
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/user_login.php?action=logout\">".i18n("Logout").'</a></li>';
} else if(get_value_from_session('schoolid') && get_value_from_session('schoolaccesscode')) {
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/schoolaccess.php\">".i18n("School Home").'</a></li>';
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/schoolaccess.php?action=logout\">".i18n("Logout").'</a></li>';
}
else if(get_value_from_session('registration_number') && get_value_from_session('registration_id')) {
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/register_participants_main.php\">".i18n("Participant Home").'</a></li>';
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/register_participants.php?action=logout\">".i18n("Logout")."</a></li>\n";
} else {
	echo "<li><a href=\"{$config['SFIABDIRECTORY']}/login.php\">".i18n("Login/Register").'</a></li>';
}
?></ul>
<div class="aligncenter">
<?
if(count($config['languages'])>1) {
	echo "<br />";
	echo "<form name=\"languageselect\" method=\"get\" action=\"".$_SERVER['PHP_SELF']."\">";
	echo "<select name=\"switchlanguage\" onchange=\"document.forms.languageselect.submit()\">\n";
	foreach($config['languages'] AS $key=>$val) {
		if($_SESSION['lang']==$key) $selected="selected=\"selected\""; else $selected="";

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

?>
</div>
<?
echo "<br /><ul class=\"mainnav\">\n";
echo "<li><a href=\"{$config["SFIABDIRECTORY"]}/contact.php\">".i18n("Contact Us")."</a></li>\n";
echo "</ul>";
?>

</div>
</td><td>
<?

if(is_array($nav)) {
	echo "<div id=\"mainwhere\">".i18n('You are here:').' ';
	foreach($nav as $t=>$l) {
		echo "<a href=\"{$config['SFIABDIRECTORY']}/$l\">".i18n($t).'</a> &raquo; ';
	}
	if(!$titletranslated)
		echo i18n($title);
	else
		echo $title;
	echo '</div>';
}
?>

<div id="main">
<?

if(committee_auth_has_access("config") || committee_auth_has_access("admin"))
	committee_warnings();
if(committee_auth_has_access("config"))
	config_warnings();
if(committee_auth_has_access("admin"))
	admin_warnings();

echo "<table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tr>";

if($icon && theme_icon($icon)) {
        echo "<td width=\"40\">";
        echo theme_icon($icon);
        echo "</td><td>";
}
else
    echo "<td>";

if($title && !$titletranslated)
	echo "<h2>".i18n($title)."</h2>";
else if($title)
	echo "<h2>".$title."</h2>";

//if we're under /admin or /config then we want to show the ? help icon
if(substr(getcwd(),-6)=="/admin" || substr(getcwd(),-7)=="/config" || substr(getcwd(),-6)=="\\admin" || substr(getcwd(),-7)=="\\config" )
{
	if(get_value_from_array($_SERVER, 'REDIRECT_SCRIPT_URL'))
		$fname=substr($_SERVER['REDIRECT_SCRIPT_URL'],strlen($config['SFIABDIRECTORY'])+1);
	else
		$fname=substr($_SERVER['PHP_SELF'],strlen($config['SFIABDIRECTORY'])+1);	
	echo "</td><td align=\"right\"><a target=\"_sfiabhelp\" href=\"http://www.sfiab.ca/wiki/index.php/Help_$fname\"><img border=\"0\" src=\"".$config['SFIABDIRECTORY']."/images/32/help.".$config['icon_extension']."\"></a>";
}
"</td></tr>";
echo "</table>";

	display_messages();
}
/* END OF send_header */

function send_footer()
{
global $config;
?>
</td></tr></table>
</div>
<div id="footer">
<? 
//we only show the debug session variables if we have an ODD numbered version.
if(substr($config['version'], -1) % 2 != 0)
{
	$pos = strpos(getcwd(),'/');
	if($pos === false){ // Windows OS
		$revision = "na";
    }
	else{
		$revision=exec("svn info |grep Revision");
	}
	$extra=" (Development $revision)";
	if(get_value_from_array($_SESSION, 'debug') == "true")
		$extra.=" DEBUG: ".print_r($_SESSION,true);
}

// FIX ME
echo "<a target=\"blank\" href=\"http://www.sfiab.ca\">SFIAB Version ".$config['version']."{$extra}</a>";
?>
</div>
<div id="debug" style="display:<?=(get_value_from_array($_SESSION, 'debug') == 'true')?'block':'none'?>; font-family:monospace; white-space:pre;">Debug...</div>
<iframe id="content" src="" style="visibility:hidden; width:0px; height:0px"></iframe>

</body>
</html>

<?
}

function send_popup_header($title="")
{
	global $HEADER_SENT;
	global $config;

	//do this so we can use send_header() a little more loosly and not worry about it being sent more than once.
	if($HEADER_SENT) return;
	else $HEADER_SENT=true;
	
	echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n";
?>
<!DOCTYPE html> 
<html>
<head>
<meta charset="utf-8" />
<title><?=i18n($title)?></title>
<link rel="stylesheet" href="<?=$config['SFIABDIRECTORY']?>/theme/<?=$config['theme']?>/jquery-ui-1.7.2.custom.css" type="text/css" media="all" />
<link rel="stylesheet" href="<?=$config['SFIABDIRECTORY']?>/theme/<?=$config['theme']?>/sfiab.css" type="text/css" media="all" />
<link media=all href="<?=$config['SFIABDIRECTORY']?>/tableeditor.css" type=text/css rel=stylesheet>
</head>
<body onLoad="window.focus()">
<script type="text/javascript" src="<?=$config['SFIABDIRECTORY']?>/js/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="<?=$config['SFIABDIRECTORY']?>/js/jqueryui/1.7.2/jquery-ui.min.js"></script> 
<script type="text/javascript" src="<?=$config['SFIABDIRECTORY']?>/js/sfiab.js"></script>
<div id="notice_area" class="notice_area"></div>

<?
if($title)
	echo "<h2>".i18n($title)."</h2>";

}

function send_popup_footer()
{
?>
<br />
<br />
<div id="footer">
<?
global $config;
$lastdigit=$config['version'][strlen($config['version'])-1];
if($lastdigit%2!=0)
{
	echo "DEBUG:";
	print_r($_SESSION); 
}
echo "SFIAB Version ".$config['version']; 
?>
</div>
<div id="debug" style="display:<?=($_SESSION['debug']=='true')?'block':'none'?>">Debug...</div>
<iframe id="content" src="" style="visibility:hidden; width:0px; height:0px"></iframe>

</body>
</html>
<?
}


function emit_month_selector($name,$selected="")
{
	echo "<select name=\"$name\">\n";
	$months=array("","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	echo "<option value=\"\">".i18n("Month")."</option>\n";
	for($x=1;$x<=12;$x++)
	{
		if($x==$selected)
			$s="selected=\"selected\"";
		else
			$s="";
		echo "<option $s value=\"$x\">".$months[$x]."</option>\n";
	}

	echo "</select>\n";

}


function emit_day_selector($name,$selected="")
{
	echo "<select name=\"$name\">\n";
	echo "<option value=\"\">".i18n("Day")."</option>\n";

	for($x=1;$x<=31;$x++)
		echo "<option value=\"".($x<10?"0":"")."$x\" ".($selected==$x?"selected=\"selected\"":"").">$x</option>\n";

	echo "</select>\n";

}

function emit_year_selector($name,$selected="",$min=0,$max=0)
{
	$curyear=date("Y");
	echo "<select name=\"$name\">\n";
	echo "<option value=\"\">".i18n("Year")."</option>\n";

	if($min&&$max)
	{
		for($x=$min;$x<=$max;$x++)
			echo "<option value=\"$x\" ".($selected==$x?"selected=\"selected\"":"").">$x</option>\n";

	}
	else 
	{
		//if we arent given a min and max, lets show current year + 5
		for($x=$curyear;$x<$curyear+5;$x++)
			echo "<option value=\"$x\" ".($selected==$x?"selected=\"selected\"":"").">$x</option>\n";
	}
	echo "</select>\n";
}

function emit_date_selector($name,$selected="")
{
	if($selected)
	{
		list($year,$month,$day)=explode("-",$selected);
	}
	echo "<table cellpadding=0>";
	echo "<tr><td>";
	emit_year_selector($name."_year",$year);
	echo "</td><td>";
	emit_month_selector($name."_month",$month);
	echo "</td><td>";
	emit_day_selector($name."_day",$day);
	echo "</td></tr>";
	echo "</table>";
}

function emit_hour_selector($name,$selected="")
{
	if($selected!="") $selected=(int)$selected;
	echo "<select name=\"$name\">\n";
	echo "<option value=\"\">HH</option>\n";


	for($x=0;$x<=23;$x++)
	{
		if($x===$selected)
			$sel="selected";
		else
			$sel="";
		echo "<option value=\"$x\" $sel>".sprintf("%02d",$x)."</option>\n";
	}

	echo "</select>\n";


}
function emit_minute_selector($name,$selected="")
{
	$mins=array("00","05","10","15","20","25","30","35","40","45","50","55");
	echo "<select name=\"$name\">\n";
	echo "<option value=\"\">MM</option>\n";

	for($x=0;$x<count($mins);$x++)
		echo "<option value=\"".$mins[$x]."\" ".($selected==$mins[$x]?"selected":"").">$mins[$x]</option>\n";

	echo "</select>\n";


}

function emit_time_selector($name,$selected="")
{
	global $hour;
	global $minute;
	if($selected)
	{
		list($hour,$minute,$second)=explode(":",$selected);
	}
	echo "<table cellpadding=0>";
	echo "<tr><td>";
	emit_hour_selector($name."_hour",$hour);
	echo "</td><td>";
	emit_minute_selector($name."_minute",$minute);
	echo "</td></tr>";
	echo "</table>";

}

function emit_province_selector($name,$selected="",$extra="")
{
	global $config;
	global $pdo;
	$q=$pdo->prepare("SELECT * FROM provinces WHERE countries_code='".$config['country']."' ORDER BY province");
	$q->execute();
	if($q->rowCount()==1)
	{
		$r = $q->fetch();
		echo "<input type=\"hidden\" name=\"$name\" value=\"$r-code\">";
		echo i18n($r->province);
	}
	else
	{
		echo "<select name=\"$name\" $extra>\n";
		echo "<option value=\"\">".i18n("Select a {$config['provincestate']}")."</option>\n";
		while($r = $q->fetch())
		{
			if($r->code == $selected) $sel="selected=\"selected\""; else $sel="";

			echo "<option $sel value=\"$r->code\">".i18n($r->province);
			echo "</option>\n";
		}

		echo "</select>\n";
	}

}


function outputStatus($status)
{
	$ret="";
	switch($status)
	{
		case 'incomplete': 
			$ret.="<div class=\"incomplete\">";
			$ret.= i18n("Incomplete");
			$ret.= "</div>";
			break;
		case 'complete':
			$ret.= "<div class=\"complete\">";
			$ret.= i18n("Complete");
			$ret.= "</div>";
			break;
		case 'empty':
			$ret.="<div class=\"incomplete\">";
			$ret.= i18n("Empty");
			$ret.= "</div>";
			break;

		default:
			$ret.=i18n("Unknown");
			break;
	}
	return $ret;
}

//returns true if its a valid email address, false if its not
function isEmailAddress($str) {
	if(preg_match('/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$/i', $str))
		return true;
	else
		return false;
}

function communication_get_user_replacements(&$u) {
	global $config;
	$rep = array('FAIRNAME' => $config['fairname'],
			'NAME' => $u['name'],
			'EMAIL' => $u['email'],
			'PASSWORD' => $u['password'],
			'SALUTATION' => $u['salutation'], 
			'FIRSTNAME' => $u['firstname'],
			'LASTNAME' => $u['lastname'],
			'ORGANIZATION' => $u['sponsor']['organization'],
	);
	return $rep;
}

function communication_replace_vars($text, &$u, $otherrep=array()) {
	global $config;
	if($u) {
		$userrep=communication_get_user_replacements($u);
	}
	else { 
		$userrep=array();
	}

	$rep=array_merge($userrep,$otherrep);
	foreach($rep AS $k=>$v) {
		$text=ereg_replace("\[$k\]",$v,$text);
	}
	return $text;
}

function email_send($val,$to,$sub_subject=array(),$sub_body=array())
{
	global $config;

	/* Standard substitutions that are constant no matter who
	 * the $to is */
	$urlproto = $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://";
	$urlmain = "$urlproto{$_SERVER['HTTP_HOST']}{$config['SFIABDIRECTORY']}";
	$urllogin = "$urlmain/login.php";
	$stdsub = array("FAIRNAME"=>i18n($config['fairname']),
			"URLMAIN"=>$urlmain,
			"URLLOGIN"=>$urllogin,
		);
	/* Add standard subs to existing sub arrays */
	$sub_subject = array_merge($sub_subject, $stdsub);
	$sub_body = array_merge($sub_body, $stdsub);

	//if our "to" doesnt look like a valid email, then forget about sending it.
	if(!isEmailAddress($to))
	{
		return false;
	}

	$q=("SELECT * FROM emails WHERE val='$val'");
	if($r = $q->fetch()) {
		//we dont want to translate these, the messages themselves shoudl contain whatever languages they need
		$subject=$r->subject;
		$body=$r->body;
		$bodyhtml=$r->bodyhtml;
		
		/* Eventually we should just do this with communication_replace_vars() */
		if(count($sub_subject)) {
			foreach($sub_subject AS $sub_k=>$sub_v) {
				$subject=ereg_replace("\[$sub_k\]","$sub_v",$subject);
			}
		}
		if(count($sub_body)) {
			foreach($sub_body AS $sub_k=>$sub_v) {
				$body=ereg_replace("\[$sub_k\]","$sub_v",$body);
			}
		}

		if(count($sub_body)) {
			foreach($sub_body AS $sub_k=>$sub_v) {
				$bodyhtml=ereg_replace("\[$sub_k\]","$sub_v",$bodyhtml);
			}
		}

		if($r->from)
			$fr=$r->from;
		else if ($config['fairmanageremail'])
			$fr=$config['fairmanageremail'];
		else
			$fr="";

		//only send the email if we have a from
		if($fr) {
			//send using RMail
			email_send_new($to,$fr,$subject,$body,$bodyhtml);
		}
		else
			echo error(i18n("CRITICAL ERROR: email '%1' does not have a 'From' and the Fair Manager Email is not configured",array($val),array("email key name")));	
	}
	else {
		echo error(i18n("CRITICAL ERROR: email '%1' not found",array($val),array("email key name")));	
	}
}

require_once("Rmail/Rmail.php");
require_once("Rmail/RFC822.php");

//this sends out an all-ready-to-go email, it does no substitution or changes or database lookups or anything
function email_send_new($to,$from,$subject,$body,$bodyhtml="") {
	$mail=new RMail();
	$mail->setFrom($from);
	$mail->setSubject($subject);
	$mail->setText($body);

	$r=new Mail_RFC822($from);
	$structure = $r->parseAddressList($from);
	$s=$structure[0];
	$ret=sprintf("%s@%s",$s->mailbox,$s->host);
	$mail->setReturnPath($ret);
	$mail->setHeader("Bounce-To",$ret);

	//only add the html if we have it
	if($bodyhtml) {
		$mail->setHTML($bodyhtml);
	}

	if(is_array($to)) {
		return $mail->send($to);
	} else {
		return $mail->send(array($to));
	}
}


/*
	returns an array of arrays
	[ 0 ] = array ( to, firstname, lastname, email )
	[ 1 ] = array ( to, firstname, lastname, email )
	...etc

*/
function getEmailRecipientsForRegistration($reg_id)
{
	global $config;
	//okay first grab the registration record, to see if we should email the kids, the teacher, and/or the parents
	$q=("SELECT * FROM registrations WHERE id='$reg_id' AND year='{$config['FAIRYEAR']}'");
	$registration=$q->fetch();

	if($registration->emailcontact && isEmailAddress($registration->emailcontact)) {
			$ret[]=array("to"=>$registration->emailcontact,
					"firstname"=>"",
					"lastname"=>"",
					"email"=>$registration->emailcontact,
				);
	}

	$sq=("SELECT * FROM students WHERE registrations_id='$reg_id' AND year='{$config['FAIRYEAR']}'");
	$ret=array();
	while($sr=$sq->fetch()) {
		if($sr->email && isEmailAddress($sr->email)) {
			$to=$sr->email;

			$ret[]=array("to"=>$to,
					"firstname"=>$sr->firstname,
					"lastname"=>$sr->lastname,
					"email"=>$sr->email,
				);
		}
	}
	return $ret;
}

function output_page_text($textname)
{
	global $config;
	global $pdo;
	
	$q = $pdo->prepare("SELECT * FROM pagetext WHERE textname='$textname' AND year='".$config['FAIRYEAR']."' AND lang='".$_SESSION['lang']."'");
	$q->execute();
	if($q->rowCount())
		$r = $q->fetch();
	else
	{
		//not defined, lets grab the default text
		$q=("SELECT * FROM pagetext WHERE textname='$textname' AND year='-1' AND lang='".$config['default_language']."'");
		$r = $q->fetch();
	}

	//if it looks like we have HTML content, dont do a nl2br, if there's no html, then do the nl2br
	if($r->text !== null and strlen($r->text)==strlen(strip_tags($r->text)))
		echo nl2br($r->text);
	else
		echo $r->text;
}

function output_page_cms($filename)
{
	global $config;
	global $pdo;
	
	$q = $pdo->prepare("SELECT * FROM cms WHERE filename='".$filename."' AND lang='".$_SESSION['lang']."' ORDER BY dt DESC LIMIT 1");
	$q->execute();
	if($q->rowCount())
	{
		$r = $q->fetch();
		send_header($r['title'],null,null,true);

		if(file_exists("data/logo-200.gif") && $r['showlogo'] == 1)
			echo "<img align=\"right\" src=\"".$config['SFIABDIRECTORY']."/data/logo-200.gif\" border=\"0\">";

		//if it looks like we have HTML content, dont do a nl2br, if there's no html, then do the nl2br
		if($r['text'] !== null and strlen($r['text'])==strlen(strip_tags($r['text'])))
			echo nl2br($r['text']);
		else
			echo $r['text'];
	}
	else {
		send_header("Error: File not found");
		echo error(i18n("The file you have requested (%1), does not exist on the server.",array($filename)));
		return;
		//not defined, lets grab the default text
	}

	send_footer();
}

function generatePassword($pwlen=8)
{
	//these are good characters that are not easily confused with other characters :)
	$available="ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789";
	$len=strlen($available) - 1;

	$key="";
	for($x=0;$x<$pwlen;$x++)
		$key.=$available[rand(0,$len)];
	return $key;
}


//config specific warning
function config_warnings()
{

}

//admin specific warnings
function admin_warnings()
{

}

//warnings to show to both config and/or admin people
function committee_warnings()
{
	global $config;
	global $pdo;
	//it is vital that each year the system be rolled over before we start it again
	//we should do this, say, 4 months after the FAIRDATE, so its soon enough that they should see
	//the message as soon as they login to start preparing for hte new year, but not too late to do it
	//properly :)


	$q = $pdo->prepare("SELECT DATE_ADD('".$config['dates']['fairdate']."', INTERVAL 4 MONTH) < NOW() AS rollovercheck");
	$q->execute();

	$r = $q->fetch(PDO::FETCH_OBJ);
	
	// FIXME Clear out Important Dates as part of rollover
	if($r->rollovercheck) {
		echo error(i18n("It has been more than 4 months since your fair.  In order to prepare the system for the next year's fair, you should go to the SFIAB Configuration page, and click on 'Rollover Fair Year'.  Do not start updating the system with new information until the year has been properly rolled over."));
	}

	$warn = false;
	
	$q = $pdo->prepare("SELECT * FROM award_prizes WHERE `external_identifier` IS NOT NULL 
				AND external_identifier=prize");
	$q->execute();
	if($q->rowCount() > 0) {
		/* The bug was that the external_identifier was set to the prize name.. so only display the warning
		 * if we find that case for a non-sfiab external fair */
		while(($p = $q->fetch(PDO::FETCH_ASSOC) )) {
			$qq = ("SELECT * FROM award_awards $r->rollovercheck
						LEFT JOIN fairs ON fairs.id=award_awards.award_source_fairs_id
						WHERE award_awards.id='{$p['award_awards_id']}'
						AND year='{$config['FAIRYEAR']}'
						AND award_awards.award_source_fairs_id IS NOT NULL
						AND fairs.type='ysc' ");
						echo pdo->errorInfo();
			if($qq->rowCount() > 0) {
				$warn;
				
				break;
			}
		}
	}
	if($warn) {
		//let everyone know about the need to re-download awards before being able to upload
		echo notice(i18n("March 30, 2010 - There was a minor issue with uploading award results that has now been corrected, however, you will need to re-download your awards from all external sources, before you will be able to upload the award winners back to those external sources.  Re-downloading the awards will not affect the awards in any visible way, it will just allow the winners to be uploaded properly.  Click on Fair Administration -> Awards Management -> Download awards from external sources -> and click 'check' for each award source"));
	}

}

$CWSFDivisions=array(
	1=>"Discovery",
	2=>"Energy",
	3=>"Environment",
	4=>"Health",
	5=>"Information",
	6=>"Innovation",
	7=>"Resources"
);

function theme_icon($icon, $width=0) {
	global $theme_icons, $config;

	$w = ($width == 0) ? '' : "width=\"$width\"" ;
	if($theme_icons['icons'][$icon])
		return "<img src=\"{$config['SFIABDIRECTORY']}/theme/{$config['theme_icons']}/{$theme_icons['icons'][$icon]}\" border=\"0\" $w alt=\"".htmlspecialchars($icon)."\">";

	return "";
}

//$d can be a unix timestamp integer, OR a text string, eg 2008-01-22
function format_date($d) {
    global $config;
    if(is_numeric($d))
        return date($config['dateformat'],$d);
    else
        return date($config['dateformat'],strtotime($d));
}

//$t can be a unix timestamp integer, or a text string, eg 10:23:48
function format_time($t) {
    global $config;
    if(is_numeric($t))
        return date($config['timeformat'],$t);
    else
        return date($config['timeformat'],strtotime($t));
}

//$dt can be a unix timestamp integer, or a text string, eg 2008-01-22 10:23:48
function format_datetime($dt) {
    if(is_numeric($dt)) {
        return format_date($dt)." ".i18n("at")." ".format_time($dt);
    }
    else {
        list($d,$t)=explode(" ",$dt);
        return format_date($d)." ".i18n("at")." ".format_time($t);
    }
}

function format_money($n,$decimals=true)
{	global $neg;
	if($n<0){
		$neg=true;
		$n=$n*-1;
	}
	//get the part before the decimal
	$before=floor(get_value_or_default($n, 0));
	$out="";

	//space it out in blocks of three
	for($x=strlen($before);$x>3;$x-=3) {
		$out=substr($before,$x-3,3)." ".$out;
	}
	if($x>0)
		$out=substr($before,0,$x)." ".$out;

	//trim any leading/trailing space that was added
	$out=trim($out);

	if($neg) $negdisp="-"; else $negdisp="";

    if($decimals) {
        //get everything after the decimal place, and %02f it.
        $after=substr(strstr(sprintf("%.02f",$n),"."),1);

        //finally display it with the right language localization
        if($_SESSION['lang']=="fr")
            return sprintf("%s%s,%s \$",$negdisp,$out,$after);
        else
            return sprintf("%s\$%s.%s",$negdisp,$out,$after);
    }
    else {
        if($_SESSION['lang']=="fr")
            return sprintf("%s%s \$",$negdisp,$out);
        else
            return sprintf("%s\$%s",$negdisp,$out);

    }
}

function message_push($m)
{
	if(!is_array($_SESSION['messages'])) $_SESSION['messages'] = array();
	$_SESSION['messages'][] = $m;
}

function notice_($str, $i18n_array=array(), $timeout=-1, $type='notice')
{
	if($timeout == -1) $timeout = 5000;
	echo "<script type=\"text/javascript\">
		notice_create('$type',\"".i18n($str,$i18n_array)."\",$timeout);
		</script>";
}

function happy_($str, $i18n_array=array(), $timeout=-1)
{
	notice_($str, $i18n_array, $timeout, 'happy');
}
function error_($str, $i18n_array=array(), $timeout=-1)
{
	notice_($str, $i18n_array, $timeout, 'error');
}

function debug_($str)
{
	if(get_value_from_array($_SESSION, 'debug') != true) return;
	$s = str_replace("\n", "", nl2br(htmlspecialchars($str))).'<br />';
	echo "<script type=\"text/javascript\">
		$(document).ready(function() {
			$(\"#debug\").append(\"$s\");
		});
		</script>";
}

//this function returns a HTML colour code ranging between red and green, with yellow in the middle based on the percent passed into it
function colour_to_percent($percent)
{
	//0 is red
	//50 is yellow
	//100 is green

	if($percent<=50) $red=255;
	else $red=(100-$percent)*2/100*255;;

	if($percent>50) $green=255;
	else $green=($percent)*2/100*255;;

//    echo "red=$red";
//    echo "green=$green";
	$str="#".sprintf("%02s",dechex($red)).sprintf("%02s",dechex($green))."00";
	return $str;
}


function format_duration($seconds, $granularity = 2)
{
	$units = array(
	'1 year|:count years' => 31536000,
	'1 week|:count weeks' => 604800,
	'1 day|:count days' => 86400,
	'1 hour|:count hours' => 3600,
	'1 min|:count min' => 60,
	'1 sec|:count sec' => 1);
	$output = '';
	// $output.=time()." - ".$timestamp." = ".$seconds;
	foreach ($units as $key => $value) {
		$key = explode('|', $key);
		if ($seconds >= $value) {
			$count = floor($seconds / $value);
			$output .= ($output ? ' ' : '');
			$output .= ($count == 1) ? $key[0] : str_replace(':count', $count, $key[1]);
			$seconds %= $value;
			$granularity--;
		}
		if ($granularity == 0) {
			break;
		}
	}
	return $output ? $output : '0 sec';
}

function getTextFromHtml($html) {
	//first, replace an </p> with </p><br />
	$text=str_replace("</p>","</p><br />",$html);
	//next, replace a </div> with </div><br />
	$text=str_replace("</div>","</div><br />",$html);
	//now replace any <br /> with newlines
	$text=eregi_replace('<br[[:space:]]*/?[[:space:]]*>',chr(13).chr(10),$text);
	//and strip the rest of the tags
	$text=strip_tags($text);

	//a few common html entities
	//replace &amp; with & first, so multiply-encoded entities will decode (like "&amp;#160;")
	$text=str_replace("&amp;","&",$text);
	$text=str_replace("&nbsp;"," ",$text);
	$text=str_replace("&#160;"," ",$text);
	$text=str_replace("&lt;","<",$text);
	$text=str_replace("&gt;",">",$text);

	//text version should always wrap at 75 chars, some mail severs wont accept
	//mail with very long lines
	$text=wordwrap($text,75,"\n",true);

	return $text;
}

function getUserForSponsor($sponsor_id) {
	// loop through each contact and draw a form with their data in it.
	$q = ("SELECT *,MAX(year) FROM users LEFT JOIN users_sponsor ON users_sponsor.users_id=users.id
	WHERE 
	sponsors_id='" . $sponsor_id . "' 
    AND types LIKE '%sponsor%'
    GROUP BY uid
	HAVING deleted='no' 
	ORDER BY users_sponsor.primary DESC,lastname,firstname
	LIMIT 1
    ");
	$r = $q->fetch();
	return user_load_by_uid($r->uid);
}

function projectdivisions_load($year = false)
{
	global $config;
	if($year == false) $year = $config['FAIRYEAR'];
	$divs = array();
	$q = ("SELECT * FROM projectdivisions WHERE year='$year'");
	while(($d = $q->fetch(PDO::FETCH_ASSOC))) $divs[$d['id']] = $d;
	return $divs;
}
function projectcategories_load($year = false)
{
	global $config;
	if($year == false) $year = $config['FAIRYEAR'];
	$cats = array();
	$q = ("SELECT * FROM projectcategories WHERE year='$year'");
	while(($c = $q->fetch(PDO::FETCH_ASSOC))) $cats[$c['id']] = $d;
	return $cats;
}

// Converts the numeric value "$val" to an English text representation of it (e.g. "two thousand four"). 
// If the "$monetize" flag is set to true, then it's formatted to be useable on printed cheques (e.g. "***** Two Thousand Four 00/100 *****".
function wordify($val, $monetize = false){
	$digits = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
	if($monetize){
		$pennies = intval(($val - intval($val)) * 100);
		$returnval = "and " . sprintf("%02d", $pennies) . "/100";
	}else if($val != intval($val)){
		$dec = $val - intval($val);
		$returnval = 'point';
		while($dec){
			$dec *= 10;
			$returnval .= " " . smallIntToText(intval($dec));
			$dec -= intval($dec);
		}
	}
	$val = intval($val);
	$powerofthousand = array(
		'', 'Thousand', 'Million', 'Billion', 'trillion', 'quadrillion'
	);
	$n = 0;
	if(!$val){
		$returnval = "Zero " . $returnval;
	}else{
		while($val > 0){
			$sectionVal = $val % 1000;
			if($sectionVal != 0){
				$sectionText = smallIntToText($sectionVal);
				if($powerofthousand[$n] != ''){
					$returnval = $sectionText . " " . $powerofthousand[$n] . " " . $returnval;
				}else{
					$returnval = $sectionText . " " . $returnval;
				}
			}
			$val = intval($val / 1000);
			$n++;
		}
	}
	if($monetize) $returnval = '***' . $returnval;
	return $returnval;
}

// Converts a number between zero and one thousand to Canadian English text
function smallIntToText($number){
	$number %= 1000;
	$rvals = array(
		0 => 'Zero',
		1 => 'One',
		2 => 'Two',
		3 => 'Three',
		4 => 'Four',
		5 => 'Five',
		6 => 'Six',
		7 => 'Seven',
		8 => 'Eight',
		9 => 'Nine',
		10 => 'Ten',
		11 => 'Eleven',
		12 => 'Twelve',
		13 => 'Thirteen',
		14 => 'Fourteen',
		15 => 'Fifteen',
		16 => 'Sixteen',
		17 => 'Seventeen',
		18 => 'Eighteen',
		19 => 'Nineteen',
		20 => 'Twenty',
		30 => 'Thirty',
		40 => 'Forty',
		50 => 'Fifty',
		60 => 'Sixty',
		70 => 'Seventy',
		80 => 'Eighty',
		90 => 'Ninety',
	);
	if(array_key_exists($number, $rvals)) return $rvals[$number];
	$returnval = '';
	if($number >= 100){
		$hundred = intval($number / 100);
		$returnval = $rvals[$hundred] . " Hundred";
		$number -= 100 * $hundred;
	}
	if(array_key_exists($number, $rvals)){
		if($number > 0) $returnval .= " " . $rvals[$number];
		return $returnval;
	}
	if($number >= 10){
		$ten = intval($number / 10);
		if($returnval != '') $returnval .= ' ';
		$returnval .= $rvals[10 * $ten];
		$number -= 10 * $ten;
	}
	if($number > 0){
		$returnval .= ' ' . $rvals[$number];
	}
	return $returnval;
}

?>
