<div id="show_popup_display"></div>
<?php $gparam="";
      if($HTTP_GET_VARS['view']=='month')
	   $gparam='&view=month';
   	echo tep_draw_form('select_service', tep_href_link(FILENAME_SERVICE_RESOURCES,'service_id='.$service_id.$gparam),'post','onsubmit="javascript:return validateForm();"'); 
	   echo tep_draw_hidden_field('service_id',(int)$HTTP_GET_VARS["service_id"]);
	   $old_params=tep_get_page_params(array('service_id','secPath'));
	   $option_id=isset($HTTP_GET_VARS['option_id'])?$HTTP_GET_VARS['option_id']:'0';
	   $category_id=isset($HTTP_GET_VARS['category_id'])?$HTTP_GET_VARS['category_id']:'0';
?>
<table border="0" width="100%" cellspacing="0" cellpadding="<?php echo CELLPADDING_SUB;?>">
<?php if ($service_check['total'] < 1) {?>
		<tr>
			<td><?php new infoBox(array(array('text' => TEXT_SERVICE_NOT_FOUND))); ?></td>
		</tr>
		<tr> 
			<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
		</tr>
		<tr>
			<td>
			 <table border="0" width="100%" cellspacing="1" cellpadding="2" class="infoBox">
				<tr class="infoBoxContents">
					<td>
					<table border="0" width="100%" cellspacing="0" cellpadding="2">
						<tr>
							<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
							<td align="right"><?php echo '<a href="' . tep_href_link(FILENAME_DEFAULT) . '">' . tep_template_image_button('button_continue.gif', IMAGE_BUTTON_CONTINUE) . '</a>'; ?></td>
							<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
<?php	} else { 
		// create calendar initial details
		$month=(isset($HTTP_GET_VARS["month"])?(int)$HTTP_GET_VARS["month"]:0);
		$year=(isset($HTTP_GET_VARS["year"])?(int)$HTTP_GET_VARS["year"]:0);
		$day=(isset($HTTP_GET_VARS["day"])?$HTTP_GET_VARS["day"]:'');
		$view=(isset($HTTP_GET_VARS["view"])?$HTTP_GET_VARS["view"]:'week');
		$current_month=date("m");
		$current_year=date("Y");
		if ($month<=0 || $month>12) $month=$current_month; 
		if ($current_year<=0) $year=$current_year;
		$current_time_stamp=mktime();
		$current_date=date('Y-m-d');	
		if ($view=="month") $day=1;
		if (!checkdate($month, (int)$day, $year)){
			$year=date("Y");
			$month=date("m");
		}
		$curDate=getServerDate();
		// fetch service details
			tep_db_query("update " . TABLE_SERVICES_DESCRIPTION . " set service_viewed = service_viewed+1 where service_id = '" . (int)$HTTP_GET_VARS['service_id'] . "' and language_id = '" . (int)$languages_id . "'");
		// get the no booking dates
		$round_amount=tep_add_tax($service_info["service_costs"],tep_get_tax_rate($service_info["service_tax_class_id"]));
		$service_costs = $currencies->display_price($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_tax_class_id=$service_info["service_tax_class_id"];
		// prepare granularity
		$granular=(int)$service_info["booking_granular"];
		if ($granular<=0) $granular=1;
		$slot=60/$granular;
		$max_bookings=(float)$service_info["max_bookings"];
		$time_length=(float)$service_info["service_total_time"];
		$booking_type=$service_info["booking_type"];
		$service_week=$service_info["service_week"];	
		$span_days=$service_info["span_days"];
		if($service_info['include_resource']=='N')
			$quantity=$service_info["quantity"];
		else{
			$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 and sTr.service_id=".(int)$service_info['service_id']." group by service_id");
			if(tep_db_num_rows($service_resource_query)>0){
				$service_resource=tep_db_fetch_array($service_resource_query);
				$quantity=$service_resource['count'];	
			}	
		}
		if ($time_length==0) $time_length=1;
		if ($max_bookings==0) $max_bookings=1;
		$no_booking_sdate=$no_booking_edate=$no_booking_stime=$no_booking_etime="";
			
		// 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=gmmktime(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',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)$HTTP_GET_VARS['service_id']."'");
			$shour=0;
			$ehour=23;
			$nbd_array=array();
			$no_booking_sdate='';
			$no_booking_edate='';
			$cur_time=date("H") .':00:00';
			while($nbk=tep_db_fetch_array($bk_query)){
					$recurring_type=$nbk['recurring_type'];
					$sInfo=$nbk;
					$nb_dates=get_dates_array($sInfo,$recurring_type);
					$nbd_array[]=formated_type_dates('dates');
					$nb_dates_array[]=formated_type_dates('n_dates');
			}
			$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,$service_info["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=$month_start_date;
			$month_edate=$month_end_date;
			$mi=1;
			$booking_type=$service_info['booking_type'];
			while($month_sdate<=$month_edate){
				/*for($nb=0;$nb<sizeof($nb_dates_array);$nb++){
					if($nb_dates_array[$nb][format_date($month_sdate)]=='full')
						$month_sdate=date('Y-m-d',dateadd(strtotime($month_sdate),"day",1));
				}*/
				$where="";
				if($booking_type=='H')
					$where=" and '" . $month_sdate . "' BETWEEN os.start_date AND os.end_date 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=" and '" . $month_sdate . "' BETWEEN os.start_date AND os.end_date  group by op.products_id order by os.start_date,os.end_date  ";
		
			// set calender view styles
			$booking_sql= "SELECT sum(op.products_quantity) as products_quantity,os.start_date,os.end_date,os.start_time,os.end_time,date_format(os.start_date,'%d') as sday,date_format(os.end_date,'%d') as eday,v.include_resource 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='" . $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)>'" . $curDate . "') " .
							$where;				
			//if($month_sdate=='2006-12-22') echo $booking_sql;
			$booking_query=tep_db_query($booking_sql);
			if(tep_db_num_rows($booking_query)>0){ 
			 while($book_result=tep_db_fetch_array($booking_query)){
				$start_date=$month_sdate;
				$end_date=$month_sdate; 
				$s_time=$book_result['start_time'];
				$e_time=$book_result['end_time'];
				$s_day=(int)$book_result['sday'];
				$e_day=(int)$book_result['eday'];
				$ret=tep_time_diff($start_date.' '.$s_time,$end_date.' '.$e_time);
				$time_diff=$ret["hours"];
				 if($booking_type=='D'){
				  	$time_diff=$ret["days"];
				 }
				if($service_info['booking_type']=='H') $book_result["products_quantity"]=$book_result["products_quantity"] * round($time_diff/$service_info["service_total_time"]);
				if($booking_type=='H' && $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'=>$mi,'eday'=>$mi);	 	
					$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'=>$mi,'eday'=>$mi);
				}else if($booking_type=='H' && $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'=>$mi,'eday'=>$mi);		
				}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'=>$mi,'eday'=>$mi);	
				}
			  }
			 }
			 $mi++;
			 $month_sdate=date('Y-m-d',dateadd(strtotime($month_sdate),"day",1));
			 }		

			$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);
			   $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);
				    $hour=round($ret1["hours"]);
				} 
				$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',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="";
		  for($bo=0;$bo<sizeof($booked_service);$bo++){
		  	$booking_result=$booked_service[$bo];
		  	if($pre_date_booking==$booking_result['start_date']){
		  		$total_quantity+=(int)$booking_result['products_quantity'];
			 	$pre_date_booking=$booking_result['start_date'];
			}else {
			 	$total_quantity=(int)$booking_result['products_quantity'];
			 	$pre_date_booking=$booking_result['start_date'];
			}	
			 $no_time="";
			 for($to=0;$to<sizeof($no_booking_datetime);$to++){
			 	if($booking_type=='H' && $booking_result["start_date"]==$no_booking_datetime[$to]['start_date']){
			 		$no_time+=$no_booking_datetime[$to]["time_diff"];	
			 	}
			 }
			 
			 $quantity=$service_info["quantity"];
			 if($booking_type=='H' && 24>$no_time) $quantity=round(24-$no_time); 
			 if($quantity && $booking_type=='H') 	$quantity=$service_info["quantity"]*round($quantity/$service_info["service_total_time"]);
			  //$total_quantity=$booked_service[$bo]['qty'];	
			  if($service_info["include_resource"]=='Y'){
			  	$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 and sTr.service_id=".(int)$service_info['service_id']." group by service_id");
				if(tep_db_num_rows($service_resource_query)>0){
					$service_resource=tep_db_fetch_array($service_resource_query);
					$quantity=$service_resource['count'];	
				}
			  }	
			 if ($booking_type=="D"){ // booking type is day
					$sday=1;
					$eday=$total_days;

					if ($booking_result["start_date"]>$month_start_date) $sday=$booking_result["sday"];
					if ($booking_result["end_date"]<$month_end_date) $eday=$booking_result["eday"];
					for ($icnt=$sday;$icnt<=$eday;$icnt++){
						$sday=($icnt-1)+$week_day;
						//echo  $total_quantity.'-'.$quantity.'<br>';
						if ($total_quantity>=$quantity){
							$calendar[$sday]["class"]="calendarBookingEmpty";
						} else if ($total_quantity<$quantity){
							$calendar[$sday]["class"]="calendarBookingPartial";
						}
					}
				} else {
					   if ($booking_result["start_date"]>=$month_start_date && $booking_result["end_date"]<=$month_end_date){
						$sday=($booking_result["sday"]-1)+$week_day;
						$ret=tep_time_diff($booking_result["start_date"],$booking_result["end_date"]);
						if ($total_quantity<$quantity){
							$calendar[$sday]["class"]="calendarBookingPartial";
						}else if ($total_quantity>=$quantity){
							$calendar[$sday]["class"]="calendarBookingEmpty";
						}
					}						
				}
			 } 
	  } 
		// get no booking dates for  start
		if ($view=="week"){
		   $week_day=date("w");
			$combo_start_date=date('Y-m-d',dateadd($current_time_stamp,"day",1));
			if ($month+1<date("m") && $year==date('Y')) $month=date("m");
			if ($year<date("Y")) {
				$year=date("Y");
				$month=1;
			}
			if (!@checkdate($month, (int)$day, $year)){	
				$week_start_time=datesub(time(),"day",$week_day);
				$week_end_time=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") $week_start_time=datesub(time(),"day",$week_day);
				if (date("w",$week_start_time)!="0" && date("m",$week_start_time)==date('m') && date("Y",$week_start_time)==date('Y') ) $week_start_time=datesub(mktime(),"day",$week_day);
				$week_start_date=date('Y-m-d',$week_start_time);
				$week_end_time=dateadd($week_start_time,"day",6);
				$week_end_date=date("Y-m-d",$week_end_time);
			}
			
			$combo_start_date=$current_date;
			$combo_end_date=date('Y-m-d',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='".(int)$HTTP_GET_VARS['service_id']."'");
			$shour=0;
			$ehour=24;
			$nbd_array=array();
			$no_booking_sdate='';
			$no_booking_edate='';
			$cur_time=date("H").':00:00';
			while($nbk=tep_db_fetch_array($bk_query)){
				$recurring_type=$nbk['recurring_type'];
				$sInfo=$nbk;
				$nb_dates=get_dates_array($sInfo,$recurring_type);
				$nbd_array[]=formated_type_dates('dates');
				$nb_dates_array[]=formated_type_dates('n_dates');
			}
			$block_dates=$nb_dates_array;
			$service_start_time="00:00:00";
			$dates_array=tep_get_service_dates_array($combo_start_date,$combo_end_date,$booking_type);
			$times_array=tep_get_service_time_array(00,24,$slot);
			for ($icnt=$shour;$icnt<$ehour;$icnt++){
				for ($jcnt=0;$jcnt<7;$jcnt++){
					$week_array[$icnt][$jcnt]=array("class"=>"calendarBookingDefault","cnt"=>0);
				}
			}
			// calender style details
			$booking_array=array();
			// 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;
			//$booked_service=array();
			$i=0;
			while($weeks_date<=$weeke_date){
				$where="";
				if($service_info['booking_type']=='H')
					$where=" and '" . $weeks_date . "' BETWEEN os.start_date AND os.end_date group by os.start_date,os.end_date,os.start_time,os.end_time order by os.start_date,os.end_date ";
				else if($service_info['booking_type']=='D')
					$where=" and '" . $weeks_date . "' BETWEEN os.start_date AND os.end_date  group by op.products_id order by os.start_date,os.end_date  ";							
			$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='" . $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)>'" . $curDate . "') " .
							$where;
			//if($weeks_date=='2006-12-15')echo $booking_sql;
			$booking_query=tep_db_query($booking_sql);
			if(tep_db_num_rows($booking_query)>0){ 
			 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'];
					if($booking_type=='H' && $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' && $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++;
				  $weeks_date=date('Y-m-d',dateadd(strtotime($weeks_date),"day",1));
			  } 
			  //echos($booked_service);
			  for($bo=0;$bo<sizeof($booked_service);$bo++){
			  	 $booking_result=$booked_service[$bo];
				 if($pre_date_booking==$booking_result['start_date']){
				 	$booking_quantity+=(int)$booking_result['products_quantity'];
				 	$pre_date_booking=$booking_result['start_date'];
				 }else {
				 	$booking_quantity=(int)$booking_result['products_quantity'];
				 	$pre_date_booking=$booking_result['start_date'];
				 }
				 if($booking_result['include_resource']=='Y')$booking_quantity=$booking_result['products_quantity'];
				 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"];
					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($service_info['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 and sTr.service_id=".(int)$service_info['service_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++){
				 	  	//echo $icnt .'<br>';
				 	 	if($service_info['booking_type']=='H'){
					 		$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>$booking_quantity){
							$week_array[$icnt][$jcnt]["class"]="calendarBookingPartial"; 
							/*if($service_info['booking_type']=='H'){
								$gran=round(60/$granular);
								$i=0;
								while($i<60){  
									$ser_st_time=date("h:i:s",timeadd(strtotime($check_tstime),"min",$i)).'<br>';
									if(get_times_quantity($tstime,$tetime,$start_date)){
										
									}else{
										
									} 	
									$i+=$gran;	
								}
							}*/
						}else if ($service_quantity<=$booking_quantity){
					 		$week_array[$icnt][$jcnt]["class"]="calendarBookingEmpty";
						}
				  	 }
				}
				 if($service_info['booking_type']=='H')$booking_quantity="";
			}
			// blocked time get
			$blck_datetime=array();
			$service_info=get_services_info((int)$service_id,(int)$languages_id);
			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($service_info['booking_type']=='D'){
					$no_booking_stime="00:00:00";
					$no_booking_etime="23:55:00";
				}
					$blck_datetime[]=array('start_date'=>$no_booking_sdate,'end_date'=>$no_booking_edate,'start_time'=>$no_booking_stime,'end_time'=>$no_booking_etime);
				}
			}
					
			//loop for hours 
			$time_array=array();
			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];
				   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	
							$date=date('Y-m-d',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
				   }
			  }
	     }
		 // calender style details
	    $booking_total=$blck_datetime; 
	    // assign no booking array for combo box dates and time
?>
<?php include(DIR_WS_INCLUDES."date_format_js.php")?>
		<script  language='javascript'>
			var max_bookings=<?php echo (float)$service_info["max_bookings"];?>;
			var time_length=<?php echo (float)$service_info["service_total_time"];?>;
			var book_type="<?php echo $service_info["booking_type"];?>";
			var book_granular="<?php echo (int)$service_info["booking_granular"];?>";
			var start_time="00:00";
			var end_time="23:59";
			var tempCheck=1;
			var cur_date="<?php echo format_date(date("Y-m-d"));?>";
			var cur_hour="<?php echo date("H").':00';?>";
			var span_days="<?php echo $service_info['span_days'];?>";
			var include_resource="<?php echo $service_info["include_resource"];?>";
			var multiple_purchase="<?php echo $service_info["multiple_purchase"];?>";
			// get block dates and timings
			var nb_dates=Array();
			<?php $count=0; 
			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[<?php echo $count;?>]="<?php echo format_date($nb_sdate).'#'.$nbstime.'#'.'24:00:00';?>";
					  nb_dates[<?php echo $count=$count+1;?>]="<?php echo format_date($nb_sdate).'#'.'00:00:00'.'#'.$nbetime;?>";	
			  <?php }else {?>
						nb_dates[<?php echo $count;?>]="<?php echo format_date($nb_sdate).'#'.$nbstime.'#'.$nbetime;?>";
		   <?php    
		  			}
		  			$count++;
			 }?>
			 // get block dates and timings
			var condition=new Array();
			var final_data="<table height='90%' border=0 width=100% align=center><tr><td class=main height=70 valign=top>";
			final_data+="<?php echo '<b>' . addslashes(stripslashes(TEXT_SERVICES_NAME)) . '</b><br>' . str_replace(chr(13). chr(10),"<br>",addslashes(stripslashes($service_info["service_name"]))) . '<p>'; ?>";
			<?php if ($service_info["conditions"] !=''){ ?>
				 final_data+="<?php echo '<b>' . addslashes(stripslashes(TEXT_CONDITIONS)) . '</b><br>' . str_replace(chr(13). chr(10),"<br>",addslashes(stripslashes($service_info["conditions"]))) .  '<p>';?>";
			<?php } ?>
				final_data+="</table><table width='100%'><tr><td align='right' class='main'><a href='javascript:window.close();'>[ close ]</a></td></tr></table>";
				condition[1]=final_data;
				function showConditions(){
					var handle;
					var feats;
					vtop=screen.availHeight /2 - screen.availHeight /6;
					vleft=screen.availWidth/2 - screen.availWidth/6;
					feats="height=350,width=350,location=no,menubar=no,resizable=no,scrollbars=yes,toolbar=no,status=no,left=" + vleft + ",top=" + vtop ;	
					handle=window.open("","conditions",feats);
					handle.document.open();
					handle.document.write('<html><head><title>Conditions</title><base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>"><link rel="stylesheet" type="text/css" href="<? echo TEMPLATE_STYLE;?>"></head>');
					handle.document.write('<body class="conditions">');
					handle.document.write(condition[1]);
					handle.document.write('</body></html>');
					handle.document.close();		
				}
				function validateForm(){
				//alert(document.quanform.elements["add_products_options["+ 1 +"]"]);
				// alert(document.quanform.elements["add_products_options["+ 1 +"]"] .options[document.quanform.elements["add_products_options[" + 1 + "] "].selectedIndex].value); 

					var frm = document.forms["select_service"];
					var error_result="";
					if (book_type=="D"){
						if (frm.start_times.value==""){
							error_result="<?php echo addslashes(ERR_SELECT_START_TIME);?>";
						} else if (frm.end_times.value==""){
							error_result="<?php echo addslashes(ERR_SELECT_END_TIME);?>";
						}
					} else {
						if (frm.start_date.value=="" || frm.start_time.value==""){
							error_result="<?php echo addslashes(ERR_SELECT_START_TIME);?>";
						} else if (frm.end_times.value==""){
							error_result="<?php echo addslashes(ERR_SELECT_END_TIME);?>";
						}
					}
					if (error_result==""){
						if (isNaN(parseInt(frm.products_qty.value))){
							error_result="<?php echo addslashes(ERROR_SELECT_HOW_MANY);?>";
						} else if(frm.elements('agreed')) {
							if (!frm.agreed.checked)
								error_result="<?php echo addslashes(ERROR_AGREE);?>";
						}
					}
					if (error_result!=""){
						alert(error_result);
						return false;
					}
					if (book_type=='D'){
						frm.start_date.value=getDatePart(frm.start_times.value,"D");
						frm.start_time.value=getDatePart(frm.start_times.value,"H");
					}
					frm.end_date.value=getDatePart(frm.end_times.value,"D");
					frm.end_time.value=getDatePart(frm.end_times.value,"H");
					return true;
				}
				function get_diff_minutes(stime,etime){
					splt1=stime.split(":");
					splt2=etime.split(":");
					smin=parseInt(splt1[0],10)*60+parseInt(splt1[1],10);
					emin=parseInt(splt2[0],10)*60+parseInt(splt2[1],10);
					return emin-smin;
				}
				function add_options(textArr,valueArr,optionElement){
					// clear the previous details
					while(optionElement.options.length>0){
						optionElement.remove(optionElement.options.length - 1);
					}
					//create the new details
					for (icnt=0;icnt<textArr.length;icnt++){
						  var option = document.createElement('option');
						  option.text = textArr[icnt];
						  option.value = valueArr[icnt];
						  try {
							optionElement.add(option, null); // standards compliant; doesn't work in IE
						  }
						  catch(ex) {
							optionElement.add(option); // IE only
						  }
					}
				}
				
				function add_start_times(){
					var tempDate=convert_to_sdate(document.select_service.start_date.value);
					var i=0;
					var hour;
					var arr_text=Array();
					var arr_value=Array();
					splt_cur_hour=cur_hour.split(":");
					var cnt=0;	
					if(tempDate==cur_date){
						i=parseInt(splt_cur_hour[0])+1;
					}
					while(true){	
					   // loop for no booking date and time
						for(j=0;j<nb_dates.length;j++){
							 n_dates=nb_dates[j].split("#");
							 n_sdate=n_dates[0];
							 nb_s_time=n_dates[1];
							 nb_e_time=n_dates[2];
							 splt_etime=nb_e_time.split(":");
							 if(n_sdate==tempDate){
							   h=((i>9)?i:'0'+i)+":00:00";
							   if(nb_s_time<=h && h<=nb_e_time || nb_s_time==h) {	
							   	   i=parseInt(splt_etime[0],10);								
							   }
						     }	 
						}// loop for no booking date and time	
						  slot=60/book_granular;	
						  if(i==24) break;
						  set_hr=((i<10)?"0"+i:i)+":00";						  
						  for(ict=0;ict<=55;ict+=slot){     	
						     var d_obj=new Date(1976,1,1,i,0,ict*60);
						     hr=((d_obj.getHours()==0)?00:d_obj.getHours());
						     mt=((d_obj.getMinutes()<10)?"0"+d_obj.getMinutes():d_obj.getMinutes());		
						     if(hr==12) hr=24;	 
						     hour=hr +":"+mt;
						     var n_obj=new Date(1976,1,1,(hr+time_length),mt);
						     if(span_days=='N' && n_obj.getDate()=="2") break;
			 	  		    	arr_text[cnt]=convert_to_stime(hour);
						     	arr_value[cnt]=hour;
						     if(i==24) i=12;	
						     cnt++;
						 }
						  i++;
						  if(i>23) break;
					} 
					if (book_type=='H')
					  add_options(arr_text,arr_value,document.select_service.start_time);
					  add_end_times();
				}
				function add_end_times(){	
					var arr_text=Array();
					var arr_value=Array();
					//check if booking is day based or hours based
					var end_time='23:59';
					var send_time=convert_to_stime('23:59:00');
					if (book_type=="D"){
						var tempDate=getDatePart(document.select_service.start_times.value,"D");
						jcnt=0;
						if (!tempDate) return;
						for (icnt=time_length;icnt<=max_bookings;icnt=icnt+time_length){
						    start_time_split=start_time.split(":");
						    hour=start_time_split[0];
						 if(hour==00){
							if(jcnt>=1)
							  	tempDate=dateAdd("d",time_length,tempDate);
							else 
								tempDate=dateAdd("d",time_length-1,tempDate);
					          }else { 
						   	tempDate=dateAdd("d",time_length,tempDate);
						 } 
							arr_value[jcnt]=tempDate+" "+end_time;
							arr_text[jcnt]=date_format(tempDate,'Y-m-d','')+" "+send_time;
							jcnt++;
						}
					} else {
						var tempTime_s=document.select_service.start_time.value;
						var tempDate=document.select_service.start_date.value;
						var check_date=tempDate;
						var tempDateS=document.select_service.start_date.value;
						var check_date=tempDateS;	
						if (!tempTime_s) return;
						if (book_granular<=0) book_granular=1;
						slot=60/book_granular;
						jcnt=0;
						elapsed=1;
						tempCheck=0;
						hour=0;
						while(true){		
							tempTime_splt=tempTime_s.split(":");
							tempdate_splt=tempDate.split("-");
							hour=parseInt(tempTime_splt[0],10)+((hour==0)?time_length:time_length);
							if(tempTime_s=="12:00"){
								hour=00+time_length;
							}else if(tempTime_s=="24:00"){
							    	hour=11+time_length;
							}else if(tempTime_splt[1]=="00"){
								hour-=1;
							}else if(tempTime_splt[0]=="24"){
								hour=12+time_length;
							}		
							hour=((hour<10)?"0"+hour:hour);
							minute=parseInt(tempTime_splt[1],10);
							minute=((minute<10)?"0"+minute:minute);
							if(minute=="00") minute="59";
							var dt=new Date(tempdate_splt[0],tempdate_splt[1]-1,tempdate_splt[2],hour,minute,1);
							getday=dt.getDate();
							if(getday<10)   getday="0"+getday; 
								getmonth=dt.getMonth()+1;
							if(getmonth<10) getmonth="0"+getmonth;
								tempDate=tempDateS=(dt.getFullYear()+"-"+getmonth+"-"+getday);
								tempTime_s=((dt.getHours()<10)?"0"+dt.getHours():dt.getHours()) + ":"+ dt.getMinutes();
							if (tempTime_s>end_time || (span_days=='N' && check_date<tempDate)) break;
								arr_text[jcnt]=date_format(tempDateS,'Y-m-d','')+" "+convert_to_stime(tempTime_s);
								arr_value[jcnt]=tempDate+" "+tempTime_s;
								elapsed+=time_length;
							if (elapsed>max_bookings){
								break;
							}
							jcnt++;	
						} 		
					}
					add_options(arr_text,arr_value,document.select_service.end_times);
				}
				function getDatePart(source,type){
					var result="";
					if (source!=""){
						splt=source.split(" ");
						if (type=="D")
							result=splt[0];
						else
							result=splt[1];
					}
				
					return result;
				}
				function convert_to_sdate(sdate){
					format="<?php echo EVENTS_DATE_FORMAT;?>";
					splt=sdate.split("-");
					switch(format){
						case "d-m-Y":
							rdate=splt[2]+"-"+splt[1]+"-"+splt[0];
							break;
						case "m-d-Y":
							rdate=splt[1]+"-"+splt[2]+"-"+splt[0];
							break;
						case "Y-m-d":
							rdate=sdate;
							break;
					}
					return rdate;
				}
				function convert_to_stime(stime){
					splt=stime.split(":");
					hour=parseInt(splt[0],10);
					minutes=parseInt(splt[1],10);
					if (hour>12 || (hour==12 && minutes>0)){
						if (hour>12) hour=hour-12;
						rtime=" PM";
					} else {
						rtime=" AM";
					}
					if (hour<10) hour="0"+hour;
					if (minutes<10) minutes="0"+minutes;
					rtime=((hour==00)?12:hour)+":"+minutes+rtime;
					return rtime;
				}
			
			
			function dateAdd(p_Interval, p_Number, p_Date){	
				if(isNaN(p_Number)){return false;}
				//convert date elements
				if (book_type=="H"){
					splt=p_Date.split(":");
					m=parseInt(splt[1],10);
					if (tempCheck==0) {
					  m--;
					  tempCheck=1;					  
					}
						var dt=new Date(1976,1,1,parseInt(splt[0],10),m);
				} else {
					splt=p_Date.split("-");
					var dt=new Date(parseInt(splt[0],10),parseInt(splt[1],10)-1,parseInt(splt[2],10));
				}
								
				p_Number = new Number(p_Number);
				switch(p_Interval.toLowerCase()){
					case "yyyy": {// year
						dt.setFullYear(dt.getFullYear() + p_Number);
						break;
					}
					case "q": {		// quarter
						dt.setMonth(dt.getMonth() + (p_Number*3));
						break;
					}
					case "m": {		// month
						dt.setMonth(dt.getMonth() + p_Number);
						break;
					}
					case "y":		// day of year
					case "d":		// day
					case "w": {		// weekday	
						dt.setDate(dt.getDate() + p_Number);
						break;
					}
					case "ww": {	// week of year
						dt.setDate(dt.getDate() + (p_Number*7));
						break;
					}
					case "h": {		// hour
						dt.setHours(dt.getHours() + p_Number);
						break;
					}
					case "n": {		// minute
						dt.setMinutes(dt.getMinutes() + p_Number);
						break;
					}
					case "s": {		// second
						dt.setSeconds(dt.getSeconds() + p_Number);
						break;
					}
					case "ms": {		// second
						dt.setMilliseconds(dt.getMilliseconds() + p_Number);
						break;
					}
					default: {
						return "invalid interval: '" + p_Interval + "'";
					}
				}
				
				if (book_type=="H") {
					hour=dt.getHours();
					minute=dt.getMinutes();
					if (hour<10) hour="0"+hour;
					if (minute<10) minute="0"+minute;
					return hour+":"+minute;
				} else {
					day=dt.getDate();
					month=dt.getMonth()+1;
					if (day<10) day="0"+day;
					if (month<10) month="0"+month;
					return dt.getFullYear()+"-"+month+"-"+day;
				}
			}
			
			function datePrepare(p_sDate,p_Type, p_Interval,p_upTo){
				var dt=date_format(p_sDate,'','',true);
				if (!dt) return;
                			var valuedate;
				var textdate;
				p_Interval = new Number(p_Interval);

				var dateArray=Array(); // first entry contains the text and second contains the value
				var jcnt=0;
				for (icnt=0;icnt<=p_upTo;icnt++){	
					switch(p_Type.toLowerCase()){
						case "yyyy":
							dt.setFullYear(dt.getFullYear() + p_Interval);
							break;
						case "H": 
							dt.setFullYear(dt.getFullYear() + (6));
							break;
						case "q": 
							dt.setMonth(dt.getMonth() + (3));
							break;
						case "m": 
							dt.setMonth(dt.getMonth() + p_Interval);
							break;
						case "y":		// day of year
						case "d":		// day
						case "w":
							dt.setDate(dt.getDate() + p_Interval);
							break;
					} 
					valuedate=date_format(dt,'','Y-m-d',false);
					//textdate=date_format(dt,'','',false);
						//dateArray[jcnt*2]=textdate;
						dateArray[jcnt]=valuedate;
						jcnt++;
				}
				return dateArray;
			}
		
			function date_format(sourceDate,srcFormat,resultFormat,bolConvertObject){
			var dt;
			if (srcFormat=="")	srcFormat="<?php echo EVENTS_DATE_FORMAT;?>";
			if (resultFormat=="")	resultFormat="<?php echo EVENTS_DATE_FORMAT;?>";
			if (typeof(sourceDate)=="string"){
				if (sourceDate=="") return false;
				if (!bolConvertObject && srcFormat!="" && resultFormat==srcFormat) return sourceDate;
				var splt=sourceDate.split("-");
				if (splt.length!=3) return false;
				switch(srcFormat){
					case "d-m-Y":
						sourceDate=new Date(parseInt(splt[2],10),parseInt(splt[1],10)-1,parseInt(splt[0],10));
						break;
					case "m-d-Y":
						sourceDate=new Date(parseInt(splt[2],10),parseInt(splt[0],10)-1,parseInt(splt[1],10));
						break;
					default:
						sourceDate=new Date(parseInt(splt[0],10),parseInt(splt[1],10)-1,parseInt(splt[2],10));
				}
			}
			if (!sourceDate) return false;
			if (bolConvertObject) return sourceDate;
			var day=sourceDate.getDate();
			var month=sourceDate.getMonth()+1;
			if (day<10) day="0"+day;
			var year=sourceDate.getFullYear();
			if (month<10) month="0"+month;
			switch(resultFormat){
				case "d-m-Y":
					var textdate=day+"-"+month+"-"+year;
					break;
				case "m-d-Y":
					var textdate=month+"-"+day+"-"+year;
					break;
				default:
					var textdate=year+"-"+month+"-"+day;
					break;
			}
			return textdate;
		}
			</script>
<?php
// BOF: WebMakerv.com Added: Show Featured subscription
if (SHOW_HEADING_TITLE_ORIGINAL=='yes') {
	$header_text = '&nbsp;';
?>
	<tr>
		<td valign="top">
		<table border="0" width="100%" cellspacing="0" cellpadding="0">
			<tr>
				<td class="pageHeading" valign="top"><?php echo $service_info["service_name"]; ?></td>
				<td class="pageHeading" align="right" valign="top"><?php echo service_costs; ?></td>
			</tr>
		</table>
		</td>
	</tr>
	<tr>
		<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
	</tr>
<?php
}else{
	$header_text =  $service_info["service_name"] .'<br>' . tep_draw_separator('pixel_trans.gif', '100%', '4');
}
?>
<?php
// BOF: Lango Added for template MOD
if (MAIN_TABLE_BORDER == 'yes'){
	table_image_border_top(false, false, $header_text);
}
// EOF: Lango Added for template MOD
?>
	<table border="0" cellpadding="2" cellspacing="0" width="100%">
		<tr>
			<td>
			<table border=0 width=100% cellpadding="0" cellspacing="0">
				<tr class="eventListing-data" valign="top">			
					<td width="50%">   
						<?php echo '<b>' . TEXT_SERVICES_DESCRIPTION . '</b><br>' . $service_info["service_description"]; ?>
					</td>
					<td class="main" valign="top">
					<?php
						if (tep_not_null($service_info["service_image"])) {
					?>
							<?php echo tep_image(DIR_WS_IMAGES . $service_info["service_image"] , $service_info["service_name"])?>
					<?php
						}
					?>
					</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td>
			<table border=0 class=formArea width=100% style='display:none' id="popup_contents">
				<tr>
					<td class="main" height="70" valign="top">
					   <?php 
							echo '<b>' . TEXT_SERVICES_NAME . '</b><br>' . $service_info["service_name"] . '<p>';
							if ($conditions!='')
							echo '<b>' . TEXT_CONDITIONS . '</b><br>' . $service_info["conditions"] .  '<p>';
						?>
					</td>
				</tr>
		   </table>
		   </td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
		</tr>
		<tr>
			<td>
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr>
					<td valign="top">
					<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 class='eventListing-data' height="30">
									<td valign="top"><?php echo '<b>' . TEXT_SERVICES_LOCATION . '</b><br>' .$service_location ;?></td>
								</tr>
								<tr>
									<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
								</tr>
							 	<tr>
									<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
								</tr>
							   <?php if($service_info['include_resource']=='Y' && $service_info['include_resource_price']=='N' || $service_info['include_resource']=='N'){?>	
								<tr class='eventListing-data' height="30">
									<td valign="top"><?php echo '<b>'. TEXT_COSTS . '</b><br>' . $currencies->display_price($service_info['service_costs'],tep_get_tax_rate($service_info['service_tax_class_id']));?></td>
								</tr>
							    <?php  } ?>
							</table>
							</td>
							<td valign="top">
							<table border="0"  cellpadding="0" cellspacing="0" width="100%">
								<tr class="eventListing-data" height="30">
									<td>
										<?php
											echo '<b>' . TEXT_MIN_BOOKING . '</b><br>' . $service_info["service_total_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
											  $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'])?$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);
									   }
										$default_service_start_time="17:00";
										if ($service_info["booking_type"]=="H"){
											echo '<b>' . TEXT_START_TIME . '</b><br>' . tep_draw_pull_down_menu('start_date',$dates_array,$default_service_start_date,' class="service_combo"  onChange="javascript:add_start_times();"'); 
											echo '&nbsp;' . tep_draw_pull_down_menu('start_time',array(),$default_service_start_time,'onChange="javascript:add_end_times();" class="service_combo" ');
										} else {
											echo '<b>' . TEXT_START_TIME . '</b><br>' . tep_draw_pull_down_menu('start_times',$dates_array,$default_service_start_date,' class="service_combo" 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,'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 ((!$resource) && $service_info["multiple_purchase"]=="Y") { ?>
									<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);
									}
								?>
							</table>
							</td>
						</tr>
						<script language="javascript">
							add_end_times();		
							add_start_times();
						</script>
					</table>
					</td>
					<?php if ($view=="month" && defined("SERVICES_SHOW_CALENDER") && (SERVICES_SHOW_CALENDER=='true' || SERVICES_SHOW_CALENDER=='True')) { ?>
						<td width="182" valign="top">
						<table border="0" cellpadding="0" cellspacing="0" width="100%" class="calendarBack">
							<TR><TD class="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 $year;?></td>
										<td class="calenTextB" align="right" valign="middle">
										<?php
											$previous_time_stamp=datesub($month_start_time,"day",1);
											$next_time_stamp=dateadd($month_end_time,"day",1);
											if ($previous_time_stamp>=mktime()){
												$params=$old_params ."year=" .date("Y",$previous_time_stamp) . "&month=" . date("m",$previous_time_stamp) . "&view=month";
												echo '<a href="' . tep_href_link(FILENAME_SERVICES_INFO,$params) . '">' . 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="' . tep_href_link(FILENAME_SERVICES_INFO,$params) . '">' . 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"><?php echo date("h:i a");?></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') && date('m',$month_start_time)==date('m') && date('D',mktime())==$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="";
										if ($icnt>=$week_day && $icnt<$week_day+$total_days) $day=($icnt+1)-$week_day;
										if($temp_month==date('Y-m-d')) $day='<font color="red"><b>'.$day.'</b></font>';
										 echo '<td class="' . $calen_data["class"] . '" width=25 height=25 valign=top>'.(($calen_data["class"]=="calendarNoBooking")?'<font color="white"><b>' . $day . '</b></font>':($calen_data["class"]=="calendarBookingEmpty")?'<font color="white"><b>'.$day.'</b></font>':$day) . '</td>';
										if($icnt)$temp_month=date('Y-m-d',tep_dateadd($month_start_time,"day",$icnt));
									}
								?>
								</table>
								</td>
							</tr>
						<tr>
							<td>
							<?php 
								echo tep_image(DIR_WS_IMAGES ."dayview.gif");
								$temptime=mktime(0,0,0,$month,1,$year);
								$date_to_string=$temptime;
								/*if ($temptime<$current_time_stamp) $temptime=$current_time_stamp;
								$tempday=date('d',datesub($temptime,"day",date('w',$temptime)));*/
								if(date('w',$temptime))	$date_to_string=tep_datesub($temptime,"day",date('w',$temptime));
								$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="' . tep_href_link(FILENAME_SERVICES_INFO,$params) . '">' . tep_image(DIR_WS_IMAGES ."weekview.gif",IMAGE_WEEK_VIEW) . '</a>';
								$params=$old_params . "&year=" . $year . "&month=" . $month . "&view=month";
								echo '<a href="' . tep_href_link(FILENAME_SERVICES_INFO,$params) . '">' . tep_image(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<mktime()) $mk_time=mktime();
					
					$month_y=date("M Y",$mk_time);
					if(date('Y',$week_start_time)<date('Y',$week_end_time) && $week_start_time!=mktime()){
					//if(date('Y',$week_start_time)<date('Y',$week_end_time) && date('Y-m-d',$week_start_time)<=date('Y-m-d') && date('Y-m-d',$week_end_time)>=date('Y-m-d'))
						$month_y=date("M Y",$week_end_time); 
						$mk_time=$week_end_time;
					}else{
						$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="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
										/*$ret=tep_time_diff(date('Y-m-d',mktime(0,0,0,1,1,$year)),$week_start_date);
										$current_week=(int)round(($ret["days"]/7)); */
										$previous_time_stamp=datesub($week_start_time,"day",7);
										$next_time_stamp=dateadd($week_end_time,"day",1);
										if ($week_start_time>=mktime()){
											$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="' . tep_href_link(FILENAME_SERVICES_INFO,$params) . '">' . tep_template_image_button('calenLeft.gif',IMAGE_PREV_WEEK,'align=absmiddle') . '</a>&nbsp;';
										} else {
											echo tep_template_image_button('calenLeft.gif','','align=absmiddle');
										}
										if(mktime()>=$mk_time && mktime()<=tep_dateadd($mk_time,"day",7) && (substr(date('D'),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="' . tep_href_link(FILENAME_SERVICES_INFO,$params) . '">' . 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 class="calenText"><?php echo date("h:i a"); ?></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;
										$week_start_time; 
										for ($icnt=0;$icnt<7;$icnt++){
											$temp=(int)date("d",$stime);
											if(date('Y-m-d',$stime)==date('Y-m-d')) {
												$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" style="cursor:pointer;cursor:hand" onMouseOut="javascript:closepopup();" onmousemove="javascript:showpopup('.$stime.','.$service_id.')"><b>' . $week_days[$icnt] . '<br>' . $temp .'</b></td><td width="1%"></td>';
											$stime=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 //$gran=array(1=>20,2=>20,3=>20,4=>20);
										for ($icnt=$shour;$icnt<$ehour;$icnt++){
											echo '<tr><td class="main" width="35" height="20" valign="top">' . sprintf("%02d",($icnt<=12?(($icnt==00)?12:$icnt):$icnt-12)) . ":00";
											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')$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" >
							<?php 
								echo tep_image(DIR_WS_IMAGES ."dayview.gif");
								if($week_start_date<date('Y-m-d')) $month=date('m');
								//$temptime=mktime(0,0,0,$month,7,$year);
								$temptime=mktime(0,0,0,$month,1,$year);
								if ($temptime<$current_time_stamp) $temptime=$current_time_stamp;
								$tempday=date('d',datesub($temptime,"day",date('w',$temptime)));	
								$params=$old_params. "&year=" . $year . "&month=" . $month . "&day=" . $tempday . "&view=week";
								echo '<a href="' . tep_href_link(FILENAME_SERVICES_INFO,$params) . '">' . tep_image(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=" . $year . "&month=" . $tempmonth . "&view=month";
								echo '<a href="' . tep_href_link(FILENAME_SERVICES_INFO,$params) . '">' . tep_image(DIR_WS_IMAGES ."monthview.gif",IMAGE_MONTH_VIEW) . '</a>';
							?>
							</td>
						</tr>
					</table>
					</td>
					<?php  }  // view 
					?> 
				</tr>
			</table> 
			</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
		</tr>
			<?php 
				// select the options
				/*$result = tep_db_query("SELECT * FROM " . TABLE_SERVICES_ATTRIBUTES . " sa LEFT JOIN " . TABLE_SERVICES_OPTIONS . " so ON so.services_options_id=sa.options_id LEFT JOIN " . TABLE_SERVICES_OPTIONS_VALUES . " sov ON sov.services_options_values_id=sa.options_values_id WHERE sa.service_id='" . (int)$HTTP_GET_VARS['service_id'] . "' and so.language_id='" . $languages_id . "' and sov.language_id='" . $languages_id . "' group by services_options_id");
				$rows=tep_db_num_rows($result);
				if ($rows>0){
			?>
				<!--	<tr class='eventListing-data'>
						<td><?php echo '<b>' . TEXT_SERVICE_OPTIONS . '</b>'; ?>
						<table border="0" cellpading="2" cellspacing="0" width="0">
						<?php
							while ($services_options_name_values = tep_db_fetch_array($result)) {
								$selected = 0;
								$services_options_array = array();
						?>
							<tr>
								<td class="main"><?php echo $services_options_name_values['services_options_name'];?>: </td>
								<td>
								<?php 
							 		$services_options = tep_db_query("select sa.services_options_sort_order, sov.services_options_values_id, sov.services_options_values_name, sa.options_values_price, sa.price_prefix,sa.options_values_id from " . TABLE_SERVICES_ATTRIBUTES . " sa, " . TABLE_SERVICES_OPTIONS_VALUES . " sov where sa.service_id = '" . $HTTP_GET_VARS['service_id'] . "' and sa.options_id = '" . $services_options_name_values['services_options_id'] . "' and sa.options_values_id = sov.services_options_values_id and sov.language_id = '" . $languages_id . "'" . " order by sa.services_options_sort_order, sa.options_values_price");
									while ($services_options_values = tep_db_fetch_array($services_options)) {
										$services_options_array[] = array('id' => $services_options_values['services_options_values_id'], 'text' => $services_options_values['services_options_values_name']);
										if ($services_options_values['options_values_price'] != '0') {
											$services_options_array[sizeof($services_options_array)-1]['text'] .= ' (' . $services_options_values['price_prefix'] . $currencies->display_price($services_options_values['options_values_price'], tep_get_tax_rate($service_info["service_tax_class_id"]),'1',true) .') ';
										}
									} // while option values
									echo tep_draw_pull_down_menu('id[' . $services_options_name_values['services_options_id'] . ']', $services_options_array, (($option_id>0)?$option_id:$cart->contents[$HTTP_GET_VARS['service_id']]['attributes'][$services_options_name_values['services_options_id']]));
								?>
								</td>
							</tr>
							<tr>
								<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '5'); ?></td>
							</tr>
						<?php } // while option name?>
						</table>
						</td>
					</tr>-->
				<?php } */// $rows ?> 
		<?php if($service_info['include_resource']=='N') { ?>		
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
			</tr>
			<tr class="eventListing-data">
				<td nowrap><?php echo tep_draw_checkbox_field('agreed','1',false) . TEXT_AGREEMENT . '<b><a id="agree" href="javascript:showConditions();" class="blue">' . TEXT_TERMS . '</a></b>';?>			</tr>
			</tr>
			
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
			</tr>
		<?php } ?>	
<?php 

    if (tep_not_null($service_info["service_url"])) {
?>
      <tr>
        <td class="main" align="center"><?php echo sprintf(TEXT_MORE_INFORMATION, tep_href_link(FILENAME_REDIRECT, 'action=url&goto=' . urlencode($service_info['service_url']), 'NONSSL', true, false)); ?></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
<?php
    }

    if ($service_info["service_date_available"] >date('Y-m-d H:i:s')) {
?>
      <tr>
        <td align="center" class="smallText" ><?php echo sprintf(TEXT_DATE_AVAILABLE, tep_date_long($service_info["service_date_available"])); ?></td>
      </tr>
<?php
    } else {
?>
      <tr>
        <td align="center" class="smallText"><?php echo sprintf(TEXT_DATE_ADDED, tep_date_long($service_info["service_date_available"])); ?></td>
      </tr>
<?php
    }
?>
</table>
<?php
// BOF: Lango Added for template MOD
if (MAIN_TABLE_BORDER == 'yes'){
table_image_border_bottom();
}
// EOF: Lango Added for template MOD
?>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
	 <?php } ?>
	<tr>
				<td align="right"><?php echo tep_template_image_submit('button_reserve.gif', IMAGE_BUTTON_RESERVE);?></td>
			</tr>	
	 <!--<tr><td colspan="3" align="left"><?php //echo '<a href="'.tep_href_link(FILENAME_SERVICES_SEARCH,'secPath='.$category_id).'">'.tep_template_image_button('button_back.gif',BUTTON_IMAGE_BACK).'</a>';?></td></tr>-->
     </table>
<input type="hidden" name="resource_costs" value="<?php echo $round_amount;?>" />
<input type="hidden" name="tax_class_id" value="<?php echo $service_tax_class_id;?>" />
</form>
<?php
	function get_times_quantity($tstime,$tetime,$start_date){
	   	global $service_id;	
	   	 $quantity="";  	 
			 $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='" . $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)>'" . getServerDate() . "') " .
							" and os.start_date='$start_date' and '$tstime' BETWEEN os.start_time AND os.end_time and '$tetime' BETWEEN os.start_time AND os.end_time group by os.start_date order by os.start_date,os.end_date ");
			 if(tep_db_num_rows($booking_sql)>0){
			 	$quan=tep_db_fetch_array($booking_sql);
			 	$quantity=$quan['quantity'];
			 }
			 return $quantity;
	}

	 
?>
<?php require(DIR_WS_JAVASCRIPT . "popup.js");?>
  <script language="JavaScript">
	var baby=new Array();
	var pevents_id=0;
	baby[1]="";
	var openpopup=0;
	function showpopup(date_string,service_id)
	{
		// check if popup has already opened
		var element=document.getElementById("show_popup_display");
		content='<table border="0" cellspacing="0" cellpadding="4" bgcolor="#f2fca73" width="100%"><tr><td id="popContent" class="main"></td></tr></table>';
		content+=element.innerHTML;
		content+='</div></td></tr></table>';
		 
		baby[1]=content;
		if (baby[1]!=""){
			popup("1","#f2fca73");
		}	
		// if needed to get the contents using ajax
		if (date_string!='' && service_id!=''){
			command="<?php echo tep_href_link(FILENAME_SERVICES_INFO);?>&command=fetch_service_details&service_id="+service_id+"&date_string="+date_string;
			do_get_command(command);
		}
		document.getElementById("popContent").innerHTML="Loading...";
	}
	function do_result(result){
		// assign the result content
		element=document.getElementById("popContent");
		if (element) element.innerHTML=result;
	}
	
	function closepopup(){
		do_close();
		kill();
	}
//  End -->

</script>