Index: /trunk/expressoAdmin1_2/inc/class.uiaccounts.inc.php
===================================================================
--- /trunk/expressoAdmin1_2/inc/class.uiaccounts.inc.php	(revision 180)
+++ /trunk/expressoAdmin1_2/inc/class.uiaccounts.inc.php	(revision 208)
@@ -216,5 +216,6 @@
 			// Pega combo das organizações e seleciona, caso seja um post, o setor que o usuario selecionou.
 			foreach ($manager_contexts as $index=>$context)
-				$sectors .= $this->functions->get_organizations($context);
+				$combo_manager_org .= $this->functions->get_organizations($context);
+			$combo_all_orgs = $this->functions->get_organizations($GLOBALS['phpgw_info']['server']['ldap_context'], '', true, true, true);
 
 			// Chama funcao para criar lista de aplicativos disponiveis.
@@ -273,7 +274,7 @@
 				'lang_back'						=> lang('Back'),
 				'lang_save'						=> lang('Save'),
-				
-				'sectors'						=> $sectors,
-				'combo_organizations'			=> $sectors,
+				'sectors'						=> $combo_manager_org,
+				'combo_organizations'			=> $combo_manager_org,
+				'combo_all_orgs'				=> $combo_all_orgs,
 				'passwd_expired_checked'		=> 'CHECKED',
 				'changepassword_checked'		=> 'CHECKED',
@@ -430,6 +431,13 @@
 							
     		// ORGANIZATIONS
+    		/*
    			foreach ($contexts as $index=>$context)
    				$orgs .= $this->functions->get_organizations($context, $user_info['context']);
+   			*/
+   				
+			foreach ($contexts as $index=>$context)
+				$combo_manager_org .= $this->functions->get_organizations($context, $user_info['context']);
+			$combo_all_orgs = $this->functions->get_organizations($GLOBALS['phpgw_info']['server']['ldap_context'], $user_info['context'], true, true, true);			
+
 
 			// GROUPS.
@@ -498,10 +506,10 @@
 				foreach ($user_info['maillists_info'] as $maillist)
 				{
-					$array_maillist[$maillist['uidnumber']] = $maillist['uid'] . "  (" . $maillist['mail'] . ") ";
+					$array_maillist[$maillist['uid']] = $maillist['uid'] . "  (" . $maillist['mail'] . ") ";
 				}
 				natcasesort($array_maillist);
-				foreach ($array_maillist as $uidnumber=>$option)
-				{
-					$ea_select_user_maillists_options .= "<option value=" . $uidnumber . ">" . $option . "</option>";
+				foreach ($array_maillist as $uid=>$option)
+				{
+					$ea_select_user_maillists_options .= "<option value=" . $uid . ">" . $option . "</option>";
 				}
 			}
@@ -622,6 +630,7 @@
 				'lang_save'						=> lang('Save'),
 				
-				'sectors'						=> $orgs,
-				'combo_organizations'			=> $orgs,
+				'sectors'						=> $combo_manager_org,
+				'combo_organizations'			=> $combo_manager_org,
+				'combo_all_orgs'				=> $combo_all_orgs,
 				
 				'uid'							=> $user_info['uid'],
Index: /trunk/expressoAdmin1_2/inc/class.user.inc.php
===================================================================
--- /trunk/expressoAdmin1_2/inc/class.user.inc.php	(revision 180)
+++ /trunk/expressoAdmin1_2/inc/class.user.inc.php	(revision 208)
@@ -23,5 +23,4 @@
 		var $current_config;
 		
-		
 		function user()
 		{
@@ -224,7 +223,7 @@
 				if ($params['maillists'])
 				{
-					foreach($params['maillists'] as $uidnumber)
+					foreach($params['maillists'] as $uid)
 	            	{
-						$result = $this->ldap_functions->add_user2maillist($uidnumber, $user_info['mail']);
+						$result = $this->ldap_functions->add_user2maillist($uid, $user_info['mail']);
 						if (!$result['status'])
 						{
@@ -756,6 +755,4 @@
 					foreach($remove_groups as $gidnumber)
 					{
-						echo $gidnumber;
-						
 						foreach($old_values['groups_info'] as $group)
 						{
@@ -836,17 +833,22 @@
 				if (count($add_maillists)>0)
 				{
-					foreach($add_maillists as $uidnumber)
-					{
-						$this->ldap_functions->add_user2maillist($uidnumber, $new_values['mail']);
-						$this->db_functions->write_log("adicionado usuario a maillist $uidnumber.",'',$dn,'','');
-					}
-				}
+					foreach($add_maillists as $uid)
+					{
+						$this->ldap_functions->add_user2maillist($uid, $new_values['mail']);
+						$this->db_functions->write_log("adicionado usuario a maillist $uid.",'',$dn,'','');
+					}
+				}
+				/*
+				echo '<pre>';
+				print_r($old_values['maillists']);
+				print_r($new_values['maillists']);
+				*/
 				
 				if (count($remove_maillists)>0)
 				{
-					foreach($remove_maillists as $uidnumber)
-					{
-						$this->ldap_functions->remove_user2maillist($uidnumber, $new_values['mail']);
-						$this->db_functions->write_log("removido usuario da maillist $uidnumber.",'',$dn,'','');
+					foreach($remove_maillists as $uid)
+					{
+						$this->ldap_functions->remove_user2maillist($uid, $new_values['mail']);
+						$this->db_functions->write_log("removido usuario da maillist $uid.",'',$dn,'','');
 					}
 				}
Index: /trunk/expressoAdmin1_2/inc/class.maillist.inc.php
===================================================================
--- /trunk/expressoAdmin1_2/inc/class.maillist.inc.php	(revision 180)
+++ /trunk/expressoAdmin1_2/inc/class.maillist.inc.php	(revision 208)
@@ -100,14 +100,5 @@
 			if ($params['phpgwAccountVisible'] == 'on')
 				$maillist_info['phpgwAccountVisible'] = '-1';
-			
-			/*
-			foreach($params['members'] as $index=>$uidnumber)
-			{
-				$mail = $this->ldap_functions->uidnumber2mail($uidnumber);
-				$maillist_info['mailForwardingAddress'][] = $mail;
-				$this->db_functions->write_log("Adicionado usuario $mail a lista ".$params['cn']." no momento da criação",$dn,$uidnumber,'','');
-			}
-			*/
-			
+						
 			$maillist_info['mailForwardingAddress'] = $params['mailForwardingAddress'];
 			
@@ -234,4 +225,8 @@
 			////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 			// USERS
+			/*
+			echo '<pre>';
+			print_r($new_values['mailForwardingAddress']);
+			*/
 			if (!$new_values['mailForwardingAddress'])
 				$new_values['mailForwardingAddress'] = array();
@@ -242,11 +237,8 @@
 			$remove_users = array_diff($old_values['mailForwardingAddress'], $new_values['mailForwardingAddress']);
 			
-/*			echo '<pre>';
-			print_r($new_values);*/
-			
 			if (count($add_users)>0)
 			{
 				sort($add_users);
-				$result = $this->ldap_functions->add_user2maillist($new_values['uidnumber'], $add_users);
+				$result = $this->ldap_functions->add_user2maillist($new_values['uid'], $add_users);
 				
 				/* log */
@@ -258,5 +250,5 @@
 				else
 				{
-					foreach($add_users['mailForwardingAddress'] as $index=>$mail)
+					foreach($add_users as $index=>$mail)
 					{
 						$this->db_functions->write_log("Adicionado usuario $mail a lista de email $dn",$dn,'','','');
@@ -268,5 +260,5 @@
 			{
 				sort($remove_users);
-				$result = $this->ldap_functions->remove_user2maillist($new_values['uidnumber'], $remove_users);
+				$result = $this->ldap_functions->remove_user2maillist($new_values['uid'], $remove_users);
 				
 				/* log */
@@ -302,8 +294,9 @@
 			//Retira os uids duplicados se existir
 			$array_tmp = array();
-			$array_tmp = array_unique($new_values['members']);
+			$array_tmp = @array_unique($new_values['members']);
 			$new_values['members'] = $array_tmp;
 			
 			$old_values = $this->get_scl_info($new_values['uidnumber'], $new_values['manager_context']);
+			
 			$diff = array_diff($new_values, $old_values);
 			$dn = $old_values['dn'];
@@ -372,40 +365,39 @@
 			if (count($add_users)>0)
 			{
-				$array_emails_add = array();
-				foreach($add_users as $uidnumber)
-				{
-					if ($uidnumber != -1)
+				sort($add_users);
+				$result = $this->ldap_functions->add_user2maillist_scl($dn, $add_users);
+				
+				/* log */
+				if (!$result['status'])
+				{
+					$return['status'] = false;
+					$return['msg'] .= $result['msg'];
+				}
+				else
+				{
+					foreach($add_users as $index=>$mail)
 					{
-						$mail = $this->ldap_functions->uidnumber2mail($uidnumber);
-						$array_emails_add[] = $mail;
-						$this->db_functions->write_log("Adicionado usuario $mail a SCL da lista $dn",'',$uidnumber,'','');
+						$this->db_functions->write_log("Adicionado usuario $mail da SCP da lista de email $dn",$dn,'','','');
 					}
 				}
-				$result = $this->ldap_functions->add_user2maillist_scl($dn, $array_emails_add);
-				if (!$result['status'])
-				{
-					$return['status'] = false;
-					$return['msg'] .= $result['msg'];
-				}
+			}
+			
+			if (count($remove_users)>0)
+			{
+				sort($remove_users);
+				$result = $this->ldap_functions->remove_user2maillist_scl($dn, $remove_users);
 				
-			}
-			
-			if (count($remove_users)>0)
-			{
-				$array_emails_remove = array();
-				foreach($remove_users as $uidnumber)
-				{
-					if ($uidnumber != -1)
+				/* log */
+				if (!$result['status'])
+				{
+					$return['status'] = false;
+					$return['msg'] .= $result['msg'];
+				}
+				else
+				{
+					foreach($add_users as $index=>$mail)
 					{
-						$mail = $this->ldap_functions->uidnumber2mail($uidnumber);
-						$array_emails_remove[] = $mail;
-						$this->db_functions->write_log("Removido usuario $mail da SCP da lista $dn",'',$uidnumber,'','');
+						$this->db_functions->write_log("Removido usuario $mail da SCP da lista de email $dn",$dn,'','','');
 					}
-				}
-				$result = $this->ldap_functions->remove_user2maillist_scl($dn, $array_emails_remove);
-				if (!$result['status'])
-				{
-					$return['status'] = false;
-					$return['msg'] .= $result['msg'];
 				}
 			}
Index: /trunk/expressoAdmin1_2/inc/class.uimanagers.inc.php
===================================================================
--- /trunk/expressoAdmin1_2/inc/class.uimanagers.inc.php	(revision 180)
+++ /trunk/expressoAdmin1_2/inc/class.uimanagers.inc.php	(revision 208)
@@ -162,5 +162,5 @@
 			*/
 			
-			$options_context = $this->functions->get_organizations($GLOBALS['phpgw_info']['server']['ldap_context'], '', false, true);
+			$options_context = $this->functions->get_organizations($GLOBALS['phpgw_info']['server']['ldap_context'], '', false, true, false);
 			
 			// Seta variaveis que estao no TPL
@@ -292,5 +292,5 @@
 			foreach ($a_context as $context)
 				$input_context_fields .= '<div><input disabled type="text" value="'.$context.'" size=60></input><span onclick="this.parentNode.parentNode.removeChild(this.parentNode);" style="cursor:pointer"> -</span></div>';
-			$options_context = $this->functions->get_organizations($GLOBALS['phpgw_info']['server']['ldap_context'], '', false, true);
+			$options_context = $this->functions->get_organizations($GLOBALS['phpgw_info']['server']['ldap_context'], '', false, true, false);
 
 			$var = Array(
Index: /trunk/expressoAdmin1_2/inc/class.uimaillists.inc.php
===================================================================
--- /trunk/expressoAdmin1_2/inc/class.uimaillists.inc.php	(revision 180)
+++ /trunk/expressoAdmin1_2/inc/class.uimaillists.inc.php	(revision 208)
@@ -189,5 +189,5 @@
 			foreach ($manager_contexts as $index=>$context)
 				$combo_manager_org .= $this->functions->get_organizations($context);
-			$combo_all_orgs = $this->functions->get_organizations($GLOBALS['phpgw_info']['server']['ldap_context'], '', true, true);			
+			$combo_all_orgs = $this->functions->get_organizations($GLOBALS['phpgw_info']['server']['ldap_context'], '', true, true, true);			
 			
 			// Seta variaveis utilizadas pelo tpl.
@@ -252,5 +252,5 @@
 			foreach ($manager_contexts as $index=>$context)
 				$combo_manager_org .= $this->functions->get_organizations($context, trim(strtolower($maillist_info['context'])));
-			$combo_all_orgs = $this->functions->get_organizations($GLOBALS['phpgw_info']['server']['ldap_context'], trim(strtolower($maillist_info['context'])), true, true);			
+			$combo_all_orgs = $this->functions->get_organizations($GLOBALS['phpgw_info']['server']['ldap_context'], trim(strtolower($maillist_info['context'])), true, true, true);			
 
 			// Usuarios da lista.
@@ -367,12 +367,12 @@
 			if (count($maillist_info['senders_info']) > 0)
 			{
-				foreach ($maillist_info['senders_info'] as $uidnumber=>$senderinfo)
-				{
-					$array_senders[$uidnumber] = $senderinfo['cn'];
+				foreach ($maillist_info['senders_info'] as $mail=>$senderinfo)
+				{
+					$array_senders[$mail] = $senderinfo['cn'];
 				}
 				natcasesort($array_senders);
-				foreach ($array_senders as $uidnumber=>$cn)
-				{
-					$ea_select_users_SCL_Maillist .= "<option value=" . $uidnumber . ">" . $cn . " [" . $maillist_info['senders_info'][$uidnumber]['mail'] . "]</option>";
+				foreach ($array_senders as $mail=>$cn)
+				{
+					$ea_select_users_SCL_Maillist .= "<option value=" . $mail . ">" . $cn . " [" . $mail . "]</option>";
 				}
 			}
Index: /trunk/expressoAdmin1_2/inc/class.ldap_functions.inc.php
===================================================================
--- /trunk/expressoAdmin1_2/inc/class.ldap_functions.inc.php	(revision 180)
+++ /trunk/expressoAdmin1_2/inc/class.ldap_functions.inc.php	(revision 208)
@@ -4,4 +4,12 @@
 include_once(PHPGW_API_INC.'/class.common.inc.php');
 include_once('class.functions.inc.php');
+
+function ldapRebind($ldap_connection, $ldap_url)
+{
+	if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
+	{
+		ldap_bind($ldap_connection, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']);
+	}
+}
 
 class ldap_functions
@@ -23,4 +31,37 @@
 	}
 	
+	/* ldap connection following referals and using Contac Center config*/
+	function ldapMasterConnect()
+	{
+		if ( (is_array($_SESSION['phpgw_info']['expresso']['cc_ldap_server'])) && ($ldap_connection = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host'])) )
+		{
+			ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
+			ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
+			ldap_set_rebind_proc($ldap_connection, ldapRebind);
+			if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
+			{
+				if ( ! ldap_bind($ldap_connection, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']) )
+				{
+					echo 'retornou false1';
+					return false;
+				}
+			}
+			return $ldap_connection;
+		}
+		else
+		{
+			$ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
+			if ($ldap_connection)
+			{
+				ldap_set_option($ldap_connection,LDAP_OPT_PROTOCOL_VERSION,3);
+				ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
+				if ( ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']) )
+					return $ldap_connection;
+			}
+		}
+		
+		return false;
+	}
+		
 	function validate_fields($params)
 	{
@@ -71,6 +112,5 @@
 		if (($type == 'create_user') || ($type == 'rename_user')) 
 		{
-			//UID
-			if (($type == 'rename_user') && ($this->current_config['expressoAdmin_prefix_org'] == 'true'))
+			if ($this->current_config['expressoAdmin_prefix_org'] == 'true')
 			{
 				//Obtenho UID sem a organização. Na criação o uid já vem sem a organização
@@ -85,4 +125,21 @@
 				$filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$uid))";
 			}
+			/*
+			//UID
+			if (($type == 'rename_user') && ($this->current_config['expressoAdmin_prefix_org'] == 'true'))
+			{
+				//Obtenho UID sem a organização. Na criação o uid já vem sem a organização
+				$tmp_uid_without_org = split("-", $params['uid']);
+				$tmp_reverse_uid_without_org = array_reverse($tmp_uid_without_org);
+				array_pop($tmp_reverse_uid_without_org);
+				$uid_without_org = implode("-", $tmp_reverse_uid_without_org);
+				$filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(uid=$uid)(uid=$uid_without_org)))";
+			}
+			else
+			{
+				$filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$uid))";
+			}
+			*/
+			
 			$justthese = array("uid", "mail", "cn");
 			$search = ldap_search($ldap_connection, $context, $filter, $justthese);
@@ -333,5 +390,4 @@
 		
 		// MAIL
-		/*
 		$filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(mail=$mail)(mailalternateaddress=$mail)))";
 		$justthese = array("mail");
@@ -343,5 +399,5 @@
 			$result['msg'] = 'E-MAIL da lista já está sendo usado.';
 			return $result;
-		}*/
+		}
 		
 		return $result;	
@@ -536,17 +592,22 @@
 	function get_available_maillists($params)
 	{
+		if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
+			return false;
+		
 		$context = $params['context'];
 		$justthese = array("uid","mail","uidNumber");
-    	$maillists=ldap_list($this->ldap, $context, ("(phpgwAccountType=l)"), $justthese);
-    	ldap_sort($this->ldap, $maillists, "uid");
+    	$maillists=ldap_list($ldapMasterConnect, $context, ("(phpgwAccountType=l)"), $justthese);
+    	ldap_sort($ldapMasterConnect, $maillists, "uid");
     	
-    	$entries = ldap_get_entries($this->ldap, $maillists);
+    	$entries = ldap_get_entries($ldapMasterConnect, $maillists);
     	
 		$options = '';			
 		for ($i=0; $i<$entries['count']; $i++)
 		{
-			$options .= "<option value=" . $entries[$i]['uidnumber'][0] . ">" . $entries[$i]['uid'][0] . " (" . $entries[$i]['mail'][0] . ")" . "</option>";
-		}
-    	return $options;		
+			$options .= "<option value=" . $entries[$i]['uid'][0] . ">" . $entries[$i]['uid'][0] . " (" . $entries[$i]['mail'][0] . ")" . "</option>";
+		}
+    	
+    	ldap_close($ldapMasterConnect);
+    	return $options;
 	}
 	
@@ -659,13 +720,20 @@
 	}
 	
-	function add_user2maillist($uidNumber, $mail)
-	{
-		$filter = "(&(phpgwAccountType=l)(uidNumber=$uidNumber))";
+	function add_user2maillist($uid, $mail)
+	{
+		if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
+		{
+			$result['status'] = false;
+			$result['msg'] = "Falha de conexão com o openLDAP.\n\nRetorno do servidor:" . ldap_error($ldapMasterConnect);
+			return $result;
+		}
+			
+		$filter = "(&(phpgwAccountType=l)(uid=$uid))";
 		$justthese = array("dn");
-		$search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
-		$entry = ldap_get_entries($this->ldap, $search);
+		$search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
+		$entry = ldap_get_entries($ldapMasterConnect, $search);
 		$group_dn = $entry[0]['dn'];
 		$attrs['mailForwardingAddress'] = $mail;
-		$res = @ldap_mod_add($this->ldap, $group_dn, $attrs);
+		$res = @ldap_mod_add($ldapMasterConnect, $group_dn, $attrs);
 		
 		if ($res)
@@ -678,4 +746,6 @@
 			$result['msg'] = "Erro na funcao ldap_functions->add_user2maillist ($mail).\nRetorno do servidor:" . ldap_error($this->ldap);
 		}
+		
+		ldap_close($ldapMasterConnect);
 		return $result;
 	}
@@ -684,4 +754,7 @@
 	{
 		$attrs['mailSenderAddress'] = $array_emails;
+		
+		print_r($attrs);
+		
 		$res = @ldap_mod_add($this->ldap, $dn, $attrs);
 		
@@ -698,13 +771,20 @@
 	}
 
-	function remove_user2maillist($uidNumber, $mail)
-	{
-		$filter = "(&(phpgwAccountType=l)(uidNumber=$uidNumber))";
+	function remove_user2maillist($uid, $mail)
+	{
+		if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
+		{
+			$result['status'] = false;
+			$result['msg'] = "Falha de conexão com o openLDAP.\n\nRetorno do servidor:" . ldap_error($ldapMasterConnect);
+			return $result;
+		}
+		
+		$filter = "(&(phpgwAccountType=l)(uid=$uid))";
 		$justthese = array("dn");
-		$search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
-		$entry = ldap_get_entries($this->ldap, $search);
+		$search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
+		$entry = ldap_get_entries($ldapMasterConnect, $search);
 		$group_dn = $entry[0]['dn'];
 		$attrs['mailForwardingAddress'] = $mail;
-		$res = @ldap_mod_del($this->ldap, $group_dn, $attrs);
+		$res = @ldap_mod_del($ldapMasterConnect, $group_dn, $attrs);
 		
 		if ($res)
@@ -717,4 +797,5 @@
 			$result['msg'] = "Erro na funcao ldap_functions->remove_user2maillist ($mail).\n\nRetorno do servidor:" . ldap_error($this->ldap);
 		}
+		ldap_close($ldapMasterConnect);
 		return $result;
 	}
@@ -829,8 +910,10 @@
 				$result['maillists_info'] = $this->get_user_maillists($result['mail']);
 				if($result['maillists_info'])
+				{
 					foreach ($result['maillists_info'] as $maillist)
 					{
-						$result['maillists'][] = $maillist['uidnumber'];
+						$result['maillists'][] = $maillist['uid'];
 					}
+				}
 				
 				// Groups
@@ -851,4 +934,7 @@
 	function get_user_maillists($mail)
 	{
+		if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
+			return false;
+		
 		$result = array();
 		
@@ -859,11 +945,9 @@
 		foreach ($this->manager_contexts as $index=>$context)
 		{
-			$search = ldap_search($this->ldap, $context, $filter, $justthese);
-			//ldap_sort($this->ldap, $search, "uid");
-    		$entries = ldap_get_entries($this->ldap, $search);
+			$search = ldap_search($ldapMasterConnect, $context, $filter, $justthese);
+    		$entries = ldap_get_entries($ldapMasterConnect, $search);
     		
 	    	for ($i=0; $i<$entries['count']; $i++)
     		{
-				$result[ $entries[$i]['uid'][0] ]['uidnumber']	= $entries[$i]['uidnumber'][0];
 				$result[ $entries[$i]['uid'][0] ]['uid']		= $entries[$i]['uid'][0];
 				$result[ $entries[$i]['uid'][0] ]['mail']		= $entries[$i]['mail'][0];
@@ -878,9 +962,9 @@
     		foreach ($a_tmp as $uid)
     		{
-    			$return[$uid]['uidnumber']	= $result[$uid]['uidnumber'];
 				$return[$uid]['uid']		= $result[$uid]['uid'];
 				$return[$uid]['mail']		= $result[$uid]['mail'];
     		}
     	}
+    	ldap_close($ldapMasterConnect);
 		return $return;
 	}
@@ -1093,8 +1177,7 @@
 					$user_entry = ldap_get_entries($this->ldap, $search);
 			
-					$result['senders_info'][$user_entry[0]['uidnumber'][0]]['uid'] = $user_entry[0]['uid'][0];
-					$result['senders_info'][$user_entry[0]['uidnumber'][0]]['cn'] = $user_entry[0]['cn'][0];
-					$result['senders_info'][$user_entry[0]['uidnumber'][0]]['mail'] = $user_entry[0]['mail'][0];
-					$result['members'][] = $user_entry[0]['uidnumber'][0];
+					$result['senders_info'][$user_entry[0]['mail'][0]]['uid'] = $user_entry[0]['uid'][0];
+					$result['senders_info'][$user_entry[0]['mail'][0]]['cn'] = $user_entry[0]['cn'][0];
+					$result['members'][] = $user_entry[0]['mail'][0];
 				}
 				return $result;
@@ -1282,16 +1365,25 @@
 		$attrs = array();
 		$attrs['mailForwardingAddress'] = $user_info['mail'];
+		
 		if (count($user_info['maillists_info']))
 		{
+			
+			if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
+			{
+				$return['status'] = false;
+				$return['msg'] .= 'Erro ao conectar ao ldap_master: ' . ldap_error($this->ldap);
+				return $return;
+			}
+			
 			foreach ($user_info['maillists_info'] as $maillists_info)
 			{
-				$uidnumber = $maillists_info['uidnumber'];
+				$uid = $maillists_info['uid'];
 				$justthese = array("dn");
-				$filter="(&(phpgwAccountType=l)(uidnumber=".$uidnumber."))";
-				$search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
-	    		$entry = ldap_get_entries($this->ldap, $search);
+				$filter="(&(phpgwAccountType=l)(uid=".$uid."))";
+				$search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
+	    		$entry = ldap_get_entries($ldapMasterConnect, $search);
 				$dn = $entry[0]['dn'];
 			
-				if (!@ldap_mod_del($this->ldap, $dn, $attrs))
+				if (!@ldap_mod_del($ldapMasterConnect, $dn, $attrs))
 				{
 					$return['status'] = false;
@@ -1299,4 +1391,5 @@
 				}
 			}
+			ldap_close($ldapMasterConnect);
 		}
 			
Index: /trunk/expressoAdmin1_2/inc/class.functions.inc.php
===================================================================
--- /trunk/expressoAdmin1_2/inc/class.functions.inc.php	(revision 107)
+++ /trunk/expressoAdmin1_2/inc/class.functions.inc.php	(revision 208)
@@ -494,11 +494,11 @@
 		}
 		
-		function get_organizations($context, $selected='', $referral=false, $show_invisible_ou=true)
+		function get_organizations($context, $selected='', $referral=false, $show_invisible_ou=true, $master=false)
 		{
 			$s = CreateObject('phpgwapi.sector_search_ldap');
-			$sectors_info = $s->get_organizations($context, $selected, $referral, $show_invisible_ou);
+			$sectors_info = $s->get_organizations($context, $selected, $referral, $show_invisible_ou, $master);
 			return $sectors_info;
 		}		
-		
+				
 		function get_sectors($selected='', $referral=false, $show_invisible_ou=true)
 		{
Index: /trunk/expressoAdmin1_2/js/jscode/maillists.js
===================================================================
--- /trunk/expressoAdmin1_2/js/jscode/maillists.js	(revision 180)
+++ /trunk/expressoAdmin1_2/js/jscode/maillists.js	(revision 208)
@@ -66,9 +66,15 @@
 	// Needed select all options from select
 	for(var i=0; i<select_userInMaillist.options.length; i++)
-		select_userInMaillist.options[i].selected = true;
-	
+	{
+		// No IE, não seleciona o separador do select
+		if (select_userInMaillist.options[i].value != -1)
+			select_userInMaillist.options[i].selected = true;
+		else
+			select_userInMaillist.options[i].selected = false;
+	}
+
 	// O UID da lista foi alterado ou é uma nova lista.
 	if ((document.forms[0].old_uid.value != document.forms[0].uid.value) || (type == 'create_maillist')){
-		cExecute ('$this.maillist.validate_fields&uid='+document.forms[0].uid.value, handler_validate_fields);
+		cExecute ('$this.maillist.validate_fields&uid='+document.forms[0].uid.value+'&mail='+document.forms[0].mail.value, handler_validate_fields);
 	}
 	else if (type == 'edit_maillist')
Index: /trunk/expressoAdmin1_2/js/jscode/users.js
===================================================================
--- /trunk/expressoAdmin1_2/js/jscode/users.js	(revision 81)
+++ /trunk/expressoAdmin1_2/js/jscode/users.js	(revision 208)
@@ -682,7 +682,23 @@
 }
 
+/*
 function search_organization(key)
 {
 	var organizations = document.getElementById('ea_combo_org_info');
+	var RegExp_org = new RegExp("\\b"+key, "i");
+	
+	for(i = 0; i < organizations.length; i++)
+	{
+		if (RegExp_org.test(organizations[i].text))
+		{
+			organizations[i].selected = true;
+			return;
+		}
+	}
+}*/
+
+function search_organization(key, element)
+{
+	var organizations = document.getElementById(element);
 	var RegExp_org = new RegExp("\\b"+key, "i");
 	
