<?php

/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd 
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
  require('includes/application_top.php');
  require(DIR_WS_CLASSES . 'currencies.php');
  require(DIR_WS_CLASSES . 'split_page_results_event.php');
  //added new
  define(BOX_WIDTH1,'125');
  
  $currencies = new currencies();
  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
 
  if ($action==""){
	  $action = (isset($HTTP_GET_VARS['firstaction']) ? $HTTP_GET_VARS['firstaction'] : ''); 	
  }
  
  $page=(isset($HTTP_GET_VARS['page'])?(int)$HTTP_GET_VARS['page']:1);

	if ($action=="search")
	{
		if (isset($HTTP_GET_VARS['page']) && ($HTTP_GET_VARS['page'] > 1)) $rows = $HTTP_GET_VARS['page'] * MAX_DISPLAY_SEARCH_RESULTS - MAX_DISPLAY_SEARCH_RESULTS;
		$rows=0;
		$search_params=array("tx_fname"=>'',"tx_lname"=>'',"page"=>1,"tx_words"=>'',"tx_code"=>'',"tx_dated"=>'',
								"tx_from"=>'',"tx_until"=>'',"tx_event"=>'',"tx_reserve"=>'',"tx_waitlist"=>'',"tx_rtype"=>'',"tx_session"=>'',"tx_current"=>'',"orders_id"=>'',"events_id"=>'',"tx_pending"=>'');
		
		$temp_query=build_search_query();						
		$fields=",min(s.start_date) as start_date,count(s.start_date) as total_count,max(s.start_date) as end_date ";
		$query=$temp_query . ' group by o.customers_id,e.events_id ';

     	
		$search_query=	"select max(o.orders_id) as 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 " . $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='" . (int)$languages_id . "'" . $query;

		$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";
	}
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<div id="spiffycalendar" class="text"></div>
	<!-- header //-->
	<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
	<!-- header_eof //-->
	<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
<tr> 
	<!-- body_text //-->	  
	<td width=100% align=left valign="top">
	<table border="0" width="100%" cellspacing="0" cellpadding="2">
		<tr>
			<td>
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td align=left class='pageHeading' colspan=8><?php echo HEADING_RESULT_TITLE; ?></td>
				</tr>
			</table>
			</td>
		</tr>
		<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
		<?php if ($cnt>0) {?>
			<tr>
				<td>
				<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td class="main" valign="middle" align="left">
						<?php echo tep_draw_checkbox_field("select_all","1",false,'','onClick="javascript:select_all_boxes(this);"') . TEXT_SELECT_ALL;	?>
					</td>
					<td align=right valign="middle">
					<script language=javascript>
					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";?>";
						}
					</script>
					<?php echo '<a href="' . tep_href_link(FILENAME_SEARCH_RESERVATION_PRINT_DATED,tep_get_all_get_params()) . '" onmouseover="change_image(1)" onmouseout="change_image(0)" target="_blank">' . '<img border=0 src="' . DIR_WS_IMAGES . "printimage.gif" . '" alt="' . IMAGE_PRINT_RESERVATIONS . '" title="' . IMAGE_PRINT_RESERVATIONS . '" id="print_image">' . TEXT_PRINT . '</a></span>';?>
					</td>
				</tr>
				</table>
				</td>
			</tr>
		<?php if (!$orders_id>0) {?>
			<tr>
				<td>
				<table border="0" width="100%" cellspacing="0" cellpadding="2">
					<tr>
						<td class="smallText" valign="top"><?php echo $reservations_split->display_count($search_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $HTTP_GET_VARS['page'], TEXT_DISPLAY_NUMBER_OF_RESERVATIONS); ?></td>
						<td class="smallText" align="right">
							<?php echo $reservations_split->display_links($search_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $HTTP_GET_VARS['page'],tep_get_all_get_params(array('page','x','y'))); ?>&nbsp;
						</td>
					</tr>
				</table>
				</td>
			</tr>
		<?php }?>
		<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_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>
				</tr>
				<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)); ?>");
						return false;
					}
					return true;
				}
				function validateLetterForm()
				{
					var found=false;
					var search_ids='';
					var formElement=document.result_reservation.elements["reservation_id[]"];
					var emailForm;
					emailForm=document.admin_letter;
					if (formElement.length){
						for (var icnt=0;icnt<formElement.length;icnt++){
							if (formElement[icnt].checked==true) {
								search_ids=search_ids+document.result_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)); ?>");
						return false;
					}
					search_ids=search_ids.substr(0,search_ids.length-1);
					emailForm.search_ids.value=search_ids;
					return true;
				}
				function validateEmailForm(frmid)
				{
					var found=false;
					var search_email='';
					var formElement=document.result_reservation.elements["reservation_id[]"];
					var emailForm;
					if (frmid==1)
						emailForm=document.admin_letter;
					else if (frmid==2)
						emailForm=document.admin_email;
					else
						emailForm=document.admin_sms;
					if (formElement.length){
						for (var icnt=0;icnt<formElement.length;icnt++){
							if (formElement[icnt].checked==true) {
								search_email+=document.result_reservation.email_address[icnt].value+",";
							}
						}
					} else {
						if (formElement.checked) search_email=search_email+document.result_reservation.email_address.value+",";
					}
					if (search_email==''){
						alert("<?php echo addslashes(stripslashes(ERR_SELECT_RESERVATION)); ?>");
						return false;
					}
					search_email=search_email.substr(0,search_email.length-1);
					emailForm.search_ids.value=search_email;
					return true;
				}
				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";
							break;
						case 3:
							document.result_reservation.action="<?php echo FILENAME_WITHDRAW_RESERVATIONS;?>";
							document.result_reservation.process_type.value="single";
							break;
						case 4:
							document.result_reservation.action="<?php echo FILENAME_WITHDRAW_RESERVATIONS;?>";
							break;
						case 5:
							document.result_reservation.action="<?php echo FILENAME_REFUND_RESERVATIONS;?>";
							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(2))
							document.admin_email.submit();
						return;
					}
					document.result_reservation.process_type.value=type; // indivitual(single) or group(order) based withdraw
					doFormAction(action);
				}
				</script>	   
				<?php 
					echo tep_draw_form('result_reservation',FILENAME_MOVE_RESERVATIONS_CONFIRM,'','post','onSubmit="return validateForm()"');
					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;
						}
						
				?>
				<tr class='dataTableRow' height=20>
					<td class='dataTableContent'>
					<?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"]);
					?>
					</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>';?></td>
					<?php echo "<span onclick='document.location.href=\"" . tep_href_link(FILENAME_ORDERS, "page=1&oID=" . $result['orders_id'] . '&action=edit&origin=' . FILENAME_SEARCH_RESERVATIONS) . "\"'>"; ?>
					<td class='dataTableContent' style="cursor:hand"><?php echo $result['customers_firstname']; ?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo $result['customers_lastname']; ?></td>
					<td class='dataTableContent' style="cursor:hand"><?php echo $result['events_name']; ?></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>";?>
			</tr></span>
			<?php } ?>
       </table>
	   </td>
	   </tr>
	   </form>
		<?php } else { ?>
			<tr><td align=left class='main' colspan=8><?php echo TEXT_NO_RESULTS ?></td></tr>
		<?php } ?>
		<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
		<tr><td align=left>
			<table border="0" width="100%" cellspacing="0" cellpadding="2">
				<tr valign=middle>
					<td align=left class="main" valign="middle">
					<?php 
						echo tep_draw_hidden_field('process','');
						if ($page>1)
							echo '<a href="' . tep_href_link(FILENAME_SEARCH_RESERVATIONS_DATED,tep_get_all_get_params(array('x','y','orders_id','events_id','error','page')) . 'page=' . ($page-1)) . '">' . tep_image_button('button_back.gif', IMAGE_BACK,'align=absmiddle') . '</a>&nbsp;&nbsp;';
						else
							echo '<a href="' . tep_href_link(FILENAME_SEARCH_RESERVATIONS) . '">' . tep_image_button('button_back.gif', IMAGE_BACK,'align=absmiddle') . '</a>&nbsp;&nbsp;';
					?>
						
                        <td align=right valign="middle">
					<?php 
						if ($cnt>0) {
							echo tep_draw_form('admin_letter',FILENAME_ADMIN_LETTERS,'','post','onSubmit="javascript:return validateLetterForm();"');
							echo tep_draw_hidden_field('search_ids','');
							echo tep_image_submit('button_letter.gif',IMAGE_ADMIN_LETTERS,"align='absmiddle'") .'&nbsp;';
							echo '</form>';
							echo tep_draw_form('admin_email',FILENAME_SEND_EMAIL,'action=new_message&mode=1','post','onSubmit="javascript:return validateEmailForm(2);"');
							echo tep_draw_hidden_field('search_ids','');
							echo tep_image_submit('button_email.gif',IMAGE_SEARCH_EMAIL,"align='absmiddle'") . '&nbsp;';
							reset($search_params);
							while(list($key,)=each($search_params)){
								echo '<input type="hidden" name="' . $key . '" value="' . htmlspecialchars($search_params[$key]) . '">';
							}
							echo '</form>';
							echo tep_draw_form('admin_sms',FILENAME_SEND_EMAIL,'action=new_message&mode=2','post','onSubmit="javascript:return validateEmailForm(3);"');
							echo tep_draw_hidden_field('search_ids','');
							echo tep_image_submit('button_sms.gif',IMAGE_SEARCH_EMAIL,"align='absmiddle'");
							reset($search_params);
							while(list($key,)=each($search_params)){
								echo '<input type="hidden" name="' . $key . '" value="' . htmlspecialchars($search_params[$key]) . '">';
							}
							echo '</form>';
				     	}
					?>
					</td>
				</tr>
			</table>
		</td>
		</tr>
	<input type=hidden name=row_check value="<?php echo $rows ?>">
	</table>
	</td>
	</tr>
</table>
<!-- body_text_eof //-->

	<!-- footer //-->
	<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
	<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>



<?php 
	function build_search_query(){
		global $HTTP_GET_VARS,$HTTP_POST_VARS,$search_params,$login_groups_type,$login_id;
		$search_group='A'; // all users both reserved and waitlist
		// form the query with given details in parameters
		if (isset($HTTP_POST_VARS['tx_fname']))
			$input_params=&$HTTP_POST_VARS;
		else
			$input_params=&$HTTP_GET_VARS;
		
		reset($search_params);	
		while(list($key,)=each($search_params)){
			if (isset($input_params[$key])) $search_params[$key]=tep_db_prepare_input($input_params[$key]);
		}
		$query_field1=(tep_not_null($search_params['tx_fname']) ? " and c.customers_firstname like '%" . tep_db_input($search_params['tx_fname']) . "%'" : '');
		$query_field2=(tep_not_null($search_params['tx_lname']) ? " and c.customers_lastname like '%" . tep_db_input($search_params['tx_lname']) . "%'" : '');
		$query_field3=(tep_not_null($search_params['tx_code']) ? " and o.customers_postcode='" . tep_db_input($search_params['tx_code']) . "'" : '');
		$query_field4=(tep_not_null($search_params['tx_words']) ? " and (c.customers_firstname like '%" . tep_db_input($search_params['tx_words']) . "%' or c.customers_lastname like '%" . tep_db_input($search_params['tx_words']) . "%' or c.customers_email_address like '%" . tep_db_input($search_params['tx_words']) . "%' or e.events_location like '%" . tep_db_input($search_params['tx_words']) . "%')": '');
		$query_field5=(tep_not_null($search_params['tx_from']) ? " and s.start_date>='" . tep_convert_date_raw(tep_db_input($search_params['tx_from'])) . "'" : '');
		$query_field6=(tep_not_null($search_params['tx_until']) ? " and s.start_date<='" . 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 s.start_date='" . 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 s.start_date>=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;
	}
	
?>