<?php
	defined( '_BIGNIGHT_EXEC' ) or die( 'Restricted access' );
	$DB = DB::Open();
	?>

	<form method="post" action="index.php?view=search">
		<table>
			<tr>
				<td style="width:135px;">Zip:</td>
				<td>
					<input type="text" name="zip" size="5" maxlength="5" onKeyPress="return numbersonly(this, event)" value="<?php echo get_param("zip"); ?>">
				</td>
			</tr>
			<tr>
				<td>Keyword:</td>
				<td>
					<input type="text" name="keyword" size="35" style="visibility:visible;" value="<?php echo get_param("keyword"); ?>">
					<a id="expand_link" href="javascript:void(0)" onclick="javascript:expand('adv')">[ + ]</a><a href="javascript:void(0)" onclick="javascript:expand('adv')"> (advanced search)</a>
				</td>
			</tr>
		</table>
		<div id="adv" name="adv" style="overflow:hidden;display:<?php echo (((get_param("type")=="any" || (!isset($_POST["type"])&&!isset($_GET["type"]))) && (get_param("date_range")=="after_date" || (!isset($_POST["date_range"])&&!isset($_GET["date_range"]))) && empty($_GET["organizer"]) && empty($_POST["organizer"]) && empty($_GET["title"]) && empty($_POST["title"]) && empty($_GET["price"]) && empty($_POST["price"]))?"none":"block")?>">
		<table>
			<tr>
				<td style="width:135px;">Date (mm-dd-yy):</td>
				<td>
					<input type="text" name="month" size="2" maxlength="2" onKeyPress="return numbersonly(this, event)" value="<?php echo (get_param("month")!="" ? get_param("month") : gmdate("m")); ?>">-
					<input type="text" name="day" size="2" maxlength="2" onKeyPress="return numbersonly(this, event)" value="<?php echo (get_param("day")!="" ? get_param("day") : gmdate("d")); ?>">-
					<input type="text" name="year" size="2" maxlength="2" onKeyPress="return numbersonly(this, event)" value="<?php echo (get_param("year")!="" ? get_param("year") : gmdate("y")); ?>">
					<select name="date_range">
						<option value="after_date" <?php echo (get_param("date_range")=="after_date" ? "selected=\"selected\"" : ""); ?>>After</option>
						<option value="before_date" <?php echo (get_param("date_range")=="before_date" ? "selected=\"selected\"" : ""); ?>>Before</option>
						<option value="on" <?php echo (get_param("date_range")=="on" ? "selected=\"selected\"" : ""); ?>>On</option>
						<option value="all" <?php echo (get_param("date_range")=="all" ? "selected=\"selected\"" : ""); ?>>All</option>
					</select>
				</td>
			</tr>			
			<tr>
				<td>Type:</td>
				<td>
					<select name="type">
						<option value="any">Any</option>';
<?php
	$rows = $DB->qry("SELECT SQL_CALC_FOUND_ROWS type FROM event_types;");
	while($row = mysql_fetch_assoc($rows)){
		echo "<option value=\"{$row["type"]}\" " . (get_param("type")==$row["type"] ? "selected=\"selected\"" : "") . ">{$row["type"]}</option>";
	}
?>					

					</select>
				</td>
			</tr>
			<tr>
				<td>Organizer:</td>
				<td>
					<input type="text" name="organizer" size="35" style="visibility:visible;" value="<?php echo get_param("organizer"); ?>">
				</td>
			</tr>
			<tr>
				<td>Title:</td>
				<td>
					<input type="text" name="title" size="35" style="visibility:visible;" value="<?php echo get_param("title"); ?>">
				</td>
			</tr>
			<tr>
				<td>Price:</td>
				<td>
					$ <input type="text" name="price" size="10" style="visibility:visible;" value="<?php echo get_param("price"); ?>">
					<select name="price_range">
						<option value="less" <?php echo (get_param("price_range")=="less" ? "selected=\"selected\"" : "");?>>Less than</option>
						<option value="greater" <?php echo (get_param("price_range")=="greater" ? "selected=\"selected\"" : ""); ?>>Greater than</option>
						<option value="exactly" <?php echo (get_param("price_range")=="exactly" ? "selected=\"selected\"" : ""); ?>>Exactly</option>
					</select>
				</td>
			</tr>
		</table>
		</div>
		<div style="margin-left:230px;"><input type="submit" value="Search"> <a href="index.php?view=search">Reset</a></div>
	</form>

<?php


//	if (get_param("submit") == "Search")
	if(isset($_GET["type"]) || isset($_POST["type"]))
	{
		$count = 0;
		$sql = "SELECT SQL_CALC_FOUND_ROWS e.*,u.name FROM events e JOIN users u ON e.username=u.username LEFT JOIN zip_code z ON e.zip=z.zip_code ";
		
		if (get_param("type") != "any")
		{
			$sql .= "WHERE type='" . get_param("type") . "'";
			$summary .= " Type='" . get_param("type") . "'";
			$count++;
		}
		if (get_param("organizer") != "")
		{
			if ($count > 0)
				$sql .= " AND e.username='" . get_param("organizer") . "'";
			else
			{
				$sql .= "WHERE e.username='" . get_param("organizer") . "' OR name='" . get_param("organizer") . "'";
				$count++;
			}
		}
		if (get_param("title") != "")
		{
			if ($count > 0)
				$sql .= " AND title LIKE '%" . get_param("title") . "%'";
			else
			{
				$sql .= "WHERE title LIKE '%" . get_param("title") . "%'";
				$count++;
			}
		}
		if (get_param("keyword") != "")
		{
			$keywords = explode(" ", get_param("keyword"));
			if ($count > 0)
				$sql .= " AND (description LIKE '%" . $keywords[0] . "%' OR title LIKE'%" . $keywords[0] . "%')";
			else
			{
				$sql .= "WHERE (description LIKE '%" . $keywords[0] . "%' OR title LIKE'%" . $keywords[0] . "%')";
				$count++;
			}
			
			if (count($keywords) > 1)
				for ($i = 1; $i < count($keywords); $i++)
					$sql .= " AND (description LIKE '%" . $keywords[$i] . "%' OR title LIKE'%" . $keywords[0] . "%')";
		}
		if (get_param("zip") != "")
		{
			if ($count > 0)
				$sql .= " AND ";
			else
			{
				$sql .= "WHERE ";
				$count++;
			}
			$zip = intval(get_param("zip"));
			$pos = get_zip_pos($zip);
			$lat = $pos["lat"];
			$lon = $pos["lon"];
			if(!empty($lat)&&!empty($lon)) $sql .= " (z.zip_code IS NULL OR (ABS(z.lat-{$lat})<0.2 AND ABS(z.lon-{$lon})<0.2))";
			else $sql .= " AND e.zip={$zip}";

		}
		if (get_param("price") != "")
		{
			if ($count > 0)
				$sql .= " AND ";
			else {
				$sql .= "WHERE ";
				$count++;
			}
			
			if (get_param("price_range") == "less")
				$sql .= "price<=" . intval(get_param("price"));
			else if (get_param("price_range") == "greater")
				$sql .= "price>=" . intval(get_param("price"));
			else if (get_param("price_range") == "exactly")
				$sql .= "price=" . intval(get_param("price"));
		}
		if (get_param("month") != "" && get_param("day") != "" && get_param("year") != "")
			$time = gmmktime(0, 0, 0, intval(get_param("month")), intval(get_param("day")), intval(get_param("year")));
		else
			$time = time();

		if ($count > 0)
				$sql .= " AND ";
		else {
			$sql .= "WHERE ";
			$count++;
		}
			
		if (get_param("date_range") == "before_date"){
			$sql .= "time<=" . $time;
			if($time > time()) $sql .= " AND time>" . time();
		}
		else if (get_param("date_range") == "after_date")
			$sql .= "time>=" . $time;
		else if (get_param("date_range") == "on")
			$sql .= "time>=" . $time . " AND time <= " . ($time + 86400);
		else $sql .= "time>0";
		
		$limit = get_page_limit();
		$sql .= " ORDER BY time ASC {$limit};";
		$result = $DB->qry($sql);
		$total = $DB->qry("SELECT FOUND_ROWS();",3);
		search_results($result,$total,make_get_url());
	}
?>