<?php

/*

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

  Copyright (c) 2005 osCommRes

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

Copyright 2007 Zac Inc 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();
	$action=isset($HTTP_GET_VARS["action"])?$HTTP_GET_VARS["action"]:'';
	$post_action=isset($HTTP_POST_VARS["post_action"])?$HTTP_POST_VARS["post_action"]:'';

	$command=isset($HTTP_GET_VARS['command'])?$HTTP_GET_VARS['command']:'';
	$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;
	$instructor_id=isset($HTTP_GET_VARS["iID"])?(int)$HTTP_GET_VARS["iID"]:0;
	$mode=isset($HTTP_GET_VARS["mode"])?$HTTP_GET_VARS["mode"]:'';
	$select_ids=isset($HTTP_GET_VARS["select_ids"])?$HTTP_GET_VARS["select_ids"]:'';

	$server_date = getServerDate(true);	
	// get search params;
	if ($mode!="1" && session_is_registered("search_payment")){
		//get query option from session
		$search_params=&$HTTP_SESSION_VARS["search_payment"];
		$action=search;
	} else {
		$search_params=array(	"start_date"=>isset($HTTP_POST_VARS["start_date"])?tep_convert_date_raw($HTTP_POST_VARS["start_date"]):'',
								"end_date"=>isset($HTTP_POST_VARS["end_date"])?tep_convert_date_raw($HTTP_POST_VARS["end_date"]):'',
								"events_id"=>isset($HTTP_POST_VARS["events_id"])?(int)$HTTP_POST_VARS["events_id"]:0,
								"instructor"=>isset($HTTP_POST_VARS["instructor_id"])?(int)$HTTP_POST_VARS["instructor_id"]:0
								);
	}
		
	if($mode==1) $table_attendees=TABLE_EVENTS_ATTENDEES;
	else $table_attendees=TABLE_EVENTS_ATTENDEES_CONFIRM;

	if($command!="") {
		switch($command) {
			case 'search_details':
				echo 'search_details^';
				table_write();
				exit;
			break;
			case 'payment_details':
				echo 'payment_details^';
				payment_details();
				exit;
			break;
			case 'close_payment_details':
				echo 'search_details^';
				table_write();
				exit;
			break;
			case 'query_selected':
			case 'view_query':
				echo 'query_selected^';
				query_selected_details();
				exit;
			break;
			case 'comment_query':
				echo 'comment_query^';
				$update_ids=split(',',$select_ids);
				$type=($mode==1?"A":"C");
				$comment_text=(isset($HTTP_GET_VARS["comment_text"])?tep_db_prepare_input($HTTP_GET_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 . "')");
				}
				//set the query flag
				tep_db_query("UPDATE " . $table_attendees . " set queried='Y' where entry_id in(" . $select_ids . ")");
				payment_details();
				exit;
			break;	
			case 'add_default':
				echo 'payment_details^';
				$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 ";
				}
				//select the possible customers of this session
				$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,o.payment_method as customers_paid_type,op.final_price 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='" . (int)$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;
					$customer_result["customers_paid_type"]=strip_tags($customer_result["customers_paid_type"]);
					$customer_result["customers_paid_type"]=trim($customer_result["customers_paid_type"],'&nbsp;');
					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));
				payment_details();
				exit;
			break;
			case 'delete_customer':
			case 'remove_query':
				echo 'payment_details^';
				if($command=='delete_customer') {
					$type=($mode==1?"A":"C");
					tep_db_query("DELETE from " . $table_attendees . " where entry_id in(" . $select_ids . ")");
					tep_db_query("DELETE from ". TABLE_EVENTS_ATTENDEES_QUERY . " where attendees_id in (" . $select_ids . ") and attendees_type='" . $type . "'");
				}
				else if($command=='remove_query') {
					tep_db_query("UPDATE " . $table_attendees . " set queried='N' where entry_id in(" . $select_ids . ")");
				}
				payment_details();
				exit;
			break;	
			case 'search_customers':
				echo 'search_customers^';
				search_customer_details();
				exit;
			break;	
			case 'add_selected_customer':
				echo 'add_selected_customer^';
				$select_oids=(isset($HTTP_GET_VARS["select_oids"])?$HTTP_GET_VARS["select_oids"]:'');
				$events_name=tep_get_events_name($events_id);
				$session_date=tep_get_session_date($sessions_id);
				//select all details of selected customer_ids
				$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 ='" . $select_oids . "' " . 
												" 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["customers_paid_type"]=strip_tags($customer_result["customers_paid_type"]);
				$customer_result["customers_paid_type"]=trim($customer_result["customers_paid_type"],'&nbsp;');
				$customer_result["events_id"]=$events_id;
				$customer_result["sessions_id"]=$sessions_id;
				tep_db_perform($table_attendees,$customer_result);
	
				$ids=tep_db_insert_id();
	
				//send a message to instructor
				if ($table_attendees==TABLE_EVENTS_ATTENDEES_CONFIRM) tep_message_add_customer($ids);
				
				// $type='A' ->Query For Attendees, $type='C' ->Query for Confirmed Attendees
				$type=($mode==1?"A":"C");
			
				$query_check=tep_db_query("SELECT op.products_id from " . TABLE_ORDERS_PRODUCTS . " op where op.orders_id='" . $select_oids . "' and op.products_id='" . $sessions_id ."'");
				
				if (tep_db_num_rows($query_check)==0){
					//add query for added customer;
					$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=>$select_oids,
											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);
				}
				payment_details();
				exit;
			break;
			case 'add_customers':
				echo 'add_customers^';
				$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);
				// send a message to instructor
				if ($table_attendees==TABLE_EVENTS_ATTENDEES_CONFIRM) tep_message_add_customer(tep_db_insert_id());
				payment_details();
				exit;
			break;
			case "confirm_attendees": // make the attendees to be valid and confirmed by admin
				echo 'search_details^';
				$query=tep_db_query("SELECT at.events_id,at.sessions_id,at.customers_id,at.entry_id,at.customers_telephone,at.customers_firstname,at.customers_lastname,at.customers_query,at.customers_paid,at.customers_paid_type,at.queried,at.final_price 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'");
				}
				//send a message to instructor
				if ($ids!="") tep_message_add_customer(substr($ids,0,-1));
	
				$comments=(isset($HTTP_GET_VARS["comments"])?$HTTP_GET_VARS["comments"]:'');
				if ($comments!="") tep_message_comments($comments);
	
				tep_db_query("DELETE from " . TABLE_EVENTS_ATTENDEES . " where sessions_id='" . $sessions_id . "'");
				table_write();
				exit;
			break;
			case 'note_paid':
				echo 'search_details^';
				if (isset($HTTP_POST_VARS["entry_id"]) && isset($HTTP_POST_VARS["pay_details"])){
				$entry_id=&$HTTP_POST_VARS["entry_id"];
				$ids="";
				for ($icnt=0;$icnt<sizeof($entry_id);$icnt++){
					$pay_details=&$HTTP_POST_VARS["pay_details"][$entry_id[$icnt]];
					$sql_array=array(	"invoice_no"=>$pay_details["invoice_no"],
										"payment_type"=>$pay_details["payment_type"],
										"payment_date"=>tep_convert_date_raw($pay_details["payment_date"]),
										"paid"=>'Y'
										);
					tep_db_perform(TABLE_EVENTS_INSTRUCTOR_PAYMENTS,$sql_array,"update","entry_id='" . $entry_id[$icnt] . "'");
					$ids.=$entry_id[$icnt] . ",";
				}
				if ($ids!="") tep_message_paid_amount(substr($ids,0,-1));
				}
				table_write();
				exit;
			break;
		}	
	}
	$found_results=false;
	$instructor_query="SELECT a.admin_lastname,a.admin_firstname,a.admin_email_address,a.admin_id from " . TABLE_ADMIN . " a, " . TABLE_ADMIN_GROUPS . " ag where ag.admin_groups_name='" . TEXT_INSTRUCTOR_ENTRY . "' and ag.admin_groups_id=a.admin_groups_id order by a.admin_lastname desc";
	$instructor_result=tep_db_query($instructor_query);
	$selected_name="";	
	$SelectInstructorBox = "<select name='instructor_id' size=10 onClick=\"javascript:setListboxText(0,'instructor');toggleList(0);\" onFocus=\"javascript:toggleList(1);\" onBlur=\"javascript:toggleList(0);\" class='customerSelect'>\n";
		if (tep_db_num_rows($instructor_result) > 0){
		// Query Successful
			while($db_Row = tep_db_fetch_array($instructor_result)){ 
				$SelectInstructorBox .= "<option value='" . $db_Row["admin_id"] . "'";
				if($db_Row["admin_id"]==$search_params["instructor"]){
					$SelectInstructorBox .= " SELECTED ";
					$selected_name=$db_Row["admin_lastname"] . ' ' . $db_Row["admin_firstname"] . ", " . $db_Row["admin_email_address"];
				}
				$SelectInstructorBox .= ">" . $db_Row["admin_lastname"] . ' ' .  $db_Row["admin_firstname"] . ", " . $db_Row["admin_email_address"] . " (" . $db_Row["admin_id"]. ") </option>\n";
			}
		}
	$SelectInstructorBox .= "</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>
<script language="JavaScript" src="includes/date-picker.js"></script>
<script language="javascript" src="includes/http.js"></script>
<script language="javascript">
	function do_submit() {
		document.getElementById('search_result').style.display='';
		document.getElementById('search_result').innerHTML=document.getElementById('loading').innerHTML;
		command='<?php echo tep_href_link(FILENAME_EVENTS_ADMIN_PAYMENTS_NEW); ?>?command=search_details&mode=1';
		do_post_command('search_payment',command);
	}
	function do_payment_details(params) {
		document.getElementById('input_table').style.display="none";
		document.getElementById('search_result').style.display="none";
		document.getElementById('payment_details').style.display="";
		document.getElementById('payment_details').innerHTML=document.getElementById('loading').innerHTML;
		command='<?php echo tep_href_link(FILENAME_EVENTS_ADMIN_PAYMENTS_NEW); ?>?command=payment_details&'+params;
		do_get_command(command);
	}
	function close_payment_detail() {
		document.getElementById('payment_details').style.display="none";
		document.getElementById('input_table').style.display="";
		document.getElementById('search_result').style.display="";
		document.getElementById('search_result').innerHTML=document.getElementById('loading').innerHTML;
		command='<?php echo tep_href_link(FILENAME_EVENTS_ADMIN_PAYMENTS_NEW); ?>?command=close_payment_details';
		do_get_command(command);
	}
	function close_search_details(params) {
		document.getElementById('search_customers').style.display="none";
		document.getElementById('payment_details').style.display="";
		document.getElementById('payment_details').innerHTML=document.getElementById('loading').innerHTML;
		command='<?php echo tep_href_link(FILENAME_EVENTS_ADMIN_PAYMENTS_NEW); ?>?command=payment_details&'+params;
		do_get_command(command);
	}
	function setAction(type,params){
		if(type=='query_selected' || type=='delete_customer' || type=='remove_query') {
			if (!validateCustomersForm(document.search_payment)) {
				return;
			}
			
			var ids="";
			var entry=document.search_payment.elements["entry_id[]"];
			if (entry.length){
				for (icnt=0;icnt<entry.length;icnt++){
					if(entry[icnt].checked)
					ids+=entry[icnt].value + ",";	
				}
			} else {
				if(entry.checked)
					ids=entry.value + ",";
			}
			var ids_length=ids.length;
			var select_ids=ids.substr(0,(ids_length-1));
			if(type=='query_selected') {
				document.getElementById('payment_details').style.display='none';
				document.getElementById('selected_query').style.display='';
				document.getElementById('selected_query').innerHTML=document.getElementById('loading').innerHTML;
			}
			else if(type=='delete_customer' || type=='remove_query') {
				document.getElementById('payment_details').innerHTML=document.getElementById('loading').innerHTML;
			}
		command='<?php echo tep_href_link(FILENAME_EVENTS_ADMIN_PAYMENTS_NEW); ?>?command='+type+'&select_ids='+select_ids+'&'+params;
		}
		else if(type=='search_customers') {
			var textbox=document.search_payment.customer_name;
			var listbox=document.search_payment.customer_id;

			if (textbox.value=="" || listbox.selectedIndex<=-1){
				alert("<?php echo addslashes(stripslashes(ERR_SELECT_CUSTOMER));?>");
				return;
			}
			else {
				document.getElementById('payment_details').style.display='none';
				document.getElementById('search_customers').style.display='';
				document.getElementById('search_customers').innerHTML=document.getElementById('loading').innerHTML;
				var cid=listbox[listbox.selectedIndex].value;
				command='<?php echo tep_href_link(FILENAME_EVENTS_ADMIN_PAYMENTS_NEW); ?>?command='+type+'&customer_id='+cid+'&'+params;
			}
				
		}
		do_get_command(command);

	}
	function add_selected_customer(params) {
		var error="<?php echo addslashes(stripslashes(ERR_SELECT_ORDER));?>";
		var oids="";
		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) {
					oids=frm.orders_id[icnt].value;
					error="";
					break;
				}
			}
		} else {
			if (frm.orders_id.checked) {
			oids=frm.orders_id.value;
			error="";
			}
		}
	//	var ids_length=oids.length;
	//	var select_oids=oids.substr(0,(ids_length-1));
		var select_oids=oids;
		
		if(error!="") {
			alert(error);
			return;
		}	
		else {
			document.getElementById('search_customers').style.display='none';
			document.getElementById('payment_details').style.display='';
			document.getElementById('payment_details').innerHTML=document.getElementById('loading').innerHTML;
			command='<?php echo tep_href_link(FILENAME_EVENTS_ADMIN_PAYMENTS_NEW); ?>?command=add_selected_customer&select_oids='+select_oids+'&'+params;
			do_get_command(command);
		}	
	}
	function add_customers(params) {
		if(!validateAddForm(document.search_payment))
			return;
		else {
			//document.getElementById('payment_details').innerHTML=document.getElementById('loading').innerHTML;
			command='<?php echo tep_href_link(FILENAME_EVENTS_ADMIN_PAYMENTS_NEW); ?>?command=add_customers&'+params;
			do_post_command('search_payment',command);
		}	
	}
	function view_query_content(index,params) {
		var entry=document.search_payment.elements["entry_id[]"];
		if (!entry) return;
		if (entry.length){
			entry[index].checked=true;
			var ids=entry[index].value
		} else {
			entry.checked=true;
			var ids=entry.value
		}
		document.getElementById('payment_details').style.display='none';
		document.getElementById('selected_query').style.display='';
		document.getElementById('selected_query').innerHTML=document.getElementById('loading').innerHTML;

		command='<?php echo tep_href_link(FILENAME_EVENTS_ADMIN_PAYMENTS_NEW); ?>?command=view_query&select_ids='+ids+'&'+params;
		do_get_command(command);
	}
	function close_query_selected(params) {
		document.getElementById('selected_query').style.display='none';
		document.getElementById('payment_details').style.display='';
		document.getElementById('payment_details').innerHTML=document.getElementById('loading').innerHTML;
		command='<?php echo tep_href_link(FILENAME_EVENTS_ADMIN_PAYMENTS_NEW); ?>?command=payment_details&'+params;
		do_get_command(command);
	}
	function comment_query(params,select_ids) {
		var error_result="";
		var comment_text=document.search_payment.comment_text.value;
		if (comment_text=="") {
			error_result="<?php echo "Please Enter Comment Details";?>";
			alert(error_result);
			return;
		}	
		else {
			document.getElementById('selected_query').style.display='none';
			document.getElementById('payment_details').style.display='';
			document.getElementById('payment_details').innerHTML=document.getElementById('loading').innerHTML;
			command='<?php echo tep_href_link(FILENAME_EVENTS_ADMIN_PAYMENTS_NEW); ?>?command=comment_query&select_ids='+select_ids+'&comment_text='+comment_text+'&'+params;
			do_get_command(command);
		}
	}
	function add_default(params) {
		document.getElementById('payment_details').innerHTML=document.getElementById('loading').innerHTML;
		command='<?php echo tep_href_link(FILENAME_EVENTS_ADMIN_PAYMENTS_NEW); ?>?command=add_default&'+params;
		do_get_command(command);
	}
	function confirm_attendees(params) {
		if (document.search_payment.select_agree.checked==false) {
			error_result="<?php echo addslashes(stripslashes(ERR_SELECT_AGREE));?>";
			alert(error_result);
			return;
		}	
		else {
		comments=document.search_payment.comments.value;
		document.getElementById('payment_details').style.display='none';
		document.getElementById('input_table').style.display='';
		document.getElementById('search_result').style.display='';
		document.getElementById('search_result').innerHTML=document.getElementById('loading').innerHTML;
		command='<?php echo tep_href_link(FILENAME_EVENTS_ADMIN_PAYMENTS_NEW); ?>?command=confirm_attendees&comments='+comments+'&'+params;
		do_get_command(command);
		}
	}
	function note_paid() {
		if(!validateNoteForm(document.search_payment))
			return;
		else {
			command='<?php echo tep_href_link(FILENAME_EVENTS_ADMIN_PAYMENTS_NEW); ?>?command=note_paid&mode=2';
			do_post_command('search_payment',command);
		}
	}
	function do_result(result) {
		token=result.split('^');
		if(token[0]!="") {
			switch(token[0]) {
				case 'search_details': 
					document.getElementById('search_result').innerHTML=token[1];
				break;	
				case 'payment_details':
				case 'comment_query':
				case 'add_selected_customer':
				case 'add_customers':
					document.getElementById('payment_details').innerHTML=token[1];
				break;	
				case 'query_selected':
					document.getElementById('selected_query').innerHTML=token[1];
				break;	
				case 'search_customers':
					document.getElementById('search_customers').innerHTML=token[1];
				break;	
				case 'add_customers':
					document.getElementById('payment_details').innerHTML=token[1];
					clear_value(document.search_payment);
				break;
			}		
		}
	}
	function selectAll(checkbox){
		var entry=document.search_payment.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.search_payment.select_all_customers.checked=false;
	}

	function validateCustomersForm(frm){
		var error_result="";
		var entry=frm.elements["entry_id[]"];
		if (!entry) return;
		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 validateAddForm(frm){
		var error_result="";
		var val=frm.customers_emailaddress.value.indexOf('@');
		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;
}

function clear_value(frm) {
		frm.customers_firstname.value=frm.customers_lastname.value=frm.customers_telephone.value=frm.customers_second_phone.value="";
		frm.customers_age.value=frm.customers_street_address.value=frm.customers_suburb.value=frm.customers_postal_code.value="";
		frm.customers_city.value=frm.customers_state.value=frm.customers_country.value=frm.customers_amount.value="";
		frm.customers_emailaddress.value=frm.customers_emailaddress.value=frm.customers_second_email.value="";
}
	function validateNoteForm(frm){
		var error="";
		var icnt;
		var selected=false;
		
		if (frm.elements["entry_id[]"]){
		
			if (frm.elements["entry_id[]"].length){
				for (icnt=0;icnt<frm.elements["entry_id[]"].length;icnt++){
					if (frm.elements["entry_id[]"][icnt].checked) {
						selected=true;
						error=checkInvoiceDetails(frm.elements["entry_id[]"][icnt].value);
						if (error!="") break;
					}
				}
				if (!selected) error="<?php echo addslashes(stripslashes(ERR_SELECT_PAYMENT));?>";
			} else {
				if (!frm.elements["entry_id[]"].checked){
					error="<?php echo addslashes(stripslashes(ERR_SELECT_PAYMENT));?>";
				} else {
					error=checkInvoiceDetails(frm.elements["entry_id[]"].value);
				}
			}
		} else {
			error="<?php echo addslashes(stripslashes(ERR_SELECT_PAYMENT));?>";				
		}
		if (error!=""){
			alert(error);
			return false;
		}
		return true;
	}
			function checkInvoiceDetails(id){
				var element;
				element=document.search_payment.elements["pay_details["+id+"][invoice_no]"];
				if (element.value=="") return "<?php echo addslashes(stripslashes(ERR_INVOICE_NO));?>";
				element=document.search_payment.elements["pay_details["+id+"][payment_date]"];
				if (element.value=="") return "<?php echo addslashes(stripslashes(ERR_INVOICE_DATE));?>";
				if (!checkDate(element.value)) return "<?php echo addslashes(stripslashes(ERR_INVOICE_DATE_INCORRECT));?>";
				return "";
			}
			function checkDate(date){
				var dateExp="^<?php echo str_replace(array("d","m","Y"),array("([0-9]{1,2})","([0-9]{1,2})","([0-9]{4})"),EVENTS_DATE_FORMAT);?>$"
				var regExp=new RegExp(dateExp);
				var dateFormat="<?php echo EVENTS_DATE_FORMAT;?>";
				var dateSplt;
				var day;
				var month;
				var year;
				
				var result=regExp.exec(date);
				if (result==null) return false;
				if (dateFormat=="") return false;
				dateSplt=dateFormat.split("-");

				for (var icnt=1;icnt<dateSplt.length;icnt++){
					if (result[icnt].substr(0,1)=="0") result[icnt]=result[icnt].substr(1);
					if (dateSplt[icnt-1]=="d") {  // check for day{
						day=parseInt(result[icnt]);
						if (day<1 || day>31) return false;
					} else if (dateSplt[icnt-1]=="m") {// check for month
						month=parseInt(result[icnt]);
						if (month<1 || month>12) return false;
					} else if (dateSplt[icnt-1]=="Y") { // check for year
						year=parseInt(result[icnt]);
						if (year<=1) return false;
					}
				}
				if ((month==4 || month==6 || month==9 || month==11) && day==31)	return false;

				if (month==2){
					var isleap=(year%4==0 && (year%100!=0 || year%400!=0));
					if (day>29 || ( day==29 && !isleap)){
						return false;
					}
				}
				return true;
			}
			function checkSelection(boxName){
				if (!boxName.checked) return;
				var frm=document.search_payment;
				var sid=boxName.value;
				var query=frm.elements["query_check["+sid+"]"];
				if (!query) return;
				if (parseInt(query.value)>0){
					alert("<?php echo addslashes(stripslashes(ERR_QUERY_EXISTS));?>");
					boxName.checked=false;
				}
			}

</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%" valign="top">
	<div id="loading" style="display:none"><div style="height:30px;margin-left:30px"><img src="images/24-1.gif"></div></div>
	<?php echo tep_draw_form("search_payment",FILENAME_EVENTS_ADMIN_PAYMENTS_NEW,"");	?>
	<table border="0" width="100%" cellspacing="0" cellpadding="0">

		<tr><td class="pageHeading"><?php echo HEADING_FIND_PAYMENTS;?></td></tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<tr>
		<td>
		<!--<table border="0" cellspacing="2" cellpadding="2" class="formArea">!-->
		<table id="input_table" border="0" cellpadding="2" cellspacing="2" class="searchArea">
		
			<tr>
				<td class="main"><?php echo TEXT_INSTRUCTOR;?></td>
				<td class="main" colspan="3"><?php echo tep_draw_separator('pixel_trans.gif', '24', '1') . tep_draw_input_field('instructor_name',$selected_name,'onFocus="javascript:toggleText(1,\'instructor\');" onBlur="javascript:toggleText(0,\'instructor\');" style="width:400px;"');?>
				<div id="instructor_list" style="display:none;width:400px;position:absolute;top:130px;left:241px">
					<?php echo $SelectInstructorBox;?>
				</div>
				</td>
			</tr>
			<tr><?php $_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,EVENTS_DATE_FORMAT);?>
				<td class="main"><?php echo TEXT_START_DATE;?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '1') . tep_draw_input_field("start_date",format_date($search_params["start_date"]),"size=10",false,'text',false);?>
					<a href="javascript:show_calendar('search_payment.start_date',null,null,'<?php echo $date_format;?>');"
					  onmouseover="window.status='Date Picker';return true;"
					  onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/>  
					</a>
				</td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '60', '1') . TEXT_END_DATE;?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '1') . tep_draw_input_field("end_date",format_date($search_params["end_date"]),"size=10",false,'text',false);?>
					<a href="javascript:show_calendar('search_payment.end_date',null,null,'<?php echo $date_format;?>');"
					  onmouseover="window.status='Date Picker';return true;"
					  onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/>  
					</a>
				</td>
			</tr>
			<tr>
				<td class="main"><?php echo TEXT_SELECT_EVENT;?></td>
				<td class="main">
				<?php 
					$events_array=tep_get_events_array(TEXT_ALL);
					echo tep_draw_separator('pixel_trans.gif', '24', '1') . tep_draw_pull_down_menu("events_id",$events_array,$search_params["events_id"]);
				?>
				</td>
				<td colspan="2" class="main"><?php echo tep_draw_separator('pixel_trans.gif', '60', '1') . tep_draw_checkbox_field("hidden",1) . '&nbsp;' . TEXT_SHOW_HIDDEN;?></td>
			</tr>
			<tr>
				<td colspan="4"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
			</tr>
			<tr>
				<td colspan="4" align="left"><?php echo '<a href="javascript:do_submit();">' . tep_image_button("button_search.gif",IMAGE_SEARCH_PAYMENTS) . '</a>'; ?></td>
			</tr>
			<script language="JavaScript">
			var iex=(document.all);//ie
			var na6=(window.sidebar);//netscape 6.2
			
			if(na6) document.search_payment.instructor_name.addEventListener("keyup",selectList,true);
			if(iex) document.search_payment.instructor_name.onkeyup=selectList;
			if(na6) document.search_payment.instructor_name.addEventListener("keydown",scrollList,true);
			if(iex) document.search_payment.instructor_name.onkeydown=scrollList;
			
				
			var currentField="";
		var intervalId;
			function checkdropDown(varName){
				if (currentField==""){
					clearInterval(intervalId);
					hideList(varName);
					intervalId="";
				}
			}
			function toggleText(mode,varName){
				if (mode==1) {
					currentField="Text";
					showList(varName);
					if (intervalId) clearInterval(intervalId);
					intervalId=setInterval("checkdropDown('"+varName+"')",300);
				} else {
					currentField="";
				}
			}
			function toggleList(mode){
				if (mode==1){
					currentField="drop";
				} else {
					currentField="";
				}
			}
			function showList(varName){
				var listbox=document.getElementById(varName+"_list");
				var textbox=document.search_payment.elements[varName+"_name"];
				listbox.style.display="";
				if(window.scrollMaxX >0)
					listbox.style.left=textbox.offsetLeft+82;
				else 
					listbox.style.left=textbox.offsetLeft+220;
				listbox.style.top=textbox.offsetTop+174;
			}
			function hideList(varName){
				var listbox=document.getElementById(varName+"_list");
				var textbox=document.search_payment.elements[varName+"_name"];
				setListboxText(1,varName);
				listbox.style.display="none";
			}
			function scrollList(e){
				var listbox=document.search_payment.instructor_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,'instructor');
					if (iex) window.event.returnValue=0;
					if (na6) e.returnValue=0;
					return;

				}
			}
			
			function selectList(e){
				var listbox=document.search_payment.instructor_id;
				var search_word=document.search_payment.instructor_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,varName){
				var listbox=document.search_payment.elements[varName+'_id'];
				if (!listbox.options || listbox.options.length<=0 || listbox.selectedIndex<=-1) return;
				var textbox_name=document.search_payment.elements[varName+'_name'];
				if (textbox_name.value=="" && mode==1) {
					listbox.selectedIndex=-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>
		</table>
		</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>

	<tr><td><div id="search_result" style="display:none;"></div></td></tr>
	<tr><td><div id="payment_details" style="display:none;"></div></td></tr>
	<tr><td><div id="selected_query" style="display:none;"></div></td></tr>
	<tr><td><div id="search_customers" style="display:none;"></div></td></tr>
	<script>
		if(document.search_payment.elements['customer_name']) {
			if(na6) document.search_payment.customer_name.addEventListener("keyup",selectList_customer,true);
			if(iex) document.search_payment.customer_name.onkeyup=selectList_customer;
			if(na6) document.search_payment.customer_name.addEventListener("keydown",scrollList_customer,true);
			if(iex) document.search_payment.customer_name.onkeydown=scrollList_customer;
		}	
			function scrollList_customer(e){
				var listbox=document.search_payment.customer_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,'customer');
					if (iex) window.event.returnValue=0;
					if (na6) e.returnValue=0;
					return;

				}
			}
			function selectList_customer(e){
				var listbox=document.search_payment.customer_id;
				var search_word=document.search_payment.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;
				}

			}
	</script>
	</table>
	</form>
	</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'); 
function table_write() {
	global $search_params, $currencies;
	$found_results=false;
	$query="SELECT s.events_id,s.start_date,op.products_id,op.products_name,e.instructors_rate_type,e.instructors_rate,e.instructors_base_rate,concat(a.admin_firstname,' ',a.admin_lastname) as instructor_name,e.events_instructor from " .
					TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s, " .  TABLE_EVENTS . " e, " . TABLE_ADMIN . " a " . 
					"where op.products_id=s.sessions_id and s.events_id=e.events_id and e.events_instructor=a.admin_id and op.products_type='E' and op.events_type='R'";
					
	//get from form submit
	$search_option="";
	if ($search_params["start_date"]!='') $search_option.=" and s.start_date>='" . $search_params["start_date"] . "' ";
	if ($search_params["end_date"]!='') $search_option.=" and s.start_date<='" . $search_params["end_date"] . "' ";
	if ($search_params["events_id"]!='' && (int)$search_params["events_id"]>0) $search_option.=" and s.events_id='" . $search_params["events_id"] . "' ";
	//if ($search_params["instructor"]!='') $search_option.=" and (a.admin_firstname like '%" . $search_params["instructor"] . "%' or a.admin_lastname like '%" . $search_params["instructor"] . "%')";
	if ($search_params["instructor"]!=0) $search_option.=" and a.admin_id='" . $search_params["instructor"] . "'";

	if (!$HTTP_SESSION_VARS["search_payment"]) tep_session_register("search_payment");

	tep_set_session_value("search_payment",$search_params);

	$query.=$search_option . "group by s.sessions_id,s.events_id order by instructor_name,op.products_name,s.start_date";

	//query for results
	$payment_query=tep_db_query($query);

	if (tep_db_num_rows($payment_query)>0) $found_results=true;
	$type_array=array(	array('id'=>'Direct Deposit','text'=>'Direct Deposit'),
						array('id'=>'Cheque','text'=>'Cheque'),
						array('id'=>'Cash','text'=>'Cash'),
						array('id'=>'BPay','text'=>'BPay'));
	if($found_results) {					
	?>	
	<table cellpadding="2" cellspacing="0" width="100%">
		<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
		<tr><td class="pageHeading"><?php echo HEADING_PAYMENTS;?></td></tr>
		<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
		<tr>
		<td>
		<?php //echo tep_draw_form("note_paid",FILENAME_EVENTS_ADMIN_PAYMENTS_NEW,'','post','onSubmit="return validateNoteForm(this);"'); 
    		 // echo '<input type="hidden" name="post_action" value="note_paid">';
		?>
		<table width="100%" border="0" cellspacing="2" cellpadding="2">
			<tr>
				<td class="tableHeading" width="10"> </td>
				<td class="tableHeading" width="10"> </td>
				<td class="tableHeading" width="100"><?php echo TABLE_INSTRUCTOR;?></td>
				<td class="tableHeading"><?php echo TABLE_EVENT_NAME;?></td>
				<td class="tableHeading" width="60"><?php echo TABLE_DATE;?></td>
				<td width="400">
					<table border="0" cellspacing="2" cellpadding="0" width="100%">
					<tr>
						<td class="tableHeading" width="90"><?php echo TABLE_AMOUNT;?></td>
						<td class="tableHeading" width="90"><?php echo TABLE_INVOICE;?></td>
						<td class="tableHeading" width="120"><?php echo TABLE_PAY_TYPE;?></td>
						<td class="tableHeading" width="100"><?php echo TABLE_PAY_DATE;?></td>
					</tr>
					</table>
				</td>
				<td class="tableHeading" align="center" width="20"><?php echo TABLE_PAID;?></td>
				<td class="tableHeading" align="center" width="20"><?php echo TABLE_QUERIED;?></td>
			</tr>
			<?php 	$row_cnt=1;
					while($payment_result=tep_db_fetch_array($payment_query)) {
						if ($row_cnt%2==0) $class="attributes-even";
						else $class="attributes-odd";
						$check_query=tep_db_query("SELECT * from " . TABLE_EVENTS_INSTRUCTOR_PAYMENTS . " where instructor_id='" . $payment_result["events_instructor"] . "' and sessions_id='" . $payment_result["products_id"] . "'");
						//echo "SELECT * from " . TABLE_EVENTS_INSTRUCTOR_PAYMENTS . " where instructor_id='" . $payment_result["events_instructor"] . "' and sessions_id='" . $payment_result["products_id"] . "'";
						$pay_request=(tep_db_num_rows($check_query)>0?true:false);
						$check_result=tep_db_fetch_array($check_query);
						$paid=($check_result["paid"]=='Y'?true:false);
						
						$count_query=tep_db_query("SELECT count(*) as total from " . TABLE_EVENTS_ATTENDEES_CONFIRM . " where sessions_id='" . $payment_result["products_id"] . "'");
						$count_result=tep_db_fetch_array($count_query);
						$mode=((int)$count_result["total"]>0?2:1);
						$table=($mode==1?TABLE_EVENTS_ATTENDEES:TABLE_EVENTS_ATTENDEES_CONFIRM);
						
						$queried_query=tep_db_query("SELECT count(*) as total from " . $table . " where sessions_id='" . $payment_result["products_id"] . "' and queried='Y'");
						$queried_result=tep_db_fetch_array($queried_query);
			?>
					<tr class="<?php echo $class;?>" valign="top">
						<td class="main" width="10">
							<?php 	if ($pay_request && !$paid) {
										echo '<input type="checkbox" name="entry_id[]" value="' . $check_result["entry_id"] . '" onClick="javascript:checkSelection(this)">';
										echo '<input type="hidden" name="query_check[' . $check_result["entry_id"] . ']" value="' . $queried_result["total"] . '">';
									}
							?>
						</td>
						<td class="main" width="10">
							<?php //if (!$paid) echo '<a href="' . tep_href_link(FILENAME_EVENTS_ADMIN_PAYMENTS_DETAIL,'eID=' . $payment_result['events_id'] . '&sID=' . $payment_result['products_id'] . '&mode=' . $mode . "&iID=" . $payment_result["events_instructor"]) . '">' . tep_image(DIR_WS_ICONS . 'image_edit.gif',IMAGE_VIEW_PAYMENT,'','','align=absmiddle') . '</a>';?>
							<?php if (!$paid) {
							$params='eID=' . $payment_result['events_id'] . '&sID=' . $payment_result['products_id'] . '&mode=' . $mode . "&iID=" . $payment_result["events_instructor"];
							echo '<a href="javascript:do_payment_details(\'' . $params . '\');">' . tep_image(DIR_WS_ICONS . 'image_edit.gif',IMAGE_VIEW_PAYMENT,'','','align=absmiddle') . '</a>';
							}
							?>
						</td>
						<td class="main" width="100"><?php echo $payment_result["instructor_name"];?></td>
						<td class="main"><?php echo $payment_result["products_name"];?></td>
						<td class="main" width="60"><?php echo format_date($payment_result["start_date"],true);?></td>
						<td width="400">
							<table border="0" cellspacing="2" cellpadding="0" width="100%">
							<tr>
							<?php if ($pay_request && !$paid) { ?>
								<td class="main" align="right" width="90" valign="top"><?php echo $currencies->format(tep_get_instructor_event_rate($payment_result,$payment_result["products_id"],$table));?></td>
								<td class="main" width="90" align="center"><?php echo tep_draw_input_field("pay_details[" . $check_result["entry_id"] . "][invoice_no]",'','size=10');?></td>
								<td class="main" width="120"><?php echo tep_draw_pull_down_menu("pay_details[" . $check_result["entry_id"] . "][payment_type]",$type_array);?></td>
								<td class="main" width="100"><?php echo tep_draw_input_field("pay_details[" . $check_result["entry_id"] . "][payment_date]",'','size=10');?></td>
							<?php } else if ($paid) { ?>
								<td class="main" align="right" width="90"><?php echo $currencies->format($check_result["total_amount"]);?></td>
								<td class="main" width="90">&nbsp;<?php echo $check_result["invoice_no"];?></td>
								<td class="main" width="120"><?php echo $check_result["payment_type"];?></td>
								<td class="main" width="100"><?php echo format_date($check_result["payment_date"],true);?></td>
							<?php } else { ?>
								<td class="main" align="right" width="90"><?php echo $currencies->format(tep_get_instructor_event_rate($payment_result,$payment_result["products_id"],$table));?></td>
								<td class="main" align="center"><?php echo TEXT_NOT_REQUESTED;?></td>
							<?php }?>
							</tr>
							</table>
						</td>
						<td class="main" align="center" width="20">
							<?php 	if ($paid) { 
										echo tep_image(DIR_WS_IMAGES . 'icon_status_green.gif',IMAGE_PAID,'','','align=absmiddle');
									} else {
										echo tep_image(DIR_WS_IMAGES . 'icon_status_green_light.gif',IMAGE_NOT_PAID,'','','align=absmiddle');
									}
							?>
						</td>
						<td class="main" align="center" width="20">
							<?php 	if ($queried_result["total"]>0) {
										echo tep_image(DIR_WS_IMAGES . 'icon_status_red.gif',IMAGE_QUERY,'','','align=absmiddle');
									} else {
										echo tep_image(DIR_WS_IMAGES . 'icon_status_red_light.gif',IMAGE_NOT_QUERY,'','','align=absmiddle');
									}
							?>
						</td>
					</tr>
			<?php $row_cnt++;} ?>
		</table>
	<!--	</form> -->
		</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '15'); ?></td>
		</tr>
		<tr>
			<td>
				<table border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td>
							<?php echo '<a href="javascript:note_paid();">' . tep_image_button("button_note_paid.gif",IMAGE_NOTE_PAID) . '</a>';?>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		</table>
		<?php 
		} else { // found results ?>
		<table cellpadding="2" cellspacing="0" width="100%">
		<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
		<tr><td class="pageHeading"><?php echo HEADING_PAYMENTS;?></td></tr>
		<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
		<tr><td class="main"><?php echo NO_RESULTS;?></td></tr>		
	<?php
		} // found results

}
function payment_details() {
	global $HTTP_GET_VARS;

	$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;
	$instructor_id=isset($HTTP_GET_VARS["iID"])?(int)$HTTP_GET_VARS["iID"]:0;
	$mode=isset($HTTP_GET_VARS["mode"])?$HTTP_GET_VARS["mode"]:'';
	

	if($mode==1) $table_attendees=TABLE_EVENTS_ATTENDEES;
	else $table_attendees=TABLE_EVENTS_ATTENDEES_CONFIRM;
	
	$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 ";
		}
		
	$add_option="";
	$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";
	$cust_result=tep_db_query($cust_query);
	$selected_name="";	
	$SelectCustomerBox = "<select name='customer_id' size=10 onClick=\"javascript:setListboxText(0,'customer');toggleList(0);\" onFocus=\"javascript:toggleList(1);\" onBlur=\"javascript:toggleList(0);\" class='customerSelect'>\n";
		if (tep_db_num_rows($cust_result) > 0){
		// Query Successful
			while($db_Row = tep_db_fetch_array($cust_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";
	
		// check if payment has been made for particular session
	$check_query=tep_db_query("SELECT * from " . TABLE_EVENTS_INSTRUCTOR_PAYMENTS . " where instructor_id='" . $instructor_id . "' and sessions_id='" . $sessions_id . "'");
	$pay_request=(tep_db_num_rows($check_query)>0?true:false);

	$found_results=false;
	$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);
	if (tep_db_num_rows($detail_query)>0) $found_results=true;
	// 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);

	
	?>
	<table cellpadding="2" cellspacing="0" width="100%">
		<tr>
			<td class="main" align="right"><?php echo '<a href="javascript:close_payment_detail();">' . 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>
		<tr>
			<td>
			<table width="100%" border="0" cellspacing="2" cellpadding="2" class="searchArea">
			<?php //echo tep_draw_form("payment_detail",FILENAME_EVENTS_ADMIN_PAYMENTS_NEW,tep_get_all_get_params(array("action")),'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="70"><?php echo TABLE_VIEW_QUERY;?></td>
				<td class="tableHeading" align="left" width="30"><?php echo TABLE_QUERIED;?></td>
			</tr>
			<?php 
				$params='eID=' . $events_id . '&sID=' . $sessions_id . '&iID=' . $instructor_id . '&mode=' . $mode;
				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="center" valign="top" width="70">
							<?php echo '<span class="cursorHand" onClick="javscript:view_query_content(\'' . ($row_cnt-1) . '\',\'' . $params . '\')">' .  tep_image(DIR_WS_ICONS . "view.gif",IMAGE_VIEW_QUERY,16,16,"align=absmiddle"); ?>
						</td>
						<td class="main" align="center" valign="top" width="30">
						<?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?>
			<tr>
			<td colspan="7">
				<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\',\'' . $params . '\')" style="cursor:hand" align="absmiddle"');
									echo '&nbsp;' . tep_image_button("button_query_selected.gif",IMAGE_QUERY_SELECTED,'onClick="setAction(\'query_selected\',\'' . $params . '\')" style="cursor:hand" align="absmiddle"');
									echo '&nbsp;' . tep_image_button("button_remove_query.gif",IMAGE_REMOVE_QUERY,'onClick="setAction(\'remove_query\',\'' . $params . '\')" style="cursor:hand" align="absmiddle"');
								}
								if (!$pay_request) echo '&nbsp;' . '<a href="javascript:add_default(\'' . $params . '\');">' . tep_image_button("button_add_default.gif",IMAGE_ADD_DEFAULT,'align="absmiddle"') . "</a>";
								echo '&nbsp;<a href="javascript:close_payment_detail();">' . tep_image_button("button_close.gif",IMAGE_CLOSE,'align="absmiddle"') . '</a>';
							?>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		<!--	</form> -->
			</table>
		</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<?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_ADMIN_PAYMENTS_DETAIL,tep_get_all_get_params(array("action"))); 
				//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',$selected_customer_name,'onFocus="javascript:toggleText(1,\'customer\');" onBlur="javascript:toggleText(0,\'customer\');" style="width:400px;"');?></td>
						<td class="main"><span class="cursorhand">&nbsp;<?php echo tep_image_button('button_search_customers.gif',IMAGE_SEARCH_CUSTOMERS,'onClick="javascript:setAction(\'search_customers\',\'' . $params . '\');"');?></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>-->
		<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>
		<?php //echo tep_draw_form("new_payment",FILENAME_EVENTS_ADMIN_PAYMENTS_DETAIL,tep_get_all_get_params(array("action")),'post','onSubmit="javascript:return validateAddForm(this);"');
			//	echo '<input type=hidden name=post_action value="add_customer">';
		?>
		<tr>
			<td>
				<table cellpadding="2" class="searchArea" cellspacing="2" 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').'<font color=red>*</font>';?></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 echo tep_draw_input_field("customers_second_phone",'','size=35').'<font color=red>*</font>';?></td>
						<td class="main"><?php echo tep_draw_input_field("customers_age",'','size=35').'<font color=red>*</font>';?></td>
						<td class="main" colspan="2"><?php echo tep_draw_input_field("customers_street_address",'','size="35"').'<font color=red>*</font>';?></td>
						<td class="main"><?php echo tep_draw_input_field("customers_suburb",'','size=35').'<font color=red>*</font>';?></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').'<font color=red>*</font>';?></td>
						<td class="main"><?php echo tep_draw_input_field("customers_city",'','size=35').'<font color=red>*</font>';?></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').'<font color=red>*</font>';?></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').'<font color=red>*</font>';?></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').'<font color=red>*</font>';?></td>
						<td class="main"><?php echo tep_draw_input_field("customers_emailaddress",'','size=35').'<font color=red>*</font>';?></td>
						<td class="main" colspan="2"><?php echo tep_draw_input_field("customers_second_email",'','size=35').'<font color=red>*</font>';?></td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<tr>
			<td><?php echo '<a href="javascript:add_customers(\''.  $params . '\');">' . tep_image_button("button_add.gif",IMAGE_ADD_CUSTOMER);?></td>
		</tr>
		<!-- </form> -->

		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '15'); ?></td>
		</tr>
		
		<?php  if ($mode==1 && $found_results){
			//	echo tep_draw_form("confirm",FILENAME_EVENTS_ADMIN_PAYMENTS_DETAIL,tep_get_all_get_params(array("action")),'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 '<a href="javascript:confirm_attendees(\'' . $params . '\');">' . tep_image_button("button_submit_attendees.gif",IMAGE_SUBMIT_ATTENDEES) . '</a>'; ?></td>
					</tr>
				</table>
			</td>
		</tr>
		<!-- </form>-->
		<?php } // mode
	}
	?>			 
				 
	</table>	
<?php
}
function query_selected_details() {
	global $table_attendees,$HTTP_GET_VARS,$command;

	$select_ids=isset($HTTP_GET_VARS['select_ids'])?$HTTP_GET_VARS['select_ids']:'';
	$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;
	$instructor_id=isset($HTTP_GET_VARS["iID"])?(int)$HTTP_GET_VARS["iID"]:0;
	$mode=isset($HTTP_GET_VARS["mode"])?$HTTP_GET_VARS["mode"]:'';
	$query="SELECT at.entry_id,at.customers_id,at.customers_telephone,at.customers_firstname,at.customers_lastname,at.queried 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 query_date,query_text from " . TABLE_EVENTS_ATTENDEES_QUERY . " where attendees_id in(" . $select_ids . ")");
	$detail_query=tep_db_query($query);
	if($command=='query_selected' || $command=='view_query') {
	 ?>
	 <table cellpadding="2" cellspacing="0" width="100%">
		<tr>
			<td class="pageHeading"><?php TEXT_VIEW_QUERY;?></td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<?php	//echo tep_draw_form("view_query",FILENAME_EVENTS_ADMIN_PAYMENTS_DETAIL,tep_get_all_get_params(array("action")),"post",'onSubmit="javascript:return validateQueryForm(this);"');
			//	echo '<input type=hidden name="post_action" value="comment_query">';
		?>
		<tr>
			<td>
				<table cellpadding="2" cellspacing="2" width="100%" class="searchArea">
					<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;
					$params='eID=' . $events_id . '&sID=' . $sessions_id . '&iID=' . $instructor_id . '&mode=' . $mode;
					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);
						
						$cust_ids.=$detail_result["customers_id"];
						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="1" cellspacing="0" 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>
	<?php if ($command=="query_selected") { ?>
		<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 class="main">
			<?php 
				echo '<a href="javascript:close_query_selected(\'' . $params . '\');">' . tep_image_button("button_back.gif",IMAGE_BACK) . "</a>&nbsp;";
				echo '<a href="javascript:comment_query(\'' . $params . '\',\'' . $select_ids . '\');">' . tep_image_button("button_submit.gif",IMAGE_SUBMIT_QUERY);
			?>
			</td>
		</tr>
		<?php } else { ?>
	<tr>
			<td class="main"><?php echo '<a href="javascript:close_query_selected(\'' . $params . '\');">' . tep_image_button("button_back.gif",IMAGE_BACK) . "</a>&nbsp;";?></td>
		</tr> 
		<?php } ?>
	<!--	</form>-->
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '15'); ?></td>
		</tr>
	</table>	
	<?php 
	}
}
function search_customer_details() {
	global $HTTP_GET_VARS;
	$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;
	$instructor_id=isset($HTTP_GET_VARS["iID"])?(int)$HTTP_GET_VARS["iID"]:0;
	$mode=isset($HTTP_GET_VARS["mode"])?$HTTP_GET_VARS["mode"]:'';
	$customer_id=isset($HTTP_GET_VARS['customer_id'])?$HTTP_GET_VARS['customer_id']:'';
	$found_results=false;
	$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=" . $customer_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";
	$detail_query=tep_db_query($query);
	if (tep_db_num_rows($detail_query)>0) $found_results=true;
		
	?>
		<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_ADMIN_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="searchArea" 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 	
				$params='eID=' . $events_id . '&sID=' . $sessions_id . '&iID=' . $instructor_id . '&mode=' . $mode;
						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="javascript:close_search_details(\'' . $params . '\');">' . tep_image_button("button_back.gif",IMAGE_BACK) . '</a>&nbsp;';
				if ($found_results) echo '<a href="javascript:add_selected_customer(\'' . $params . '\');">' . tep_image_button("button_add_selected.gif",IMAGE_ADD_CUSTOMER) . '</a>';

			?>
			</td>
		</tr>
	<!--	</form> -->
	<?php 
}
?>
