<?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
	$secCat_path="";

	if ($secPath!="") $secCat_path.="&secPath=" . $secPath;
	
	echo tep_draw_form("frmView",tep_href_link(FILENAME_SERVICES_MONTH_VIEW,$secCat_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_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"];
	}

	
	// creation an filter if category_id is present
	if ($category_id>0) $cat_option=" and sc.services_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;
	
	$service_ids="";
	$service_found=false;
	// select the service of current service category id
	$find_service_query=tep_db_query("SELECT distinct se.service_id,sed.service_name from " . TABLE_SERVICES . " se, " . TABLE_SERVICES_DESCRIPTION  . " sed,". TABLE_SERVICES_TO_SERVICES_CATEGORIES  . " se2c  where se2c.service_id=se.service_id and sed.service_id=se.service_id " . $cat_option . SERVICES_CONDITION_STATUS . " order by se.service_id");
		
		$service_details=array();
		$icnt=0;
	// select the event ids and session ids
	while($find_service_result=tep_db_fetch_array($find_service_query)){
		$service_id=$find_service_result["service_id"];
		$service_name=$find_service_result["service_name"];
		
		
			/*$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;*/

			if (!isset($service_details[$service_id])){
			$service_details[$service_id]=1;
			$service_ids.=$service_id . ",";
		}
	}
	echo $service_ids;
	echo $service_name;
	// select the event details
	if ($service_ids!=""){
		$service_ids=substr($service_ids,0,-1);
		
		
		
		$service_info_query = tep_db_query("select se.service_id, sed.service_name, date_format(se.service_date_available,'%Y-%m-%d') as service_date_available,se.service_date_added,se.service_tax_class_id, se.service_costs  from " . TABLE_SERVICES . " se , " . TABLE_SERVICES_DESCRIPTION . " sed  where se.service_id in (" . $service_ids . ")  and (se.service_status>=1) and date_format(se.service_date_available,'%Y-%m-%d')>=curdate() and sed.service_id = se.service_id and sed.language_id = '" . (int)$languages_id . "'");
		
		
		while($service_info=tep_db_fetch_array($service_info_query)){
			$service_id=$service_info["service_id"];
			$service_details[$service_id]=array("service_name"=>$service_info["service_name"],
												"service_date_available"=>$service_info["service_date_available"],
												"service_costs"=>$service_info["service_costs"],
												"location"=>$service_info["location_name"],
												"tax_id"=>$service_info["service_tax_class_id"],
												"start_time"=>strtolower($service_info["service_start_time"]),
												"end_time"=>strtolower($service_info["service_end_time"]));
												
											echo $service_details[$service_id]['service_date_available'];
											echo '<br>';
		}
	}
	echo sizeof($service_details[$service_id]);
	//print_r($service_details[$service_id]);
	// 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 event category details
	$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"]);
	}

	// 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_SERVICES_MONTH_VIEW);?>";
				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.current_date.value=dateValue;
			document.frmView.action="<?php echo tep_href_link(FILENAME_SERVICES_DAY_VIEW,$secCat_path);?>";
			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>
			
	<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" align="left" width="140" class="eventTitle"><?php echo TEXT_SERVICE_CATEGORY;?></td>
				<td valign="middle" class="eventTitle">
					<?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="300" class="eventTitle">
					<?php 
						echo '<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%">
		<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 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" cellpadding="2" cellspacing="3" 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>
								<td><?php echo tep_draw_separator('pixel_trans.gif',1,20);?></td>
							</tr>
							<tr>
								<td valign="top">
								<table border="0" cellspacing="0" cellpadding="2" valign="top" width="100%">
									<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_SERVICES,'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>
								<td><?php echo tep_draw_separator('pixel_trans.gif',1,15);?></td>
							</tr>
							<tr>
								<td valign="top">
								<table border="0" cellspacing="1" cellpadding="0" valign="top" width="100%" class="formArea">
									<tr height="20">
										<?php 
											// display the week day names header
											for ($icnt=1;$icnt<=7;$icnt++){
												if ($icnt==7)
													$class="headerCalendarSunday";
												else
													$class="headerCalendar";
												echo '<td class="' . $class . '" align="center">' . constant("TEXT_DAY" . $icnt) . '</td>';
											}
										?>
									</tr>
											<?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) {
															if ($day_cnt % 7==0) {
																$class=	"dataCalendarSunday";
															} 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="12%" 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 valign="top" class="tdSubCalendarGray1"><span class="headerSubCalendarGray1" onClick="javascript:OpenDayView('<?php echo $temp_date;?>');"><?php echo date("d",$temp_start_time);?></span></Td>
																				<?php } else {?>
																			<Td valign="top"><span class="headerSubCalendar" 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 
												//	echo $date_details[$temp_date];
													//		if (isset($date_details[$temp_date])){
														//		$date_info=&$date_details[$temp_date];
																
							$service_id_array=split(',',$service_ids);
							for($icnt=0;$icnt<sizeof($service_id_array);$icnt++)
							{
							$service_id=$service_id_array[$icnt];
							$no_booking_dates_query=tep_db_query("select no_booking_days from " .TABLE_SERVICES_NO_BOOKING_DAYS . " where service_id= '".$service_id . "'");
							if($temp_date>=$service_details[$service_id]['service_date_available'])
								echo ' <a href="' . tep_href_link(FILENAME_SERVICES_INFO,'service_id=' . $service_id . $secCat_path) . '">' .  '<font style="font-family: Verdana, Arial, sans-serif;
  font-size: 10px;" >'.$service_details[$service_id]['service_name'] .'</font>'. "</a>".'<br>';
  							} // 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>  
						<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="current_date" value="<?php echo $server_date;?>">
						<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_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","month");
						?>
						<tr>
								<td class="main">
								<table width='100%' border="0" cellspacing="0" cellpadding="2" align="right">
									<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 width="5" class="contentBackCol3"></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
	?>		
			
</table>



