<?php
header( "Cache-Control: no-cache, must-revalidate" );
header( "Pragma: no-cache" );

include("functions.php");

if(!isset($_COOKIE['ID_wycnumber']))
{
	header("Location: login.php");
	exit();
}
else
{
	$dbhost = 'db.washingtonyachtclub.org';
	$dbuser = 'wyc';
	$dbpass = 'WYC#Sail!Pass';
	$database = 'production';

	$conn = mysql_connect($dbhost, $dbuser, $dbpass);
	@mysql_select_db($database) or die(mysql_error());

	$wycnumber = $_COOKIE['ID_wycnumber'];
	$password = $_COOKIE['Key_site'];


	$query = "SELECT * FROM WYCDatabase WHERE wycnumber = '$wycnumber' and password = PASSWORD('$password')";
	$check = mysql_query($query) or die(mysql_error());

	$check2 = mysql_num_rows($check);
	if ($check2 == 0)
	{
		header("Location: login.php");
	}
	$defaultPhoneNumber = mysql_result($check,0,"phone1");
	$first = mysql_result($check,0,"first");
	$last = mysql_result($check,0,"last");

	$hour = time() + 3600;
	setcookie(ID_wycnumber, $wycnumber, $hour);
	setcookie(Key_site, $password, $hour);


	if (isset($_POST['cancel']))
	{
		header("Location: cal.php");
		die();
	}

	$check4 = getAllowReservations($_SERVER['REMOTE_ADDR']);
	if ($check4 == 0 | $is_guest)
		$allowReservation=0;
	else
		$allowReservation=1;


	if($allowReservation)
	{

		//check that the user entered all the required data
		if ($_POST['boatname']=="Choose a boat")
			die("Whoops! You didn't choose a boat to reserve.<br/><br/><input type=\"button\" value=\"Go Back\" onClick=\"history.back()\"/>");
		if(!$_POST['from'])
			die("Whoops! You didn't enter a departure date.<br/><br/><input type=\"button\" value=\"Go Back\" onClick=\"history.back()\"/>");
		if(!$_POST['rFrom'])
			die("Whoops! You didn't enter a departure time.<br/><br/><input type=\"button\" value=\"Go Back\" onClick=\"history.back()\"/>");
		if(!$_POST['until'])
			die("Whoops! You didn't enter a return date.<br/><br/><input type=\"button\" value=\"Go Back\" onClick=\"history.back()\"/>");
		if(!$_POST['rUntil'])
			die("Whoops! You didn't enter a return time.<br/><br/><input type=\"button\" value=\"Go Back\" onClick=\"history.back()\"/>");
		if(!$_POST['destination'])
			die("Whoops! You didn't enter a destination.<br/><br/><input type=\"button\" value=\"Go Back\" onClick=\"history.back()\"/>");
		if(!$_POST['phone'])
			die("Whoops! You didn't enter a phone number.<br/><br/><input type=\"button\" value=\"Go Back\" onClick=\"history.back()\"/>");

		if(!parseInput($_POST['numcrew']))
			$numcrew = 0;
		else
			$numcrew = parseInput($_POST['numcrew']);

		$comments = parseInput($_POST['comments']);


		$incorrectpassword=0;
		if (isset($_POST['continue']))
		{
			if($password == $_POST['passwordconfirm'] && $_POST['passwordconfirm'])
			{
				header("Location: reserve.php?boatname=".parseInput($_POST['boatname'])."&from=".parseInput($_POST['from'])."&rFrom=".parseInput($_POST['rFrom'])."&until=".parseInput($_POST['until'])."&rUntil=".parseInput($_POST['rUntil'])."&destination=".parseInput($_POST['destination'])."&phone=".parseInput($_POST['phone'])."&numcrew=".$numcrew."&comments=".$comments);
				die();
			}
			else
			{
				$incorrectpassword=1;
			}
		}

	//else block closed below to encapuslate following html code
	?>


	<html>
	<head>
		<title>Washington Yacht Club Calendar</title>
		<link rel="stylesheet" type="text/css" href="css/wyc.css">
	</head>
	<body>
		<table style="width:100%">
			<tr>
			<td style="vertical-align:top; text-align:left; width:150px">
				<a href="http://www.washingtonyachtclub.com"><img src="images/burgee_purple.gif" alt="burgee_purple" /></a>
			</td>
			<td style="vertical-align:top">
				<span style="font-size:30">Washington Yacht Club</span>
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
				<span style="font-size:18">Keelboat availability</span>
				<br />
				<span style="font-size:9pt">
					Confirm your reservation details, re-enter your password, and click Finalize Reservation at the bottom (scroll down).
				</span>
			</td>
			<td style="vertical-align:top; text-align:right; width:250px">
				<b>Remember to <a href="logout.php">Logout</a></b>
			</td>
			</tr>
		</table>
		<br/><br/>
		<span style="color:red">WARNING:</span> This is your last chance to back out of this reservation. If you continue with your reservation, club policy is to not allow refunds. The only way out would be to find a friend who can take your spot outside of this system.
		<br/><br/>

		Should you wish to continue, here are the reservation details you specified:<br /><br />
		<form name="form1" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
		<table style="border: solid 1px black; background-color:#dddddd">
			<tr>
				<td>
					Selected boat:
				</td>
				<td>
					<input type="hidden" name="boatname" value="<?php echo parseInput($_POST['boatname']) ?>" />
					<?php echo parseInput($_POST['boatname']) ?>
				</td>
			</tr>
			<tr>
				<td style="vertical-align:top">
					Reserve from:
				</td>
				<td>
					<?php echo "(".date("l",stringToDate($_POST['from'], mktime(), 'Error')).") "; ?>
					<input type="hidden" value="<?php echo $_POST['from'] ?>" name="from" READONLY/>
					<?php echo $_POST['from'] ?>
					<input type="hidden" name="rFrom" checked value="<?php echo $_POST['rFrom'] ?>" />
					<?php echo $_POST['rFrom'] . " <a style =\"font-size:9pt\" href=\"reservationrates.php\" target=\"_blank\">(more info)</a>" ?>
				</td>
			</tr>
			<tr>
				<td style="vertical-align:top">
					Reserve through:
				</td>
				<td>
					<?php echo "(".date("l",stringToDate($_POST['until'], mktime(), 'Error')).") "; ?>
					<input type="hidden" value="<?php echo $_POST['until'] ?>" name="until" READONLY/>
					<?php echo $_POST['until'] ?>
					<input type="hidden" name="rUntil" checked value="<?php echo $_POST['rUntil'] ?>" />
					<?php echo $_POST['rUntil'] . " <a style =\"font-size:9pt\" href=\"reservationrates.php\" target=\"_blank\">(more info)</a>" ?>
				</td>
			</tr>
			<tr>
				<td>
					Destination:
				</td>
				<td>
					<?php echo $_POST['destination'] ?>
					<input type="hidden" name="destination" value = "<?php echo parseInput($_POST['destination']) ?>" READONLY/>
				</td>
			</tr>
			<tr>
				<td>
					Number of crew:
				</td>
				<td>
					<?php echo $numcrew ?>
					<input type="hidden" name="numcrew" value = "<?php echo parseInput($numcrew) ?>" READONLY/>
				</td>
			</tr>
			<tr>
				<td>
					Contact phone:
				</td>
				<td>
					<?php echo $_POST['phone'] ?>
					<input type="hidden" name="phone" value = "<?php echo parseInput($_POST['phone']) ?>" READONLY/>
				</td>
			</tr>
			<tr>
				<td style=\"vertical-align:top\">
					Comments:
				</td>
				<td>
					<?php echo $comments ?>
					<input type="hidden" name="comments" value = "<?php echo $comments ?>" READONLY/>
				</td>
			</tr>
		</table>
		<br />
		To change any of this information, go back by clicking here:
		<input type="button" value="Go Back" onClick="history.back()"/>
		<br/><br/>

			Re-enter password for WYC#<?php echo $wycnumber ?>: <input type="password" name="passwordconfirm" />
			Not your WYC#? <a href="logout.php">Logout</a>
			<?php
				if($incorrectpassword)
					echo "<span style=\"color:red\">Incorrect password!</span>";
			?>
			<br/><br/>
			<center>
			<input type="submit" name="continue" value="Finalize Reservation" />
			<input type="button" name="cancel" value="Cancel" onclick="window.location='cal.php'" />
			</center>
		</form>
	</html>

	<?php

	}
	else
	{
		die ('You are only allowed to reserve from the sail locker!');
	}
}

?>
