<?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('includes/classes/currencies.php');
	require('includes/functions/message_functions.php');
	$currencies=new currencies();
	$return_url=tep_href_link(FILENAME_EVENTS_PAYMENTS,'mode=2');

	$sessions_id=isset($HTTP_GET_VARS["sID"])?(int)$HTTP_GET_VARS["sID"]:0;
	$events_id=isset($HTTP_GET_VARS["eID"])?(int)$HTTP_GET_VARS["eID"]:0;
	$mode=isset($HTTP_GET_VARS["mode"])?$HTTP_GET_VARS["mode"]:'';
	$default_entry=isset($HTTP_GET_VARS["default_entry"])?1:0;
	$server_date = getServerDate(true);		


	if ($sessions_id<=0 || $events_id<=0 || ($mode!=1 && $mode!=2)) tep_redirect($return_url);
	
	$post_action=isset($HTTP_POST_VARS["post_action"])?$HTTP_POST_VARS["post_action"]:'';
	$get_action=isset($HTTP_GET_VARS["action"])?$HTTP_GET_VARS["action"]:'';


	$current_url=tep_href_link(FILENAME_EVENTS_PAYMENTS_DETAIL,tep_get_all_get_params(array("action","default_entry")));

	if ($mode==1)
		$table_attendees=TABLE_EVENTS_ATTENDEES;
	else
		$table_attendees=TABLE_EVENTS_ATTENDEES_CONFIRM;

	// add default customers for the first time
	if ($mode==1 && $default_entry==1){
		$default_query=tep_db_query("SELECT count(*) as total from " . TABLE_EVENTS_ATTENDEES . " where events_id='" . $events_id . "' and sessions_id='" . $sessions_id . "'");
		$default_result=tep_db_fetch_array($default_query);

		if ($default_result["total"]==0) add_default_customers();
	}
	
	$check_query=tep_db_query("SELECT * from " . TABLE_EVENTS_INSTRUCTOR_PAYMENTS . " where instructor_id='" . $login_id . "' and sessions_id='" . $sessions_id . "'");
	$pay_request=(tep_db_num_rows($check_query)>0?true:false);
	switch ($post_action){
		case "add_customer":
			$final_price=0;
			$fees_query=tep_db_query("SELECT events_fees from " . TABLE_EVENTS ." where events_id='" . $events_id ."'");
			$fees=tep_db_fetch_array($fees_query);
			if (tep_not_null($fees["events_fees"])) $final_price=$fees["events_fees"];
			$sql_array=array(	'customers_id'=>0,
								'customers_firstname'=>(isset($HTTP_POST_VARS["customers_firstname"])?tep_db_prepare_input($HTTP_POST_VARS["customers_firstname"]):''),
								'customers_lastname'=>(isset($HTTP_POST_VARS["customers_lastname"])?tep_db_prepare_input($HTTP_POST_VARS["customers_lastname"]):''),
								'customers_telephone'=>(isset($HTTP_POST_VARS["customers_telephone"])?tep_db_prepare_input($HTTP_POST_VARS["customers_telephone"]):''),
								'customers_second_telephone'=>(isset($HTTP_POST_VARS["customers_second_phone"])?tep_db_prepare_input($HTTP_POST_VARS["customers_second_phone"]):''),
								'customers_age'=>(isset($HTTP_POST_VARS["customers_age"])?tep_db_prepare_input($HTTP_POST_VARS["customers_age"]):''),
								'customers_street_address'=>(isset($HTTP_POST_VARS["customers_street_address"])?tep_db_prepare_input($HTTP_POST_VARS["customers_street_address"]):''),
								'customers_suburb'=>(isset($HTTP_POST_VARS["customers_suburb"])?tep_db_prepare_input($HTTP_POST_VARS["customers_suburb"]):''),
								'customers_postal_code'=>(isset($HTTP_POST_VARS["customers_postal_code"])?tep_db_prepare_input($HTTP_POST_VARS["customers_postal_code"]):''),
								'customers_city'=>(isset($HTTP_POST_VARS["customers_city"])?tep_db_prepare_input($HTTP_POST_VARS["customers_city"]):''),
								'customers_state'=>(isset($HTTP_POST_VARS["customers_state"])?tep_db_prepare_input($HTTP_POST_VARS["customers_state"]):''),
								'customers_country'=>(isset($HTTP_POST_VARS["customers_state"])?tep_db_prepare_input($HTTP_POST_VARS["customers_country"]):''),
								'customers_amount'=>(isset($HTTP_POST_VARS["customers_amount"])?tep_db_prepare_input($HTTP_POST_VARS["customers_amount"]):''),
								'customers_emailaddress'=>(isset($HTTP_POST_VARS["customers_emailaddress"])?tep_db_prepare_input($HTTP_POST_VARS["customers_emailaddress"]):''),
								'customers_second_emailaddress'=>(isset($HTTP_POST_VARS["customers_second_email"])?tep_db_prepare_input($HTTP_POST_VARS["customers_second_email"]):''),
								'sessions_id'=>$sessions_id,
								'events_id'=>$events_id,
								'customers_paid'=>(isset($HTTP_POST_VARS["customers_paid"])?tep_db_prepare_input($HTTP_POST_VARS["customers_paid"]):'N'),
								'customers_paid_type'=>(isset($HTTP_POST_VARS["customers_paid_type"])?tep_db_prepare_input($HTTP_POST_VARS["customers_paid_type"]):''),
								'final_price'=>$final_price
								);
			tep_db_perform($table_attendees,$sql_array);
			$ids=tep_db_insert_id();
			if ($table_attendees==TABLE_EVENTS_ATTENDEES_CONFIRM) {
				tep_message_add_customer($ids);
				//add query;
			}
			$message=tep_get_message_string("ANQ");
			$replace_array=array(	MES_FTN=>$sql_array["customers_firstname"],
									MES_LTN=>$sql_array["customers_lastname"],
									MES_PHN=>$sql_array["customers_telephone"],
									MES_PAD=>($sql_array["customers_paid"]=="Y"?"Yes":"No"),
									MES_PAT=>$sql_array["customers_paid_type"],
									MES_EVA=>tep_get_events_name($events_id),
									MES_INS=>tep_get_instructor_name($login_id),
									MES_SAT=>format_date(tep_get_session_date($sessions_id))
								);
			$temp_text=$message;			
			while(list($key,$value)=each($replace_array)){
				$temp_text=str_replace("%%" . $key . "%%",$value,$temp_text);
			}
			tep_db_query("INSERT INTO " . TABLE_EVENTS_ATTENDEES_QUERY . "(attendees_id,query_date,query_text,attendees_type) values(" . $ids . ",'" . $server_date ."','" . tep_db_input($temp_text)  . "','" . $type . "')");
			tep_db_query("UPDATE " . $table_attendees . " set queried='Y' where entry_id=" . $ids);
			tep_redirect($current_url);
			break;
		case "add_search_customers":
			$orders_id=(isset($HTTP_POST_VARS["orders_id"])?$HTTP_POST_VARS["orders_id"]:0);
			$events_name=tep_get_events_name($events_id);
			$session_date=tep_get_session_date($sessions_id);
			$customer_query=tep_db_query(	"SELECT c.customers_id,c.customers_firstname,c.customers_lastname,c.customers_telephone,op.products_id as sessions_id,o.payment_method as customers_paid_type,op.final_price,s.events_id from " .
											TABLE_ORDERS . " o," . TABLE_ORDERS_PRODUCTS . " op," . TABLE_CUSTOMERS . " c, " . TABLE_EVENTS_SESSIONS . " s where o.orders_id ='" . $orders_id . "' " . 
											" and o.orders_id=op.orders_id and o.customers_id=c.customers_id and op.products_type='E' and op.products_id=s.sessions_id group by o.orders_id");
			$customer_result=tep_db_fetch_array($customer_query);
			$ids="";
			$order_events_id=$customer_result["events_id"];
			$status_query=tep_db_query("SELECT orders_status_id from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id='" . (int)$result["customers_paid"] . "'");
			$status=tep_db_fetch_array($status_query);
			$customer_result["customers_paid"]=((int)$status["order_status_id"]>1)?"Y":"N";
			$customer_result["events_id"]=$events_id;
			$customer_result["sessions_id"]=$sessions_id;
			tep_db_perform($table_attendees,$customer_result);

			$ids=tep_db_insert_id();
			
			if ($table_attendees==TABLE_EVENTS_ATTENDEES_CONFIRM) tep_message_add_customer($ids);

			$type=($mode==1?"A":"C");
		
			$query_check=tep_db_query("SELECT op.products_id from " . TABLE_ORDERS_PRODUCTS . " op where op.orders_id='" . $orders_id . "' and op.products_id='" . $sessions_id ."'");
			
			if (tep_db_num_rows($query_check)==0){
				//add query;
				$message=tep_get_message_string("ADQ");
				$replace_array=array(	MES_FTN=>$customer_result["customers_firstname"],
										MES_LTN=>$customer_result["customers_lastname"],
										MES_PHN=>$customer_result["customers_telephone"],
										MES_PAD=>($customer_result["customers_paid"]=="Y"?"Yes":"No"),
										MES_PAT=>$customer_result["customers_paid_type"],
										MES_EVA=>$events_name,
										QUR_EVA=>tep_get_events_name($order_events_id),
										QUR_ORD=>$orders_id,
										MES_INS=>tep_get_instructor_name($login_id),
										MES_SAT=>format_date($session_date)
									);
				$temp_text=$message;			
				while(list($key,$value)=each($replace_array)){
					$temp_text=str_replace("%%" . $key . "%%",$value,$temp_text);
				}
				tep_db_query("INSERT INTO " . TABLE_EVENTS_ATTENDEES_QUERY . "(attendees_id,query_date,query_text,attendees_type) values(" . $ids . ",'" . $server_date . "','" . tep_db_input($temp_text)  . "','" . $type . "')");
				tep_db_query("UPDATE " . $table_attendees . " set queried='Y' where entry_id=" . $ids);
			}
			tep_redirect($current_url);
			break;
		case "delete_customer":
			if (isset($HTTP_POST_VARS["entry_id"])){
				$delete_ids=join(",",$HTTP_POST_VARS["entry_id"]);
				if ($delete_ids!=""){
					$type=($mode==1?"A":"C");
					tep_db_query("DELETE from " . $table_attendees . " where entry_id in(" . $delete_ids . ")");
					tep_db_query("DELETE from ". TABLE_EVENTS_ATTENDEES_QUERY . " where attendees_id in (" . $delete_ids . ") and attendees_type='" . $type . "'");
				}
			}	
			tep_redirect($current_url);
			break;
		case "comment_query":
			$update_ids=$HTTP_POST_VARS["entry_id"];
			$type=($mode==1?"A":"C");
			$comment_text=tep_db_prepare_input($HTTP_POST_VARS["comment_text"]);
			for ($icnt=0;$icnt<sizeof($update_ids);$icnt++){
				tep_db_query("INSERT INTO " . TABLE_EVENTS_ATTENDEES_QUERY . "(attendees_id,query_date,query_text,attendees_type) values(" .  $update_ids[$icnt] . ",'" . $server_date ."','" . tep_db_input($comment_text) . "','" . $type . "')");
			}
			tep_redirect($current_url);
			break;
		case "confirm_attendees":
			$query=tep_db_query("SELECT at.events_id,at.sessions_id,at.entry_id,at.customers_id,at.customers_telephone,at.customers_firstname,at.customers_lastname,at.customers_query,at.customers_paid,at.customers_paid_type,at.final_price,at.queried from " .
						TABLE_EVENTS_ATTENDEES . " at where at.sessions_id='" . $sessions_id . "' order by at.customers_firstname,at.customers_lastname ");
			$ids="";
			$id=0;
			while($result=tep_db_fetch_array($query)){
				$old_id=$result["entry_id"];
				unset($result["entry_id"]);
				tep_db_perform(TABLE_EVENTS_ATTENDEES_CONFIRM,$result);
				$id=tep_db_insert_id();
				if ($result["customers_id"]==0){
					$ids.=$id . ",";
				}
				tep_db_query("UPDATE " . TABLE_EVENTS_ATTENDEES_QUERY . " set attendees_id=" . $id . ",attendees_type='C' where attendees_id=" . $old_id . " and attendees_type='A'");
			}
			if ($ids!="") tep_message_add_customer(substr($ids,0,-1));

			$comments=(isset($HTTP_POST_VARS["comments"])?$HTTP_POST_VARS["comments"]:'');
			if ($comments!="") tep_message_comments($comments);
			tep_db_query("DELETE from " . TABLE_EVENTS_ATTENDEES . " where sessions_id='" . $sessions_id . "'");
			tep_redirect($return_url);
	}
	switch ($get_action){
		case "add_default":
			add_default_customers();
			tep_redirect($current_url);
			break;
	}
	$found_results=false;

	//build html ouput query
	switch($post_action){
		case "search_customers":
			$customers_id=(int)$HTTP_POST_VARS["customers_id"];
			$query="SELECT c.customers_id,c.customers_firstname,c.customers_lastname,c.customers_telephone,c.customers_email_address,o.orders_id,o.payment_method,max(s.start_date) as edate,min(s.start_date) as sdate,op.products_name from " .
					TABLE_ORDERS . " o," . TABLE_ORDERS_PRODUCTS . " op," . TABLE_EVENTS_SESSIONS . " s, " . TABLE_CUSTOMERS . " c where o.customers_id=" . $customers_id . " and " .
					" o.orders_id=op.orders_id and o.customers_id=c.customers_id and op.products_type='E' and op.products_id=s.sessions_id and op.events_type='R' group by orders_id ORDER BY o.date_purchased desc";
			break;
		case "view_query":
			$select_ids=join(",",$HTTP_POST_VARS["entry_id"]);
			$query="SELECT at.entry_id,at.customers_id,at.customers_telephone,at.customers_firstname,at.customers_lastname from " .
						$table_attendees . " at where at.entry_id in(" . $select_ids . ") order by at.customers_firstname,at.customers_lastname ";
			$history_query=tep_db_query("SELECT * from " . TABLE_EVENTS_ATTENDEES_QUERY . " where attendees_id in (" . $select_ids . ")");
			break;
		default:
			$query="SELECT at.entry_id,at.customers_id,at.customers_telephone,at.customers_firstname,at.customers_lastname,at.customers_query,at.queried from " .
						$table_attendees . " at where at.sessions_id='" . $sessions_id . "' order by at.customers_firstname,at.customers_lastname ";

	}			
	//query for results
	$detail_query=tep_db_query($query);

	// get event related details	
	$event_query=tep_db_query("SELECT ed.events_name,el.location_name from " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed, " . TABLE_EVENTS_LOCATIONS . " el where e.events_id='" . $events_id . "' and e.events_id=ed.events_id and e.events_location=el.location_id");
	$event_result=tep_db_fetch_array($event_query);
	
	if (tep_db_num_rows($detail_query)>0) $found_results=true;

	$filter_string=check_customers_added();
	$add_option="";
	//if ($HTTP_POST_VARS["customer_name"]!="") $add_option=" (c.customers_firstname like '%" . $HTTP_POST_VARS["customer_name"] . "%' or c.customers_lastname like '%" . $HTTP_POST_VARS["customer_name"] . "%') and ";
	$cust_query="SELECT c.customers_id,c.customers_firstname,c.customers_lastname,c.customers_telephone,c.customers_email_address from " .
			TABLE_ORDERS . " o," . TABLE_ORDERS_PRODUCTS . " op," . TABLE_EVENTS_SESSIONS . " s, " . TABLE_CUSTOMERS . " c where " . $filter_string . $add_option .
			" o.orders_id=op.orders_id and o.customers_id=c.customers_id and op.products_type='E' and op.products_id=s.sessions_id and op.events_type='R' group by customers_id ORDER BY c.customers_lastname desc";
	
	$result=tep_db_query($cust_query);
	$selected_name="";	
	$SelectCustomerBox = "<select name='customers_id' size=10 onClick=\"javascript:setListboxText(0);\" class='customerSelect'>\n";
		if (tep_db_num_rows($result) > 0){
		// Query Successful
			while($db_Row = tep_db_fetch_array($result)){ 
				$SelectCustomerBox .= "<option value='" . $db_Row["customers_id"] . "'";
				if($db_Row["customers_id"]==$customers_id){
					$SelectCustomerBox .= " SELECTED ";
					$selected_name=$db_Row["customers_lastname"] . ' ' . $db_Row["customers_firstname"] . ", " . $db_Row["customers_email_address"];
				}
					//$SelectCustomerBox .= ">" . $db_Row["customers_lastname"] . " , " . $db_Row["customers_firstname"] . " - " . $db_Row["customers_id"] . "</option>\n"; 
				$SelectCustomerBox .= ">" . $db_Row["customers_lastname"] . ' ' .  $db_Row["customers_firstname"] . ", " . $db_Row["customers_email_address"] . " (" . $db_Row["customers_id"]. ") </option>\n";
			}
		}
	$SelectCustomerBox .= "</select>\n";
?>
<!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 //-->
	<link rel="stylesheet" type="text/css" href="includes/javascript/spiffyCal/spiffyCal_v2_1.css">
	<script language="JavaScript" src="includes/javascript/spiffyCal/spiffyCal_v2_1.js"></script>
	<script language="javascript"><!--
	  var dateStart = new ctlSpiffyCalendarBox("dateStart", "new_payment", "start_date","btnDate1","<?php echo '';?>",scBTNMODE_CUSTOMBLUE);
	  var dateEnd = new ctlSpiffyCalendarBox("dateEnd", "new_payment", "end_date","btnDate2","<?php echo '';?>",scBTNMODE_CUSTOMBLUE);
	//-->
	</script>
	<script language="javascript">
		function validateCustomersForm(frm){
			var error_result="";
			var entry=frm.elements["entry_id[]"];
			if (entry.length){
				for (icnt=0;icnt<entry.length;icnt++){
					if (entry[icnt].checked) break;
				}
				if (icnt>=entry.length) error_result="<?php echo addslashes(stripslashes(ERR_SELECT_CHECK));?>";
			} else {
				if (entry.checked==false) error_result="<?php echo addslashes(stripslashes(ERR_SELECT_CHECK));?>";
			}
			if (error_result!=""){
				alert(error_result);
				return false;
			}
			return true;
		}
		function setAction(type){
			document.payment_detail.post_action.value=type;
			if (validateCustomersForm(document.payment_detail))
				document.payment_detail.submit();
		}
		function selectAll(checkbox){
			var entry=document.payment_detail.elements["entry_id[]"];
			if (!entry) return;
			if (entry.length){
				for (icnt=0;icnt<entry.length;icnt++){
					entry[icnt].checked=checkbox.checked;
				}
			} else {
				entry.checked=checkbox.checked;
			}
		}
		function checkSelectAll(checkbox){
			if (!checkbox.checked) document.payment_detail.select_all_customers.checked=false;
		}
	</script>
	<td width="100%" valign="top">
	<table border="0" width="100%" cellspacing="0" cellpadding="0">
	<?php if ($post_action=="") { ?>
		<tr>
			<td class="main" align="right"><?php echo '<a href="' . $return_url . '">' . tep_image_button("button_back.gif",IMAGE_BACK) . '</a>';?></td>
		</tr>
		<tr>
			<td class="pageHeading"><?php echo HEADING_PAYMENTS_DETAIL;?></td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<tr><td class="main"><?php echo $event_result["events_name"] . " " . format_date(tep_get_session_date($sessions_id)) . " " . $event_result["location_name"];?></td></tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>		
		</tr>
		<?php if ($found_results){ ?>
		<tr>
			<td class="main"><?php ?></td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>		
		</tr>
		<?php } ?>
		<tr>
		<td>
		<table width="100%" border="0" cellspacing="2" cellpadding="2" class="formArea">
			<?php echo tep_draw_form("payment_detail",FILENAME_EVENTS_PAYMENTS_DETAIL,tep_get_all_get_params(array("action","default_entry")),'post','onSubmit="javascript:return validateCustomersForm(this);"');
					echo '<input type=hidden name=post_action value="delete_customer">';
			?>
			<tr>
				<td class="tableHeading" width="20">&nbsp;</td>
				<td class="tableHeading"><?php echo TABLE_FIRSTNAME;?></td>
				<td class="tableHeading"><?php echo TABLE_LASTNAME;?></td>
				<td class="tableHeading" width="100"><?php echo TABLE_START_DATE;?></td>
				<td class="tableHeading" width="100"><?php echo TABLE_END_DATE;?></td>
				<td class="tableHeading" width="100"><?php echo TABLE_PHONE_NUMBER;?></td>
				<td class="tableHeading" align="left" width="60"><?php echo TABLE_QUERIED;?></td>
			</tr>
			<?php 
				if ($found_results){
					$row_cnt=1;
					while($detail_result=tep_db_fetch_array($detail_query)) {
					//to get customers start_date and end_date
						if ($detail_result["customers_id"]>0)
							$session_query=tep_db_query("SELECT min(s.start_date) as sdate,max(s.start_date) as edate from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_EVENTS_SESSIONS . " s  where o.customers_id='" . (int)$detail_result["customers_id"] . 
														"' and o.orders_id=op.orders_id and op.products_id=s.sessions_id and s.events_id='" . $events_id . "'");
						else
							$session_query=tep_db_query("SELECT start_date as sdate,start_date as edate from " . TABLE_EVENTS_SESSIONS . " where sessions_id='" . (int)$sessions_id . "'");

						$session_result=tep_db_fetch_array($session_query);
						
						if ($row_cnt%2==0) $class="attributes-even";
						else $class="attributes-odd";
			?>
					<tr class="<?php echo $class;?>">
						<td class="main" valign="top"><?php echo tep_draw_checkbox_field("entry_id[]",$detail_result["entry_id"],false,false,'onClick="javascript:checkSelectAll(this);"');?></td>
							<?php 
								if ($detail_result["customers_id"]>0){
									echo '<td class="main" valign="top"><a href="' . tep_href_link(FILENAME_CUSTOMERS,'action=edit&cuID=' . $detail_result['customers_id']) . '">' . $detail_result["customers_firstname"] . '</a></td>';
									echo '<td class="main" valign="top"><a href="' . tep_href_link(FILENAME_CUSTOMERS,'action=edit&cuID=' . $detail_result['customers_id']) . '">' . $detail_result["customers_lastname"] . '</a></td>';
								} else {
									echo '<td class="main" valign="top">' . $detail_result["customers_firstname"] . '</td>';
									echo '<td class="main" valign="top">' . $detail_result["customers_lastname"] . '</td>';
								}
							?>
						<td class="main" valign="top"><?php echo format_date($session_result["sdate"]);?></td>
						<td class="main" valign="top"><?php echo format_date($session_result["edate"]);?></td>
						<td class="main" valign="top"><?php echo $detail_result["customers_telephone"];?></td>
						<td class="main" align="left" valign="top">
						<?php	if ($detail_result["queried"]=="Y") {
									echo tep_image(DIR_WS_IMAGES . 'icon_status_red.gif',IMAGE_QUERY);
								} else {
									echo tep_image(DIR_WS_IMAGES . 'icon_status_red_light.gif',IMAGE_NOT_QUERY);
								}
						?>
						</td>
					</tr>
			<?php 
				$row_cnt++;
				} 
			} else { // found_results if
			?>
			<tr>
				<td colspan="7" class="main" align="center"><?php echo TEXT_NO_PAYMENT_DETAILS;?></td>
			</tr>
			<?php } // found_results if?>
		</table>
		</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '15'); ?></td>
		</tr>
		<tr>
			<td>
				<table border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td class="main" valign="middle">
							<?php 
								if ($found_results){
									echo tep_draw_checkbox_field("select_all_customers",'',false,false,'onClick="javascript:selectAll(this);"') . '&nbsp;' . TEXT_SELECT_ALL_CUST . "&nbsp;&nbsp;";
									if (!$pay_request) echo tep_image_button("button_delete_selected.gif",IMAGE_DELETE_SELECTED,'onClick="setAction(\'delete_customer\')" style="cursor:hand" align="absmiddle"');
									echo '&nbsp;' . tep_image_button("button_view_query.gif",IMAGE_QUERY_SELECTED,'onClick="setAction(\'view_query\')" style="cursor:hand" align="absmiddle"') . '&nbsp;';
								}
								echo '<a href="' . tep_href_link(FILENAME_EVENTS_PAYMENTS) . "'>" . tep_image_button("button_back.gif",IMAGE_BACK) . '</a>';
								//if (!$pay_request) echo '&nbsp;' . '<a href="' . tep_href_link(FILENAME_EVENTS_PAYMENTS_DETAIL,tep_get_all_get_params(array("action","default_entry")) . "action=add_default") . '">' . tep_image_button("button_add_default.gif",IMAGE_ADD_DEFAULT,'align="absmiddle"') . "</a>";
							?>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		</form>
		<?php if (!$pay_request) { ?>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '25'); ?></td>
		</tr>
		<tr>
			<td class="pageHeading"><?php echo TEXT_ADD_EXTRA;?></td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<?php 	echo tep_draw_form("search_customers",FILENAME_EVENTS_PAYMENTS_DETAIL,tep_get_all_get_params(array("action","default_entry"))); 
				echo '<input type=hidden name=post_action value="search_customers">';
		?>
		<tr>
			<td>
				<table border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td class="main"><?php echo tep_draw_input_field('customer_name','','onFocus="javascript:toggleList(1);" onBlur="javascript:toggleList(0);" style="width:400px;"');?>&nbsp;</td>
						<td class="main"><span class="cursorhand"><?php echo tep_image_button('button_search_customers.gif',IMAGE_SEARCH_CUSTOMERS,'onClick="javascript:setSearchAction(\'search_customers\');"');?></span></td>
						<td class="main"><span class="cursorhand"><?php //echo tep_image_button('button_add.gif',IMAGE_ADD_SELECT_CUSTOMER,'onClick="javascript:setSearchAction(\'add_customer_id\');"');?></span></td>
					</tr>
					<tr>
						<td colspan="3">
							<div id="customer_list" style="display:none;width:400px">
								<?php echo $SelectCustomerBox;?>
							</div>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		</form>
		<script language="JavaScript">
			var iex=(document.all);//ie
			var na6=(window.sidebar);//netscape 6.2
			
			if(na6) document.search_customers.customer_name.addEventListener("keyup",selectList,true);
			if(iex) document.search_customers.customer_name.onkeyup=selectList;
			if(na6) document.search_customers.customer_name.addEventListener("keydown",scrollList,true);
			if(iex) document.search_customers.customer_name.onkeydown=scrollList;
			function setSearchAction(type){
				if (document.search_customers.customer_name.value=="" || document.search_customers.customers_id.selectedIndex<=-1){
					alert("<?php echo addslashes(stripslashes(ERR_SELECT_CUSTOMER));?>");
					return;
				}
				document.search_customers.post_action.value=type;
				document.search_customers.submit();
			}
			function toggleList(mode){
				var listbox=document.getElementById("customer_list");
				var textbox=document.search_customers.customer_name;
				var element=document.search_customers.customers_id.options.length;
				if (mode==0){
					listbox.style.display="none";
					setListboxText(1);
				} else if (element) {
					listbox.style.display="";
					listbox.style.clientLeft=textbox.style.clientLeft;
					listbox.style.clientTop=textbox.style.clientTop;
				}
			}
			function scrollList(e){
				var listbox=document.search_customers.customers_id;

				if (iex) keyCode=window.event.keyCode;
				if (na6) keyCode=e.which;

				if (keyCode==40 || keyCode==38){
					if (keyCode==40 && listbox.selectedIndex>=0 && listbox.selectedIndex<listbox.options.length-1){
						listbox.selectedIndex+=1;
					} else if (keyCode==38 && listbox.selectedIndex>0 && listbox.selectedIndex<=listbox.options.length-1){
						listbox.selectedIndex-=1;
					}

					setListboxText(0);
					if (iex) window.event.returnValue=0;
					if (na6) e.returnValue=0;
					return;

				}
			}
			function selectList(e){
				var listbox=document.search_customers.customers_id;
				var search_word=document.search_customers.customer_name.value;
				var text_length;
				var found=-1;
				var listbox_word;
				var eventObject;
				var keyCode=0;
				var fromPos=0;
				var toPos=0;
	
				if (!listbox.options || listbox.options.length<=0) return;

				if (search_word!="") {
					search_word=search_word.toLowerCase();
					found=binaryFind(listbox,search_word,0,listbox.options.length-1);
					if (found>=0)
						listbox.selectedIndex=found;
				}

			}
			function setListboxText(mode){
				var listbox=document.search_customers.customers_id;
				if (!listbox.options || listbox.options.length<=0 || listbox.selectedIndex<=-1) return;
				var textbox_name=document.search_customers.customer_name;
				if (textbox_name.value=="" && mode==1) return "";
				textbox_name.value=listbox.options[listbox.selectedIndex].innerHTML;
			}
			function binaryFind(listBox,fWord,sPos,ePos){
				var listElement;
				var tWord;
				var mPos;
				if (sPos>ePos) return -1;

				mPos=sPos+Math.ceil((ePos-sPos)/2);
				
				if (mPos<0 || mPos>=listBox.options.length) return -1;
				listElement=listBox.options[mPos].innerHTML.toLowerCase();
				tWord=listElement.substr(0,fWord.length);

				if (tWord==fWord) return mPos;
				if (mPos<=0 || mPos>=listBox.options.length) return -1;
				
				if (fWord<tWord) return binaryFind(listBox,fWord,mPos+1,ePos);
				else return binaryFind(listBox,fWord,sPos,mPos-1);
			}
		</script>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '25'); ?></td>
		</tr>
		<tr>
			<td class="pageHeading"><?php echo TEXT_ADD_CUSTOMER;?></td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<script language="javascript">
			function validateAddForm(frm){
				var error_result="";
				if (frm.customers_firstname.value=="") 
					error_result="<?php echo addslashes(stripslashes(ERR_FIRSTNAME));?>";
				else if (frm.customers_lastname.value=="")
					error_result="<?php echo addslashes(stripslashes(ERR_LASTNAME));?>";
				else if (frm.customers_telephone.value=="")
					error_result="<?php echo addslashes(stripslashes(ERR_TELEPHONE));?>";
				else if(frm.customers_second_phone.value=="")
					error_result="<?php echo addslashes(stripslashes(ERR_SECOND_PHONE));?>";
				else if(frm.customers_age.value=="")
					error_result="<?php echo addslashes(stripslashes(ERR_AGE));?>";
				else if(frm.customers_street_address.value=="")
					error_result="<?php echo addslashes(stripslashes(ERR_STREET_ADDRESS));?>";
				else if(frm.customers_suburb.value=="")
					error_result="<?php echo addslashes(stripslashes(ERR_SUBURB));?>";
				else if(frm.customers_postal_code.value=="")
					error_result="<?php echo addslashes(stripslashes(ERR_POSTAL_CODE));?>";
				else if(frm.customers_city.value=="")
					error_result="<?php echo addslashes(stripslashes(ERR_CITY));?>";
				else if(frm.customers_state.value=="")
					error_result="<?php echo addslashes(stripslashes(ERR_STATE));?>";
				else if(frm.customers_country.value=="")
					error_result="<?php echo addslashes(stripslashes(ERR_COUNTRY));?>";
				else if(frm.customers_amount.value=="")
					error_result="<?php echo addslashes(stripslashes(ERR_AMOUNT));?>";
				else if(frm.customers_emailaddress.value=="")
					error_result="<?php echo addslashes(stripslashes(ERR_EMAILADDRESS));?>";
			    else if(val<1 || val==(frm.customers_emailaddress.value.length-1))
					  error_result="<?php echo addslashes(stripslashes(ERR_EMAIL));?>";
			    else if(frm.customers_second_email.value=="")
					error_result="<?php echo addslashes(stripslashes(ERR_SECOND_EMAIL));?>";
				
				if (error_result!=""){
					alert(error_result);
					return false;
				}
				return true;
			}
		</script>
		<?php echo tep_draw_form("new_payment",FILENAME_EVENTS_PAYMENTS_DETAIL,tep_get_all_get_params(array("action","default_entry")),'post','onSubmit="javascript:return validateAddForm(this);"');
				echo '<input type=hidden name=post_action value="add_customer">';
		?>
		<tr>
			<td>
				<table cellpadding="2" cellspacing="2" class="formArea" border="0">
					<tr>
						<td class="tableHeading"><?php echo TABLE_FIRSTNAME;?></td>
						<td class="tableHeading"><?php echo TABLE_LASTNAME;?></td>
						<td class="tableHeading" width="20"><?php echo TABLE_PAID;?></td>
						<td class="tableHeading" width="130"><?php echo TABLE_PAID_TYPE;?></td>
						<td class="tableHeading" width="130"><?php echo TABLE_TELEPHONE;?></td>
					</tr>
					<?php 
						$type_array=array(	array('id'=>TEXT_CREDIT_CARD,'text'=>TEXT_CREDIT_CARD),
											array('id'=>TEXT_CHEQUE,'text'=>TEXT_CHEQUE),
											array('id'=>TEXT_MONEY_ORDER,'text'=>TEXT_MONEY_ORDER),
											array('id'=>TEXT_CASH,'text'=>TEXT_CASH)
											);
					?>
					<tr>
						<td class="main"><?php echo tep_draw_input_field("customers_firstname",'','size=35');?></td>
						<td class="main"><?php echo tep_draw_input_field("customers_lastname",'','size=35');?></td>
						<?php /*<td class="main" width="150"><script language="javascript">dateStart.writeControl(); dateStart.dateFormat="yyyy-MM-dd";</script></td>
						<td class="main" width="150"><script language="javascript">dateEnd.writeControl(); dateEnd.dateFormat="yyyy-MM-dd";</script></td>*/ ?>
						<td class="main"><?php echo tep_draw_checkbox_field("customers_paid",'Y');?></td>
						<td class="main"><?php echo tep_draw_pull_down_menu("customers_paid_type",$type_array);?></td>
						<td class="main"><?php echo tep_draw_input_field("customers_telephone",'','size=10');?></td>
					</tr>
					<tr>
						<td class="tableHeading" width="130"><?php echo TABLE_SOCOND_PHONE;?></td>
						<td class="tableHeading" width="130"><?php echo TABLE_AGE;?></td>
						<td class="tableHeading" colspan="2" width="130"><?php echo TABLE_STREET_ADDRESS;?></td>
						<td class="tableHeading" width="130"><?php echo TABLE_SUBURB;?></td>
					</tr>
					<tr>
						<td class="main"><?php $required='<font color=red>*</font>';
						echo tep_draw_input_field("customers_second_phone",'','size=35').$required;?></td>
						<td class="main"><?php echo tep_draw_input_field("customers_age",'','size=35').$required;?></td>
						<td class="main" colspan="2"><?php echo tep_draw_input_field("customers_street_address",'','size="35"').$required;?></td>
						<td class="main"><?php echo tep_draw_input_field("customers_suburb",'','size=35').$required;?></td>
					</tr>
					<tr>
						<td class="tableHeading" width="130"><?php echo TABLE_POSTAL_CODE;?></td>
						<td class="tableHeading" width="130"><?php echo TABLE_CITY;?></td>
						<td class="tableHeading" colspan="2" width="130"><?php echo TABLE_STATE;?></td>
						<td class="tableHeading" width="130"><?php echo TABLE_COUNTRY;?></td>
					</tr>
					<tr>
						<td class="main"><?php echo tep_draw_input_field("customers_postal_code",'','size=35').$required;?></td>
						<td class="main"><?php echo tep_draw_input_field("customers_city",'','size=35').$required;?></td>
						<td class="main" colspan="2">
						<?php $tep_query=tep_db_query("select configuration_value from configuration where configuration_title='zone'");
							  $tep_result=tep_db_fetch_array($tep_query);																		
						echo tep_draw_input_field("customers_state",tep_cfg_get_zone_name($tep_result['configuration_value']),'size=35').$required;?></td>
						<td class="main"><?php 
							  $tep_country_query=tep_db_query("select configuration_value from configuration where configuration_title='country'");
							  $tep_country_value=tep_db_fetch_array($tep_country_query); 
						echo tep_draw_pull_down_menu('customers_country',tep_get_countries(),$tep_country_value['configuration_value'],'style=width:230').$required;?></td>
					</tr>
					<tr>
						<td class="tableHeading" width="130"><?php echo TABLE_AMOUNT;?></td>
						<td class="tableHeading" width="130"><?php echo TABLE_EMAILADDRESS;?></td>
						<td class="tableHeading" colspan="2" width="130"><?php echo TABLE_SECOND_EMAIL;?></td>
					</tr>
					<tr>
						<td class="main"><?php echo tep_draw_input_field("customers_amount",'','size=35').$required;?></td>
						<td class="main"><?php echo tep_draw_input_field("customers_emailaddress",'','size=35').$required;?></td>
						<td class="main" colspan="2"><?php echo tep_draw_input_field("customers_second_email",'','size=35').$required;?></td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<tr>
			<td><?php echo tep_image_submit("button_add.gif",IMAGE_ADD_CUSTOMER);?></td>
		</tr>
		</form>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '15'); ?></td>
		</tr>
		<script language="javascript">
			function validateConfirmForm(frm){
				var error_result="";
				if (frm.select_agree.checked==false)
					error_result="<?php echo addslashes(stripslashes(ERR_SELECT_AGREE));?>";

				if (error_result!=""){
					alert(error_result);
					return false;
				}
				return true;
			}
		</script>
		<?php  if ($mode==1 && $found_results){
				echo tep_draw_form("confirm",FILENAME_EVENTS_PAYMENTS_DETAIL,tep_get_all_get_params(array("action","default_entry")),'post','onSubmit="javascript:return validateConfirmForm(this);"');
				echo '<input type=hidden name=post_action value="confirm_attendees">';
				
		?>
		<tr>
			<td>
				<table width="75%" border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td class="pageHeading"><?php echo TEXT_COMMENT_EVENT;?></td>
					</tr>
					<tr>
						<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
					</tr>
					<tr>
						<td class="main" width="100%"><?php echo tep_draw_textarea_field("comments",'',"100%",4);?></td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table width="75%" border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td class="main" valign="middle" width="30"><?php echo tep_draw_checkbox_field("select_agree",1);?></td>
						<td class="smallText" valign="middle"><?php echo TEXT_AGREE_SELECTED;?></td>
					</tr>
					<tr>
						<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
					</tr>
					<tr>
						<td class="main" colspan="2"><?php	echo tep_image_submit("button_submit_attendees.gif",IMAGE_SUBMIT_ATTENDEES); ?></td>
					</tr>
				</table>
			</td>
		</tr>
		</form>
		<?php } // mode 
			} // pay_request
		?>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '15'); ?></td>
		</tr>
	<?php } else if ($post_action=='view_query') { ?>
		<tr>
			<td class="pageHeading"><?php echo TEXT_VIEW_QUERY;?></td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<script language="JavaScript">
			function validateQueryForm(frm){
				var error_result="";
				if (frm.comment_text.value=="")
					error_result="<?php echo addslashes(stripslashes(ERR_COMMENT_QUERY));?>";
				
				if (error_result!=""){
					alert(error_result);
					return false;
				}
				return true;
			}
		</script>
		<?php	echo tep_draw_form("view_query",FILENAME_EVENTS_PAYMENTS_DETAIL,tep_get_all_get_params(array("action","default_entry")),"post",'onSubmit="javascript:return validateQueryForm(this);"');
				echo '<input type=hidden name="post_action" value="comment_query">';
		?>
		<tr>
			<td>
				<table cellpadding="2" cellspacing="2" class="formArea" width="100%">
					<tr>
						<td class="tableHeading"><?php echo TABLE_FIRSTNAME;?></td>
						<td class="tableHeading"><?php echo TABLE_LASTNAME;?></td>
						<td class="tableHeading" width="100"><?php echo TABLE_START_DATE;?></td>
						<td class="tableHeading" width="100"><?php echo TABLE_END_DATE;?></td>
						<td class="tableHeading" width="150"><?php echo TABLE_TELEPHONE;?></td>
					</tr>
					<?php $row_cnt=1;
					while($detail_result=tep_db_fetch_array($detail_query)) {
					//to get customers start_date and end_date
						if ($detail_result["customers_id"]>0)
							$session_query=tep_db_query("SELECT min(s.start_date) as sdate,max(s.start_date) as edate from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_EVENTS_SESSIONS . " s  where o.customers_id='" . (int)$detail_result["customers_id"] . 
														"' and o.orders_id=op.orders_id and op.products_id=s.sessions_id and s.events_id='" . $events_id . "'");
						else
							$session_query=tep_db_query("SELECT start_date as sdate,start_date as edate from " . TABLE_EVENTS_SESSIONS . " where sessions_id='" . (int)$sessions_id . "'");

						$session_result=tep_db_fetch_array($session_query);
						
						if ($row_cnt%2==0) $class="attributes-even";
						else $class="attributes-odd";
						echo '<input type=hidden name="entry_id[]" value="' . $detail_result["entry_id"] . '">';
					?>
					<tr class="<?php echo $class;?>">
						<?php 
								if ($detail_result["customers_id"]>0){
									echo '<td class="main" valign="top"><a href="' . tep_href_link(FILENAME_CUSTOMERS,'action=edit&cuID=' . $detail_result['customers_id']) . '">' . $detail_result["customers_firstname"] . '</a></td>';
									echo '<td class="main" valign="top"><a href="' . tep_href_link(FILENAME_CUSTOMERS,'action=edit&cuID=' . $detail_result['customers_id']) . '">' . $detail_result["customers_lastname"] . '</a></td>';
								} else {
									echo '<td class="main" valign="top">' . $detail_result["customers_firstname"] . '</td>';
									echo '<td class="main" valign="top">' . $detail_result["customers_lastname"] . '</td>';
								}
						?>
						<td class="main" valign="top"><?php echo format_date($session_result["sdate"]);?></td>
						<td class="main" valign="top"><?php echo format_date($session_result["edate"]);?></td>
						<td class="main" valign="top"><?php echo $detail_result["customers_telephone"];?></td>
					</tr>
					<?php }?>
				</table>
			</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '15'); ?></td>
		</tr>
		<tr>
			<td>
				<table cellpadding="2" cellspacing="2" width="100%">
					<?php while($history_result=tep_db_fetch_array($history_query)) {?>
						<tr>
							<td class="smallText" height="12" valign="top"><?php echo date('H:ma d-m-Y',strtotime($history_result["query_date"]));?><br></td>
						</tr>
						<tr>
							<td class="smallText"><?php echo $history_result["query_text"];?></td>
						</tr>
						<tr>
							<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
						</tr>
					<?php }?>
				</table>
			</td>
		</tr>		
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '15'); ?></td>
		</tr>
		<tr>
			<td class="pageHeading"><?php echo TEXT_COMMENT_QUERY;?></td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<tr>
			<td class="main" width="100%"><?php echo tep_draw_textarea_field("comment_text",'',"100%",4);?></td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<tr>
			<td>
				<?php 
					echo '<a href="' . tep_href_link(FILENAME_EVENTS_PAYMENTS_DETAIL,tep_get_all_get_params(array("action","default_entry"))) . '">' . tep_image_button("button_back.gif",IMAGE_BACK) . '</a>&nbsp;';
					echo tep_image_submit("button_submit.gif",IMAGE_SUBMIT_QUERY);
				?>
			</td>
		</tr>
		</form>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '15'); ?></td>
		</tr>
	<?php } else if ($post_action=='search_customers') {?>
		<script language="javascript">
			function validateSearchForm(frm){
				var error="<?php echo addslashes(stripslashes(ERR_SELECT_ORDER));?>";
				if (!frm.orders_id) return false;
				if (frm.orders_id.length){
					for (icnt=0;icnt<frm.orders_id.length;icnt++){
						if (frm.orders_id[icnt].checked) {
							error="";
							break;
						}
					}
				} else {
					if (frm.orders_id.checked) error="";
				}
				if (error!=""){
					alert(error);
					return false;
				}
				return true;
			}
			
		</script>
		<tr>
			<td class="pageHeading"><?php echo TEXT_ATTENDANCE_RESULTS;?></td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<tr>
			<td>
			<table border="0" cellpadding="0" cellspacing="0">
				<tr>
					<td class="TableHeading" width="150"><?php echo TEXT_CUSTOMER_NAME;?></td>
					<td class="main"><?php echo $customer_name;?></td>
				</tr>
				<tr>
					<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
				</tr>
				<tr>
					<td class="TableHeading"><?php echo TEXT_EVENT_NAME;?></td>
					<td class="main"><?php echo tep_get_events_name($events_id);?></td>
				</tr>
				<tr>
					<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
				</tr>
				<tr>
					<td class="TableHeading"><?php echo TEXT_SESSION_DATE;?></td>
					<td class="main"><?php echo format_date(tep_get_session_date($sessions_id));?></td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<?php 
			echo tep_draw_form("add_search_customers",FILENAME_EVENTS_PAYMENTS_DETAIL,tep_get_all_get_params(array("action")),'post','onSubmit="javascript:return validateSearchForm(this);"');
			echo '<input type=hidden name=post_action value="add_search_customers">';
			echo '<input type=hidden name=customers_id value="' . $customers_id . '">';
		?>
		<tr>
			<td>
				<table cellpadding="2" cellspacing="2" class="formArea" width="100%">
					<tr>
						<td class="tableHeading" width="20"> </td>
						<td class="tableHeading" width="100"><?php echo TABLE_ORDERS_ID;?></td>
						<td class="tableHeading"><?php echo TABLE_EVENTS_NAME;?></td>
						<td class="tableHeading" width="100"><?php echo TABLE_START_DATE;?></td>
						<td class="tableHeading" width="100"><?php echo TABLE_END_DATE;?></td>
						<td class="tableHeading" width="150"><?php echo TABLE_PAID_TYPE;?></td>
					</tr>
					<?php 	
						if ($found_results) {
							while($detail_result=tep_db_fetch_array($detail_query)){
					?>
								<tr>
									<td class="main" valign="middle"><?php echo tep_draw_radio_field("orders_id",$detail_result["orders_id"],false,"align=absmiddle");?></td>
									<td class="main" valign="top"><?php echo $detail_result["orders_id"];?></td>
									<td class="main" valign="top"><?php echo $detail_result["products_name"];?></td>
									<td class="main" valign="top"><?php echo format_date($detail_result["sdate"]);?></td>
									<td class="main" valign="top"><?php echo format_date($detail_result["edate"]);?></td>
									<td class="main" valign="top"><?php echo $detail_result["payment_method"];?></td>
								</tr>
					<?php 
							} // while loop
						} else { //found_results
					?>
						<tr>
							<td class="main" colspan="5"><?php echo TEXT_NO_SEARCH_RESULTS;?></td>
						</tr>
					<?php } ?>
				</table>
			</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<tr>
			<td>
			<?php 
				echo '<a href="' . $current_url . '">' . tep_image_button("button_back.gif",IMAGE_BACK) . '</a>&nbsp;';
				if ($found_results) echo tep_image_submit("button_add_selected.gif",IMAGE_ADD_CUSTOMER);
			?>
			</td>
		</tr>
		</form>
	<?php } ?>
	</table>
	</td>
	<!-- body_eof //-->
	</tr>
</table>
<!-- body_eof //-->
	<!-- footer //-->
	<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
	<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
<?php 
	function check_customers_added(){
		global $sessions_id,$table_attendees;
		$filter_check_query=tep_db_query("SELECT customers_id from " . $table_attendees . " where sessions_id='" . $sessions_id . "'");
		$filter_string="";
		if (tep_db_num_rows($filter_check_query)>0){
			while($result=tep_db_fetch_array($filter_check_query)){
				$filter_string.=(int)$result["customers_id"] . ",";
			}
			$filter_string=substr($filter_string,0,-1);
			$filter_string=" o.customers_id not in (" . $filter_string . ") and ";
		}
		return $filter_string;
	}
	
	// add default customers to the entry
	function add_default_customers(){
		global $table_attendees,$sessions_id,$events_id;
		$filter_string=check_customers_added();
		$customer_query=tep_db_query(	"SELECT o.orders_id,c.customers_id,c.customers_firstname,c.customers_lastname,c.customers_telephone,op.products_id as sessions_id,op.final_price,o.payment_method as customers_paid_type from " .
										TABLE_ORDERS . " o," . TABLE_ORDERS_PRODUCTS . " op," . TABLE_CUSTOMERS . " c where " . $filter_string . 
										" o.orders_id=op.orders_id and o.customers_id=c.customers_id and op.products_type='E' and op.events_type='R' and op.products_id='" . $sessions_id . "'");
		$ids="";
		while($customer_result=tep_db_fetch_array($customer_query)){

			$status_query=tep_db_query("SELECT orders_status_id from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id='" . $customer_result["orders_id"] . "'");
			$status=tep_db_fetch_array($status_query);
			unset($customer_result["orders_id"]);
			$customer_result["customers_paid"]=((int)$status["orders_status_id"]>1)?"Y":"N";
			$customer_result["events_id"]=$events_id;
			tep_db_perform($table_attendees,$customer_result);
			if ($table_attendees==TABLE_EVENTS_ATTENDEES_CONFIRM) $ids.=tep_db_insert_id() . ",";
		}
		//if ($ids!="") tep_message_add_customer(substr($ids,0,-1));
	}
?>