<?php


function ruleTwoWeeks($reservefrom, $reserveto)
{
	$a = mktime(0,0,0,date("m", $reservefrom),date("d", $reservefrom)+14,date("Y", $reservefrom));
	$b = mktime(0,0,0,date("m", $reserveto),date("d", $reserveto),date("Y", $reserveto));
	
	// if the start date plus 14 days is still less than the end date, then the reservation is more than 14 days
	if($b>=$a)
		return 0;
	else
		return 1;
}


function ruleMaxThreeReservations($wycnumber)
{
	$query = "select reserveTo from calendartable where memberWYCNumber = '" . parseInput($wycnumber) . "'";
	$result = mysql_query($query)or die(mysql_error());
	$nowdate = mktime();
	$reservationCount=0;
	$check2=mysql_num_rows($result);
	for($i=0;$i<$check2;$i=$i+1)
	{
		$rdate = datetime(mysql_result($result, $i, "reserveTo"));
		if($rdate>$nowdate)
			$reservationCount=$reservationCount+1;
	}
	
	//if heshe already has 3 or more outstanding reservations, then heshe is not allowed to make more
	if($reservationCount>=3)
		return 0;
	else
		return 1;
}


function ruleNoFutureHalfDays($reservefrom, $reserveto)
{
	$now = mktime();
	$f = mktime(date("H", $reservefrom),0,0,date("m", $reservefrom),date("d", $reservefrom)-1,date("Y", $reservefrom));
	$t = mktime(date("H", $reserveto),0,0,date("m", $reserveto),date("d", $reserveto)-1,date("Y", $reserveto));
	
//	$fromIsHalfDay=0;
//	if(date("H", $reservefrom)>12)
//		$fromIsHalfDay=1;
//	$toIsHalfDay=0;
//	if(date("H", $reserveto)<12)
//		$toIsHalfDay=1;
	
	// Don't allow if reservation is more than one day away AND length of reservation less than 1 day (86400 seconds); o.w. allow
//	echo $f + "<br/>";
//	echo $t + "<br/>";
//	echo $now + "<br/>";
//	echo ($t-$f) + "<br/>";
	
	
	if($f>$now && (($t - $f) < 43200))
		return 0;
	else
		return 1;
}


function checkCompliance($wycnumber, $reservefrom, $reserveto)
{
	if(ruleTwoWeeks($reservefrom, $reserveto)==0)
	{
		echo "You cannot reserve a boat for more than two weeks at a time!<br/>";
		return 0;
	}
	if(ruleMaxThreeReservations($wycnumber)==0)
	{
		echo "You already have 3 outstanding reservations. No more than 3 are allowed!<br/>";
		return 0;
	}
	if(ruleNoFutureHalfDays($reservefrom, $reserveto)==0)
	{
		echo "You cannot make half day reservations more than a day in advance!<br/>";
		return 0;
	}
	else return 1;
}

?>
