<?php
/*
	osCommRes, Services Oline 
	http://www.oscommres.com 
	Copyright (c) 2005 osCommRes 
	
	Freeway eCommerce 
	http://www.zac-ware.com/freeway 
	Copyright (c) 2007 ZacWare
	
	Released under the GNU General Public License
*/ 


// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	
 	  if (!function_exists('tep_db_connect')) require('includes/application_top.php');
  	  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_SERVICES_INFO);
	  //get initial parameters
	  tep_session_unregister('services');
	  tep_session_unregister('time_details');
	  $error="";
	  $error=urldecode(tep_db_prepare_input($HTTP_GET_VARS['error']));
	  $service_id=$service_costs=0;
	  $current_date=getServerDate();
	  $current_date_string=getServerDate(false);
	  $mode=$gparam=$no_booking_sdate=$no_booking_edate=$no_booking_stime=$no_booking_etime="";
	  $category_id=isset($HTTP_GET_VARS['category_id'])?(int)$HTTP_GET_VARS['category_id']:'0';
	  if (isset($HTTP_GET_VARS['service_id']) && is_numeric($HTTP_GET_VARS['service_id'])) $service_id=(int)$HTTP_GET_VARS['service_id'];
	  if(!$service_id) tep_redirect(tep_href_link(FILENAME_DEFAULT));
	  $service_info=get_services_info($service_id,$languages_id);
	  if (sizeof($service_info)>0) $service_costs=$currencies->display_price($service_info['service_costs'],tep_get_tax_rate($service_info['service_tax_class_id']));
	  if($view=='month') $gparam='&view=month';
	  $round_amount=(int)tep_add_tax($service_info["service_costs"],tep_get_tax_rate($service_info["service_tax_class_id"]));
	  $service_location=tep_get_service_location_name($service_info["service_location"]);
	  $service_name=$service_info['service_name'];
	  $service_conditions=$service_info["conditions"];
	  $service_description=$service_info["service_description"];
	  $service_url=$service_info["service_url"];
	  $service_date_available=$service_info['service_date_available'];
	  if($service_info["service_tax_class_id"]>0)$service_tax_class_id=(int)$service_info["service_tax_class_id"];
	  $old_params=tep_get_page_params(array('service_id','secPath'));
	  $month=(int)(isset($HTTP_GET_VARS["month"])?$HTTP_GET_VARS["month"]:0);
	  $year=(int)(isset($HTTP_GET_VARS["year"])?$HTTP_GET_VARS["year"]:0);
	  $day=(int)(isset($HTTP_GET_VARS["day"])?$HTTP_GET_VARS["day"]:'');
	  $view=(isset($HTTP_GET_VARS["view"])?tep_db_prepare_input($HTTP_GET_VARS["view"]):'week');
	  $current_month=(int)date("m",$current_date_string);
	  $current_year=(int)date("Y",$current_date_string); 
	  $granular=(int)($service_info["booking_granular"])?$service_info["booking_granular"]:1;
	  $slot=(int)round(60/$granular);
	  $max_bookings=(int)($service_info["max_bookings"])?$service_info["max_bookings"]:1;
	  $time_length=(int)($service_info["service_total_time"])?$service_info["service_total_time"]:1;
	  $booking_type=$service_info["booking_type"];	  
	  $span_days=$service_info["span_days"];
	  $include_resource=($service_info['include_resource'])?$service_info['include_resource']:'N';
	  $booking_buffer=$service_info['booking_buffer'];
	  $booking_buffer_split=split(",",$booking_buffer);
	  $book_buffer='N';
	  $book_buffer_minute=0;
	  if($booking_buffer!='' && $booking_buffer_split[1]){
	  	 $book_buffer=$booking_buffer_split[0]; 
	  	 $book_buffer_minute=$booking_buffer_split[1];
	  }
	  if($include_resource=='N')
			$service_quantity=$service_info["quantity"];
		else{
			$service_quantity=1;
			$service_resource_query=tep_db_query("select count(*) as count from ".TABLE_SERVICES_TO_SERVICES_RESOURCES." sTr LEFT JOIN ".TABLE_SERVICE_RESOURCE." r on sTr.service_resource_id=r.resource_id, ".TABLE_SERVICE_RESOURCE_DESCRIPTIONS." red where red.resource_id=r.resource_id and red.resource_id=sTr.service_resource_id and sTr.service_id=".tep_db_input($service_id)." and red.language_id='".(int)$languages_id."' group by service_id");
			if(tep_db_num_rows($service_resource_query)>0){
				$service_resource=tep_db_fetch_array($service_resource_query);
				$service_quantity=$service_resource['count'];	
			}	
		}
		if ($month<=0 || $month>12) $month=$current_month; 
	 	if ($current_year<=0) $year=$current_year;	
	 	if ($view=="month") $day=1;
	 	if (!checkdate($month, $day, $year)){
			$year=(int)date("Y",$current_date_string);
			$month=(int)date("m",$current_date_string);
	 	}	
	 	$shour=0; 
		$ehour=23;
  	//get initial parameters
   // update service viewed count
		tep_db_query("update " . TABLE_SERVICES_DESCRIPTION . " set service_viewed = service_viewed+1 where service_id = '" . tep_db_input($service_id) . "' and language_id = '" . (int)$languages_id . "'");
   // update service viewed count
	// prepare the details to show in the month view
		if ($view=="month"){
			if ($year==$current_year && $month<$current_month) $month=$current_month;
			if ($year<$current_year) $year=$current_year;
			$total_days=1;
			$month_start_time=mktime(0,0,0,$month,1,$year); 
			$total_days=date("t",$month_start_time);
			$week_day=date("w",$month_start_time);
			$month_start_date=date('Y-m-d',$month_start_time);
			$month_end_time=mktime(0,0,0,$month,$total_days,$year);
			$month_end_date=date('Y-m-d',$month_end_time);
			$combo_start_date=$current_date; // combo start_date
			$service_start_time="00:00:00"; // start_time
			$service_end_time="23:59:00"; // end time
			if ($week_start_date>$combo_start_date) $combo_start_date=$week_start_date;
			$combo_end_date=date('Y-m-d',tep_dateadd($week_end_time,"day",60));
			// get no booking dates
			$bk_query=tep_db_query("select no_booking_start_date,no_booking_end_date,no_booking_start_time,no_booking_end_time,recurring_type,recurring_details from ".TABLE_SERVICES_NO_BOOKING_DAYS ." snd where snd.service_id='".(int)$service_id."' and (('$combo_start_date'>=no_booking_start_date and '$combo_start_date'<=no_booking_start_date) OR  ('$month_end_date'>=no_booking_start_date and '$month_end_date'<=no_booking_end_date) OR ('$combo_start_date'<=no_booking_start_date and '$month_end_date'>=no_booking_end_date)) order by no_booking_start_date,no_booking_end_date,no_booking_start_time,no_booking_end_time");
			$shour=0;
			$ehour=23;
			$nbd_array=array();
			$no_booking_sdate='';
			$no_booking_edate='';
			$cur_time=date("H",$current_date_string) .':00:00';
			$start_date=$combo_start_date;
			$end_date=$month_end_date;
			while($nbk=tep_db_fetch_array($bk_query)){
				$recurring_type=$nbk['recurring_type'];
				if($recurring_type!='DD'){
					$sInfo=$nbk;
					$nb_dates=get_dates_array($sInfo,$recurring_type);
					//$start_date=$month_start_date;
					$nbd_array[]=formated_type_dates('dates');
					$nb_dates_array[]=formated_type_dates('n_dates');
				}else if($recurring_type=='DD'){
					 if($booking_type=='D') $no_booking_sdate_array[]=array('start_date'=>$nbk['no_booking_start_date'],'end_date'=>$nbk['no_booking_end_date']);
					 $temp_month_start_date=$month_start_date;
					 while($temp_month_start_date<=$month_end_date){
					 	if($nbk['no_booking_start_date']<=$temp_month_start_date && $nbk['no_booking_end_date']>=$temp_month_start_date  && $current_date<=$temp_month_start_date){
					 		$no_b_stime=$nbk['no_booking_start_time'];
							$no_b_etime=$nbk['no_booking_end_time'];
							if($booking_type=='D'){
								$no_b_stime="00:00:00";
								$no_b_etime="24:00:00";
							}
							$no_b_sdate=$temp_month_start_date;
							$no_b_edate=$temp_month_start_date;	
					 		if($no_b_etime<=$no_b_stime && $span_days=='Y')
								$no_b_edate=date("Y-m-d",tep_dateadd(@strtotime($temp_month_start_date),"day",1));
							else if($no_b_etime<=$no_b_stime && $span_days=='N')
								$no_b_etime="24:00:00";
							if($temp_month_start_date<=$no_b_sdate && $month_end_date>=$no_b_sdate){
								$nbd_array[][]=array('type'=>$recurring_type,'n_s_date'=>$no_b_sdate,'n_e_date'=>$no_b_edate,'n_s_time'=>$no_b_stime,'n_e_time'=>$no_b_etime);
								$nb_split=split(':',$no_b_etime);
								
								if($no_b_stime=='00:00:00' && ($no_b_etime>=(24-$time_length).':05:00' && $no_b_etime<='24:00:00') || (($booking_type=='H' || $booking_type=='M')  && $time_length!='' && date("d",mktime(($nb_split[0]+$time_length),$mt,0,1,1,1987))=="02")){ 
									$nb_dates_array[][format_date($no_b_sdate)]='full';
								}		
							 	if($no_b_stime<=$cur_time && ($no_b_etime>=(24-$time_length).':05:00' && $no_b_etime<='24:00:00') && $no_b_sdate<=$combo_start_date && $no_b_edate>=$combo_start_date){
									$nb_dates_array[][format_date($no_b_sdate)]='full';
							 	}
							}  	
					 	} 
					     if($temp_month_start_date) $temp_month_start_date=date('Y-m-d',tep_dateadd(@strtotime($temp_month_start_date),"day",1));
					 }
				}		
			}
			$block_dates=$nb_dates_array;
			// get no booking dates
			// service date array
			$dates_array=tep_get_service_dates_array($combo_start_date,$month_end_date,$booking_type);
			//calender styles details 
			for ($icnt=$shour;$icnt<$ehour;$icnt++){
				for ($jcnt=0;$jcnt<7;$jcnt++){
					$week_array[$icnt][$jcnt]=array("class"=>"calendarBookingDefault","cnt"=>0);
				}
			}	
			$calendar=array();
			$jcnt=($total_days+$week_day);
			$jcnt=$jcnt+6-date('w',$month_end_time);
			
			for ($icnt=0;$icnt<$jcnt;$icnt++){
				$calendar[$icnt]=array("class"=>"calendarBookingDefault","total"=>0);
				if ($icnt<$week_day) {
					$calendar[$icnt]["class"]="calendarBookingPrevious";
				}
				if ($icnt>=$week_day+$total_days) $calendar[$icnt]["class"]="calendarBookingNext";
			}
			$month_sdate=date('Y-m-d',$month_start_time);
			$month_edate=date('Y-m-d',$month_end_time);
		$mi=$week_day; 		
		$booking_type=$service_info['booking_type'];
		while($month_sdate<=$month_edate){
 			if($current_date<=$month_sdate){ 				
 				$service_quantity=(($service_info['include_resource']=='Y')?1:$service_info['quantity']);
 				if($booking_type=='H' || $booking_type=='M'){
 					if($booking_type=='M') $service_quantity=round((24*60/$service_info['service_total_time']) * $service_quantity);
 					else $service_quantity=round((24/$service_info['service_total_time']) * $service_quantity);
 				}
 				$reserved_quantity=get_orders_products_quantity((int)$service_id,$month_sdate,$month_sdate,'00:00:00','23:59:00',$booking_type,0);
 				if($reserved_quantity>=$service_quantity) $calendar[$mi]["class"]="calendarBookingEmpty";
 				else if($reserved_quantity>0) $calendar[$mi]["class"]="calendarBookingPartial";
 				else $calendar[$mi]["class"]="calendarBookingDefault";				
 			}
 			if($month_sdate) $month_sdate=date('Y-m-d',tep_dateadd(@strtotime($month_sdate),"day",1));
			$mi++;
		}		
		 
		 $no_booking_datetime=array();
		 $blck_datetime=array();
			//calender styles
			for($j=0;$j<sizeof($nbd_array);$j++){
			 for($i=0;$i<sizeof($nbd_array[$j]);$i++){
			   $no_booking_sdate=$nbd_array[$j][$i]['n_s_date'];	
			   $no_booking_edate=$nbd_array[$j][$i]['n_e_date'];	
			   $no_booking_stime=$nbd_array[$j][$i]['n_s_time'];	
			   $no_booking_etime=$nbd_array[$j][$i]['n_e_time'];
			   $no_b_sdate_split=split("-",$no_booking_sdate);		
			   $no_b_edate_split=split("-",$no_booking_edate);
			   $no_booking_stime_splt=split(":",$no_booking_stime);
			   $no_booking_etime_splt=split(":",$no_booking_etime);
			    //$blck_datetime[]=array('start_date'=>$no_booking_sdate,'end_date'=>$no_booking_edate,'start_time'=>$no_booking_stime,'end_time'=>$no_booking_etime);
			    if($no_booking_stime>$no_booking_etime){
					if($span_days=='Y') $blck_datetime[sizeof($blck_datetime)]=array('start_date'=>$no_booking_edate,'end_date'=>$no_booking_edate,'start_time'=>'00:00:00','end_time'=>$no_booking_etime,'type'=>$nbd_array[$j][$i]['type']);
					$blck_datetime[]=array('start_date'=>$no_booking_sdate,'end_date'=>$no_booking_sdate,'start_time'=>$no_booking_stime,'end_time'=>'24:00:00','type'=>$nbd_array[$j][$i]['type']);							
				}else $blck_datetime[]=array('start_date'=>$no_booking_sdate,'end_date'=>$no_booking_edate,'start_time'=>$no_booking_stime,'end_time'=>$no_booking_etime,'type'=>$nbd_array[$j][$i]['type']);
			   
			   $str_sdate=mktime(0,0,0,$no_b_sdate_split[1],$no_b_sdate_split[2],$no_b_sdate_split[0]);
			   $str_edate=mktime(0,0,0,$no_b_edate_split[1],$no_b_edate_split[2],$no_b_edate_split[0]);
			    if($no_booking_stime<$no_booking_etime){
				 	 $ret1=tep_time_diff($no_booking_stime,$no_booking_etime);
				    if($ret1["hours"]>0) $hour=round($ret1["hours"]); else $hour=0;
				} 
				$no_booking_datetime[]=array('start_date'=>$no_booking_sdate,'time_diff'=>$hour);
				// prepare for no_booking dates
				for ($jcnt=0;$jcnt<$total_days;$jcnt++){
						if($jcnt==0) $date=date('Y-m-d',$month_start_time);
						else $date=date('Y-m-d',tep_dateadd($month_start_time,"day",$jcnt));
						if ($date==$no_booking_sdate && $date==$no_booking_edate &&($no_booking_stime=="00:00:00" && $no_booking_etime>="23:00:00" || $booking_type=="D"))
							$calendar[$jcnt+$week_day]["class"]="calendarNoBooking";
				 }
			  }
		  }
		  $pre_date="";
		  $pre_date_booking="";
		  $total_quantity="";		  
	  }	  
		// get no booking dates for  start
		if ($view=="week"){
		   $week_day=date("w",$current_date_string);
		   $service_start_time="00:00:00";
		   $combo_start_date=$current_date;
			$shour=0;
			$ehour=24;
			$nbd_array=array();
			$no_booking_sdate='';
			$no_booking_edate='';
			$booking_array=array(); 
			$cur_time=date("H",$current_date_string).':00:00';
			if ($month+1<date("m",$current_date_string) && $year==date('Y',$current_date_string)) $month=date("m",$current_date_string);
			if ($year<date("Y",$current_date_string)) {
				$year=date("Y",$current_date_string);
				$month=1;
			}
			if (!@checkdate($month, (int)$day, $year)){	
			  	$week_start_time=tep_datesub($current_date_string,"day",$week_day); 
				$week_end_time=tep_dateadd($week_start_time,"day",6);
				$week_start_date=date('Y-m-d',$week_start_time);
				$week_end_date=date('Y-m-d',$week_end_time);
			} else { 
				$week_start_time=mktime(0,0,0,$month,$day,$year);
				if (date("w",$week_start_time)!="0" && date("m",$week_start_time)==date('m',$current_date_string) && date("Y",$week_start_time)==date('Y',$current_date_string) ) $week_start_time=tep_datesub($current_date_string,"day",$week_day);
				$week_start_date=date('Y-m-d',$week_start_time);
				$week_end_time=tep_dateadd($week_start_time,"day",6);
				$week_end_date=date("Y-m-d",$week_end_time);
			}
			$combo_end_date=date('Y-m-d',tep_dateadd($week_end_time,"day",60));
			//no booking dates details
			$bk_query=tep_db_query("select no_booking_start_date,no_booking_end_date,no_booking_start_time,no_booking_end_time,recurring_type,recurring_details from ".TABLE_SERVICES_NO_BOOKING_DAYS ." snd where snd.service_id='".tep_db_input($service_id)."' and (('$combo_start_date'>=no_booking_start_date and '$combo_start_date'<=no_booking_end_date) OR  ('$combo_end_date'>=no_booking_start_date and '$combo_end_date'<=no_booking_end_date) OR ('$combo_start_date'<=no_booking_start_date and '$combo_end_date'>=no_booking_end_date)) order by no_booking_start_date,no_booking_start_time,no_booking_end_date,no_booking_end_time");
			while($nbk=tep_db_fetch_array($bk_query)){
				$recurring_type=$nbk['recurring_type'];
				$sInfo=$nbk;							
				if($recurring_type!='DD'){
					$nb_dates=get_dates_array($sInfo,$recurring_type);					
					//$start_date=$week_start_date;
					$start_date=$combo_start_date;
					$end_date=$combo_end_date;
					$nbd_array[]=formated_type_dates('dates');
					$nb_dates_array[]=formated_type_dates('n_dates');
				}else if($recurring_type=='DD'){
					 if($booking_type=='D') $no_booking_sdate_array[]=array('start_date'=>$nbk['no_booking_start_date'],'end_date'=>$nbk['no_booking_end_date']);
					 //$tep_week_start_date=$week_start_date;
					 //$tep_week_end_date=$week_end_date;
					 $tep_week_start_date=$combo_start_date;
					 $tep_week_end_date=$combo_end_date;
					 while($tep_week_start_date<=$tep_week_end_date){
					  	if($nbk['no_booking_start_date']<=$tep_week_start_date && $nbk['no_booking_end_date']>=$tep_week_start_date  && $current_date<=$tep_week_start_date){
					 		$no_b_stime=$nbk['no_booking_start_time'];
							$no_b_etime=$nbk['no_booking_end_time'];
							if($booking_type=='D'){
								$no_b_stime="00:00:00";
								$no_b_etime="24:00:00";
							}
							$no_b_sdate=$tep_week_start_date;
							$no_b_edate=$no_b_sdate;							 
					 		if($no_b_etime<=$no_b_stime && $span_days=='Y')
								$no_b_edate=date("Y-m-d",tep_dateadd(@strtotime($tep_week_start_date),"day",1));
							else if($no_b_etime<=$no_b_stime && $span_days=='N')
								$no_b_etime="24:00:00";
							if($tep_week_start_date<=$no_b_sdate && $tep_week_end_date>=$no_b_sdate){
								$nbd_array[][]=array('type'=>$recurring_type,'n_s_date'=>$no_b_sdate,'n_e_date'=>$no_b_edate,'n_s_time'=>$no_b_stime,'n_e_time'=>$no_b_etime);
								$nb_split=split(':',$no_b_stime);
								if($no_b_stime=='00:00:00' && ($no_b_etime>=(24-$time_length).':05:00' && $no_b_etime<='24:00:00') || (($booking_type=='H' || $booking_type=='M')  && $time_length!='' && date("d",mktime(($nb_split[0]+$time_length),$mt,0,1,1,1987))=="02"))
									$nb_dates_array[][format_date($no_b_sdate)]='full';								
								if($no_b_stime<=$cur_time && ($no_b_etime>=(24-$time_length).':05:00' && $no_b_etime<='24:00:00') && $no_b_sdate<=$combo_start_date && $no_b_edate>=$combo_start_date)
									$nb_dates_array[][format_date($no_b_sdate)]='full';
						    }
					 	}
					 	$rec_details=1;
					 	if($nbk['recurring_details'])  $rec_details=$nbk['recurring_details'];
					    if($tep_week_start_date) $tep_week_start_date=date('Y-m-d',tep_dateadd(@strtotime($tep_week_start_date),"day",$rec_details));
					 }
				}
			}
			$block_dates=$nb_dates_array;
			$dates_array=tep_get_service_dates_array($combo_start_date,$combo_end_date,$booking_type);
			//echo $combo_start_date.'-'.$combo_end_date;
			for ($icnt=$shour;$icnt<$ehour;$icnt++)
				for ($jcnt=0;$jcnt<7;$jcnt++) $week_array[$icnt][$jcnt]=array("class"=>"calendarBookingDefault","cnt"=>0);
			// calender style details
			// get the details from orders table with start date,end date,week time,end time
			$weeks_date=$week_start_date;
			$weeke_date=$week_end_date;
			$i=0;						
			while($weeks_date<=$weeke_date){
			 	$where="";
				if($booking_type=='H' || $booking_type=='M')
					$where="  group by os.start_date,os.end_date,os.start_time,os.end_time order by os.start_date,os.end_date";
				else if($booking_type=='D')
					$where="  group by op.products_id order by os.start_date,os.end_date";							
				if($where)											
					$booking_sql= "SELECT sum(op.products_quantity) as products_quantity ,v.include_resource,os.start_date,os.end_date,os.start_time,os.end_time,date_format(os.start_date,'%w') as sday,date_format(os.end_date,'%w') as eday,time_format(os.start_time,'%H') as stime,time_format(os.end_time,'%H') as etime from " . TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS_SERVICES . " os, " .  TABLE_SERVICES . " v " .
							" where op.orders_products_id=os.orders_products_id and op.products_id=os.products_id and op.products_id='" . tep_db_input($service_id) . "' and op.products_type='V' and op.products_id=v.service_id and o.orders_id=op.orders_id and op.products_id=os.products_id " .
							" and o.orders_id=os.orders_id and (o.orders_status>1 or date_add(o.date_purchased,interval v.pending_period day)>'" . date('Y-m-d H:i:s',getServerDate(false)) . "') AND os.start_date <= '".$weeks_date."'
							  AND os.end_date >= '".$weeks_date."' ". $where;	 
				
			if($where) $booking_query=tep_db_query($booking_sql);
			if($booking_query!='' && tep_db_num_rows($booking_query)>0 && $current_date<=$weeks_date){
			 	while($book_result=tep_db_fetch_array($booking_query)){
					$start_date=$weeks_date;
					$end_date=$weeks_date;
					$start_time=$book_result['start_time'];
					$end_time=$book_result['end_time'];
					$s_time=(int)$book_result['stime'];
					$e_time=(int)$book_result['etime'];
					$s_day=(int)$book_result['sday'];
					$e_day=(int)$book_result['eday']+1;
					if(($booking_type=='H' || $booking_type=='M') && $start_date<$end_date){ 
						$booked_service[]=array('include_resource'=>$book_result['include_resource'],'products_quantity'=>$book_result['products_quantity'],'start_date'=>$start_date,'end_date'=>$start_date,'stime'=>$s_time,'etime'=>'24','sday'=>$i,'eday'=>$i,'start_time'=>$start_time,'end_time'=>$end_time);		
						$booked_service[sizeof($booked_service)]=array('include_resource'=>$book_result['include_resource'],'products_quantity'=>$book_result['products_quantity'],'start_date'=>$end_date,'end_date'=>$end_date,'stime'=>'0','etime'=>$e_time,'sday'=>$i,'eday'=>$i,'start_time'=>$start_time,'end_time'=>$end_time);
					}else if(($booking_type=='H' || $booking_type=='M') && $start_date==$end_date){
						$booked_service[]=array('include_resource'=>$book_result['include_resource'],'products_quantity'=>$book_result['products_quantity'],'start_date'=>$start_date,'end_date'=>$end_date,'stime'=>$s_time,'etime'=>$e_time,'sday'=>$i,'eday'=>$i,'start_time'=>$start_time,'end_time'=>$end_time);		
					}else if($booking_type=='D'){
						$booked_service[]=array('include_resource'=>$book_result['include_resource'],'products_quantity'=>$book_result['products_quantity'],'start_date'=>$start_date,'end_date'=>$end_date,'stime'=>$s_time,'etime'=>$e_time,'sday'=>$i,'eday'=>$i,'start_time'=>$start_time,'end_time'=>$end_time);	
					}
				}
			 }
			 $i++;
			  if($weeks_date) $weeks_date=date('Y-m-d',tep_dateadd(@strtotime($weeks_date),"day",1));
			}
			  $booking_quantity=0;
			  for($bo=0;$bo<sizeof($booked_service);$bo++){
			  	 $booking_result=$booked_service[$bo];
				 $booking_quantity=get_orders_products_quantity($service_id,$booking_result['start_date'],$booking_result['start_date'],$booking_result['start_time'],$booking_result['end_time'],$booking_type);
				 if($booking_result["start_date"]=="" || $booking_result["end_date"]=="") continue; 
				 $sday=0;
				 $eday=6;
				 if ($booking_result["start_date"]>$week_start_date) $sday=(int)$booking_result["sday"];
				 if ($booking_result["end_date"]<$week_end_date) $eday=(int)$booking_result["eday"];
				 	
				 	$stime=(int)$booking_result["stime"];
					$etime=(int)$booking_result["etime"];
					$etime=date('H',@strtotime($booking_result["end_date"] .' '. $booking_result["end_time"])+60);
					$check_tstime=sprintf("%02d",$icnt).":00:00";
					if($etime==00) $etime=24; 
					$etime=sprintf("%2d",$etime);
					if ($stime<$shour) $stime=$shour;
					if ($etime>$ehour) $etime=$ehour;
					for ($jcnt=$sday;$jcnt<=$eday;$jcnt++){
						if ($booking_type=="D") {
							if ($jcnt==$sday){
								$tstime=$stime;
								$tetime=24;
							} else if ($jcnt==$eday){
								$tstime=0;
								$tetime=$etime;
							} else {
								$tstime=0;
								$tetime=24;
							}
						} else {
							$tstime=$stime;
							$tetime=$etime;
	 					}	
	 					if($include_resource=='N'){
							{} 
						}else if($include_resource=='Y'){   
							$service_quantity=1;
							$service_resource_query=tep_db_query("select count(*) as count from ".TABLE_SERVICES_TO_SERVICES_RESOURCES." sTr LEFT JOIN ".TABLE_SERVICE_RESOURCE." r on sTr.service_resource_id=r.resource_id, ".TABLE_SERVICE_RESOURCE_DESCRIPTIONS." red where red.resource_id=r.resource_id and red.resource_id=sTr.service_resource_id and sTr.service_id=".tep_db_input($service_id)." and red.language_id='".(int)$languages_id."' group by service_id");
							if(tep_db_num_rows($service_resource_query)>0){
								$service_resource=tep_db_fetch_array($service_resource_query);
								$service_quantity=$service_resource['count'];	
					   }
					}   
				 	for ($icnt=$tstime;$icnt<=$tetime;$icnt++){
				 	  	if($booking_type=='H' || $booking_type=='M'){
							$booking_quantity=0;
					 		$check_tstime=sprintf("%02d",$icnt).":00:00"; 
					 		$check_tetime=sprintf("%02d",$icnt).":00:00";
					 		$booking_quantity=get_times_quantity($check_tstime,$check_tetime,$booking_result["start_date"]);	
					 	}
					 	if($service_quantity>0 && $booking_quantity>0){
							if ($booking_quantity && $service_quantity>$booking_quantity){							 
								$week_array[$icnt][$jcnt]["class"]="calendarBookingPartial"; 
							}else if ($service_quantity<=$booking_quantity){	    	
						 		$week_array[$icnt][$jcnt]["class"]="calendarBookingEmpty";
							}
						} 
				  	 }
				}
				 if($booking_type=='H' || $booking_type=='M') $booking_quantity="";
			}
			// blocked time get
			$blck_datetime=array();
			for($j=0;$j<sizeof($nbd_array);$j++){	
			  for($i=0;$i<sizeof($nbd_array[$j]);$i++){
				    $no_booking_sdate=$nbd_array[$j][$i]['n_s_date'];		
				    $no_booking_edate=$nbd_array[$j][$i]['n_e_date'];	
				    $no_booking_stime=$nbd_array[$j][$i]['n_s_time'];	
				    $no_booking_etime=$nbd_array[$j][$i]['n_e_time'];
					if($booking_type=='D'){
						$no_booking_stime="00:00:00";
						$no_booking_etime="23:55:00";
					}
					if($no_booking_stime>$no_booking_etime){
						if($span_days=='Y') $blck_datetime[sizeof($blck_datetime)]=array('start_date'=>$no_booking_edate,'end_date'=>$no_booking_edate,'start_time'=>'00:00:00','end_time'=>$no_booking_etime,'type'=>$nbd_array[$j][$i]['type']);
						$blck_datetime[]=array('start_date'=>$no_booking_sdate,'end_date'=>$no_booking_sdate,'start_time'=>$no_booking_stime,'end_time'=>'24:00:00','type'=>$nbd_array[$j][$i]['type']);							
					}else $blck_datetime[]=array('start_date'=>$no_booking_sdate,'end_date'=>$no_booking_edate,'start_time'=>$no_booking_stime,'end_time'=>$no_booking_etime,'type'=>$nbd_array[$j][$i]['type']);
				}
			}
			//loop for hours
			for($i=0;$i<sizeof($blck_datetime);$i++){
				   $no_booking_sdate=$blck_datetime[$i]['start_date'];
				   $no_booking_edate=$blck_datetime[$i]['end_date'];
				   $no_booking_stime=$blck_datetime[$i]['start_time'];
				   $no_booking_etime=$blck_datetime[$i]['end_time'];
				   $no_booking_stime_splt=split(":",$no_booking_stime);
			   	   $no_booking_etime_splt=split(":",$no_booking_etime);
				   //get time
				   $ict=$no_booking_stime_splt[0];
				   if($no_booking_sdate!=$blck_datetime[$i-1]['start_date']) $time_array=array();
				   while($ict<=$no_booking_etime_splt[0]){
				       $time_array[]=array('block_time'=>$ict.':00');
					   $ict++;
				 	   $ict=(($ict<10)?'0'.$ict:$ict);
				   }
				   for ($jcnt=0;$jcnt<7;$jcnt++){
						 if($jcnt==0) 
							$date=date('Y-m-d',$week_start_time);
						 else if($week_start_time)
						 	$date=date('Y-m-d',tep_dateadd($week_start_time,"day",$jcnt));
						// check if no_booking dates is exists
						  if(($no_booking_sdate>=$date && $no_booking_sdate<=$date) || ($no_booking_edate>=$date && $no_booking_edate<=$date))
						 	 for ($icnt=$shour;$icnt<$ehour;$icnt++) $week_array[$icnt][$jcnt]["time"]=$time_array;
					    // check if no_booking dates is exists
				   }
			  }
	     }
		 $booking_total=$blck_datetime;
		 $nb_dates_string="";
		 for($ic=0;$ic<sizeof($booking_total);$ic++){
			 $nbstime=$booking_total[$ic]['start_time'];
			 $nbetime=$booking_total[$ic]['end_time'];
			 $nb_sdate=$booking_total[$ic]['start_date'];
			 $nb_edate=$booking_total[$ic]['end_date'];
		 	 if($nbstime=="12:00:00") $nbstime="00:00:00";
			 if($nb_sdate!=$nb_edate){
			  $nb_dates_string.=$nb_sdate.'#'.$nb_sdate.'#'.$nbstime.'#'.'24:00:00{{}}';
			  $nb_dates_string.=$nb_sdate.'#'.$nb_sdate.'#'.'00:00:00'.'#'.$nbetime.'{{}}';
	  		 }else $nb_dates_string.=$nb_sdate.'#'.$nb_sdate.'#'.$nbstime.'#'.$nbetime.'{{}}';	  			
		  }
		  $date_begin=$week_start_date;
		  $date_end=$week_end_date;
		  if($view=='month'){
			$date_begin=$month_start_date;
			$date_end=$month_end_date;
		   }
		   $bk_query=tep_db_query("select no_booking_start_date,no_booking_end_date,no_booking_start_time,no_booking_end_time,recurring_type,recurring_details from ".TABLE_SERVICES_NO_BOOKING_DAYS ." snd where snd.service_id='".tep_db_input($service_id)."' and (('$date_begin'>=no_booking_start_date and '$date_begin'<=no_booking_start_date) OR  ('$date_end'>=no_booking_start_date and '$date_end'<=no_booking_end_date) OR ('$date_begin'<=no_booking_start_date and '$date_end'>=no_booking_end_date)) order by no_booking_start_date,no_booking_end_date,no_booking_start_time,no_booking_end_time");
		   if(tep_db_num_rows($bk_query)>0){
			  while($nbk=tep_db_fetch_array($bk_query)){
			 	if($nbk['recurring_type']=='DD'){
				 	$nbstime=$nbk['no_booking_start_time'];
				 	$nbetime=$nbk['no_booking_end_time'];
				 	if($span_days=='Y' && $nbetime<$nbstime){
				 	  	$nb_dates_string.=$nbk['no_booking_start_date'].'#'.$nbk['no_booking_end_date'].'#00:00:00'.'#'.$nbetime.'{{}}';
				 	  	$nb_dates_string.=$nbk['no_booking_start_date'].'#'.$nbk['no_booking_end_date'].'#'.$nbstime.'#24:00:00'.'{{}}';
					}else {
						if($nbetime<$nbstime)$nbetime='24:00:00';
						$nb_dates_string.=$nbk['no_booking_start_date'].'#'.$nbk['no_booking_end_date'].'#'.$nbstime.'#'.$nbetime.'{{}}';			    
			 		} 
			    }			   
			 }
		   }
 	      //
		  // calender style details
	     // assign no booking array for combo box dates and time
	    // create calendar initial details
	   //details
	  //ajax start
  	 $command=tep_db_prepare_input($HTTP_GET_VARS['command']);
	 if($command=='service_detail_view'){
		get_service_details_view();
		exit;
	}
  //ajax end 
  if ($service_info['service_id']<=0){
		tep_redirect(tep_href_link(FILENAME_DEFAULT));
  }	
  $content = CONTENT_SERVICES_INFO;
  
  $javascript = 'popup_window.js';
  $booking_type=$service_info['booking_type'];
  require('includes/javascript/services_info.js.php');
  require(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/' . TEMPLATENAME_MAIN_PAGE);
  require(DIR_WS_INCLUDES . 'application_bottom.php');
 function get_times_quantity($tstime,$tetime,$start_date){
	   	global $service_id,$cart,$customer_id; 	 
	   		$reserve_quantity=0;
	   	    $where="and (o.orders_status>1 or date_add(o.date_purchased,interval v.pending_period day)>'" . date('Y-m-d H:i:s',getServerDate(false)) . "') " .
							" and '$start_date $tstime' BETWEEN concat(os.start_date,' ',os.start_time) AND date_add(concat(os.end_date,' ',os.end_time),interval 1 minute) ";
							
			$booking_sql=tep_db_query("SELECT sum(op.products_quantity) as quantity ,v.include_resource,os.start_date,os.end_date,os.start_time,os.end_time,date_format(os.start_date,'%w') as sday,date_format(os.end_date,'%w') as eday,time_format(os.start_time,'%H') as stime,time_format(os.end_time,'%H') as etime from " . TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS_SERVICES . " os, " .  TABLE_SERVICES . " v " .
							" where op.orders_products_id=os.orders_products_id and op.products_id=os.products_id and op.products_id='" . tep_db_input($service_id) . "' and op.products_type='V' and op.products_id=v.service_id and o.orders_id=op.orders_id and op.products_id=os.products_id " .
							" and o.orders_id=os.orders_id ".$where." group by os.start_date order by os.start_date,os.end_date "); 
			$booking_customers_basket_edit=tep_db_query("select op.products_id,os.resource_id,op.customers_basket_quantity as quantity from ".TABLE_CUSTOMERS_BASKET." op,".TABLE_CUSTOMERS_BASKET_SERVICES ." os  where  op.products_id=os.products_id  and '$start_date $tstime' BETWEEN concat(os.start_date,' ',os.start_time) AND date_add(concat(os.end_date,' ',os.end_time),interval 1 minute) ");
			if(tep_db_num_rows($booking_customers_basket_edit)>0){ 
			 	 while($booking_customers_basket_result=tep_db_fetch_array($booking_customers_basket_edit)){
			 	 	  $pro_id=substr($booking_customers_basket_result['products_id'],1,strpos($booking_customers_basket_result['products_id'],"{")-1);
			 	 	  if($pro_id==$service_id){
			 	 			$reserve_quantity+=$booking_customers_basket_result['quantity'];
			 	 	  }
			 	 }
			} 
			 
			$get_edit_order_product=tep_db_query("select os.resource_id,op.products_quantity as quantity from ".TABLE_ORDERS_EDIT_PRODUCTS." op,".TABLE_ORDERS_EDIT_SERVICES ." os  where op.orders_products_id=os.orders_products_id and  op.products_id=os.products_id and  op.products_id='".(int)$service_id."' and '$start_date $tstime' BETWEEN concat(os.start_date,' ',os.start_time) AND date_add(concat(os.end_date,' ',os.end_time),interval 1 minute) ");
			if(tep_db_num_rows($get_edit_order_product)>0){
	  			while($order_edit=tep_db_fetch_array($get_edit_order_product)){
					$reserve_quantity+=$order_edit['quantity'];
	  			}
			}		 
			$check_products=$cart->get_products();
			if($check_products && !$customer_id){
			 	 for($i=0;$i<=sizeof($check_products);$i++){
					if($check_products[$i]['element_type']=='V'){
						if((($start_date.' '.$tstime>=$check_products[$i]['others']['start_date'].' '.$check_products[$i]['others']['start_time'] and  $start_date.' '.$tstime <=$check_products[$i]['others']['end_date'].' '.$check_products[$i]['others']['end_time']) OR ($start_date.' '.$tetime>=$check_products[$i]['others']['start_date'].' '.$check_products[$i]['others']['start_time'] and $start_date . ' '.$tetime<=$check_products[$i]['others']['end_date'].' '.$check_products[$i]['others']['end_time']) OR ($start_date.' '.$start_time<=$check_products[$i]['others']['start_date'].' '.$check_products[$i]['others']['start_time'] and $start_date.' '.$tetime>=$check_products[$i]['others']['end_date'].' '.$check_products[$i]['others']['end_time']))) {
						 	$reserve_quantity+=$check_products[$i]['others']['quantity'];
						}
					 }
				  } 
			  }
			 
			 if(tep_db_num_rows($booking_sql)>0){
			 	$quan=tep_db_fetch_array($booking_sql);
			 	$reserve_quantity+=$quan['quantity'];
			 }
			 return $reserve_quantity;
}     
function get_service_details_view(){ 
 global $service_id,$view,$HTTP_GET_VARS,$HTTP_SESSION_VARS,$service_info,$week_start_date,$week_end_date,$week_start_time,$week_end_time,$month_start_date,$month_end_date,$month_start_time,$month_end_time,$dates_array,$current_month,$current_year,$year,$month,$day,$stime,$week_array,$shour,$ehour,$week_day,$total_days,$calendar,$time_length,$start_time,$booking_type,$current_date,$nb_dates_string;    
  if($current_date) $current_date_string=@getServerDate(false); 
  if(count($service_info)>0 && $service_info['service_id']){
 ?>
 <table border="0" cellpadding="0" cellspacing="0" width="100%">
 		<tr>
 			<td width="50%" valign="top">
  	  			<table border="0"  cellpadding="0" cellspacing="0" width="100%">						
						<tr>
							<td><?php echo tep_draw_separator('pixel_trans.gif', '50%', '1'); ?></td>
						</tr>
						<tr  class='eventListing-data' height="30">	
							<td><?php
										$default_service_start_date="";
										$default_service_end_date="";	
										$default_service_start_time=$start_time;
										$default_service_end_time="";
										$action_edit=(isset($HTTP_GET_VARS['action'])?tep_db_prepare_input($HTTP_GET_VARS['action']):'');  
										if($action_edit=='edit'){
											$default_service_start_date=$services->start_date;
											$default_service_end_date_time=$services->end_date . '   '  . $services->end_time;
											$default_service_start_time=(($services->start_time<10)?'0'.$services->start_time:$services->start_time);
									   }
										if (($booking_type=="H" || $booking_type=='M') && sizeof($dates_array)>0){
											echo '<b>' . TEXT_START_TIME . '</b><br>' . tep_draw_pull_down_menu('start_date',$dates_array,$default_service_start_date,' id="start_date" onChange="javascript:add_start_times();"'); 
											echo tep_draw_pull_down_menu('start_time',array(),'',' id="start_time" onChange="javascript:add_end_times();" ');
										} else if($booking_type=='D' && sizeof($dates_array)>0){
											echo '<b>' . TEXT_START_TIME . '</b><br>' . tep_draw_pull_down_menu('start_times',$dates_array,$default_service_start_date,' id="start_times" onChange="javascript:add_end_times();"'); 
											echo tep_draw_hidden_field('start_date','');
											echo tep_draw_hidden_field('start_time',''); 
										}
										?>
									</td>
								</tr>
								<tr>
									<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
								</tr>
								<tr class='eventListing-data' height="30">
									<td>
										<?php 
											echo '<b>' . TEXT_END_TIME . '</b><br>' . tep_draw_pull_down_menu('end_times',array(),$default_service_end_date_time,' id="end_times" class="service_combo"'); 
											echo tep_draw_hidden_field('end_date','','');
											echo tep_draw_hidden_field('end_time','','');
										?>
									</td>
								</tr>
								<tr>
									<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
								</tr>
								<?php 								
								if ($service_info["multiple_purchase"]=="Y" && $service_info['include_resource']=='N') { ?>
									<tr class='eventListing-data'>
										<td><?php echo '<b>' . TEXT_HOW_MANY . '</b><br>' . tep_draw_input_field('products_qty',1,'size=6 class="service_combo" '); ?></td>
									</tr>
								<?php } else {
										echo tep_draw_hidden_field('products_qty',1);
									}
								?>
								<tr style="height:70">
									<td colspan="2" align="right" style="padding-right:15px" valign="bottom"><?php echo tep_template_image_submit('button_reserve.gif', IMAGE_BUTTON_RESERVE);?></td>
								</tr>
							</table>
							</td>
					<?php if ($view=="month" && defined("SERVICES_SHOW_CALENDER") && (SERVICES_SHOW_CALENDER=='true' || SERVICES_SHOW_CALENDER=='True')) { 
						?>
						<td width="50%" valign="top">
						<table border="0" cellpadding="0" cellspacing="0" width="100%" class="calendarBack">
							<TR><TD class="service_smalltext" align="center"><?php echo TEXT_HEADING_CURRENT_BOOKINGS;?></td></tr>
							<tr style="height:10"><td></td></tr>
							<tr>
								<td valign="top">
								<table border="0" cellpadding="0" cellspacing="0" width="100%">
									<tr>
										<td class="calendarBookingHeader" width="40" align="center"><?php echo tep_output_string($year);?></td>
										<td class="calenTextB" align="right" valign="middle">
										<?php 
											$previous_time_stamp=tep_datesub($month_start_time,"day",1);
											$next_time_stamp=tep_dateadd($month_end_time,"day",1);
											if ($previous_time_stamp>=$current_date_string){
												$params=$old_params ."year=" .date("Y",$previous_time_stamp) . "&month=" . date("m",$previous_time_stamp) . "&view=month";
												echo '<a href="javascript:get_service_detail_view('.$service_id.','.'\'month\','.date("m",$previous_time_stamp).','.date("Y",$previous_time_stamp).');">' . tep_template_image_button('calenLeft.gif',IMAGE_PREV_MONTH,'align=absmiddle') . '</a>&nbsp;';
											} else {
												echo tep_template_image_button('calenLeft.gif','','align=absmiddle');
											}
											echo '<b>' .date("M",$month_start_time) . '</b>&nbsp;';

											$params=$old_params . "year=" .date("Y",$next_time_stamp) . "&month=" . date("m",$next_time_stamp) . "&view=month";
											echo '<a href="javascript:get_service_detail_view('.$service_id.',\'month\','.date("m",$next_time_stamp).','.date("Y",$next_time_stamp).');">'  . tep_template_image_button('calenRight.gif',IMAGE_NEXT_MONTH,'align=absmiddle') . '</a>&nbsp;';
										?>
									</tr> 
								</table>
								</td>
							</tr> 
							<tr height="2">
								<td class="calendarBookingHeader"></td>
							</tr>
							<tr>
								<td class="calenText" align="left"><?php echo date("h:i a",$current_date_string);?></td>
							</tr>
							<tr>
								<td valign="top" class="calendarBack">
								<table cellpadding="0" cellspacing="0" width="100%">
									<tr>
									<?php 
										$week_days=array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
										for ($icnt=0;$icnt<7;$icnt++){
											if(date('Y',$month_start_time)==date('Y',$current_date_string) && date('m',$month_start_time)==date('m',$current_date_string) && date('D',$current_date_string)==$week_days[$icnt]) $week_days[$icnt]='<font color="red"><b>'.$week_days[$icnt].'</b></font>';
											echo '<td class="calenText" width=25 align="center"><b>' . $week_days[$icnt] . '</b></td>';
										} //icnt
									?>
									</tr>
								</table>
								</td>
							</tr>
							<tr>
								<td>
								<table class="calendarData"  cellpadding="1" cellspacing="1" width="100%" border="0">
								<?php 
									 $temp_month=date('Y-m-d',$month_start_time);
									 for ($icnt=0;$icnt<count($calendar);$icnt++){
										if (($icnt+1)%7==1 && $icnt>1) echo '</tr>';
										if (($icnt+1)%7==1) echo '<tr>';
										$calen_data=$calendar[$icnt];
										$day="";
										$border="";
										$day_view="";
										if ($icnt>=$week_day && $icnt<$week_day+$total_days) $day=($icnt+1)-$week_day;
										$sub_day=($week_day-1);
										if(tep_datesub(@strtotime($temp_month),"day",(($sub_day>0)?$sub_day:1))==$current_date_string) $day_view='red';
										if($calen_data["class"]=="calendarNoBooking" || $calen_data["class"]=="calendarBookingEmpty" || $calen_data["class"]=='calendarBookingPartial') $day_view='white';
										echo '<td class="' . $calen_data["class"] . '" width=25 height=25 valign=top><b><font color="'.$day_view.'"><b>'.$day.'</font></b></td>';
										if($icnt)$temp_month=date('Y-m-d',tep_dateadd($month_start_time,"day",$icnt));
									} 
								?>
								</table>
								</td>
							</tr>
						<tr>
							<td>
							<?php								
								$temptime=mktime(0,0,0,$month,1,$year);
								$date_to_string=$temptime;
								if(date('w',$temptime))	$date_to_string=tep_datesub($temptime,"day",date('w',$temptime));
								if(date('m',$temptime)==date('m',$current_date_string)) $date_to_string=tep_datesub($current_date_string,"day",date('w',$current_date_string));
								$params=$old_params. "&year=" . date('Y',$date_to_string) . "&month=" . date('m',$date_to_string) . "&day=" . date('d',$date_to_string) . "&view=week";
								echo '<a href="javascript:get_service_detail_view('.$service_id.',\'week\','.date('m',$date_to_string).','.date('Y',$date_to_string).','.date('d',$date_to_string).');">' . tep_image(DIR_WS_HTTP_CATALOG.DIR_WS_IMAGES ."weekview.gif",IMAGE_WEEK_VIEW) . '</a>';
								$params=$old_params . "&year=" . tep_output_string($year) . "&month=" . tep_output_string($month) . "&view=month";
								echo '<a href="javascript:get_service_detail_view('.$service_id.',\'month\','.tep_output_string($month).','.tep_output_string($year).');">' . tep_image(DIR_WS_HTTP_CATALOG.DIR_WS_IMAGES ."monthview.gif",IMAGE_MONTH_VIEW) . '</a>';
							?>
							</td>
						</tr>
						</table>
						</td>
					<?php } else if ($view=="week" && defined("SERVICES_SHOW_CALENDER") && (SERVICES_SHOW_CALENDER=='true' || SERVICES_SHOW_CALENDER=='True')) {   
					$mk_time=$week_start_time;
					if($mk_time<$current_date_string) $mk_time=$current_date_string;
					$month_y=date("M Y",$mk_time);
					if(date('Y',$week_start_time)<date('Y',$week_end_time) && $week_start_time!=$current_date_string){
						$month_y=date("M Y",$week_end_time); 
						$mk_time=$week_end_time;
					}else if($week_start_time==$current_date_string){
						$month_y=date("M Y",$week_start_time);
						$mk_time=$week_start_time;
					}
					?>
					<td width="210" valign="top">
					<table border="0" cellpadding="0" cellspacing="0" width="100%" class="calendarBack">
						<TR><TD class="service_smalltext" align="center"><?php echo TEXT_HEADING_CURRENT_BOOKINGS;?></td></tr>
						<tr style="height:10"><td></td></tr>
						<tr>
							<td valign="top">
							<table border="0"  cellpadding="0" cellspacing="0" width="100%">
								<tr>
									<td class="calendarBookingHeader" width="70" align="center"><?php echo $month_y;?></td>
									<td class="calenTextB" align="right" valign="middle">
									<?php
										$previous_time_stamp=tep_datesub($week_start_time,"day",7);
										$next_time_stamp=tep_dateadd($week_end_time,"day",1);
										if ($week_start_time>=$current_date_string){
											$params=$old_params ."year=" .date("Y",$previous_time_stamp) . "&month=" . date("m",$previous_time_stamp) . "&day=" . date("d",$previous_time_stamp) . "&view=week";
										   echo '<a href="javascript:get_service_detail_view('.$service_id.',\'week\','.date("m",$previous_time_stamp).','.date("Y",$previous_time_stamp).','.date("d",$previous_time_stamp).');">' . tep_template_image_button('calenLeft.gif',IMAGE_PREV_WEEK,'align=absmiddle') . '</a>&nbsp;';
										} else {
											echo tep_template_image_button('calenLeft.gif','','align=absmiddle');
										}
										if($current_date_string>=$mk_time && $current_date_string<=tep_dateadd($mk_time,"day",7) && (substr(date('D',$current_date_string),0,1)!='S' || (date('m',$mk_time)==12 && date('d',$mk_time)==31))) 
											{}		
										else
											$mk_time=tep_dateadd($mk_time,"day",1);
										echo '<b>' . TEXT_WEEK . date('W',$mk_time) . '</b>&nbsp;';
										$params=$old_params . "year=" .date("Y",$next_time_stamp) . "&month=" . date("m",$next_time_stamp) . "&day=" . date("d",$next_time_stamp) . "&view=week";
										echo '<a href="javascript:get_service_detail_view('.$service_id.',\'week\','.date("m",$next_time_stamp).','.date("Y",$next_time_stamp).','.date("d",$next_time_stamp).');">' . tep_template_image_button('calenRight.gif',IMAGE_NEXT_WEEK,'align=absmiddle') . '</a>&nbsp;';
									?> 
									</td>
								</tr>
							</table>
							</td>
						</tr>
						<tr height="2">
							<td class="calendarBookingHeader"></td>
						</tr>
						<tr>
							<td align="left" class="calenText"><?php echo date("h:i a",$current_date_string); ?></td>
						</tr>
						<tr>
							<td valign="top">
							<table cellpadding="1" cellspacing="0" width="100%" border="0"> 
								<tr>
								<td class="Cal_Left_top" width="10" height="20" align="right"><?php echo tep_draw_separator('pixel_trans.gif',30,1);?></td>
									<?php 
										$week_days=array("S","M","T","W","T","F","S");
										$stime=$week_start_time;
										
										for ($icnt=0;$icnt<7;$icnt++){
											$temp=(int)date("d",$stime);
											if(date('Y-m-d',$stime)==$current_date) {
												$temp='<font color="red"><b>'.$temp.'</b></font>';
												$week_days[$icnt]='<font color="red"><b>'.$week_days[$icnt].'</b></font>';
											}	
											echo '<td class="calenText" width="10%" align="center"><b>' . $week_days[$icnt] . '<br>' . $temp .'</b></td><td width="1%"></td>';
											$stime=tep_dateadd($stime,"day",1);
										} //icnt
									?>
									<td class="calenText" width="2%" align="left"></td><td width="2%"></td>
								</tr>
								<tr>
									<td width="30"><?php echo tep_draw_separator('pixel_trans.gif',30,1);?></td>
									<td colspan="21" style="border-bottom:1px solid #BFB8BF;font-size:1px" height="1">&nbsp;</td>
								</tr>
							</table>
							</td>
						</tr>
						<tr>
							<td>
								<div style="height:120px;overflow:auto">
								<table cellpadding="0" cellspacing="0" width="100%" border="0"> 
									<?php 
										for ($icnt=$shour;$icnt<$ehour;$icnt++){
											echo '<tr><td class="service_mains" width="35" height="20" valign="bottom">' . sprintf("%02d",($icnt<=12?(($icnt==00)?12:$icnt):$icnt-12)) . ":00" .'</td>';
											reset($week_array[$icnt]);
											while(list($key,$value)=each($week_array[$icnt])){
												$icn_splt=split(":",$icnt);
												$hour=sprintf("%02d:%02d",$icn_splt[0],$icn_splt[1]);
												$class=$value["class"];
												if(sizeof($value["time"])>0){
													for($i=0;$i<sizeof($value["time"]);$i++)
													{
													 if($hour=="23:59" && ($booking_type=='H' || $booking_type=='M'))$hour="00:00";
													 if($value["time"][$i]["block_time"]==$hour){
													 	   $class="calendarNoBooking";		
													  }
													}
												}
												 	echo '<td width="2%">' . tep_draw_separator('grayline.gif',1,20) . '</td>';
											      echo '<td width=16 height=20 valign=top class="' . $class . '">&nbsp;</td>';
											      echo '<td width="3"></td>';	
											}
											echo '</tr>';
										}
									?>
								</table>
								</div>
							</td>
						</tr>
						<tr>
							<td class="calendarBack" align="left">
							<?php								
								if($week_start_date<$current_date) $month=date('m',$current_date_string);
								$temptime=mktime(0,0,0,$month,1,$year);
								if ($temptime<$current_date_string) $temptime=$current_date_string;
								$day_cnt=date('w',$temptime);
								if($day_cnt<=0) $day_cnt=1;
								$tempday=date('d',tep_datesub($temptime,"day",$day_cnt));	
								$params=$old_params. "&year=" . tep_output_string($year) . "&month=" . tep_output_string($month) . "&day=" . $tempday . "&view=week";
								echo '<a href="javascript:get_service_detail_view('.$service_id.',\'week\','.tep_output_string($month).','.tep_output_string($year).','.$tempday.');">' . tep_image(DIR_WS_HTTP_CATALOG.DIR_WS_IMAGES ."weekview.gif",IMAGE_WEEK_VIEW) . '</a>';
								$tempmonth=$month;
								if ($month<$current_month && $year<$current_year) $tempmonth=$current_month;
								$params=$old_params . "&year=" . tep_output_string($year) . "&month=" . $tempmonth . "&view=month";
								echo '<a href="javascript:get_service_detail_view('.$service_id.',\'month\','.$tempmonth.','.tep_output_string($year).');">' . tep_image(DIR_WS_HTTP_CATALOG.DIR_WS_IMAGES ."monthview.gif",IMAGE_MONTH_VIEW) . '</a>';
							?>
							</td>
						</tr>
					</table>
					</td>
					<?php  }  // view 
					?> 
				</tr> 
		  </table>
<?php  echo '{{{}}}'.$nb_dates_string;
	 }
	} ?>
