<?php 

// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();
?>
<div id="spiffycalendar" class="text"></div>
<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_WEEK_VIEW,$ecCat_path));
	$category_id=(isset($HTTP_POST_VARS['category_id'])?(int)$HTTP_POST_VARS['category_id']:0);
	$start_date=($HTTP_POST_VARS['start_date']?tep_db_prepare_input($HTTP_POST_VARS['start_date']):getServerDate());
	$end_date=($HTTP_POST_VARS['end_date']?tep_db_prepare_input($HTTP_POST_VARS['end_date']):getServerDate());
	$post_action=(isset($HTTP_POST_VARS['post_action'])?tep_db_prepare_input($HTTP_POST_VARS['post_action']):"change_cat");
	$server_date=getServerDate();
	
	// determine category id from post data or current event category or from the first data
	if ($category_id<=0) $category_id=(int)$current_events_category_id;

	if ($category_id<=0) {
		$first_cat_sql = "SELECT events_categories_id from " . TABLE_EVENTS_CATEGORIES_DESCRIPTION . " order by events_categories_name limit 1";
		$first_cat_query=tep_db_query($first_cat_sql);
		$first_cat_result=tep_db_fetch_array($first_cat_query);
		$category_id=(int)$first_cat_result["events_categories_id"];
	}

	if ($category_id>0) $cat_option=" and ec.events_categories_id='" . $category_id . "'";
	
	//determine action to nav_left or nav_right or view_date
	if ($post_action=="view_date") {
		$cur_date=tep_db_prepare_input($HTTP_POST_VARS['select_date']);
	}else if ($post_action=="nav_left"){
		$end_date=date('Y-m-d',tep_datesub(strtotime($start_date),"day",1));
		$start_date=date('Y-m-d',tep_datesub(strtotime($start_date),"day",7));
	} else if ($post_action=="nav_right"){
		$start_date=date('Y-m-d',tep_dateadd(strtotime($end_date),"day",1));
		$end_date=date('Y-m-d',tep_dateadd(strtotime($end_date),"day",7));
	} elseif($start_date==$end_date){
		// find week start date and week end date
		 $get_month=date('t',strtotime($start_date));
		 $total_days=($get_month - date('d',strtotime($start_date)));
		 $end_date=date('Y-m-d',tep_dateadd(strtotime($start_date),'day',$total_days));
		 
		 $get_events_sql = "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>='" . $start_date . "' and s.start_date<='" . $end_date ."' and s.start_date>='" .  $server_date . "' " . $cat_option . " and s.sessions_type in ('S','R') 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";
		 $get_events_query=tep_db_query($get_events_sql);

		 		 
		 //$get_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>='" . $start_date . "' and s.start_date<='" . $end_date ."' and s.start_date>='" .  $server_date . "' " . $cat_option . " and s.sessions_type in ('S','R') and e.events_id=s.events_id and ec.events_id=e.events_id order by events_start_time,events_end_time");

	     if(tep_db_num_rows($get_events_query)>0){
	     	$res=tep_db_fetch_array($get_events_query);
	     	$start_date=$res['start_date'];
	     }
	
		$week_day=date("w",strtotime($start_date))-1;
		if($week_day<0) $week_day=6;
		 
		if($week_day>0) $start_date=date("Y-m-d",tep_datesub(strtotime($start_date),"day",$week_day));
		//$week_day=6-$week_day;
		
		//if ($week_day>0) 
		$end_date=date("Y-m-d",tep_dateadd(strtotime($start_date),"day",6));
	}
 
	$navigate_left=true;
	$navigate_right=true;
	if ($start_date<=$server_date) $navigate_left=false;

	$month=date("m",strtotime($start_date));
	$year=date("Y",strtotime($start_date));
	
	// prepare for weeks
	$cur_week_date=$start_date . "|" . $end_date; 
	$week_array=array();
	// get the first day of the month
	$temp_time_stamp=strtotime($start_date);
	$temp_time_stamp=tep_datesub($temp_time_stamp,"day",14);
	for ($icnt=0;$icnt<5;$icnt++){
		$end_time_stamp=tep_dateadd($temp_time_stamp,"day",6); 
		$week_array[]=array("id"=>date("Y-m-d",$temp_time_stamp) . "|" . date("Y-m-d",$end_time_stamp),'text'=>date("M d",$temp_time_stamp). " - " . date("M d",$end_time_stamp));
		$temp_time_stamp=tep_dateadd($temp_time_stamp,"day",7);
	}
	$events_ids="";
	$event_found=false;

  	// select the events of current event category
	
	$find_events_sql = "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>='" . $start_date . "' and s.start_date<='" . $end_date ."' and s.start_date>='" .  $server_date . "' " . $cat_option . " and s.sessions_type in ('S','R') and e.events_id=s.events_id and ec.events_id=e.events_id order by events_start_time,events_end_time";
	$find_events_query=tep_db_query($find_events_sql);
	 
	$date_details=array();
	$events_details=array();
	// select events_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($date_details[$session_date])) $date_details[$session_date]=array();
		if (!in_array($events_id,$date_details[$session_date])) $date_details[$session_date][]=array("events_id"=>$events_id,"sessions_id"=>$find_events_result["sessions_id"]);
		if (!isset($events_details[$events_id])){
			$events_details[$events_id]=1;
			$events_ids.=$events_id . ",";
		}
	}
	
	// select events details
	if ($events_ids!=""){
		$events_ids=substr($events_ids,0,-1);
		$event_info_sql = "select e.events_id, ed.events_name, e.events_date_available,e.events_date_added,e.events_tax_class_id, e.events_fees," .
										"e.events_start_time as start_time,e.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_stime,time_format(e.events_end_time,'%h:%i %p') as events_end_time,time_format(e.events_end_time,'%H:%i') as events_etime,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 . "'";
		$event_info_query = tep_db_query($event_info_sql);
	
		while($event_info=tep_db_fetch_array($event_info_query)){
			if($event_info['link_service']=='L'){
				$location_sql = "SELECT location_name,location_details from " . TABLE_EVENTS_LOCATIONS . " where location_id='" . (int)$event_info["events_location"] . "'";
				$location_query=tep_db_query($location_sql);
			} else if($event_info['link_service']=='S'){
				$location_sql = "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"] ."'";
				$location_query=tep_db_query($location_sql);
			}
			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"]),
												"stime"=>strtolower($event_info["events_stime"]),
												"etime"=>strtolower($event_info["events_etime"]));
		
		}
		// get max date of current event
		$max_date_sql = "SELECT max(start_date) as last_date from " . TABLE_EVENTS_SESSIONS . " where events_id in(" . $events_ids . ")";
		$max_date_query=tep_db_query($max_date_sql);
		$max_date_result=tep_db_fetch_array($max_date_query);
		if ($end_date>=$max_date_result["last_date"]) $navigate_right=false;
	}
	if (sizeof($events_details)>0) $event_found=true;
		
	// BOF: Lango Added for template MOD
	if (MAIN_TABLE_BORDER == 'yes'){
	//table_image_border_top(false, false, TEXT_HEADING_BROWSE);
	}
	// EOF: Lango Added for template MOD
	
	// select category details
	$cat_array=array();
	$cat_list_sql = "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_list_query=tep_db_query($cat_list_sql);
	//$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"]);
	}
	
?>
	<script language="JavaScript">
		function ChangeResult(mode){
			if (mode==1){
				document.frmView.post_action.value="nav_left";
			} else if (mode==2){
				document.frmView.post_action.value="nav_right";
			}else if (mode==3){
				document.frmView.post_action.value="view_date";
			} else if (mode==5){
				var element=document.frmView.selWeek;
				var spltDetail;
				var content;
				if (element.selectedIndex<0) return;
				content=element.options[element.selectedIndex].value;
				spltDetail=content.split("|");
				document.frmView.start_date.value=spltDetail[0];
				document.frmView.end_date.value=spltDetail[1];
				document.frmView.action="<?php echo tep_href_link(FILENAME_EVENTS_WEEK_VIEW,tep_get_all_get_params(),'NONSSL');?>";
			} else {
				document.frmView.action="<?php echo tep_href_link(FILENAME_EVENTS_WEEK_VIEW,tep_get_all_get_params(),'NONSSL');?>";
				document.frmView.post_action.value="change_cat";
			}
			document.frmView.submit();			
		}
		function OpenDayView(id){
			var element=document.frmView.elements["date_"+id];
			if (!element) return;
			document.frmView.cur_date.value=element.value;
			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>
		<td class="main" nowrap="nowrap">
		<table width='100%' border="0" cellspacing="0" cellpadding="2" align="left">
			<tr style="height:10"><td></td></tr>
			<tr class="eventTitleRow">
				<td valign="middle" width="100" align="right" class="eventTitle" nowrap="nowrap">&nbsp;&nbsp;<?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(4);">' . 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.' <a href="javascript:ChangeView(1)">' . tep_template_image_button("button_day_view.gif",IMAGE_DAY_VIEW,'align=absmiddle') . '</a>&nbsp;';
						echo tep_template_image_button("button_week_view_selected.gif",IMAGE_WEEK_VIEW,'align=absmiddle') . '&nbsp;';
						echo '<a href="javascript:ChangeView(3)">' . tep_template_image_button("button_month_view.gif",IMAGE_MONTH_VIEW,'align=absmiddle') . '</a>&nbsp;';
					?>
				</td>
			</tr>
		</table>
		</td>
	</tr>
	<tr>
		<td><?php echo tep_draw_separator('pixel_trans.gif','1','10');?></td>
	</tr>
	<?php if ($category_id>0) {
						// select current events category
						$cat_sql = "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_query=tep_db_query($cat_sql);
						$cat_info=tep_db_fetch_array($cat_query);
					}	
					?>
	<tr>
		<td><table cellpadding="0" cellspacing="0">
			<tr>
				<td width="15"></td>
				<td valign="top" class="headerBrowse"><?php echo $cat_info['events_categories_name'];?></td>
			</tr>
		</table></td>		
	</tr>				
	<tr>
		<td>
		<table border="0" cellpadding="10" cellspacing="0" width="100%">
		<tr>
			<td>
			
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr>
					<td class="contentBackCol1" width="6"></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>
					<?php if($category_id>0) { ?>
					<tr>
						<td>
						<table border="0" cellpadding="2" cellspacing="3" valign="top" width="100%" class="tab_strip">
							<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,SMALL_IMAGE_HEIGHT,'align="left"'));
										echo $cat_info['events_categories_description'];
									?>
								</td>
							</tr>
						</table>
						</td>
					</tr>
					<?php } 
					?> 
					
					<tr>
						<td>
						<table border="0" cellspacing="0" cellpadding="1" valign="top" width="100%" class="calender_frame" height="40">
							<tr>
								<!--<td valign="top" width="20"><?php if ($navigate_left) echo '<a href="javascript:ChangeResult(1);">' . tep_template_image_button("navigate_left.gif",IMAGE_PREVIOUS_DAY) . '</a>';?></td>-->
								<td valign="middle" align="center" class="headerSubBrowse">
								    <?php //echo $cur_week_date.'<br>'; print_r($week_array); ?>
									 Week: <?php echo tep_draw_pull_down_menu("selWeek",$week_array,$cur_week_date,'OnChange="ChangeResult(5)"');?>
								</td>
								<!--<td valign="top" width="20"><?php if ($navigate_right) echo '<a href="javascript:ChangeResult(2);">' . tep_template_image_button("navigate_right.gif",IMAGE_NEXT_DAY) . '</a>';?></td>-->
							</tr>
						</table>
						</td>
					</tr>
					
					<td class="headerCalendarRow">
					  <table border="0" cellspacing="0" cellpadding="0" valign="top" width="100%" height="29"> 
						<tr height="20">
								<?php 
									// display the week name header
									
									//$header_text_sub_gray='headerSubCalendar';
									for ($icnt=1;$icnt<=7;$icnt++){
										if ($icnt==7)
											$class="headerCalendarSunday";
										else
											$class="headerCalendar";
										echo '<td class="' . $class . '" width="80" align="center" onClick="javascript:OpenDayView(' . $icnt . ');">' . 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">
							
							<tr height="100">
									<?php
											$temp_date=$start_date;
											$cur_date=$server_date;
	 										 $flag=0;
											// display the selected week days and event details
											for ($icnt=1;$icnt<=7;$icnt++) {
												echo '<input type="hidden" name="date_' . $icnt . '" value="' . $temp_date . '">';
											$header_text_sub_gray1='headerSubCalendar';
											$calEventName="calEventName";
											$calEventTime="calEventTime";
											if ($icnt==7){
												$class="dataCalendarSunday";
												$header_text_sub_gray1='headerSundayGray1';
												$calEventName="calEventNameSunday";
												$calEventTime="calEventTimeSunday";
											} else if ($icnt%2==0){
												$class="dataCalendarEven";
											} else {
												$class="dataCalendarOdd";
											}
									?>
									<Td valign="top" width="12%" class="<?php echo $class;?>">
									<table border="0" cellspacing="0" cellpadding="3" valign="top" width="100%" height="100%">
										<tr height="20">
											<?php if ($temp_date<$cur_date) { ?>
												<Td class="tdSubCalendarGray" valign="top"><span class="headerSubCalendarGray" onClick="javascript:OpenDayView(<?php echo $icnt;?>);"><?php echo date("d",strtotime($temp_date));?></span></Td>
											<?php } else{?>
												<Td valign="top"><span class="<?php echo $header_text_sub_gray1;?>" onClick="javascript:OpenDayView(<?php echo $icnt;?>);"><?php echo date("d",strtotime($temp_date));?></span></Td>
											<?php }?>
										</tr>					
										<?php
												$rtime=false;
												if(defined('TIME_FORMAT')) {
													if(TIME_FORMAT=='24') 
														$rtime=true;
												}
											if (isset($date_details[$temp_date])){
												// display the events belong to the current date
												$date_info=&$date_details[$temp_date];
												for ($jcnt=0;$jcnt<sizeof($date_info);$jcnt++) {
					 							$events_id=$date_info[$jcnt]["events_id"];
												$sessions_id=$date_info[$jcnt]["sessions_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]["stime"];
													$end_time=$events_details[$events_id]["etime"];
												}	
										?>
												<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=" . $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 } 
												
											}
										?>
									</table>
									</td>
								<?php 
									// increment the date
									$temp_date=date("Y-m-d",tep_dateadd(strtotime($temp_date),"day",1));
									} // for icnt
								?>
								</tr>
							</table>
							</td>
					</tr>
					<tr>
						<td class="main">
						<table width='100%' border="0" cellspacing="0" cellpadding="2" align="left">
							<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 class="contentBackCol3" width="6"></td>
			</tr>
		</table>	
		</td>
		</tr>
		</table>
		</td>
	</tr>
	<tr>
		<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
	</tr>
<input type="hidden" name="start_date" value="<?php echo $start_date;?>">
<input type="hidden" name="end_date" value="<?php echo $end_date;?>">
<input type="hidden" name="cur_date" value="<?php echo $temp; ?>" />
<input type="hidden" name="post_action" value="chane_cat">
</form>

<?php
	// display the post details as hidden values for back action
	$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","week");
	if (MAIN_TABLE_BORDER == 'yes'){
		table_image_border_bottom();
	} 
?>
</table>
