Index: /branches/2.2/mobile/inc/class.common_functions.inc.php
===================================================================
--- /branches/2.2/mobile/inc/class.common_functions.inc.php	(revision 3609)
+++ /branches/2.2/mobile/inc/class.common_functions.inc.php	(revision 3923)
@@ -1,98 +1,98 @@
 <?php
-	/**************************************************************************\
-	* eGroupWare                                                               *
-	* http://www.egroupware.org                                                *
-	* The file written by Mário César Kolling <mario.kolling@serpro.gov.br>    *
-	* --------------------------------------------                             *
-	*  This program is free software; you can redistribute it and/or modify it *
-	*  under the terms of the GNU General Public License as published by the   *
-	*  Free Software Foundation; either version 2 of the License, or (at your  *
-	*  option) any later version.                                              *
-	\**************************************************************************/
+/**************************************************************************\
+* eGroupWare                                                               *
+* http://www.egroupware.org                                                *
+* The file written by Mário César Kolling <mario.kolling@serpro.gov.br>    *
+* --------------------------------------------                             *
+*  This program is free software; you can redistribute it and/or modify it *
+*  under the terms of the GNU General Public License as published by the   *
+*  Free Software Foundation; either version 2 of the License, or (at your  *
+*  option) any later version.                                              *
+\**************************************************************************/
 
-	class common_functions
+class common_functions
+{
+	function borkb($size, $enclosed = NULL) 
 	{
-	    function borkb($size, $enclosed = NULL) 
-	    {
-			if (!$size)
+		if (!$size)
 			$size = 0;
-	
-			if ($enclosed)
-			{
-				$left = '(';
-				$right = ')';
+
+		if ($enclosed)
+		{
+			$left = '(';
+			$right = ')';
+		}
+
+		if ($size < 1024)
+			$rstring = $left . $size . ' B' . $right;
+		else if ($size < 1048576)
+			$rstring = $left . round($size/1024) . ' KB' . $right;
+		else if ($size < 1073741824)
+			$rstring = $left . round($size/1024/1024) . ' MB' . $right;
+		else
+			$rstring = $left . round($size/1024/1024/1024) . ' GB' . $right;
+
+		return $rstring;
+	}
+
+	function complete_string($str = "", $length = 10, $align = "R", $char = " ") {
+		if( $str == null )
+			$str = "";
+		else 
+		if( strlen($str) > $length ) {
+			return substr($str, 0, $length);
+		} else if( strlen($str) == $length ) {
+			return $str;
+		}			
+
+		$char = substr($char, 0, 1);
+		$complete_str = "";
+
+		while( strlen($str) + strlen($complete_str) < $length  )
+			$complete_str .= $char;
+
+		if( $align == "L" )
+			return $str . $complete_str;
+		else
+			return $complete_str . $str;
+	}		
+
+	function strach_string($string,$size) {
+		return strlen($string)>$size ? substr($string,0,$size)."...":
+		$string;
+	}
+
+	/**
+	* Fixes the odd indexing of multiple file uploads from the format:
+	*
+	* $_FILES['field']['key']['index']
+	*
+	* To the more standard and appropriate:
+	*
+	* $_FILES['field']['index']['key']
+	*
+	* @param array $files
+	* @author Corey Ballou
+	* @link http://www.jqueryin.com
+	*/
+	function fixFilesArray(&$files)
+	{
+		$names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1);
+
+		foreach ($files as $key => $part) {
+			// only deal with valid keys and multiple files
+			$key = (string) $key;
+			if (isset($names[$key]) && is_array($part)) {
+				foreach ($part as $position => $value) {
+					$files[$position][$key] = $value;
+				}
+				// remove old key reference
+				unset($files[$key]);
 			}
-	
-			if ($size < 1024)
-				$rstring = $left . $size . ' B' . $right;
-			else if ($size < 1048576)
-				$rstring = $left . round($size/1024) . ' KB' . $right;
-			else if ($size < 1073741824)
-				$rstring = $left . round($size/1024/1024) . ' MB' . $right;
-			else
-				$rstring = $left . round($size/1024/1024/1024) . ' GB' . $right;
-	
-			return $rstring;
 		}
-		
-		function complete_string($str = "", $length = 10, $align = "R", $char = " ") {
-			if( $str == null )
-				$str = "";
-			else 
-				if( strlen($str) > $length ) {
-					return substr($str, 0, $length);
-				} else if( strlen($str) == $length ) {
-					return $str;
-				}			
-	
-			$char = substr($char, 0, 1);
-			$complete_str = "";
-	
-	        while( strlen($str) + strlen($complete_str) < $length  )
-	            $complete_str .= $char;
-	
-	        if( $align == "L" )
-	        	return $str . $complete_str;
-	        else
-	        	return $complete_str . $str;
-		}		
-		
-		function strach_string($string,$size) {
-			return strlen($string)>$size ? substr($string,0,$size)."...":
-										$string;
-		}
-		
-		/**
-		 * Fixes the odd indexing of multiple file uploads from the format:
-		 *
-		 * $_FILES['field']['key']['index']
-		 *
-		 * To the more standard and appropriate:
-		 *
-		 * $_FILES['field']['index']['key']
-		 *
-		 * @param array $files
-		 * @author Corey Ballou
-		 * @link http://www.jqueryin.com
-		 */
-		function fixFilesArray(&$files)
-		{
-		    $names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1);
-		
-		    foreach ($files as $key => $part) {
-		        // only deal with valid keys and multiple files
-		        $key = (string) $key;
-		        if (isset($names[$key]) && is_array($part)) {
-		            foreach ($part as $position => $value) {
-		                $files[$position][$key] = $value;
-		            }
-		            // remove old key reference
-		            unset($files[$key]);
-		        }
-		    }
-		}
-		
-	} //end common class
-	
+	}
+
+} //end common class
+
 
 ?>
Index: /branches/2.2/mobile/inc/class.ui_home.inc.php
===================================================================
--- /branches/2.2/mobile/inc/class.ui_home.inc.php	(revision 3829)
+++ /branches/2.2/mobile/inc/class.ui_home.inc.php	(revision 3923)
@@ -232,11 +232,11 @@
 					$p->set_var('next_max_msgs',$mail_params['max_msgs']+10);
 					$p->set_var('max_msgs',$mail_params['max_msgs']);
-
+					
 					$messages = $imap_functions->mobile_search($mail_params);
-					if($mail_params['max_msgs']>=$messages["total_msgs"])
+					if($messages["has_more_msg"])
+						$p->set_var('show_more_messages',"block");
+					else
 						$p->set_var('show_more_messages',"none");
-					else
-						$p->set_var('show_more_messages',"block");
-					$p->set_var('mails',$ui_mobilemail->print_mails_list($messages));
+					$p->set_var('mails',$ui_mobilemail->print_mails_list($messages['msgs']));
 				}
 				else {
Index: /branches/2.2/mobile/inc/class.ui_mobilemail.inc.php
===================================================================
--- /branches/2.2/mobile/inc/class.ui_mobilemail.inc.php	(revision 3907)
+++ /branches/2.2/mobile/inc/class.ui_mobilemail.inc.php	(revision 3923)
@@ -502,5 +502,5 @@
 				foreach($messages as $id => $message) {
 					
-					if(($id==='num_msgs') ||($id==='total_msgs'))
+					if(($id==='num_msgs') ||($id==='total_msgs') || ($id==='has_more_msg'))
 						continue;
 					if($message['from']['name'])
Index: /branches/2.2/expressoMail1_2/inc/class.imap_functions.inc.php
===================================================================
--- /branches/2.2/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 3922)
+++ /branches/2.2/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 3923)
@@ -210,7 +210,9 @@
 		$head_array['msg_number'] = $msg_number;
 		$head_array['udate'] = $header->udate;
-                $head_array['offsetToGMT'] = $this->functions->CalculateDateOffset();
-
-                $msgTimestamp = $header->udate + $head_array['offsetToGMT'];
+		$head_array['offsetToGMT'] = $this->functions->CalculateDateOffset();
+
+		$msgTimestamp = $header->udate + $head_array['offsetToGMT'];
+		$head_array['timestamp'] = $msgTimestamp;
+		
 		$date_msg = gmdate("d/m/Y",$msgTimestamp);
 //		if (date("d/m/Y") == $date_msg)
@@ -3289,6 +3291,6 @@
 		return $return;
 	}
-	
-	
+
+
 	function mobile_search($params)
 	{
@@ -3306,15 +3308,18 @@
 			$folders = array(0=>array('folder_id'=>$params['folder']));
 		$num_msgs = 0;
-		$max_msgs = $params['max_msgs'];
-		$return["total_msgs"] = 0;
+		$max_msgs = $params['max_msgs'] + 1; //get one more because mobile paginate
+		$return["msgs"] = array();
+		
+		//get max_msgs of each folder order by date and later order all messages together and retur only max_msgs msgs
 		foreach($folders as $id =>$folder)
 		{
 			if(strpos($folder['folder_id'],'user')===false && is_array($folder)) {
 				foreach($criterias as $criteria_fixed)
-	            {
-	            	$_filter = $criteria_fixed . ' "'.$params['filter'].'"';
+				{
+					$_filter = $criteria_fixed . ' "'.$params['filter'].'"';
+
 					$mbox_stream = $this->open_mbox($folder['folder_id']);
-	
-					$messages = imap_search($mbox_stream, $_filter, SE_UID);
+
+					$messages = imap_sort($mbox_stream,SORTARRIVAL,1,SE_UID,$_filter);
 					
 					if ($messages == ''){
@@ -3323,26 +3328,31 @@
 						continue;	
 					}
-									
+					
 					foreach($messages as $msg_number)
-					{	
-						if($num_msgs>=$max_msgs)
-							break;				
+					{
 						$temp = $this->get_info_head_msg($msg_number);
 						if(!$temp)
 							return false;
 						$temp['msg_folder'] = $folder['folder_id'];
-						$return[$num_msgs] = $temp;
+						$return["msgs"][$num_msgs] = $temp;
 						$num_msgs++;
-						
 					}
-					$return['num_msgs'] = $num_msgs;
-					$return["total_msgs"]+=count($messages);
+
 					if($mbox_stream)
 						imap_close($mbox_stream);
-					if($num_msgs>=$max_msgs)
-							continue;
-				}
-			}			
-		}
+				}
+			}
+		}
+
+		if(!function_exists("cmp_date")) {
+			function cmp_date($obj1, $obj2){
+		    if($obj1['timestamp'] == $obj2['timestamp']) return 0;
+		    return ($obj1['timestamp'] < $obj2['timestamp']) ? 1 : -1;
+			}
+		}
+		usort($return["msgs"], "cmp_date");
+		$return["has_more_msg"] = (sizeof($return["msgs"]) > $max_msgs);
+		$return["msgs"] = array_slice($return["msgs"], 0, $max_msgs);
+		
 		return $return;
 	}
