Index: /branches/2.3/expressoMail1_2/index.php
===================================================================
--- /branches/2.3/expressoMail1_2/index.php	(revision 4324)
+++ /branches/2.3/expressoMail1_2/index.php	(revision 5246)
@@ -78,4 +78,5 @@
 	$_SESSION['phpgw_info']['user']['preferences']['expressoMail']['telephone_number'] = $GLOBALS['phpgw_info']['user']['telephonenumber'];
 	$_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_cache'] = $current_config['expressoMail_enable_cache'];
+        $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['cloud_ldap'] = $current_config['expressoMail_cloud_ldap'];
 	$_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_x_origin'] = $current_config['expressoMail_use_x_origin'];
 	$_SESSION['phpgw_info']['user']['preferences']['expressoMail']['number_of_contacts'] = $current_config['expressoMail_Number_of_dynamic_contacts'] ? $current_config['expressoMail_Number_of_dynamic_contacts'] : "0";
Index: /branches/2.3/expressoMail1_2/templates/default/config.tpl
===================================================================
--- /branches/2.3/expressoMail1_2/templates/default/config.tpl	(revision 4533)
+++ /branches/2.3/expressoMail1_2/templates/default/config.tpl	(revision 5246)
@@ -37,4 +37,13 @@
      </select>
     </td>
+   </tr>
+   <tr bgcolor="{row_off}">
+   <td>{lang_Do_you_want_to_use_cloud_ldap_structure?}</td>
+       <td>
+           <select name="newsettings[expressoMail_cloud_ldap]">
+               <option value=""{selected_expressoMail_cloud_ldap_False}>{lang_No}</option>
+               <option value="True"{selected_expressoMail_cloud_ldap_True}>{lang_Yes}</option>
+           </select>
+       </td>
    </tr>
    <tr bgcolor="{row_off}">
Index: /branches/2.3/expressoMail1_2/setup/phpgw_pt-br.lang
===================================================================
--- /branches/2.3/expressoMail1_2/setup/phpgw_pt-br.lang	(revision 4906)
+++ /branches/2.3/expressoMail1_2/setup/phpgw_pt-br.lang	(revision 5246)
@@ -665,2 +665,4 @@
 Exporting selected messages, this can take some time.	expressoMail1_2	pt-br	Exportando mensagens, isso pode levar algum tempo.
 You cant manipulate messages from multiple folders on search	expressoMail1_2	pt-br	Impossível manipular mensagens de múltiplas pastas a partir de uma busca.
+No user found by this search in you organization	expressoMail1_2	pt-br	Nenhum resultado encontrado para essa pesquisa na sua organização.
+Advanced Search	expressoMail1_2	pt-br	Busca detalhada
Index: /branches/2.3/expressoMail1_2/inc/class.ldap_functions.inc.php
===================================================================
--- /branches/2.3/expressoMail1_2/inc/class.ldap_functions.inc.php	(revision 4906)
+++ /branches/2.3/expressoMail1_2/inc/class.ldap_functions.inc.php	(revision 5246)
@@ -171,5 +171,5 @@
 
 			// New search only on user sector
-			if ($count_entries > $this->max_result)
+			if ($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['cloud_ldap'] || $count_entries > $this->max_result)
  			{
 				// Close old ldap conection
@@ -190,7 +190,14 @@
 					return $return;
 				}
-				else
-				{
-					$quickSearch_only_in_userSector = true;
+				elseif($count_entries < 1){
+                                    $return = array();
+                                    return $return;
+
+                                }else
+				{
+                                        if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['cloud_ldap'])
+                                            $quickSearch_cloud = true;
+                                        else
+                                            $quickSearch_only_in_userSector = true;
 				}
 			}
@@ -226,5 +233,5 @@
 			for ($i=0; $i<$info["count"]; $i++)
 			{
-				if ($quickSearch_only_in_userSector)
+				if ($quickSearch_only_in_userSector || $quickSearch_cloud)
 				{
 					$tmp[$info[$i]["mail"][0] . '%' . $info[$i]["telephonenumber"][0] . '%'. $info[$i]["mobile"][0] . '%' . $info[$i]["uid"][0] . '%' . $info[$i]["jpegphoto"]['count'] . '%' . $info[$i]["employeenumber"][0] . '%' . $info[$i]["ou"][0]] = utf8_decode($info[$i]["cn"][0]);
@@ -260,4 +267,5 @@
 				}
 				$contacts_result['quickSearch_only_in_userSector'] = $quickSearch_only_in_userSector;
+                                $contacts_result['quickSearch_cloud'] = $quickSearch_cloud;
 			}
 			else
@@ -303,4 +311,19 @@
 						return $return;
 					}
+				}
+                                if ($quickSearch_cloud)
+				{
+						$head_option =
+							'<tr class="quicksearchcontacts_unselected">' .
+								'<td colspan="2" width="100%" align="center">' .
+									$this->functions->getLang('Showing only the results found in your organization') . '.'.
+                                                                        '<input id="QuickSearchUser_button_search" type="button" onclick="QuickSearchUser.close_window();openListUsers(\'1\');" value="busca avançada">'.
+								'</td>' .
+							'</tr>';
+
+
+
+						$contacts_result = $head_option . $options_users_from_user_org . $options;
+
 				}
 				else
@@ -414,28 +437,33 @@
 		$organizations = array();
 		$params['referral']?$referral = $params['referral']:$referral = false;
+                $params['organization']?$org = "ou=".$params['organization'].",":$org = "";
 		$cat = $params['catalog'];
-
-		$this->ldapConnect($referral,$cat);
-
-			if($this->branch != '') {
-				$filter="(&(".$this->branch."=*)(!(phpgwAccountVisible=-1)))";
-				$justthese = array("$this->branch");
-			$sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
-			$info = ldap_get_entries($this->ds, $sr);
-
-			if($info["count"] == 0)
-			{
-			    $organizations[0]['ou'] = $this->ldap_context;
-			}
-
-			for ($i=0; $i<$info["count"]; $i++)
-				$organizations[$i] = $info[$i]["ou"][0];
-
-			ldap_close($this->ds);
-			sort($organizations);
-		return $organizations;
-			}else{
+                // primeiro nível de ou
+                $this->ldapConnect($referral,$cat);
+                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['cloud_ldap'] && $cat == 0)
+                {
+                    $fistOu = substr($_SESSION['phpgw_info']['expressomail']['user']['account_dn'],strripos($_SESSION['phpgw_info']['expressomail']['user']['account_dn'], "ou="));
+                }
+                else
+                {
+                    $fistOu = $this->ldap_context;
+                }
+                if($this->branch != '') {
+                    $filter="(&(".$this->branch."=*)(!(phpgwAccountVisible=-1)))";
+                    $justthese = array("$this->branch");
+                    $sr = ldap_list($this->ds, $org.$fistOu, $filter, $justthese);
+                    $info = ldap_get_entries($this->ds, $sr);
+                    if($info["count"] == 0)
+                    {
+                        return null;
+                    }
+                    for ($i=0; $i<$info["count"]; $i++)
+                        $organizations[$i] = $info[$i]["ou"][0];
+                    ldap_close($this->ds);
+                    sort($organizations);
+                    return $organizations;
+		}else{
 			return null;
-	}
+            }
 	}
 	function get_organizations2($params){
@@ -540,6 +568,14 @@
 		$this->ldapConnect(true,$catalog);
 
-		$params['organization'] == 'all' ? $user_context = $this->ldap_context :$user_context = $this->branch."=".$params['organization'].",".$this->ldap_context;
-
+                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['cloud_ldap'] && $catalog == 0)
+                    $fistOu = substr($_SESSION['phpgw_info']['expressomail']['user']['account_dn'],strripos($_SESSION['phpgw_info']['expressomail']['user']['account_dn'], "ou="));
+                else
+                    $fistOu = $this->ldap_context;
+                
+                if($params['organization_sub'] =="")
+                    $params['organization_sub'] =="all";
+                
+		$params['organization'] == 'all' ? $user_context = $fistOu :$user_context = $this->branch."=".$params['organization'].",".$fistOu;
+                $params['organization_sub'] == 'all' ? $user_context = $user_context :$user_context = $this->branch."=".$params['organization_sub'].",".$user_context;
 		if ($this->ds) {
 			if ($catalog == 0){
Index: /branches/2.3/expressoMail1_2/js/common_functions.js
===================================================================
--- /branches/2.3/expressoMail1_2/js/common_functions.js	(revision 5162)
+++ /branches/2.3/expressoMail1_2/js/common_functions.js	(revision 5246)
@@ -571,5 +571,5 @@
 			if ((field != 'null') && (ID != 'null'))
 			{
-				QuickCatalogSearch.showList(data, quickSearchKeyBegin, quickSearchKeyEnd);
+ 				QuickCatalogSearch.showList(data, quickSearchKeyBegin, quickSearchKeyEnd);
 			}
 			else
@@ -577,8 +577,12 @@
 				QuickSearchUser.showList(data);
 			}
-		}
-		else
-			alert(get_lang('None result was found.'));
-		return true;
+	}
+		else{
+                        if(preferences.cloud_ldap)
+                            openListUsers("1",emailList);
+                        else
+                            alert(get_lang('None result was found.'));
+                }
+        	return true;
 	}
 
Index: /branches/2.3/expressoMail1_2/js/doiMenuData.js
===================================================================
--- /branches/2.3/expressoMail1_2/js/doiMenuData.js	(revision 3388)
+++ /branches/2.3/expressoMail1_2/js/doiMenuData.js	(revision 5246)
@@ -137,10 +137,14 @@
 }
 
-function openListUsers(border_id) {
+function openListUsers(border_id,search) {
 	connector.loadScript("QuickCatalogSearch");
 	if (typeof(QuickCatalogSearch) == 'undefined'){
-					setTimeout('openListUsers('+border_id+')',500);
+                                        if(typeof(search) == 'undefined')
+                                            setTimeout('openListUsers('+border_id+')',500);
+                                        else
+                                            setTimeout('openListUsers('+border_id+',"'+search+'")',500);
 					return false;
 				}
+        QuickCatalogSearch.search = search;
 	QuickCatalogSearch.showCatalogList(border_id);
 }
Index: /branches/2.3/expressoMail1_2/js/QuickCatalogSearch.js
===================================================================
--- /branches/2.3/expressoMail1_2/js/QuickCatalogSearch.js	(revision 5102)
+++ /branches/2.3/expressoMail1_2/js/QuickCatalogSearch.js	(revision 5246)
@@ -66,4 +66,9 @@
 				title_innerHTML += "<font color='BLACK' nowrap> ("+get_lang('Showing only the results found in your organization')+".)</font>"
 
+                        if (preferences.cloud_ldap)
+                        {
+                             title_innerHTML += "<font color='BLACK' nowrap> ("+get_lang('Showing only the results found in your organization')+".)</font>";
+                        }
+
 			var title = document.createElement("SPAM");
 			title.id = 'window_QuickCatalogSearch_title';
@@ -98,4 +103,13 @@
 			butt.onclick = func_add_contact;
 			el_A.appendChild(butt);
+                        if(preferences.cloud_ldap)
+                        {
+                            var butt = document.createElement('BUTTON');
+                            butt.id = "QuickCatalogSearch_button_search";
+                            var buttext = document.createTextNode(get_lang("Advanced search"));
+                            butt.appendChild(buttext);
+                            butt.onclick = function(){openListUsers("1");QuickCatalogSearch.close_QuickSearch_window(data.field, data.ID);};
+                            el_A.appendChild(butt);
+                        }
 
 		if (document.getElementById('select_QuickCatalogSearch') == null){
@@ -321,4 +335,10 @@
 			win = this.arrayWin[el.id];
 			win.open();
+                        Element('search_for').value = "";
+                        if(this.search){
+                            Element('search_for').value = this.search;
+                            write_msg("No user found by this search in you organization");
+                            this.search = null;
+                        }
 			return;		
 		}
@@ -344,6 +364,7 @@
 					   '<table border="0" cellpading="0" cellspacing="0">'+
 					   '<tr><td>'+get_lang("Select an organization and click on button <b>Search</b>")+'&nbsp;:</td></tr>'+
-  					'<tr><td><select style="display:none;width:150px" id="select_catalog" name="select_catalog" onchange="javascript:QuickCatalogSearch.update_organizations();"></select>&nbsp'+
-					'<select id="select_organization" style="width:150px" name="select_organization"></select>&nbsp;'+   
+  					'<tr><td><select style="display:none;width:100px" id="select_catalog" name="select_catalog" onchange="javascript:QuickCatalogSearch.update_organizations();"></select>&nbsp'+
+					'<select id="select_organization" style="width:100px" name="select_organization" onchange="javascript:QuickCatalogSearch.update_organizations_sub();" ></select>&nbsp;'+
+                                        '<select id="select_organization_sub" style="width:100px;display:none;" name="select_organization_sub"></select>&nbsp;'+
 					'<input type="text" id="search_for" name="search_for" value="" size="30" maxlength="30"/>&nbsp;<input type="button" onclick="QuickCatalogSearch.searchCatalogList(true)" class="button" value="'+get_lang('Search')+'">&nbsp;<input style="display:visible" type="button" onclick="QuickCatalogSearch.searchCatalogList(false)" class="button" value="'+get_lang('List All')+'"></td></tr>'+
 					   '<tr><td><input onclick="javascript:QuickCatalogSearch.changeOptions(this.value)" id="type_l" type="radio" name="type" value="l"/>'+get_lang('Public Lists')+'&nbsp;&nbsp;<input type="radio" id="type_u" name="type" value="u" onclick="javascript:QuickCatalogSearch.changeOptions(this.value)" checked/>'+get_lang('Users')+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
@@ -386,5 +407,10 @@
 		};
 
-		win = new dJSWin({			
+                if(this.search){
+                    Element('search_for').value = this.search;
+                    this.search = null;
+                    write_msg("No user found by this search in you organization");
+                }
+                win = new dJSWin({
 			id: 'win_'+el.id,
 			content_id: el.id,
@@ -495,7 +521,31 @@
 				}
 			}
+                        if(preferences.cloud_ldap)
+                            QuickCatalogSearch.update_organizations_sub();
 		}		
-		cExecute ("$this.ldap_functions.get_organizations&referral=false&catalog="+Element('select_catalog').value, handler_org);		
-	}
+		cExecute ("$this.ldap_functions.get_organizations&referral=false&catalog="+Element('select_catalog').value, handler_org);
+	}
+
+        emQuickCatalogSearch.prototype.update_organizations_sub = function(){
+		while(Element('select_organization_sub').options.length > 0) {
+			Element('select_organization_sub').remove(0);
+		}
+	 	var handler_org = function(data){
+                        Element('select_organization_sub').style.display = '';
+			Element('select_organization_sub').options[0] = new Option(get_lang('all'),'all');
+			if (data != null){
+				//var user_organization = Element('user_organizations').value;
+				for(x = 0; x < data.length; x++) {
+					Element('select_organization_sub').options[x+1] = new Option(data[x].toUpperCase(),data[x]);
+//					if(user_organization.toUpperCase() == data[x].toUpperCase())
+					
+				}
+                        Element('select_organization_sub').options[0].selected = true;
+			}
+                       
+		}
+		cExecute ("$this.ldap_functions.get_organizations&referral=false&catalog="+Element('select_catalog').value+"&organization="+Element('select_organization').value, handler_org);
+	}
+
 
 	emQuickCatalogSearch.prototype.changeOptions = function(type){	
@@ -566,5 +616,6 @@
 			return false;
 		}
-		var organization = Element('select_organization').value;		
+		var organization = Element('select_organization').value;
+                var organization_sub = Element('select_organization_sub').value;
 		var search		 = itemSearch ? Element('search_for').value : '';
 		var catalog		 = Element('select_catalog').value;
@@ -641,5 +692,5 @@
 
 		Element('msg_search').style.visibility = 'visible';
-		cExecute ("$this.ldap_functions.catalogsearch&max_result="+max_result+"&organization="+organization+"&search_for="+search+"&catalog="+catalog, handler_searchResults);
+		cExecute ("$this.ldap_functions.catalogsearch&max_result="+max_result+"&organization="+organization+"&organization_sub="+organization_sub+"&search_for="+search+"&catalog="+catalog, handler_searchResults);
 	}
 
