<?php
/*
	osCommRes, Services Online 
	http://www.oscommres.com 
	Copyright (c) 2005 osCommRes 
	
	Freeway eCommerce 
	http://www.zac-ware.com/freeway 
	Copyright (c) 2007 ZacWare
	
	Released under the GNU General Public License 
*/

// Set flag that this is a parent file
	define( '_FEXEC', 1 );

	require('includes/application_top.php');
	require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CHECKOUT_ATTENDEES);
	require(DIR_WS_INCLUDES .'javascript/http.js');
	$command=tep_db_prepare_input($HTTP_GET_VARS['command']);
	$cmd=isset($HTTP_GET_VARS['action'])?tep_db_prepare_input($HTTP_GET_VARS['action']):'';
	$no_attendees=isset($HTTP_GET_VARS['no_att'])?tep_db_prepare_input($HTTP_GET_VARS['no_att']):'';
	$customer_list="";
	$cnt=tep_db_prepare_input($HTTP_GET_VARS['cnt']);
	$cus_id=tep_db_prepare_input($HTTP_GET_VARS['cust_id']);
	$first_name="";
	$last_name="";
	$email_address="";
	$street_address="";
	$city="";
	$country="";
	$state="";
	$post_code="";
	if($command=='fetch_customers_details'){
		$customer_query=tep_db_query("select c.customers_firstname,c.customers_lastname,c.customers_email_address as email_address,a.entry_postcode as postcode,a.entry_city as city,a.entry_zone_id as state,a.entry_country_id as country,a.entry_street_address as street_address,a.entry_state from ".TABLE_CUSTOMERS ." c LEFT JOIN ".TABLE_ADDRESS_BOOK." a on(c.customers_id=a.customers_id) where c.customers_id='".tep_db_input($cus_id)."'");
		if(tep_db_num_rows($customer_query)>0){
			$customer=tep_db_fetch_array($customer_query);
			$first_name=$customer['customers_firstname'];
			$last_name=$customer['customers_lastname'];
			$email_address=$customer['email_address'];
			$street_address=$customer['street_address'];
			$city=$customer['city'];
			$post_code=$customer['postcode'];
			$state=tep_get_zone_name($customer['country'],$customer['state'],$customer['entry_state']);
			$country=$customer['country'];
			$customer_list=tep_output_string($first_name)."^".tep_output_string($last_name)."^".tep_output_string($email_address)."^".tep_output_string($street_address)."^".tep_output_string($city) ."^".$customer['postcode']."^".tep_output_string($state)."^".tep_output_string($country);
		}
		echo 'customer_list'.$customer_list.'##'.tep_output_string($cnt);
			if($cmd=='return'){
				$customer_list=array();	
				$cus=0;
				$customer_query=tep_db_query("select distinct c.customers_id,c.customers_firstname,c.customers_lastname,c.customers_email_address,a.entry_postcode,a.entry_city,a.entry_state,a.entry_country_id from ".TABLE_CUSTOMERS_TO_CUSTOMERS ." c2c LEFT JOIN ".TABLE_CUSTOMERS ." c on(c2c.customer_id=c.customers_id) LEFT JOIN ".TABLE_ADDRESS_BOOK ." a ON(c.customers_id=a.customers_id) where group_customer_id!=c.customers_id and group_customer_id='".tep_db_input($cus_id)."' order by c.customers_lastname,c.customers_firstname");
				if(tep_db_num_rows($customer_query)){
					while($customer=tep_db_fetch_array($customer_query)){
						if($customer['customers_id']) $customer_list[]=array('id'=>$customer['customers_id'],'text'=>$customer['customers_lastname'].' '.$customer['customers_firstname'].','.$customer['customers_email_address'].',('.$customer['customers_id'].')');
						$cus++; 
					}echo "##";	
					for($icnt=0;$icnt<$no_attendees;$icnt++){
					  echo tep_draw_pull_down_menu('customer_text',$customer_list,check_customer_exists($customer->email_address,($customer->attendee==1)?true:false),"onchange=javascript:fill_details($icnt); onkeydown=javascript:fill_details($icnt); onkeyup=javascript:fill_details($icnt); style='display:none'").'~';
					  }
				}			
		   }
		exit;
	}else if($command=='show_state'){
		 $zone_ids="";
		 $zone_name="";
		 $country=isset($HTTP_GET_VARS['country_id'])?tep_db_prepare_input($HTTP_GET_VARS['country_id']):0;
	     $zones_query = tep_db_query("select zone_id,zone_name from " . TABLE_ZONES . " where zone_country_id = '" . tep_db_input($country) . "' order by zone_name");
	     while ($zones_values = tep_db_fetch_array($zones_query)) {
	         $zone_ids.=tep_db_prepare_input($zones_values['zone_id']).',';
	         $zone_name.=tep_db_prepare_input($zones_values['zone_name']).',';
	      }
	   echo 'show_state'.substr($zone_ids,0,-1)."^".substr($zone_name,0,-1)."^".$zone."^".tep_output_string($cnt);
	   exit;
	}   
	require(DIR_WS_CLASSES .'object_info.php');
	$action=(isset($HTTP_POST_VARS["post_action"])?tep_db_prepare_input($HTTP_POST_VARS["post_action"]):1);
	$mode=(isset($HTTP_GET_VARS["mode"])?tep_db_prepare_input($HTTP_GET_VARS["mode"]):"add");
	$error=false;
	$entry_state_has_zones=false;
	if (count($cart->attendees)>0){
		$no_attendees=count($cart->attendees)+1;
	} else { 
		$no_attendees=(isset($HTTP_POST_VARS["no_attendees"])?(int)$HTTP_POST_VARS["no_attendees"]:1);
	}
if(isset($HTTP_GET_VARS['no_att'])) $no_attendees=tep_db_prepare_input($HTTP_GET_VARS['no_att']);
	$check_array=tep_db_prepare_input($HTTP_POST_VARS['attendee']);
	if($action=='save'){
		$customers_list=array();
		$errors=array(); 
		  for($icnt=0;$icnt<$no_attendees;$icnt++){
			$first_name=tep_db_prepare_input($HTTP_POST_VARS["first_name"][$icnt]);
			$last_name=tep_db_prepare_input($HTTP_POST_VARS["last_name"][$icnt]);
			$email_address=tep_db_prepare_input($HTTP_POST_VARS["email_address"][$icnt]);
			$street_address=tep_db_prepare_input($HTTP_POST_VARS["street_address"][$icnt]);
			$city=tep_db_prepare_input($HTTP_POST_VARS["city"][$icnt]);
			$postcode=tep_db_prepare_input($HTTP_POST_VARS["postcode"][$icnt]);
			$state=tep_db_prepare_input($HTTP_POST_VARS["customer_state"][$icnt]);
			$country=tep_db_prepare_input($HTTP_POST_VARS["country"][$icnt]);
			$check_existing=$check_array[$icnt];
			//get existing customer
			if (strlen($first_name) < ENTRY_FIRST_NAME_MIN_LENGTH) {	
				$error = true;
				$errors[$icnt][]=FIRST_NAME_ERROR;
			}
			if (strlen($last_name) < ENTRY_LAST_NAME_MIN_LENGTH) {
				$error = true;
				$errors[$icnt][]=LAST_NAME_ERROR;
			}
			if (strlen($email_address) < ENTRY_EMAIL_ADDRESS_MIN_LENGTH) {
				$error = true;
				$errors[$icnt][]=EMAIL_ADDRESS_ERROR;
			} elseif (tep_validate_email($email_address) == false) {
				$error = true;
				$errors[$icnt][]=ADDRESS_CHECK_ERROR;
			} else {//if($check_existing!=1 && $mode!='edit') {
				if($icnt>0){
				$check_email_query = tep_db_query("select count(*) as total from " . TABLE_CUSTOMERS . " where customers_email_address = '" . tep_db_input($email_address) . "'");
				$check_email = tep_db_fetch_array($check_email_query);
				if ($check_email['total'] > 0 && !$check_existing) {
					$error = true;
					$errors[$icnt][]=ADDRESS_ERROR_EXISTS;
				}
				}
			}
			if (strlen($street_address) < ENTRY_STREET_ADDRESS_MIN_LENGTH) {
				$error = true;
				$errors[$icnt][]=STREET_ADDRESS_ERROR;
			}
			
			if (strlen($postcode) < ENTRY_POSTCODE_MIN_LENGTH) {
				$error = true;
				$errors[$icnt][]=POSTCODE_ERROR;
			}
			
			if (strlen($city) < ENTRY_CITY_MIN_LENGTH) {
				$error = true;
				$errors[$icnt][]=CITY_ERROR;
			}
			
			if (is_numeric($country) == false) {
				$error = true;
				$errors[$icnt][]=COUNTRY_ERROR;
			}
			$zone_id = 0;
			$check_query = tep_db_query("select count(*) as total from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "'");
			$check = tep_db_fetch_array($check_query);
			$entry_state_has_zones = ($check['total'] > 0);
			if ($entry_state_has_zones == true) {
				$zone_query = tep_db_query("select distinct zone_id from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' and (zone_name like '" . tep_db_input($state) . "%' or zone_code like '%" . tep_db_input($state) . "%')");
				if (tep_db_num_rows($zone_query) == 1) {
					$zone = tep_db_fetch_array($zone_query);
					$zone_id = $zone['zone_id'];
				} else {
					$error = true;
					$errors[$icnt][]=STATE_ERROR_SELECT;
					$state_errors[$icnt][]=1;
				}
			} else {
				if (strlen($state) < ENTRY_STATE_MIN_LENGTH) {
					$error = true;
					$errors[$icnt][]=STATE_ERROR;
				}
			} if($icnt==0) $error=false;
			$customers_list[]=array("first_name"=>$first_name,
								"last_name"=>$last_name,
								"email_address"=>$email_address,
								"street_address"=>$street_address,
								"city"=>$city,
								"postcode"=>$postcode,
								"state"=>$state,
								"country"=>$country,
								"attendee" =>(int)$check_existing);
		  }
		// if mode is add_customers just add to the shopping cart
		if (!$error){ 
			$cart->set_attendees($customers_list);
			if ($mode=="add"){
				echo '<html><head><title></title></head><body>';
				echo tep_draw_form("checkout_event",tep_href_link(FILENAME_CHECKOUT_ATTENDEES,tep_get_all_get_params() . "event_action=buy_product&element_type=E"));
				// draw the hidden field
				echo tep_draw_hidden("old_orders_id",tep_db_prepare_input($HTTP_POST_VARS["old_orders_id"]));
				echo tep_draw_hidden("events_id",tep_db_prepare_input($HTTP_POST_VARS["events_id"]));
				echo tep_draw_hidden("event_question_type",tep_db_prepare_input($HTTP_POST_VARS["event_question_type"]));
				echo tep_draw_hidden("event_question_value_data",tep_db_prepare_input($HTTP_POST_VARS["event_question_value_data"]));
				echo tep_draw_hidden("no_attendees",$no_attendees);
				
				$session_id=tep_db_prepare_input($HTTP_POST_VARS["session_id"]);
				$session_type=tep_db_prepare_input($HTTP_POST_VARS["session_type"]);
				$session_group=tep_db_prepare_input($HTTP_POST_VARS["session_group"]);
				$session_date=tep_db_prepare_input($HTTP_POST_VARS["session_date"]);
				for ($icnt=0;$icnt<count($session_id);$icnt++){
					echo tep_draw_hidden("session_select_" . $session_id[$icnt],tep_db_prepare_input($HTTP_POST_VARS["session_select_" . $session_id[$icnt]]));
					echo tep_draw_hidden("session_id[]",$session_id[$icnt]);
					echo tep_draw_hidden("session_type[]",$session_type[$icnt]);
					echo tep_draw_hidden("session_group[]",$session_group[$icnt]);
					echo tep_draw_hidden("session_date[]",$session_date[$icnt]);
				}
				echo '</form>';
				echo '<script language="javascript">document.checkout_event.submit();</script>';
				echo '</script>';
				echo '</body></html>';
			} else {
				tep_redirect(tep_href_link(FILENAME_SHOPPING_CART,'','SSL'));
			}
		}
	}
	//get customer details
		$customer_list=array();	
		$cus=0;
		$customer_query=tep_db_query("select distinct c.customers_id,c.customers_firstname,c.customers_lastname,c.customers_email_address,a.entry_postcode,a.entry_city,a.entry_state,a.entry_country_id from ".TABLE_CUSTOMERS_TO_CUSTOMERS ." c2c LEFT JOIN ".TABLE_CUSTOMERS ." c on(c2c.customer_id=c.customers_id) LEFT JOIN ".TABLE_ADDRESS_BOOK ." a ON(c.customers_id=a.customers_id) where group_customer_id!=c.customers_id and group_customer_id='$customer_id' order by c.customers_lastname,c.customers_firstname");
		if(tep_db_num_rows($customer_query)){
			while($customer=tep_db_fetch_array($customer_query)){
				if($customer['customers_id']) $customer_list[]=array('id'=>$customer['customers_id'],'text'=>$customer['customers_lastname'].' '.$customer['customers_firstname'].','.$customer['customers_email_address'].',('.$customer['customers_id'].')');
				$cus++; 
			}
		} 
		
	function check_customer_exists($email,$id=false){
		global $check_array,$icnt,$mode;
		
		if(!$check_array[$icnt] && $mode!='edit') return;
		$return=false;
		if($email=='') return;
		$check_customer=tep_db_query("select customers_id from ".TABLE_CUSTOMERS." where customers_email_address='".$email."'");
		if(tep_db_num_rows($check_customer)>0)
			$return=true;
		if($id){
			$customer_id=tep_db_fetch_array($check_customer);
			$return=$customer_id['customers_id'];
		}	
		return $return;
	}
	
	//get customer details
	$breadcrumb->add(NAVBAR_TITLE);
	
	$content = CONTENT_CHECKOUT_ATTENDEES; 

	$javascript=$content . ".js.php";
	require(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/' . TEMPLATENAME_MAIN_PAGE);
	
	require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
