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

include ("functions.php");

//error_reporting('0');
//ini_set('display_errors', '0');

$dbhost = 'db.washingtonyachtclub.org';
$dbuser = 'wyc';
$dbpass = 'WYC#Sail!Pass';
$database = 'production';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
@mysql_select_db($database) or die( "Unable to select database");

if(isset($_COOKIE['ID_wycnumber']))
{
	$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");
	else
	{
		$reservationId = $_REQUEST['resid'];
		if($reservationId=="")
			die("No reservation specified. <br /><br /><INPUT type=\"button\" value=\"Go back\" onClick=\"history.back()\">");

		//if we made a new comment, go in here and then proceed with rendering the page
		if (isset($_POST['submit']))
		{
			$insertcomments="insert into calendarcomment (id, userwyc, date, comment) values (".$reservationId.", ".$wycnumber.", '".date("Y-m-d G:i:s")."', '".parseInput($_POST['newcomment'])."')";
			mysql_query($insertcomments);
		}


		//see if this user is an admin
		$queryadmin = "SELECT wycnum FROM calendaradmin WHERE wycnum = '$wycnumber'";
		$checkadmin = mysql_query($queryadmin)or die(mysql_error());
		$isadmin = mysql_num_rows($checkadmin);


		$query2="select id, name, wycnumber, reservefrom, reserveto, destination, numberofcrew, comments, first, last, phone
				from calendartable, WYCDatabase, calendarboats
				where memberwycnumber=wycnumber and calendartable.cboatid=calendarboats.cboatid and id=".$reservationId.";";

		$result=mysql_query($query2);

		if(mysql_num_rows($result)==0)
			die("Not a valid Reservation number!");
		$i=0;
		$resid = mysql_result($result,$i,"id");
		$reserverWYCNumber = mysql_result($result,$i,"wycnumber");
		$rFrom = datetime(mysql_result($result,$i,"reservefrom"));
		$rTo = datetime(mysql_result($result,$i,"reserveto"));
		$fname = mysql_result($result,$i,"first");
		$lname = mysql_result($result,$i,"last");
		$phone = mysql_result($result,$i,"phone");
		$numCrew = mysql_result($result,$i,"numberofcrew");
		$destination = mysql_result($result,$i,"destination");
		$boatname = mysql_result($result,$i,"name");
		$comments = mysql_result($result,$i,"comments");

		$departTime = mktime(date('H', $rFrom),date('i', $rFrom),date('s', $rFrom), 1,1,2000);
		$returnTime = mktime(date('H', $rTo),date('i', $rTo),date('s', $rTo), 1,1,2000);
		$mTime = mktime(0,0,0, 1,1,2000);
		$aTime = mktime(12,0,0, 1,1,2000);
		$tmTime = mktime(0,0,0, 1,2,2000);

		$tFrom = date('m-d-Y', $rFrom);
		$tTo = date('m-d-Y', $rTo);

		if($departTime > $mTime && $departTime < $aTime)
		{
			$tFrom = $tFrom . " morning";
			$fromFullTime = date("Y-m-d", $rFrom) . " 03:00:00";
		}
		else if($departTime > $aTime && $departTime < $tmTime)
		{
			$tFrom = $tFrom . " afternoon";
			$fromFullTime = date("Y-m-d", $rFrom) . " 15:00:00";
		}

		if($returnTime > $mTime && $returnTime < $aTime)
		{
			$tTo = $tTo . " morning";
			$toFullTime = date("Y-m-d", $rTo) . " 03:00:00";
		}
		else if($returnTime > $aTime && $returnTime < $tmTime)
		{
			$tTo = $tTo . " afternoon";
			$toFullTime = date("Y-m-d", $rTo) . " 15:00:00";
		}

		$wycnum = $_COOKIE['ID_wycnumber'];


		//calculate # of (half)weekdays/ends
		$fromdt =  datetime($fromFullTime);
		$untildt = datetime($toFullTime);


		$idate = mktime(0,0,0,date('m', $fromdt),date('d', $fromdt)+1,date('Y', $fromdt));
		$edate = mktime(0,0,0,date('m', $untildt),date('d', $untildt),date('Y', $untildt));
		$numhalfweekdays=0;
		$numhalfweekends=0;
		$numfullweekdays=0;
		$numfullweekends=0;
		$holidays="";

		//echo strftime("%A", $rFrom) . "<br/>";
		if(strftime("%A",$fromdt)=="Saturday" || strftime("%A",$fromdt)=="Sunday" || isHoliday($fromdt))
		{
			if(isHoliday($fromdt))
			{
				$holidays=$holidays . " " . date("m-d-Y",$fromdt);
			}
			if(date('H', $fromdt)==3 && (date('H', $untildt)==15 || (date('m', $untildt)-date('m', $fromdt))!=0 || (date('d', $untildt)-date('d', $fromdt))!=0 || (date('Y', $untildt)-date('Y', $fromdt))!=0))
				$numfullweekends=$numfullweekends+1;
			else
				$numhalfweekends=$numhalfweekends+1;
		}
		else
		{
			if(date('H', $fromdt)==3 && (date('H', $untildt)==15 || (date('m', $untildt)-date('m', $fromdt))!=0 || (date('d', $untildt)-date('d', $fromdt))!=0 || (date('Y', $untildt)-date('Y', $fromdt))!=0))
				$numfullweekdays=$numfullweekdays+1;
			else
				$numhalfweekdays=$numhalfweekdays+1;
		}

		while($idate<$edate)
		{
			//echo strftime("%A", $idate) . "<br/>";
			if(strftime("%A",$idate)=="Saturday" || strftime("%A",$idate)=="Sunday" || isHoliday($idate))
			{
				if(isHoliday($idate))
				{
					$holidays=$holidays . " " . date("m-d-Y",$idate);
				}
				$numfullweekends=$numfullweekends+1;
			}
			else
			{
				$numfullweekdays=$numfullweekdays+1;
			}
			$idate = mktime(0,0,0,date('m', $idate),date('d', $idate)+1,date('Y', $idate));
		}
		if((date('m', $untildt)-date('m', $fromdt))!=0 || (date('d', $untildt)-date('d', $fromdt))!=0 || (date('Y', $untildt)-date('Y', $fromdt))!=0)
		{
			//echo strftime("%A", $rTo) . "<br/>";
			if(strftime("%A",$untildt)=="Saturday" || strftime("%A",$untildt)=="Sunday" || isHoliday($untildt))
			{
				if(isHoliday($untildt))
				{
					$holidays=$holidays . " " . date("m-d-Y",$untildt);
				}
				if(date('H', $untildt)==15)
					$numfullweekends=$numfullweekends+1;
				else
					$numhalfweekends=$numhalfweekends+1;
			}
			else
			{
				if(date('H', $untildt)==15)
					$numfullweekdays=$numfullweekdays+1;
				else
					$numhalfweekdays=$numhalfweekdays+1;
			}
		}
	}
}
else
	header("Location: login.php");
?>
<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">
				Reservation information
			</span>
		</td>
		<td style="vertical-align:top; text-align:right">
			<b>Remember to <a href="logout.php">Logout</a></b>
		</td>
		</tr>
	</table>
	<br />
	<?php echo "<a href=\"cal.php?monthyear=$rFrom\">Return to calendar</a>";  ?>
	<div style="margin-left:160px">
	<br />
		<table>
			<tr>
				<td>
					Name:
				</td>
				<td>
					<?php echo $fname . " " .$lname ?>
				</td>
			</tr>
			<tr>
				<td>
					WYC number:
				</td>
				<td>
					<?php echo $reserverWYCNumber ?>
				</td>
			</tr>
			<tr>
				<td>
					Boat:
				</td>
				<td>
					<?php echo $boatname ?>
				</td>
			</tr>
			<tr>
				<td>
					Reserve from:
				</td>
				<td>
					<?php echo "(".date("l", $rFrom).") ".$tFrom . " <a style =\"font-size:9pt\" href=\"reservationrates.php\" target=\"_blank\">(more info)</a>"; ?>
				</td>
			</tr>
			<tr>
				<td>
					Reserve through:
				</td>
				<td>
					<?php echo "(".date("l", $rTo).") ".$tTo . " <a style =\"font-size:9pt\" href=\"reservationrates.php\" target=\"_blank\">(more info)</a>"; ?>
				</td>
			</tr>
			<tr>
				<td>
					Destination:
				</td>
				<td>
					<?php echo $destination ?>
				</td>
			</tr>
			<tr>
				<td>
					Number of crew:
				</td>
				<td>
					<?php echo $numCrew ?>
				</td>
			</tr>
			<tr>
				<td>
					Contact phone:
				</td>
				<td>
					<?php echo $phone ?>
				</td>
			</tr>
			<tr>
				<td>
					Reservation number:
				</td>
				<td>
					<?php echo $resid ?>
				</td>
			</tr>
			<tr>
				<td>
					Full weekdays:
				</td>
				<td>
					<?php echo "$numfullweekdays" ?>
				</td>
			</tr>
			<tr>
				<td>
					Half weekdays:
				</td>
				<td>
					<?php echo "$numhalfweekdays" ?>
				</td>
			</tr>
			<tr>
				<td>
					Full weekends/holidays:
				</td>
				<td>
					<?php echo "$numfullweekends" ?>
				</td>
			</tr>
			<tr>
				<td>
					Half weekends/holidays:
				</td>
				<td>
					<?php echo "$numhalfweekends" ?>
				</td>
			</tr>
			<tr>
				<td>
					Included holiday dates:
				</td>
				<td>
					<?php echo "$holidays" ?>
				</td>
			</tr>
			<?php
				if($isadmin>0)
				{
					echo "<tr><td colspan=\"2\"><hr/></td?</tr><tr><td>Admin section:</td><td><a href=\"deletereservation.php?resid=$resid\">Delete this reservation</a></td></tr>";
				}
			?>

		</table>
	</div>
	<br/><br/>
	Comments:<br /><br />

	<table>
	<?php
		$querycomments = "SELECT d.Last, d.First, c.date, c.comment FROM calendarcomment c, WYCDatabase d WHERE c.userwyc = d.WYCNumber and c.id = '".$reservationId."'";
		$checkcomments = mysql_query($querycomments)or die(mysql_error());

		for($i=0;$i<mysql_num_rows($checkcomments);$i++)
		{
			echo "<tr><td>" . mysql_result($checkcomments,$i,"First") . " " . mysql_result($checkcomments,$i,"Last") . "<br/><span style=\"font-size:9pt; color:grey\">".mysql_result($checkcomments,$i,"date")."</span></td><td style=\"width:5px\"></td>";
			if($i%2==1)
				echo "<td>".mysql_result($checkcomments,$i,"comment")."</td></tr>";
			else
				echo "<td style=\"width: 500px; background-color: #eeeeee\">".mysql_result($checkcomments,$i,"comment")."</td></tr>";
		}
	?>
	</table>
	</br>
	<form name="form1" action="<?php echo $_SERVER['PHP_SELF'] . "?resid=" . $reservationId ?>" method="post">
		<textarea name="newcomment" rows="2" cols="50" ></textarea>
		<input type="submit" name="submit" value="Post New Comment">&nbsp;<span style="font-size:9pt">(Limit 63996 characters, no formating is kept)</span>
	</form>

<br/>
<?php echo "<a href=\"cal.php?monthyear=$rFrom\">Return to calendar</a>";  ?>
<br/>

</body>
</html>
