<?php
require("calendar/common.php");

$LocationID = $_GET["LocationID"];
$min = $_GET["min"];
$add = $_GET["add"];
$keyword = $_GET["keyword"];

// Get the location query string
$LocQuery = getLocQuery($LocationID);

// Get the WebAlias for the location
$WebAlias = getWebAlias($LocationID);

// figure out the proper week to show
$todaysNumber = date("w",mktime());

// if we're subtracting days, we need to take a larger number away from the base date
if ($min) {
	$todaysNumber = (7 * $min) + $todaysNumber;
}

// if we're adding, we need to subtract a negative number of days from the base
// in other words, we need to add, but I'm lazy and didn't want to write a
// whole new set of base values for the condition. Whay should I have to?
if ($add) {
	$todaysNumber = ($todaysNumber - (7 * $add));
}

$querySunday = date("Y-m-d", mktime(0,0,0,date("m"), (date("d") - $todaysNumber), date("Y")));
$querySaturday = date("Y-m-d", mktime(0,0,0,date("m"), ((date("d") - $todaysNumber) + 6), date("Y")));
$showSunday = date("n\/j\/Y", mktime(0,0,0,date("m"), (date("d") - $todaysNumber), date("Y")));
$showSaturday = date("n\/j\/Y", mktime(0,0,0,date("m"), ((date("d") - $todaysNumber) + 6), date("Y")));

// we need a week navigation bar  
// if we are at the default week
if (!$min AND !$add) {
	$var1 = "min";
	$val1 = "1";
	$var2 = "add";          
	$val2 = "1";            
        
// if we've subtracted weeks
} elseif ($min) {
	$var1 = "min";                          
	$val1 = $min + 1;
	$var2 = "min";
	$val2 = $min - 1;       
                                
// if we've added weeks
} elseif ($add) {       
	$var1 = "add"; 
	$val1 = $add - 1;
	$var2 = "add";
	$val2 = $add + 1;
}

// see if we're using IE
if (ereg(  "MSIE ([0-9]+)",$HTTP_USER_AGENT,$version)) {
        $ver=(int)$version[1];
}


if ($ver >= 4) {
	expandItems();
}

popUp(500,375);

echo "<table cellpadding=0 cellspacing=0 width=99% border=0>";

// set the view, figure out the number of days in the month and the weekday of the first
$showMonth = date("F Y", mktime(0,0,0,$Month,1,$Year));
$numDays = date("t",mktime(0,0,0,$Month,1,$Year));
$startDay = date("w",mktime(0,0,0,$Month,1,$Year));

// set an array with they names of the days of week
$days = array(
"0" => "Sunday",
"1" => "Monday",
"2" => "Tuesday",
"3" => "Wednesday",
"4" => "Thursday",
"5" => "Friday",
"6" => "Saturday");

echo "<tr><td align=\"right\"><form action=\"\" method=\"get\"><input type=\"text\" name=\"keyword\" size=10 value=\"$keyword\">&nbsp;<input type=\"submit\" value=\"Search\"></td></form></tr>";

echo "
<tr><td align=\"left\">
<a href=\"$PHP_SELF?LocationID=$LocationID&$var1=$val1\">&lt;&lt;Previous Week</a>
&nbsp;&nbsp;&nbsp;<b>$showSunday - $showSaturday</b>&nbsp;&nbsp;&nbsp;
<a href=\"$PHP_SELF?LocationID=$LocationID&$var2=$val2\">Next Week&gt;&gt;</a>
</td></tr>
";
echo "<tr><td height=5><p>&nbsp;</p></td></tr>";

// start the table
?>
<tr><td>
<table cellpadding="3" cellspacing="0" bgcolor="#FFFFFF" frame=void width=100%>

<?php
$split = explode("-",$querySunday);
$qsYear = $split[0];
$qsMonth = $split[1];
$qsDay = $split[2];

for ($i = 0; $i <= 6; $i++) 
{
	$qr = 0;
	$q = ($qsDay + $i);

	$Month = date("m", mktime(0,0,0,$qsMonth,$q,$qsYear));
	$Year = date("Y", mktime(0,0,0,$qsMonth,$q,$qsYear));
	$Day = date("d", mktime(0,0,0,$qsMonth,$q,$qsYear));
	$showDay = date("M jS", mktime(0,0,0,$Month,$Day,$Year));
	
	$weeknumber = date("l", mktime(0,0,0,$Month,$Day,$Year));

	// this is the query results counter
	$qr = 0;

	echo "<tr bgcolor=\"#FFFFFF\">";
	echo "<td nowrap valign=\"top\" style=\"border-bottom:";
	if ($i != 6)
	{
		echo " 1 ";
	}
	else
	{
		echo " 0 ";
	}
	echo "solid #993300;\" >";
	echo "<table border=0 width=100% height=100% cellspacing=0 cellpadding=5>";
	echo "<tr><td width=100 valign=\"top\">";
	echo "<table cellspacing=0 cellpadding=5><tr><td><b>$showDay</b></td></tr><tr><td><b>$weeknumber</b></td></tr></table>";
	echo "</td><td>";

	$queryDate = $Year."-".$Month."-".$Day;
	$todaysDay = date("w", mktime(0,0,0,$Month, $Day, $Year));

	// how many of today have happened this month? assume at least one
	$numOfTodays = 1;

	if ( date("m", mktime(0,0,0,$Month, $Day, $Year)) == date("m", mktime(0,0,0,$Month, ($Day - 7), $Year)) ) 
	{
		$numOfTodays = 2;
	}
	if ( date("m", mktime(0,0,0,$Month, $Day, $Year)) == date("m", mktime(0,0,0,$Month, ($Day - 14), $Year)) ) 
	{
		$numOfTodays = 3;
	}
	if ( date("m", mktime(0,0,0,$Month, $Day, $Year)) == date("m", mktime(0,0,0,$Month, ($Day - 21), $Year)) ) 
	{
		$numOfTodays = 4;
	}
	if ( date("m", mktime(0,0,0,$Month, $Day, $Year)) == date("m", mktime(0,0,0,$Month, ($Day - 28), $Year)) ) 
	{
		$numOfTodays = 5;
	}

	// we need to pre-query and then sort
	// query the CalendarDaily table
	$result = mysql("$DBName","SELECT CalendarDetailsID FROM phpcalendar_daily WHERE ".$LocQuery." AND Active = '1' AND ( ('$queryDate' = StartDate AND StopDate = '0000-00-00') OR (StopDate != '0000-00-00' AND '$queryDate' BETWEEN StartDate AND StopDate) )") or die(mysql_error());
	while ($row = mysql_fetch_row($result)) 
	{
		$gotResults[$qr] = $row[0];
		$qr++;
	}

	// query the CalendarWeekly table
	$result = mysql("$DBName","SELECT CalendarDetailsID, DaysOfWeek FROM phpcalendar_weekly	WHERE ".$LocQuery." AND Active = '1' AND ( ('$queryDate' >= DisplayStart AND DisplayStop = '0000-00-00') OR (DisplayStop != '0000-00-00' AND '$queryDate' BETWEEN DisplayStart AND DisplayStop) )") or die(mysql_error());
	while ($row = mysql_fetch_row($result)) 
	{
		$CalendarDetailsID = $row[0];
		$DaysOfWeek = $row[1];

		if ($DaysOfWeek == $todaysDay) 
		{
			$gotResults[$qr] = $CalendarDetailsID;
			$qr++;
		} 
		else 
		{
			$split = explode("|", $DaysOfWeek);
			for ($x = 0; $split[$x]; $x++) 
			{
				if ($split[$x] == $todaysDay) 
				{
					$gotResults[$qr] = $CalendarDetailsID;
					$qr++;
				}
			}
			while ($split[0]) 
			{
				array_pop($split);
			}
		}
	}


	// query the CalendarMonthly table
	$result = mysql("$DBName","SELECT CalendarDetailsID FROM phpcalendar_monthly WHERE ".$LocQuery." AND Active = '1' AND DayOfMonth = '$q' AND ( ('$queryDate' >= DisplayStart AND DisplayStop = '0000-00-00') OR (DisplayStop != '0000-00-00' AND '$queryDate' BETWEEN DisplayStart AND DisplayStop) )") or die(mysql_error());
	while ($row = mysql_fetch_row($result)) 
	{
		$gotResults[$qr] = $row[0];
		$qr++;
	}

	// query the CalendarYearly table
	$result = mysql("$DBName","SELECT CalendarDetailsID FROM phpcalendar_yearly WHERE LocationID = '$LocationID' AND Active = '1' AND DayOfMonth = '$q' AND Month = '$Month' AND ( ('$queryDate' >= DisplayStart AND DisplayStop = '0000-00-00') OR (DisplayStop != '0000-00-00' AND '$queryDate' BETWEEN DisplayStart AND DisplayStop) )") or die(mysql_error());
	while ($row = mysql_fetch_row($result)) 
	{
		$gotResults[$qr] = $row[0];
		$qr++;
	}

	// query the CalendarPeriodicalMonthly table
	$result = mysql("$DBName","SELECT CalendarDetailsID, WeekDays FROM phpcalendar_periodicalmonthly WHERE ".$LocQuery." AND Active = '1' AND WeekNumber = '$numOfTodays' AND ( ('$queryDate' >= DisplayStart AND DisplayStop = '0000-00-00') OR (DisplayStop != '0000-00-00' AND '$queryDate' BETWEEN DisplayStart AND DisplayStop) )") or die(mysql_error());
	while ($row = mysql_fetch_row($result)) 
	{
		$CalendarDetailsID = $row[0];
		$WeekDays = $row[1];

		if ($WeekDays == $todaysDay) 
		{
			$gotResults[$qr] = $CalendarDetailsID;
			$qr++;
		} 
		else 
		{
			$split = explode("|", $WeekDays);
			for ($x = 0; $split[$x]; $x++) 
			{
				if ($split[$x] == $todaysDay) 
				{
					$gotResults[$qr] = $CalendarDetailsID;
					$qr++;
				}
			}
			while ($split[0]) 
			{
				array_pop($split);
			}
		}
	}

	// query the CalendarPeriodicalYearly table
	$result = mysql("$DBName","SELECT CalendarDetailsID, WeekDays FROM phpcalendar_periodicalyearly WHERE ".$LocQuery." AND Active = '1' AND WeekNumber = '$numOfTodays' AND Month = '$Month' AND ( ('$queryDate' >= DisplayStart AND DisplayStop = '0000-00-00') OR (DisplayStop != '0000-00-00' AND '$queryDate' BETWEEN DisplayStart AND DisplayStop) )") or die(mysql_error());
	while ($row = mysql_fetch_row($result)) 
	{
		$CalendarDetailsID = $row[0];
		$WeekDays = $row[1];

		if ($WeekDays == $todaysDay) 
		{
			$gotResults[$qr] = $CalendarDetailsID;
			$qr++;
		} 
		else 
		{
			$split = explode("|", $WeekDays);
			for ($x = 0; $split[$x]; $x++) 
			{
				if ($split[$x] == $todaysDay) 
				{
					$gotResults[$qr] = $CalendarDetailsID;
					$qr++;
				}
			}
			while ($split[0]) 
			{
				array_pop($split);
			}
		}
	}


	for ($g = 0; $gotResults[$g]; $g++) 
	{
		$query .= " OR CalendarDetailsID = '$gotResults[$g]'";
	}

	while ($gotResults[0]) 
	{
		array_pop($gotResults);
	}

	//echo "lalala $query lalala";
	if ($query) 
	{
		echo "<table cellspacing=0 border=0 cellpadding=5 width=100%>";
		$query = "(".ereg_replace("^ OR ","",$query).")";
		
		$detailQuery = "SELECT Title, CalendarDetailsID, StartTime, StopTime FROM phpcalendar_details";
		$detailQuery .= " WHERE ";
		if (isset($keyword))
		{
			$detailQuery .= "(Title like '%$keyword%' or Details like '%$keyword%') and ";
		}
		$detailQuery .= " $query ORDER BY StartTime, StopTime";
//		echo $detailQuery;
		$result = mysql("$DBName", "$detailQuery");
		
		while ($row = mysql_fetch_row($result)) 
		{
			$CDTi = $row[0];
			$CDCI = $row[1];
			$CDKT = $row[2];
			$CDJT = $row[3];

			// See if there is an exception
			if (isException($CDCI, $queryDate) > 0) 
			{
				continue;
			}

			$len = strlen($CDTi);

			if ($len > 99) {
				$showName = substr($CDTi,0,96)."...";
			} else {
				$showName = $CDTi;
			}
			//echo "<br>\n";
			showEvent($CDCI, "$showName", $queryDate, $CDKT, $CDJT);
		}
		echo "</table>";
	}

	$query = "";

	echo "</tr></table></td></tr>";
	$j++;
}

?>
</table>

</td></tr></table>

<?php
function showEvent($ID, $AbbrTitle, $Date, $KS, $JS) {
	$AbbrTitle = htmlspecialchars($AbbrTitle);
	
	$split = explode("-",$Date);
	$split2 = explode(":",$KS);
	$showStart = date("g:iA", mktime($split2[0],$split2[1],0,$split[1],$split[2],$split[0]));
	
	$split3 = explode(":",$JS);
	$showEnd = date("g:iA", mktime($split3[0],$split3[1],0,$split[1],$split[2],$split[0]));
	
	if ($showStart) {
	$showTime = $showStart;
		if ($showEnd) {
		$showTime .= " - $showEnd";
		}
	}
	
	echo "<tr>";

	echo "<td width=130 valign=\"top\" nowrap><table border=0 width=100% cellpadding=0 cellspacing=0><tr><td nowrap>$showTime</td></tr></table></td>";
	echo "<td><a href=\"calendar/event.php?ID=$ID&Date=$Date\" onClick=return(openSmallWindow('calendar/event.php?ID=$ID&Date=$Date'))><font face=verdana size=2>$AbbrTitle</font></a></td>";
	echo "</tr>";
}
?>
