<?php defined( '_BIGNIGHT_EXEC' ) or die( 'Restricted access' );?>
<?php
	global $user;
	require_once("include/profile.php");
?>

<?php if (!isset($user)):	
		// not logged in - cannot create	?>
<p>Please <a href="index.php?view=user">log in</a> to create an event</p>
<?php
	elseif (isset($_POST["submit"])):
		session_start();
		$DB = DB::Open();
		global $error;
		
		if (!isset($_POST["type"])) $error = ("Error! Missing type.");
		if (empty($_POST["title"])) $error = ("Error! Missing title.");
		if (!isset($_POST["description"]) || 
		!isset($_POST["address"]) ||
		!isset($_POST["zip"]) ||
		!isset($_POST["price"])) $error = ("Error! Missing parameters.");
		if(empty($_POST["datetime"]) ||
		empty($_POST["gmdatetime"])) $error = ("Error! Missing date.");

		include_once 'securimage/securimage.php';
		$securimage = new Securimage();
		if ($securimage->check($_POST['captcha_code']) == false) {
			$error = ("Error! Incorrect captcha entered.");
		}

		$time = ($_POST["gmdatetime"]);
		
		// prepare other inputs for query
		$name = str_replace("'", "''", $_POST["title"]);
		$description = str_replace("'", "''", $_POST["description"]);
		$address = str_replace("'", "''", $_POST["address"]);
		if ($address == "") {
			$address = $DB->qry("SELECT address FROM users WHERE username='{$user["username"]}';",3);
		}
		
		$zip = intval($_POST["zip"]);
		if ($zip == "") {
			$zip = $DB->qry("SELECT zip FROM users WHERE username='{$user["username"]}';",3);
		}	
	
		$price = round ((float)$_POST["price"], 2);
		if(empty($error)){
			if(!empty($_POST["type"])) $type = str_replace("'", "''", $_POST["type"]);
			else if(!empty($_POST["type_str"])) $type = str_replace("'", "''", $_POST["type_str"]);
			else $type = "Other";
			$sql = "INSERT INTO events (`username`,`type`,`description`,`title`,`address`,`zip`,`price`,`time`) VALUES ('{$user["username"]}', '{$type}', '{$description}','{$name}','{$address}',{$zip},{$price},{$time});";
			
			$DB->qry($sql);
			
			if($DB->numrows()!=0){
				$id = $DB->qry("SELECT LAST_INSERT_ID();",3);
			//	header('Location: index.php?view=event&id={$id}');
				echo "<script type=\"text/javascript\">window.location = 'index.php?view=event&id={$id}'</script>";
				$error = "Success! Event created.";
			} else $error = "Error creating event.";
		}
?>		
<?php
	endif;
	if((!isset($_POST["submit"])||!empty($error))&&isset($user)):
		// display create event form
		
		$DB = DB::Open();
?>
			<script src="js/mootools-core-1.4.2-full-compat-yc.js" type="text/javascript"></script>
			<script src="js/mootools-more-1.4.0.1_datepicker_custom_yc.js" type="text/javascript"></script>    <script src="arian-mootools-datepicker-0768c57/Source/Locale.en-US.DatePicker.js" type="text/javascript"></script>
			<script src="js/arian-mootools-datepicker-0768c57/Source/Picker.js" type="text/javascript"></script>
			<script src="js/arian-mootools-datepicker-0768c57/Source/Picker.Attach.js" type="text/javascript"></script>
			<script src="js/arian-mootools-datepicker-0768c57/Source/Picker.Date.js" type="text/javascript"></script>
			<link href="js/arian-mootools-datepicker-0768c57/Source/datepicker_dashboard/datepicker_dashboard.css" rel="stylesheet">
			<script>
			// TODO: Date limiter - no dates in the past, or greater than 2 years
			var d = new Date();
			window.addEvent('domready', function(){
				new Picker.Date('datetime', {
					timePicker: true,
					positionOffset: {x: 5, y: 0},
					pickerClass: 'datepicker_dashboard',
					useFadeInOut: !Browser.ie,
					format: '%x %X',
					startView: 'days',
					onSelect: function(date){
						document.id('gmdatetime').value = Math.round((date.valueOf()-(d.getTimezoneOffset()*60000))/1000);
					} 
				 });
			 });
			</script>

	<form method="post" action="index.php?view=create">
		<table>
			<tr><th></th><th>Please enter event information</th></tr>
			<tr><td>Type</td>
				<td>
					<select name="type">
						<?php
							$rows = $DB->qry("SELECT type FROM event_types;");
							while($row = mysql_fetch_assoc($rows)){
								echo "<option name=\"type\" value=\"{$row["type"]}\"  onclick=\"document.getElementById('type_str').style.display = 'none';\" " . ($_POST["type"]==$row["type"]?"selected=\"selected\"":"") . ">{$row["type"]}</option>\n";
							}
						?>
						<option name="type" value="Other" onclick="document.getElementById('type_str').style.display = 'inline';">Other</option>
					</select>
					<input type="text" size="20" name="type_str" id="type_str" <?php echo (empty($_POST["type_str"])?"style=\"display:none;\"":"value=\"{$_POST["type_str"]}\""); ?>>
				</td>
			</tr>					
			<tr><td>Title:</td><td><input type="text" size="35" name="title" <?php echo (empty($_POST["title"])?"":"value=\"{$_POST["title"]}\""); ?>></td></tr>
			<tr><td>Description:</td><td><textarea rows="5" cols="50" name="description"><?php echo (empty($_POST["description"])?"":$_POST["description"]); ?></textarea></td></tr>
			<tr><td>Address:</td><td><input type="text" name="address" <?php echo (empty($_POST["address"])?"":"value=\"{$_POST["address"]}\""); ?>> (blank for your address)</td></tr>
			<tr><td>Zip:</td><td><input type="text" name="zip" size="5" maxlength="5" onKeyPress="return numbersonly(this, event)" <?php echo (empty($_POST["zip"])?"":"value=\"{$_POST["zip"]}\""); ?>> (blank for your zip)</td></tr>
			<tr><td>Price ($)</td><td><input type="text" name="price" size="5" onKeyPress="return numbersonly(this, event, true)" <?php echo (empty($_POST["price"])?"":"value=\"{$_POST["price"]}\""); ?>></td></tr>
			<tr><td>Date & Time</td><td><input type="text" name="datetime" id="datetime" <?php echo (empty($_POST["datetime"])?"":"value=\"{$_POST["datetime"]}\""); ?>><input type="hidden" name="gmdatetime" id="gmdatetime" <?php echo (empty($_POST["gmdatetime"])?"":"value=\"{$_POST["gmdatetime"]}\""); ?>></td></tr>
			<tr><td></td><td><img id="captcha" src="securimage/securimage_show.php" alt="CAPTCHA Image" /><td></tr>
			<tr><td>Enter code:</td><td><input type="text" name="captcha_code" size="10" maxlength="6" />
					<a href="#" onclick="document.getElementById('captcha').src = 'securimage/securimage_show.php?' + Math.random(); return false">[ Different Image ]</a>
					<td></tr>
			<tr><td></td><td><input type="submit" name="submit" value="Create event"></td></tr>
		</table>			
	</form>
<?php
	endif;
?>