<?php 

// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();
?>
<table align="left" border="0" width="100%" cellspacing="0" cellpadding="<?php echo CELLPADDING_SUB; ?>">
	<tr><td width="100%" valign="top">
		<table cellpadding="0" cellspacing="0" width="100%">
	<?php 
		if(!isset($hide_row_top)) { ?>
 		   <tr>
     		   <td colspan="2" valign="top"><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
	      </tr>
	<?php	} ?>	  
	  <?php  	  
	$gparam="";
	if($HTTP_GET_VARS['view']=='month')
		$gparam='&view=month';
	$header_text=HEADING_TITLE;
	if($category_id>0) $header_text=tep_get_services_name($service_id,(int)$languages_id) . TEXT_RESOURCE_DETAILS;	
    echo tep_draw_form("services_resource",tep_href_link(FILENAME_SERVICES_ATTRIBUTES, 'service_id=' . (int)$service_id .$gparam),'post','onsubmit="javascript:return validate();"');?>	
	<script language="javascript" type="text/javascript">
		var baby=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>";
				baby[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="<?php echo TEMPLATE_STYLE;?>"></head>');
					handle.document.write('<body class="conditions">');
					handle.document.write(baby[1]);
					handle.document.write('</body></html>');
					handle.document.close();		
				}
	</script>
<?php if (MAIN_TABLE_BORDER == 'yes'){
	table_image_border_top(true, false, $header_text);
	}
  if($service['booking_type']=='H'){
 	$start_time_session='-'.$start_time_session;
	$end_time_session='-'.$end_time_session;
  }else if($service['booking_type']=='D'){
	$start_time_session="";
	$end_time_session="";	 
  }		   
  if($resource_query && tep_db_num_rows($resource_query)>0 && !$error){
	 $no_resource=false;
 	?>	
 	<tr style="height:10"><td></td></tr>
	<tr>
	  <td colspan="2" valign="top">
		<table border="0" cellspacing="0" cellpadding="0" width="100%" >
		  <tr>	
		    <td valign="top" class="main" align="left" width="75%">
				<table border="0" cellspacing="0" cellpadding="2" width="100%">
					<tr id="resource_header" align="center" >
						<td colspan="3">
							<table border="0" cellspacing="0" cellpadding="2" width="100%">
								<tr class="headerCalendar">
									<td width="5%"></td>
									<td class="main" align="left" width="80%"><?php echo TEXT_RESOURCE_NAME;?></td>
									<td class="main" align="left" width="10%"><?php echo TEXT_COSTS;?></td>
								</tr>
							</table>
						</td>
					</tr>
		<?php $count=0;
		$class="moduleRowOver";
		$cnt=0;
		$bk_cnt=0;
		while($resource=tep_db_fetch_array($resource_query)){
		if($class=="moduleRowOver"){ 
			$class="moduleRow";
		}else { 
			$class="moduleRowOver";
		} 	
		$service_start_date=$services->start_date;
		$service_end_date=$services->end_date;
		$service_start_time=$services->start_time;
		$service_end_time=$services->end_time;
		$costs=$currencies->display_price($resource['resource_costs'], tep_get_tax_rate($resource['resource_tax_class_id']));
		$service_costs=$resource['resource_costs'];
		$service_tax_class_id=$resource['resource_tax_class_id'];
		if($service['include_resource']=='Y' && $service['include_resource_price']=='N' || $service['include_resource']=='N'){
			$costs=$currencies->display_price($service['service_costs'], tep_get_tax_rate($service['service_tax_class_id']));
			$service_costs=$service['service_costs'];
			$service_tax_class_id=$service['service_tax_class_id'];
		}		
		//get loading price		
		$loading_price_dates=get_loading_array($service_id);
		$temp_service_costs=$service_costs;
		$loading_price=tep_loading_price($loading_price_dates,$services->start_date,$services->end_date,$services->start_time,$services->end_time,$service["booking_type"],$temp_service_costs);
		if($loading_price<0) $loading_price=0;
		if($loading_price)	$temp_service_costs=$loading_price + $temp_service_costs;
		$costs=$currencies->format(tep_add_tax($temp_service_costs,tep_get_tax_rate($service_tax_class_id)));
		//get loading price		
		$resource_price_array[]=array('id'=>$resource['resource_id'],'costs'=>tep_add_tax($service_costs,tep_get_tax_rate($service_tax_class_id)));
		$buffer_string='#'.$service_start_date.' '.$service_start_time.'#'.$service_end_date.' '.$service_end_time;
		$reserved_resource="";
		$reserved_resource=get_resource_ids($service_id,$service_start_date,$service_end_date,date('H:i',@strtotime($service_start_time)),date('H:i',@strtotime($service_end_time)),'',$resource['resource_id']);		
		//check booking buffer
		$tep_date_time=@strtotime($service_start_date.' '.$service_start_time);
			if($booking_buffer!='' && $booking_buffer_split[1]){ 
				$temp_date=date('Y-m-d',@strtotime($service_start_date.' '.$service_start_time)-60);
				$temp_time=date('H:i',@strtotime($service_start_date.' '.$service_start_time)-60);				
				$booked_resource_ids=get_resource_ids($service_id,$temp_date,$temp_date,$temp_time,$temp_time,'dates',$resource['resource_id']);
				$res_ids="";
				$raw_date="";
				$sorted_date="";
				$raw_date=$booked_resource_ids;
				$sorted_date=rsort($raw_date);
				$temp_date_time=$service_start_date.' '.$service_start_time;
				//while(list(,$val)=each($booked_resource_ids)){
					if($raw_date[0]) {
						$buffer_string="";
						$tep_end_time_check=tep_dateadd(@strtotime($raw_date[0]),'min',$booking_buffer_split[1])+60;
						if($tep_end_time_check) $ret=tep_time_diff($service_start_date.' '.$service_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($ret['minutes'] && $tep_date_time) {
							$tep_end_string=tep_dateadd($tep_date_time-60,'hr',$services->bookings);
							if($service['booking_type']=='M')$tep_end_string=tep_dateadd($tep_date_time-60,'min',$services->bookings);
							else if($service['booking_type']=='D') $tep_end_string=tep_dateadd($tep_date_time-60,'day',$services->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),'',$resource['resource_id']); 		
					}
				//}	
			}
		//check booking buffer
		$resource_array='resource_id-' . $resource['resource_id'] .'-' . tep_add_tax($service_costs,tep_get_tax_rate($service_tax_class_id)) .'-'.$service_tax_class_id.$buffer_string;
		$cnt++;
		$ids='<div id="s_resource_id" name="s_resource_id"></div>';
		$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 id="defaultSelected" class="<?php echo $class;?>" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" style="cursor:pointer;cursor:hand" onclick="javascript:do_radio('<?php echo $count;?>',this);">
						<td align="left" valign="top" width="5%"><?php echo tep_draw_radio_field('resource_name[]',$resource_array);?></td>
						<td class="main" align="left" valign="top" width="80%"><?php echo  '<b>'.$resource['resource_name'] . '</b>';?></td>
						<td class="main" align="right" valign="top" width="10%"><?php echo $costs;?></td>				
					</tr>
		
					<tr>
						<td ></td>
						<td class="main"  valign="top" align="left" colspan="2"><?php echo $resource['resource_description'];?></td>
					</tr>
	<?php  $bk_cnt+=1;
	      }
				$count++;
		}
 			if(!$bk_cnt) $error="No Resource Found";
			if(!$error){ 				
			?>
				</table>
			</td>
			<td width="25%" valign="top" align="right">
				<table border="0" cellspacing="0" cellpadding="0" width="100%">
					<tr>
						<td width="100%" align="right" valign="top">
						<?php include(CHECK_CON . $freeway_path_after_joomla.'service_board.php')?>
						</td>	
					</tr>
				</table>
			</td>
		 </tr>	
		</table>
		</td>
	</tr> 	
	<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="infoBoxHeadingPage">' . TEXT_TERMS . '</a></b>';?>
	</tr>
	<tr>
		<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
	</tr>
	<?php
	  }
	} else {	
		$no_resource=true;?>
	<tr style="height:20"><td></td></tr>
	<tr><td colspan="2" align="center" class="smalltext"><?php echo (($error)?$error:'');?></td></tr>
	<?php }	?>
	<tr>
		<td align="right" colspan="2">	
			<table border="0" cellspacing="0" width="100%">
				<tr>
					<td class="main" width="50%" align="left"><?php echo '<a href="'.tep_href_link(FILENAME_SERVICES_INFO,'service_id=' . $HTTP_GET_VARS['service_id'] . '&action=edit'.$gparam).'">' . tep_template_image_button('button_back.gif',BUTTON_IMAGE_BACK) . '</a>';?></td>
					<td class="main" width="50%" align="right"><?php if(!$no_resource &&  !$error) echo tep_template_image_submit('button_continue.gif', IMAGE_BUTTON_CONTINUE); else echo '&nbsp;';?></td>	
				</tr>
			</table>
		</td>
	</tr>
	<input type="hidden" name="service_id" value="<?php echo $service->service_id;?>" />
	<input type="hidden" name="products_qty" value="<?php echo $service->qty;?>" />
	<input type="hidden" name="start_time" value="<?php echo $service->start_time;?>" />
	<input type="hidden" name="end_time" value="<?php echo $service->end_time;?>" />
	<input type="hidden" name="start_date" value="<?php echo $service->start_date;?>" />
	<input type="hidden" name="end_date" value="<?php echo $service->end_date;?>" />
	<input type="hidden" name="booking_type" value="<?php echo $service->booking_type;?>">
	<input type="hidden" name="temp_start_time" value="<?php echo tep_output_string($HTTP_POST_VARS['start_time']);?>" />
	<input type="hidden" name="temp_end_time" value="<?php echo tep_output_string($HTTP_POST_VARS['end_time']);?>" />
	<input type="hidden" name="temp_start_date" value="<?php echo tep_output_string($HTTP_POST_VARS['end_date']);?>" />
	<input type="hidden" name="temp_end_date" value="<?php echo tep_output_string($HTTP_POST_VARS['end_date']);?>" />
    <input type="hidden" name="resource_ids">
    <input type="hidden" name="resource_costs"> 
	<input type="hidden" name="tax_class_id" />
</form>
<?php if (MAIN_TABLE_BORDER == 'yes'){
	table_image_border_bottom();
	}?>
 </table></td></tr>
</table>
</td>
</td></tr>
</table>
</tr>