<?php 

// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();
?>
<table border="0" width="100%" cellspacing="0" cellpadding="<?php echo CELLPADDING_SUB;?>">
<?php
	$ecCat_path="";
	if($ecPath!="") $ecCat_path.="ecPath=" . $ecPath;
	echo tep_draw_form("frmView",tep_href_link(FILENAME_EVENTS_MONTH_VIEW,$ecCat_path));
	// get initial parameters
	$category_id=(isset($HTTP_POST_VARS['category_id'])?(int)$HTTP_POST_VARS['category_id']:0);
	$year=(isset($HTTP_POST_VARS['year'])?tep_db_prepare_input($HTTP_POST_VARS['year']):date('Y'));
    $month=(isset($HTTP_POST_VARS['month'])?tep_db_prepare_input($HTTP_POST_VARS['month']):date('m'));
	$post_action=(isset($HTTP_POST_VARS['post_action'])?tep_db_prepare_input($HTTP_POST_VARS['post_action']):"change_cat");
	$server_date=getServerDate();
	$server_year=date("Y");
	
	if ($category_id<=0) $category_id=(int)$current_events_category_id;

	if ($category_id<=0) { 
		$first_cat_query=tep_db_query("SELECT events_categories_id from " . TABLE_EVENTS_CATEGORIES_DESCRIPTION . " order by events_categories_name limit 1");
		$first_cat_result=tep_db_fetch_array($first_cat_query);
		$category_id=(int)$first_cat_result["events_categories_id"];
	}
	
	// creation an filter if category_id is present
	if ($category_id>0) $cat_option=" and ec.events_categories_id='" . $category_id . "'";

	$cur_date_time=getServerDate(false);
	
	// get the first day of the month
	$start_time_stamp=mktime(0,0,0,$month,1,$year);

	// count the total_days
	$total_days=date("t",$start_time_stamp);
	
	//$start_date=date('Y-m-d',$start_time_stamp); // get the start_date of month
	$end_time_stamp=mktime(0,0,0,$month,$total_days,$year); // get the end_date of month

	$month_start=date("Y-m-d",$start_time_stamp);
	$month_end=date("Y-m-d",$end_time_stamp);

	// determine start and end date of selected month

	// find start_index;
	$start_col=date("w",$start_time_stamp)-1;
	if ($start_col<0) $start_col=6;
	if ($start_col>0) $start_time_stamp=tep_datesub($start_time_stamp,"day",$start_col);
	$end_col=date("w",$end_time_stamp)-1;
	if ($end_col<0) $end_col=6;
	if ($end_col<6) $end_time_stamp=tep_dateadd($end_time_stamp,"day",6-$end_col);
	
	$start_date=date("Y-m-d",$start_time_stamp);
	$end_date=date("Y-m-d",$end_time_stamp);
	$cur_day=(int)date("d",$start_time_stamp);
	//$start_row=floor($cur_day/7);
	//if ($start_row<0) $start_row=0;
	$start_row=0;
	
	$events_ids="";
	$event_found=false;

	// select the events of current event category id
	$find_events_query=tep_db_query("SELECT e.events_id,s.start_date,s.sessions_id from " . TABLE_EVENTS_SESSIONS . " s, " . TABLE_EVENTS . " e, " . TABLE_EVENTS_TO_EVENTS_CATEGORIES  . " ec " .
									" where s.start_date>='" . $month_start . "' and s.start_date<='" . $month_end ."' and s.start_date>='" .  $server_date . "' " . $cat_option . EVENTS_CONDITION_STATUS . " and s.sessions_type in ('S','R') and s.sessions_type!='' and e.events_id=s.events_id and ec.events_id=e.events_id order by s.start_date,events_start_time,events_end_time");
	$date_details=array();
	$events_details=array();
	$session_dates=array();
	$icnt=0;
	// select the event ids and session ids
	while($find_events_result=tep_db_fetch_array($find_events_query)){
		$session_date=$find_events_result["start_date"];
		$events_id=$find_events_result["events_id"];
		if (!isset($session_dates[$session_date][$events_id])) {

			/*$cur_day=(int)date("d",strtotime($session_date))-1;
			$cur_col=date("w",strtotime($session_date))-1;
			if ($cur_col<0) $cur_col=6;
			$cur_row=floor(($cur_day+$start_col)/7);
			if ($cur_row<0) $cur_row=0;*/

			$date_details[$session_date][]=array('date'=>$session_date,'events_id'=>$events_id,'sessions_id'=>$find_events_result["sessions_id"]);
			$session_dates[$session_date][$events_id]=1;
		}
		if (!isset($events_details[$events_id])){
			$events_details[$events_id]=1;
			$events_ids.=$events_id . ",";
		}
	}
	unset($session_dates);

	// select the event details
	if ($events_ids!=""){
		$events_ids=substr($events_ids,0,-1);
		$event_info_query = tep_db_query("select e.events_id, ed.events_name, e.events_date_available,e.events_date_added,e.events_tax_class_id, e.events_fees,events_location," .
										"time_format(e.events_start_time,'%h:%i %p') as events_start_time,time_format(e.events_start_time,'%H:%i') as events_start_hour,time_format(e.events_end_time,'%h:%i %p') as events_end_time,time_format(e.events_end_time,'%H:%i') as events_end_hour,e.link_service,e.service_id  " . 
										" from 	" . TABLE_EVENTS . " e , " . TABLE_EVENTS_DESCRIPTION . " ed " .
										" where e.events_id in (" . $events_ids . ") " . EVENTS_CONDITION_STATUS . " and ed.events_id = e.events_id and ed.language_id = '" . (int)$languages_id . "'");
										

		while($event_info=tep_db_fetch_array($event_info_query)){
			if($event_info['link_service']=='L'){
				$location_query=tep_db_query("SELECT location_name,location_details from " . TABLE_EVENTS_LOCATIONS . " where location_id='" . (int)$event_info["events_location"] . "'");
			} else if($event_info['link_service']=='S'){
				$location_query=tep_db_query("SELECT sl.service_location_name as location_name,sl.service_location_details as location_details from " . TABLE_SERVICES." s, ".TABLE_SERVICES_LOCATIONS ." sl where  s.service_location=sl.service_location_id and s.service_id='".(int)$event_info["service_id"] ."'");
			}
			if($location_query)$location=tep_db_fetch_array($location_query);
			$events_id=$event_info["events_id"];
			$events_details[$events_id]=array(	"events_name"=>$event_info["events_name"],
												"events_fees"=>$event_info["events_fees"],
												"location"=>$location["location_name"],
												"tax_id"=>$event_info["events_tax_class_id"],
												"start_time"=>strtolower($event_info["events_start_time"]),
												"end_time"=>strtolower($event_info["events_end_time"]),
												"start_hour"=>strtolower($event_info["events_start_hour"]),
												"end_hour"=>strtolower($event_info["events_end_hour"])
												);
		}
	}
	// BOF: Lango Added for template MOD
	if (MAIN_TABLE_BORDER == 'yes'){
	 //table_image_border_top(false, false, '');
	}
	// EOF: Lango Added for template MOD
	
	// select event category details
	$cat_array=array();
	$cat_list_query=tep_db_query("SELECT ecd.events_categories_name,ecd.events_categories_id from " . TABLE_EVENTS_CATEGORIES . " ec, " . 
								TABLE_EVENTS_CATEGORIES_DESCRIPTION . " ecd where ecd.events_categories_id=ec.events_categories_id and ecd.language_id='" . 
								$languages_id . "' order by events_categories_name ");
	
	$cat_array[]=array('id'=>0,'text'=>"All Categories");
	while($cat_list_result=tep_db_fetch_array($cat_list_query)){
		$cat_array[]=array('id'=>$cat_list_result["events_categories_id"],'text'=>$cat_list_result["events_categories_name"]);
	}

	// create Month
	$month_array=array();
	for ($icnt=1;$icnt<=12;$icnt++){
		$month_array[]=array('id'=>$icnt,'text'=>date("F",mktime(0,0,0,$icnt,1,$year)));
	}
	//form the year array
	$year_array=array();
	for ($icnt=$server_year;$icnt<=2036;$icnt++){
		$year_array[]=array('id'=>$icnt,'text'=>$icnt);
	}
	

?>
	<script language="JavaScript">
		function ChangeResult(mode){
			var month=document.frmView.month;
			var year=document.frmView.year;
			var server_year=document.frmView.check_server_year;
			if (mode==1){ // naviagte left
				if (month.value<=1 && year.value<=server_year.value) return;
				if (month.value<=1){
					month.value=12;
					year.value=parseInt(year.value)-1;
				} else {
					month.value=parseInt(month.value)-1;
				}
				document.frmView.post_action.value="nav_left";
			} else if (mode==2){
				if (month.value>=12 && year.value>=2036) return;
				if (month.value>=12){
					month.value=1;
					year.value=parseInt(year.value)+1;
				} else {
					month.value=parseInt(month.value)+1;
				}
				document.frmView.post_action.value="nav_right";
			} else {
				document.frmView.post_action.value="change_cat";
				document.frmView.action="<?php echo tep_href_link(FILENAME_EVENTS_MONTH_VIEW,tep_get_all_get_params(),'NONSSL');?>";
				document.frmView.month.value=document.frmView.selMonth.options[document.frmView.selMonth.selectedIndex].value;
				document.frmView.year.value=document.frmView.selYear.options[document.frmView.selYear.selectedIndex].value;
			}
			document.frmView.submit();			
		}
		function OpenDayView(dateValue){
			document.frmView.cur_date.value=dateValue;
			document.frmView.action="<?php echo tep_href_link(FILENAME_EVENTS_DAY_VIEW,$ecCat_path . '&set_view=day');?>";
			document.frmView.submit();
		}
		function ChangeView(mode){
			switch(mode){
				case 1:
					document.frmView.action="<?php echo tep_href_link(FILENAME_EVENTS_DAY_VIEW,$ecCat_path . '&set_view=day');?>";
					break;
				case 2:
				    document.frmView.action="<?php echo tep_href_link(FILENAME_EVENTS_WEEK_VIEW,$ecCat_path . '&set_view=week');?>";
					break;
				case 3:
					document.frmView.action="<?php echo tep_href_link(FILENAME_EVENTS_MONTH_VIEW,$ecCat_path . '&set_view=month');?>";
					break;
			}
			document.frmView.submit();
		}
		function ViewPrevious(){
			document.frmPrevious.submit();
		}
	</script>
<?php 
if (MAIN_TABLE_BORDER == 'yes'){ 
if(!defined('BOX_HEADING_EVENTS_CATEGORIES'))define('BOX_HEADING_EVENTS_CATEGORIES', 'Events');
	 echo table_image_border_top(false, false, BOX_HEADING_EVENTS_CATEGORIES);
}
?>			
<table width='100%' border="0" cellspacing="0" cellpadding="0" align="left">
	<tr height="20" >
		<td class="main" valign="top" nowrap="nowrap" >
		<table width='100%' border="0" cellspacing="0" cellpadding="0" align="left">
		<!--<tr height="30"><td width="100" valign="middle"  colspan="3" align="left" class="infoBoxHeadingPage">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php //echo tep_template_image('arrow_1.gif','','align=absmiddle') .'  '.BOX_HEADING_EVENTS_CATEGORIES;?></td></tr>-->
		<tr style="height:10"><td></td></tr>
			<tr class="eventTitleRow">	
				<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>	
				<td valign="middle" align="left" width="120" class="eventTitle" nowrap="nowrap"><?php echo TEXT_EVENT_CATEGORY;?></td>
				<td valign="middle" class="eventTitle" nowrap="nowrap">
				<?php 
				echo tep_draw_pull_down_menu("category_id",$cat_array,$category_id) . '&nbsp;';
				echo '<a href="javascript:ChangeResult(3);">' . tep_template_image_button("button_quick_find.gif",IMAGE_VIEW_DATE,'align=absmiddle') . '</a>';
				?>
				</td>
				<td valign="middle" align="right" width="350" class="eventTitle" nowrap="nowrap">
				<?php 
				echo TEXT_VIEWS .'&nbsp; <a href="javascript:ChangeView(1)">' . tep_template_image_button("button_day_view.gif",IMAGE_DAY_VIEW,'align=absmiddle') . '</a>&nbsp;';
				echo '<a href="javascript:ChangeView(2)">' . tep_template_image_button("button_week_view.gif",IMAGE_WEEK_VIEW,'align=absmiddle') . '</a>&nbsp;';
				echo tep_template_image_button("button_month_view_selected.gif",IMAGE_MONTH_VIEW,'align=absmiddle') .'&nbsp;';
				?>
				</td>		
			</tr>
		</table>
		</td>
	</tr>
	<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>	
	</tr>
	<tr>
		<td>
		<table border="0" cellpadding="10" cellspacing="0" width="100%">
			<?php if ($category_id>0) {
					// select the category details
					$cat_query=tep_db_query("SELECT ecd.events_categories_name,ec.events_categories_image,if(CHAR_LENGTH(ecd.events_categories_description)>250,concat(left(ecd.events_categories_description,250),'...'),ecd.events_categories_description) as events_categories_description from " . 
											TABLE_EVENTS_CATEGORIES . " ec, " . TABLE_EVENTS_CATEGORIES_DESCRIPTION . " ecd " .
											" where ecd.events_categories_id='" . $category_id . "' and ecd.language_id='" . $languages_id . "' and ecd.events_categories_id=ec.events_categories_id");
					$cat_info=tep_db_fetch_array($cat_query);
				}						
			?>
			<tr>
				<td valign="top" class="headerBrowse" ><?php echo $cat_info['events_categories_name'];?></td>												
			</tr>
			<tr>
				<td>
					<table border="0" cellpadding="0" cellspacing="0" width="100%" class="tab_strip">
						<tr>
							<td class="contentBackCol1" width="5"></td>	
							<td class="contentBackCol2">
							<table border="0" cellpadding="0" cellspacing="0" width="100%">
								<tr>
									<td><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>	
								</tr>
								
										<tr height="100">
											<td valign="top" >
											<table border="0" cellpadding="2" cellspacing="3" valign="top" width="100%">
												
												<tr>
													<td valign="top" class="smalltext" >
														<?php 
														echo ($cat_info['events_categories_image']==''?'':tep_image(DIR_WS_IMAGES . $cat_info['events_categories_image'] , $cat_info['events_categories_name'],SMALL_IMAGE_WIDTH,'','align=left'));
														echo $cat_info['events_categories_description'];
													?>
													</td>
												</tr>
											</table>						
											</td>
										</tr>
								<tr>
									<td><?php echo tep_draw_separator('pixel_trans.gif',1,20);?></td>
								</tr>
								
								<tr><td>
									<table border="0" cellspacing="0" cellpadding="2" valign="top" width="100%" class="calender_frame" height="40">
										<tr>
											<!--<td valign="top" width="20"><?php echo '<a href="javascript:ChangeResult(1);">' . tep_template_image_button("navigate_left.gif",IMAGE_PREVIOUS_MONTH) . '</a>';?></td>-->
											<td width="951" align="center" valign="middle" class="headerSubBrowse"> 
												<b>Month</b> <?php echo tep_draw_pull_down_menu("selMonth",$month_array,$month);?>&nbsp;
												<b>Year</b> <?php echo tep_draw_pull_down_menu("selYear",$year_array,$year);?>&nbsp;
												<a href="javascript:ChangeResult(3)"><?php echo tep_template_image_button("button_quick_find.gif",IMAGE_VIEW_EVENTS,'align=absmiddle');?></a>
											</td>
											<!--<td valign="top" width="40"><?php echo '<a href="javascript:ChangeResult(2);">' . tep_template_image_button("navigate_right.gif",IMAGE_NEXT_MONTH) . '</a>';?></td>-->
										</tr>
									</table>
									</td>
								</tr>
						
								<tr height="20">
									<td class="headerCalendarRow">
										<table border="0" cellspacing="0" cellpadding="0" valign="top" width="100%" height="29"> 
											<tr>  
												<?php  $flag=0;
												// display the week day names header 
												for ($icnt=1;$icnt<=7;$icnt++){
													if ($icnt==7)
														$class="headerCalendarSunday";
													else
														$class="headerCalendar"; 
													echo '<td width="75" class="' . $class . '" align="center">' . constant("TEXT_DAY" . $icnt) . '</td>';
												}
											?>		
											</tr>
										</table>	
									</td>  
								  </tr>
								 <tr>
									<td valign="top">
									<table border="0" cellspacing="1" cellpadding="0" valign="top" width="100%" class="contentCalendar" bgcolor="#000000">
											<?php 
													$temp_start_time=$start_time_stamp;
													$temp_end_time=$end_time_stamp;
													$month_start_time=strtotime($month_start);
													$month_end_time=strtotime($month_end);
													$day_cnt=1;
													$cur_date=$server_date;
													
													// iterate and display the days of selected month
													while($temp_start_time<=$temp_end_time) {
															$header_text_sub_gray1='headerSubCalendarGray1';
														    $header_text_sub_gray='headerSubCalendar';
														    $calEventName="calEventName";
														    $calEventTime="calEventTime";
															if ($day_cnt % 7==0) {
																$header_text_sub_gray1='headerSundayGray1'; 	
																$header_text_sub_gray='headerSundayGray1';
																$calEventName="calEventNameSunday";
														    	$calEventTime="calEventTimeSunday";	
																$class=	"dataCalendarSunday";
																//echo $day_cnt;
															} else if (($day_cnt%7)%2==0){
																$class="dataCalendarEven";
															} else {
																$class="dataCalendarOdd";
															}
															if ($day_cnt==1 || $day_cnt%7==1) echo '<tr height="100">';
															$temp_date=date("Y-m-d",$temp_start_time);
															
												
															// check of start of session in the month and change the color t represent it
											       ?>
																<Td valign="top" width="14%" class="<?php echo $class;?>" >
																<table border="0" cellspacing="0" cellpadding="2" valign="top" width="100%" height="100%" bgcolor="">
																	<tr height="20">
																		<?php 
																		   if($temp_start_time>=$month_start_time && $temp_start_time<=$month_end_time) 
																		  {
																				if($temp_start_time>=$month_start_time && $temp_date<$cur_date)
																			 {  
																		?>
																			  <Td width="25" valign="top" class="tdSubCalendarGray1"><span class="<?php echo $header_text_sub_gray1;?>" onClick="javascript:OpenDayView('<?php echo $temp_date;?>');"><?php echo date("d",$temp_start_time);?></span></Td> 
																		<?php } else {  ?>
																			<Td width="5" valign="top"><span class="<?php echo $header_text_sub_gray;?>" onClick="javascript:OpenDayView('<?php echo $temp_date;?>');"><?php echo date("d",$temp_start_time);?></span></td> 
																				<?php }
																		 } else { ?>
																					<Td valign="top" class="tdSubCalendarGray"><span class="headerSubCalendarGray" onClick="javascript:OpenDayView('<?php echo $temp_date;?>');"><?php echo date("d",$temp_start_time);?></span></Td>
																		   <?php }	?>
																	</tr>
														<?php 
															if (isset($date_details[$temp_date])){
																$date_info=&$date_details[$temp_date];
														?>
																	<tr>
																		<Td valign="top">
																		<table border="0" cellpadding="1" cellspacing="0" width="100%" height="100%">
														<?php
																$rtime=false;
																if(defined('TIME_FORMAT')) {
																	if(TIME_FORMAT=='24') 
																	$rtime=true;
																}	
																// display the events of current date
																for ($kcnt=0;$kcnt<sizeof($date_info);$kcnt++) {
																	$events_id=$date_info[$kcnt]["events_id"];
																	$start_time=$events_details[$events_id]["start_time"];
																	$end_time=$events_details[$events_id]["end_time"];
																	if($rtime) {
																		$start_time=$events_details[$events_id]["start_hour"];
																		$end_time=$events_details[$events_id]["end_hour"];
																	}	
														?>
																		<tr>
																			<td valign="top"> 
																				<?php																				
																				 if($flag==0){ $temp=$temp_date; $flag=1; }
																					if($events_details[$events_id]["location"] && $events_details[$events_id]["events_name"]){
																						if (EVENTS_CALENDAR_DISPLAY_NAME == 'event name')
																						{
																						$val=$events_details[$events_id]["events_name"];
																						} else {
																						$val=$events_details[$events_id]["location"];
																						}
																						echo '<a class="'.$calEventName.'" href="' . tep_href_link(FILENAME_SESSION_DETAILS,'events_id=' . $events_id . '&sID=' . $date_info[$kcnt]["sessions_id"] . '&' . $ecCat_path) . '">' . 
																							$val. "</a><br><span class='".$calEventTime."'>" . $start_time . " - " . $end_time ."</span><br>";
																					}
																				?>
																			</td>
																		</tr>
																		<tr>
																			<Td height="5">&nbsp;</Td>
																		</tr>
														<?php } // for $kcnt ?>
																	</table>
																</tr>
														<?php 
															} // if $temp_date
															$temp_start_time=tep_dateadd($temp_start_time,"day",1);															 
															$day_cnt=$day_cnt+1;
														?>
														</table>
														</td>
										<?php
												if ($dat_cnt % 7==1) echo '</tr>';
											} //while
										?>
										</table>
									   </td>
									</tr>  
									
									<input type="hidden" name="month" value="<?php echo $month;?>">
									<input type="hidden" name="year" value="<?php echo $year;?>">
									<input type="hidden" name="check_server_year" value="<?php echo $server_year;?>">
									<input type="hidden" name="cur_date" value="<?php echo $temp; ?>" />
									<input type="hidden" name="post_action" value="change_cat">
									
									</form>
									<?php
										// display post data as hidden value
										$page_param=(isset($HTTP_SESSION_VARS["calendar_style"])?$HTTP_SESSION_VARS["calendar_style"]:"day");
										if ($page_param=="month") {
											$page_name=FILENAME_EVENTS_MONTH_VIEW;
										} else if ($page_param=="week"){
											$page_name=FILENAME_EVENTS_WEEK_VIEW;
										} else if ($page_param=="day"){
											$page_name=FILENAME_EVENTS_DAY_VIEW;
										} else {
											$page_name=basename($GLOBALS["PHP_SELF"]);
										}
										echo tep_draw_form("frmPrevious",tep_href_link($page_name,$ecCat_path));
										echo '<input type="hidden" name="temp" value="1">';
											$view_state=(isset($HTTP_SESSION_VARS["VIEW_STATE"])?$HTTP_SESSION_VARS["VIEW_STATE"]:array());
										if (sizeof($view_state)){
										  reset($view_state);
										  while (list($key, $value) = each($view_state)) {
											if (!is_array($view_state[$key])) {
											  echo tep_draw_hidden_field($key, htmlspecialchars(stripslashes($value)));
											}
										  }
										}
										echo '</form>';
										tep_set_session_value("VIEW_STATE",$HTTP_POST_VARS);
										tep_set_session_value("calendar_style","month");
									?>
									<tr>
										<td class="main">
										<table width='100%' border="0" cellspacing="0" cellpadding="2" align="right">
											<tr height="30">
												<td valign="bottom">
												<?php
												echo '<a href="javascript:ViewPrevious();">' . tep_template_image_button("button_back.gif",IMAGE_BACK) . '</a>';
												?>
												</td>
											</tr>
										</table>
										</td>
									</tr>
							</table>
							</td>
							<td width="5" class="contentBackCol3"></td>
					   </tr>
				   </table>
				   </td>
			   </tr>
			</table>
<?php // BOF: Lango Added for template MOD
	if (MAIN_TABLE_BORDER == 'yes'){
		table_image_border_bottom();
	}
	// EOF: Lango Added for template MOD
	?>		
</table>




