<?php 
date_default_timezone_set('America/Los_Angeles');

include('includes/find_cal.php');  // Sets $tbl_name and $_COOKIE.
define('BASE', './');
$viewpage = "embedmonth.php";
$edpage = "cal/embedcal.php";

include('userauth.php');
if ( $editor ) {
	// header("Location: " . HOMEFOLDER . "events/$edpage"); /* redirect to editing page */
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<?php
$show_weekend = (isset($_GET['weekend'])) ? '&weekend=1' : '';

$filtstring = '';
if (isset($_COOKIE['myfilter'])) {
//	setcookie( 'myfilter', $filt,182*24*3600000,'/' ); //extend cookie another 1/2 year (headers already sent?)
	$filt = $_COOKIE['myfilter'];
	if (($filt > 0)) {
		$filtstring = 'and (';
		for ($i=1; $i <= 12; $i++) {
				$j = 1; // check bits 1 - 10
				$j = $j << $i-1; 
			  if (($filt & $j) == $j) {
			  	$filtstring .= "(evtype & $j = $j) or ";
			  }
		} // for
	}
	if ($filtstring <> '') $filtstring .= 'isnull(evtype))'; //tack on holidays
}

include('functions/dates.php');

require '../scripts/create_connection.php';
$dbc = create_dbc('selectonly', 'engl');

# source: http://firsttube.com/read/sorting-a-multi-dimensional-array-with-php/
# o needed for recurring events, dtlink advances but dtstart stays the same
# o dtlink and *time* of dtstart must be compared for sorting
function subval_sort($a,$subkey,$subkey2) {
	foreach($a as $k=>$v) {
		$b[$k] = strtolower($v[$subkey] . substr($v[$subkey2],10));
	}
	asort($b);
	foreach($b as $key=>$val) {
		$c[] = $a[$key];
	}
	return $c;
}

# adopt any filters filter form or filter cookie

# process filter form (if this page called by it



$calendar_name	= 'English Dept.';

# rw	from english.inc.php
$day_lang			= 'Day';
$week_lang			= 'Week';
$month_lang			= 'Month';
$year_lang			= 'Year';
$calendar_lang		= 'Calendar';
$next_day_lang		= 'Next Day';
$next_month_lang	= 'Next Month';
$next_week_lang		= 'Next Week';
$next_year_lang		= 'Next Year';
$last_day_lang		= 'Previous Day';
$last_month_lang	= 'Previous Month';
$last_week_lang		= 'Previous Week';
$last_year_lang		= 'Previous Year';

// $format_recur, items enclosed in % will be substituted with variables
$format_recur_lang['delimiter']	= ', ';								// ie, 'one, two, three'

$format_recur_lang['yearly']		= array('year','years');		// for these, put singular
$format_recur_lang['monthly']		= array('month','months');		// and plural forms
$format_recur_lang['weekly']		= array('week','weeks');		// these will be %freq%
$format_recur_lang['daily']			= array('day','days');			// in the replacement below
$format_recur_lang['hourly']		= array('hour','hours');
$format_recur_lang['minutely']		= array('minute','minutes');
$format_recur_lang['secondly']		= array('second','seconds');

$format_recur_lang['start']			= 'Every %int% %freq% %for%';	// ie, 'Every 1 day until January 4' or 'Every 1 day for a count of 5'
$format_recur_lang['until']			= 'until %date%';				// ie, 'until January 4'
$format_recur_lang['count']			= 'for a count of %int%';		// ie, 'for 5 times'

$format_recur_lang['bymonth']		= 'In months: %list%';			// ie, 'In months: January, February, March'
$format_recur_lang['bymonthday']	= 'On dates: %list%';			// ie, 'On dates: 1, 2, 3, 4'
$format_recur_lang['byday']			= 'On days: %list%';			// ie, 'On days: Mon, Tues, Wed, Thurs'
$daysofweek_lang			= array ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$daysofweekshort_lang		= array ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
$daysofweekreallyshort_lang	= array ('S','M','T','W','T','F','S');
$monthsofyear_lang			= array ('January','February','March','April','May','June','July','August','September','October','November','December');
$monthsofyearshort_lang		= array ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

// For time formatting, check out: http://www.php.net/manual/en/function.date.php
$timeFormat = 'g:i A';
$timeFormat_small = 'g:i';

// For date formatting, see note below
$dateFormat_day = '%A, %B %e';
$dateFormat_week = '%B %e';
$dateFormat_week_list = '%a, %b %e';
$dateFormat_week_jump = '%b %e';
$dateFormat_month = '%B %Y';
$dateFormat_month_list = '%A, %B %e';

# end of english.inc.php

# rw	from config.inc.php
//$style_sheet					= 'contrast';	// higher contrast scheme
$style_sheet 			= 'gold';			// Themes support - silver, gold, contrast
$language 				= 'English';		// Language support - 'English', 'Polish', 'German', 'French', 'Dutch', 'Danish', 'Italian', 'Japanese', 'Norwegian', 'Spanish', 'Swedish', 'Portuguese', 'Catalan', 'Traditional_Chinese', 'Esperanto'
$week_start_day 		= 'Sunday';			// Day of the week your week starts on
$day_start 				= '0800';			// Start time for day grid
$day_end				= '2000';			// End time for day grid
$gridLength 			= '15';				// Grid distance in minutes for day view, multiples of 15 preferred
$default_path			= 'http://rob-home1/calendar'; 	// The HTTP URL to the PHP iCalendar directory, ie. http://www.example.com/phpicalendar
$second_offset			= '0';				// The time in seconds between your time and your server's time.

# end of config.inc.php

//if (isset($_POST['month'])) {
//	$getdate=$_POST['year'] . $_POST['month'] . '01';
//	$pdate = $getdate;
//} else {
# rw	taken from init.inc.php--the bulk of which is specific to ical setup
	if (isset($_GET['getdate'])) {
//	if (isset($HTTP_GET_VARS['getdate']) && ($HTTP_GET_VARS['getdate'] !== '')) {
		$getdate = $_GET['getdate'];
	} else {
		$getdate = date('Ymd', strtotime("now + $second_offset seconds"));
	}
//}

# rw	preg_match is trying to extract yyyy mm dd from $getdate and store
# the results in $day_array2[1], [2], and [3] (year, month, day, respectively)
preg_match ("/^([0-9]{4})([0-9]{2})([0-9]{2})$/", $getdate, $day_array2);
$this_day 				= $day_array2[3]; 
$this_month 			= $day_array2[2];
$this_year 				= $day_array2[1];

# rw $cur_year used to display list of years +/- 2 from current year in drop-down list
# regardless of the requested year, reflected in getdate
$cur_year				= date('Y',strtotime("now + $second_offset seconds"));

# rw	find yyyymmdd dates for yesterday, today, and tomorrow
# rw	convert today as UNIX timestamp (measured in seconds from Jan 1 1970)
$unix_time 				= strtotime($getdate); 
# rw	yyyymmdd returned from UNIX timestamp for today, tomorrow, yesterday
$today_today 			= date('Ymd', strtotime("now + $second_offset seconds")); 
$tomorrows_date 		= date( "Ymd", strtotime("+1 day",  $unix_time));
$yesterdays_date 		= date( "Ymd", strtotime("-1 day",  $unix_time));

// find out next month
$next_month_month 		= ($this_month+1 == '13') ? '1' : ($this_month+1);
$next_month_day 		= $this_day;
$next_month_year 		= ($next_month_month == '1') ? ($this_year+1) : $this_year;
# rw	if today is bad next month (e.g.: 1/31, but no 2/31), seek backwards until good date
#	checkdate(mdy:int) returns true/false
while (!checkdate($next_month_month,$next_month_day,$next_month_year)) 	$next_month_day--;
# rw	convert to UNIX timestamp (seconds)
$next_month_time 		= mktime(0,0,0,$next_month_month,$next_month_day,$next_month_year);

// find out last month
$prev_month_month 		= ($this_month-1 == '0') ? '12' : ($this_month-1);
$prev_month_day 		= $this_day;
$prev_month_year 		= ($prev_month_month == '12') ? ($this_year-1) : $this_year;
# rw	check today in previous month, seek backwards for good date if needed
while (!checkdate($prev_month_month,$prev_month_day,$prev_month_year)) $prev_month_day--;
# rw	mktime also returns UNIX timestamp 
$prev_month_time 		= mktime(0,0,0,$prev_month_month,$prev_month_day,$prev_month_year);

$next_month 			= date("Ymd", $next_month_time);
$prev_month 			= date("Ymd", $prev_month_time);
$display_date 			= localizeDate ($dateFormat_month, $unix_time);
# rw	$parse_month used in calendar loop to know which dates to retrieve
$parse_month 			= date ("Ym", $unix_time);

# rw	find the calendar date for the Sunday prior to (or equal to) the first 1st day of current month
# if $show_weekend is false, find the prior Monday instead,
# however, if the 1st falls on a Sat or Sun, must move date upward to Monday
#	eg: the week of 12/1/03 starts with Sunday, Nov 30, 2003
# 		so, dateOfWeek("20031201","sun") returns "20031130"
$fstday = '01';
$first_of_month 		= $this_year.$this_month.$fstday;
if ($show_weekend=='') {
	$week_start_day = 'Monday';
	$s = date('D',strtotime($first_of_month));
	if (substr($s,0,1) == 'S') { // checking for Sat or Sun
		$s == 'Sat' ? $fstday = '03' : $fstday = '02'; // bump date to Monday
	}
} else {
	$week_start_day = 'Sunday';
}

$first_of_month 		= $this_year.$this_month.$fstday;
$start_month_day 		= dateOfWeek($first_of_month, $week_start_day);
$thisday2 				= localizeDate($dateFormat_week_list, $unix_time);
$num_of_events2 			= 0;

define('HOME',substr( __FILE__,0,strpos(__FILE__,'engl')+4));
?>



<title><?php echo "$calendar_name - $display_date"; ?></title>
<?php

$myhome="events"; 

?>

<script src='includes/addAnchors.js'></script>
<script type="text/javascript">

<?php include('includes/create_jsevents_array.php'); ?>


/* utility functions */
function getid(e) { return document.getElementById(e); }
function gettags(i,t) { return i.getElementsByTagName(t); }
function dce(e) { return document.createElement(e); } 
function dct(s) { return document.createTextNode(s); }
function sa(e,a,val) { e.setAttribute(a,val); }
function ga(e,a) { return e.getAttribute(a); }
function ra(e,a) { e.removeAttribute(a); }
function ac(p,c) { p.appendChild(c); }
function getcss(o,p) { return document.defaultView.getComputedStyle(o,'').getPropertyValue(p); }

var w_pad = 25; var w_W = 300+w_pad; var w_H = 225+w_pad; /* dang, I hate coding fixed values here */

var ua=navigator.userAgent.toLowerCase();
var oldIE=(ua.indexOf('msie') != -1) && (ua.indexOf('msie 9') == -1);  // IE prior to v.9
if (oldIE) {
	var iebody=(document.compatMode && document.compatMode != "BackCompat")
		? document.documentElement : document.body;
}

function str_replace(haystack, needle, replacement) {
	var temp = haystack.split(needle);
	return temp.join(replacement);
}

function hyphenate(s) {  // returns Y-m-d
	return s.substr(0,4) + '-' + s.substr(4,2) + '-' + s.substr(6,2);
}

function format_date(dt) {
	var d = new Date(dt+'T00:00');  // buggy -- w/out T00:00 returns 1 day earlier
	var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
	var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
	return days[d.getDay()] + ', ' + months[d.getMonth()] + ' ' + d.getDate() + ', ' + d.getFullYear();
}

function y2m(s) {  // returns m-d-Y
	if (s.indexOf('-') > 0) {  // does it contain hyphens?
		s=str_replace( s,'-','' );   // strip out hyphens
	}
	return s.substr(4,2) + '-' + s.substr(6,2) + '-' + s.substr(0,4); 
}

function m2y(s) {
	if (s.indexOf('-') > 0) {  // does it contain hyphens?
		s=str_replace( s,'-','' );   // strip out hyphens
	}
	return s.substr(4,4) + '-' + s.substr(0,2) + '-' + s.substr(2,2); 
}

function to12hr(t) {
	if (t.length>0) {
		var curr_hour = t.substr(0,2);
		curr_hour=parseInt(curr_hour,10);
		var a_p = (curr_hour < 12) ? "am" : "pm";
		if (curr_hour >12) { 
			curr_hour=curr_hour-12; 
		} else { 
			if (curr_hour==0) { 
			curr_hour=12; 
			}
		}
		var curr_min = t.substr(3,2);
		return curr_hour.toString() + ":" + curr_min.toString() + a_p;
	} else {
		return t;
	}
}

function to24hr(t) {  // converts 12-hour values from event edit form
	var curr_hour = t.substr(0,2);
	curr_hour=parseInt(curr_hour,10);
	if (t.indexOf("pm") >0) {
		if (curr_hour != 12) curr_hour = curr_hour+ 12;  // add 12
	} else {
		if (curr_hour == 12) curr_hour = 0;
	}
	curr_hour=(curr_hour<10) ? "0" + curr_hour.toString() : curr_hour.toString();
	var colon=t.indexOf(':');  // find hour:min boundary
	return curr_hour + ":" + t.substr(colon+1,2) + ":00";
}

// function format_notes(s) {
// 	s=str_replace(s, "<br />","\n");
// 	return s;
// }


// process date selected in form
function newdate( calpage ) {
	d=document.pickdate.myyear.options[document.pickdate.myyear.selectedIndex].value +
		document.pickdate.mymonth.options[document.pickdate.mymonth.selectedIndex].value +
		'01<?php echo "$show_weekend"; ?>';
	location='<?=HOMEFOLDER?>events/' + calpage+'?getdate='+d;
}

function showdetail(n) {
	var w=window.open('includes/event.php?id='+n,'Detail','scrollbars=yes,width=460,height=375');
	w.focus();
}

function position_edit_form(eX,eY) {
//	getid('eventdata').innerHTML = "<p class='busy'>working ...</p>"; // clear old contents
	var w = getid('show_event');
	var winW = (oldIE) ? iebody.clientWidth : window.innerWidth;
	var winH = (oldIE) ? iebody.clientHeight : window.innerHeight;
	var adjX = (winW - eX < w_W) ? winW - w_W : eX;
	var adjY = (winH - eY < w_H) ? winH - w_H : eY;
	// if (adjX < 65) { adjX=65; }  // make room for time_menu
	w.style.cssText = 'left: ' + adjX + 'px; top: ' + adjY + 'px; ';
}

function showdiv(s,n,o) {
	var d=document.getElementById(s);
	if (n==0) {  // hiding div
		d.style.visibility='hidden';
	} else {  // showing div
		d.style.visibility='visible';
	}
}

function showevent(obj,eX,eY,eventID) {
	position_edit_form( eX,eY );
	var evtitle=getid('evtitle');
	var tagstart = '<p>', tagend = '</p>', labelstart = '<span>', labelend = '</span>';
	evtitle.firstChild.nodeValue=eventarr[eventID][5]; 

	var event_content='';  // innerHTML filled from array

	var dtstart = format_date( eventarr[eventID][6].substr(0,10) );  // dtlink (YYYYMMDD)
	var tmstart = to12hr( eventarr[eventID][6].substr(11,5) );  // hours:mins of begin time
	if (eventarr[eventID][7].length>0) {
		tmstart += ' - ' + to12hr( eventarr[eventID][7].substr(11,5) );  // hours:mins of end time
	}
	var evplace = eventarr[eventID][8];
	var evnotes = addAnchors(eventarr[eventID][9]);
/*	
	if (eventarr[eventID][7].length==0) {
		getid('dtend').value='';
		getid('tmend').value='';
	} else {
		var dtend = eventarr[eventID][7];
		var tmend = to12hr( dtend.substr(11,5) );  // hours:mins of end time
		getid('dtend').value=y2m(dtend.substr(0,10));
		getid('tmend').value=tmend;
	}
*/
	event_content = tagstart+labelstart+ 'date ' +labelend+ dtstart +tagend;
	event_content += tagstart+labelstart+ 'time ' +labelend+ tmstart +tagend;
	if (evplace.length>0) event_content += tagstart+labelstart+ 'place ' +labelend+ evplace +tagend;
	if (evnotes.length>0) event_content += tagstart+labelstart+ 'info. ' +labelend+ evnotes +tagend;
	getid('event_content').innerHTML = event_content;

	getid('show_event').style.visibility='visible';

}

function openfilter(d,wk) {
	// name window so filter.php can restore focus here
	window.name = 'myCalendar';
	d='?getdate='+d;
	w=window.open('includes/filter.php'+d+wk,'Filter','scrollbars=yes,width=500px,height=550px'); 
	w.focus();   
}

//function closefilter() {
//	w=window.open('Filter','js');
//	w.close();
//}

</script>


<meta name="keywords" content="English, UW, University of Washington" />
<meta name="description" content="English web page" />

<!-- Custom styles for this page go here -->
<link rel="stylesheet" type="text/css" href="<?php echo BASE."styles/$style_sheet/default.css"; ?>">
<?php
	if ($cal_name) {  // Set by INCLUDES/FIND_CAL.PHP
		echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"".BASE."styles/$style_sheet/$cal_name.css\">";
	}
?>
<style type="text/css">
#cal {
	clear: left;
	margin: 0;
}
</style>



</head>

<body style="margin:0;">
<?php // include(HOME.'/scripts/navbar.html'); ?>








<?php
/* [update]
# rw	fix this test code, calc end of month sensibly
# This *has* to be known by the code elsewhere, since the calendar cells 
# are correctly drawn.  Find the variables and use them here instead.
	$seek_day_num	= '28';
	while (checkdate($this_month,$seek_day_num,$this_year)) $seek_day_num++;
	$seek_day_num--;
	# rw	convert $seek_day_num to day of week
	$timestamp = strtotime($this_year.$this_month.$seek_day_num); // convert date to unix timestamp
	$i = 6-date('w',$timestamp ); // find number of weekday for this date (0-6)
	$next_month_month < '10' ? $pad='0' : $pad='';
	$i > 0 ? $last_cal_day = $next_month_year.$pad.$next_month_month.'0'.$i 
					: $last_cal_day = $this_year.$this_month.$seek_day_num;

# eventID, employID, evtype, evtitle, dtstart, dtend, evplace, evnotes

$sql = "SELECT * from events 
				WHERE date_format(dtstart,'%Y%m%d') >= '$start_month_day' and date_format(dtstart,'%Y%m%d') <= '$last_cal_day'  
				$filtstring  
				order by dtstart";

$result = $dbc->query($sql);
[update] 
*/

//echo $filtstring;
/*
# init $row to blank array
//$row = 

# following converts dtstart and dtend to yyyymmdd
if ($result->num_rows>0) {
	while ($row = $result->fetch_assoc()) {
		echo $row['dtstart'].' -> ';
		$row['dtstart'] = date('Ymd', strtotime($row['dtstart']));
		echo $row['dtstart'].'<br />';
		$row['dtend'] = date('Ymd', strtotime($row['dtend']));
	}
	$result->data_seek(0); // reset $result internal pointer to 1st record
}
*/

// $row = $result->fetch_assoc();

$evcnt = 0;

if ($tbl_name==='events') {
	$other_cal_name = 'a11';
	$other_cal_display = 'see A11 Reservations';
	$cal_table_title = '';
} else {
	$other_cal_name = '';
	$other_cal_display = 'see Other Meetings';
	$cal_table_title = 'A11 Reservations';
}
?>
<div style="float: right;">
<button 
	type="button"
	onclick="window.open('/engl/events/embedmonth.php?cal=<?=$other_cal_name;?>','_self');"> 
	<?=$other_cal_display;?>
</button>
</div>

<table id="cal" width="100%" border="0" cellspacing="0" cellpadding="0" class="calborder">
	<tr>
		<td>
			<table width="100%" border="0" cellspacing="0" cellpadding="0" class="calborder">
				<tr class="navback">
					<td valign="middle" align="left" width="40%">
			    	<?php 
			    		// prepare weekend toggle in top left of table
							$s=($show_weekend<>'') ? 'hide' : 'show';
							$sw=($s=='show') ? '&weekend=1' : '';
							echo "&nbsp; <a href=\"$viewpage?getdate=$getdate$sw\"> 
								<img border=\"0\" src=\"images/blip2.gif\" /><span class=\"p\">$s weekend</span></a>\n" .
								"<span class='cal-display'>$cal_table_title</span>";
						?>
			    </td>
		
					<td valign="middle" width="20%">
						<table width="100%" border="0" cellspacing="0" cellpadding="0">
							<tr>
								<td align="right">
									<a class="psf" id="larr" href="<?php echo "$viewpage?getdate=$prev_month$show_weekend"; ?>"></a> 
								</td>

								<td align="center" width="20%" nowrap valign="middle">
									<form name="pickdate">
									<select name="mymonth">
									<?php
									for ($i = 1; $i <= 12; $i++) {
										$s = ($i <10) ? '0'.$i : $i;
										if ($s == $this_month) { 
											echo "<option selected=\"yes\" value=\"$s\">".strtoupper($monthsofyear_lang[$i-1])."</option>\n";
										} else {
									    echo "<option value=\"$s\">".strtoupper($monthsofyear_lang[$i-1])."</option>\n";
									  }
									}
									?>
									</select>&nbsp;
									<select name="myyear">
									<?php
									if (($this_year < $cur_year-2) || ($this_year > $cur_year+2)) echo "<option selected=\"yes\" value=\"$this_year\">$this_year</option>\n";
									for ($i = $cur_year -2 ; $i <= $cur_year + 2; $i++) {
										if ($i == $this_year) { 
											echo "<option selected=\"yes\" value=\"$i\">$i</option>\n";
										} else {
											echo "<option value=\"$i\">$i</option>\n";
									  }
									}
									?>
									</select>
									<input type="button" value="GO" onclick="newdate('<?php echo $viewpage; ?>')" />
									</form>
								</td>

								<td align="left" width="40%" >
									<a class="psf" id="rarr" href="<?php echo "$viewpage?getdate=$next_month$show_weekend"; ?>"></a> 
								</td>
							</tr>
						</table>
					</td>
		
					<td align="right" width="40%">
						<a href="#" onclick="newdate('<?php echo $edpage; ?>'); return false;"><img border="0" src="images/blip2.gif" /><span class="p">add event </span></a>
					</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td align="center" valign="top">
			<table width="100%" border="0" cellspacing="0" cellpadding="2" class="monthback">
				<tr>
					<?php
						// create header row (sun - sat, or mon - fri)
						$start_day = strtotime($week_start_day);
						$fstday = 0;
						$lstday = 7; //one past last day (0..6)
						if (!$show_weekend) {
							$fstday++; 
							$lstday--;
						}
						for ($i=$fstday; $i<$lstday; $i++) {
							$day = strtoupper($daysofweekshort_lang[$i]); 
							echo '<th valign="top" width="20%" class="wdayheader" scope="col">'.$day.'</th>';
							$start_day = strtotime("+1 day", $start_day);
						}
					?>
				</tr>
				<tr>	
				<?php 	 
# rw	main calendar cells populated here, $sunday as unix timestamp (date as seconds)
					$sunday 		= strtotime("$start_month_day");
					$i 				= 0;
					$show_weekend ? $lstday = 7 : $lstday = 5;
					$whole_month 	= TRUE; # rw	conditional for do loop
					do {
						$day 			= date ("j", $sunday);
						$daylink 		= date ("Ymd", $sunday);
						$check_month 	= date ("m", $sunday);
						# rw	$this_month	is initialized as "mm" based on today's date, e.g. "12" or "01"
						if ($check_month != $this_month) {
							# rw	color the calendar cells differently for overlap of previous/next month
							$bgclass	= 'class="monthoff"';
						} else {
							if ($today_today == $daylink) {
								$bgclass = 'class="monthon"'; # rw	highlight today in current month
							} else {
								$bgclass = 'class="monthreg"';
							}
						}
						if ($i == 0) echo '<tr >';
# rw	$master_array uses yyyymmdd as keys for events
#	so a simple check for the key indicates events fall on this day

/*
# rw	this first condition is necessary because an entire year is being read into $master_array elsewhere.
  						if (isset($master_array[("$daylink")])) {
*/

#	rw	next 3 lines turn dates into active links to day.php file
# (clumsy use of CSS, these fonts should be done by context, not coded in each table cell)
							echo "\n".'<td valign="top" align="left" height="30" '.$bgclass.'>';
							echo '<div class="dnum">'.$day.'</div>'."\n";

/*
[update]
# My SQL query should return only dates we need.  
# If we have any more dates, convert the next to unix timestamp
		if (!empty($row['dtstart'])) {
			$event_utime = strtotime($row['dtstart']);
# compare yyyymmdd dates and loop through our events while they match
			while (date('Ymd',$event_utime)==$daylink) {

	# rw	fix next lines
//				(!empty($row['dtend'])) ? $eend = date('g:ia',strtotime($row['dtend'])) : $eend=''; 
[update]
*/
# My SQL query should've returned only dates we need.  
# If we have any more dates, convert the next to unix timestamp
		if ($evcnt < $evmax) {  
			$event_utime = strtotime($ev[$evcnt]['dtstart']);
			while ($evcnt < $evmax && $ev[$evcnt]['dtlink']==$daylink) {

	# rw	fix next lines
//				(!empty($row['dtend'])) ? $eend = date('g:ia',strtotime($row['dtend'])) : $eend=''; 

				$t = date('g:ia',$event_utime);
				if ($t == '12:00am') { // no time given explicitly
					if (empty($ev[$evcnt]['evnotes'])) { // no event note?
						echo '<p class="h">'.stripslashes($ev[$evcnt]['evtitle']);
						if (!empty($ev[$evcnt]['evplace'])) echo ' - '.stripslashes($ev[$evcnt]['evplace']).'</a></p>';
					} else {
						echo '<p class="h">';
						// echo "<a href=\"javascript:showdetail('{$ev[$evcnt]['eventID']}'); return false;'>";
						echo "<a href='#' onclick='showevent(this,event.clientX,event.clientY,$evcnt); return false;'>";
						echo '<span class="allday">'.stripslashes($ev[$evcnt]['evtitle']);
						if (!empty($ev[$evcnt]['evplace'])) echo ' '.stripslashes($ev[$evcnt]['evplace']);
						echo "</span></a></p>\n";
					}
				} else {
					echo '<p>';
					// echo "<a href=\"javascript:showdetail('{$ev[$evcnt]['eventID']}')\">";
					echo "<a href='#' onclick='showevent(this,event.clientX,event.clientY,$evcnt); return false;'>";
					echo '<span class="t">'.substr($t,0,-1).'</span> ';
					if (!empty($ev[$evcnt]['evplace'])) 
						echo '<span class="p">'.stripslashes($ev[$evcnt]['evplace']).'&nbsp;&#8226;</span> ';
					echo stripslashes($ev[$evcnt]['evtitle']).' ';
					echo "</a></p>\n";
				}
				# get next event
				$evcnt++; 

				if ($evcnt < $evmax) {
					$event_utime = strtotime($ev[$evcnt]['dtstart']);
				} else {
					$event_utime = 28800; // set to 19700101
				}

			} // while
		} // if

echo "</td>\n"; //	close day


						$sunday = strtotime("+1 day", $sunday); 
						$i++;	# rw	why not just make $i mod 7? rather than increment and check all the time?
# rw	I think code should be:
#		$i = ($i+1) % 7 // or $lstday;
#		if ($i == 0) {
						if ($i == $lstday) { 
							echo "</tr>\n"; // end table row, start next week
							$i = 0;

							//	handle weekend skip
							if (!$show_weekend) { 
								// skip 2 days to next Monday
								$sunday = strtotime("+2 day", $sunday);
								// skip any weekend events in calendar -- get it back in synch with display loop
								if (!empty($ev[$evcnt]['dtlink'])) {
									$dtlink_utime = strtotime($ev[$evcnt]['dtlink']);
									while ($evcnt<($evmax-1) && $dtlink_utime < $sunday ) {
										$evcnt++;
										if (!empty($ev[$evcnt]['eventID'])) $dtlink_utime = strtotime($ev[$evcnt]['dtlink']);
									}  // while
								}  // if
							}  // end weekend skip

							$checkagain = date ("m", $sunday);
							if ($checkagain != $this_month) $whole_month = FALSE; 
						}
					} while ($whole_month == TRUE);
				?>
			</table>
		</td>
	</tr>
</table>

<div id="show_event" class="ds_flatter">

	<div class="close"><button onclick="showdiv('show_event',0,null);">&#215</button></div>
	<h2 id="evtitle">dategoeshere</h2>

	<div id="event_content">

	</div>

</div>

</body>
</html>
