<?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');
 tep_get_last_access_file();
 require(DIR_WS_CLASSES . 'currencies.php');
 require(DIR_WS_CLASSES . 'split_page_results_event.php');
 //added new
  define(BOX_WIDTH1,'125');
  $currencies = new currencies();
  $action = (isset($HTTP_GET_VARS['action']) ? tep_db_prepare_input($HTTP_GET_VARS['action']) : '');
  $return = (isset($HTTP_GET_VARS['return']) ? tep_db_prepare_input($HTTP_GET_VARS['return']) : '');
  if ($action=="") $action = (isset($HTTP_GET_VARS['firstaction']) ? tep_db_prepare_input($HTTP_GET_VARS['firstaction']) : '');  
  $orders_id=(isset($HTTP_GET_VARS['orders_id'])?(int)$HTTP_GET_VARS['orders_id']:0);
  $mode=(isset($HTTP_GET_VARS['mode'])?(int)$HTTP_GET_VARS['mode']:0);
  $error=(isset($HTTP_GET_VARS['error'])? tep_db_prepare_input($HTTP_GET_VARS['error']):'');
  $page=(isset($HTTP_GET_VARS['page'])?(int)$HTTP_GET_VARS['page']:1);
  if($action!=""){
  	if ($action=="search")
	{
		if (isset($HTTP_GET_VARS['page']) && ((int)$HTTP_GET_VARS['page'] > 1)) $rows = $HTTP_GET_VARS['page'] * MAX_DISPLAY_SEARCH_RESULTS - MAX_DISPLAY_SEARCH_RESULTS;
		$rows=0;
		$search_params=array("tx_fname"=>'',"tx_lname"=>'',"page"=>1,"tx_words"=>'',"tx_code"=>'',"tx_from"=>'',"tx_until"=>'',"tx_service"=>'',"tx_resource"=>'',"orders_id"=>'',"service_id"=>'',"tx_pending"=>'');
		$temp_query=build_search_query();
		$arg="";
		if($HTTP_POST_VARS || $HTTP_GET_VARS){
			if($HTTP_POST_VARS) $get_post_vars=&$HTTP_POST_VARS;
			else if($HTTP_GET_VARS) $get_post_vars=&$HTTP_GET_VARS;
			$arg='&tx_code='.$get_post_vars['tx_code'].'&tx_fname='.$get_post_vars['tx_fname'].'&tx_from='.$get_post_vars['tx_from'].'&tx_lname='.$get_post_vars['tx_lname'].'&tx_pending='.$get_post_vars['tx_pending'].'&tx_resource='.$get_post_vars['tx_resource'].'&tx_service='.$get_post_vars['tx_service'].'&tx_until='.$get_post_vars['tx_until'];
		}
		if ($orders_id>0){
			$fields=",os.start_date,os.start_date as end_date ";
			$query=" and o.orders_id='" . $orders_id . "' and se.service_id='" . (int)$HTTP_GET_VARS['service_id'] ."' order by os.start_date";
		} else {
			$fields=",os.start_date as start_date,count(os.start_date) as total_count,os.end_date as end_date ";
			$query=$temp_query . ' group by o.orders_id,op.orders_products_id,se.service_id order by os.orders_id,os.start_date';
		}
		
		if ($search_params["tx_pending"]=="Y") {
			$cDate=getServerDate();
			$pending_option=" and ((op.orders_products_status>1 and op.orders_products_status<5)  or (op.orders_products_status=1 and (se.pending_period=0 or date_add(o.date_purchased,interval se.pending_period day)>='" . $cDate . "')))";
		}
		$search_query=	"select o.orders_id,op.orders_products_id,op.products_id,o.customers_id,os.resource_id,c.customers_firstname,c.customers_lastname,c.customers_email_address,o.customers_postcode,se.service_location,os.resource_name,sd.service_name,se.service_id,os.start_time,os.end_time,op.products_price,op.products_tax,op.products_quantity " . $fields . " from " . 
						TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_CUSTOMERS . " c, " .  TABLE_ORDERS_SERVICES . " os, " . TABLE_SERVICES . " se, " . TABLE_SERVICES_DESCRIPTION . " sd " . 
						" where op.products_type='V' and op.orders_id=os.orders_id and op.orders_products_id=os.orders_products_id " .
						" and  op.orders_id=o.orders_id and c.customers_id=o.customers_id and op.products_id=os.products_id and os.products_id=se.service_id and sd.service_id=se.service_id and sd.language_id='" . (int)$languages_id . "'" . $pending_option . $query;
															
		if ($orders_id<=0)
		$bookings_split = new splitPageResultsEvent($HTTP_GET_VARS['page'], MAX_DISPLAY_SEARCH_RESULTS, $search_query, $search_query_numrows);

		$result_query=tep_db_query($search_query);
		
		$cnt=tep_db_num_rows($result_query);
		
		$action="search";			
	echo '<table width="100%" border="0" cellpacing="0" cellpadding="2">';
	if ($error!='') { ?>						
			<tr>
				<td>
				<table width=100% border=0 cellpacing=0 cellpadding=2>
					<tr>
						<td class=main><?php echo '<font color=red>' . $error . '</font>'; ?></td>
					</tr>
				</table>
				</td>
			</tr>
		<?php } ?>
		<tr class="openContent_top">
			<td>
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td align=left class='pageHeading' colspan=8><?php echo HEADING_RESULT_TITLE; ?></td>
				</tr>
			</table>
			</td>
		</tr>
		<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
		<?php if ($cnt>0) {?>
			<tr>
				<td>
				<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td class="main" valign="middle" align="left">
						<?php echo tep_draw_checkbox_field("select_all","1",false,'','onClick="javascript:select_all_boxes(this);"') . TEXT_SELECT_ALL;	?>
					</td>
					<td align=right valign="middle" class="print_image">
					<?php echo '<a href="' . tep_href_link(FILENAME_SERVICE_BOOKING_PRINT,tep_get_all_get_params()) . '" onmouseover="change_image(1)" onmouseout="change_image(0)" target="_blank">' . '<img border=0 src="' . DIR_WS_IMAGES . "printimage.gif" . '" alt="' . IMAGE_PRINT_BOOKINGS . '" title="' . IMAGE_PRINT_BOOKINGS . '" id="print_image">' . TEXT_PRINT . '</a></span>';?>
					</td>
				</tr>
				</table>
				</td>
			</tr>		
			<tr>
				<td>
				<table border="0" width="100%" cellspacing="0" cellpadding="2">
					<tr>
						<td class="smallText" valign="top"><?php echo $bookings_split->display_count($search_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $HTTP_GET_VARS['page'], TEXT_DISPLAY_NUMBER_OF_BOOKINGS); ?></td>
						<td class="smallText" align="right"><?php echo $bookings_split->display_script_links($search_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $HTTP_GET_VARS['page'],$arg,'doactions(\'search\'','1');?>&nbsp;</td>
					</tr>
				</table>
				</td>
			</tr>		
		<tr>
			<td valign="top">
			 <?php echo tep_draw_form('result_booking',FILENAME_MOVE_BOOKINGS_CONFIRM,'','post','onSubmit="javascript:return validateForm()"');?>
			    <table border="0" width='100%'cellspacing="1" cellpadding="2">
				 <tr class='dataTableHeadingRow'><td class='dataTableHeadingContent'><?php echo TEXT_RESULT_OPTION; ?>&nbsp;</td>
					<td width="10"></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_FIRSTNAME; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_LASTNAME; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_SERVICE; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_RESOURCE; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_STARTDATE ; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_ENDDATE ; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_AMOUNT; ?></td>
				</tr>
				<?php 					
					reset($search_params);
					while(list($key,)=each($search_params)){
						echo '<input type="hidden" name="' . $key . '" value="' . tep_output_string($search_params[$key]) . '">';
					}
					//echo '<input type="hidden" name="process_type" value="">';
					$rows=0;$currow=0;
					while($result=tep_db_fetch_array($result_query)) { 
						$result['total_count']=(tep_not_null($result['total_count'])?$result['total_count']:1);
						$rows++;
						$currow++;
						if (strlen($rows) < 2)  $rows = '0' . $rows;						
				?>
				<tr class='dataTableRow' height="20">
					<td class='dataTableContent'>
						<?php  
						if ($result['total_count']==1){
							echo tep_draw_checkbox_field('booking_id[]',$result['orders_products_id'],false);
							echo '<input type="hidden" name="items_type[' .$result['orders_products_id'] . ']" value="S">';
						} else {
							echo tep_draw_checkbox_field('booking_id[]',$result['orders_id'] . '_' . $result['service_id'],false);
							echo '<input type="hidden" name="items_type[' . $result['orders_id'] . '_' . $result['service_id'] . ']" value="G">';
						}
						echo tep_draw_hidden_field('email_address',$result['customers_email_address']);
						echo tep_draw_hidden_field('letters_id',$result["orders_id"]);
						if(defined('TIME_FORMAT')) $time_format=TIME_FORMAT;
						$start_times=date('h:i A',strtotime($result['start_time']));
						$end_time_string=strtotime($result['end_date'].' '.$result['end_time']);
						$end_date=date('Y-m-d',$end_time_string);
						$end_times=date('h:i A',$end_time_string);
						if($time_format==24){
							$start_times=date('H:i',strtotime($result['start_time']));
							$end_times=date('H:i',$end_time_string);
						}
						?>
					</td>
					<td class='dataTableContent'><?php echo '<span onClick="javascript:selectBox(' . $currow  . ',\'\',6);" class="cursorhand">' .  tep_image(DIR_WS_ICONS . '/mail.gif',IMAGE_MAIL_BOOKING) . '</span>';?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo $result['customers_firstname']; ?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo $result['customers_lastname']; ?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo $result['service_name']; ?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo (($result['resource_id']>0)?$result['resource_name']:$result['products_quantity']);?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo format_date($result['start_date']) . ' '.$start_times; ?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo format_date($end_date) . ' '.$end_times; ?></td>
					<?php // get amount for this service
						  $product_id=(int)$result['products_id'];
						  $orders_id=(int)$result['orders_id'];
						  $orders_products_id=(int)$result['orders_products_id'];
						  $service=get_services_info((int)$product_id,(int)$languages_id);
						  $product_price=get_amount_for_orders($orders_id,$orders_products_id,$product_id); // get amount for this service
					?>
					<td class='dataTableContent' style="cursor:hand" align="right"><?php echo $currencies->format($product_price); ?></td>
			</tr>
			<?php } ?>
       </table>
       </form>
	   </td>
	   </tr>
	   <?php } else {?>
			<tr><td align=left class='main' colspan="8"><?php echo TEXT_NO_RESULTS ?></td></tr>
	   <?php } ?>
	   <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
	   <tr><td align=left>
			<table border="0" width="100%" cellspacing="0" cellpadding="2">
				<tr valign=middle>
					<td align=left class="main" valign="middle">
					<?php 
						echo tep_draw_hidden_field('process','');
						if ($orders_id<=0) {
							if ($page>1)
								echo '<a href="javascript:doactions(\'back_search_form\');">' . tep_image_button('button_back.gif', IMAGE_BACK,'align=absmiddle') . '</a>&nbsp;&nbsp;';
							else
								echo '<a href="javascript:doactions(\'back_search_form\');">' . tep_image_button('button_back.gif', IMAGE_BACK,'align=absmiddle') . '</a>&nbsp;&nbsp;';
						} else
								echo '<a href="javascript:doactions(\'back_search_form\');">' . tep_image_button('button_back.gif',IMAGE_BACK,'align=absmiddle') . '</a>&nbsp;&nbsp;';
					?>
                     <td align=right valign="middle">
					<?php 		
						if ($cnt>0) {
							
							$service_array=tep_get_services_array();
							if (sizeof($service_array)>0) echo tep_image_button('button_move_to.gif', IMAGE_MOVE, " style='cursor:hand;cursor:pointer;' onclick='javascript:validateForm();' id='but' align='absmiddle'") . '&nbsp;';

							echo tep_draw_form('admin_letter',FILENAME_ADMIN_LETTERS,'','post','onSubmit="javascript:return validateLetterForm();"');
							echo tep_draw_hidden_field('search_ids','');
							echo tep_image_submit('button_letter.gif',IMAGE_ADMIN_LETTERS,"align='absmiddle'") .'&nbsp;';
							echo '</form>';
							echo tep_draw_form('admin_email',FILENAME_SEND_SERVICES_EMAIL,'action=new_message&mode=1','post','onSubmit="javascript:return validateEmailForm(1);"');
							echo tep_draw_hidden_field('search_ids','');
							echo tep_image_submit('button_email.gif',IMAGE_SEARCH_EMAIL,"align='absmiddle'") . '&nbsp;';
							reset($search_params);
							while(list($key,)=each($search_params)){
								echo '<input type="hidden" name="' . $key . '" value="' . tep_output_string($search_params[$key]) . '">';
							}
							echo '</form>';
							echo tep_draw_form('admin_sms',FILENAME_SEND_SERVICE_SMS_EMAIL,'action=new_message&mode=2','post','onSubmit="javascript:return validateEmailForm(2);"');
							echo tep_draw_hidden_field('search_ids','');
							echo tep_image_submit('button_sms.gif',IMAGE_SEARCH_EMAIL,"align='absmiddle'");
							reset($search_params);
							while(list($key,)=each($search_params)){
								echo '<input type="hidden" name="' . $key . '" value="' . tep_output_string($search_params[$key]) . '">';
							}
							echo '</form>';
				     	}
					?>
					</td>
				</tr>
			</table>
		</td>
		</tr>
		<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
	</table>	
	<?php 
	}else if($action=='back_search_form')
		search_form();	
  
  exit;
  }  
	// if action is perform waitlist or reserve moving operation
	if ($action=='setflag'){
		$flag=(isset($HTTP_GET_VARS['flag'])?tep_db_prepare_input($HTTP_GET_VARS['flag']):'');
		$products_id=(isset($HTTP_GET_VARS['pID'])?(int)$HTTP_GET_VARS['pID']:0);
		$service_id=(isset($HTTP_GET_VARS['sID'])?(int)$HTTP_GET_VARS['sID']:0);
		$orders_products_id=(isset($HTTP_GET_VARS['opID'])?(int)$HTTP_GET_VARS['opID']:0);
		$error='';
		tep_redirect(tep_href_link(FILENAME_SERVICES_SEARCH_BOOKINGS,tep_get_all_get_params(array('action','opID','sID','pID','x','y','flag')) . 'action=search&error=' . $error));
	}	
?>
<!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">
<style type="text/css">
	#pup {position:absolute; visibility:hidden; z-index:200; width:130; }
</style>
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="JavaScript" src="includes/date-picker.js"></script>
<script language="JavaScript" src="includes/http.js"></script>
<script language=javascript>
		var command="";		
		function do_submit(type){
			var frm=document.move_booking_confirm;
			var loading=document.getElementById("loading");
			if(type=='sel_service') type="";
			frm.service_id.value=frm.nservice.value;
				if(type=='select')
					frm.select_action.value="true";
				else if(type=='move')
					frm.select_action.value="move";
			command="<?PHP echo tep_href_link(FILENAME_MOVE_BOOKINGS_CONFIRM);?>?";
			if(command!="") {
				if(loading) loading.style.display=""; 
				do_post_command('move_booking_confirm',command);
			}			
			return;
		}
		function change_image(mode){
			if (mode==1)
				document.all.print_image.src="<?php echo DIR_WS_IMAGES . "printimage_over.gif";?>";
			else
				document.all.print_image.src="<?php echo DIR_WS_IMAGES . "printimage.gif";?>";
		}
		function validateForm()
		{
			var error="";
			var loading=document.getElementById("loading");
			var found=false;
			var formElement=document.result_booking.elements["booking_id[]"];			
			var cnt=0;
			if (formElement.length){
				for (var icnt=0;icnt<formElement.length;icnt++){
					if (formElement[icnt].checked){
						found=true;
						cnt+=1;
					}
				}
			} else 
			   found=formElement.checked;
			if(1<cnt)
				error="<?php echo addslashes(stripslashes(ERR_SELECT_EXCITED)); ?>";
			if(!found) 
				error="<?php echo addslashes(stripslashes(ERR_SELECT_BOOKING)); ?>";
			if (error){
				alert(error);
				return false;
			}
			command="<?PHP echo tep_href_link(FILENAME_MOVE_BOOKINGS_CONFIRM);?>?";
			if(command!="") {
				if(loading) loading.style.display="";
				do_post_command('result_booking',command);
			}
		}
		function validateLetterForm()
		{
			var found=false;
			var search_ids='';
			var formElement=document.result_booking.elements["booking_id[]"];
			var emailForm;
			emailForm=document.admin_letter;
			if (formElement.length){
				for (var icnt=0;icnt<formElement.length;icnt++){
					if (formElement[icnt].checked==true) {
						search_ids=search_ids+document.result_booking.letters_id[icnt].value+",";
					}
				}
			} else {
				if (formElement.checked) search_ids=search_ids+document.result_booking.letters_id.value+",";
			}
			if (search_ids==''){
				alert("<?php echo addslashes(stripslashes(ERR_SELECT_BOOKING)); ?>");
				return false;
			}
			search_ids=search_ids.substr(0,search_ids.length-1);
			emailForm.search_ids.value=search_ids;
			return true;
		}
		function validateEmailForm(frmid)
		{
			var found=false;
			var search_ids='';
			var formElement=document.result_booking.elements["booking_id[]"];
			var emailForm;
			if (frmid==1)
				emailForm=document.admin_email;
			else
				emailForm=document.admin_sms;
			if (formElement.length){
				for (var icnt=0;icnt<formElement.length;icnt++){
					if (formElement[icnt].checked==true) {
						search_ids=search_ids+document.result_booking.email_address[icnt].value+",";
					}
				}
			} else {
				if (formElement.checked) search_ids=search_ids+document.result_booking.email_address.value+",";
			}
			if (search_ids==''){
				alert("<?php echo addslashes(stripslashes(ERR_SELECT_BOOKING)); ?>");
				return false;
			}
			search_ids=search_ids.substr(0,search_ids.length-1);
			emailForm.search_ids.value=search_ids;
			return true;
		}
		function select_all_boxes(checkbox){
			var formElement=document.result_booking.elements["booking_id[]"];
			if (formElement.length){
				for (icnt=0;icnt<formElement.length;icnt++){
					formElement[icnt].checked=checkbox.checked;
				}					
			} else {
				formElement.checked=checkbox.checked;
			}
		}
		/*function doFormAction(type){
		alert(type);
			if (!validateForm()) return;
			switch(type){
				case 2:
					document.result_booking.action="<?php //echo FILENAME_REFUND_BOOKINGS;?>";
					document.result_booking.process_type.value="single";
					break;
				case 3:
					document.result_booking.action="<?php //echo FILENAME_WITHDRAW_BOOKINGS;?>";
					document.result_booking.process_type.value="single";
					break;
				case 4:
					document.result_booking.action="<?php //echo FILENAME_WITHDRAW_BOOKINGS;?>";
					break;
				case 5:
					document.result_booking.action="<?php //echo FILENAME_REFUND_BOOKINGS;?>";
					break;
			}
			document.result_booking.submit();
		}*/
		function selectBox(id,type,action){
			var element=document.result_booking.elements["booking_id[]"];
			if (element.length)	element[id-1].checked=true;
			else element.checked=true;
			
			if (action==6){
				if (validateEmailForm(1))
					document.admin_email.submit();
				return;
			}
			//document.result_booking.process_type.value=type; // indivitual(single) or group(order) based withdraw
			//doFormAction(action);
		}
</script>
<?php  include(DIR_WS_INCLUDES."date_format_js.php");?>
<?php  include(DIR_WS_INCLUDES."service_bookings.js.php");?>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
			<script language="javascript" src="includes/popup.js"></script>
			<script language="JavaScript">
				var baby=new Array();
				baby[1]="";
				var command="";				
				function showpopup(type)
				{
					var contentText;
					
					if(type=="pending"){		
						contentText="By default, pending payments are hidden. Pending payments are ones which have been booked but with an amount outstanding to complete the payment. Can be " +
										"combined with other criteria to refine choices.";
					}
					content='<table border="0" cellspacing="0" cellpadding="4"  width="100%"><tr><td class=smallText>';
					content+=contentText;
					content+='</td></tr></table>';
					popwidth=250;
					baby[1]=content;
					if (baby[1]!="") popup("1","#ffffff");
				}
			function doReport() {
				startDate=date_format(document.search_booking.tx_from.value,'','Y-m-d',true);
				endDate=date_format(document.search_booking.tx_until.value,'','Y-m-d',true);	
				if(startDate.getTime()>endDate.getTime()) {
					alert("<?php echo REPORT_START_AFTER_END?>");
					return;
				}
				document.search_booking.submit();
			}
			function doactions(steps,arg1,arg2,arg3){
				command="<?PHP echo tep_href_link(FILENAME_SERVICES_SEARCH_BOOKINGS,'action=');?>";
				var loading=document.getElementById("loading");
				if(loading) loading.style.display="";
				var search_form=document.getElementById("search_form");
				var page_obj=document.getElementById("page_1");
				var page_no=1;
				if(document.getElementById("steps")) var step=document.getElementById("steps").value;
				if(steps) step=steps;
				command+=step;
				if(!arg1 && step=='search' && command!="" && document.search_booking) {
					do_post_command('search_booking',command);
					return false;
				}else if(!arg1 && step=='back_search_form') {
					do_get_command(command);
					if(loading) loading.style.display="";
					return;	
				}else if(arg1=='step2' && command!="")
					do_get_command(command);
				if(arg3!='' && page_obj){
					if(arg3=='next') page_no=parseInt(page_obj.options[page_obj.selectedIndex].value)+1;						
					if(arg3=='prev') page_no=parseInt(page_obj.options[page_obj.selectedIndex].value)-1;
					if(arg3=='select') page_no=parseInt(page_obj.options[page_obj.selectedIndex].value);
					command+=arg1+"&page="+page_no;
					if(command!="") do_get_command(command);					
				}
			}
			function do_result(result){
				var loading=document.getElementById("loading");
				if(loading) loading.style.display="none";
				var step="";
				var type="";
				var splt="";
				if(document.move_booking_confirm) type=document.move_booking_confirm.select_action;				
				var frm_div=document.getElementById("search_form");
				if(document.getElementById("steps")) step=document.getElementById("steps").value;
				if(result!="" && frm_div) frm_div.innerHTML=result;
				var sel_time="";
				if(type && document.getElementById("nservice")){
					if(document.getElementById("start_times")) add_end_times();
					else {
						if(document.getElementById("times_val")) splt=document.getElementById("times_val").value.split('{}');
						add_start_times();
						var obj=document.getElementById("start_time");
						if(obj){
							var n=obj.options.length;
							for(i=0;i<n;i++){
								if(obj.options[i].value==splt[8]) {
									obj.selectedIndex=i;
									add_end_times();
									break;									
								}
							}
						}												
				  }
			   }
			   		 
			}
			</script>
	<div id="spiffycalendar" class="text"></div>
	<!-- 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% align=left valign="top">		
			<table border="0" width="100%" cellspacing="0" cellpadding="2">					
				<tr>
					<td colspan="2" class="openContent" id="search_form"><?php search_form();?></td>
				</tr>
				<tr>
					<td colspan="2" align="center">
						&nbsp;<img id="loading" style="display:none;" src='images/24-1.gif' alt='Loading...' title='Loading...'>
					</td>
				</tr>
			</table>			
		</td>
	</tr>
</table>
<!-- body_text_eof //-->

	<!-- footer //-->
	<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
	<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php');  
	function build_search_query(){
	  global $HTTP_GET_VARS,$HTTP_POST_VARS,$search_params,$login_groups_type,$login_id;
		$search_group='A'; // all users both reserved and waitlist
		// form the query with given details in parameters
		if (isset($HTTP_POST_VARS['tx_fname']))
			$input_params=&$HTTP_POST_VARS;
		else
			$input_params=&$HTTP_GET_VARS;
		
		reset($search_params);	
		while(list($key,)=each($search_params)){
			if (isset($input_params[$key])) $search_params[$key]=tep_db_prepare_input($input_params[$key]);
		}
		$query_field1=(tep_not_null($search_params['tx_fname']) ? " and c.customers_firstname like '%" . tep_db_input($search_params['tx_fname']) . "%'" : '');
		$query_field2=(tep_not_null($search_params['tx_lname']) ? " and c.customers_lastname like '%" . tep_db_input($search_params['tx_lname']) . "%'" : '');
		$query_field3=(tep_not_null($search_params['tx_code']) ? " and o.customers_postcode='" . tep_db_input($search_params['tx_code']) . "'" : '');
		$query_field4=(tep_not_null($search_params['tx_words']) ? " and (c.customers_firstname like '%" . tep_db_input($search_params['tx_words']) . "%' or c.customers_lastname like '%" . tep_db_input($search_params['tx_words']) . "%' or c.customers_email_address like '%" . tep_db_input($search_params['tx_words']) . "%' or se.service_location like '%" . tep_db_input($search_params['tx_words']) . "%')": '');
		$date_begin=tep_convert_date_raw(tep_db_input($search_params['tx_from']));
		$date_end=tep_convert_date_raw(tep_db_input($search_params['tx_until']));
		$query_field5=" and (('".$date_begin."'>=os.start_date and '".$date_begin."'<=os.end_date) OR ('".$date_end."'>=os.start_date and '".$date_end."'<=os.end_date) OR ('".$date_begin."'<=os.start_date AND '".$date_end."'>=os.end_date)) " ;
		//$query_field6=(tep_not_null($search_params['tx_until']) ? " and os.end_date<='" . tep_db_input($search_params['tx_until']) . "'" : '');
		if(tep_not_null($search_params['tx_resource'])){
			if($search_params['tx_resource']=='-1')
				$query_field7="";
			else
				$query_field7=" and os.resource_id='" . tep_db_input($search_params['tx_resource']) . "'";
		}
		else
			$query_field7='';
		$query_field8=(tep_not_null($search_params['tx_service'])? " and se.service_id='" . tep_db_input($search_params['tx_service']) . "'":'');

		$query=$query_field1 . $query_field2 . $query_field3 . $query_field4 . $query_field5 . $query_field6 .  $query_field7. $query_field8;		
		return $query;
	}
	
	function search_form(){ 
		global $languages_id,$login_option;
	 echo tep_draw_form('search_booking',FILENAME_SERVICES_SEARCH_BOOKINGS,'','post','onsubmit="javascript:return doactions();"'); ?>
		<table border="0" cellspacing="0" cellpadding="0" width="100%" class="openContent">
		<tr class="openContent_top"><td>&nbsp;</td></tr>
		<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
		<?php 
			$login_option="";
			$service_query=tep_db_query("SELECT se.service_id,sd.service_name from " . TABLE_SERVICES . " se, " . TABLE_SERVICES_DESCRIPTION . " sd where sd.language_id='" . $languages_id . "' " . $login_option . SERVICES_CONDITION_STATUS . " and se.service_id=sd.service_id order by sd.service_name");
			$services_array[]=array('id'=>'','text'=>TEXT_ALL);
			while ($result=tep_db_fetch_array($service_query)){
				$services_array[]=array('id'=>$result['service_id'],'text'=>$result['service_name']);
			}	    
			
			$resources_query=tep_db_query("SELECT red.resource_id,red.resource_name from " . TABLE_RESOURCES . " re, " . TABLE_RESOURCES_DESCRIPTION . " red where red.language_id='" . $languages_id . "'  and re.resource_id=red.resource_id order by red.resource_name");
			$resources_array[]=array('id'=>'-1','text'=>TEXT_RESOURCE_ALL);
			while ($resource_result=tep_db_fetch_array($resources_query)){
				$resources_array[]=array('id'=>$resource_result['resource_id'],'text'=>$resource_result['resource_name']);
			}
		?>
		<tr>
			<td>
			<table border="0" cellspacing="3" cellpadding="2">
			<tr>
				<td class="main"><?php echo TEXT_SEARCH_FIRSTNAME ?></td>
				<td class="main"><?php echo tep_draw_input_field('tx_fname','','size=35');?></td>
				<td width="30"></td>
				<td class="main"><?php echo TEXT_SEARCH_LASTNAME ?></td>
				<td class="main"><?php echo tep_draw_input_field('tx_lname','','size=35'); ?></td>
			</tr>			
			<tr>
				<td class="main"><?php echo TEXT_SEARCH_KEYWORDS ?></td>
				<td class="main"><?php echo tep_draw_input_field('tx_words','','size=20') ?></td>
				<td width="30"></td>
				<td class="main"><?php echo TEXT_SEARCH_POSTCODE ?></td>
				<td class="main"><?php echo tep_draw_input_field('tx_code','','size=20') ?></td>
			</tr> 			
			<tr>
				<?php $_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,EVENTS_DATE_FORMAT);?>
				<td class="main"><?php echo TEXT_SEARCH_FROM ?><br><small>(YYYY-MM-DD)</small></td>
				<td class="main"><?php echo tep_draw_input_field("tx_from",format_date(date('Y-m-d')),"size=10",false,'text',false);?>
				<a href="javascript:show_calendar('search_booking.tx_from',null,null,'<?php echo $date_format;?>');"
				  onmouseover="window.status='Date Picker';return true;"
				  onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/>  
				</a>
				</td>
				<td width="30"></td>
				<td class="main"><?php echo TEXT_SEARCH_UNTIL ?><br><small>(YYYY-MM-DD)</small></td>
				<td class="main"><?php echo tep_draw_input_field("tx_until",format_date(date('Y-m-d')),"size=10",false,'text',false);?>
				<a href="javascript:show_calendar('search_booking.tx_until',null,null,'<?php echo $date_format;?>');"
				  onmouseover="window.status='Date Picker';return true;"
				  onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/>  
				</a>
				</td>
			</tr> 			
			<tr>
				<td class="main"><?php echo TEXT_SEARCH_SERVICE; ?></td>
				<td class="main"><?php echo tep_draw_pull_down_menu('tx_service',$services_array); ?></td>
				<td width="30"></td>
				<td class="main"><?php echo TEXT_SEARCH_RESOURCE; ?></td>
				<td class="main"><?php echo tep_draw_pull_down_menu('tx_resource',$resources_array); ?></td>
			</tr>			
			<tr>
				<td class="main">&nbsp;</td>
				<td class="main"><?php echo tep_draw_checkbox_field('tx_pending','Y',true) . TEXT_SEARCH_PENDING_PERIOD; ?>
					<span onMouseOver="javascript:showpopup('pending')" onMouseOut="javascript:kill();"><?php echo tep_image(DIR_WS_ICONS . "help_icon.gif");?></span>
				</td>
				<td colspan=3 align="center"><?php echo tep_image_submit('button_report_search.gif', IMAGE_SEARCH_BOOKING) ; ?></td>
			</tr>   
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>                    
		</table>
		</td>
	   </tr>
	 </table>
	 <input type="hidden" name="steps" id="steps" value='search'/>		
	 <input type=hidden name=row_check value="<?php echo tep_output_string($rows); ?>" id=row_check/>
    </form>
	<?php 
	}
?>
