Index: /trunk/expressoMail1_2/inc/class.imap_functions.inc.php
===================================================================
--- /trunk/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 650)
+++ /trunk/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 659)
@@ -1577,4 +1577,75 @@
 	}
 	
+	function messages_sort($sort_box_type,$sort_box_reverse, $search_box_type)
+	{
+		$sort = array();
+		$sort_uid = array();
+		
+		$num_msgs = imap_num_msg($this->mbox);
+		for ($i=1; $i<=$num_msgs; $i++)
+		{
+			$header = $this->get_header(imap_uid($this->mbox,$i));
+			// List UNSEEN messages.
+			if($search_box_type == "UNSEEN" &&  (!trim($header->Recent) && !trim($header->Unseen))){
+				continue;
+			}
+			// List SEEN messages.
+			elseif($search_box_type == "SEEN" && (trim($header->Recent) || trim($header->Unseen))){
+				continue;
+			}
+			// List ANSWERED messages.			
+			elseif($search_box_type == "ANSWERED" && !trim($header->Answered)){
+				continue;				
+			}
+			// List FLAGGED messages.			
+			elseif($search_box_type == "FLAGGED" && !trim($header->Flagged)){
+				continue;
+			}
+						
+			
+			if($sort_box_type=='SORTFROM') {
+				if (($header->from[0]->mailbox . "@" . $header->from[0]->host) == $_SESSION['phpgw_info']['expressomail']['user']['email'])				
+					$from = $header->to;
+				else
+					$from = $header->from;
+				
+				$tmp = imap_mime_header_decode($from[0]->personal);			
+				
+				if ($tmp[0]->text != "")
+					$sort[$i] = $tmp[0]->text;
+				else
+					$sort[$i] = $from[0]->mailbox . "@" . $from[0]->host;
+			}
+			else if($sort_box_type=='SORTSUBJECT') {
+				$sort[$i] = $header->subject;
+			}
+			else if($sort_box_type=='SORTSIZE') {
+				$sort[$i] = $header->Size;
+			}
+			else {
+				$sort[$i] = $header->udate;
+			}
+			
+		}
+		
+		natcasesort($sort);
+		
+		foreach($sort as $index => $header_msg)
+		{	
+			$sort_uid[] = imap_uid($this->mbox, $index);
+		}
+		
+		if ($sort_box_reverse)
+			$sort_uid = array_reverse($sort_uid);
+		
+		return $sort_uid;
+
+	}
+	
+	/**
+	 * Deprecated
+	 * 
+	 * Replaced for the method messages_sort
+	 */
 	function imap_sortfrom($sort_box_reverse, $search_box_type)
 	{
@@ -1987,4 +2058,5 @@
 			$flag_importance = false;
 			$msgs_number = explode(",",$msgs_to_set);
+			$unflagged_msgs = "";
 			foreach($msgs_number as $msg_number) {
 				preg_match('/importance *: *(.*)\r/i',
@@ -1993,14 +2065,24 @@
 				if(strtolower($importance[1])=="high") {
 					$flag_importance=true;
-					break;
-				}
-				
-			}
-
-			if(!$flag_importance)			
-			$return["status"] = imap_clearflag_full($this->mbox, $msgs_to_set, "\\Flagged", ST_UID);
-			else{
+				}
+				else {
+					$unflagged_msgs.=$msg_number.",";
+				}				
+			}
+
+			if($unflagged_msgs!="") {
+				imap_clearflag_full($this->mbox,substr($unflagged_msgs,0,strlen($unflagged_msgs)-1), "\\Flagged", ST_UID);
+				$return["msgs_unflageds"] = substr($unflagged_msgs,0,strlen($unflagged_msgs)-1);
+			}
+			else {
+				$return["msgs_unflageds"] = false;
+			}
+
+			if($flag_importance) {
 				$return["status"] = false;
 				$return["msg"] = $this->functions->getLang("At least one of selected message cant be marked as normal");
+			}
+			else {
+				$return["status"] = true;
 			}
 		}
@@ -2098,5 +2180,6 @@
 		}
 
-		switch($sort_box_type){
+		return $this->messages_sort($sort_box_type,$sort_box_reverse, $search_box_type);
+		/*switch($sort_box_type){
 			case 'SORTFROM':
 				return $this->imap_sortfrom($sort_box_reverse, $search_box_type);				
@@ -2107,5 +2190,5 @@
 			default:
 				return imap_sort($this->mbox, SORTARRIVAL, $sort_box_reverse, SE_UID, $search_box_type);						
-		}		
+		}*/
 	}	
 	
@@ -2732,4 +2815,12 @@
 		$pdate = date_parse($header->MailDate);
  		$header->udate +=  $pdate['zone']*(-60);
+		
+		if($header->Flagged!="F") {
+			$flag = preg_match('/importance *: *(.*)\r/i',
+						imap_fetchheader($this->mbox, imap_msgno($this->mbox, $msg_number))
+						,$importance);		
+			$header->Flagged = $flag==0?false:strtolower($importance[1])=="high"?"F":false;
+		}
+		
 		return $header;
 	}
Index: /trunk/expressoMail1_2/js/main.js
===================================================================
--- /trunk/expressoMail1_2/js/main.js	(revision 649)
+++ /trunk/expressoMail1_2/js/main.js	(revision 659)
@@ -1230,5 +1230,8 @@
 				remove_className(Element(msgs_to_set[i]), 'selected_msg');
 			}
-			return;
+			if(!data.msgs_unflageds)
+				return;
+			else
+				msgs_to_set = data.msgs_unflageds.split(",");
 		}
 		
Index: /trunk/expressoMail1_2/js/draw_api.js
===================================================================
--- /trunk/expressoMail1_2/js/draw_api.js	(revision 652)
+++ /trunk/expressoMail1_2/js/draw_api.js	(revision 659)
@@ -589,5 +589,5 @@
 		_img_important.src = "templates/default/images/important.gif";
 
-		if ((headers_msgs.Flagged == 'F') || (headers_msgs.Importance.indexOf("high")!=-1))
+		if ((headers_msgs.Flagged == 'F') || (headers_msgs.Importance.toLowerCase().indexOf("high")!=-1))
 			td_element22.innerHTML += "<img src ='templates/default/images/important.gif' title='"+get_lang('Important')+"'>";
 		else
