<?php

/*
osCommRes, Services Oline 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare

Released under the GNU General Public License 
*/
// Set flag that this is a parent file
  define( '_FEXEC', 1 );
  require('includes/application_top.php');
  require(DIR_WS_CLASSES . 'split_page_results_event.php');
  require(DIR_WS_CLASSES . 'currencies.php');
  require(DIR_WS_FUNCTIONS . 'events_tree.php');
  $currencies = new currencies();

  $command=isset($HTTP_GET_VARS['command'])?tep_db_prepare_input($HTTP_GET_VARS['command']):'';
  $expand=isset($HTTP_GET_VARS['expand'])?tep_db_prepare_input($HTTP_GET_VARS['expand']):'';
  $orders_id=isset($HTTP_GET_VARS['oID'])?(int)$HTTP_GET_VARS['oID']:'';
  $customer_id=isset($HTTP_GET_VARS['cID'])?(int)$HTTP_GET_VARS['cID']:'';
  
  if($command!=''){ 
  		if($command=='search_reservation_first')
			tep_session_unregister('search_params');
		if(!tep_session_is_registered('search_params'))
		{
			tep_session_register('search_params');
			$search_params=array("tx_fname"=>"","tx_lname"=>"","booking_page"=>1,"tx_words"=>"","tx_code"=>"",
							"tx_from"=>"","tx_until"=>"","tx_event"=>"","tx_reserve"=>"","tx_waitlist"=>"","tx_rtype"=>"","tx_session"=>"","tx_dated"=>"","tx_current"=>"","oID"=>"","events_id"=>"","tx_pending"=>"");
		}
		update_session_search_vals();
  	switch($command){ 
		case 'reset_search':
			echo'reset_search^^';
			tep_session_unregister('search_params');
			break;
		case 'search_reservation':
		case 'search_reservation_first':
			echo 'search_reservation^^';
			echo search_reservation();
			break;
		case 'bookings_page_navigation':
			echo 'search_reservation^^';
			echo search_reservation();
			break;
		case 'events_booking_back':
			echo 'events_booking_back^^';
			echo events_booking_search();
			break;
		case 'events_move_reservations_confirm':
			echo 'events_move_reservations_confirm^^';
			echo events_move_reservations_confirm();
			break;
		case 'events_move_reservations_process':
			echo 'events_move_reservations_process^^';
			echo events_move_reservations_process();
			break;
		case 'setflag':
			$error=setflag();
			echo 'setflag^^';
			echo search_reservation();
			echo '^^'.$error;
			break;
		case 'move_waitlist':
			$error=move_waitlist();
			echo 'move_waitlist^^';
			echo search_reservation();
			echo '^^'.$error;
			break;
		case 'reserve_view':
			echo 'reserve_view^^';
			echo search_reservation();
			break;
		case 'letter':
			break;
		case 'withdraw_confirmation':
			echo 'withdraw_confirmation^^';
			echo withdraw_confirmation();
			break;
		case 'withdraw_confirm_back':
			echo 'withdraw_confirm_back^^';
			echo search_reservation();
			break;
		case 'withdraw_process':
			$error=withdraw_process();
			echo 'withdraw_process^^';
			echo search_reservation();
			echo '^^'.$error;
			break;
		case 'refund_reservation':
			echo 'refund_reservation^^';
			echo refund_reservation();
			break;
		case 'refund_back':
			echo 'refund_back^^';
			echo search_reservation();
			break;
		case 'refund_reservation_confirm':
			echo 'refund_reservation_confirm^^';
			echo refund_reservation_confirm();
			break;
		case 'refund_reservation_process':
			
			echo 'refund_reservation_process^^';
   			$status=refund_reservation_process();
			echo search_reservation();
			echo '^^'.$status;
			break;
		case 'refund_reservation_back':
			echo 'refund_reservation_back^^';
			echo search_reservation();
			break;
	}
  exit;
  }
  
  	
  tep_get_last_access_file();

  //added new
  define(BOX_WIDTH1,'125');

  
  
  $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);
  $get_group_type=(isset($HTTP_GET_VARS['group'])?tep_db_prepare_input($HTTP_GET_VARS['group']):'N');
  // if action is perform waitlist or reserve moving operation
	if ($action=='setflag'){
		$flag=(isset($HTTP_GET_VARS['flag'])?$HTTP_GET_VARS['flag']:'');
		$products_id=(isset($HTTP_GET_VARS['pID'])?(int)$HTTP_GET_VARS['pID']:0);
		$events_id=(isset($HTTP_GET_VARS['eID'])?(int)$HTTP_GET_VARS['eID']:0);
		$orders_products_id=(isset($HTTP_GET_VARS['opID'])?(int)$HTTP_GET_VARS['opID']:0);
		$error='';
		if ($flag=='W'){
			// if need to move waitlist find the appropriate group and error if not found
			$group=tep_get_waitlist_name($events_id,$products_id,$orders_products_id);
			if ($group!='E')
				tep_db_query("update " . TABLE_ORDERS_PRODUCTS . " set events_type='W', waitlist_type='" . $group . "' where orders_products_id='" . (int)$orders_products_id . "'");
			else 
				$error=ERR_EMPTY_WAIT_SIZE;
		} else {
			// if need to move reservation check if enough seats are present
			if (tep_check_max_reservations($events_id,$products_id,$orders_products_id)==true){
				tep_db_query("update " . TABLE_ORDERS_PRODUCTS . " set events_type='R', waitlist_type='P' where orders_products_id='" . (int)$orders_products_id . "'");

				tep_send_event_email($orders_products_id,'WPR');
				tep_process_waiting_list($events_id,$products_id);
				tep_promote_waiting_list($events_id,$products_id);
			}
			else $error=ERR_EMPTY_RESERVE;
    	}
		tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS,tep_get_all_get_params(array('action','opID','eID','pID','x','y','flag')) . 'action=search&error=' . $error));
	} else if ($action=="move_waitlist"){
		$events_id=(isset($HTTP_GET_VARS['eID'])?(int)$HTTP_GET_VARS['eID']:0);
		$orders_id=(isset($HTTP_GET_VARS['oID'])?(int)$HTTP_GET_VARS['oID']:0);
		$events_query=tep_db_query("SELECT " . tep_event_fields_list() . " from " . TABLE_EVENTS . " e where e.events_id='" . (int)$events_id . "'");
		$events_result=tep_db_fetch_array($events_query);

		$order_query=tep_db_query("SELECT op.orders_products_id,op.products_id as session_id,op.products_model as start_date,o.customers_id from " . TABLE_ORDERS_PRODUCTS . " op, " .  TABLE_ORDERS . " o where o.orders_id='" . (int)$orders_id . "' and op.products_type='E' and op.events_type='W' and o.orders_id=op.orders_id  op.events_id='" . (int)$events_id . "'");

		$move_waitlist=true;
		$update_list="";
		$error="";
		while($order_result=tep_db_fetch_array($order_query)){
			$reserve_count=tep_reserve_count($order_result["sessions_id"],$events_result);
			$reserve_count+=tep_count_cart($order_result["sessions_id"],'R');
			$reserve_count+=tep_get_renewal_users_count($events_result['events_id'],$events_result['events_renewal_end'],$order_result['customers_id'],$order_result['start_date']);
			if ($events_result["events_max_reserve"]<=$reserve_count){
				$move_waitlist=false;
				$error=ERR_EMPTY_SEATS_ALL_WAITLIST;
				break;
			}
			$update_list.=$order_result["orders_products_id"] . ",";
		}
		if ($move_waitlist && $update_list!="") {
			$update_list=substr($update_list,0,-1);
			tep_db_query("UPDATE " . TABLE_ORDERS_PRODUCTS . " set events_type='R',waitlist_type='P' where orders_products_id in(" . $update_list . ")");
			tep_send_event_order_notify_email($orders_id,$events_id);
		} 
		tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS,tep_get_all_get_params(array('action','opID','eID','pID','x','y','flag')) . 'action=search&error=' . $error));	
	}
	if ($action=="search")
	{
		$rows=0;
		
		$temp_query=build_search_query();						
		if ($orders_id>0){
			$fields=",op.products_model as start_date,op.products_model as end_date ";
			$query=" and o.orders_id='" . (int)$orders_id . "' and e.events_id='" . (int)$HTTP_GET_VARS['events_id'] ."' order by op.products_model";
		} else {
			$fields=",min(op.products_model) as start_date,count(op.products_model) as total_count,max(op.products_model) as end_date,sum(ascii(events_type)) as refund_flag";
			//$query=$temp_query . ' group by o.orders_id,c2c.customer_id,e.events_id ';
			//$query=$temp_query . ' group by op.orders_products_id order by o.orders_id,s.start_date';
			$query=$temp_query . ' group by o.orders_id,o.customers_id,e.events_id  order by o.orders_id,op.products_model';
		}
		if ($search_params["tx_pending"]!="") {
			$cDate="curdate()";
			$pending_option=" and (o.orders_status>1 or e.events_pending_period=0 or date_add(o.date_purchased,interval e.events_pending_period day)>=" . $cDate . ")";
		}

		/*$search_query=	"select o.orders_id,op.orders_products_id,op.products_id,op.waitlist_type,op.events_type,o.customers_id,c.customers_firstname,c.customers_lastname,c.customers_email_address,o.customers_postcode,e.events_location,e.events_instructor,ed.events_name,e.events_id,e.events_group_bookings,op.products_quantity " . $fields . " from " . 
						TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_CUSTOMERS . " c, " .  TABLE_EVENTS_SESSIONS . " s, " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed " . 
						" where op.products_type='E' and  op.events_type!='' and op.events_type!='C'" .
						" and  op.orders_id=o.orders_id and c.customers_id=o.customers_id and op.products_id=s.sessions_id and s.events_id=e.events_id and ed.events_id=e.events_id and ed.language_id='" . $languages_id . "'" . $pending_option . $query;			 */
		$search_query=	"select o.orders_id,op.orders_products_id,op.products_id,op.waitlist_type,op.events_type,o.customers_id,o.customers_name,o.customers_email_address,o.customers_postcode,e.events_location,e.events_instructor,e.events_id,e.events_group_bookings,op.products_quantity " . $fields . " from " . 
						TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " .  TABLE_EVENTS . " e " . 
						" where op.products_type='E' and  op.events_type!='' and op.events_type!='C'" .
						" and op.orders_id=o.orders_id and op.events_id=e.events_id " . $pending_option . $query;				
		if ($orders_id<=0)
			$reservations_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";
	}
	
	function refund_reservation_confirm(){
	global $HTTP_POST_VARS,$currencies,$HTTP_SESSION_VARS,$language,$search_params;
	 require(DIR_WS_CLASSES . 'payment.php');
	 require(DIR_WS_LANGUAGES.$language.'/'.FILENAME_REFUND_CONFIRM_RESERVATIONS);
	$refund_flag=false;

	if ($refund_flag){
		$payment=(isset($HTTP_POST_VARS['payment'])?$HTTP_POST_VARS['payment']:'');
		if (!tep_session_is_registered('payment')) tep_session_register('payment');
		
		$payment = $HTTP_POST_VARS['payment']; 
	
	
	  
		$payment_modules = new payment($payment);
	}
	$reserve_ids=isset($HTTP_POST_VARS["reservation_id"])?$HTTP_POST_VARS["reservation_id"]:array();
	
	$type=(isset($HTTP_POST_VARS['process_type']) ? $HTTP_POST_VARS['process_type'] : 'single');

	// if no reservation ids found just return to search reservation page
	if (sizeof($reserve_ids)<=0) {
		tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS));
	}

	/*$params=array("tx_fname","tx_lname","page","tx_words","tx_code",
							"tx_from","tx_until","tx_event","tx_reserve","tx_waitlist","tx_rtype","tx_session","tx_current","orders_id","events_id","tx_pending");

	$search_params=tep_prepare_hidden_params($params,"post","post");*/

	if ($type=='single') { // if indivitual event items selected
			$select_string=" op.orders_products_id in (" . join(",",$reserve_ids) . ") ";
		$customer_query="select o.orders_id,o.customers_id,o.customers_name,o.customers_street_address,o.customers_suburb, " .
						" o.customers_postcode,o.customers_city,o.customers_state,o.customers_country from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o " . 
						" where " . $select_string . " and op.orders_id=o.orders_id group by o.customers_id ";
		$customer_fetch=@tep_db_query($customer_query);
		$dates_query="SELECT op.orders_products_id,op.products_model as start_date from " . TABLE_ORDERS_PRODUCTS . " op where " . $select_string . " and op.products_type='E' and op.events_type in('R','W','T') order by op.products_model";
		$dates_fetch=tep_db_query($dates_query);
	} else { // if a single order is selected
		$split_details=split("_",$reserve_ids[0]);
		$orders_id=0;
		$events_id=0;
		if (sizeof($split_details)>1){
			$orders_id=(int)$split_details[0];
			$events_id=(int)$split_details[1];
		}
		$customer_query="select o.orders_id,o.customers_id,o.customers_name,o.customers_street_address,o.customers_suburb, " .
						" o.customers_postcode,o.customers_city,o.customers_state,o.customers_country from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o " . 
						" where op.orders_id='" . (int)$orders_id . "' and op.orders_id=o.orders_id  group by o.customers_id ";
		$customer_fetch=tep_db_query($customer_query);

		$dates_query="SELECT op.orders_products_id,op.products_model as start_date from " . TABLE_ORDERS_PRODUCTS . " op where op.orders_id='" . (int)$orders_id . "' and op.events_id='" . (int)$events_id . "' and op.products_type='E' and op.events_type in('R','W','T') order by op.products_model";
		$dates_fetch=tep_db_query($dates_query);
	}	
	$customer_cnt=tep_db_num_rows($customer_fetch);
	$dates_cnt=tep_db_num_rows($dates_fetch);
	
	if ($customer_cnt<=0 || $dates_cnt<=0) tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS));

	if (!tep_session_is_registered("refund_details")) tep_session_register("refund_details");
	
	$search_details=array("tx_fname"=>'',"tx_lname"=>'',"page"=>1,"tx_words"=>'',"tx_code"=>'',
							"tx_from"=>'',"tx_until"=>'',"tx_event"=>'',"tx_reserve"=>'',"tx_waitlist"=>'',"tx_rtype"=>'',"tx_session"=>'',"tx_dated"=>'',"tx_current"=>'',"orders_id"=>'',"events_id"=>'',"tx_pending"=>'');
	reset($search_details);	
	while(list($key,)=each($search_details)){
		if (isset($HTTP_POST_VARS[$key])) $search_details[$key]=tep_db_prepare_input($HTTP_POST_VARS[$key]);
	}
	
	$refund_details=array("ids"=>$reserve_ids,'type'=>$type,'search'=>$search_details,'amount'=>0,'payment'=>'');
	$HTTP_SESSION_VARS['refund_details']=array("ids"=>$reserve_ids,'type'=>$type,'search'=>$search_details,'amount'=>0,'payment'=>'');

	if ($refund_flag){
		if ( (is_array($payment_modules->modules)) && (sizeof($payment_modules->modules) > 1) && (!is_object($$payment))) tep_redirect(tep_href_link(FILENAME_REFUND_RESERVATIONS,'action=back&error_message=' . urlencode(ERROR_NO_PAYMENT_MODULE_SELECTED)));
	}
	tep_session_register('payment_page');
	tep_session_register('payment_params');
	$payment_page='refund';
	$payment_params='action=back&';

	if ($refund_flag){
		if (is_array($payment_modules->modules)) {
			$payment_modules->pre_confirmation_check();
		}
		$refund_details["payment"]=$payment;
	}
	$customer=tep_db_fetch_array($customer_fetch);
	?>
	<?php  
			if ($refund_flag && isset($$payment->form_action_url)) {
				echo "<form name='checkout_confirmation' action='" . $$payment->form_action_url . "' method=post>";
			} else {
				echo tep_draw_form('checkout_confirmation', FILENAME_REFUND_PROCESS);
			} 
			$customer_name=split(' ',$customer['customers_name']);
			
	?>
	<!-- 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>
		<table border="0" width="100%" cellspacing="0" cellpadding="0">
			<tr>
				<td align=left class='pageHeading' colspan='2'><?php echo HEADING_REFUND_CONFIRM_TITLE ?></td>
			</tr>
		</table>
		</td>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>	  
		<tr>
	    	<td>
			<table border="0" cellspacing="4" cellpadding="2">
			
	        <tr align="left">
				<td class="tableHeading"><?php echo TEXT_REFUND_FIRST_NAME ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer_name[0]; ?></td>
			</tr>
			<tr align="left">
				<td class="tableHeading"><?php echo TEXT_REFUND_LAST_NAME; ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer_name[1]; ?></td>
			</tr>
			<tr>
				<td class="tableHeading"><?php echo TEXT_REFUND_ADDRESS; ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_street_address']; ?></td>
			</tr> 
			<tr>
				<td class="tableHeading"><?php echo TEXT_REFUND_SUBRUB; ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_suburb'];?></td>
			</tr> 
			<tr>
				<td class="tableHeading"><?php echo TEXT_REFUND_POSTCODE; ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_postcode']; ?></td>
			</tr>
			<tr>
				<td class="tableHeading"><?php echo TEXT_REFUND_CITY; ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_city']; ?></td>
			</tr>
			<tr>
				<td class="tableHeading"><?php echo TEXT_REFUND_STATE; ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_state']; ?></td>
			</tr>
			<tr>
				<td class="tableHeading"><?php echo TEXT_REFUND_COUNTRY; ?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_country']; ?></td>
			</tr>
			<tr>
				<td class="tableHeading" valign="top"><?php echo TEXT_REFUND_DATES; ?></td>
				<td class="main" nowrap>
				<?php 	
						echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;';
						$icnt=1;
						$date_string="";
						while($dates=tep_db_fetch_array($dates_fetch)){
							$date_string.=format_date($dates["start_date"]) . ", ";
							$icnt++;
							if ($icnt%4==0) $date_string.='<br>' . tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;';
						}
						if (substr($date_string,-2)==", ") $date_string=substr($date_string,0,-2);
						echo $date_string;
				?>
				</td>
			</tr>
			<?php echo  tep_get_attendees($customer['orders_id'],$customer['customers_id']);?>
	        <tr>
			   <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
	        </tr>
			<?php
			if ($refund_flag && tep_not_null($payment)){
				$confirmation = $payment_modules->confirmation();
			?>
				<tr>
					<td class="tableHeading"><b><?php echo TEXT_PAYMENT_METHOD; ?></b></td>
					<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $GLOBALS[$payment_modules->selected_module]->title;//echo $confirmation['title']; ?></b></td>
				</tr>
				<tr>
					<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
				</tr>
				<?php
					for ($j=0, $n2=sizeof($confirmation['fields']); $j<$n2; $j++) {
				?>
					<tr>
						<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $confirmation['fields'][$j]['title']; ?></td>
						<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' .$confirmation['fields'][$j]['field']; ?></td>
					</tr>
					<?php
					}
				}
				?>
					<tr>
						<td class="tableHeading"><?php echo TEXT_REFUND_AMOUNT; ?></td>
						<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $currencies->format($HTTP_POST_VARS['refund_amount']); ?></td>
					</tr>
					<tr>
						<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
					</tr>
					<tr>
						<td align=right colspan="2" valign="middle">
						<?php
							echo '<a href="javascript: refund_reservation_back();">' . tep_image_button('button_back.gif',IMAGE_BACK) . '</a>&nbsp;<span id="refund_back_load"></span>';
							$amount=$HTTP_POST_VARS['refund_amount'];
							$order_detail=array('info'=>array('total'=>tep_get_rounded_amount($amount)));
							$order=new objectInfo($order_detail);
							if (is_array($payment_modules->modules)) {
								echo $payment_modules->process_button();
							}
							if ($order->info['total']>0) 
								echo '<a href="javascript: refund_reservation_process();">'.tep_image_button('button_confirm_refund.gif', IMAGE_REFUND_CONFIRM).'</a>&nbsp;<span id="refund_ajax_load"></span>';
							$refund_details["amount"]=$order->info["total"];
						?>
						</td>
					</tr>
					</form>
					</table>
				</td>
			</tr>
      </table>
      </td>
	</tr>
	</table>
	<!-- body_text_eof //-->
	
	<?php
	}
	
	function move_waitlist(){
	$events_id=(isset($HTTP_GET_VARS['eID'])?(int)$HTTP_GET_VARS['eID']:0);
		$orders_id=(isset($HTTP_GET_VARS['oID'])?(int)$HTTP_GET_VARS['oID']:0);
		$events_query=tep_db_query("SELECT " . tep_event_fields_list() . " from " . TABLE_EVENTS . " e where e.events_id='" . $events_id . "'");
		$events_result=tep_db_fetch_array($events_query);

		$order_query=tep_db_query("SELECT op.orders_products_id,op.products_id as sessions_id,op.products_model as start_date,o.customers_id from " . TABLE_ORDERS_PRODUCTS . " op, " .  TABLE_ORDERS . " o where o.orders_id='" . (int)$orders_id . "' and op.products_type='E' and op.events_type='W' and o.orders_id=op.orders_id and op.events_id='" . (int)$events_id . "'");

		$move_waitlist=true;
		$update_list="";
		$error="";
		while($order_result=tep_db_fetch_array($order_query)){
			$reserve_count=tep_reserve_count($order_result["sessions_id"],$events_result);
			$reserve_count+=tep_count_cart($order_result["sessions_id"],'R');
			$reserve_count+=tep_get_renewal_users_count($events_result['events_id'],$events_result['events_renewal_end'],$order_result['customers_id'],$order_result['start_date']);
			if ($events_result["events_max_reserve"]<=$reserve_count){
				$move_waitlist=false;
				$error=ERR_EMPTY_SEATS_ALL_WAITLIST;
				break;
			}
			$update_list.=$order_result["orders_products_id"] . ",";
		}
		if ($move_waitlist && $update_list!="") {
			$update_list=substr($update_list,0,-1);
			tep_db_query("UPDATE " . TABLE_ORDERS_PRODUCTS . " set events_type='R',waitlist_type='P' where orders_products_id in(" . $update_list . ")");
			tep_send_event_order_notify_email($orders_id,$events_id);
		} 
	return $error;
	}
	
	function setflag(){
	global $HTTP_GET_VARS;
	$flag=(isset($HTTP_GET_VARS['flag'])?$HTTP_GET_VARS['flag']:'');
	$products_id=(isset($HTTP_GET_VARS['pID'])?(int)$HTTP_GET_VARS['pID']:0);
	$events_id=(isset($HTTP_GET_VARS['eID'])?(int)$HTTP_GET_VARS['eID']:0);
	$orders_products_id=(isset($HTTP_GET_VARS['opID'])?(int)$HTTP_GET_VARS['opID']:0);
	$error='';
		if ($flag=='w'){
			// if need to move waitlist find the appropriate group and error if not found
			$group=tep_get_waitlist_name($events_id,$products_id,$orders_products_id);
			if ($group!='E') {
				tep_db_query("update " . TABLE_ORDERS_PRODUCTS . " set events_type='W', waitlist_type='" . $group . "' where orders_products_id='" . (int)$orders_products_id . "'");
				$vb="update " . TABLE_ORDERS_PRODUCTS . " set events_type='W', waitlist_type='" . $group . "' where orders_products_id='" . (int)$orders_products_id . "'";
			} else 
				$error=ERR_EMPTY_WAIT_SIZE;
		} else {
			// if need to move reservation check if enough seats are present
			if (tep_check_max_reservations($events_id,$products_id,$orders_products_id)==true){
				tep_db_query("update " . TABLE_ORDERS_PRODUCTS . " set events_type='R', waitlist_type='P' where orders_products_id='" . (int)$orders_products_id . "'");
				$vb="update " . TABLE_ORDERS_PRODUCTS . " set events_type='R', waitlist_type='P' where orders_products_id='" . (int)$orders_products_id . "'";

				tep_send_event_email($orders_products_id,'WPR');
				tep_process_waiting_list($events_id,$products_id);
				tep_promote_waiting_list($events_id,$products_id);
			}
			else $error=ERR_EMPTY_RESERVE;
    	} //echo $vb .'sdfas fas asd sad ';
	return $error;
	}
	function refund_reservation_process(){
	global $HTTP_POST_VARS,$HTTP_SESSION_VARS;
	$server_date = getServerDate(true);

  	if (!isset($HTTP_SESSION_VARS['refund_details'])){
	return 0;
    //tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS, '', 'SSL'));
	}
	$refund=$HTTP_SESSION_VARS["refund_details"];
	$reserve_ids=isset($refund["ids"])?$refund["ids"]:array();
	$type=(isset($refund['type']) ? $refund['type'] : 'single');
	$fetch_type=(isset($refund['search']) ? $refund['search'] : array());

	// if no reservation ids found just return to search reservation page
	if (sizeof($reserve_ids)<=0) {
		return 0;

	}
	/*$params=array("tx_fname","tx_lname","page","tx_words","tx_code",
							"tx_from","tx_until","tx_event","tx_reserve","tx_waitlist","tx_rtype","tx_session","tx_current","orders_id","events_id","tx_pending");


	$search_get_params=tep_prepare_hidden_params($params,"var","get",$fetch_type);*/


	if ($type=='single') { // if indivitual event items selected
		$select_string=" op.orders_products_id in (" . join(",",$reserve_ids) . ") ";
		$dates_query="SELECT op.orders_id,op.orders_products_id,op.events_id from " . TABLE_ORDERS_PRODUCTS . " op where " . $select_string . " and op.products_type='E' and op.events_type in('R','W','T')";
		$dates_fetch=tep_db_query($dates_query);
	} else { // if a single order is selected
		$split_details=split("_",$reserve_ids[0]);
		$orders_id=0;
		$events_id=0;
		if (sizeof($split_details)>1){
			$orders_id=(int)$split_details[0];
			$events_id=(int)$split_details[1];
		}
		$dates_query="SELECT op.orders_id,op.orders_products_id,op.events_id from " . TABLE_ORDERS_PRODUCTS . " op where op.orders_id='" . $orders_id . "' and op.events_id='" . $events_id . "' and op.products_type='E' and op.events_type in('R','W','T')";
		$dates_fetch=tep_db_query($dates_query);
	}	

	$dates_cnt=tep_db_num_rows($dates_fetch);

	$mail_order=array();
	while ($dates=tep_db_fetch_array($dates_fetch)){
	    tep_db_query("update " . TABLE_ORDERS_PRODUCTS . " set events_type='F' where orders_products_id='" . (int)$dates["orders_products_id"] . "'");
		if (!isset($mail_order[$dates["orders_id"]])) $mail_order[$dates["orders_id"]]=array();
		$mail_order[$dates["orders_id"]][]=$dates["orders_products_id"];
	}
	if (!isset($refund["payment"])) $refund["payment"]="manual";
	
	if (sizeof($mail_order)>0){
		reset($mail_order);

		while(list($oID,)=each($mail_order)){
			if (sizeof($mail_order[$oID])>0){
				$id_string=join(",",$mail_order[$oID]);
				$sql_array=array(	"refund_date"=>$server_date,
									"orders_id"=>$oID,
									"amount"=>$refund["amount"],
									"payment_method"=>$refund["payment"],
									"session_ids"=>$id_string
								);
								
				tep_db_perform(TABLE_EVENTS_REFUND_RESERVATIONS,$sql_array);
				$id_string=join("','",$mail_order[$oID]);
				tep_send_event_email($id_string,'WTR');
			}
		}
	}
	
    tep_session_unregister("refund_details");
    tep_session_unregister("payment");
    tep_session_unregister("payment_page");
    tep_session_unregister("payment_params");
	}
	
	function refund_reservation(){
	global $HTTP_POST_VARS,$HTTP_GET_VARS,$HTTP_SESSION_VARS,$language;

    require(DIR_WS_CLASSES . 'payment.php');
	require(DIR_WS_LANGUAGES.$language.'/'.FILENAME_REFUND_RESERVATIONS);
	$refund_flag=false;
	tep_session_unregister('payment'); 
	

	$action = (isset($HTTP_GET_VARS['action']) ? tep_db_prepare_input($HTTP_GET_VARS['action']) : '');
	
	tep_session_unregister($payment);
	tep_session_unregister('payment_page');
	tep_session_unregister('payment_params');

	if ($action!="back"){
		$reserve_ids=isset($HTTP_POST_VARS["reservation_id"])?$HTTP_POST_VARS["reservation_id"]:array();
		$type=(isset($HTTP_POST_VARS['process_type']) ? $HTTP_POST_VARS['process_type'] : 'single');
		$fetch_type=array();
		$method="post";
		if (tep_session_is_registered("refund_details")) tep_session_unregister("refund_details");
	} else {
		$refund=isset($HTTP_SESSION_VARS["refund_details"])?$HTTP_SESSION_VARS["refund_details"]:array();
		$reserve_ids=isset($refund["ids"])?$refund["ids"]:array();
		$type=(isset($refund['type']) ? $refund['type'] : 'single');
		$fetch_type=(isset($refund['search']) ? $refund['search'] : array());
		$method="var";
	}
	// if no reservation ids found just return to search reservation page

	/*$params=array("tx_fname","tx_lname","page","tx_words","tx_code",
							"tx_from","tx_until","tx_event","tx_reserve","tx_waitlist","tx_rtype","tx_session","tx_current","orders_id","events_id","tx_pending");


	$search_post_params=tep_prepare_hidden_params($params,$method,"post",$fetch_type);
	$search_get_params=tep_prepare_hidden_params($params,$method,"get",$fetch_type);*/
	

	if ($type=='single') { // if indivitual event items selected
		$select_string=" op.orders_products_id in (" . join(",",$reserve_ids) . ") ";
		$customer_query="select o.orders_id,o.customers_id,o.customers_name,o.customers_street_address,o.customers_suburb, " .
						" o.customers_postcode,o.customers_city,o.customers_state,o.customers_country from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o " . 
						" where " . $select_string . " and op.orders_id=o.orders_id  group by o.customers_id ";
		$customer_fetch=tep_db_query($customer_query);
		$dates_query="SELECT op.orders_products_id,op.products_model as start_date,op.final_price,op.products_tax from " . TABLE_ORDERS_PRODUCTS . " op where " . $select_string . " and op.products_type='E' and op.events_type in('R','W','T')order by op.products_model";
		$dates_fetch=tep_db_query($dates_query);
	} else { // if a single order is selected
		$split_details=split("_",$reserve_ids[0]);
		$orders_id=0;
		$events_id=0;
		if (sizeof($split_details)>1){
			$orders_id=(int)$split_details[0];
			$events_id=(int)$split_details[1];
		}
		$customer_query="select o.orders_id,o.customers_id,o.customers_name,o.customers_street_address,o.customers_suburb, " .
						" o.customers_postcode,o.customers_city,o.customers_state,o.customers_country from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o " . 
						" where op.orders_id='" . (int)$orders_id . "' and op.orders_id=o.orders_id group by o.customers_id ";
		$customer_fetch=tep_db_query($customer_query);

		$dates_query="SELECT op.orders_products_id,op.products_model as start_date,op.final_price,op.products_tax from " . TABLE_ORDERS_PRODUCTS . " op where op.orders_id='" . (int)$orders_id . "' and op.events_id='" . (int)$events_id . "' and op.products_type='E' and op.events_type in('R','W','T') order by op.products_model";
		$dates_fetch=tep_db_query($dates_query);
	}	
	$customer_cnt=tep_db_num_rows($customer_fetch);
	$dates_cnt=tep_db_num_rows($dates_fetch);

	if ($refund_flag){
     $payment_modules = new payment();
	}
	echo tep_draw_form('refund_reservation',FILENAME_REFUND_CONFIRM_RESERVATIONS);
	?>
	<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr>
				<td>
				<table border="0" width="100%" cellspacing="0" cellpadding="0">
					<tr>
						<td align=left class='pageHeading' colspan='2'><?php echo HEADING_REFUND_TITLE ?></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
			</tr>	  
			<tr>
				<td>
				<table border="0" cellspacing="4" cellpadding="2" width=50%>
				<?php 
					if ($customer_cnt>0) {
						$customer=tep_db_fetch_array($customer_fetch);	
						$customer_name=split(' ',$customer['customers_name']);

						echo $search_post_params;
						for ($icnt=0;$icnt<sizeof($reserve_ids);$icnt++){
							echo '<input type="hidden" name="reservation_id[]" value="' . tep_output_string($reserve_ids[$icnt]) . '">';
						}
						echo '<input type="hidden" name="process_type" value="' . tep_output_string($type) . '">';
				?>
						<tr align="left">
							<td class="tableHeading"><?php echo TEXT_REFUND_FIRST_NAME ?></td>
							<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer_name[0]; ?></td>
						</tr>
						<tr align="left">
							<td class="tableHeading"><?php echo TEXT_REFUND_LAST_NAME; ?></td>
							<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer_name[1]; ?></td>
						</tr>
						<tr>
							<td class="tableHeading"><?php echo TEXT_REFUND_ADDRESS; ?></td>
							<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_street_address']; ?></td>
						</tr> 
						<tr>
							<td class="tableHeading"><?php echo TEXT_REFUND_SUBRUB; ?></td>
							<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_suburb'];?></td>
						</tr> 
						<tr>
							<td class="tableHeading"><?php echo TEXT_REFUND_POSTCODE; ?></td>
							<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_postcode']; ?></td>
						</tr>
						<tr>
							<td class="tableHeading"><?php echo TEXT_REFUND_CITY; ?></td>
							<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_city']; ?></td>
						</tr>
						<tr>
							<td class="tableHeading"><?php echo TEXT_REFUND_STATE; ?></td>
							<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_state']; ?></td>
						</tr>
						<tr>
							<td class="tableHeading"><?php echo TEXT_REFUND_COUNTRY; ?></td>
							<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_country']; ?></td>
						</tr>
						<tr>
							<td class="tableHeading" valign="top"><?php echo TEXT_REFUND_DATES; ?></td>
							<td class="main">
							<?php 	
									echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;';
									if ($dates_cnt>0){ // if dates found
										$icnt=1;
										$date_string="";
										$refund_amount=0;
										while($dates=tep_db_fetch_array($dates_fetch)){
											$date_string.=format_date($dates["start_date"]) . ", ";
											$refund_amount+=tep_add_tax($dates["final_price"],$dates["products_tax"]);
											$icnt++;
											if ($icnt%4==0) $date_string.='<br>' . tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;';
										}
										if (substr($date_string,-2)==", ") $date_string=substr($date_string,0,-2);
										echo $date_string;
									} else {
										echo TEXT_NO_REFUND_DATES;
									}
							?>
							</td>
						</tr>
						<?php
						echo tep_get_attendees($customer['orders_id'],$customer['customers_id']);
						 if ($dates_cnt>0){ 
						?>
							<tr>
								<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
							</tr>
							<?php if ($refund_flag) {?>
							<tr>
								<td class=main><b><?php echo TEXT_PAYMENT_METHOD; ?></b></td>
							</tr>
							<tr>
							<td colspan=2>
							<table border="0" width="100%" cellspacing="1" cellpadding="2">
								<tr class="infoBoxContents">
								<td>
								<table border="0" width="100%" cellspacing="0" cellpadding="2">
								<?php
									$selection = $payment_modules->selection();
									$radio_buttons = 0;
									for ($i=0, $n=sizeof($selection); $i<$n; $i++) {
										if ( ($selection[$i]['id'] == $payment) || ($n == 1) ) {
											echo '                  <tr id="defaultRow" class="moduleRowSelected" onmouseover="rowOverEffectRefund(this)" onmouseout="rowOutEffectRefund(this)" onclick="selectRowEffectRefund(this, ' . $radio_buttons . ')">' . "\n";
										} else {
											echo '                  <tr class="moduleRow" onmouseover="rowOverEffectRefund(this)" onmouseout="rowOutEffectRefund(this)" onclick="selectRowEffectRefund(this, ' . $radio_buttons . ')">' . "\n";
										}
								?>
									<td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
									<td class="main"><b><?php echo $selection[$i]['module']; ?></b></td>
									<?php
										echo '<td>';
										if (sizeof($selection) > 1) {
											echo tep_draw_radio_field('payment', $selection[$i]['id']);
										} else {
											echo tep_draw_hidden_field('payment', $selection[$i]['id']);
										}
									?>
								</tr>
								<?php
									if (isset($selection[$i]['error'])) {
								?>
								<tr>
									<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
									<td class="main"><?php echo $selection[$i]['error']; ?></td>
								</tr>
								<?php
									} elseif (isset($selection[$i]['fields']) && is_array($selection[$i]['fields'])) {
								?>
									<tr>
										<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
										<td>
										<table border="0" cellspacing="0" cellpadding="2">
										<?php
										for ($j=0, $n2=sizeof($selection[$i]['fields']); $j<$n2; $j++) {
										?>
											<tr>
												<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
												<td class="main"><?php echo $selection[$i]['fields'][$j]['title']; ?></td>
												<td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
												<td class="main"><?php echo $selection[$i]['fields'][$j]['field']; ?></td>
												<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
											</tr>
										<?php
										}
										if ($selection[$i]['module']=='Test') {
										?>
											<tr>
												<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
												<td class="main"><?php echo ENTRY_CREDIT_CARD_CVV; ?></td>
												<td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
												<td class="main"><?php echo tep_draw_input_field('update_info_cc_cvv','','size=10');?></td>
												<td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
											</tr>
										<?php } ?>
										</table>
										</td>
									</tr>
								<?php
								}
								$radio_buttons++;
							  }
							?>
							</table>
							</td>
						</tr>
						</table>
						</td>
						</tr>
						<?php 
 						 echo tep_get_attendees($customer['orders_id'],$customer['customers_id']);
						 } // refund flag ?> 
						<tr>
							<td class="tableHeading"><?php echo TEXT_REFUND_AMOUNT; ?></td>
							<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . '<input type="text" id="refund_amount" name="refund_amount" value="' . tep_output_string($refund_amount) . '" size=15 maxlength=15>'; ?></td>
						</tr>
				<?php  } // dates_cnt
					} else { // if customer cnt ?>
					<tr>
						<td class="tableHeading" colspan="2"><?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=right colspan="2" valign="middle">
							<?php 
									echo '<a href="javascript: refund_back();">' . tep_image_button('button_back.gif',IMAGE_BACK) . '</a>&nbsp;<span id="refund_back_ajax_load"></span>';
									if ($customer_cnt>0 && $dates_cnt>0) echo '<a href="javascript: refund_reservation();">'.tep_image_button('button_refund.gif', IMAGE_REFUND_RESERVATION).'</a>&nbsp;<span id="refund_ajax_load"></span>'; 
							?>
						</td>
					</tr>
			</table>
			
			</td>
		</tr>
	</table>
	</form>
	<?php
	}
function update_session_search_vals()
{
	global $HTTP_GET_VARS,$HTTP_POST_VARS,$languages_id,$search_params;
	reset($search_params);	
	while(list($key,)=each($search_params)){
	//echo $key ."*";
		if(isset($HTTP_POST_VARS[$key]))
			$search_params[$key]=tep_db_prepare_input($HTTP_POST_VARS[$key]);
		elseif(isset($HTTP_GET_VARS[$key]))
			$search_params[$key]=tep_db_prepare_input($HTTP_GET_VARS[$key]);
	}
}
  function search_reservation(){
	global $HTTP_GET_VARS,$HTTP_POST_VARS,$languages_id,$search_params;
	if($search_params['events_id']==0)
		$search_params['oID']=0;
	$orders_id=$search_params['oID'];	
	$customer_id=isset($HTTP_GET_VARS['cID'])?$HTTP_GET_VARS['cID']:'';
	$events_id=$search_params['events_id'];
    
		$rows=0;
		$temp_query=build_search_query();						
		if ($orders_id>0 && $events_id>0){ 
			$fields=",op.products_model as start_date,op.products_model as end_date ";
			$query=" and o.orders_id='" . $orders_id . "' and e.events_id='" . $events_id ."' order by op.products_model";
		} else {
			$fields=",min(op.products_model) as start_date,count(op.products_model) as total_count,max(op.products_model) as end_date,sum(ascii(events_type)) as refund_flag";
			$query=$temp_query . ' group by o.orders_id,o.customers_id,e.events_id  order by o.orders_id,op.products_model';
		}
		if ($search_params["tx_pending"]!="") {
			$cDate="curdate()";
			$pending_option=" and (o.orders_status>1 or e.events_pending_period=0 or date_add(o.date_purchased,interval e.events_pending_period day)>=" . $cDate . ")";
		}
		$search_query=	"select o.orders_id,op.products_name as events_name,op.orders_products_id,op.products_id,op.waitlist_type,op.events_type,o.customers_id,o.customers_name,o.customers_email_address,o.customers_postcode,e.events_location,e.events_instructor,e.events_id,e.events_group_bookings,op.products_quantity " . $fields . " from " . 
						TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " .   TABLE_EVENTS . " e " . 
						" where op.products_type='E' and  op.events_type!='' and op.events_type!='C'" .
						" and op.orders_id=o.orders_id and op.events_id=e.events_id " . $pending_option . $query;
		if ($orders_id<=0 || $events_id<=0){
			$reservations_split = new splitPageResultsEvent($search_params['booking_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 tep_draw_form('result_reservation',FILENAME_MOVE_RESERVATIONS_CONFIRM,'','post','onSubmit="return validateForm()"');
		//while(list($name,$val)=each($HTTP_POST_VARS)) if (in_array($name,$params)) echo "<input type='hidden' name='".$name."' id='".$name."' value='".$val."'>";
	?>
	<table width="100%" cellpadding="0" cellspacing="0" border="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">
					<?php echo '<a class="menuBoxContentLink" href="' . tep_href_link(FILENAME_SEARCH_RESERVATION_PRINT,'orders_id='.$orders_id.'&action=search') . '" onmouseover="change_image(1)" onmouseout="change_image(0)" target="_blank">' . '<img border=0 src="' . DIR_WS_IMAGES . "printimage.gif" . '" alt="' . IMAGE_PRINT_RESERVATIONS . '" title="' . IMAGE_PRINT_RESERVATIONS . '" id="print_image">' . TEXT_PRINT . '</a></span>';?>
					</td>
				</tr>
				</table>
				</td>
			</tr>
		<?php 
		
		if($cnt>0){
		?>
		<tr>
			<td valign="top">
				<table border="0" width='100%'cellspacing="1" cellpadding="2">
				<tr class='dataTableHeadingRow'><td class='dataTableHeadingContent'><?php echo TEXT_RESULT_OPTION; ?>&nbsp;</td>
					<td></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_POQ; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_EVENT; ?></td>
					<td class='dataTableHeadingContent'><?php echo TEXT_RESULT_POSTCODE; ?></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_RESULT_WAITLIST; ?></td>
					<td class='dataTableHeadingContent'><?php echo '<font color=green>W</font>'; ?></td>
				</tr>				  
				<?php
					//reset($search_params);
					//while(list($key,)=each($search_params))  echo '<input type="hidden" name="' . $key . '" value="' . htmlspecialchars($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;
						}
						$question_query=tep_db_query("SELECT answer_type,answer_value from " . TABLE_ORDERS_EVENTS_ANSWERS . " where orders_id='" . (int)$result["orders_id"] . "' and events_id='" . (int)$result["events_id"] . "'");
						$question_result=tep_db_fetch_array($question_query);
						if ($question_result["answer_type"]=="D" && $question_result["answer_value"]!="") $question_result["answer_value"]=format_date(date("Y-m-d",strtotime($question_result["answer_value"])));
				?>
				<tr class='dataTableRow' height=20>
					<td class='dataTableContent'>&nbsp;
					<?php  
					if ($result['total_count']==1){
						echo tep_draw_checkbox_field('reservation_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('reservation_id[]',$result['orders_id'] . '_' . $result['events_id'],false);
						echo '<input type="hidden" name="items_type[' . $result['orders_id'] . '_' . $result['events_id'] . ']" value="G">';
					}
					echo tep_draw_hidden_field('email_address',$result['customers_email_address']);
					echo tep_draw_hidden_field('letters_id',$result["orders_id"]);
					$customer_name=split(' ',$result['customers_name']);
					?>
					</td>
					<td class='dataTableContent'><?php echo '<span onClick="javascript:selectBox(' . $currow  . ',\'\',6);" class="cursorhand">' .  tep_image(DIR_WS_ICONS . '/mail.gif',IMAGE_MAIL_RESERVATION) . '</span>';?>
					<?php echo "<span onclick='document.location.href=\"" . tep_href_link(FILENAME_ORDERS, "oID=" . $result['orders_id'] . '&return=sr&page=1&action=edit&oid=' . $result['orders_id']) . "\"'>"; ?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo $customer_name[0]; ?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo $customer_name[1]; ?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo $question_result["answer_value"]; ?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo "<a href='".tep_href_link(FILENAME_ORDERS,'oID='.$result['orders_id'].'&return=sr&action=edit&oid='.$result['orders_id'].'&')."'>".$result['events_name']."</a>"; ?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo $result['customers_postcode']; ?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo format_date($result['start_date']); ?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo format_date($result['end_date']); ?></td><?php echo "</span>";?>
					<td class='dataTableContent' align='left'>
					<?php 
						$refund_flag="";
						if ($result["total_count"]>0 && (int)$result["refund_flag"]>0) {
							if (intval($result["refund_flag"]/$result["total_count"])==ord("F")){
								$refund_flag="F";
							} else if (intval($result["refund_flag"]/$result["total_count"])==ord("T")){
								$refund_flag="T";
							}
						}
						if ($result['events_group_bookings']=='Y' && $result['total_count']==1 && $result['products_quantity']==1 || $result['events_group_bookings']=='N' && $result['total_count']==1) {
							if ($result['events_type'] == 'R' || $result['events_type'] == 'W') {
								if ($result['events_type']=='W'){
									if ($result['waitlist_type']=='V') $display_text=TEXT_VIP_GROUP;
									if ($result['waitlist_type']=='F') $display_text=TEXT_FIRSTCOME_GROUP;
									if ($result['waitlist_type']=='C') $display_text=TEXT_CUSTOM_GROUP;
									echo tep_image(DIR_WS_IMAGES . 'icon_status_red.gif', $display_text, 10, 10) . '&nbsp;&nbsp;<a href="javascript: setflag(\'R\','.$result['orders_products_id'].','.$result['products_id'].','.$result[events_id].');">' . tep_image(DIR_WS_IMAGES . 'icon_status_green_light.gif', IMAGE_ICON_MOVE_RESERVATION, 10, 10) . '</a>';
								} else {
									echo '<a href="javascript: setflag(\'w\','.$result['orders_products_id'].','.$result['products_id'].','.$result[events_id].');">' . tep_image(DIR_WS_IMAGES . 'icon_status_red_light.gif', IMAGE_ICON_MOVE_WAIT_LIST, 10, 10) . '</a>&nbsp;&nbsp;' . tep_image(DIR_WS_IMAGES . 'icon_status_green.gif', IMAGE_ICON_STATUS_RESERVED, 10, 10);
								}
							}
						} else if($orders_id<=0){
							$waitlist_query=tep_db_query("SELECT count(op.orders_products_id) as total from " . TABLE_ORDERS_PRODUCTS . " op where op.orders_id='" . $result["orders_id"] . "' and op.products_type='E' and op.events_type='W' and op.events_id='" . $result["events_id"] . "'");
							$waitlist=tep_db_fetch_array($waitlist_query);
							if ($waitlist["total"]>0){
								echo tep_image(DIR_WS_IMAGES . 'icon_status_red.gif', $display_text, 10, 10) . '&nbsp;&nbsp;<a href="javascript: move_waitlist('.$result['orders_id'].','.$result[events_id].');">' . tep_image(DIR_WS_IMAGES . 'icon_status_green_light.gif', IMAGE_ICON_MOVE_RESERVATION, 10, 10) . '</a>&nbsp;';
							}
							if($result['events_group_bookings']=='Y' && $result['products_quantity']>1){								
								echo '<a href="javascript:search_reservation('.$result['orders_id'].','.$result['events_id'].')">'. tep_image(DIR_WS_IMAGES . 'multi_booking.gif',IMAGE_MODIFY_GROUP) . '</a>';
								//echo '<a href="' . tep_href_link(FILENAME_ORDERS,'oID='.$result['orders_id'].'&return=sr&page=1&action=edit&oid='.$result['orders_id']) . '">' . tep_image(DIR_WS_IMAGES . 'multi_booking.gif',IMAGE_MODIFY_GROUP) . '</a>';
							}else
								echo '<a href="javascript:search_reservation('.$result['orders_id'].','.$result['events_id'].')">' . tep_image(DIR_WS_IMAGES . 'reserve_view.gif',IMAGE_MODIFY_RESERVE) . '</a>';
								//echo '<a href="' . tep_href_link(FILENAME_SEARCH_RESERVATIONS,'oID='.$result['orders_id'].'&events_id='.$result['events_id']) . '">' . tep_image(DIR_WS_IMAGES . 'reserve_view.gif',IMAGE_MODIFY_RESERVE) . '</a>';						    
						}
					?></td>
					<td class="dataTableContent" align="left" valign=middle>
					<?php 
						if ($result['total_count']==1) {
							if ($result['events_type']=="T") 
								echo tep_image(DIR_WS_IMAGES . 'checked.gif',IMAGE_ICON_WITHDRAW);
							else if ($result['events_type']!="F")
								echo '<span class="cursorhand" onClick="javascript:selectBox(\'' . $currow . '\',\'single\',4)">' . tep_image(DIR_WS_IMAGES . 'unchecked.gif',IMAGE_ICON_NOT_WITHDRAW) . '</span></a>';
						} else { 
							if ($refund_flag=='T'){
								echo tep_image(DIR_WS_IMAGES . 'checked.gif',IMAGE_ICON_WITHDRAW);
							} else if ($refund_flag!="F"){
								echo '<span class="cursorhand" onClick="javascript:selectBox(\'' . $currow . '\',\'group\',4)">' . tep_image(DIR_WS_IMAGES . 'unchecked.gif',IMAGE_ICON_NOT_WITHDRAW) . '</span></a>';
							}
						}
					?>
					</td>
			      <!-- <td class="dataTableContent" align="center" valign=middle>
					<?php 
						if ($result['total_count']==1) {
							if ($result['events_type']=="F")
								echo tep_image(DIR_WS_IMAGES . 'checked.gif',IMAGE_ICON_REFUND);
							else if ($result['events_type']!="F")
								echo '<span class="cursorhand" onClick="javascript:selectrefund(\'' . $result['orders_id'] . '\')">' . tep_image(DIR_WS_IMAGES . 'unchecked.gif',IMAGE_ICON_NOT_REFUND) . '</span></a>';
								//echo '<span class="cursorhand" onClick="javascript:selectBox(\'' . $currow . '\',\'single\',5)">' . tep_image(DIR_WS_IMAGES . 'unchecked.gif',IMAGE_ICON_NOT_REFUND) . '</span></a>';
						} else {
							if ($refund_flag=="F")
								echo tep_image(DIR_WS_IMAGES . 'checked.gif',IMAGE_ICON_REFUND);
							else
								echo '<span class="cursorhand" onClick="javascript:selectrefund(\'' . $result['orders_id'] . '\')">' . tep_image(DIR_WS_IMAGES . 'unchecked.gif',IMAGE_ICON_NOT_REFUND) . '</span></a>';
								//echo '<span class="cursorhand" onClick="javascript:selectBox(\'' . $currow . '\',\'group\',5)">' . tep_image(DIR_WS_IMAGES . 'unchecked.gif',IMAGE_ICON_NOT_REFUND) . '</span></a>';
						}
					?>
	    			</td>-->
			</tr></span>
			<?php } ?>
       </table>
	   </td>
	   </tr> 

	<?php
		}else {
		echo '<tr><td align="left" class="main" colspan="8">'. TEXT_NO_RESULTS.'</td></tr>';
		}
	?>
	<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: events_booking_back();">' . tep_image_button('button_back.gif', IMAGE_BACK,'align=absmiddle') . '</a>&nbsp;&nbsp;<span id="back_ajax_load"></span>';
							else
								echo '<a href="javascript: events_booking_back();">' . tep_image_button('button_back.gif', IMAGE_BACK,'align=absmiddle') . '</a>&nbsp;&nbsp;<span id="back_ajax_load"></span>';

						 } else if($orders_id){
							echo '<a href="javascript:search_reservation(0,0);">'. tep_image_button('button_back.gif',IMAGE_BACK,'align=absmiddle') . '</a>';
						}
					?>
					</td>	
                        <td align="right" valign="middle">

					<?php 
						if ($cnt>0) {
							if ($orders_id>0) {
								echo '<span class=cursorhand>' . tep_image_button('button_withdraw.gif',IMAGE_WITHDRAW_RESERVATIONS,'onClick="javascript:doFormAction(3);" align="absmiddle"') . '</span>&nbsp;';
								//echo '<span class=cursorhand>' . tep_image_button('button_refunds.gif',IMAGE_REFUND_RESERVATIONS,'onClick="javascript:doFormAction(2);" align="absmiddle"') . '</span>&nbsp;';
								echo '<span class=cursorhand>' . tep_image_button('button_refunds.gif',IMAGE_REFUND_RESERVATIONS,'onClick="javascript:selectrefund(' . $orders_id. ');" align="absmiddle"') . '</span>&nbsp;';
							}
							$all_events_array=array();
							$all_events_array = tep_get_events_array_single();
							if (sizeof($all_events_array)>0){
								echo '<a href="javascript: validateForm();">'.tep_image_button('button_move_to.gif', IMAGE_MOVE, "align='absmiddle'") . '</a>&nbsp;';
								echo tep_draw_event_select_menu('new_events_id',$all_events_array,'','',false,0,false);
							}
							echo tep_draw_hidden_field('search_ids','');
							echo '<a href="javascript: validateLetterForm();">'.tep_image_button('button_letter.gif',IMAGE_ADMIN_LETTERS,"align='absmiddle'") .'</a>&nbsp;';
							echo '<a href="javascript: validateEmailForm(1);">'.tep_image_button('button_email.gif',IMAGE_SEARCH_EMAIL,"align='absmiddle'") . '</a>&nbsp;';
							//reset($search_params);
							//while(list($key,)=each($search_params))  echo '<input type="hidden" name="' . $key . '" value="' . htmlspecialchars($search_params[$key]) . '">';
							echo '<a href="javascript: validateEmailForm(2);">'.tep_image_button('button_sms.gif',IMAGE_SEARCH_EMAIL,"align='absmiddle'").'</a>';
							//reset($search_params);
							//while(list($key,)=each($search_params)) echo '<input type="hidden" name="' . $key . '" value="' . htmlspecialchars($search_params[$key]) . '">';
				     	}
					?>

					</td>
				</tr>
			</table>
		</td>
		</tr>

	<input type="hidden" name="row_check" value="<?php echo tep_output_string($rows); ?>">
	<?php if (!($orders_id>0 && $events_id>0)) {?>
	<tr>
	<td><table style="padding:10px;" width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr>
	<td class="smallText"><?php echo $reservations_split->display_count($search_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $search_params['booking_page'], TEXT_DISPLAY_NUMBER_OF_RESERVATIONS); ?></td>
	<td class="smallText" align="right"><?php echo $reservations_split->display_script_links($search_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $search_params['booking_page'],1,'bookings_page_navigation(\'\'','1'); ?>&nbsp;</td>
	</tr>
	</table></td>
	<?php } ?>
	<?php
	echo '</table></form>';
	}
	function withdraw_process(){
	global $HTTP_POST_VARS,$search_params;
	$reserve_ids=isset($HTTP_POST_VARS["reservation_id"])?$HTTP_POST_VARS["reservation_id"]:array();
	$type=(isset($HTTP_POST_VARS['process_type']) ? $HTTP_POST_VARS['process_type'] : 'single');
	$orders_id=(isset($HTTP_POST_VARS['oID']) ? (int)$HTTP_POST_VARS['oID'] : 0);
	$events_id=(isset($HTTP_POST_VARS['eID']) ? (int)$HTTP_POST_VARS['eID'] : 0);
	
	// if no reservation ids found just return to search reservation page
	if (sizeof($reserve_ids)<=0) {
		//tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS));
		return;
	}

	$select_string=" op.orders_products_id in (" . join(",",$reserve_ids) . ") ";
	$session_query="SELECT o.orders_id,op.orders_id,o.customers_id,op.products_id,op.orders_products_id,op.products_model as start_date from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o where " . $select_string . " and o.orders_id=op.orders_id";
	
	$session_fetch=tep_db_query($session_query);
	
	$currencies=new currencies();
	$mail_order=array();
	while ($session_detail=tep_db_fetch_array($session_fetch)){
	    tep_db_query("update " . TABLE_ORDERS_PRODUCTS . " set events_type='T' where orders_products_id='" . (int)$session_detail["orders_products_id"] . "'");
		if ($type=='single') {
			if (!isset($mail_order[$session_detail["orders_id"]])) $mail_order[$session_detail["orders_id"]]=array();
			$mail_order[$session_detail["orders_id"]][]=$session_detail["orders_products_id"];
		}
   	}
	if ($type=="group"){
		tep_send_event_group_email($orders_id,$events_id,'WTR','T');
	} else {
		if (sizeof($mail_order)>0){
			reset($mail_order);
			while(list($oID,)=each($mail_order)){
				if (sizeof($mail_order[$oID])>0){
					$id_string=join("','",$mail_order[$oID]);
					tep_send_event_email($id_string,'WTR');
				}
			}
		}
	}
	return TEXT_WITHDRAW_SUCCESS;
	}
	
	function withdraw_confirmation(){
	global $HTTP_GET_VARS,$HTTP_POST_VARS,$language,$search_params;
	require(DIR_WS_LANGUAGES.$language.'/'.FILENAME_WITHDRAW_RESERVATIONS);
	

	$action = (isset($HTTP_GET_VARS['action']) ? tep_db_prepare_input($HTTP_GET_VARS['action']) : '');
	$reserve_ids=array();
	
	$reserve_ids=isset($HTTP_POST_VARS["reservation_id"])?$HTTP_POST_VARS["reservation_id"]:array();
	
	$type=(isset($HTTP_POST_VARS['process_type']) ? $HTTP_POST_VARS['process_type'] : 'single');
	// if no reservation ids found just return to search reservation page
	
	if (sizeof($reserve_ids)<=0) {
		//tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS));
		search_reservation();
		return;
	}
	if ($type=='single') { // if indivitual event items selected

		$select_string=" op.orders_products_id in (" . join(",",$reserve_ids) . ") ";
		$customer_query="select o.orders_id,o.customers_id,o.customers_name,o.customers_street_address,o.customers_suburb, " .
						" o.customers_postcode,o.customers_city,o.customers_state,o.customers_country from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o " . 
						" where " . $select_string . " and op.orders_id=o.orders_id  group by o.customers_id ";
		$customer_fetch=tep_db_query($customer_query);
		$dates_query="SELECT op.orders_products_id,op.products_model as start_date from " . TABLE_ORDERS_PRODUCTS . " op where " . $select_string . " and op.products_type='E' and op.events_type in('R','W') order by op.products_model";
		$dates_fetch=tep_db_query($dates_query);
	} else { // if a single order is selected

		$split_details=split("_",$reserve_ids[0]);
		$orders_id=0;
		$events_id=0;

		if (sizeof($split_details)>1){
			$orders_id=(int)$split_details[0];
			$events_id=(int)$split_details[1];
		}
		$customer_query="select o.orders_id,o.customers_id,o.customers_name,o.customers_street_address,o.customers_suburb, " .
						" o.customers_postcode,o.customers_city,o.customers_state,o.customers_country from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o " . 
						" where op.orders_id='" . (int)$orders_id . "' and op.orders_id=o.orders_id  group by o.customers_id ";

		$customer_fetch=tep_db_query($customer_query);

		$dates_query="SELECT op.orders_products_id,op.products_model as start_date from " . TABLE_ORDERS_PRODUCTS . " op where op.orders_id='" . (int)$orders_id . "' and op.events_id='" . (int)$events_id . "' and op.products_type='E' and op.events_type in('R','W') order by op.products_model";
		$dates_fetch=tep_db_query($dates_query);
	}	
	$customer_cnt=tep_db_num_rows($customer_fetch);
	$dates_cnt=tep_db_num_rows($dates_fetch);
	echo tep_draw_form('withdraw_reservation',FILENAME_WITHDRAW_PROCESS,'');	
	?>
	<!-- 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">	
	<?php 
	?>		
		<tr>
			<td>
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td align=left class='pageHeading' colspan='2'><?php echo HEADING_WITHDRAW_TITLE ?></td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<tr>
			<td>
			<table border="0" cellspacing="4" cellpadding="2" style="background-color: #F9FAFF; border-color: #7b9ebd; border-style: solid; border-width: 1px;">
			<?php 
				if ($customer_cnt>0) { // if customer has enough details
					$customer=tep_db_fetch_array($customer_fetch);
					$customer_name=split(' ',$customer['customers_name']);
					//echo $search_params;
					echo '<input type="hidden" name="process_type" value="' . tep_output_string($type) . '">';
					if ($type=="group"){
						echo '<input type="hidden" name="oID" value="' . tep_output_string($orders_id) . '">';
						echo '<input type="hidden" name="eID" value="' . tep_output_string($events_id) . '">';
					}
			?>
				<tr align="left">
					<td class="tableHeading"><?php echo TEXT_WITHDRAW_FIRST_NAME ?></td>
					<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer_name[0]; ?></td>
				</tr>
				<tr align="left">
					<td class="tableHeading"><?php echo TEXT_WITHDRAW_LAST_NAME; ?></td>
					<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer_name[1]; ?></td>
				</tr>
				<tr>
					<td class="tableHeading"><?php echo TEXT_WITHDRAW_ADDRESS; ?></td>
					<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_street_address']; ?></td>
				</tr> 
				<tr>
					<td class="tableHeading"><?php echo TEXT_WITHDRAW_SUBRUB; ?></td>
					<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_suburb'];?></td>
				</tr>
				<tr>
					<td class="tableHeading"><?php echo TEXT_WITHDRAW_POSTCODE; ?></td>
					<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_postcode']; ?></td>
				</tr>
				<tr>
					<td class="tableHeading"><?php echo TEXT_WITHDRAW_CITY; ?></td>
					<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_city']; ?></td>
				</tr>
				<tr>
					<td class="tableHeading"  valign="top"><?php echo TEXT_WITHDRAW_STATE; ?></td>
					<td class="main" valign="top"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_state']; ?></td>
				</tr>
				<tr>
					<td class="tableHeading"  valign="top"><?php echo TEXT_WITHDRAW_COUNTRY; ?></td>
					<td class="main"  valign="top"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $customer['customers_country']; ?></td>
				</tr>
				<tr>
					<td class="tableHeading" valign="top"><?php echo TEXT_WITHDRAW_DATES; ?></td>
					<td class="main" nowrap>
					<?php 
						echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;';
						if ($dates_cnt>0){ // if dates found
							$icnt=1;
							$date_string="";
							while($dates=tep_db_fetch_array($dates_fetch)){
								$date_string.=format_date($dates["start_date"]) . ", ";
								echo '<input type="hidden" name="reservation_id[]" value="' . tep_output_string($dates["orders_products_id"]) . '">';
								$icnt++;
								if ($icnt%4==0) $date_string.='<br>' . tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;';
							}
							if (substr($date_string,-2)==", ") $date_string=substr($date_string,0,-2);
							echo $date_string;
						} else {
							echo TEXT_NO_WITHDRAW_DATES;
						}
					?>
					</td>
				</tr>
				<?php echo tep_get_attendees($customer['orders_id'],$customer['customers_id']);
				
				} else {  // if customer_cnt>0?>
					<tr>
						<td align=left class='tableHeading' colspan="2"><?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=right colspan="2">
						<span onClick="javascript:withdraw_confirm_back()" class="cursorhand">
							<?php echo tep_image_button('button_back.gif', IMAGE_BACK); ?>
						</span>&nbsp;<span id="confirm_back_ajax_load"></span>
					<?php if ($customer_cnt>0 && $dates_cnt>0) echo '<a href="javascript: withdraw_process();">'.tep_image_button('button_withdraw.gif', IMAGE_WITHDRAW).'</a>&nbsp;<span id="withdraw_ajax_load"></span>'; ?></td>
				</tr>
				</table>
				
				</td>
			</tr>
		</table>
		</td>
	</tr>
</table></form>
<!-- body_text_eof //-->
	<?php 
	}
	function events_booking_search(){	
	global $search_params;
	echo tep_draw_form('search_reservation',FILENAME_SEARCH_RESERVATIONS,'','get','onSubmit="return validateSearchForm(this);"');
			$login_option="";
			if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY){
				$login_option=" and e.events_instructor='" . $login_id . "'";
			}
			$event_query=tep_db_query("SELECT e.events_id,ed.events_name from " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed where ed.language_id='" . (int)$languages_id . "' " . $login_option . EVENTS_CONDITION_STATUS . " and e.events_id=ed.events_id order by ed.events_name");
			$events_array[]=array('id'=>'','text'=>'');
			while ($result=tep_db_fetch_array($event_query)){
				$events_array[]=array('id'=>$result['events_id'],'text'=>$result['events_name']);
			}	 
			$firstname="";$lastname="";$keywords="";$postcode="";$searchfrom="";$searchuntil="";$searchevent="";
			$showstartend="";$showcurrent="";$showreserve="";$showwaitlist="";$hidepending="";	    
			if(tep_session_is_registered('search_params'))
			{
				$firstname=$search_params["tx_fname"];
				$lastname=$search_params["tx_lname"];
				$keywords=$search_params["tx_words"];
				$postcode=$search_params["tx_code"];
				$searchfrom=$search_params["tx_from"];
				$searchuntil=$search_params["tx_until"];
				$searchevent=$search_params["tx_event"];
				$showstartend=$search_params["tx_dated"];
				$showcurrent=$search_params["tx_current"];
				$showreserve=$search_params["tx_reserve"];
				$showwaitlist=$search_params["tx_waitlist"];
				$hidepending=$search_params["tx_pending"];	    
		}
   
		?>
		<table width="100%" cellpadding="0" cellspacing="0" border="0">
		<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',$firstname,'size=35');?></td>
			</tr>
			<tr>
				<td class="main"><?php echo TEXT_SEARCH_LASTNAME ?></td>
				<td class="main"><?php echo tep_draw_input_field('tx_lname',$lastname,'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',$keywords,'size=20') ?></td>
			</tr> 
			<tr>
				<td class="main"><?php echo TEXT_SEARCH_POSTCODE ?></td>
				<td class="main"><?php echo tep_draw_input_field('tx_code',$postcode,'size=20') ?></td>
			</tr>
			<?php $_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,EVENTS_DATE_FORMAT);?>
			<tr>
				<td class="main"><?php echo TEXT_SEARCH_FROM ?><br><small><?php echo "(" . $date_format . ")";?></small></td>
				<td class="main"><?php echo tep_draw_input_field("tx_from",$searchfrom,"size=10",false,'text',false);?>
				<a href="javascript:show_calendar('search_reservation.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>
			</tr> 
			<tr>
				<td class="main"><?php echo TEXT_SEARCH_UNTIL ?><br><small><?php echo "(" . $date_format . ")";?></small></td>
				<td class="main"><?php echo tep_draw_input_field("tx_until",$searchuntil,"size=10",false,'text',false);?>
				<a href="javascript:show_calendar('search_reservation.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>
			</tr>
			<tr>
				<td class="main"><?php echo TEXT_SEARCH_EVENT ?></td>
				<td class="main">
				<?php 
					$all_events_array=array();
					$all_events_array = tep_get_events_array_single();
					if(sizeof($all_events_array)<=0) echo 'Events are not Available';
					else echo tep_draw_event_select_menu('tx_event',$all_events_array,$searchevent);
				?>
				</td>
			</tr>
			<tr>
				<td class="main">&nbsp;</td>
				<td class="main" valign="middle"><?php echo tep_draw_checkbox_field('tx_dated','Y',($showstartend=='Y')) . TEXT_SEARCH_START_END; ?>&nbsp;
				<span onMouseOver="javascript:showpopup('start-end')" onMouseOut="javascript:kill();"><?php echo tep_image(DIR_WS_ICONS . "help_icon.gif");?></span>
				</td>
			</tr>
			<tr>
				<td class="main">&nbsp;</td>
				<td class="main"><?php echo tep_draw_checkbox_field('tx_current','Y',($showcurrent=='Y')) . TEXT_SEARCH_ONLYCURRENT; ?>
				<span onMouseOver="javascript:showpopup('current')" onMouseOut="javascript:kill();"><?php echo tep_image(DIR_WS_ICONS . "help_icon.gif");?></span>
				</td>
			</tr>
			<tr>
				<td class="main">&nbsp;</td>
				<td class="main"><?php echo tep_draw_checkbox_field('tx_reserve','Y',($showreserve=='Y')) . TEXT_SEARCH_RESERVED; ?>
				<span onMouseOver="javascript:showpopup('reserved')" onMouseOut="javascript:kill();"><?php echo tep_image(DIR_WS_ICONS . "help_icon.gif");?></span>
				</td>
			</tr>
			<tr>
				<td class="main">&nbsp;</td>
				<td class="main"><?php echo tep_draw_checkbox_field('tx_waitlist','Y',($showwaitlist=='Y')) . TEXT_SEARCH_WAITLIST; ?>
				<span onMouseOver="javascript:showpopup('waitlist')" onMouseOut="javascript:kill();"><?php echo tep_image(DIR_WS_ICONS . "help_icon.gif");?></span>
				</td>
			</tr>
			<tr>
				<td class="main">&nbsp;</td>
				<td class="main"><?php echo tep_draw_checkbox_field('tx_pending','Y',($hidepending=='Y')) . TEXT_SEARCH_PENDING_PERIOD; ?>
				<span onMouseOver="javascript:showpopup('pending')" onMouseOut="javascript:kill();"><?php echo tep_image(DIR_WS_ICONS . "help_icon.gif");?></span>
				</td>
			</tr>
			
            <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10') . tep_draw_hidden_field('firstaction','search'); ?></td></tr>
            <tr>
				<td colspan=2 align=right>
				<?php echo '<a href="javascript: reset_search();">'.tep_image_button('button_reset.gif', IMAGE_SEARCH_RESERVATION).'</a>&nbsp;&nbsp;&nbsp;';
				echo '<a href="javascript: search_reservation_first();">'.tep_image_button('button_report_search.gif', IMAGE_SEARCH_RESERVATION).'</a>&nbsp;<span id="search_ajax_load"></span>'; ?></td>
			</tr>
		</table>
		</td>
		</tr>
		</table>
		</form>
	<?php } ?>
	<?php 
	function events_move_reservations_confirm(){
	global $HTTP_POST_VARS,$languages_id,$HTTP_SESSION_VARS,$HTTP_GET_VARS,$language;
	require(DIR_WS_LANGUAGES.$language.'/'.FILENAME_MOVE_RESERVATIONS_CONFIRM);
	
	$err_array=array();
     $reserve_ids=(isset($HTTP_POST_VARS['reservation_id'])?$HTTP_POST_VARS['reservation_id']:'');
	 $success_ids=(isset($HTTP_POST_VARS['moved_ids'])?$HTTP_POST_VARS['moved_ids']:array());
	 $failure_ids=(isset($HTTP_POST_VARS['unmoved_ids'])?$HTTP_POST_VARS['unmoved_ids']:array());
	 $new_session=(isset($HTTP_POST_VARS['new_session'])?$HTTP_POST_VARS['new_session']:array());
	 $type=(isset($HTTP_POST_VARS['items_type'])?$HTTP_POST_VARS['items_type']:'');

//	if (sizeof($reserve_ids)<=0 || sizeof($type)<=0) tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS));
	
	$select_string_order="";
	$select_string_single="";
	for ($icnt=0;$icnt<sizeof($reserve_ids);$icnt++){
		$reserve_id=$reserve_ids[$icnt];
		if (isset($type[$reserve_id]) && $type[$reserve_id]=="G"){
			$split_array=split("_",$reserve_id);
			if (sizeof($split_array)>1) $select_string_order.=" (op.orders_id='" . $split_array[0] . "' and op.events_id='" . $split_array[1] . "') or ";
		} else {
			$select_string_single.="'" . $reserve_id . "',";
		}
	}
	$query=$query_part_1=$query_part_2="";
	
	if (strlen($select_string_order)>4){
		$select_string_order=substr($select_string_order,0,-4);
		$query_part_1="select o.orders_id,op.orders_products_id,op.products_id,o.customers_id,o.customers_name,o.customers_email_address,op.final_price,op.events_type,op.products_name,
		     		    op.products_id as sessions_id,op.products_model as start_date,op.products_tax,op.events_id from " . 
        	            TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op " . 
		    			"where o.orders_id=op.orders_id and op.products_type='E' and op.events_type in ('R','W') and (" . $select_string_order . ")";

	}
	if (strlen($select_string_single)>2){
		$select_string_single=substr($select_string_single,0,-1);
		$query_part_2="select o.orders_id,op.orders_products_id,op.products_id,o.customers_id,o.customers_name,o.customers_email_address,op.final_price,op.events_type,op.products_name,
		     		    op.products_id as sessions_id,op.products_model as start_date,op.products_tax,op.events_id from " . 
        	            TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op " .
		    			"where o.orders_id=op.orders_id and op.products_type='E' and op.events_type in ('R','W') and op.orders_products_id in (" . $select_string_single . ")";
	}

     if ($query_part_1!='' && $query_part_2!='')
        $query=$query_part_1 . ' union ' . $query_part_2;
     else if($query_part_1!='')
        $query=$query_part_1;
     else if($query_part_2!='')
        $query=$query_part_2;

     $query=$query . " order by events_id,start_date,customers_name";
	 

	$result_query=tep_db_query($query);
	
	$new_events_id=isset($HTTP_POST_VARS['new_events_id'])?(int)$HTTP_POST_VARS['new_events_id']:0;
	
	$new_event_query=tep_db_query("select e.session_style,e.session_gaps,e.session_reduction,e.events_fees,e.waitfee_vip,e.events_forward_period,e.events_forward_length,ed.events_name from " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed where e.events_id=ed.events_id and e.events_id='" . (int)$new_events_id . "' and ed.language_id='" . (int)$languages_id . "'");


	$new_event_result=tep_db_fetch_array($new_event_query);



	if ($new_event_result['events_forward_period']=='Y'){
		$check_date=date('Y-m-d',tep_dateadd(time(),'day',(int)$new_event_result['events_forward_length']));
		$check_data=" and start_date<='" . $check_date . "' ";
	}	 

	/*$params=array("tx_fname","tx_lname","page","tx_words","tx_code",
							"tx_from","tx_until","tx_event","tx_reserve","tx_waitlist","tx_rtype","tx_session","tx_current","orders_id","events_id","tx_pending");


	$search_post_params=tep_prepare_hidden_params($params,"post","post",array());
	$search_get_params=tep_prepare_hidden_params($params,"post","get",array());*/
		
	// get new session dates
	$session_query=tep_db_query("select sessions_id,start_date from " . TABLE_EVENTS_SESSIONS . " where events_id='" . (int)$new_events_id . "' and start_date>current_date and sessions_type!='' " . $check_data . " order by start_date");
	
	$sessions_array=array();
	$find_array=array();
	while ($session_details = tep_db_fetch_array($session_query)) {
		$sessions_array[] = array(	'id' => $session_details['sessions_id'],
									'text' => format_date($session_details['start_date']));
		$find_array[$session_details['start_date']]=$session_details['sessions_id'];
		$rows++;
	}
	$move_elements=array();
	$icnt=0;
	while($result=tep_db_fetch_array($result_query)){
		$current_event=$result["events_id"];
		if (!isset($move_elements[$current_event])) {
			$move_elements[$current_event]=array();
			$move_elements[$current_event]['status']=1;
			$move_elements[$current_event]['dates']=array();
			$old_event_query=tep_db_query("select e.session_style,e.session_gaps,e.session_reduction,e.events_fees,e.waitfee_vip from " . TABLE_EVENTS . " e where e.events_id='" . $current_event . "'");
			$old_event_result=tep_db_fetch_array($old_event_query);
			$move_elements[$current_event]['name']=$result["products_name"];
			if ($old_event_result["session_style"]!=$new_event_result["session_style"] || $old_event_result["session_gaps"]!=$new_event_result["session_gaps"] || $old_event_result["events_fees"]!=$new_event_result["events_fees"] || $old_event_result["waitfee_vip"]!=$new_event_result["waitfee_vip"])
				$move_elements[$current_event]['status']=0;
		}
		$current_date=$result["start_date"];
		if (!isset($move_elements[$current_event]['dates'][$current_date])) {
			$move_elements[$current_event]['dates'][$current_date]=array();
		}
		if (!isset($success_ids[$result["orders_products_id"]])){
			
			$move_elements[$current_event]['dates'][$current_date][]=array('id'=>$result["orders_products_id"],'name'=>$result["customers_name"]);
			//echo sizeof($move_elements[$current_event]['dates'][$current_date]);
		}
	}
	echo tep_draw_form('move_reservation_confirm',FILENAME_MOVE_RESERVATIONS_PROCESS);
	?>

	<!-- 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>
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td align=left class='pageHeading' colspan='2'><?php echo sprintf(HEADING_EVENT_TITLE,$new_event_result["events_name"]); ?></td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>	  
		<?php 
			if (tep_db_num_rows($session_query)>0) {
		  		if (sizeof($failure_ids)>0) {
		?>
					<tr>
						<td>
						<table border="0" width="100%" class="dataTableContent">
							<tr>
								<td><?php echo '<font color=red>' . TEXT_MOVE_ERROR . '</font>'; ?></td>
							</tr>
						</table>
						</td>
					</tr>
		<?php   }

					echo '<input type="hidden" name="new_events_id" value="' . tep_output_string($new_events_id) . '">';
					echo $search_post_params;
					for ($icnt=0;$icnt<sizeof($reserve_ids);$icnt++){
						echo '<input type="hidden" name="reservation_id[]" value="' . tep_output_string($reserve_ids[$icnt]) . '">';
					}
					if (sizeof($type)>0){
						reset($type);
						while(list($id,$value)=each($type)){
							echo '<input type="hidden" name="items_type[' . $id . ']" value="' . tep_output_string($value) . '">';
						}
					}
					if (sizeof($success_ids)>0){
						reset($success_ids);
						while(list($id,)=each($success_ids)){
							echo '<input type="hidden" name="moved_ids[' . $id . ']" value="1">';
						}
					}
		?>
					<tr>
					<td>
						<table border="0" width=100% cellspacing="" cellpadding="2">
							<tr align="left" class="attributes-odd">
								<td>
									<table border="0" cellpadding="2" cellspacing="0" width="100%"> 
										<tr>
											<td class="tableHeading" align="left" width=100><?php echo TEXT_DATE ?></td>
											<td class="tableHeading" align="left"><?php echo TEXT_MEMBERS; ?></td>
											<td class="tableHeading" align="left" width=300><?php echo TEXT_SELECT_DATE; ?>
										</tr>
									</table>
								</td>
							</tr>
							<tr>
								<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5');?></td>
							</tr>
							<tr>
								<td>
								<table border="0" width="100%" cellpadding="2">
								<?php
								$row_count=0;
								$event_cnt=0;
								//iterate using each of the events
								reset($move_elements);
								while(list($events_id,$events)=each($move_elements)){
									if (sizeof($events['dates'])>0){
										echo '<tr class="attributes-even"><td class="tableHeading" valign="top">' . sprintf(TEXT_EVENT_FROM,$events['name']);
										if ($events['status']==0) echo '<br><span class="errorText">' . TEXT_EVENT_MISMATCH . '</span>';
										echo '</td></tr>' . "\n";
										echo '<tr class="attributes-odd"><td><table border="0" width="100%" cellpadding="2">'  . "\n";
										while(list($start_date,$session)=each($events['dates'])){
											if (sizeof($session)>0){
												echo 	'<tr><td class="tableHeading" width=100 valign="top">' . format_date($start_date) . '</td>'  . "\n" . 
														'<td valign="top"><table border="0" width="100%" cellpadding="0">'  . "\n";
												
												for ($icnt=0;$icnt<sizeof($session);$icnt++){
													echo 	'<tr><td class="main" valign="top">' . $session[$icnt]['name']  . "\n";
													if ($events['status']==1){
														echo	'<input type="hidden" name="move_elements[]" value="' . tep_output_string($session[$icnt]['id']) . '">'  . "\n" . 
																'<input type="hidden" name="new_session_index[]" value="' . tep_output_string($event_cnt) . '">' . "\n" . 
																'</td></tr>' . "\n";
														if (isset($failure_ids[$session[$icnt]['id']])){
															echo '<tr><td class="smallText">' . get_err_type($failure_ids[$session[$icnt]['id']]) . "</td></tr>";
														}
													}
												}
												echo '</table></td>' . "\n";
												echo '<td valign="middle" width=300>';
												echo '<table border="0" width="100%" cellpadding="2">' . "\n";
												if ($events['status']==1){
													echo '<tr><td class="main" align="left">' . tep_draw_pull_down_menu('new_session_id[' . $event_cnt . ']',$sessions_array,get_selected_date($event_cnt,$start_date));
													echo '</td></tr>' . "\n";
													$event_cnt++;
												}
												echo '</table>';
												echo '</td></tr>' . "\n";
												
											}
										}
										echo '</table></td></tr>' . "\n";
									}
								}
								echo '<tr><td>' . tep_draw_separator('pixel_trans.gif', '1', '5') . '</td></tr>' . "\n";
								echo '<tr><td class="main">' . tep_draw_checkbox_field('process_wait','Y') . TEXT_CHECK_WAITLIST . '</td></tr>' . "\n";
								echo '</table></td></tr></table></td></tr>' . "\n";
								echo '<tr><td>' . tep_draw_separator('pixel_trans.gif', '1', '5') . '</td></tr>' . "\n";
								echo '<tr><td align=left class="main">';
								echo '<a href="javascript: reservations_confirm_back(\''.$search_get_params.'\');">' . tep_image_button('button_back.gif',IMAGE_BACK,'align=absmiddle') . '</a>&nbsp;&nbsp;<span id="back_ajax_load"></span>' . "\n";
								if ($event_cnt>0) echo '<a href="javascript: move_reservation_confirm();">'.tep_image_button('button_move.gif',IMAGE_BUTTON_MOVE,'align=absmiddle') . "</a>&nbsp;<span id='move_ajax_load'></span>\n";
								?>
          </table></td>
	  
  	 </tr>
	 <?php } else { ?>
		<tr>
			<td class=main><?php echo '<font color=red>' . TEXT_NO_SESSIONS . '</font><p>'; ?></td>
		</tr>
		<tr> 
			<td class="main"><?php echo '<a href="javascript: reservations_confirm_back(\''.$search_get_params.'\');">' . tep_image_button('button_back.gif',IMAGE_BACK) . '</a>&nbsp;<span id="back_ajax_load"></span>'; ?>
			</td>
		 </tr>
	 <?php } ?>
      </table>
      </td>
      
	</tr>
	</table>
	</form>
	<!-- body_text_eof //-->
	<?php
	}
	
	function events_move_reservations_process(){
	global $HTTP_POST_VARS,$languages_id,$messageStack;
	?>
	<?php	
	$move_elements=(isset($HTTP_POST_VARS["move_elements"])?$HTTP_POST_VARS["move_elements"]:array());
	$session_index=(isset($HTTP_POST_VARS["new_session_index"])?$HTTP_POST_VARS["new_session_index"]:array());
	$new_session_id=(isset($HTTP_POST_VARS["new_session_id"])?$HTTP_POST_VARS["new_session_id"]:array());
    $new_events_id=(isset($HTTP_POST_VARS['new_events_id'])?(int)$HTTP_POST_VARS["new_events_id"]:0);
	if (sizeof($move_elements)<=0 || sizeof($session_index)<=0 || sizeof($new_session_id)<=0 || $new_events_id<=0){
		//tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS));
		echo 'search_reservation^^';
		search_reservation();
		return;
	}
    $process_wait=(isset($HTTP_POST_VARS['process_wait'])?$HTTP_POST_VARS['process_wait']:'N');
	$success_ids=(isset($HTTP_POST_VARS['moved_ids'])?$HTTP_POST_VARS['moved_ids']:array());
	$failure_ids=array();

    $events_query=tep_db_query("SELECT " . tep_event_fields_list() . " from " . TABLE_EVENTS . " e where e.events_id='" . (int)$new_events_id . "'");
    $events_result=tep_db_fetch_array($events_query);


	$check_session=array();
	$new_session_unique=array_unique($new_session_id);
	
	$new_events_name=tep_get_events_name($new_events_id);
	
	$select_ids_string=join(",",$new_session_unique);

	$new_session=array();
	if ($select_ids_string!="") {
		$session_query=tep_db_query("SELECT start_date,sessions_id from " . TABLE_EVENTS_SESSIONS . " where sessions_id in(" . $select_ids_string . ") order by start_date");
		while($session_result=tep_db_fetch_array($session_query)){
			$new_session[$session_result["sessions_id"]]['start_date']=$session_result["start_date"];
			$new_session[$session_result["sessions_id"]]['sessions_id']=$session_result["sessions_id"];
		}
	}
	$select_ids_string=join(",",$move_elements);
	if ($select_ids_string!=""){
		$move_elements_query=tep_db_query("SELECT o.customers_id,op.orders_products_id,op.products_id,events_type from " . TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op where op.orders_products_id in(" . $select_ids_string . ") and op.orders_id=o.orders_id");
	}
	while($move_result=tep_db_fetch_array($move_elements_query)){
		$move_details[$move_result["orders_products_id"]]=array('orders_products_id'=>$move_result["orders_products_id"],	
																'customers_id'=>$move_result["customers_id"],
																'products_id'=>$move_result["products_id"],
																'events_type'=>$move_result["events_type"]);
	}
	$icnt=0;
	$prev_index=-1;
    for ($icnt=0;$icnt<sizeof($move_elements);$icnt++){
		$err_type='U';
		$session_id=(int)$new_session_id[$session_index[$icnt]];
		if ($session_id>0){
			$move_detail=$move_details[$move_elements[$icnt]];
			if (tep_check_single_reservation($session_id,$move_detail["customers_id"])){
				$err_type='A';
			} else {
				if (tep_check_session_free($new_session[$session_id],$events_result,'R',$move_detail["customers_id"])){
					//$events_price=tep_get_events_price($events_result,'R','',$new_session[$new_session_id]);
					$waitlist_type="";
					if ($move_detail["events_type"]=="W") $waitlist_type='P';
					tep_db_query("UPDATE " . TABLE_ORDERS_PRODUCTS . " set products_id='" . (int)$session_id . "', products_model='" . $new_session[$session_id]['start_date'] . "', products_name='" . tep_db_input($new_events_name) . "', events_type='R', waitlist_type='" . $waitlist_type . "' where orders_products_id='" . (int)$move_detail["orders_products_id"] . "'");
					$err_type='N';
				} else {
					$err_type='M';
					if ($events_result["events_wait_list"]=='Y') {
						$err_type='W';
						$group=tep_find_waitlist_group($events_result["events_id"],$session_id,$move_detail["customers_id"],$events_result);
						if ($group!='E'){
							$err_type='W';
							if ($process_wait=='Y'){
								tep_db_query("UPDATE " . TABLE_ORDERS_PRODUCTS . " set products_id='" . $session_id . "', products_model='" . $new_session[$session_id]['start_date'] . "', products_name='" . tep_db_input($new_events_name) . "', events_type='W', waitlist_type='" . $group . "' where orders_products_id='" . (int)$move_detail["orders_products_id"] . "'");
								$err_type='N';
							}
						}
					}
				}
			}
		}
		if ($err_type=='N'){
			$success_ids[$move_detail["orders_products_id"]]=1;
		} else {
			$failure_ids[$move_detail["orders_products_id"]]=$err_type;
			if ($prev_index!=$session_index[$icnt]) {
				$prev_index=$session_index[$icnt];
				$check_session[]=$session_id;
			}
		}
		$err_type='U';
     }
	/*$params=array("tx_fname","tx_lname","page","tx_words","tx_code",
							"tx_from","tx_until","tx_event","tx_reserve","tx_waitlist","tx_rtype","tx_session","tx_current","orders_id","events_id","tx_pending");
	$search_get_params=tep_prepare_hidden_params($params,"post","get",array());*/
	
	if (sizeof($failure_ids)<=0){
		$messageStack->add_session(TEXT_SUCCESS_MOVE_RESERVTIONS);
		//tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS,$search_get_params . 'action=search'));
		echo 'search_reservation^^';
		search_reservation();
		return;
	}
	
	$search_post_params=tep_prepare_hidden_params($params,'post','post',array());

	echo tep_draw_form('post_submit',FILENAME_MOVE_RESERVATIONS_CONFIRM) . "\n";
	echo '<input type="hidden" name="new_events_id" value="' . tep_output_string($new_events_id) . '">';
	echo $search_post_params  . "\n";
	if (sizeof($success_ids)>0){
		reset($success_ids);
		while(list($id,)=each($success_ids)){
			echo '<input type="hidden" name="moved_ids[' . $id . ']" value="1">'  . "\n";
		}
	}
	if (sizeof($failure_ids)>0){
		reset($failure_ids);
		while(list($id,$value)=each($failure_ids)){
			echo '<input type="hidden" name="unmoved_ids[' . $id . ']" value="' . $value . '">'  . "\n";
		}
	}
	$reserve_ids=(isset($HTTP_POST_VARS["reservation_id"])?$HTTP_POST_VARS["reservation_id"]:array());
	$type=(isset($HTTP_POST_VARS["items_type"])?$HTTP_POST_VARS["items_type"]:array());
	for ($icnt=0;$icnt<sizeof($reserve_ids);$icnt++){
		echo '<input type="hidden" name="reservation_id[]" value="' . tep_output_string($reserve_ids[$icnt]) . '">'  . "\n";
	}
	if (sizeof($type)>0){
		reset($type);
		while(list($id,$value)=each($type)){
			echo '<input type="hidden" name="items_type[' . $id . ']" value="' . tep_output_string($value) . '">';
		}
	}
	for ($icnt=0;$icnt<sizeof($check_session);$icnt++){
		echo '<input type="hidden" name="new_session[]" value="' . tep_output_string($check_session[$icnt]) . '">'  . "\n";
	}
	echo '</form>';

	?>
	<script language="javascript">document.post_submit.submit();</script>
	<?php } ?>
<!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>
</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]="";
	
	function showpopup(type)
	{
		var contentText;
		
		switch(type){
			case "start-end":
				contentText="Shows customers' first session date and the last one currently reserved. Can be<br> combined with other criteria to refine choices.";
				break;
			case "current":
				contentText="Shows only the reservations of users from the current date onwards. Can be<br>	combined with other criteria to refine choices.";
				break;
			case "reserved":
				contentText="Shows only reserved reservations. Can be combined with other criteria to refine choices.";
				break;
			case "waitlist":
				contentText="Shows waitlisted customers. Can be combined with other criteria to refine choices.";
				break;
			case "pending":
				contentText="By default, pending payments are hidden. Pending payments are ones which have been reserved but with an amount outstanding to complete the payment. Can be " +
							"combined with other criteria to refine choices.";
				break;
		}
		content='<table border="0" cellspacing="0" cellpadding="4" class="formArea" width="100%"><tr><td class=smallText>';
		content+=contentText;
		content+='</td></tr></table>';
		popwidth=250;
		baby[1]=content;
		if (baby[1]!="")
		{
			popup("1","#ffffff");
		}	
	}
var ajax_image_load='<?php echo tep_image(DIR_WS_IMAGES.'24-1.gif'); ?>';
	function search_reservation(oID,eID){
		if(oID>=0){
			do_post_command('result_reservation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=search_reservation'); ?>&oID='+oID+'&events_id='+eID);										
		}else {
			document.getElementById('search_ajax_load').innerHTML=ajax_image_load;
			do_post_command('search_reservation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=search_reservation'); ?>&oID=0&events_id=0');
		}
	}
	function search_reservation_first(){
			document.getElementById('search_ajax_load').innerHTML=ajax_image_load;
			do_post_command('search_reservation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=search_reservation_first'); ?>');
	}
	function events_booking_back(){
		document.getElementById('back_ajax_load').innerHTML=ajax_image_load;
		do_get_command('<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=events_booking_back'); ?>'+'&booking_page='+booking_page);
	}
	function reservations_confirm_back(){
		document.getElementById('back_ajax_load').innerHTML=ajax_image_load;
		do_get_command('<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=search_reservation'); ?>'+'&booking_page='+booking_page);
	}
	
	function move_reservation_confirm(){
		document.getElementById('move_ajax_load').innerHTML=ajax_image_load;
		do_post_command('move_reservation_confirm','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=events_move_reservations_process&booking_page='); ?>'+booking_page);
	}
	function setflag(flag,opID,pID,eID){
		do_get_command('<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=setflag&flag='); ?>'+flag+'&opID='+opID+'&pID='+pID+'&eID='+eID+'&booking_page='+booking_page);
	}
	function move_waitlist(oID,eID){
		do_get_command('<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=move_waitlist&oID='); ?>'+oID+'&eID='+eID);
	}
	function reserve_view(orders_id,events_id){
	do_get_command('<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=reserve_view&orders_id='); ?>'+orders_id+'&events_id='+events_id+'&booking_page='+booking_page);
	}
	function withdraw_confirm_back(){
	document.getElementById('confirm_back_ajax_load').innerHTML=ajax_image_load;
	do_get_command('<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=withdraw_confirm_back'); ?>');
	}
	function withdraw_process(){
	document.getElementById('withdraw_ajax_load').innerHTML=ajax_image_load;
	do_post_command('withdraw_reservation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=withdraw_process&booking_page='); ?>'+booking_page);
	}
	function refund_back(){
	document.getElementById('refund_back_ajax_load').innerHTML=ajax_image_load;
	do_get_command('<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=refund_back'); ?>');
	}
	function refund_reservation(){
	var error_message='';
		if(document.getElementById('refund_amount').value=='') 
			error_message='Refund Amount is required';
		if(isNaN(document.getElementById('refund_amount').value))
			error_message+='\nRefund Amount must be numberic';
		
		if(error_message==''){		
		document.getElementById('refund_ajax_load').innerHTML=ajax_image_load;
		do_post_command('refund_reservation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=refund_reservation_confirm&booking_page='); ?>'+booking_page);
		} else alert(error_message);
	}
	function refund_reservation_process(){
	document.getElementById('refund_ajax_load').innerHTML=ajax_image_load;
	do_post_command('checkout_confirmation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=refund_reservation_process&booking_page='); ?>'+booking_page);
	}
	function refund_reservation_back(){
	document.getElementById('refund_back_load').innerHTML=ajax_image_load;
	do_get_command('<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=refund_reservation_back&booking_page='); ?>'+booking_page);
	}
	function do_result(result){
	var token=result.split('^^');
		switch(token[0]){
			case 'search_reservation':
			case 'withdraw_confirmation':
			document.getElementById('ajax_load').innerHTML=token[1];
			break;
			case 'events_booking_back':
			document.getElementById('ajax_load').innerHTML=token[1];
			break;
			case 'events_move_reservations_confirm':
			document.getElementById('ajax_load').innerHTML=token[1];
			break;
			case 'events_move_reservations_process':
				if(str_trim(token[1])=='search_reservation') {
					document.getElementById('ajax_load').innerHTML=token[2];
				} else {
					document.getElementById('ajax_load').innerHTML=token[1];
					do_post_command('post_submit','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=events_move_reservations_confirm'); ?>');
				}	
			break
			case 'setflag':
			case 'move_waitlist':
			case 'withdraw_process':
			document.getElementById('ajax_load').innerHTML=token[1];
			error=token[2];
			if(error && error!=''){
					document.getElementById('error_tag').style.display='';
					document.getElementById('error_message').innerHTML=error;
					setInterval('error_function()',3000);
					clearInterval();
			}
			
			break;
			case 'refund_reservation_process':
			document.getElementById('ajax_load').innerHTML=token[1];
			error=token[2];
			if(error && error==0){
					document.getElementById('error_tag').style.display='';
					document.getElementById('error_message').innerHTML="Refund Fails. Error will occur";
					setInterval('error_function()',3000);
					clearInterval();
			}
			
			break;
			case 'reserve_view':
			document.getElementById('ajax_load').innerHTML=token[1];
			break;
			case 'withdraw_confirm_back':
			case 'withdraw_process':
			case 'refund_reservation':
			case 'refund_back':
			case 'refund_reservation_confirm':
			case 'refund_reservation_back':
				document.getElementById('ajax_load').innerHTML=token[1];
				break;
		}
	}
	function error_function(){
	document.getElementById('error_tag').style.display='none';
	}

	function validateSearchForm(frm){
	if (frm.tx_dated.checked) frm.action="<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS_DATED);?>";
	if(frm.tx_from.value!='' && frm.tx_until.value!='' && frm.tx_from.value>frm.tx_until.value){
		alert("From Date Exceeds Until Date");
		return false;
	}
	return true;
	}
	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";?>";
	}
var booking_page='<?php echo $search_params["booking_page"];?>';
	function bookings_page_navigation(arg1,agr2,agr3,agr4){
		var page_no=1;
		if(agr2==1) var obj=document.getElementById("page_1");
		else if(agr2==2) var obj=document.getElementById("page_2");
		if(agr2 && agr4){
			if(obj){
 	 			page_no=parseInt(obj.options[obj.selectedIndex].value);
 	 			if(agr4=='next') {
 	 				if(obj.length==obj.selectedIndex+1) return;
 	 				page_no+=1;
 	 			}else if(agr4=='prev') {
	 	 			if(obj.selectedIndex==0) return;
 		 			page_no-=1;
 	 			}
 	 			if(page_no<=1) page_no=1;
 	 		} 	 		
		}		
		if(agr2==1) command="<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=bookings_page_navigation&booking_page='); ?>"+page_no;
		else if(agr2==2) command="<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=bookings_page_navigation&booking_page='); ?>"+page_no;

		if(command!=""){ do_post_command('result_reservation',command);
		document.getElementById('ajax_load').innerHTML=ajax_image_load;
		booking_page=page_no;
		}
	}
		function selectrefund(oid){
			//for(i=0; i<(document.result_reservation.length); i++)
			//	alert(document.result_reservation[i].name);
			//	return;
			document.result_reservation.action='<?php echo FILENAME_ORDERS_REFUND . '?preturn=sr';?>' + '&oID=' + oid;
			document.result_reservation.submit();
		//location.href="<?php echo tep_href_link(FILENAME_ORDERS_REFUND,'preturn=sr'); ?>"+ '&oID=' + oid;
	}

</script>

<div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- Ajax Work Starts -->
<table width="100%" cellpadding="2" cellspacing="2" border="0">
<tr style="display:none;padding:10px;" id="error_tag"><td><table width="100%" border="0" cellpacing="0" cellpadding="2" class="formArea">
      <tr><td class="main" id="error_message" style="color:#FF0000;"></td></tr>
	  </table></td>
</tr>
<tr>
	<td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
</tr>
	<?php $action=isset($HTTP_GET_VARS['action'])?tep_db_prepare_input($HTTP_GET_VARS['action']):'';?>
	<tr>
	<td id="ajax_load"><?php 
		if($expand=='page_navigation')
			echo search_reservation();
		else 	
			echo ($action=='search')? search_reservation() :  events_booking_search(); ?></td>
	</tr>
</table>
<!-- Ajax Work Ends -->
<?php if( ($HTTP_GET_VARS['oID']!='') && ($HTTP_GET_VARS['events_id']!='') ){?>
<script language="javascript">
var ajax_image_load='<?php echo tep_image(DIR_WS_IMAGES.'24-1.gif'); ?>';
document.getElementById('search_ajax_load').innerHTML=ajax_image_load;
do_post_command('search_reservation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=search_reservation&oID=' . $HTTP_GET_VARS['oID'] . '&events_id=' . $HTTP_GET_VARS['events_id']);?>');
</script>
<?php }?>
<?php if($HTTP_GET_VARS['orders_id']!=''){?>
<script language="javascript">
var ajax_image_load='<?php echo tep_image(DIR_WS_IMAGES.'24-1.gif'); ?>';
document.getElementById('search_ajax_load').innerHTML=ajax_image_load;
do_post_command('search_reservation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=search_reservation');?>');
search_reservation();
</script>
<?php }?>
	<!-- footer //-->
	<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
	<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>

<?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
		$query_field1=(tep_not_null($search_params['tx_fname']) ? " and o.customers_name like '%" . tep_db_input($search_params['tx_fname']) . "%'" : '');
		$query_field2=(tep_not_null($search_params['tx_lname']) ? " and o.customers_name 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 (o.customers_name like '%" . tep_db_input($search_params['tx_words']) . "%' or o.customers_name like '%" . tep_db_input($search_params['tx_words']) . "%' or o.customers_email_address like '%" . tep_db_input($search_params['tx_words']) . "%' or e.events_location like '%" . tep_db_input($search_params['tx_words']) . "%')": '');
		$query_field5=(tep_not_null($search_params['tx_from']) ? " and trim(op.products_model)>='" . tep_convert_date_raw(tep_db_input($search_params['tx_from'])) . "'" : '');
		$query_field6=(tep_not_null($search_params['tx_until']) ? " and trim(op.products_model)<='" . tep_convert_date_raw(tep_db_input($search_params['tx_until'])) . "'" : '');

		if (tep_not_null($search_params['tx_waitlist'])) $search_group='W';
		if (tep_not_null($search_params['tx_reserve'])){
			if ($search_group=='W') $search_group='A';
			else $search_group='R';
		}
		$query_field7="";
		if ($search_group=='W' || $search_group=='R') $query_field7=" and op.events_type='" . $search_group . "'";

		if($search_params['tx_event'] == -1)$search_params['tx_event'] = "";
		$query_field8=(tep_not_null($search_params['tx_event'])? " and e.events_id='" . tep_db_input($search_params['tx_event']) . "'":'');
		$query_field9="";
		if (tep_not_null($search_params['tx_session'])) {
			$date_select=$search_params['tx_session'];
			$date_split=split("-",$date_select);
			$format_split=split("-",EVENTS_DATE_FORMAT);
			$date_final=array();
			// convert date to database format
			if (sizeof($date_split)==3 && sizeof($format_split)==3){
				for ($icnt=0;$icnt<sizeof($date_split);$icnt++){
					$date_final[$format_split[$icnt]]=$date_split[$icnt];
				}
				$date_select=$date_final['Y'] . '-' . $date_final['m'] . '-' . $date_final['d'];
			}
			$query_field9=" and op.products_model='" . tep_db_input($date_select) . "'";
		}
		if (tep_not_null($search_params['tx_rtype'])){
			$tx_rtype=$search_params['tx_rtype'];
			if ($tx_rtype=='R')
				$query_field10=" and op.events_type='R' ";
			else
			$query_field10=" and op.events_type='W' and op.waitlist_type='" . $tx_rtype . "'";
		}
     	$query_field11=(tep_not_null($search_params["tx_current"])?" and op.products_model>=curdate() ":'');
		$query_field12="";

		if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY) $query_field12=" and e.events_instructor='" . $login_id ."' ";

		$query=$query_field1 . $query_field2 . $query_field3 . $query_field4 . $query_field5 . $query_field6 . $query_field7 . $query_field8 . $query_field9 . $query_field10 . $query_field11 . $query_field12;
		
		return $query;
	}
	 function get_err_type($type){
     if ($type=='M')
        return TEXT_MOVE_ERR_EMPTY;
     else if($type=='W')
        return TEXT_MOVE_ERR_WAITLIST;
     else if($type=='A')
        return TEXT_MOVE_ERR_ALREADY;
  }
  function get_selected_date($index,$date){
  	global $new_session,$find_array;
	$select_id=0;
	if (isset($new_session[$index])){
		$select_id=$new_session[$index];
	} else if (isset($find_array[$date])) {
		$select_id=$find_array[$date];
	}
	return $select_id;
  }
  function  tep_get_attendees($orders_id,$customers_id){
	if($orders_id=='' || $customers_id=='') return;
	$multiple_attendees_name="";	
	$query=tep_db_query("select concat(c.customers_lastname,' ',c.customers_firstname) as name from ".TABLE_CUSTOMERS ." c, " .	TABLE_CUSTOMERS_TO_CUSTOMERS ." c2c where c2c.customer_id=c.customers_id and c2c.group_customer_id=".(int)$customers_id." and  c2c.orders_id=".(int)$orders_id." and c2c.group_customer_id!=c.customers_id");
	if(tep_db_num_rows($query)>0){	
		while($result=tep_db_fetch_array($query)){
			$multiple_attendees_name.=$result['name'].",";
		}
	}
	if($multiple_attendees_name)	$multiple_attendees_name="<tr><td class='TableHeading' valign='top'>".TEXT_ATTENDEES."</td><td class='main' nowrap>".substr($multiple_attendees_name,0,-1)."</td></tr>";
	return $multiple_attendees_name;
  }
  /*function  tep_get_attendees($orders_id,$customers_id){
	if($orders_id=='' || $customers_id=='') return;
	$multiple_attendees_name="";	
	$query=tep_db_query("select concat(c.customers_lastname,' ',c.customers_firstname) as name from ".TABLE_CUSTOMERS ." c, " .	TABLE_CUSTOMERS_TO_CUSTOMERS ." c2c where c2c.customer_id=c.customers_id and c2c.group_customer_id=".$customers_id." and  c2c.orders_id=".$orders_id." and c2c.group_customer_id!=c.customers_id");
	if(tep_db_num_rows($query)>0){	
		while($result=tep_db_fetch_array($query)){
			$multiple_attendees_name.=$result['name'].",";
		}
	}
	if($multiple_attendees_name)	$multiple_attendees_name="<tr><td class='TableHeading' valign='top'>".TEXT_ATTENDEES."</td><td class='main' nowrap>".substr($multiple_attendees_name,0,-1)."</td></tr>";
	return $multiple_attendees_name;
}*/
?>

<script language=javascript>
	function validateForm()
	{
		var found=false;
		var formElement=document.result_reservation.elements["reservation_id[]"];
		if (formElement.length){
			for (var icnt=0;icnt<formElement.length;icnt++){
				if (formElement[icnt].checked){
					found=true;
					break;
				}
			}
		} else {
			found=formElement.checked;
		}
		if (!found)
			alert("<?php echo addslashes(stripslashes(ERR_SELECT_RESERVATION)); ?>");
		else 
		do_post_command('result_reservation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=events_move_reservations_confirm'); ?>');
	}
	function reset_search()
	{
		frm=document.forms['search_reservation'];
		frm.tx_fname.value="";
		frm.tx_lname.value="";
		frm.tx_words.value="";
		frm.tx_code.value="";
		frm.tx_from.value="";
		frm.tx_until.value="";
		frm.tx_event.selectedIndex=0;
		frm.tx_dated.checked=false;
		frm.tx_current.checked=false;
		frm.tx_reserve.checked=false;
		frm.tx_waitlist.checked=false;
		frm.tx_pending.checked=false;
		do_get_command('<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=reset_search'); ?>');

	}
	function validateLetterForm()
	{
		var found=false;
		var search_ids='';
		var formElement=document.result_reservation.elements["reservation_id[]"];
		var emailForm;
		emailForm=document.result_reservation;
		if (formElement.length){
			for (var icnt=0;icnt<formElement.length;icnt++){
				if (formElement[icnt].checked==true) {
					search_ids=search_ids+document.result_reservation.letters_id[icnt].value+",";
				}
			}
		} else {
			if (formElement.checked) search_ids=search_ids+document.result_reservation.letters_id.value+",";
		}
		if (search_ids==''){
			alert("<?php echo addslashes(stripslashes(ERR_SELECT_RESERVATION)); ?>");
			found=true;
		}
		search_ids=search_ids.substr(0,search_ids.length-1);
		emailForm.search_ids.value=search_ids;
		if(found==false){
			//do_post_command('result_reservation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=letter'); ?>');
			document.result_reservation.action='<?php echo tep_href_link(FILENAME_ADMIN_LETTERS,''); ?>';
			document.result_reservation.submit();
		}

	}
	function validateEmailForm(frmid)
	{
		var found=false;
		var search_ids='';
		var formElement=document.result_reservation.elements["reservation_id[]"];
		
		var emailForm;
		if (frmid==1)
			emailForm=document.result_reservation;
		else
			emailForm=document.result_reservation;
		

		if (formElement.length){
			for (var icnt=0;icnt<formElement.length;icnt++){
				if (formElement[icnt].checked==true) {
					search_ids=search_ids+document.result_reservation.email_address[icnt].value+",";
				}
			}
		} else {
			if (formElement.checked) search_ids=search_ids+document.result_reservation.email_address.value+",";
		}
		if (search_ids==''){
			alert("<?php echo addslashes(stripslashes(ERR_SELECT_RESERVATION)); ?>");
			found=true;
		}
		search_ids=search_ids.substr(0,search_ids.length-1);
		emailForm.search_ids.value=search_ids;
				if(found==false){
					if(frmid==1){
					document.result_reservation.action='<?php echo tep_href_link(FILENAME_SEND_EMAIL,'action=new_message&mode=1'); ?>';
					//do_post_command('result_reservation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=send_email&action=new_message&mode=1'); ?>');
					}
					else{
					document.result_reservation.action='<?php echo tep_href_link(FILENAME_SEND_EMAIL,'action=new_message&mode=2'); ?>';
					//do_post_command('result_reservation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=send_email&action=new_message&mode=2'); ?>');
					}
				document.result_reservation.submit();
				}
	}
	function select_all_boxes(checkbox){
		var formElement=document.result_reservation.elements["reservation_id[]"];
		if (formElement.length){
			for (icnt=0;icnt<formElement.length;icnt++){
				formElement[icnt].checked=checkbox.checked;
			}					
		} else {
			formElement.checked=checkbox.checked;
		}
	}

	function doFormAction(type){
		//if (!validateForm()) return;
		switch(type){
			case 2:
				document.result_reservation.action="<?php echo FILENAME_REFUND_RESERVATIONS;?>";
				document.result_reservation.process_type.value="single";
				do_post_command('result_reservation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=refund_reservation'); ?>');
				break;
			case 3:
				document.result_reservation.action="<?php echo FILENAME_WITHDRAW_RESERVATIONS;?>";
				document.result_reservation.process_type.value="single";
				do_post_command('result_reservation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=withdraw_confirmation'); ?>');
				break;
			case 4:
				document.result_reservation.action="<?php echo FILENAME_WITHDRAW_RESERVATIONS;?>";
				do_post_command('result_reservation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=withdraw_confirmation'); ?>');
				break;
			case 5:
				document.result_reservation.action="<?php echo FILENAME_REFUND_RESERVATIONS;?>";
				do_post_command('result_reservation','<?php echo tep_href_link(FILENAME_SEARCH_RESERVATIONS,'command=refund_reservation'); ?>');
				break;
		}

		//document.result_reservation.submit();
	}
	function selectBox(id,type,action){ 
		var element=document.result_reservation.elements["reservation_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_reservation.process_type.value=type; // indivitual(single) or group(order) based withdraw
		doFormAction(action);
	}
	</script>	 