Index: /branches/2.2/expressoMail1_2/setup/phpgw_pt-br.lang
===================================================================
--- /branches/2.2/expressoMail1_2/setup/phpgw_pt-br.lang	(revision 3178)
+++ /branches/2.2/expressoMail1_2/setup/phpgw_pt-br.lang	(revision 3223)
@@ -300,6 +300,4 @@
 More	expressoMail1_2	pt-br	Mais
 More options	expressoMail1_2	pt-br	Mais opções
-More than %1 results. Please, try to refine your search.	expressoMail1_2	pt-br	Mais que %1 resultados foram encontrados. Tente refinar sua pesquisa.
-More than %1 results were found	expressoMail1_2	pt-br	Mais de %1 resultados foram encontrados
 Move	expressoMail1_2	pt-br	Mover
 Move to	expressoMail1_2	pt-br	Mover para
@@ -384,4 +382,5 @@
 Result of the search	expressoMail1_2	pt-br	Resultado da busca
 results found	expressoMail1_2	pt-br	resultados encontrados
+no results found	expressoMail1_2	pt-br	Nenhum resultado encontrado
 Return receipt	expressoMail1_2	pt-br	Conf. de Leitura
 REVOKED Certificate.	expressoMail1_2	pt-br	CERTIFICADO REVOGADO.
Index: /branches/2.2/expressoMail1_2/inc/class.imap_functions.inc.php
===================================================================
--- /branches/2.2/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 3192)
+++ /branches/2.2/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 3223)
@@ -727,6 +727,6 @@
 
 		$return = ""; 
-		if( (!isset($this->preferences['preview_msg_subject']) || ($this->preferences['preview_msg_subject'] != "1")) &&  
-			(!isset($this->preferences['preview_msg_tip']    ) || ($this->preferences['preview_msg_tip']     != "1")) ) 
+		if( (!isset($this->prefs['preview_msg_subject']) || ($this->prefs['preview_msg_subject'] != "1")) &&  
+			(!isset($this->prefs['preview_msg_tip']    ) || ($this->prefs['preview_msg_tip']     != "1")) ) 
 		{ 
 			$return['body'] = ""; 
@@ -3237,10 +3237,11 @@
 	function search_msg($params = ''){
             $mbox_stream = "";
-            if(strpos($params['condition'],"#")===false) { //local messages
+            if(strpos($params['condition'],"#")===false) { //local messages? It shouldn't be necessary, should it? FIXME
                     $search=false;
             }
             else {
                     $search = explode(",",$params['condition']);
-            }
+	    }
+	    $params['page'] = $params['page']*1;
 
 	    if(is_array($search)){
@@ -3311,5 +3312,5 @@
                             $search_criteria = imap_search($mbox_stream, $_filter, SE_UID);
 
-                            if(is_array($search_criteria)) //&& count($search_criteria) < 50)
+                            if(is_array($search_criteria))
                             {
                                 foreach($search_criteria as $new_search)
@@ -3319,20 +3320,6 @@
 				    $elem['uid'] = $new_search; 
 				    $retorno[] = $elem; 
-
-				    /* NOTE: This code was lost on 2.2 merge 
-                                    if ($search_result_number != '65536' && $sum == $search_result_number)
-                                    {
-                                        return $retorno ? $sum . "=sumResults=" . $retorno : "none";
-                                    }
-
-                                    $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 ++;
-				    }
-				     */
-                                }
-                            }
+				}
+			    }
                         }
                     }
@@ -3346,15 +3333,8 @@
 				    $elem['boxname'] = mb_convert_encoding( $name_box, "ISO_8859-1", "UTF7-IMAP" ); 
 				    $elem['uid'] = $new_search; 
-				    $retorno[] = $elem; 
-		                //  NOTE: This code was lost on 2.2 merge 
-				/*if ($search_result_number != '65536' && $sum == $search_result_number)
-                                {
-                                    return $retorno ? $sum . "=sumResults=" . $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++;*/
+				    $retorno[] = $elem;
                             }
                         }
-                    }
+		    }
                 }
             }
@@ -3362,9 +3342,31 @@
             {
                 imap_close($mbox_stream);
-            }
-
-            if ($retorno)
+	    }
+	    $num_msgs = count($retorno);
+
+	    /* Comparison functions, descendent is ascendent with parms inverted */
+	    function SORTDATE($a, $b) { return ($a['udate'] < $b['udate']); }
+	    function SORTDATE_REVERSE($b, $a) { return SORTDATE($a,$b); }
+
+	    function SORTWHO($a, $b) { return (strtoupper($a['from']) > strtoupper($b['from'])); }
+	    function SORTWHO_REVERSE($b, $a) { return SORTWHO($a,$b); }
+
+	    function SORTSUBJECT($a, $b) { return (strtoupper($a['subject']) > strtoupper($b['subject'])); }
+	    function SORTSUBJECT_REVERSE($b, $a) { return SORTSUBJECT($a,$b); }
+
+	    function SORTBOX($a, $b) { return ($a['boxname'] > $b['boxname']); }
+	    function SORTBOX_REVERSE($b, $a) { return SORTBOX($a,$b); }
+
+	    function SORTSIZE($a, $b) { return ($a['size'] > $b['size']); }
+	    function SORTSIZE_REVERSE($b, $a) { return SORTSIZE($a,$b); }
+
+	    usort($retorno, $params['sort_type']);
+	    $pageret = array_slice($retorno, $params['page'] * $this->prefs['max_email_per_page'], $this->prefs['max_email_per_page']);
+	    $arrayRetorno['num_msgs'] =  $num_msgs;
+	    $arrayRetorno['data'] =  $pageret;
+
+            if ($pageret)
             {
-                return $retorno;
+                return $arrayRetorno;
             }
             else
Index: /branches/2.2/expressoMail1_2/js/common_functions.js
===================================================================
--- /branches/2.2/expressoMail1_2/js/common_functions.js	(revision 3178)
+++ /branches/2.2/expressoMail1_2/js/common_functions.js	(revision 3223)
@@ -186,9 +186,8 @@
 
 function show_hide_span_paging(ID){
-	if ((ID != "0") && Element("span_paging")) 
-		Element("span_paging").style.display = 'none';
-	else
-		if (Element("span_paging"))
-			Element("span_paging").style.display = '';
+	if (Element("span_paging"+ID))
+		Element("span_paging"+ID).style.display = '';
+	if (Element("span_paging"+currentTab))
+		Element("span_paging"+currentTab).style.display = 'none';
 }
 
Index: /branches/2.2/expressoMail1_2/js/search.js
===================================================================
--- /branches/2.2/expressoMail1_2/js/search.js	(revision 3074)
+++ /branches/2.2/expressoMail1_2/js/search.js	(revision 3223)
@@ -5,4 +5,7 @@
 	{
 		this.searchW   = new Array;
+		this.condition = "";
+		this.sort_type = "";
+		this.page = 0;
 		this.name_box_search = "";
 		this.all_boxes = new Array;
@@ -203,61 +206,78 @@
 		ttree.make_tree(folders,"div_folders_search","_folders_tree_search","","","","");
 	}
-
+	function openpage(data){
+		delete_border(currentTab,false);
+		EsearchE.mount_result(data);
+	}
+
+	searchE.prototype.show_paging = function(size){
+		if (size > preferences.max_email_per_page)
+		{
+			if (Element("span_paging"+currentTab)){
+				Element("span_paging"+currentTab).style.display = '';
+				Element("span_paging"+currentTab).innerHTML = "";
+			}
+			else{
+				var span_paging = document.createElement('SPAN');
+				span_paging.id = "span_paging"+currentTab;
+				Element("div_menu_c3").appendChild(span_paging);
+			}
+			if(this.page > 1){
+				var fLink = document.createElement('A');
+				fLink.innerHTML = '&lt; &lt;';
+				fLink.href = 'javascript:EsearchE.page=0;'+
+					'cExecute("$this.imap_functions.search_msg",openpage,'+
+					'"condition='+this.condition+'&sort_type='+this.sort_type+'&page=0");';
+				Element("span_paging"+currentTab).appendChild(fLink);
+			}
+			if(this.page > 2){
+				var prevLink = document.createElement('A');
+				prevLink.innerHTML = '...';
+				prevLink.href = 'javascript:EsearchE.page='+
+					(this.page-1)+';'+
+					'cExecute("$this.imap_functions.search_msg",openpage,'+
+					'"condition='+this.condition+'&sort_type='+this.sort_type+'&page='+(this.page-1)+'");';
+				Element("span_paging"+currentTab).appendChild(prevLink);
+			}
+			for (var i=(this.page > 2 ? this.page-2 : 0); i <= parseInt(this.page)+4; i+= 1){
+				if (i*preferences.max_email_per_page > size)
+					break;
+				if (this.page == i)
+					Element("span_paging"+currentTab).innerHTML += "<b> "+(this.page+1)+" </b>";
+				else
+				{
+					var pageLink = document.createElement('A');
+					pageLink.innerHTML = (i+1)+" ";
+					pageLink.href = 'javascript:EsearchE.page='+i+';'+
+						'cExecute("$this.imap_functions.search_msg",openpage,'+
+						'"condition='+this.condition+'&sort_type='+this.sort_type+'&page='+this.page+'");';
+					Element("span_paging"+currentTab).appendChild(pageLink);
+				}
+			}
+			if (size > preferences.max_email_per_page)
+			{
+				var lLink = document.createElement('A');
+				lLink.innerHTML = '&gt; &gt;';
+				lLink.href = 'javascript:EsearchE.page='+parseInt(size/preferences.max_email_per_page)+';'+
+					'cExecute("$this.imap_functions.search_msg",openpage,'+
+					'"condition='+this.condition+'&sort_type='+this.sort_type+'&page=0");';
+				Element("span_paging"+currentTab).appendChild(lLink);
+			}
+		}
+
+	}
 	// Form resultado
-	searchE.prototype.mount_result = function(data,sort_type){
+	searchE.prototype.mount_result = function(Rdata,sort_type){
+		var data = Rdata['data'];
 		if (data == undefined)
 			return;
+		if(this.page == 0)
+			write_msg(Rdata['num_msgs']+" "+get_lang("results found"));
 		var cont = parseInt(0);
 
-                //This func returns the comparison between criterias 
-                var date_comp = function(a,b){ return a.udate > b.udate;}; 
-                var date_comp_reverse = function(a,b){ return date_comp(b,a);}; 
-                var box_comp = function(a,b){   return a.boxname > b.boxname; } 
-                var box_comp_reverse = function(a,b){   return box_comp(b,a); } 
-                var who_comp = function(a,b){ return a.from > b.from; } 
-                var who_comp_reverse = function(a,b){ return who_comp(b,a); } 
-                var subject_comp = function(a,b){ return a.subject > b.subject; } 
-                var subject_comp_reverse = function(a,b){ return subject_comp(b,a); } 
-                var size_comp = function(a,b){ return a.size > b.size; } 
-                var size_comp_reverse = function(a,b){ return size_comp(b,a); }
-
-		switch (sort_type)
-		{
-			case 'SORTDATE':
-				data.sort(date_comp);
-				break;
-			case 'SORTDATE_REVERSE':
-				data.sort(date_comp_reverse);
-				break;
-			case 'SORTBOX':
-				data.sort(box_comp);
-				break;
-			case 'SORTBOX_REVERSE':
-                                data.sort(box_comp_reverse);
-                                break;
-			case 'SORTSUBJECT':
-				data.sort(subject_comp);
-				break;
-			case 'SORTSUBJECT_REVERSE':
-                                data.sort(subject_comp_reverse);
-				break;
-			case 'SORTWHO':
-				data.sort(who_comp);
-				break;
-			case 'SORTWHO_REVERSE':
-                                data.sort(who_comp_reverse);
-				break;
-			case 'SORTSIZE':
-				data.sort(size_comp);
-				break;
-			case 'SORTSIZE_REVERSE':
-				data.sort(size_comp_reverse);
-				break;
-			default:
-				var showMsg = 1;
-		}
-
-		if(data.length > 0 && showMsg) 
-	                        write_msg(data.length + " "+ get_lang('results found')); 
+		if (typeof(sort_type) != 'undefined')
+			this.sort_type = sort_type;
+		else
+			sort_type = this.sort_type;
 
 		numBox++;
@@ -270,4 +290,5 @@
                 openTab.content_id[currentTab] = Element('content_id_search_' + numBox);
 		openTab.type[currentTab] = 1;
+
 		var table = document.createElement("TABLE");
 		table.id    = "table_resultsearch_" + numBox;
@@ -280,9 +301,8 @@
 		var tbody = document.createElement("TBODY");
 		tbody.id    = "tbody_box_" + numBox;
-
 		for( var i=0; i < data.length; i++){
 			var tr = document.createElement("TR");
 			tr.style.height = preferences.line_height;
-			var aux = data[i]; 
+			var aux = data[i];
 			var mailbox = aux.boxname; 
 			var uid_msg = aux.uid; 
@@ -470,10 +490,20 @@
 		if (sort_type == 'SORTBOX')
 		{
-			td_element1.onclick = function () { EsearchE.mount_result(data,'SORTBOX_REVERSE'); delete_border(border_id,false);};
+			td_element1.onclick = function () {
+				var handler = function(data){ delete_border(border_id,false); EsearchE.mount_result(data,'SORTBOX_REVERSE'); };
+				var args   = "$this.imap_functions.search_msg";
+				var params = "condition="+EsearchE.condition+"&page="+EsearchE.page+"&sort_type=SORTBOX_REVERSE";
+				cExecute(args,handler,params);
+			};
 			td_element1.innerHTML ="<b>"+get_lang("Folder")+"</b><img src='templates/"+template+"/images/arrow_descendant.gif'>";
 		}
 		else
 		{
-			td_element1.onclick = function () { EsearchE.mount_result(data,'SORTBOX'); delete_border(border_id,false);};
+			td_element1.onclick = function () {
+				var handler = function(data){ delete_border(border_id,false); EsearchE.mount_result(data,'SORTBOX'); };
+				var args   = "$this.imap_functions.search_msg";
+				var params = "condition="+EsearchE.condition+"&page="+EsearchE.page+"&sort_type=SORTBOX";
+				cExecute(args,handler,params);
+			};
 			if  (sort_type == 'SORTBOX_REVERSE')
 				td_element1.innerHTML ="<b>"+get_lang("Folder")+"</b><img src='templates/"+template+"/images/arrow_ascendant.gif'>";
@@ -486,10 +516,20 @@
 		if (sort_type == 'SORTWHO')
 		{
-			td_element2.onclick = function () { EsearchE.mount_result(data,'SORTWHO_REVERSE'); delete_border(border_id,false);};
+			td_element2.onclick = function () {
+				var handler = function(data){ delete_border(border_id,false); EsearchE.mount_result(data,'SORTWHO_REVERSE'); };
+				var args   = "$this.imap_functions.search_msg";
+				var params = "condition="+EsearchE.condition+"&page="+EsearchE.page+"&sort_type=SORTWHO_REVERSE";
+				cExecute(args,handler,params);
+			};
 			td_element2.innerHTML ="<b>"+get_lang("who")+"</b><img src='templates/"+template+"/images/arrow_descendant.gif'>";
 		}
 		else
 		{
-			td_element2.onclick = function () { EsearchE.mount_result(data,'SORTWHO'); delete_border(border_id,false);};
+			td_element2.onclick = function () {
+				var handler = function(data){ delete_border(border_id,false); EsearchE.mount_result(data,'SORTWHO'); };
+				var args   = "$this.imap_functions.search_msg";
+				var params = "condition="+EsearchE.condition+"&page="+EsearchE.page+"&sort_type=SORTWHO";
+				cExecute(args,handler,params);
+			};
 			if  (sort_type == 'SORTWHO_REVERSE')
 				td_element2.innerHTML ="<b>"+get_lang("who")+"</b><img src='templates/"+template+"/images/arrow_ascendant.gif'>";
@@ -502,10 +542,20 @@
 		if (sort_type == 'SORTSUBJECT')
 		{
-			td_element3.onclick = function () { EsearchE.mount_result(data,'SORTSUBJECT_REVERSE'); delete_border(border_id,false);};
+			td_element3.onclick = function () {
+				var handler = function(data){ delete_border(border_id,false); EsearchE.mount_result(data,'SORTSUBJECT_REVERSE'); };
+				var args   = "$this.imap_functions.search_msg";
+				var params = "condition="+EsearchE.condition+"&page="+EsearchE.page+"&sort_type=SORTSUBJECT_REVERSE";
+				cExecute(args,handler,params);
+			};
 			td_element3.innerHTML ="<b>"+get_lang("subject")+"</b><img src='templates/"+template+"/images/arrow_descendant.gif'>";
 		}
 		else
 		{
-			td_element3.onclick = function () { EsearchE.mount_result(data,'SORTSUBJECT'); delete_border(border_id,false);};
+			td_element3.onclick = function () {
+				var handler = function(data){ delete_border(border_id,false); EsearchE.mount_result(data,'SORTSUBJECT'); };
+				var args   = "$this.imap_functions.search_msg";
+				var params = "condition="+EsearchE.condition+"&page="+EsearchE.page+"&sort_type=SORTSUBJECT";
+				cExecute(args,handler,params);
+			};
 			if  (sort_type == 'SORTSUBJECT_REVERSE')
 				td_element3.innerHTML ="<b>"+get_lang("subject")+"</b><img src='templates/"+template+"/images/arrow_ascendant.gif'>";
@@ -518,10 +568,20 @@
 		if (sort_type == 'SORTDATE')
 		{
-			td_element4.onclick = function () { EsearchE.mount_result(data,'SORTDATE_REVERSE'); delete_border(border_id,false);};
+			td_element4.onclick = function () {
+				var handler = function(data){ delete_border(border_id,false); EsearchE.mount_result(data,'SORTDATE_REVERSE'); };
+				var args   = "$this.imap_functions.search_msg";
+				var params = "condition="+EsearchE.condition+"&page="+EsearchE.page+"&sort_type=SORTDATE_REVERSE";
+				cExecute(args,handler,params);
+			};
 			td_element4.innerHTML ="<b>"+get_lang("Date")+"</b><img src='templates/"+template+"/images/arrow_descendant.gif'>";
 		}
 		else
 		{
-			td_element4.onclick = function () { EsearchE.mount_result(data,'SORTDATE'); delete_border(border_id,false);};
+			td_element4.onclick = function () {
+				var handler = function(data){ delete_border(border_id,false); EsearchE.mount_result(data,'SORTDATE'); };
+				var args   = "$this.imap_functions.search_msg";
+				var params = "condition="+EsearchE.condition+"&page="+EsearchE.page+"&sort_type=SORTDATE";
+				cExecute(args,handler,params);
+			};
 			if  (sort_type == 'SORTDATE_REVERSE')
 				td_element4.innerHTML ="<b>"+get_lang("Date")+"</b><img src='templates/"+template+"/images/arrow_ascendant.gif'>";
@@ -534,10 +594,20 @@
 		if (sort_type == 'SORTSIZE')
 		{
-			td_element5.onclick = function () { EsearchE.mount_result(data,'SORTSIZE_REVERSE'); delete_border(border_id,false);};
+			td_element5.onclick = function () {
+				var handler = function(data){ delete_border(border_id,false); EsearchE.mount_result(data,'SORTSIZE_REVERSE'); };
+				var args   = "$this.imap_functions.search_msg";
+				var params = "condition="+EsearchE.condition+"&page="+EsearchE.page+"&sort_type=SORTSIZE_REVERSE";
+				cExecute(args,handler,params);
+			};
 			td_element5.innerHTML ="<b>"+get_lang("size")+"</b><img src='templates/"+template+"/images/arrow_descendant.gif'>";
 		}
 		else
 		{
-			td_element5.onclick = function () { EsearchE.mount_result(data,'SORTSIZE'); delete_border(border_id,false);};
+			td_element5.onclick = function () {
+				var handler = function(data){ delete_border(border_id,false); EsearchE.mount_result(data,'SORTSIZE'); };
+				var args   = "$this.imap_functions.search_msg";
+				var params = "condition="+EsearchE.condition+"&page="+EsearchE.page+"&sort_type=SORTSIZE";
+				cExecute(args,handler,params);
+			};
 			if  (sort_type == 'SORTSIZE_REVERSE')
 				td_element5.innerHTML ="<b>"+get_lang("size")+"</b><img src='templates/"+template+"/images/arrow_ascendant.gif'>";
@@ -576,5 +646,7 @@
 			content_search.appendChild(div_scroll_result);
 		}
-		resizeWindow(); 
+		resizeWindow();
+		EsearchE.show_paging(Rdata['num_msgs']);
+
 	}
 
@@ -788,20 +860,16 @@
 		}
 		var handler = function(data){
-			var tmp;
-
-			if (local_folders.length > 0) {
-				tmp = expresso_local_messages.search(local_folders, fields);
-				if (tmp!=false) {
-					var tmp2 = tmp.substr(2, data.length - 4);
-	                                } 
-	                        } 
-	                        if((data == "none") || (data.length==0)){alert(get_lang("None result was found."));} 
-	                        else if(data.length > preferences.search_result_number){alert(get_lang("More than %1 results. Please, try to refine your search.",1000));}
-			else if(data == "many results"){alert(get_lang("More than %1 results. Please, try to refine your search.",preferences.search_result_number));} 
-			else{EsearchE.mount_result(data);}
+			if (data == "none")
+	                        alert(get_lang('no results found'));
+			else
+				EsearchE.mount_result(data,'SORTDATE');
 
 		}
 		var args   = "$this.imap_functions.search_msg";
-		var params = "condition=" + nm_box;
+		this.condition = nm_box;
+		var params = "condition=" + nm_box+
+			"&page=0"+
+			"&sort_type=SORTDATE";
+		this.page = 0;
 		if(expresso_offline)
 			handler('none');
Index: /branches/2.2/expressoMail1_2/js/draw_api.js
===================================================================
--- /branches/2.2/expressoMail1_2/js/draw_api.js	(revision 3220)
+++ /branches/2.2/expressoMail1_2/js/draw_api.js	(revision 3223)
@@ -521,13 +521,13 @@
 
 	if(total_pages == 1) {
-		if(span_paging = document.getElementById("span_paging")) {
+		if(span_paging = document.getElementById("span_paging0")) {
 			span_paging.parentNode.removeChild(span_paging);
 		}
 		return;
 	}
-  	span_paging = document.getElementById("span_paging");
+  	span_paging = document.getElementById("span_paging0");
 	if(!span_paging){
 		span_paging = document.createElement("DIV");
-		span_paging.id = "span_paging";
+		span_paging.id = "span_paging0";
 		span_paging.className = "boxHeaderText";
 		span_paging.align="right";
