<?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

	$secCat_path="";

	if ($secPath!="") $secCat_path.="&secPath=" . $secPath;

	echo tep_draw_form("frmView",tep_href_link(FILENAME_SERVICES_DAY_VIEW,$secCat_path));
	
	$category_id=(isset($HTTP_POST_VARS['category_id'])?(int)$HTTP_POST_VARS['category_id']:0);
	$current_date=(isset($HTTP_POST_VARS['current_date'])?tep_db_prepare_input($HTTP_POST_VARS['current_date']):getServerDate());
	$post_action=(isset($HTTP_POST_VARS['post_action'])?tep_db_prepare_input($HTTP_POST_VARS['post_action']):"change_cat");
	$server_date=getServerDate();
	
	//check for category_id from post data or current events_category_id selected or the first category from database
	if ($category_id<=0) $category_id=(int)$current_services_category_id;
	
	if ($category_id<=0) {
		$first_cat_query=tep_db_query("SELECT services_categories_id from " . TABLE_SERVICES_CATEGORIES_DESCRIPTION . " order by services_categories_name limit 1");
		$first_cat_result=tep_db_fetch_array($first_cat_query);
		$category_id=(int)$first_cat_result["services_categories_id"];
	}
	if ($category_id>0) $cat_option=" and ec.services_categories_id='" . $category_id . "'";

	// determine type of action
	if ($post_action=="view_date") {
		$current_date=tep_db_prepare_input($HTTP_POST_VARS['select_date']);
	}else if ($post_action=="nav_left"){
		$current_date=date('Y-m-d',tep_datesub(strtotime($current_date),"day",1));
	} else if ($post_action=="nav_right"){
		$current_date=date('Y-m-d',tep_dateadd(strtotime($current_date),"day",1));
	}
	$navigate_left=true;
	$navigate_right=true;

	// display of left navigation and right navigation buttons	
	if ($current_date<=$server_date) $navigate_left=false;
	
	$services_ids="";
	$services_found=false;
	// select the events id of category selected
	
	$find_services_query=tep_db_query("SELECT distinct se.service_id from  " . TABLE_SERVICES . " se, " . TABLE_SERVICES_TO_SERVICES_CATEGORIES  . " sec " .
									" where  sec.service_id=se.service_id and services_categories_id='".$category_id."'");

	while($find_services_result=tep_db_fetch_array($find_services_query)){
		$services_ids.=$find_services_result["service_id"] . ",";
		//$sessions_id=$find_events_result["sessions_id"];
	}

	// select the service details
	if ($services_ids!=""){
		$services_ids=substr($services_ids,0,-1);
		
		
		echo "select se.service_id, sed.service_name,se.service_location, se.service_date_available,se.service_date_added,sed.service_description,se.service_image, se.service_tax_class_id,se.service_costs, " .
										"se.service_start_time as start_time,time_format(se.service_start_time,'%h:%i %p') as service_start_time,time_format(se.service_end_time,'%h:%i %p') as service_end_time,se.service_location," . 
										"se.conditions from " . TABLE_SERVICES . " se, " . TABLE_SERVICES_DESCRIPTION .
										" sed where se.service_id in (" . $services_ids . ") " .  " and (se.service_status>=1) and date_format(se.service_date_available,'%Y-%m-%d')='".$current_date . "' and sed.service_id = se.service_id and sed.language_id = '" . (int)$languages_id . "' order by se.service_start_time,se.service_end_time";
		
		$services_info_query = tep_db_query("select se.service_id, sed.service_name,se.service_location, se.service_date_available,se.service_date_added,sed.service_description,se.service_image, se.service_tax_class_id,se.service_costs, " .
										"se.service_start_time as start_time,time_format(se.service_start_time,'%h:%i %p') as service_start_time,time_format(se.service_end_time,'%h:%i %p') as service_end_time,se.service_location," . 
										"se.conditions from " . TABLE_SERVICES . " se, " . TABLE_SERVICES_DESCRIPTION .
										" sed where se.service_id in (" . $services_ids . ") " .  " and (se.service_status>=1) and date_format(se.service_date_available,'%Y-%m-%d')='".$current_date . "' and sed.service_id = se.service_id and sed.language_id = '" . (int)$languages_id . "' order by se.service_start_time,se.service_end_time");
		if (tep_db_num_rows($services_info_query)>0) $services_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 the list of categories	
	$cat_array=array();
	$cat_list_query=tep_db_query("SELECT secd.services_categories_name,secd.services_categories_id from " . TABLE_SERVICES_CATEGORIES . " sec, " . 
								TABLE_SERVICES_CATEGORIES_DESCRIPTION . " secd where secd.services_categories_id=sec.services_categories_id and secd.language_id='" . 
								$languages_id . "' order by services_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["services_categories_id"],'text'=>$cat_list_result["services_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 {
				document.frmView.action="<?php echo tep_href_link(FILENAME_SERVICES_DAY_VIEW);?>";
				document.frmView.post_action.value="change_cat";
			}
			document.frmView.submit();			
		}
		function ChangeView(mode){
			switch(mode){
				case 1:
					document.frmView.action="<?php echo tep_href_link(FILENAME_SERVICES_DAY_VIEW,$secCat_path);?>";
					break;
				case 2:
					document.frmView.action="<?php echo tep_href_link(FILENAME_SERVICES_WEEK_VIEW,$secCat_path);?>";
					break;
				case 3:
					document.frmView.action="<?php echo tep_href_link(FILENAME_SERVICES_MONTH_VIEW,$secCat_path);?>";
					break;
			}
			document.frmView.submit();
		}
		function ViewPrevious(){
			document.frmPrevious.submit();
		}
	</script>
	<link rel="stylesheet" type="text/css" href="includes/javascript/spiffyCal/spiffyCal_v2_1.css">
	<?php require(DIR_WS_JAVASCRIPT . "spiffyCal/spiffyCal_v2_1.js");?>
	<script language="javascript">
	  var dateSelect = new ctlSpiffyCalendarBox("dateSelect", "frmView", "select_date","btnSelect","<?php echo $current_date;?>",scBTNMODE_CUSTOMBLUE);
	</script>
<table width='100%' border="0" cellspacing="0" cellpadding="0" align="left">
	<tr height="20">
		<td class="main" valign="top">
		<table width='100%' border="0" cellspacing="0" cellpadding="0" align="left">
			<tr class="eventTitleRow">
				<td valign="middle" width="130" class="eventTitle" align="left">&nbsp;&nbsp;<?php echo TEXT_SERVICES_CATEGORY;?></td>
				<td valign="middle" class="eventListing-data">
					<?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">
					<?php 
						echo tep_template_image_button("button_day_view_selected.gif",IMAGE_DAY_VIEW,'align=absmiddle') . '&nbsp;';
						echo '<a href="javascript:ChangeView(2)">' . tep_template_image_button("button_week_view.gif",IMAGE_WEEK_VIEW,'align=absmiddle') . '</a>&nbsp;';
						echo '<a href="javascript:ChangeView(3)">' . tep_template_image_button("button_month_view.gif",IMAGE_MONTH_VIEW,'align=absmiddle') . '</a>&nbsp;&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%">
		<tr>
			<td>
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<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>
					<?php if ($category_id>0) {
						// select the current category details
						$cat_query=tep_db_query("SELECT secd.services_categories_name,sec.services_categories_image,if(CHAR_LENGTH(secd.services_categories_description)>250,concat(left(secd.services_categories_description,250),'...'),secd.services_categories_description) as services_categories_description from " . 
													TABLE_SERVICES_CATEGORIES . " sec, " . TABLE_SERVICES_CATEGORIES_DESCRIPTION . " secd " .
												" where secd.services_categories_id='" . $category_id . "' and secd.language_id='" . $languages_id . "' and secd.services_categories_id=sec.services_categories_id");
						$cat_info=tep_db_fetch_array($cat_query);
					?>
					<tr height="100">
						<td valign="top">
						<table border="0" cellspacing="0" cellpadding="2" valign="top" width="100%">
							<tr>
								<td valign="top" class="headerBrowse"><?php echo $cat_info['services_categories_name'];?></td>
							</tr>
							<tr>
								<td valign="top" class="smalltext">
									<?php 
									echo ($cat_info['services_categories_image']==''?'':tep_image(DIR_WS_IMAGES . $cat_info['services_categories_image'] , $cat_info['services_categories_name'],SMALL_IMAGE_WIDTH,SMALL_IMAGE_HEIGHT,'align=right'));
									echo $cat_info['services_categories_description'];
								?>
								</td>
							</tr>
						</table>
						</td>
					</tr>
					<?php }
					?>
					<tr height="20">
						<td valign="top">
						<table border="0" cellspacing="0" cellpadding="2" valign="top" width="100%">
							<tr class='eventListing-data'>
								<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">
									<script language="javascript">dateSelect.writeControl(); dateSelect.dateFormat="yyyy-MM-dd";</script>&nbsp;
									<?php echo '<a href="javascript:ChangeResult(3);">' . tep_template_image_button("button_quick_find.gif",IMAGE_VIEW_DATE,'align=absmiddle') . '</a>';?>
								</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>
					<tr height="200">
						<td valign="top">
						<table border="0" cellspacing="2" cellpadding="2" valign="top" width="100%">
						<?php 
							if ($services_found) {
								// if events found on current criteria
								while($services_info=tep_db_fetch_array($services_info_query)) {
							// Get the location details of event
							
							$location_info_query = tep_db_query("select location_name from " . TABLE_EVENTS_LOCATIONS . " where location_id = '" . (int)$services_info['service_location'] . "'");
							$location_info = tep_db_fetch_array($location_info_query);
							$location_name = $location_info['location_name']; 
								
							//	$events_name=tep_get_events_display_text($event_info["events_name"],$event_info["start_time"],$location_info["location_name"]);
								
						?>
							<tr>
								<Td>
								<table border="0" cellspacing="2" cellpadding="2" valign="top" width="100%" class="formArea">
									
									<tr class="dayBoxTop">
										<td class="headerSubBrowse">
										
										<?php echo $services_info['service_name'];?>
											<?php //echo '<a class="headerSubBrowse" href="' . tep_href_link(FILENAME_SESSION_DETAILS,'events_id=' . $event_info["events_id"] . "&sId=" . $sessions_id . "&" . $ecCat_path) . '">' . $events_name  .'</a>';?>
										</td>
									</tr>
									<tr>
										<td valign="top" class="dataCalendarOdd">
										<table border="0" cellspacing="0" cellpadding="2" valign="top" width="100%">
										<tr>
								<td valign="top" class="headerBrowse"><?php //echo $cat_info['services_categories_name'];?></td>
							</tr>
											<tr class='eventListing-data'>
											
											
												<td valign="top" width="<?php echo SMALL_IMAGE_WIDTH;?>">
													<?php 
															//echo '<a href="' . tep_href_link(FILENAME_SESSION_DETAILS,'events_id=' . $event_info["events_id"] . "&sId=" . $sessions_id . "&" . $secCat_path) . '">';
															if ($services_info['service_image']!=""){
																echo tep_image(DIR_WS_IMAGES . $services_info['service_image'] , $services_info['service_name'],SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT);
															 } else if ($cat_info['services_categories_image']!="") {
																echo tep_image(DIR_WS_IMAGES . $cat_info['services_categories_image'] , $services_info['service_name'],SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT);
															 }
															 echo '</a>';
													?>
												</td>
												<td valign="top" width="250" class="smalltext">
													<?php 
														$text_description=strip_tags($services_info['service_description'],'<p><br><b><i><u>');
														if (strlen($text_description)>250)
															$text_description=substr($text_description,0,250) . "...";
															
														echo $text_description;
													?>
												</td>
												
												<td valign="top">
												<table border="0" cellpadding="0" cellspacing="6">
													<tr>
														<td class="eventTitle" valign="top"><?php echo TEXT_WHEN;?></td>
														<td class="eventListing-Data" valign="top"><?php echo $service_text . " " . strtolower($services_info["service_start_time"]) . " - " . strtolower($services_info["service_end_time"]);?></td>
													</tr>
													<tr>
														<td class="eventTitle" valign="top"><?php echo TEXT_LOCATION;?></td>
														<td class="eventListing-Data" valign="top"><?php echo $location_info["location_name"];?></td>
													</tr>
													<tr>
														<td class="eventTitle" valign="middle"><?php echo TEXT_COST;?>
														<td class="eventListing-Data" valign="middle">
														<?php 	
															echo $services_info["service_costs"];
															?>
													</tr>
													
													
													</table>
												</td>
											</tr>
										</table>
										</td>
									</tr>
								</table>
								</td>
							</tr>
							<tr>
								<td><?php echo tep_draw_separator('pixel_trans.gif',1,10);?></td>
							</tr>	
						<?php }
							}
							if (!$services_found) {
						?>
							<tr>
								<td>
								<table border="0" cellpadding="4" cellspacing="0" class="formArea" width="100%">
									<tr>
										<Td class="main" align="center"><b>No services found!</b></Td>
									</tr>				
								</table>
								</td>
							</tr>
						<?php } ?>
						</table>
						</td>
					</tr>
	<tr>
		<td class="main" valign="top">
		<table width='100%' border="0" cellspacing="0" cellpadding="2" align="left">
			<tr>
				<td>
				<?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="5"></td>
				</tr>
			</table>
			</td>
			</tr>

		</table>
		</td>
	</tr>
	</table>
	</td>
	</tr>
<?php
	// BOF: Lango Added for template MOD
	if (MAIN_TABLE_BORDER == 'yes'){
	table_image_border_bottom();
	}
	// EOF: Lango Added for template MOD
?>
<input type="hidden" name="start_date" value="<?php echo $current_date;?>">
<input type="hidden" name="end_date" value="<?php echo $current_date;?>">
<input type="hidden" name="current_date" value="<?php echo $current_date;?>">
<input type="hidden" name="post_action" value="">
</form>
<?php
	// construct current post details as hidden details to work out "back" action
	$page_param=(isset($HTTP_SESSION_VARS["calendar_style"])?$HTTP_SESSION_VARS["calendar_style"]:"day");
	if ($page_param=="month") {
		$page_name=FILENAME_SERVICES_MONTH_VIEW;
	} else if ($page_param=="week"){
		$page_name=FILENAME_SERVICES_WEEK_VIEW;
	} else if ($page_param=="day"){
		$page_name=FILENAME_SERVICES_DAY_VIEW;
	} else {
		$page_name=basename($GLOBALS["PHP_SELF"]);
	}

	echo tep_draw_form("frmPrevious",tep_href_link($page_name,$secCat_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","day");
?>
</table>
<?php 
 /*  function tep_get_event_daytext($session_result)
   {
     $type=$session_result['sessions_type_option'];
     $result_text='';
     //$days_array=array('1'=>'Sunday','2'=>'Monday','3'=>'Tuesday','4'=>'Wednesday','5'=>'Thursday','6'=>'Friday','7'=>'Saturday');
     $days_array=array('1'=>'Monday','2'=>'Tuesday','3'=>'Wednesday','4'=>'Thursday','5'=>'Friday','6'=>'Saturday','7'=>'Sunday');
     $count_array=array('1'=>'first','2'=>'second','3'=>'third','4'=>'fourth');
     $months_array=array('1'=>'January','2'=>'February','3'=>'March','4'=>'April','5'=>'May','6'=>'June','7'=>'July','8'=>'August','9'=>'September','10'=>'October','11'=>'November','12'=>'December');
     switch($type)
     {
        case 'DD':
		  if ($session_result['sessions_type_daily_days']<=1){
			$result_text=TEXT_DAILY_DAY;}
		  else
			$result_text=sprintf(TEXT_DAILY_DAYS,$session_result['sessions_type_daily_days']);
		  break;
        case 'WW':
		  $temp_string=$session_result['sessions_type_weekly_days'];
		  $day_string="";
		  for ($icnt=0;$icnt<strlen($temp_string);$icnt++){
			 if (substr($temp_string,$icnt,1)=='1'){
			   if ($day_string!='') $day_string=$day_string . ", ";
			   $day_string=$day_string . $days_array[$icnt+1];
			 }
		  }
		  if ($session_result['sessions_type_weekly_weeks']<=1)
			$result_text=sprintf(TEXT_WEEKLY_WEEK,$day_string);
		  else
			$result_text=sprintf(TEXT_WEEKLY_WEEKS,$session_result['sessions_type_weekly_weeks'],$day_string);
		  break;
		case 'MD':
		  if ($session_result['sessions_type_monthly_months']>1)
			$result_text=sprintf(TEXT_MONTHLY_DAYS,$session_result['sessions_type_monthly_months'],$session_result['sessions_type_monthly_days']);
			  else
			$result_text=sprintf(TEXT_MONTHLY_DAY,$session_result['sessions_type_monthly_days']);
		  break;
        case 'MM':
		  if ($session_result['sessions_type_monthly_weekmonths']>1)
			  $result_text=sprintf(TEXT_MONTHLY_WEEKS,$session_result['sessions_type_monthly_weekmonths'],$count_array[$session_result['sessions_type_monthly_weeks']],$days_array[$session_result['sessions_type_monthly_weekdays']]);
			  else
			  $result_text=sprintf(TEXT_MONTHLY_WEEK,$count_array[$session_result['sessions_type_monthly_weeks']],$days_array[$session_result['sessions_type_monthly_weekdays']]);	  
		  break;
        case 'YM':
		  $result_text=sprintf(TEXT_YEARLY_MONTHS,$months_array[$session_result['sessions_type_yearly_months']],$session_result['sessions_type_yearly_monthdays']);
		  break;
        case 'YY':
	  $result_text=sprintf(TEXT_YEARLY_WEEKS,$count_array[$session_result['sessions_type_yearly_weeks']],$days_array[$session_result['sessions_type_yearly_weekdays']],$months_array[$session_result['sessions_type_yearly_weekmonths']]);
	  break;
     }
     return $result_text;   
   }*/
  ?>