<?php
	///////////// Calendar Code ///////////////////////////////////////////
	// EB Content Management System
	// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
	// Revised: 6/15/2007
	// Revised: 8/6/2007	Added pagebreak div.
        // Revised: 8/1/2009  ELL
        // Added class hooks for styling.
	//
	include "../../connect/db_connection.php";

	$month_count = 0;
	$month = array();
	$day = array();
	$year = array();
	$Description = array();
	$EventDate = array();
	$StartTime = array();
	$EndTime = array();
        $URL = array();
//	$HebrewDate = array();
	$query = "SELECT * FROM schedule WHERE `Active` = 1 ORDER BY `EventDateSort`";
	$result = mysql_query($query);
	$num_results = mysql_num_rows($result);

	for ($i = 0; $i < $num_results; $i++)
	{
		$row = mysql_fetch_array($result);
		$Description[$i] = stripslashes($row["Description"]);
		$EventDate[$i] = $row["EventDate"];
		$StartTime[$i] = $row["StartTime"];
		$EndTime[$i] = $row["EndTime"];
		$URL[$i] = $row["URL"];
//		$HebrewDate[$i] = $row["HebrewDate"];
//		$month[$i] = date("n", $EventDate[$i]);
//		$day[$i] = date("j", $EventDate[$i]);
//		$year[$i] = date("Y", $EventDate[$i]);
		list($month[$i], $day[$i], $year[$i]) = split('[/.-]', $EventDate[$i]);
		if ($month[$i] != $month[$i-1])
			$month_count++; 
	}

	//////////////////////////////////////////////////
	$m = 0;
for ($k = 0; $k < $month_count; $k++)
{
	$current_month = $month[$m];
	$current_year = $year[$m];
	
	$start_timestamp = mktime(0, 0, 0, $month[$m], 1, $year[$m]);
	$on_the_1st_day_of_week = date("w", $start_timestamp) + 1;	// 1 = Sunday, 7 = Saturday
	$days_in_month = date("t", $start_timestamp);
	$month_name = date("F", $start_timestamp);
	$year_string = date("Y", $start_timestamp);
	
	// Get Jewish date
	$julian_date = gregoriantojd($current_month, 1, $current_year);
	$jewishdate = jdtojewish($julian_date);
	// Find the Jewish calendar date and parse out the day and year values
	list ($jc_month, $jc_day, $jc_year) = explode ('/', $jewishdate);
	// Get Jewish month name
	$jc_month_string = jdmonthname ($julian_date, 4);
	// Get second Jewish date
	$julian_date_2 = gregoriantojd($current_month, $days_in_month, $current_year);
	$jewishdate_2 = jdtojewish($julian_date_2);
	// Find the Jewish calendar date and parse out the day and year values
	list ($jc_month_2, $jc_day_2, $jc_year_2) = explode ('/', $jewishdate_2);
	// Get Jewish month name
	$jc_month_string_2 = jdmonthname ($julian_date_2, 4);
	// In case this month spans the Jewish year, make year adjustment for heading.
	if ($jc_year != $jc_year_2)
		$jc_year = $jc_year . " - " . $jc_year_2;

?>

<DIV CLASS="pagebreak">
<div class="calendar_div">
<table border="1" class="calendar_table">
<tr bgcolor="#CCCCCC" class="calendar_tr_head">
	<td colspan="7" align="center" class="calendar_td_head"><b><font size="4"><?php echo "$month_name, $year_string<br><font color='#000099'>$jc_month_string - $jc_month_string_2, $jc_year</font>"; ?></font></b></td>
</tr>
<tr bgcolor="#CCCCCC" class="calendar_tr_head2">
	<td align="center" width="100" class="calendar_td_head2"><b>S</b></td>
	<td align="center" width="100" class="calendar_td_head2"><b>M</b></td>
	<td align="center" width="100" class="calendar_td_head2"><b>T</b></td>
	<td align="center" width="100" class="calendar_td_head2"><b>W</b></td>
	<td align="center" width="100" class="calendar_td_head2"><b>T</b></td>
	<td align="center" width="100" class="calendar_td_head2"><b>F</b></td>
	<td align="center" width="100" class="calendar_td_head2"><b>S</b></td>
</tr>
<tr class="calendar_tr">
<?php
	$got_1st_day = false;
	$got_last_day = false;
	$j = 1;
	for ($i = 1; $i < 43; $i++)
	{
		if ($i == $on_the_1st_day_of_week)
			$got_1st_day = true;
			
		echo " <td height='100' align='left' valign='top' class="calendar_td">";
		if (($got_1st_day == true) && ($got_last_day == false))
		{
			// Get Jewish date
			$julian_date = gregoriantojd($current_month, $j, $current_year);
			$jewishdate = jdtojewish($julian_date);
			// Find the Jewish calendar date and parse out the day and year values
			list ($jc_month, $jc_day, $jc_year) = explode ('/', $jewishdate);
			// Find the Jewish calendar month name
			$jc_month = jdmonthname ($julian_date, 4);
			echo "$j &nbsp;&nbsp;&nbsp; <font color='#000099'>$jc_day $jc_month</font>";	// Print day.
			/////////////////////////////////////////////////////////
			// Print event if one exists for this day.
			while (($day[$m] == $j) && ($month[$m] == $current_month))
			{
				if ($URL[$m])
					echo "<br><br><font size='2'><a href='$URL[$m]'>$Description[$m]<br>$StartTime[$m] - $EndTime[$m]</a></font>";
				else
					echo "<br><br><font size='2'>$Description[$m]<br>$StartTime[$m] - $EndTime[$m]</font>";
				$m++;
			}
			/////////////////////////////////////////////////////////
			$j++;
			if ($j > $days_in_month)
				$got_last_day = true;
			
		}
		else
			echo "&nbsp;";
		echo "</td>";
		
		if (($i == 35) && ($got_last_day == true))	//All done, don't need another row.
			$i = 50;
		else if (!($i % 7))	//If Saturday, start new row.
			echo "</tr><tr>";
	}
?>
</tr>
</table>
<br>
</div>
</DIV>

<?php
}	//month count
///////////////////////////////////////////////////////////////////////////////////////////////////
?>
