<?php


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

Copyright 2007 ZacWare Pty. Ltd
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	require('includes/application_top.php');
	include(DIR_WS_CLASSES . 'order.php');
	include(DIR_WS_CLASSES . 'currencies.php');
	$oId=isset($HTTP_GET_VARS['oID'])?(int)$HTTP_GET_VARS['oID']:0;
	if ($oID<=0){
		tep_redirect(tep_href_link(FILENAME_ORDERS));
	}	
	$error="";
	$where="";
	$reserved_quantity="";
	$currencies=new currencies();
	$navigation=isset($HTTP_POST_VARS['navigation'])?$HTTP_POST_VARS['navigation']:''; 
	$step=isset($HTTP_POST_VARS['step'])?$HTTP_POST_VARS['step']:2;
	$mode=isset($HTTP_GET_VARS['mode'])?$HTTP_GET_VARS['mode']:'';
	$id=(int)isset($HTTP_GET_VARS['id'])?$HTTP_GET_VARS['id']:0;
	$start_date=isset($HTTP_POST_VARS['start_date'])?$HTTP_POST_VARS['start_date']:'0000-00-00';
	$start_time=isset($HTTP_POST_VARS['start_time'])?$HTTP_POST_VARS['start_time']:'0000-00-00';
	$start_times=isset($HTTP_POST_VARS['start_times'])?$HTTP_POST_VARS['start_times']:'00:00:00';
	$end_times=isset($HTTP_POST_VARS['end_times'])?$HTTP_POST_VARS['end_times']:'00:00:00';
	$splt_date=split(" ",$end_times);
	$end_date=$splt_date[0];
	$end_time=$splt_date[1];
	$service=get_services_info((int)$service_id,(int)$languages_id);
	if($service['booking_type']=='D'){
		$splt_stime=split(" ",$start_times);
		$start_date=$splt_stime[0];
		$start_time=$splt_stime[1];
	}
	$resource_id=isset($HTTP_POST_VARS['resource_ids'])?$HTTP_POST_VARS['resource_ids']:"0";	
	$splt=split("-",$resource_id);	
	if($splt[0]) $resource_id=$splt[0];
	$resource_costs=isset($HTTP_POST_VARS['resource_costs'])?$HTTP_POST_VARS['resource_costs']:0;;
	$include_resource=isset($HTTP_POST_VARS['include_resource'])?$HTTP_POST_VARS['include_resource']:'N';
	$sel_quantity=isset($HTTP_POST_VARS['ser_quantity'])?(int)$HTTP_POST_VARS['ser_quantity']:1;
	$quantity=(int)$sel_quantity;
	if($start_time=='24:00') $start_time="12:00";
	if ($mode=="edit" && $step==2)
		$service_id=(int)isset($HTTP_GET_VARS['service_id'])?(int)$HTTP_GET_VARS['service_id']:0;
	 else 
		$service_id=(int)isset($HTTP_POST_VARS['service_name'])?(int)$HTTP_POST_VARS['service_name']:0;
	 
	$page_params="oID=" . $oId . '&mode=' . $mode . '&id=' . $id;		
	$attribute_array=array();
	$edit_mode=false;
	//get customer id
	$customer_find=tep_db_query("select customers_id from " . TABLE_ORDERS . " where orders_id='" . (int)$oID . "'");
	$customer_result=tep_db_fetch_array($customer_find);
	$customer_id=(int)$customer_result['customers_id'];
	// get service details  
	$booking_type=$service['booking_type'];
	$include_resource_price=$service['include_resource_price'];
	$service_quantity=(int)(($service['include_resource']=='Y')?1:$service['quantity']);
	if(!tep_check_service_found($service_id,$start_date,$end_date,$start_time,$end_time) && $service['include_resource']=='N')
		$error=tep_check_service_found($service_id,$start_date,$end_date,$start_time,$end_time,'error');	  
	$resource_query="";
	if($service['include_resource']=='Y' || $error) {
		$reserved_resource_id="";
		if($service_id){ 
		//get resource details
		    $start_time_split=split(":",$start_time);
			$start_time=sprintf("%02d:%02d",$start_time_split[0],$start_time_split[1]);
	   //get resource details	
			 $res_num_rows=0;
			 if(!$reserved_resource_id) $reserved_resource_id=0;
			 $resource_query=tep_db_query("select red.resource_name,red.resource_description,re.resource_id,re.resource_costs,re.resource_tax_class_id from " . TABLE_RESOURCES . " re, " . TABLE_RESOURCES_DESCRIPTION . " red," .  TABLE_SERVICES_TO_SERVICES_RESOURCES . " seTre where seTre.service_id='" . (int)$service_id . "' and re.resource_id not in(".$reserved_resource_id.")  and red.language_id='".(int)$languages_id."' and seTre.service_resource_id=re.resource_id and re.resource_id=red.resource_id order by red.resource_name");			 
			 $res_num_rows=tep_db_num_rows($resource_query);
			 //if(!$res_num_rows)$error=ERR_TIME_ALREADY_RESERVED;
			}
			$quantity=1;
		}	 
		$products_id=get_details('resource_id','orders_edit_services',"start_date='".$start_date."' and end_date='".$end_date."' and start_time='".$start_time."' and end_time='".$end_time."' and products_id='".(int)$service_id."' and resource_id='".(int)$resource_id."'",$list=true);
		if($products_id && $mode!='edit'){
		while(list($key,$value)=each($products_id)){
		  if($products_id[$key] && $service['include_resource']=='Y')
			$mode="";
		  }
		}
	if ($step>4){ 
		$service=get_services_info((int)$service_id,(int)$languages_id);
		$sInfo=new objectInfo($service);
		$AddedOptionsPrice = 0;
		if(isset($HTTP_POST_VARS['option_ids'])){
			$option_array=split(",",$HTTP_POST_VARS['option_ids']); 
			while(list($key,$value)=each($option_array)){
				$attribute_split=split("-",$value);
				if($attribute_split[0])		$attribute_array[].=$attribute_split[0] .'#'.$attribute_split[1];
			}
		}
		
		if(isset($attribute_array)){
		 for($at=0;$at<=sizeof($attribute_array);$at++){
				$splt=split("#",$attribute_array[$at]);
				$attrib_id=$splt[0];
				$option_id=$splt[1];		
				$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)$service_id. "' and so.services_options_id= '" .(int)$attrib_id . "' and sov.services_options_values_id= '" . (int)$option_id . "' and sov.language_id='" . (int)$languages_id . "' and so.language_id='" . (int)$languages_id . "'");
				$row = tep_db_fetch_array($result);
					if($row){
						extract($row, EXTR_PREFIX_ALL, "opt");
						$AddedOptionsPrice += $opt_options_values_price;
						$option_value_details[$opt_services_options_id][$opt_services_options_values_id] = array ("options_values_price" => $opt_options_values_price);
						$services_options_names[$opt_services_options_id] = $opt_services_options_name;
						$services_options_values_names[$opt_services_options_values_id] = $opt_services_options_values_name;
		  			}
		  }
		}
		$order_detail=new order($oID);
		$CountryID = tep_get_country_id($order_detail->billing["country"]);
		$ZoneID = tep_get_zone_id($CountryID, $order_detail->billing["state"]);
		$product_tax = tep_get_tax_rate($sInfo->service_tax_class_id, $CountryID, $ZoneID);
		$product_type='V';
		$product_price=$sInfo->service_costs;
		if($resource_id && $sInfo->include_resource_price=='Y') $product_price=$resource_costs;
		// get loading price
		$loading_price_dates=get_loading_array((int)$service_id);
		$sInfo=new objectInfo($service);
		// find the final price by calculating the total time
		$loading_dates=array('loading_price_dates'=>$loading_price_dates,'start_date'=>$start_date,'end_date'=>$end_date,'start_time'=>$start_time,'end_time'=>$end_time);
		if($resource_id && $sInfo->include_resource_price=='Y'){
			$product_tax=tep_get_tax_rate(tep_get_resource('resource_tax_class_id',$HTTP_POST_VARS['resource_ids'],(int)$languages_id), $CountryID, $ZoneID);
			$final_price=tep_calc_service_cost($service,$start_date . " " . $start_time,$end_date . " " . $end_time,$AddedOptionsPrice,$loading_dates,$quantity,$product_price);
		}else
			$final_price=tep_calc_service_cost($service,$start_date . " " . $start_time,$end_date . " " . $end_time,$AddedOptionsPrice,$loading_dates,$quantity,$product_price);					
		//get resource name	 
		$resource_name="";
	 	if($resource_id)	$resource_name=tep_get_resource('resource_name',(int)$resource_id,(int)$languages_id);
		//to check the quantity with service total quantity
		
		$sql_array=array('orders_id'=>(int)$oID,
				    	 'products_id' =>(int)$service_id,
						 'products_name' =>$sInfo->service_name,
						 'products_price'=>((strpos($product_price,".")>0)?$product_price:(int)$product_price),
						 'final_price' =>((strpos($final_price,".")>0)?$final_price:(int)$final_price),
						 'products_tax' =>$product_tax,
						 'products_quantity'=>$quantity,
						 'products_type'=>$product_type,
						 'date_added'=>'now()'
						 ); 
		//print_r($sql_array);		
		$service_array=array('products_id'=>(int)$service_id,
						   'start_date'=>$start_date,
						   'end_date'=>$end_date,
						   'start_time'=>$start_time,
						   'end_time'=>$end_time,
						   'resource_id'=>(int)$resource_id,
						   'resource_name'=>$resource_name,
						   'orders_id'=>$oID);
		// check if going to edit or add
		$info_query=tep_db_query("SELECT count(oep.products_quantity)as products_quantity,oes.products_id,oes.start_date,oes.end_date,time_format(oes.start_time,'%H:%i') as start_time,time_format(oes.end_time,'%H:%i') as end_time from " . TABLE_ORDERS_EDIT_SERVICES . " oes, " . TABLE_ORDERS_EDIT_PRODUCTS . 
							    " oep where oep.orders_id='" . (int)$oID . "' and oep.products_id='" . (int)$service_id . "' and oep.products_id=oes.products_id and oep.orders_id=oes.orders_id and oes.start_date='".$start_date."' and oes.end_date='".$end_date."' and oes.start_time='".$start_time."' and oes.end_time='".$end_time."'".(($resource_id)?"and oes.resource_id='".(int)$resource_id."'":'')." group by oep.products_id");
	   	   		 
		$info_result=tep_db_fetch_array($info_query);
		if ($service_id==$info_result["products_id"])
			$products_quantity=(int)$info_result["products_quantity"];		
		$error=found_service();
		if($mode=='edit' && $id>0 && $error==''){	
			// get the previous id to update service details	
			tep_db_perform(TABLE_ORDERS_EDIT_PRODUCTS,$sql_array,'update',"orders_products_id='" . $id . "'");
			tep_db_perform(TABLE_ORDERS_EDIT_SERVICES,$service_array,'update',"orders_id='" . $oID . "' and orders_products_id='".$id."'");
			tep_db_query("DELETE from " . TABLE_ORDERS_EDIT_PRODUCTS_ATTRIBUTES . " where orders_products_id='" . (int)$id . "'");
		} else if($error=='') {
				$curDate=getServerDate(true);
				tep_db_perform(TABLE_ORDERS_EDIT_PRODUCTS,$sql_array);
				$orders_product_id=tep_db_insert_id();
				$service_array['orders_products_id']=$orders_product_id;	
				tep_db_perform(TABLE_ORDERS_EDIT_SERVICES,$service_array);
		} 
		
		if(is_array($attribute_array) && $error==""){
			for($at=0;$at<sizeof($attribute_array);$at++){
				$splt=split("#",$attribute_array[$at]);
				$attrib_id=$splt[0];
				$option_id=$splt[1];
				$new_product_id=$id;
				if($mode!='edit' || $id=='0'){	
					$insert_query=tep_db_query("select max(orders_products_id)  as orders_products_id from ".TABLE_ORDERS_EDIT_PRODUCTS." where orders_id='".(int)$oID."' and products_id='".(int)$service_id."'");
					$insert_id=tep_db_fetch_array($insert_query);
					$new_product_id=$insert_id['orders_products_id'];
				}
			$Query = "insert into " . TABLE_ORDERS_EDIT_PRODUCTS_ATTRIBUTES . " set
				orders_id = '".(int)$oID."',
				orders_products_id = '".(int)$new_product_id."',
				products_options = '" . $services_options_names[$attrib_id] . "',
				products_options_values = '" . $services_options_values_names[$option_id] . "',
				options_values_price = '" .  $option_value_details[$attrib_id][$option_id]["options_values_price"] . "',
				price_prefix = '+';";
			  tep_db_query($Query);
			}
		}
		if($error=='')	tep_redirect(tep_href_link(FILENAME_EDIT_ORDERS,'oID=' . $oID . '&step=2'));		
	}			
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<?php include(DIR_WS_INCLUDES."date_format_js.php");?>
<script language="JavaScript">
	function selectServiceItem(boxname,id,index){
		var element=document.getElementById(boxname+id);
		var field=document.select_service.elements[boxname+"_index"];
		field.value=index;
		field=document.select_service.elements[boxname];
		if (field.value!=''){
			document.getElementById(boxname+field.value).style.backgroundColor="#FFFFFF";
		}
		field.value=id;
		element.style.backgroundColor="#D8EDFB";
	}
	
	function setElementPos(){
		var boxnames=Array("service_name","service_date","start_time_hours","end_time_hours");
		var ids=Array("<?php echo $service_id;?>","<?php echo $start_date;?>","<?php echo $start_time;?>","<?php echo $end_time;?>");
		var box;
		var element;
	
		for (icnt=0;icnt<boxnames.length;icnt++){
			box=document.getElementById(boxnames[icnt]+"_box");
			element=document.getElementById(boxnames[icnt]+ids[icnt]);
			if (box!=null && element!=null)
				box.scrollTop=element.offsetTop;
		}
	}

	function rowOverEffect(object) {	
	 	if (object.className == 'RowEven') object.className = 'RowOdd';
	}

	function rowOutEffect(object) {
	  	if (object.className == 'RowOdd') object.className = 'RowEven';
	}
	function on_select(cou){
	   if(document.select_resource){
	  	var frm=document.select_resource.elements["resource[]"];
		var resource_id="";
		if(frm){ 
		 if(frm.length){
			frm[0].checked=true;
			frm[cou].checked=true;
			for(i=0;i<frm.length;i++){
			 if(frm[i].checked){ 
			  resource_split=frm[i].value.split('#');
			  resource=resource_split[0].split('-');
			  st_time_string=resource_split[1];
			  et_time_string=resource_split[2];
			  start_date=getDatePart(st_time_string,'D');
			  start_time=getDatePart(st_time_string,'H');
			  end_date=getDatePart(et_time_string,'D');
			  end_time=getDatePart(et_time_string,'H');
			  end_times=et_time_string;
			  resource_id=resource[0];
			  costs=resource[1];
			 }
		   } 
		 }else { 
		 	frm.checked=true;
		 	if(frm.checked) {
		 		resource_split=frm.value.split('#');
			    resource=resource_split[0].split('-');
		 	}
			resource_id=resource_split[0];
			st_time_string=resource_split[1]; 
			et_time_string=resource_split[2]; 	
			start_date=getDatePart(st_time_string,'D');
			start_time=getDatePart(st_time_string,'H');
			end_date=getDatePart(et_time_string,'D');
			end_time=getDatePart(et_time_string,'H');
			end_times=et_time_string;
			costs=resource[1];
		 } 
		 document.select_resource.resource_ids.value=resource_id;
		 document.select_resource.resource_costs.value=costs;
		 document.select_resource.start_date.value=start_date;
		 document.select_resource.start_time.value=start_time;
		 document.select_resource.end_times.value=end_times;
		 //alert(end_times + " " + end_date + " " +end_time);
		 formatdate=dateAdd('n',1,end_times,true);
		 if(document.getElementById("div_dates")) document.getElementById("div_dates").innerHTML="<span class=smalltext><?php echo '<b>'.TEXT_START_DATE.'</b>&nbsp;';?>"+date_format(start_date,'Y-m-d','<?php echo EVENTS_DATE_FORMAT;?>')+" "+convert_to_stime(start_time)+"<?php echo '&nbsp;  &nbsp;&nbsp;&nbsp;<b>'.TEXT_END_DATE.'</b>&nbsp;';?>"+date_format(getDatePart(formatdate,'D'),'Y-m-d','<?php echo EVENTS_DATE_FORMAT;?>')+" "+convert_to_stime(getDatePart(formatdate,'H'))+"</span>";
		}
	  }
	}
	
</script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onLoad="setElementPos();">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

	<!-- body //-->
	<table border="0" width="100%" cellspacing="2" cellpadding="2">
		<tr>
	<!-- body_text //-->

    	<td  width="100%" valign="top">
			<table border="0" width="100%" cellspacing="0" cellpadding="0"> 
		 		<tr>
					<td><table border="0" width="67%" cellspacing=0 cellpadding=0 ?>
						<tr>
							<!--<td class="pageHeading"><?php //echo ($mode=="edit"?HEADING_TITLE_EDIT:HEADING_TITLE); ?> #<?php //echo $oID; ?></td>!-->
							<td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', 1, HEADING_IMAGE_HEIGHT); ?></td>
							<td width="30%" class="pageHeading" align="right"><?php echo '<a href="' . tep_href_link(FILENAME_EDIT_ORDERS, "oID=$oId&step=2") . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?></td>
						</tr>
						</table> 
					</td>
				</tr>
				<tr>
					<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
				</tr>
				<!--- Step 1 ---> 
				<tr>
					<td>
						<table border="0" width=100% cellspacing="0" cellpadding="1"> 
							<?php 
							 	echo tep_draw_form("select_service",FILENAME_ADD_SERVICES,$page_params,'post');
							 ?>
							<tr class="attributes-odd" valign="middle">
								<td class="dataTableContent" align=left height="20"><b><?php echo SERVICE_STEP1; ?></b></td>
							</tr>
							<tr class="attributes-even">
								<td>
								<table border="0" cellpadding="2" cellspacing="0">
									<tr>
										<td class="dataTableContent" width="100"><?php echo TEXT_SERVICE_NAME;?></td>
										<td><?php echo tep_draw_separator('pixel_trans.gif', '20', ''); ?></td>
										<td class="dataTableContent" align=left valign="middle" width="550">
											<?php  $service_array=tep_get_service_time_info_tree($customer_id);
												   customServiceListBox('service_name',$service_array,$service_id,"100%","100px");
											 ?> 
										</td>
										<td><?php echo tep_draw_separator('pixel_trans.gif', '1', ''); ?></td>
											<td class="main" align="center" valign="bottom"><?php echo tep_image_submit("button_select.gif",IMAGE_SELECT_SERVICE);?></td>	
									</tr>
								</table>	
								</td>	
							</tr>
							<input type='hidden' name='step' value='3'>
							<input type='hidden' name='mode' value='<?php echo $mode; ?>'>
							</form>
						</table>
				</td>
			</tr>
		<?php 
			if($step>2) {
			 	$current_date=getServerDate(true);
				$current_time=strtotime(getServerDate(true));
				$combo_end_date=date('Y-m-d',tep_dateadd($current_time,"day",30));
				$service_info_query=tep_db_query( "select se.service_id,se.multiple_purchase,se.service_total_time,se.booking_granular,se.booking_type,se.quantity,".
							  				   "se.service_location, sed.service_name, sed.service_description, se.service_image, sed.service_url, se.service_costs, se.service_tax_class_id, se.service_date_added, se.service_date_available, se.pending_period, " .
											    "se.service_total_time,se.max_bookings," . 
											    "se.span_days,include_resource from " . TABLE_SERVICES . " se, " . TABLE_SERVICES_DESCRIPTION . " sed  where se.service_status = '1' and se.service_id = '" . 
											     (int)$service_id . "' and sed.service_id = se.service_id and sed.language_id = '" . (int)$languages_id . "'");
														
				$service_info = get_services_info($service_id,$languages_id);
				$service=new objectInfo($service_info);
				$booking_type=$service->booking_type;
				$time_length=$service->service_total_time;
				$max_bookings=$service->max_bookings;
				$service_week=$service->service_week;
				$recurring_details=$service->recurring_details;
				
				if ($time_length==0) $time_length=1;
				if ($max_bookings==0) $max_bookings=1;
						
				$service_costs = $currencies->display_price($service->service_costs, tep_get_tax_rate($service->service_tax_class_id));
				$granular=(int)$service->booking_granular;
				if ($granular==0) $granular=1;
				$slot=60/$granular;
				$select_edate="";
				$combo_start_date=date('Y-m-d',strtotime($current_date));
				/*Month */
				$current_month=date("n",$current_time);
				$current_year=date("Y",$current_time);
				$year=date("Y",$current_time);
				$month=date("n",$current_time);
				$month_start_time=mktime(0,0,0,$month,1,$year);
				$week_day=date("w",$month_start_time);
				$total_days=date("t",$month_start_time);
				$month_end_time=mktime(0,0,0,$month,$total_days,$year);
				$month_end_date=date('Y-m-d',$month_end_time);
				$service_start_time="00:00:00";
				if ($booking_type=='H'){
				$shour=0;
				$ehour=24;
				$no_booking_sdate='';
				$no_booking_edate='';
				$cur_time=date("H",$current_time) .':00:00';
				$span_days=$service->span_days;
				$service_end_time="24:00:00";
				//$bk_query="select recurring_type,recurring_details,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='". $service_id . "' order by recurring_type,no_booking_end_date";
				$bk_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  ('$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_end_date,no_booking_start_time,no_booking_end_time";
				$bk=tep_db_query($bk_query);
				$dates=array();
				$n_dates=array();
				while($nbk=tep_db_fetch_array($bk)){					
				$recurring_type=$nbk['recurring_type'];				
				$sInfo=$nbk;													
				if($recurring_type!='DD'){ 
					$nb_dates=get_dates_array($sInfo,$recurring_type);					
					$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'){					 
					 $tep_start_date=$combo_start_date;					 										
					 $tep_end_date=$combo_end_date;					 					
					 while($tep_start_date<=$tep_end_date){					   					    					    					   
					    if($nbk['no_booking_start_date']<=$tep_start_date && $nbk['no_booking_end_date']>=$tep_start_date){					 		
					 		$no_b_stime=$nbk['no_booking_start_time'];
							$no_b_etime=$nbk['no_booking_end_time'];													
							$no_b_sdate=$no_b_edate=$tep_start_date;														
					 		if($no_b_etime<=$no_b_stime && $span_days=='Y') 
								$no_b_edate=date("Y-m-d",tep_dateadd(@strtotime($tep_start_date),"day",1));
							else if($no_b_etime<=$no_b_stime && $span_days=='N')
								$no_b_etime="24:00:00";								
							if($tep_start_date<=$no_b_sdate && $tep_end_date>=$no_b_sdate){								
								$booking_total[]=array('start_date'=>$no_b_sdate,'end_date'=>$no_b_edate,'start_time'=>$no_b_stime,'end_time'=>$no_b_etime);								
								//$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' && $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($tep_start_date) $tep_start_date=date('Y-m-d',tep_dateadd(@strtotime($tep_start_date),"day",1));
					 }
				}								
					/*$sInfo=new objectInfo($nbk);
					$recurring_type=$sInfo->recurring_type;
					//$nb_dates=get_dates_array($sInfo,$recurring_type);					
					$recurring_type=$nbk['recurring_type'];
					$type_start_date=$current_date;
					$type_end_date=$combo_end_date;
					$nbd_array[]=formated_type_dates('dates');
					$nb_dates_array[]=formated_type_dates('n_dates');*/
				}
				$block_dates=$nb_dates_array;
				$dates_array=tep_get_service_dates_array($combo_start_date,$combo_end_date,$booking_type);
				/*for ($icnt=$shour;$icnt<$ehour;$icnt++){
					for ($jcnt=0;$jcnt<7;$jcnt++){
						$week_array[$icnt][$jcnt]=array("class"=>"calendarBookingDefault","cnt"=>0);
					}
				}*/
			// get no booking dates	
			/*for($j=0;$j<sizeof($nbd_array);$j++){  //for  start	
			   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_booking_stime_splt=split(":",$no_booking_stime);
			   $no_booking_etime_splt=split(":",$no_booking_etime);			
			   $time_array=array();
			   $ict=$no_booking_stime_splt[0];	
			   if($ict=="12"){
				$ict="00";
			   }
			  while($ict<=$no_booking_etime_splt[0]){
				$time_array[]=array('block_time'=>$ict.':00');
				$ict++;
				if($ict<10) $ict='0'.$ict;
			   }
			for ($icnt=$week_day;$icnt<=$total_days+$week_day;$icnt++){
				$date=date('Y-m-d',mktime(0,0,0,$month,($icnt+1)-$week_day,$year));
				// check no booking date is exists
				if ($date==$no_booking_sdate && $date==$no_booking_edate &&($no_booking_stime=="01:00:00" && $no_booking_etime=="24:00:00" || $booking_type=="D")){
						$week_array[$icnt][$jcnt]["time"]=$time_array;
				}
		    }	
		  } 
		}//for end*/
		$select_sdate=$start_date;
		$select_stime=$start_time;
		$select_edate=$end_date . " " . $end_time;
		} else {					
				$shour=0;
				$ehour=24;
				$no_booking_sdate='';
				$no_booking_edate='';
				$cur_time=date("H",$current_time) .':00:00';
				$span_days=$service->span_days;
				$service_end_time="24:00:00";
				//$bk_query="select recurring_type,recurring_details,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='". $service_id . "' order by recurring_type,no_booking_end_date";
				$bk_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 (('$current_date'>=no_booking_start_date and '$current_date'<=no_booking_start_date) OR  ('$combo_end_date'>=no_booking_start_date and '$combo_end_date'<=no_booking_end_date) OR ('$current_date'<=no_booking_start_date and '$combo_end_date'>=no_booking_end_date)) order by no_booking_start_date,no_booking_end_date,no_booking_start_time,no_booking_end_time";
				$bk=tep_db_query($bk_query);
				$dates=array();
				$n_dates=array();
				$sel_ssplit=split(" ",$current_date);
				$select_sdate=$sel_ssplit[0];
				while($nbk=tep_db_fetch_array($bk)){
					/*$sInfo_loading=new objectInfo($nbk);
					$recurring_type=$sInfo_loading->recurring_type;
					$nb_dates=service_get_dates_array($sInfo_loading,$recurring_type);
					$recurring_type=$nbk['recurring_type'];					
					$type_start_date=$select_sdate;
					$type_end_date=$combo_end_date;
					$nbd_array[]=formated_type_dates('dates');
					$nb_dates_array[]=formated_type_dates('n_dates');*/					
					$recurring_type=$nbk['recurring_type'];					
					if($recurring_type!='DD'){ 
						$nb_dates=service_get_dates_array($sInfo,$recurring_type);					
						$start_date=$select_sdate;
						$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_start_date=$select_sdate;											
					 	$tep_end_date=$combo_end_date;					 
					 	while($tep_start_date<=$tep_end_date){					   					    
						    if($nbk['no_booking_start_date']<=$tep_start_date && $nbk['no_booking_end_date']>=$tep_start_date){					 		
					 		$no_b_stime=$nbk['no_booking_start_time'];
							$no_b_etime=$nbk['no_booking_end_time'];													
							$no_b_sdate=$no_b_edate=$tep_start_date;
							if($booking_type=='D'){
								$no_b_stime="00:00:00";
								$no_b_etime="24:00:00";
								$nb_dates_array[][format_date($no_b_sdate)]='full';
							}
							//echo $tep_start_date.'-'.$no_b_sdate.'-'.$tep_end_date.'-'.$no_b_sdate.'<br>';								
					 		if($no_b_etime<=$no_b_stime && $span_days=='Y') 
								$no_b_edate=date("Y-m-d",tep_dateadd(@strtotime($tep_start_date),"day",1));
							else if($no_b_etime<=$no_b_stime && $span_days=='N')
								$no_b_etime="24:00:00";
							if($tep_start_date<=$no_b_sdate && $tep_end_date>=$no_b_sdate){								
								$booking_total[]=array('start_date'=>$no_b_sdate,'end_date'=>$no_b_edate,'start_time'=>$no_b_stime,'end_time'=>$no_b_etime);
								//$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' && $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($tep_start_date) $tep_start_date=date('Y-m-d',tep_dateadd(@strtotime($tep_start_date),"day",1));
					 }
				}
			 }							
			$block_dates=$nb_dates_array;
			$granular=1;
			$dates_array=tep_get_service_dates_array($current_date,$combo_end_date,$booking_type);
			$select_sdate=$start_date . ' ' . $start_time;
			$select_edate=$end_date . ' ' . $end_time;
		}
		if ($mode=="edit" && $start_date=="" && $end_date==""){
			// fetch the old details for current service booking
			$info_query=tep_db_query("SELECT oep.products_quantity,oes.products_id,oes.start_date,oes.end_date,time_format(oes.start_time,'%H:%i') as start_time,time_format(oes.end_time,'%H:%i') as end_time from " . TABLE_ORDERS_EDIT_SERVICES . " oes, " . TABLE_ORDERS_EDIT_PRODUCTS . 
									" oep where oep.orders_id='" . (int)$oID ."' and oep.products_id='" . (int)$service_id . "' and oep.products_id=oes.products_id and oep.orders_id=oes.orders_id and oes.start_date='".$start_date."' and oes.end_date='".$end_date."' and oes.start_time='".$start_time."' and oes.end_time='".$end_time."' group by oep.products_id");
			$info_result=tep_db_fetch_array($info_query);
			if ($service_id==$info_result["products_id"]){
				$start_date=$info_result["start_date"];
				$end_date=$info_result["end_date"];						
				$start_time=$info_result["start_time"];
				$end_time=$info_result["end_time"];
				$quantity=$info_result["products_quantity"];
				$start_times=$info_result["start_date"].' '.$info_result["start_time"];
			}
		}
		// check for the service already reserved
		if ($step>3 ){
			$curDate=date('Y-m-d',$current_time);
		}
	/*$booking_total=array();
	for($j=0;$j<sizeof($nbd_array);$j++){ 
		for($ic=0;$ic<sizeof($nbd_array[$j]);$ic++){ 
		 $booking_total[]=array('start_date'=>$nbd_array[$j][$ic]['n_s_date'],'end_date'=>$nbd_array[$j][$ic]['n_e_date'],'start_time'=>$nbd_array[$j][$ic]['n_s_time'],'end_time'=>$nbd_array[$j][$ic]['n_e_time']);
		}			
	}*/	
	$no_resource_display="false";
	$no_attributes_display="false";
	$result_attributes = 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)$service_id . "' and so.language_id='" . (int)$languages_id . "' and sov.language_id='" . (int)$languages_id . "' group by services_options_id");
	$rows=tep_db_num_rows($result_attributes);
	if(!$rows && $service_id && $start_date && $error=='')$no_attributes_display="true";
	if(!$res_num_rows && $service_id && $start_date && $error=='')$no_resource_display="true";
?>
<script language="javascript">
			var max_bookings=<?php echo (int)$service->max_bookings;?>;
			var time_length=<?php echo (int)$service->service_total_time;?>;
			var book_type="<?php echo $service->booking_type;?>";
			var book_granular="<?php echo (int)$service->booking_granular;?>";
			var service_quantity="<?php echo (int)$service->quantity;?>";
			var start_time="00:00";
			var end_time="23:59";
			var span_days="<?php echo $service->span_days;?>";
			var tempCheck=1;
			var cur_hour="<?php echo date("H",strtotime(getServerDate(true))).':00';?>";
			var cur_date="<?php echo format_date(getServerDate(true));?>";
			var include_resource="<?php echo $service->include_resource;?>";
			var multiple_purchase="<?php echo $service->multiple_purchase;?>"; 
			var nb_dates=Array(); 
			var st_time="<?php echo $start_time;?>";
			var et_time="<?php echo $end_times;?>";
			var sel_start_date="<?php echo $HTTP_POST_VARS['start_date'];?>";
			var sel_start_time="<?php echo $HTTP_POST_VARS['start_time'];?>";
			if(book_type=='D')
				var sel_start_times="<?php echo $HTTP_POST_VARS['start_times'];?>";	
			<?php
			$booking_buffer=$service->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];
	  		}
			?>
			var book_buffer="<?php echo $book_buffer;?>";
			var book_buffer_minute="<?php echo $book_buffer_minute;?>"; 
			<?php $count=0;
			for($bk=0;$bk<sizeof($booking_total);$bk++){ 
			 $nbstime=$booking_total[$bk]['start_time'];
			 $nbetime=$booking_total[$bk]['end_time'];
			 $nb_sdate=$booking_total[$bk]['start_date'];
			 $nb_edate=$booking_total[$bk]['end_date'];
			if($nbstime=="12:00:00"){ $nbstime="00:00:00"; } 
			 if($nb_sdate!=$nb_edate || $nbstime>$nbetime){
			?>
			  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++;
			 }?>
			 var attribute_array=Array();
			<?php $cou=0;
				  $pre_id=""; 	
				  $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='" . $service_id . "' and so.language_id='" . $languages_id . "' and sov.language_id='" . $languages_id . "' order by services_options_id");
				  $rows=tep_db_num_rows($result); 
				  if($rows){
				   while($attribute=tep_db_fetch_array($result)){
				    if($pre_id!=$attribute['services_options_id']){?>
						attribute_array[<?php echo $cou;?>]="<?php echo $attribute['services_options_id'];?>";
			<?php   	$pre_id=$attribute['services_options_id'];
						$cou++;
					 }
					} 
				 }?>
				 add_start_times();
				 add_end_times();
				function ValidateForm(){
					var frm = document.select_period;
					var error_result="";
					if (book_type=="D"){
						if (frm && frm.start_times.value==""){
							error_result="<?php echo addslashes(ERR_SELECT_START_TIME);?>";
						} else if (frm && frm.end_times.value==""){
							error_result="<?php echo addslashes(ERR_SELECT_END_TIME);?>";
						}
					} else {
						if (frm && frm.start_date.value=="" || frm && frm.start_time.value==""){
							error_result="<?php echo addslashes(ERR_SELECT_START_TIME);?>";
						} else if (frm && frm.end_times.value==""){
							error_result="<?php echo addslashes(ERR_SELECT_END_TIME);?>";
						}
					}
					if(frm && frm.ser_quantity && isNaN(frm.ser_quantity.value)){
						error_result="<?php echo ERR_QUANTITY_S;?>";
					}
					if (error_result!=""){
						alert(error_result);
						return false;
					}
					var option_id="";
					if(frm && include_resource=='N' &&  multiple_purchase=='Y')	
						frm.ser_quantity.value=frm.quantity.value;
					else if(frm)
						frm.ser_quantity.value=1;	
					var id="";
					if(frm){
						for(i=0;i<attribute_array.length;i++){
							id="id_attrib["+attribute_array[i]+"]";
							frm_option=document.getElementById(id);
							if(frm_option) option_id+=frm_option.options[frm_option.selectedIndex].value+",";
						}
					}
					if(document.select_resource) document.select_resource.option_ids.value=option_id;  
					if(frm && book_type=='D' && frm.start_times)
						frm.start_times.value=frm.start_times.options[frm.start_times.selectedIndex].value;
					
					return true;
				} 

				function sel_services(boxname,id,index){ 
					var element=document.getElementById(boxname+id); 
					var field=document.select_period.elements[boxname+"_index"];  
					field.value=index;
					field=document.select_period.elements[boxname];
					if (field.value!=''){
						document.getElementById(boxname+field.value).style.backgroundColor="#FFFFFF"; 
					}
					field.value=id;  
					element.style.backgroundColor="#D8EDFB";
				}
				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
						  }
						  if (valueArr[icnt]==select_edate) optionElement.selectedIndex=optionElement.options.length-1;
					}
				}
				
				function add_start_times(){
					var select_cnt=0;
					if(book_type=='H' && document.select_period && document.select_period.start_date){
						 st_date=document.select_period.start_date.value; 
						 var tempDate=convert_to_sdate(st_date);
					}
					var i=0;
					var hour;
					var sel_ecnt=0;
					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) * 60;
					}
					while(i<=1440){
						if(i>1440) break;	
						slot=60/book_granular;
					   // 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(":");
							 splt_stime=nb_s_time.split(":");
							 splt_stime_mt=splt_stime[0] * 60;
							 splt_etime_mt=splt_etime[0] * 60;
							 if(n_sdate==tempDate){ 
							      if(splt_stime_mt<=i && i<=splt_etime_mt || splt_stime_mt==i) {	
							   	  i=splt_etime_mt;	       
							    }
						     }	 
						}
						// loop for no booking date and time	
						  var d_obj=new Date(1976,1,1,0,i);
						  	 if(i>=1440) break;		  
						  	 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") hour="";
						     	if(hour!=""){
						     		if(convert_to_stime(st_time)==convert_to_stime(hour)) select_cnt=cnt;
						     		arr_text[cnt]=convert_to_stime(hour);
						     		arr_value[cnt]=hour;
						     		cnt++;
						     	}
						      
						  i=i+(time_length*60);
						  if(i>1440) break;
					 }						 										  		
					 if(arr_text.length<=0 && arr_value.length<=0 && book_type=='H')					 						 						 	
					 	if(document.getElementById("start_time")) document.getElementById("start_time").style.display='none';					 						   
					 if(book_type=='H' && document.select_period && document.select_period.start_time && arr_text.length>0 && arr_value.length>0) {					 	
					 	if(document.select_period.start_time) document.select_period.start_time.style.display='';
					 	add_options(arr_text,arr_value,document.select_period.start_time);
					 }			 
					add_end_times();					 
					return select_cnt;
				}

				function add_end_times(){
				var arr_text=Array();
				var sel_ecnt=0;
				var arr_value=Array();
				//check if booking is day based or hours based
				var end_time='23:59:00';
				var send_time=convert_to_stime('00:00:00');
				if (book_type=="D"){
						if(document.select_period && document.select_period.start_times) var tempDate=getDatePart(document.select_period.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);
						   } 
						 	if(et_time==tempDate+" "+end_time)sel_ecnt=jcnt;
						 	tempDate_ed=dateAdd("d",1,tempDate);
							arr_value[jcnt]=tempDate+" "+end_time;
							arr_text[jcnt]=date_format(tempDate_ed,'Y-m-d','')+" "+send_time;
							jcnt++;
						}
					} else {
						var tempTime_s='0:0';
						var tempDate='1-1';
						if(document.select_period){
							tempTime_s=document.select_period.start_time.value;
							tempDate=document.select_period.start_date.value;
							var check_date=tempDate;
							var tempDateS=document.select_period.start_date.value;
						}
						var check_date=tempDateS;
						if (!tempTime_s && document.select_period) {
							document.select_period.end_times.style.display="none";
							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[0]=="24"){
									hour=12+time_length;
								}else if(tempTime_splt[1]=="00"){
									hour-=1;
								}	
														
							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,0);
							getday=((dt.getDate()<10)?"0"+dt.getDate():dt.getDate());
							month=dt.getMonth()+1;
							getmonth=((month<10)?"0"+month:month);
							tempDate=tempDateS=(dt.getFullYear()+"-"+getmonth+"-"+getday);
							tempTime_s=((dt.getHours()<10)?"0"+dt.getHours():dt.getHours()) + ":"+ dt.getMinutes();
							var display_dt=new Date(tempdate_splt[0],tempdate_splt[1]-1,tempdate_splt[2],hour,minute,(minute=="59")?60:1);
							display_tempDate=(display_dt.getFullYear()+"-"+(display_dt.getMonth()+1)+"-"+display_dt.getDate());
							display_tempTime_s=display_dt.getHours()+ ":" + display_dt.getMinutes();	
							if (tempTime_s>end_time || (span_days=='N' && check_date<tempDate)) break;
								if(et_time==tempDate+" "+tempTime_s)sel_ecnt=jcnt;
								arr_text[jcnt]=date_format(display_tempDate,'Y-m-d','')+" "+convert_to_stime(display_tempTime_s);
								arr_value[jcnt]=tempDate+" "+tempTime_s;
								elapsed+=time_length;
							if (elapsed>max_bookings){
								break;
							}
							jcnt++;	
						} 
					}
					if(arr_text.length>0 && document.select_period) {
						document.select_period.end_times.style.display='';
						add_options(arr_text,arr_value,document.select_period.end_times);	
					}
					return sel_ecnt;
				}
				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;
				}
			
			
			// REQUIRES: isDate()
	/*function dateAdd(p_Interval, p_Number, p_Date,disp){	
				if(isNaN(p_Number)){return false;}
				//convert date elements
				if (book_type=="H"){
					splt_hours=p_Date.split(" ");
					if(splt_hours[1]) splt_times=splt_hours[1].split(":");
					splt=splt_hours[0].split("-");
					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;
					if(disp) dt.getFullYear()+"-"+month+"-"+day;
					return dt.getFullYear()+"-"+month+"-"+day;
			}
	}*/

   function dateAdd(p_Interval, p_Number, p_Date,ret_type){	
				if(isNaN(p_Number)){return false;}
				//convert date elements
				if(!ret_type) ret_type=false;
				if (book_type=="H"){ 
					splt=new Array(1967,1,1);
					splt_hours=p_Date.split(" ");
					if(splt_hours[1]) splt_times=splt_hours[1].split(":");
					splt=splt_hours[0].split("-");  
					m=parseInt(splt[1],10);
					if (tempCheck==0) {
					  m--;
					  tempCheck=1;					  
					}
					var dt=new Date(parseInt(splt[0],10),parseInt(splt[1],10)-1,parseInt(splt[2],10),parseInt(splt_times[0],10),parseInt(splt_times[1],10));
				} else {
					splt_times=new Array(0,0,0);
					splt_hours=getDatePart(p_Date,'D');
					if(getDatePart(p_Date,'H')) splt_times=getDatePart(p_Date,'H').split(":");
					splt=getDatePart(p_Date,'D').split("-");
					var dt=new Date(parseInt(splt[0],10),parseInt(splt[1],10)-1,parseInt(splt[2],10),parseInt(splt_times[0],10),parseInt(splt_times[1],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 + "'";
					}
				}
			hour=dt.getHours();
			minute=dt.getMinutes();
			if (hour<10) hour="0"+hour;
			if (minute<10) minute="0"+minute;
			day=dt.getDate();
			month=dt.getMonth()+1;
			if (day<10) day="0"+day;
			if (month<10) month="0"+month;
			var ret="";	
			if (book_type=="H") {
				if(ret_type) ret=dt.getFullYear()+"-"+month+"-"+day+" "+hour+":"+minute;
				else ret=hour+":"+minute;
			}else {
				if(ret_type) ret=dt.getFullYear()+"-"+month+"-"+day+" "+hour+":"+minute;
				else ret=dt.getFullYear()+"-"+month+"-"+day;	
			}	
			  return ret;
			}
   
	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;
	}
</script>
		<?php  if ($error!="") {?>
				<tr>
					<td align="center" class="errorText"><?php echo $error;?></td>
				</tr>
			 <?php } ?> 
			 
				<tr>
					<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
				</tr>

				<tr>
					<td>
					<table cellpadding="2" cellspacing="0" width="100%" border="0">
						<?php  echo tep_draw_form("select_period",FILENAME_ADD_SERVICES,$page_params,'post','onSubmit="return ValidateForm();"');
						?>
						<tr class="attributes-odd" valign=middle height="20">
							<td class="dataTableContent" align=left><b><?php echo SERVICE_STEP2; ?></b></td>
						</tr>
						<tr class="attributes-even">
							<td>
							<table cellpadding="2" cellspacing="0" border="0">
								<tr>
									<td class="dataTableContent"> <?php echo  TEXT_START_TIME;?></td>
									<td class="dataTableContent"  valign="middle"  align="left">
										<?php 
										if ($service->booking_type=="H"){
											echo  tep_draw_pull_down_menu('start_date',$dates_array,$HTTP_POST_VARS['start_date'],'onChange="javascript:add_start_times();"'.',size=10'); 
											echo '&nbsp;' . tep_draw_pull_down_menu('start_time',$times_array,$HTTP_POST_VARS['start_time'],'onChange="javascript:add_end_times();"');
										} else {
											echo tep_draw_pull_down_menu('start_times',$dates_array,$HTTP_POST_VARS['start_times'], 'onChange="javascript:add_end_times();"'.',size=10'); 
											echo tep_draw_hidden_field('start_date_combo','');   
											echo tep_draw_hidden_field('start_time_combo','');  
										}?>
										</td> 
									</tr>
									<tr><td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '10', ''); ?></td></tr>
									<tr>
										<td class="dataTableContent"><?php echo  TEXT_END_TIME ; ?></td>
										<td>
										<?php 
										echo tep_draw_pull_down_menu('end_times',array(),'',''); 
										echo tep_draw_hidden_field('end_date_combo','');
										echo tep_draw_hidden_field('end_time_combo','');
										?> 
									</td>
								</tr>
								<script languages="javascript">
									var select_edate="<?php echo $select_edate;?>";
									var select_stime="<?php echo $HTTP_POST_VARS['start_time'];?>";
									var select_stimes="<?php echo $HTTP_POST_VARS['start_times'];?>"; 
									add_start_times();
									add_end_times();
									if(select_stime && document.select_period.start_time)document.select_period.start_time.selectedIndex=add_start_times();
									if(select_stimes && document.select_period.start_time)document.select_period.start_time.selectedIndex=add_start_times();
									if(document.select_period.end_times)document.select_period.end_times.selectedIndex=add_end_times();
								</script>
								<tr><td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '10', ''); ?></td></tr>
								<?php if($service->multiple_purchase=='Y' && $service->include_resource=='N') {?>
										<tr>
											<td  class="dataTableContent" valign="middle"><?php echo TEXT_HOW_MANY; ?></td>
											<td class="dataTableContent" valign="middle"><?php echo tep_draw_input_field('quantity',$sel_quantity,'size=10 maxlength=6'); ?></td>
										</tr>													
								<?php }	?>	
								<tr>
									<td colspan="2" width="750" class="dataTableContent" align="right"></td> 
								</tr> 
							</table>
						</td>
					</tr>
					<Tr><td colspan="2">	
						<table border="0" cellspacing="0" cellpadding="0" width="100%">
							<tr><td width="16%" id="div_dates"></td><td width="10%" align="left"><?php echo tep_image_submit('button_select.gif',IMAGE_BUTTON_SELECT);?></td></tr>
						</table>
					</td></Tr>	
					<input type="hidden" name="service_id" value="<?php echo $service_id;?>">
					<input type="hidden" name="navigation" value="resource" >
					<input type='hidden' name='step' value='3'> 
					<input type="hidden"  name="service_name" value="<?php echo $service_id;?>">
					<input type="hidden" name="ser_quantity">	
					</form>
				</table>
				</td>
			</tr>
			<?php 
			 if($navigation=='resource' && $error==''){
			 	echo tep_draw_form("select_resource",FILENAME_ADD_SERVICES,$page_params,'post','onSubmit="return ValidateForm();"'); 
			 ?>
			<tr class="attributes-odd" valign=middle height="20">
			<td class="dataTableContent" align=left><b><?php echo SERVICE_STEP3; ?></b></td>
			</tr>
			<tr id="div_resource">
   				<td width="70%" align="left">
					<table border="0" align="left" cellspacing="0" cellpadding="0" width="65%">
					<?php if($no_resource_display=="false"){ ?>
					<tr>
						<td colspan="4" class="pageHeading" nowrap="true"><?php echo HEADING_RESOURCE;?></td>
					</tr>
					<tr style="height:20" valign="bottom"><td></td></tr>
					<?PHP 
					   $cou=0;
					   if($resource_query!='' && tep_db_num_rows($resource_query)>0){
					   $no_resource=false;?>
					<tr class='pageheading'> 
						<td width="5%"></td>
						<td class="main" align="left" width="40%"><?php echo TEXT_NAME;?></td>
						<td class="main" align="left" width="10%"><?php echo TEXT_COSTS;?></td>
					</tr>
			<?php $bk_cnt=0;
			  while($resource=tep_db_fetch_array($resource_query)){	
				$reserved_resource="";
				$time_booking=tep_time_diff($start_date.' ' .$start_time,$end_date.' ' . $end_time);
			    $bookings=$time_booking['hours'];
			    $booking_type=$service->booking_type;
			    if($booking_type=='D'){
					$splt_stime=split(" ",$start_times);
					$start_date=$splt_stime[0];
					$start_time=$splt_stime[1];
					$splt_etime=split(" ",$end_times);
					$end_date=$splt_etime[0];
					$end_time=$splt_etime[1];
				}
				$reserved_resource=get_resource_ids((int)$service_id,$start_date,$end_date,$start_time,$end_time,$booking_type);
				$bookings=$time_booking['days'];
				$resource_id=$resource['resource_id']; 
				//check booking buffer
				$tep_date_time=strtotime($start_date.' '.$start_time);
				$buffer_string='#'.$start_date.' '.$start_time.'#'.$end_date.' '.$end_time;
				if($booking_buffer!='' && $booking_buffer_split[1]){ 
					$temp_date=date('Y-m-d',strtotime($start_date.' '.$start_time)-60);
					$temp_time=date('H:i',strtotime($start_date.' '.$start_time)-60);
					$booked_resource_ids=get_resource_ids($service_id,$temp_date,$temp_date,$temp_time,$temp_time,$booking_type,'dates',$resource_id);
					$res_ids="";
					$raw_date="";
					$sorted_date="";
					$raw_date=$booked_resource_ids;
					$sorted_date=rsort($raw_date);
					$temp_date_time=$start_date.' '.$start_time;
				if($raw_date[0]) {
					$buffer_string="";
					$tep_end_time_check=strtotime($raw_date[0])+60+($booking_buffer_split[1] * 60);
					if($tep_end_time_check) $ret=tep_time_diff($start_date.' '.$start_time,date('Y-m-d H:i:s',$tep_end_time_check));					
					if($ret['minutes']>0 && $ret['minutes']<=($booking_buffer_split[1] * 4)) $tep_date_time=$tep_end_time_check;
					if($bookings && $tep_date_time) {
						$tep_end_string=tep_dateadd($tep_date_time-60,'hr',$bookings);
						if($booking_type=='D') $tep_end_string=tep_dateadd($tep_date_time-60,'day',$bookings);
					} 	
					$buffer_string='#'.date('Y-m-d H:i',$tep_date_time).'#'.date('Y-m-d',$tep_end_string).' '.date('H:i',$tep_end_string);
					$reserved_resource=get_resource_ids($service_id,date('Y-m-d',$tep_date_time),date('Y-m-d',$tep_end_string),date('H:i',$tep_date_time),date('H:i',$tep_end_string),$booking_type,'',$resource_id);
							
				} 
			  }		
			 $resource_ids_found=false;
			 if($reserved_resource){
			  while(list($key,$value)=each($reserved_resource)){ 
				if($value==$resource['resource_id'])	$resource_ids_found=true;
			  }
			 }
			 if(!$resource_ids_found){
			  ?>
					 	<tr style="cursor:pointer;cursor:hand" class="RowEven" onMouseOver="rowOverEffect(this);" onMouseOut="rowOutEffect(this)" onClick="on_select(<?php echo $cou;?>);">
							<td class="main" align="left"><?php echo tep_draw_radio_field('resource[]',$resource['resource_id'].'-'.$resource['resource_costs'].$buffer_string);?></td>
							<td class="main" align="left"><?php echo $resource['resource_name'];?></td>
							<td class="main" align="left"><?php echo $currencies->display_price($resource['resource_costs'], tep_get_tax_rate($resource['resource_tax_class_id']));?></td>
						</tr>
						<tr>
							<td></td>
							<td class="main" align="left"><?php echo $resource["resource_description"];?></td>
							<td class="main"></td> 
						</tr>	
			<?php $bk_cnt+=1;
				} 
				 $cou++;
				}
				if(!$bk_cnt) {
				  echo '<script language=javascript>
				          if(document.getElementById("div_resource")) document.getElementById("div_resource").innerHTML="<br><span class=main>'.TEXT_RES_SKIPPED.'</span>";  
				       </script>';
				   $no_resource=true;	
				}
			  }else {
				   $no_resource=true;
			   }
			 }else {?>
					<tr style="10"><td>&nbsp;</td></tr>
					<tr style="20%" >
						<td valign="bottom" class="smalltext"><?php echo TEXT_SKIPPED;?></td>
					</tr>				
					<tr style="10"><td>&nbsp;</td></tr>
		 <?php }
				?>
				 </table>
				</td>
			</tr>
			<tr class="attributes-odd" valign=middle height="20">
				<td class="dataTableContent" align=left><b><?php echo SERVICE_STEP4; ?></b></td>
			</tr>
			<tr> 
				<td colspan="3" align="left"> 
					<table border="0" cellpadding="0" cellspacing="0" width="89%">
					<?php if($no_attributes_display=="false"){?>
						<tr>
							<td colspan="3" class="pageHeading" align="left"><?php echo TEXT_HEADING_ATTRIBUTE;?></td>
						</tr>
						<tr style="height:20" valign="bottom"><td></td></tr>
						<?php 
							if($rows>0){ ?> 
						<tr>
							<td class="main" align="left" width="20%"><?php echo TEXT_ATTRIBUTE_NAME;?></td>
							<td class="main" align="left" width="70%"><?php echo TEXT_ATTRIBUTE_VALUE;?></td>
						</tr>
				<?php while ($services_options_name_values = tep_db_fetch_array($result_attributes)) {
						$selected = 0;
						$services_options_array = array();?> 
					   <tr style="height:40" valign="middle">
								<td class="main" nowrap="nowrap"><?php echo $services_options_name_values['services_options_name'];?>: </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 = '" . $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");
							 		$services_options_array[0]['text'] = TEXT_SERVICE_NOT_REQUIRED;
									while ($services_options_values = tep_db_fetch_array($services_options)) {
										$services_options_array[] = array('id' => $services_options_name_values['services_options_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 
									$id.= $services_options_name_values['services_options_id'].'-';
									$id1='id_attrib[' . $services_options_name_values['services_options_id'] . ']';
									?> 
						<td>
								<?php echo tep_draw_pull_down_menu($id1, $services_options_array, (($option_id>0)?$option_id:$cart->contents[$HTTP_GET_VARS['service_id']]['attributes'][$services_options_name_values['services_options_id']])) ;?>
						</td>		
				<?php  	} 
					  }
				}else { ?>
					<tr style="10"><td>&nbsp;</td></tr>
					<tr style="20%" >
						<td valign="bottom" class="smalltext"><?php echo TEXT_SKIPPED;?></td>
					</tr>				
					<tr style="10"><td>&nbsp;</td></tr>
			<?php } ?>
				<Tr>
					<td colspan="3">
					  <table border="0" cellspacing="0" cellpadding="0" width="76%">
					  	<tr>
					  		<td width="30%" class="main" align="right"><?php echo ((!$no_resource || $service->include_resource=='N')?tep_image_submit('button_select.gif',IMAGE_BUTTON_SELECT):'');?></td>
					  	</tr>
					  </table>
				</td></Tr>
				<input type="hidden" name="ser_quantity" value="<?php echo $HTTP_POST_VARS['ser_quantity'];?>">
				<input type="hidden" name="resource_ids">
				<input type="hidden" name="resource_costs"> 
				<input type="hidden" name="option_ids"> 
				<input type='hidden' name='step' value='5'>
				<?php  
					$start_date=isset($HTTP_POST_VARS['start_date'])?$HTTP_POST_VARS['start_date']:''; 
					$start_time=isset($HTTP_POST_VARS['start_time'])?$HTTP_POST_VARS['start_time']:'';
					$start_times=isset($HTTP_POST_VARS['start_times'])?$HTTP_POST_VARS['start_times']:'';
					$end_times=isset($HTTP_POST_VARS['end_times'])?$HTTP_POST_VARS['end_times']:'';
					$splt_date=split(" ",$end_times);
					$end_date=$splt_date[0];
					$end_time=$splt_date[1]; 
					$service=get_services_info((int)$service_id,(int)$languages_id);
					if($service['booking_type']=='D'){
						$splt_stime=split(" ",$start_times);
						$start_date=$splt_stime[0];
						$start_time=$splt_stime[1];
					}
				?>
				<input type="hidden" name="start_date" value="<?php echo $start_date;?>">	
				<input type="hidden" name="start_time" value="<?php echo $start_time;?>">
				<input type="hidden" name="end_date" value="<?php echo $end_date;?>">	
				<input type="hidden" name="end_time" value="<?php echo $end_time;?>">	
				<input type="hidden" name="start_times" value="<?php echo $start_times; ?>">
				<input type="hidden" name="end_times" value="<?php echo $end_times; ?>">
				<input type="hidden" name="service_name" value="<?php echo $service_id;?>">
				<?php echo '</form>'; ?> 
					<script language="javascript">on_select(0);</script>
				  </table>
				</td>
			</tr>		
		<?php	}
			  } ?> 
			       </table>
			     </td>
			   </tr>
			</table>
		<!-- body_eof //--> 
		</tr>
	</table>
<!-- body_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
<?php

function customServiceListBox($name,&$list,$selected,$width,$height){
	$selIndex=0;
	
	echo '<div id="' . $name . '_box" style="border-color: #7b9ebd; border-style: solid; border-width: 1px;overflow:auto;width:' . $width . ';height:' . $height . '">' . "\n";
	echo '<table border="0" cellspacing="0" cellpadding="2" width="100%" valign="middle" align="center" class="BoxTable">' . "\n";
	for ($icnt=0;$icnt<sizeof($list);$icnt++){
		if ($list[$icnt]['mode']=='V') {
			if ($selected==0) $selected=$list[$icnt]['id'];
			echo  '<tr class="attributes-even"><td nowrap bgcolor="' . ($selected==$list[$icnt]['id']?"#D8EDFB":"#FFFFFF") . '" valign="middle" width="100%" id="' . $name . $list[$icnt]['id'] . '" onClick="javascript:selectServiceItem(\'' . $name . '\',\'' . $list[$icnt]['id'] .  '\',' . $icnt .')" class="BoxTd">' . "\n";
			echo  $list[$icnt]['text'] .'</td></tr>' ."\n";
			if ($selected==$list[$icnt]['id']) $selIndex=$icnt;
		} else {
			echo  '<tr class="attributes-even"><td nowrap bgcolor="#FFFFFF" valign="middle" width="100%" class="BoxTd">' . "\n";
			echo  $list[$icnt]['text'] .'</td></tr>' ."\n";
		}
	}
	echo '</table></div>' . "\n";
	echo '<input type="hidden" name="' . $name . '" value="' . $selected . '">';
	echo '<input type="hidden" name="' . $name . '_index" value="' . $selIndex . '">';
}


function customListBox($name,&$list,$selected,$width,$height){
	$selIndex=0;
	
	echo '<div id="' . $name . '_box" style="border-color: #7b9ebd; border-style: solid; border-width: 1px;overflow:auto;width:' . $width . ';height:' . $height . '">' . "\n";
	echo '<table border="0" cellspacing="0" cellpadding="2" width="100%" valign="middle" align="center" class="BoxTable">' . "\n";
	for ($icnt=0;$icnt<sizeof($list);$icnt++){
		 	echo  '<tr class="attributes-even" onClick="javascript:sel_services(\'' . $name . '\',\'' . $list[$icnt]['id'] .  '\',' . $icnt .')"><td nowrap bgcolor="' . ($selected==$list[$icnt]['id']?"#D8EDFB":"#FFFFFF") . '" valign="middle" width="100%" id="' . $name . $list[$icnt]['id'] . '" class="BoxTd">' . "\n";
			echo  $list[$icnt]['text'] .'</td></tr>' ."\n";
			if ($selected==$list[$icnt]['id']) $selIndex=$icnt;
	}
	echo '</table></div>' . "\n";
	echo '<input type="hidden" name="' . $name . '" value="' . $selected . '">';
	echo '<input type="hidden" name="' . $name . '_index" value="' . $selIndex . '">';
}


  function tep_get_country_id($country_name) {

    $country_id_query = tep_db_query("select * from " . TABLE_COUNTRIES . " where countries_name = '" . $country_name . "'");

    if (!tep_db_num_rows($country_id_query)) {
      return 0;
    }
    else {
      $country_id_row = tep_db_fetch_array($country_id_query);
      return $country_id_row['countries_id'];
    }
  }
  
  
  function tep_get_zone_id($country_id, $zone_name) {

    $zone_id_query = tep_db_query("select * from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country_id . "' and zone_name = '" . $zone_name . "'");

    if(!tep_db_num_rows($zone_id_query)) {
      return 0;
    } else {
      $zone_id_row = tep_db_fetch_array($zone_id_query);
      return $zone_id_row['zone_id'];
    }
  }
  function found_service(){
	global $service_id,$start_date,$end_date,$start_time,$end_time,$service,$languages_id,$resource_id;
	$error="";
	if(!tep_check_service_found($service_id,$start_date,$end_date,$start_time,$end_time) && $service['include_resource']=='N'){
		$error=ERR_TIME_ALREADY_RESERVED;
    }
	if($service['include_resource']=='Y' || $error) {
			//echo $start_date.'-'.$start_time.'-'.$end_date.'-'.$end_time;
			$qty=get_orders_products_quantity($service_id,$start_date,$end_date,$start_time,$end_time,$booking_type,$resource_id);
			if($qty>0) $error=ERR_TIME_ALREADY_RESERVED;
	}	
	return $error;	
  }
?>
