Index: /trunk/expressoMail1_2/inc/class.imap_functions.inc.php
===================================================================
--- /trunk/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 1608)
+++ /trunk/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 1614)
@@ -2956,93 +2956,113 @@
 
 	function search_msg($params = ''){
-		$retorno = "";
-		$mbox_stream = "";
-		if(strpos($params['condition'],"#")===false) { //local messages
-			$search=false;
-		}
-		else {
-			$search = explode(",",$params['condition']);
-		}
-
-		if($search){
-			$search_criteria = '';
-                        $search_result_number = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['search_result_number'];
-                        $acumulated_results = 0;
-			foreach($search as $tmp)
-			{
-				$tmp1 = explode("##",$tmp);
-                                $name_box = $tmp1[0];
-				unset($filter);
-				foreach($tmp1 as $index => $criteria)
-				{
-					if ($index != 0 && strlen($criteria) != 0)
-					{
-						$filter_array = explode("<=>",rawurldecode($criteria));
-						$filter .= " ".$filter_array[0];
-						$filter .= '"'.$filter_array[1].'"';
-					}
-				}
-				$name_box = mb_convert_encoding(utf8_decode($name_box), "UTF7-IMAP", "ISO_8859-1" );
-				$filter = $this->remove_accents($filter);
-				//Este bloco tem a finalidade de transformar o login (quando numerico) das pastas compartilhadas em common name
-				$folder_name = explode($this->imap_delimiter,$name_box);
-				if (is_numeric($folder_name[1])) {
-					$this->ldap = new ldap_functions();
-					if ($cn = $this->ldap->uid2cn($folder_name[1]))	{
-						$folder_name[1] = $cn;
-					}
-				}
-				$folder_name = implode($this->imap_delimiter,$folder_name);
-
-				if(!is_resource($mbox_stream))
-					$mbox_stream = $this->open_mbox($name_box);
-				else
-					imap_reopen($mbox_stream, "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$name_box);
-
-                                $sum = 0;
-				if (preg_match("/^.?\bALL\b/", $filter)){ // Quick Search, note: this ALL isn't the same ALL from imap_search
-
-					$all_criterias = array ("TO","SUBJECT","FROM","CC");
-					foreach($all_criterias as $criteria_fixed)
-					{
-						$_filter = $criteria_fixed . substr($filter,4);
-
-						$search_criteria = imap_search($mbox_stream, $_filter, SE_UID);
-
-						if($search_criteria) //&& count($search_criteria) < 50)
-						{
-							foreach($search_criteria as $new_search){
-								$m_token = trim("##".mb_convert_encoding( $name_box, "ISO_8859-1", "UTF7-IMAP" ) . "--" . $this->get_msg($new_search,$name_box,$mbox_stream) . "--".$new_search."##"."\n");
-								if(!@strstr($retorno,$m_token))
-                                                                {
-                                                                    $retorno .= $m_token;
-                                                                    $sum += 1;
-                                                                }
-							}
-						}
-					}
-				}
-				else {
-                                    $search_criteria = imap_search($mbox_stream, $filter, SE_UID);
-                                    if( is_array( $search_criteria) )
+            $retorno = "";
+            $mbox_stream = "";
+            if(strpos($params['condition'],"#")===false) { //local messages
+                    $search=false;
+            }
+            else {
+                    $search = explode(",",$params['condition']);
+            }
+
+            if($search){
+                $search_criteria = '';
+                $search_result_number = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['search_result_number'];
+                foreach($search as $tmp)
+                {
+                    $tmp1 = explode("##",$tmp);
+                    $sum = 0;
+                    $name_box = $tmp1[0];
+                    unset($filter);
+                    foreach($tmp1 as $index => $criteria)
+                    {
+                        if ($index != 0 && strlen($criteria) != 0)
+                        {
+                            $filter_array = explode("<=>",rawurldecode($criteria));
+                            $filter .= " ".$filter_array[0];
+                            $filter .= '"'.$filter_array[1].'"';
+                        }
+                    }
+                    $name_box = mb_convert_encoding(utf8_decode($name_box), "UTF7-IMAP", "ISO_8859-1" );
+                    $filter = $this->remove_accents($filter);
+                    //Este bloco tem a finalidade de transformar o login (quando numerico) das pastas compartilhadas em common name
+                    $folder_name = explode($this->imap_delimiter,$name_box);
+                    if (is_numeric($folder_name[1]))
+                    {
+                        $this->ldap = new ldap_functions();
+                        if ($cn = $this->ldap->uid2cn($folder_name[1]))
+                        {
+                            $folder_name[1] = $cn;
+                        }
+                    }
+                    $folder_name = implode($this->imap_delimiter,$folder_name);
+
+                    if(!is_resource($mbox_stream))
+                    {
+                        $mbox_stream = $this->open_mbox($name_box);
+                    }
+                    else
+                        {
+                            imap_reopen($mbox_stream, "{".$this->imap_server.":".$this->imap_port.$this->imap_options."}".$name_box);
+                        }
+
+                    if (preg_match("/^.?\bALL\b/", $filter))
+                    { // Quick Search, note: this ALL isn't the same ALL from imap_search
+
+                        $all_criterias = array ("TO","SUBJECT","FROM","CC");
+                        foreach($all_criterias as $criteria_fixed)
+                        {
+                            $_filter = $criteria_fixed . substr($filter,4);
+
+                            $search_criteria = imap_search($mbox_stream, $_filter, SE_UID);
+
+                            if($search_criteria) //&& count($search_criteria) < 50)
+                            {
+                                foreach($search_criteria as $new_search)
+                                {
+                                    if ($search_result_number != '65536' && $sum == $search_result_number)
                                     {
-                                            foreach($search_criteria as $new_search)
-                                                    $retorno .= trim("##".mb_convert_encoding( $name_box, "ISO_8859-1", "UTF7-IMAP" ) . "--" . $this->get_msg($new_search,$name_box,$mbox_stream) . "--" . $new_search."##"."\n");
+                                        return $retorno ? $sum . "=sumResults=" . $retorno : "none";
                                     }
-                                    $sum += count($search_criteria);
-				}
-
-                                $acumulated_results += $sum;
-
-                                if ($search_result_number != '65536' && $acumulated_results >= $search_result_number)
+
+                                    $m_token = trim("##".mb_convert_encoding( $name_box, "ISO_8859-1", "UTF7-IMAP" ) . "--" . $this->get_msg($new_search,$name_box,$mbox_stream) . "--".$new_search."##"."\n");
+                                    if(!@strstr($retorno,$m_token))
+                                    {
+                                        $retorno .= $m_token;
+                                        $sum ++;
+                                    }
+                                }
+                            }
+                        }
+                    }
+                    else {
+                        $search_criteria = imap_search($mbox_stream, $filter, SE_UID);
+                        if( is_array( $search_criteria) )
+                        {
+                            foreach($search_criteria as $new_search)
+                            {
+                                if ($search_result_number != '65536' && $sum == $search_result_number)
                                 {
-                                    return "many results";
+                                    return $retorno ? $sum . "=sumResults=" . $retorno : "none";
                                 }
-			}
-		}
-		if($mbox_stream)
-			imap_close($mbox_stream);
-
-		return $retorno ? $retorno : "none";
+                                $retorno .= trim("##".mb_convert_encoding( $name_box, "ISO_8859-1", "UTF7-IMAP" ) . "--" . $this->get_msg($new_search,$name_box,$mbox_stream) . "--" . $new_search."##"."\n");
+                                $sum++;
+                            }
+                        }
+                    }
+                }
+            }
+            if($mbox_stream)
+            {
+                imap_close($mbox_stream);
+            }
+
+            if ($retorno)
+            {
+                return $retorno;
+            }
+            else
+            {
+                return 'none';
+            }
 	}
 
Index: /trunk/expressoMail1_2/js/search.js
===================================================================
--- /trunk/expressoMail1_2/js/search.js	(revision 1608)
+++ /trunk/expressoMail1_2/js/search.js	(revision 1614)
@@ -292,6 +292,13 @@
 			}
 		}
-		if(nw_array.length > 0 && showMsg)
-			write_msg(nw_array.length + " "+ get_lang('results found'));
+
+                if( (preferences.search_result_number != '65536') && (nw_array.length > 0) && (nw_array.length >= preferences.search_result_number) ){
+                    nw_array.length = preferences.search_result_number;
+                    write_msg(get_lang("More than %1 results. Please, try to refine your search.",preferences.search_result_number));
+                }
+                else {
+                        write_msg(nw_array.length + " "+ get_lang('results found'));
+                }
+
 		numBox++;
 		var border_id = create_border(get_lang("Search Results"), "search_" + numBox);
@@ -781,4 +788,10 @@
 			var tmp;
 			var countTmp= new Array();
+
+                        var dataTemp = data.split('=sumResults=');
+                        var sumResults = dataTemp[0];
+
+                        var data = dataTemp[1] ? dataTemp[1] : dataTemp[0];
+
 			if (local_folders.length > 0) {
 				tmp = expresso_local_messages.search(local_folders, fields);
@@ -788,13 +801,22 @@
 				}
 			}
-			if((data == "none") && (countTmp.length==0)){alert(get_lang("None result was found."));}
-			if (preferences.search_result_number == '65536' || (data != 'many results' && countTmp.length < preferences.search_result_number))
+//			if((data == "none") && (countTmp.length==0)){alert(get_lang("None result was found."));}
+//			if (preferences.search_result_number == '65536' || (data != 'many results' && countTmp.length < preferences.search_result_number))
+//                        {
+//                            EsearchE.mount_result((data=='none')?tmp:(countTmp.length==0)?data:data+tmp);
+//                        }
+//                        else if (data == 'many results' || countTmp.length >= preferences.search_result_number)
+//                        {
+//                            alert(get_lang("More than %1 results. Please, try to refine your search.",preferences.search_result_number));
+//                        }
+
+                        if ((sumResults == "none") && (countTmp.length==0)){
+                            alert(get_lang("None result was found."));
+                        }
+                        else
                         {
-                            EsearchE.mount_result((data=='none')?tmp:(countTmp.length==0)?data:data+tmp);
+                            EsearchE.mount_result((sumResults=='none')?tmp:(countTmp.length==0)?data:data+tmp);
                         }
-                        else if (data == 'many results' || countTmp.length >= preferences.search_result_number)
-                        {
-                            alert(get_lang("More than %1 results. Please, try to refine your search.",preferences.search_result_number));
-                        }
+
 		}
 		var args   = "$this.imap_functions.search_msg";
