<?php
error_reporting(0);

//Constants
if(!defined(PAGE_USES_GMAPS))
	define("PAGE_USES_GMAPS",false);
if(!defined(PAGE_USES_AUTOCOMPLETE))
	define("PAGE_USES_AUTOCOMPLETE",false);
if(!defined(PAGE_USES_FACEBOX))
	define("PAGE_USES_FACEBOX",false);
if(!defined(PAGE_USES_FLASH))
	define("PAGE_USES_FLASH",false);

require_once("includes/SQLConnect.php");
require_once("includes/functions.php");
require_once("includes/user_data.php"); //"Returns" $user_data, an associative array that equals the Users table row for the logged in user



//If the user is not logged in...
if(!isset($user_data)){
				
	//Pages that can be visited while not logged in
	$no_login_whitelist = array(
		"index.php",
		"user_login.php",
		"user_logout.php",
		"register_user.php",
		"register_confirm.php",
		"register_district.php");

	$login_not_required_flag = false;
	foreach($no_login_whitelist as $no_login_page)
		if(strpos($_SERVER['SCRIPT_NAME'],$no_login_page) != FALSE)
			$login_not_required_flag = true;
	
	if(!$login_not_required_flag)
		header("Location:http://".$_SERVER['SERVER_NAME']."/user_login.php?validation=You are not logged in.");
}


?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

		<title>District by District Organizing Tool</title>
		<link rel="stylesheet" href="http://<?=$_SERVER['SERVER_NAME'];?>/styles/main.css" type="text/css" media="screen, tv, projection" />	
		
 		<!--[if lte IE 6]><style type="text/css" media="all">@import "/styles/ie6.css";</style><![endif]-->
		
		<script type='text/javascript' src='http://<?=$_SERVER['SERVER_NAME'];?>/js/jquery/jquery-1.3.2.min.js'></script> 

		<?php
		if(PAGE_USES_FLASH){
		?>
		<script type='text/javascript' src='http://<?=$_SERVER['SERVER_NAME'];?>/js/swfobject.js'></script> 
	
		<?php
		}
		?>
		
		
		<?php
		if(PAGE_USES_FACEBOX){
		?>
			<link rel="stylesheet" href="http://<?=$_SERVER['SERVER_NAME'];?>/styles/facebox.css" type="text/css" media="screen" />
			<script type='text/javascript' src='http://<?=$_SERVER['SERVER_NAME'];?>/js/jquery_facebox/facebox.js'></script> 		
			<script type='text/javascript' src='http://<?=$_SERVER['SERVER_NAME'];?>/js/jquery_facebox/facebox-driver.js'></script> 		
		<?php
		}
		?>
		
		<?php
		if(PAGE_USES_AUTOCOMPLETE){
		?>
			<link rel="stylesheet" href="http://<?=$_SERVER['SERVER_NAME'];?>/styles/autocomplete.css" type="text/css" media="screen" />
			<script type='text/javascript' src='http://<?=$_SERVER['SERVER_NAME'];?>/js/jquery_ac/autocomplete.js'></script> 
			<script type='text/javascript' src='http://<?=$_SERVER['SERVER_NAME'];?>/js/jquery_ac/autocomplete-driver.js'></script> 
		<?php
		}		
		if(PAGE_USES_GMAPS){
			?>
			<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAslJOdSNuKmi3x4j-m-qgGhSUTexI0NaKKOrqT6aDYOgH_JohTBR5K6JvzE-HlWwEkolGkM9zbYp8lA" type="text/javascript"></script>
			<script src="http://<?=$_SERVER['SERVER_NAME'];?>/js/gmap-wms.js"></script>
		<?php
		}
		?>
	</head>
	<body>
		<div id="header">
			<h1><a href="http://<?=$_SERVER['SERVER_NAME'];?>/index.php">The District-by-District Organizing Tool</a></h1>
			<?php
			if(isset($user_data)){
			?>
				<div id="menu">
					<a href="http://<?=$_SERVER['SERVER_NAME'];?>/profile_user_edit.php">Edit Your Profile & Account</a>
					<a href="http://<?=$_SERVER['SERVER_NAME'];?>/user_logout.php">Logout</a>
				</div>
			<?php
			}
			else{
			?>
				<div id="menu">
					<a href="http://<?=$_SERVER['SERVER_NAME'];?>/register_user.php">Register</a>
					<a href="http://<?=$_SERVER['SERVER_NAME'];?>/user_login.php">Login</a>
				</div>			
			<?php
			}
			?>
		</div>
		
<?php
/*
	
	The following processes any error/status/warning/validation messages sent via GET or set via $

*/



//System->User Messaging System (reports errors/success/validation required/etc.)
//Takes either strings ($error) or arrays ($error[])
$types = array("error","success","validation");


//Import GET variables if any are sent
foreach($types as $type)
	if(isset($_GET[$type]))
		eval('$'.$type.' = $_GET[\''.$type.'\'];');

//This will output all the messages stored in whatever variable (see $types for allowed variables)
foreach($types as $type){	
	@eval('$type_var = $'.$type.';'); //Set $type_var to the value of the variable in question
	
	if(isset($type_var))
		if(is_array($type_var)){ //If there are multiple messages being passed, make sure they all get printed
			?>
			<div class="msg <?=$type;?>">
				<?php
				foreach($type_var as $msg)
					echo stripslashes($msg);
				?>
			</div>
			<?php
		}
		else{ //If it's just one message, print i
			?>
			<div class="msg <?=$type;?>">
				<?php print stripslashes($type_var);?>
			</div>
			<?php
		}
} 


?>
	 	<!--[if IE 6]><div class='msg error'>We're sorry, but for the Alpha version of this, the site only works in Firefox and Safari. Internet Explorer functionality is extremely limited. The final version of this site will work in IE, but for now, please use Firefox or Safari.</div><![endif]-->
		

		<div id="wrap">
			<?php
			if(isset($user_data)){
			
				function create_menu_item($url,$text,$last_flag = false){
					$selected = "";
					if(strpos($_SERVER['SCRIPT_NAME'],$url) != FALSE || strpos($_SERVER['SCRIPT_NAME'],substr($url,0,strrpos($url,'_')-1))!=FALSE &&  strpos($_SERVER['SCRIPT_NAME'],substr($url,0,strrpos($url,'_')-1)) == 0)
						$selected = "class = 'selected'";
											
					if(@$last_flag)
						$last=" id='last'";
				?>
					<li <?=@$last;?>>
						<div>
							<a href="http://<?=$_SERVER['SERVER_NAME'];?>/<?=$url;?>" <?=$selected;?>><?=$text;?></a>
						</div>
						<div class='corner'></div>
					</li>
				<?php
				}
				
				?>
				<div id="menu-internal">
					<ul>
						<?php
						create_menu_item("district.php",$user_data['state']."-".$user_data['district'].' Homepage');
						create_menu_item("representative.php",$representative['first_name'].' '.$representative['last_name']." (".$representative['party_abbreviated'].")");
						create_menu_item("senators.php","Your Senators");
						
						$unread_message_count = @(int)mysql_result(mysql_query("SELECT COUNT( ID ) FROM `User_Messages` WHERE open_flag=0 AND to_user_ID =".$user_data['ID']." GROUP BY to_user_ID LIMIT 1"),0);
						if($unread_message_count>1)
							create_menu_item("message_inbox.php",$unread_message_count." Unread Messages");
						elseif($unread_message_count>0)
							create_menu_item("message_inbox.php",$unread_message_count." Unread Message");
						else
							create_menu_item("message_inbox.php","Messages");
							
						create_menu_item("events_browse.php","Events");
						create_menu_item("news_suggest.php","Suggest News");
						create_menu_item("affiliations.php","Affiliations");
						create_menu_item("user_invite.php","Invite a Friend");
						create_menu_item("forum_browse.php","National Forum",true);
	
						?>
					</ul>
				</div>
	
				<?php
			}
			else{
			?>
				<div id="welcome-box">
					<div id="welcome-message">
						Welcome to the District-by-District Alpha.
					</div>
					<div class="corner"></div>
				</div>
			
			<?php
			}
			?>			
			<div id="content-main">