<?php define( '_BIGNIGHT_EXEC', 1 );?>
<?php session_start();?>

<?php require_once("helper.php"); ?>
<?php require_once("user.php"); ?>


<?php

	if(!isset($_GET["id"])) die("No event specified.");
	$id = intval($_GET["id"]);
	
	if($user===NULL) die("Log in to join events.");
	
	global $DB;
	
	$event = $DB->qry("SELECT a.event_id AS attending FROM events e LEFT JOIN (SELECT * FROM attending WHERE username='{$user["username"]}') a ON e.id=a.event_id WHERE id={$id};");
	
	if ($DB->numrows()===0){
		echo "Invalid event id.";
	} else {					
		$row = mysql_fetch_assoc($event);
			
		if($row["attending"] === NULL) $attending = false;
		else $attending = true;
		
		if($attending){
			$DB->qry("DELETE FROM attending WHERE event_id={$id} AND username='{$user["username"]}';");
			echo "You have left the event.";
		} else {
			$DB->qry("INSERT INTO attending (`username`,`event_id`) VALUES ('{$user["username"]}',{$id})");
			echo "You have joined the event.";
		}
	}
	
	echo "|" . attend_count($id) . "|" . get_events($id) . "|" . get_people($id);

?>