<?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/functions/events_tree.php');
	require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CHECKOUT_ATTENDEES);

	$post_action=(isset($HTTP_POST_VARS["post_action"])?$HTTP_POST_VARS["post_action"]:1);
	$action=(isset($HTTP_POST_VARS["action"])?$HTTP_POST_VARS["post_action"]:1);
	$mode=(isset($HTTP_GET_VARS['mode'])?$HTTP_GET_VARS['mode']:'');
	$orders_id=(isset($HTTP_GET_VARS['oID'])?(int)$HTTP_GET_VARS['oID']:'0');
	$sel_customer_id=(isset($HTTP_GET_VARS['customer_id'])?(int)$HTTP_GET_VARS['customer_id']:-1);
	
	// get customer id
	$customer_query=tep_db_query("select customers_id from " . TABLE_ORDERS . " where orders_id='" . (int)$oID . "'");
	$customer_result=tep_db_fetch_array($customer_query);
	$customer_id=(int)$customer_result['customers_id'];
	//ajax codes
	$command=(isset($HTTP_GET_VARS['command'])?$HTTP_GET_VARS['command']:'');
	$country=isset($HTTP_GET_VARS['country_id'])?(int)$HTTP_GET_VARS['country_id']:0; 
	$cnt=$HTTP_GET_VARS['cnt'];
	if($command=="fetch_customer_details")
	{
		$sel_customer_id=isset($HTTP_GET_VARS["sel_customer"])?(int)$HTTP_GET_VARS["sel_customer"]:-1;
		$cnt=(isset($HTTP_GET_VARS['cnt'])?$HTTP_GET_VARS['cnt']:'');
		$sel_customer_query=tep_db_query("select a.*,c.customers_email_address from ".TABLE_ADDRESS_BOOK ." a, ".TABLE_CUSTOMERS." c where c.customers_id=a.customers_id and c.customers_id='".(int)$sel_customer_id."'");
		while($customer_result=tep_db_fetch_array($sel_customer_query)) {
			$content=$sel_customer_id."^".$customer_result['entry_firstname']."^".$customer_result['entry_lastname']."^".$customer_result['customers_email_address']."^". $customer_result['entry_street_address']."^".
					$customer_result['entry_city']."^".$customer_result['entry_postcode']."^".tep_get_zone_name($customer_result['entry_country_id'],$customer_result['entry_zone_id'],$customer_result['entry_state'])."^".$customer_result['entry_country_id'];
		}
		echo 'customer_list'.$content."##".$cnt;
		exit;
	}else if($command=='show_state'){
		 $zone_ids="";
		 $zone_name="";
	     $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_output_string($zones_values['zone_id']).',';
	         $zone_name.=tep_output_string($zones_values['zone_name']).',';
	      }
	   $content='show_state'.substr($zone_ids,0,-1)."^".substr($zone_name,0,-1)."^".$zone.'^'.$cnt;    
	   echos($content);
	   echo $content; 
	   exit;
	} 

   $edit_cart = new editOrderCart($oID);
	$error=false;
	$entry_state_has_zones=false;
	
	$attendees=$edit_cart->attendees;
	$attendee_query=tep_db_query("select c.customers_id,c.customers_firstname,c.customers_lastname,c.customers_email_address from ".TABLE_CUSTOMERS." c , ".TABLE_CUSTOMERS_TO_CUSTOMERS ." ctc where c.customers_id=ctc.customer_id and ctc.group_customer_id='".(int)$customer_id."' and ctc.customer_id!='".(int)$customer_id."' group by c.customers_id order by c.customers_lastname,c.customers_firstname");
	$attendee_count=tep_db_num_rows($attendee_query);
	if($attendee_count>0){
		while($attendee_result=tep_db_fetch_array($attendee_query)){
			$name=$attendee_result['customers_lastname']."&nbsp;".$attendee_result['customers_firstname'].", ".$attendee_result['customers_email_address'];
			$attendee_array[]=array('id' => $attendee_result['customers_id'],'text'=>$name);
	}
	}
	//if(isset($HTTP_GET_VARS["no_attendees"]) && $HTTP_GET_VARS["no_attendees"]
	if (count($edit_cart->attendees)>0){
		$no_attendees=count($edit_cart->attendees);
	} else {
		$no_attendees=(isset($HTTP_GET_VARS["no_attendees"])?(int)$HTTP_GET_VARS["no_attendees"]:1);
	}
	if($post_action=='save'){

		$customers_list=array();
		$errors=array();
		$state_errors=array();

		for($icnt=0;$icnt<$no_attendees;$icnt++){
			$existing_customer=(isset($HTTP_POST_VARS["customer_id"][$icnt])?$HTTP_POST_VARS["customer_id"][$icnt]:-1);
			
			$first_name=$HTTP_POST_VARS["first_name"][$icnt];
			$last_name=$HTTP_POST_VARS["last_name"][$icnt];
			$email_address=$HTTP_POST_VARS["email_address"][$icnt];
			$street_address=$HTTP_POST_VARS["street_address"][$icnt];
			$city=$HTTP_POST_VARS["city"][$icnt];
			$postcode=$HTTP_POST_VARS["postcode"][$icnt];
			$state=tep_db_prepare_input($HTTP_POST_VARS["customer_state"][$icnt]);
			$country=$HTTP_POST_VARS["country"][$icnt];
			if($existing_customer=='-1'){
			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 {
				$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) {
					$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($zone_id=="")$zone_id=0;
			$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,
								"zone_id"=>$zone_id,
								"orders_id"=>$oID,
								"attendee_id"=>$icnt+1,
								"customer_id" => $existing_customer
								);
								
								
		}
		if (!$error){
		   $edit_cart->set_attendees($customers_list);
			tep_redirect(tep_href_link(FILENAME_EDIT_ORDERS, 'oID=' . $oID. '&step=2'));
	    }
	}
		?>
<!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/general.js"></script>
<script language="javascript" src="includes/http.js"></script>
<script language="javascript" src="includes/attendees.js"></script>
<script language="javascript"> 
	var submitted = false;
	
	function check_input(field_name, pos,field_size) {
	  var form=document.checkout_attendees;
	  if (pos>=0){
		  var control=form.elements[field_name][pos];
	  } else {
		  var control=form.elements[field_name];
	  }
	  var field_value = control.value;
	  if (field_value == '' || field_value.length < field_size) {
		return true;
	  }
	}
	
	function check_select(field_name, pos,field_default) {
	  var form=document.checkout_attendees;
	  if (pos>=0){
		  var control=form.elements[field_name][pos];
	  } else {
		  var control=form.elements[field_name];
	  }
	  var field_value = control.value;
	
	   if (field_value == field_default) {
		 return true;
	   }
	}
	
	function dofill(){
		var frm=document.checkout_attendees;
		frm_customers=frm.elements["customer[]"];
		for (icnt=1;icnt<frm.elements["city[]"].length;icnt++)
		{
		 frm.elements["city[]"][icnt].value=frm.elements["city[]"][0].value;
		 
		 frm.elements["state1[]"][icnt].style.display=''; 
		 frm.elements["state[]"][icnt].style.display="none";
		 if(frm.elements["state[]"][0].style.display!='none') 	
		 	state=frm.elements["state[]"][0].value;
		 else if(frm.elements["state1[]"][0].style.display!='none')
		 	state=frm.elements["state1[]"][0].value;
		 frm.elements["state1[]"][icnt].value=state; 
		 frm.elements["country[]"][icnt].value=frm.elements["country[]"][0].value;
		 frm.elements["postcode[]"][icnt].value=frm.elements["postcode[]"][0].value;
		}
	}
	
	function clear_error(pos){
		error_control=document.getElementById("error_box_"+pos);
		error_control.innerHTML="";
		error_control.style.display="none";
	}
	
	function show_errors(count){
		for (icnt=0;icnt<count;icnt++){
			error_control=document.getElementById("error_box_"+icnt);
			error_control.style.display="";
		}
	}
	
	function check_customer(pos){
		var error_att="";
			if (check_input("first_name[]",pos,"<?php echo ENTRY_FIRST_NAME_MIN_LENGTH;?>")){
				error_att+="<?php echo FIRST_NAME_ERROR;?><br>";
			}
			if (check_input("last_name[]",pos,"<?php echo ENTRY_LAST_NAME_MIN_LENGTH;?>")){
				error_att+="<?php echo LAST_NAME_ERROR;?><br>";
			}
			if (check_input("email_address[]",pos,"<?php echo ENTRY_EMAIL_ADDRESS_MIN_LENGTH;?>")){
				error_att+="<?php echo EMAIL_ADDRESS_ERROR;?><br>";
			}
			if (check_input("street_address[]",pos,"<?php echo ENTRY_STREET_ADDRESS_MIN_LENGTH;?>")){
				error_att+="<?php echo STREET_ADDRESS_ERROR;?><br>";
			}
			if (check_input("city[]",pos,"<?php echo ENTRY_CITY_MIN_LENGTH;?>")){
				error_att+="<?php echo CITY_ERROR;?><br>";
			}
			if (check_input("postcode[]",pos,"<?php echo ENTRY_POSTCODE_MIN_LENGTH;?>")){
				error_att+="<?php echo POSTCODE_ERROR;?><br>";
			}
			/*if (check_input("state[]",pos,"<?php echo ENTRY_STATE_MIN_LENGTH;?>")){
				error_att+="<?php echo STATE_ERROR;?><br>";
			}*/
			if(document.checkout_attendees.elements["state[]"].size!=0){
				if (document.checkout_attendees.elements["state[]"][pos].style.display!='none' && check_input("state[]",pos,"<?php echo ENTRY_STATE_MIN_LENGTH;?>")){
					error_att+="<?php echo STATE_ERROR;?><br>";
				}else if(document.checkout_attendees.elements["state1[]"][pos].style.display!='none' && check_input("state1[]",pos,"<?php echo ENTRY_STATE_MIN_LENGTH;?>")){
					error_att+="<?php echo STATE_ERROR;?><br>";
				}
			}else if(document.checkout_attendees.elements["state[]"].size==0) {
				if (document.checkout_attendees.elements["state[]"].style.display!='none' && check_input("state[]",pos,"<?php echo ENTRY_STATE_MIN_LENGTH;?>")){
					error_att+="<?php echo STATE_ERROR;?><br>";
				}else if(document.checkout_attendees.elements["state1[]"].style.display!='none' && check_input("state1[]",pos,"<?php echo ENTRY_STATE_MIN_LENGTH;?>")){
					error_att+="<?php echo STATE_ERROR;?><br>";
				}
			}	
		
			if (check_select("country[]",pos,"")){
				error_att+="<?php echo COUNTRY_ERROR;?><br>";
			}
			if (pos>0){
				var controls=document.checkout_attendees.elements["email_address[]"];
				for (jcnt=pos-1;jcnt>=0;jcnt--){
					if (controls[pos].value==controls[jcnt].value){
						error_att+="<?php echo EMAIL_UNIQUE_ERROR;?>";
						break;
					}
				}
			}
		return error_att;
	}
	
	function check_form(form_name) {
		var error=false;
		var frm=document.checkout_attendees;
		if(frm.elements["customer[]"]){
			for(i=0;i<frm.elements["customer[]"].length;i++){
				if(!frm.elements["customer[]"][i].checked)
					frm.elements["customer_id[]"][i].value=-1;	
			}
			
			if(frm.elements["country[]"].size!=0){
				for(i=0;i<frm.elements["country[]"].length;i++){
					if(frm.elements["state[]"][i].style.display!='none')
						frm.elements["state[]"][i].disabled=false;
						frm.elements["country[]"][i].disabled=false; 
				}
			}else if(frm.elements["country[]"].size==0){
					if(frm.elements["state[]"].style.display!='none')
						frm.elements["state[]"].disabled=false;
					frm.elements["country[]"].disabled=false;  	
			}
		  } 
		  		set_state();
		  
		var controls=frm.elements["first_name[]"];
		
		if (controls.length){
			for (icnt=0;icnt<controls.length;icnt++){
				error_att=check_customer(icnt);
				if (error_att!="") {
					error=true;
					error_control=document.getElementById("error_box_"+icnt);
					error_control.innerHTML=error_att;
					error_control.style.display="";
				}
			}
		} else {
			error_att=check_customer(-1);
			if (error_att!="") {
				error=true;
				error_control=document.getElementById("error_box_0");
				error_control.innerHTML=error_att;
				error_control.style.display="";
			}
		}
		if (error == true) {
			return false;
		} else {
			submitted = true;
			return true;
		}
	}

	function sel_attendee(obj,cnt){
		var check=obj.checked;
		var frm=document.checkout_attendees;
		if(check){
			document.getElementById("attendees"+cnt).style.display="";
			if(frm.elements["customer[]"].length>1){
				frm.elements["first_name[]"][cnt].readOnly=frm.elements["last_name[]"][cnt].readOnly=frm.elements["email_address[]"][cnt].readOnly=frm.elements["street_address[]"][cnt].readOnly=frm.elements["postcode[]"][cnt].readOnly=frm.elements["city[]"][cnt].readOnly=check;
				frm.elements["country[]"][cnt].disabled=check;
				
				frm.elements["state[]"][cnt].style.display='none';
				frm.elements["state1[]"][cnt].style.display='';
				if(frm.elements["state1[]"][cnt])
					frm.elements["state1[]"][cnt].readOnly=check;
					
			}	else{
				frm.elements["first_name[]"].readOnly=frm.elements["last_name[]"].readOnly=frm.elements["email_address[]"].readOnly=frm.elements["street_address[]"].readOnly=frm.elements["postcode[]"].readOnly=frm.elements["city[]"].readOnly=check;
				frm.elements["country[]"].disabled=check;
				frm.elements["state[]"].style.display='none';
				frm.elements["state1[]"].style.display='';
				if(frm.elements["state1[]"])
					frm.elements["state1[]"].readOnly=check;
			}
			get_details(cnt);
		}else{
			document.getElementById("attendees"+cnt).style.display="none";
			if(frm.elements["customer[]"].length>1){
				frm.elements["first_name[]"][cnt].readOnly=frm.elements["last_name[]"][cnt].readOnly=frm.elements["email_address[]"][cnt].readOnly=frm.elements["street_address[]"][cnt].readOnly=frm.elements["postcode[]"][cnt].readOnly=frm.elements["city[]"][cnt].readOnly=obj.check;
				frm.elements["country[]"][cnt].disabled=check;
				
				frm.elements["state[]"][cnt].style.display='none';
				frm.elements["state1[]"][cnt].style.display='';
				if(frm.elements["state1[]"][cnt])
					frm.elements["state1[]"][cnt].readOnly=check;
			}else{
				frm.elements["first_name[]"].readOnly=frm.elements["last_name[]"].readOnly=frm.elements["email_address[]"].readOnly=frm.elements["street_address[]"].readOnly=frm.elements["postcode[]"].readOnly=frm.elements["city[]"].readOnly=check;
				frm.elements["country[]"].disabled=check;
				frm.elements["state[]"].style.display='none';
				frm.elements["state1[]"].style.display='';
				if(frm.elements["state1[]"])
					frm.elements["state1[]"].readOnly=check;
			}
			var element=Array('','','','','','','','','');
			fetch_result(element,cnt);
		}
	}
	function set_state(){
		var frm=document.checkout_attendees;
			if(frm.elements["country[]"].size!=0){
				for(i=0;i<frm.elements["country[]"].length;i++){
					if(frm.elements["state[]"][i].style.display!='none'){
						frm.elements["customer_state[]"][i].value=frm.elements["state[]"][i].value;
					}
					else	
						frm.elements["customer_state[]"][i].value=frm.elements["state1[]"][i].value;
				}
			}else if(frm.elements["country[]"].size==0){
					if(frm.elements["state[]"].style.display!='none'){
						frm.elements["customer_state[]"].value=frm.elements["state[]"].value;
					}
					else 
						frm.elements["customer_state[]"].value=frm.elements["state1[]"].value;
			}		  
	}

	function get_details(cnt)
	{	
		customer_id=0;
  		var frm=document.checkout_attendees.elements["select_customer[]"];
  		if(frm.size!=0)
  			customer_id=frm[cnt].options[frm[cnt].selectedIndex].value; 
		else 
			customer_id=frm.options[frm.selectedIndex].value;
		//customer_id=document.checkout_attendees.elements["select_customer[]"][cnt].value;
		command="<?php echo tep_href_link(FILENAME_CHECKOUT_ATTENDEES,'command=fetch_customer_details');?>&sel_customer="+customer_id+"&cnt="+cnt;
		do_get_command(command);
	}
	
	
	/*function do_result(result){
		var frm=document.checkout_attendees;
		var element=Array('','','','','','','','','');
		var cust=Array();
		var val_array=Array();
		cust=result.split("##");
		var cnt=cust[1];
		if(cust[0]) element=cust[0].split("^")
		fetch_result(element,cnt);
	}*/
	
	function do_result(result){
  	 var textArr=new Array();
	 var valueArr=new Array();
	 cnt=0;
	 var frm=document.checkout_attendees;
	 var frm_attendees=frm.elements["attendee[]"];
	 var state;
	 var state1;
	 if(result!='' && result.substr(result.indexOf('show_state'),10)=='show_state'){
		result=result.substr(result.indexOf('show_state')+10,result.length);
		splt=result.split("^");
		valueArr=splt[1].split(",");		
		textArr=splt[1].split(",");
		cnt=parseInt(splt[3]);
		if(frm_attendees){
			if(frm_attendees && frm_attendees.checked) return;
			else if(frm_attendees[cnt]  && frm_attendees[cnt].checked) return;
		} 
		//var div_state=false;
		
		
		//div_state=document.getElementById("div_state");
		if(frm.elements["first_name[]"].length>1){
			state=frm.elements["state[]"][cnt];
			state1=frm.elements["state1[]"][cnt];
		} else{
			state=frm.elements["state[]"];
			state1=frm.elements["state1[]"];
		}	
		/*if(frm.elements["state[]"][cnt] && frm.elements["state[]"][cnt].type=='select-one') var state=frm.elements["state[]"][cnt];
		else if(frm[7].type=='select-one') var state=frm[7];
		if(frm.elements["state1[]"][cnt]) var state1=frm.elements["state1[]"][cnt];
		else if(frm.elements["state1[]"]) var state1=frm.elements["state1[]"];*/
		if(valueArr.length==1) {	
		//	frm.elements[""].style.display="";
			//if(div_state) div_state.style.display="none"; 
			if(state1) state1.style.display="";
			if(state) state.style.display="none";
		}else {
			state1.style.display="none";
			state.style.display="";
			//if(div_state) div_state.style.display="";
			optionElement=state;
			// clear the previous details
			while(optionElement.options.length>0){
				optionElement.remove(optionElement.options.length - 1);
			}
			//create the new details
			for (icnt=0;icnt<textArr.length;icnt++){
			  var option = document.createElement('option');
			  option.text = textArr[icnt];
			  option.value = valueArr[icnt];
			  try {
				optionElement.add(option, null); // standards compliant; doesn't work in IE
			  }
			  catch(ex) {
				optionElement.add(option); // IE only
			  }
			}
		}
	}else if(result!='' && result.substr(result.indexOf('customer_list'),13)=='customer_list'){
	   result=result.substr(result.indexOf('customer_list')+13,result.length); 
	   var frm=document.checkout_attendees;
		var element=Array('','','','','','','','','');
		var cust=Array();
		var val_array=Array();
		cust=result.split("##");
		var cnt=cust[1];
		if(cust[0]) element=cust[0].split("^")
		fetch_result(element,cnt); 
	}
  }  
	
	function fetch_result(result,cnt)	{
		var frm=document.checkout_attendees;
		var val_array=Array();
		val_array=result;
		if(!frm.elements["first_name[]"].length){
			frm.elements["customer_id[]"].value=val_array[0]
	  		frm.elements["first_name[]"].value=val_array[1];  
			frm.elements["last_name[]"].value=val_array[2];
			frm.elements["email_address[]"].value=val_array[3];
			frm.elements["street_address[]"].value=val_array[4];
			frm.elements["city[]"].value=val_array[5];  
			frm.elements["postcode[]"].value=val_array[6]; 
			if(frm.elements["state[]"].style.display!='none') 
				frm.elements["state[]"].value=val_array[7];
			else if(frm.elements["state1[]"])
				frm.elements["state1[]"].value=val_array[7];
			frm.elements["country[]"].value=val_array[8];
		}else {
			frm.elements["customer_id[]"][cnt].value=val_array[0];
			frm.elements["first_name[]"][cnt].value=val_array[1];  
			frm.elements["last_name[]"][cnt].value=val_array[2];
			frm.elements["email_address[]"][cnt].value=val_array[3];
			frm.elements["street_address[]"][cnt].value=val_array[4];
			frm.elements["city[]"][cnt].value=val_array[5];  
			frm.elements["postcode[]"][cnt].value=val_array[6]; 
			if(frm.elements["state[]"][cnt].style.display!='none')
				frm.elements["state[]"][cnt].value=val_array[7];
			else if(frm.elements["state1[]"][cnt])
				frm.elements["state1[]"][cnt].value=val_array[7];
			
			frm.elements["country[]"][cnt].value=val_array[8];	
		}
		
		/*if(frm.elements["customer[]"].length>1){
			frm.elements["first_name[]"][cnt].value=val_array[0];
			frm.elements["last_name[]"][cnt].value=val_array[1];
			frm.elements["email_address[]"][cnt].value=val_array[2];
			frm.elements["street_address[]"][cnt].value=val_array[3];
			frm.elements["city[]"][cnt].value=val_array[4];
			frm.elements["postcode[]"][cnt].value=val_array[5];
			frm.elements["state[]"][cnt].value=val_array[6];
			frm.elements["country[]"][cnt].value=val_array[7];
		}else{
			frm.elements["first_name[]"].value=val_array[0];
			frm.elements["last_name[]"].value=val_array[1];
			frm.elements["email_address[]"].value=val_array[2];
			frm.elements["street_address[]"].value=val_array[3];
			frm.elements["city[]"].value=val_array[4];
			frm.elements["postcode[]"].value=val_array[5];
			frm.elements["state[]"].value=val_array[6];
			frm.elements["country[]"].value=val_array[7];
		}*/
	}
	function show_state(cnt){ 
  		var this_=document.checkout_attendees.elements["country[]"];
  		if(this_[cnt].size==0)
  			id=this_[cnt].options[this_[cnt].selectedIndex].value;
  		else
  			id=this_.options[this_.selectedIndex].value;
		if(id){
		  command="<?php echo tep_href_link(FILENAME_CHECKOUT_ATTENDEES,'command=show_state');?>&country_id="+id+"&cnt="+cnt;
		  do_get_command(command);	
		}
  } 
</script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">

<!-- 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">
	<?php echo tep_draw_form('checkout_attendees', FILENAME_CHECKOUT_ATTENDEES , tep_get_all_get_params(),'post','onSubmit="javascript: return check_form();"'); ?>
	<table border="0" width="100%" cellspacing="0" cellpadding="2">
   <?php 
	function sbs_get_countries($countries_id = '', $with_iso_codes = false) {
    $countries_array = array();
    if ($countries_id) {
      if ($with_iso_codes) {
        $countries = tep_db_query("select countries_name, countries_iso_code_2, countries_iso_code_3 from " . TABLE_COUNTRIES . " where countries_id = '" . (int)$countries_id . "' order by countries_name");
        $countries_values = tep_db_fetch_array($countries);
        $countries_array = array('countries_name' => $countries_values['countries_name'],
                                 'countries_iso_code_2' => $countries_values['countries_iso_code_2'],
                                 'countries_iso_code_3' => $countries_values['countries_iso_code_3']);
      } else {
        $countries = tep_db_query("select countries_name from " . TABLE_COUNTRIES . " where countries_id = '" . (int)$countries_id . "'");
        $countries_values = tep_db_fetch_array($countries);
        $countries_array = array('countries_name' => $countries_values['countries_name']);
      }
    } else {
      $countries = tep_db_query("select countries_id, countries_name from " . TABLE_COUNTRIES . " order by countries_name");
      while ($countries_values = tep_db_fetch_array($countries)) {
        $countries_array[] = array('countries_id' => $countries_values['countries_id'],
                                   'countries_name' => $countries_values['countries_name']);
      }
    }
    return $countries_array;
  } 
   function sbs_get_country_list($name, $selected = '', $parameters = '') { 
      $countries_array = array(array('id' => '', 'text' => PULL_DOWN_DEFAULT)); 
      $countries = sbs_get_countries(); 
      $size = sizeof($countries); 
     for ($i=0; $i<$size; $i++) { 
        $countries_array[] = array('id' => $countries[$i]['countries_id'], 'text' => $countries[$i]['countries_name']); 
      } 
     return tep_draw_pull_down_menu($name, $countries_array, $selected, $parameters); 
   }
	$cust_array=array("name"=>"",
					"email"=>"",
					"streetaddress"=>"",
					"city"=>"",
					"postcode"=>"",
					"state"=>"",
					"city"=>"",
					"country"=>"",
					"zone_id"=>0,
					"customer_id"=>-1
					);
$cust_info=new objectInfo($cust_array);
for($icnt=0;$icnt<$no_attendees;$icnt++){
	if ($error==true && isset($customers_list[$icnt])){
		$customer=new objectInfo($customers_list[$icnt]);
	} else if ($mode=="edit" && count($attendees)>0){
		$customer=new objectInfo($attendees[$icnt]);
	} else {
		$customer=$cust_info;
	}
	$customer->name=sprintf(TEXT_ATTENDEE,$icnt+1);
	$focus_option=' onFocus="javascript:clear_error(' . $icnt . ');"';?>
	   <tr>
			<td class="messageStackWarning" id="error_box_<?php echo $icnt;?>" style="display:<?php echo ($error && isset($errors[$icnt])?"":"none");?>">
				<?php
					if ($error) {
						for ($jcnt=0;$jcnt<=count($errors[$icnt]);$jcnt++){
							echo $errors[$icnt][$jcnt] . '<br>';
						}
					}
				?>
			</td>
		</tr>
      <tr>
        <td class="main"><table border="0" width="100%" cellspacing="1" cellpadding="2">
          <tr>
            <td class="main">
			<table border="0" width="70%" cellspacing="0" cellpadding="2" >
              <tr>
			    <td  class="main" colspan="2" valign="top" align="left"><B><?php echo $customer->name;?></B></td>
			  </tr>
			  <?php if($attendee_count>0){
			  ?>
			  <tr>
			  	<td  class="main"><?php echo tep_draw_checkbox_field('customer[]','Y',($customer->customer_id>0)?true:false,'','onClick="javascript:sel_attendee(this,'.$icnt.')"').'&nbsp;'.ENTRY_EXISTING_CUSTOMER;?></td>
				<td id="attendees<?php echo $icnt?>" style="display:none"><?php echo tep_draw_pull_down_menu('select_customer[]',$attendee_array,$customer->customer_id,' onChange="javascript:get_details('.$icnt.');"');?></td>

			  </tr>
				
			  <?php  echo tep_draw_hidden_field('customer_id[]',$customer->customer_id);  }?>
			  <tr>
                <td class="main" width="90" nowrap><?php echo ENTRY_FIRST_NAME; ?></td>
                <td class="main"  nowrap><?php  echo tep_draw_input_field('first_name[]',$customer->first_name,$focus_option,true) ;?></td>
			  </tr>
			  <tr>
                <td class="main" width="90" align="left"><?php echo ENTRY_LAST_NAME; ?></td>
                <td class="main" align="left" nowrap><?php  echo tep_draw_input_field('last_name[]',$customer->last_name,$focus_option,true) ;?></td>
			  </tr>
              <tr>
                <td nowrap class="main" width="100" align="left"><?php echo ENTRY_EMAIL_ADDRESS; ?></td>
                <td class="main" align="left"><?php echo tep_draw_input_field('email_address[]',$customer->email_address,$focus_option,true) ; ?></td>
			  </tr>
			  <tr>
                <td nowrap class="main" width="100" align="left"><?php echo ENTRY_STREET_ADDRESS; ?></td>
                <td class="main" align="left"><?php echo tep_draw_input_field('street_address[]',$customer->street_address,$focus_option,true) ; ?></td>
			  </tr>
			  <tr>
                <td nowrap class="main" width="90" align="left"><?php echo ENTRY_CITY; ?></td>
                <td class="main" width="70%">
				  <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0">
				     <tr><td  class="main" align="left"><?php echo tep_draw_input_field('city[]',$customer->city,$focus_option,true) ; ?></td>
				  </table>
				</td>
			  </tr>
			  <tr>
                <td nowrap class="main" width="90" align="left"><?php echo ENTRY_POSTCODE ; ?></td>
                <td class="main" align="left"><?php echo tep_draw_input_field('postcode[]',$customer->postcode,$focus_option,true) ; ?></td>
			  </tr>
			  
			  <tr>
                <td nowrap class="main" width="90" align="left"><?php echo ENTRY_COUNTRY; ?></td>
                <td class="main" width="70%" align="left">
						<?php echo sbs_get_country_list('country[]',$customer->country,'onchange="javascript:show_state('.$icnt.');"',$focus_option) ;
				  		  echo '&nbsp;' . (($icnt==0 && $no_attendees>1)?'<a href="javascript:dofill();">' . TEXT_APPLY_ALL .'</a>':'');
						?>
					 </td>
			  </tr>
			  <tr>
                <td nowrap class="main" width="90" align="left"><?php echo ENTRY_STATE; ?></td> 
                <td class="main" width="70%" align="left">
				<?php 
					$customer_state=tep_get_zone_name($customer->country, $customer->zone_id, $customer->state);
					if ($error==true && sizeof($state_errors[$icnt])>0){
						//if ($entry_state_has_zones) {
							$zones_array = array();
							$zones_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$customer->country. "' order by zone_name");
							while ($zones_values = tep_db_fetch_array($zones_query)) {
								$zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);
							}	
						//} 
						   		echo tep_draw_pull_down_menu('state[]', $zones_array,$customer->state);
								echo tep_draw_input_field('state1[]',$customer_state,'style="display:none"'); 
					} else {
						   echo tep_draw_pull_down_menu('state[]', array(),$customer->state,'style="display:none"','');
							echo tep_draw_input_field('state1[]',$customer_state); 
					}
					
				//	echo tep_draw_input_field('state1[]',$customer_state,'id="state1[]"'); 
					//if($no_attendees==1)
					//	echo '<div id="div_state">'.tep_draw_pull_down_menu('state[]', $zones_array,$customer->state,'id="state[]" style="display:none"').'</div>';
					//else
				//	   echo tep_draw_pull_down_menu('state[]', $zones_array,$customer->state,'id="state[]" style="display:none"','');
					   echo tep_draw_hidden_field('customer_state[]');
				?>
				</td>
			  </tr>
			  <tr>
			    <td colspan="2" style="height:10"></td>
			  </tr>
			  
        </table></td>
      </tr>
	  <?php  
	  if(isset($mode) && $mode=='edit' && $customer->customer_id>0){?>
		<script>
		var icnt="<?php echo $icnt;?>";
		if(document.checkout_attendees.elements["customer[]"]){
			if(document.checkout_attendees.elements["customer[]"].length>1)
				sel_attendee(document.checkout_attendees.elements["customer[]"][icnt],icnt);
			else	
				sel_attendee(document.checkout_attendees.elements["customer[]"],icnt);
		}	
		</script>

	  <?php }
	  } ?>
	  <tr><td colspan="4">
	    <table width="70%" cellpadding="0" cellspacing="0" border="0">
		   <tr>
		     <td class="main" align="right" width="60%">
			 	<?php if(isset($HTTP_GET_VARS['mode']) && $HTTP_GET_VARS['mode']=='edit')
						echo '<a href="' . tep_href_link(FILENAME_EDIT_ORDERS,'oID=' . $oID . '&step=2') . '">' . tep_image_button('button_back.gif',BUTTON_BACK). '</a>';
					  else
						echo '<a href="' . tep_href_link(FILENAME_ADD_EVENTS,'oID=' . $oID.'&opID='.$HTTP_GET_VARS['opID'] ) . '">' . tep_image_button('button_back.gif',BUTTON_BACK). '</a>';
				?>
			</td>
			 <Td class="main" align="right" width="10%"><?php echo '&nbsp;&nbsp;' . tep_image_submit('button_confirm.gif',BUTTON_CONFIRM);?></Td>
		   </tr>
		</table>
	  <?php  ?></td></tr>
	</table>
	<input type="hidden" name="post_action" value="save">
   </form>
   </td>
  </tr>
</table>
<!-- body_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php 	require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>