<?php
error_reporting(E_ALL);

define("PAGE_USES_TIMEPICKR",true);

require_once("includes/SQLConnect.php");
require_once("includes/user_data.php");
require_once("includes/functions.php");

if(isset($_POST['title']) && $_POST['title']!=""){
	if(strlen($_POST['title'])>0){
		if(strlen($_POST['description'])>0){	
			if(strlen($_POST['city'])>0){
				//This is assuming you're doing it in your own district - theoretically possible otherwise, but not going to be coded for now
				$zip = zip_string_to_integer($_POST['zip']);
				$phone = phone_string_to_integer($_POST['phone']);
				
				error_reporting(2048);
				
				$temp = $_POST['being_held_time'];
				$temp = explode(":",$temp);
				$being_held = $_POST['being_held_date'].' '.((int)$temp[0]+(int)$_POST['being_held_time_ampm']).":".$temp[1];
				
				$sql="INSERT INTO Events(
						`user_ID` ,
						`district_ID` ,
						`title` ,
						`description` ,
						`place` ,
						`address` ,
						`city` ,
						`zip` ,
						`state` ,
						`phone` ,
						`email`,
						`being_held`) 
					VALUES(
						".$user_data['ID'].",
						".$representative['district_ID'].",
						'".mysql_real_escape_string($_POST['title'])."',
						'".mysql_real_escape_string($_POST['description'])."',
						'".mysql_real_escape_string($_POST['place'])."',
						'".mysql_real_escape_string($_POST['address'])."',
						'".mysql_real_escape_string($_POST['city'])."',
						".$zip.",
						'".strtoupper(substr($_POST['state'],0,2))."',
						".$phone.",
						'".mysql_real_escape_string($_POST['email'])."',
						'".mysql_real_escape_string($being_held)."'
					
					)";
					
						
				if(mysql_query($sql))
					header("Location:district.php?success=Event successfully created.");
				else //No one will ever see this message because this page will redirect them when it's working
					$error = "Event not created. Probably you've clicked submit twice or hit refresh."; 
			}
			else
				$error = "Please identify what city this event is taking place in.";
		}
		else
			$error = "Please write a description for your event.";
	}
	else
		$error = "You must give your event a title so people know what it's about.";

}
include_once("includes/header.php");


//Set defaults
if(!isset($_POST['city']))
	$city = $user_data['city'];
else
	$city = $_POST['city'];

if(!isset($_POST['state']))
	$state = strtoupper($user_data['user_state']);
else
	$state = strtoupper(substr($_POST['state'],0,2));

if(!isset($_POST['zip']))
	$zip = zip_integer_to_string($user_data['zip']);
else
	$city = $_POST['zip'];
	
if(!isset($_POST['phone']) && strlen($user_data['phone'])>3)
	$phone = phone_string_to_integer($user_data['phone']);
else
	$phone = $_POST['phone'];	

if(!isset($_POST['email']))
	$email = $user_data['email'];
else
	$email = $_POST['email'];	



?>

<h2>Host An Event in Your District</h2>
<p>
	Please make sure these details are correct as errors can't be corrected afterwards.
</p>

<form action="event_host.php" method="POST">
	<label>Title (Mandatory):</label>
	<input type=text name="title" class="large">
	
	<label>Description (Mandatory):</label>
	<textarea rows="5" name="description"></textarea>
	
	<label>Place:</label>
	<input type="text" name="place">
	
	<label>Address:</label>
	<input type="text" name="address">
	
	<label>City (Mandatory):</label>
	<input type="text" name="city" value="<?=$city;?>">
	
	<label>Zip:</label>
	<input type="text" name="zip" size="10" value="<?=$zip;?>">
	
	<label>State:</label>
	<select name="state" id="state">
		<option value='AL' class='AL'>Alabama</option>
		<option value='AK' class='AK'>Alaska</option>
		<option value='AZ' class='AZ'>Arizona</option>
		<option value='AR' class='AR'>Arkansas</option>
		<option value='CA' class='CA'>California</option>
		<option value='CO' class='CO'>Colorado</option>
		<option value='CT' class='CT'>Connecticut</option>
		<option value='DC' class='DC'>District of Columbia</option>
		<option value='DE' class='DE'>Delaware</option>
		<option value='FL' class='FL'>Florida</option>
		<option value='GA' class='GA'>Georgia</option>
		<option value='HI' class='HI'>Hawaii</option>
		<option value='ID' class='ID'>Idaho</option>
		<option value='IL' class='IL'>Illinois</option>
		<option value='IN' class='IN'>Indiana</option>
		<option value='IA' class='IA'>Iowa</option>
		<option value='KS' class='KS'>Kansas</option>
		<option value='KY' class='KY'>Kentucky</option>
		<option value='LA' class='LA'>Louisiana</option>
		<option value='ME' class='ME'>Maine</option>
		<option value='MD' class='MD'>Maryland</option>
		<option value='MA' class='MA'>Massachusetts</option>
		<option value='MI' class='MI'>Michigan</option>
		<option value='MN' class='MN'>Minnesota</option>
		<option value='MS' class='MS'>Mississippi</option>
		<option value='MO' class='MO'>Missouri</option>
		<option value='MT' class='MT'>Montana</option>
		<option value='NE' class='NE'>Nebraska</option>
		<option value='NV' class='NV'>Nevada</option>
		<option value='NH' class='NH'>New Hampshire</option>
		<option value='NJ' class='NJ'>New Jersey</option>
		<option value='NM' class='NM'>New Mexico</option>
		<option value='NY' class='NY'>New York</option>
		<option value='NC' class='NC'>North Carolina</option>
		<option value='ND' class='ND'>North Dakota</option>
		<option value='OH' class='OH'>Ohio</option>
		<option value='OK' class='OK'>Oklahoma</option>
		<option value='OR' class='OR'>Oregon</option>
		<option value='PA' class='PA'>Pennsylvania</option>
		<option value='RI' class='RI'>Rhode Island</option>
		<option value='SC' class='SC'>South Carolina</option>
		<option value='SD' class='SD'>South Dakota</option>
		<option value='TN' class='TN'>Tennessee</option>
		<option value='TX' class='TX'>Texas</option>
		<option value='UT' class='UT'>Utah</option>
		<option value='VT' class='VT'>Vermont</option>
		<option value='VA' class='VA'>Virginia</option>
		<option value='WA' class='WA'>Washington</option>
		<option value='WV' class='WV'>West Virginia</option>
		<option value='DC' class='DC'>Washington D.C.</option>
		<option value='WI' class='WI'>Wisconsin</option>
		<option value='WY' class='WY'>Wyoming</option>
	</select>
	<script type="text/javascript">
		$("#state option.<?=$state;?>").attr({selected:'selected'});
	</script>
		
	<label>Phone:</label>
	<input type="text" name="phone" size="14" value="<?=$phone;?>">
	
	<label>Email (Mandatory):</label>
	<input type="text" name="email" value="<?=$email;?>">
	
	<label>Date (YYYY-MM-DD) of Event (Mandatory):</label>
	<input type="text" name="being_held_date" value="<?=date("Y-m-d");?>">
	
	<label>Event Time (Mandatory):</label>
	<select name="being_held_time">
		<?php
		for($i=0;$i<=12;$i++)
			for($j=0;$j<=45;$j+=15){
				echo '<option value="'.$i.':'.str_pad($j,2,"0").'">'.$i.':'.str_pad($j,2,"0").'</option>';
			}
		?>
	</select>
	
	<select name="being_held_time_ampm">
		<option value="0">AM</option>
		<option value="12">PM</option>
	</select>
	
	<input type="submit" class="submit" value="Host Event">
		

</form>


<?php
include_once("includes/footer.php");
?>