Index: /sandbox/2.5.1-expresso1/prototype/modules/attach_message/attach_message.js
===================================================================
--- /sandbox/2.5.1-expresso1/prototype/modules/attach_message/attach_message.js	(revision 7912)
+++ /sandbox/2.5.1-expresso1/prototype/modules/attach_message/attach_message.js	(revision 7920)
@@ -458,5 +458,46 @@
 			],
 			jsonReader : {
-				  root:"rows",
+				  root: function(obj){
+				  	obj['data'] = {};
+		            obj.data = {rows : obj.rows};
+				  	if(obj.rows){
+				  		if(!obj.rows[0].flag){
+					  		var msgs = [];
+						  	$.each(obj.rows, function(i, msg){
+								flag = msg['flags'];
+		                        id = msg['msg_number'];
+		                        var message = {};
+		                     
+		                        message['msg_number'] = id;
+		                        message['flags'] = flag;
+		                        message['to'] = {
+		                                name: msg['toaddress2'],
+		                                email: msg['toaddress2']
+		                        };
+		
+		                        if(msg['from'] != undefined &&  msg['header']['from'] != undefined){
+		                                message['from'] = {
+		                                        'email' : msg['from']['email'],
+		                                        'name' : msg['header']['from']['name']
+		                                }
+		                            }else{
+		                                message['from'] = {
+		                                        'email' : '',
+		                                        'name' : 'Rascunho'
+		                                }
+		                            }
+		                        //message['from']['name'] = msg['from']['name'];
+		                        message['subject'] = msg['subject'] ? msg['subject'] : "(sem assunto)";
+		                        message['timestamp'] = parseInt(msg['timestamp']);
+		                        message['size'] = msg['size'];
+		                        message['id'] = id;
+		                        msgs.push( message );
+			                });
+							obj['data'] = {};
+			                obj.data = {rows : msgs};
+			            }
+	                }
+				  	return obj.data.rows;
+				  },
 				  page: "page",
 				  total: "total",
@@ -474,5 +515,5 @@
 			multiselect: true,
 			autowidth: true,
-			loadComplete: function(data) {
+			loadComplete: function(data) {			
 				lastLoadedMessages = data.rows;
 				
@@ -797,43 +838,22 @@
 			
             var data = DataLayer.get( 'message', { filter: filters, criteria: { properties: { context: { folder: folder } } } }, true );
-            var tmp = [DataLayer.get( 'message', {filter: [ "msgNumber", "IN", data ], criteria: { properties: { context: { folder: folder } } } })];
-
-            msgs = [];
-
-            if(tmp[0].rows){
-            	$.each(tmp[0].rows, function(i, msg){	            	
-	                flag = msg['flags'];
-	                id = msg['msg_number'];
-	                var message = {};
-	              
-	                message['msg_number'] = id;
-	                message['flags'] = flag;
-	                message['to'] = {
-	                	name: msg['toaddress2'],
-	                	email: msg['toaddress2']
-	                };
-
-	                if(msg['from'] != undefined &&  msg['header']['from'] != undefined){
-		                message['from'] = {
-		                	'email' : msg['from']['email'],
-		                	'name' : msg['header']['from']['name']
-		                }
-		            }else{
-		            	message['from'] = {
-		                	'email' : '',
-		                	'name' : 'Rascunho'
-		                }
-		            }
-	                //message['from']['name'] = msg['from']['name'];
-	                message['subject'] = msg['subject'] ? msg['subject'] : "(sem assunto)";
-	                message['timestamp'] = parseInt(msg['timestamp']);
-	                message['size'] = msg['size'];
-	                message['id'] = id;
-	                msgs.push( message );
-           		});
+
+            if(DataLayer.criterias['message:jqGridSearch']){
+				delete DataLayer.criterias['message:jqGridSearch'];	
+			}
+
+        	DataLayer.register( 'criterias', 'message:jqGridSearch', function( crit ){
+			    crit.properties = { context: { folder: folder } };
+			    console.log(crit);
+			    return { filter: [ "msgNumber", "IN", data ], criteria: crit };
+			});
+			
+            if(typeof(data) == 'object'){
+				grid.jqGrid("clearGridData", true);
+				grid.jqGrid('setGridParam',{datatype: "json", url: 'message:jqGridSearch'}).trigger("reloadGrid");
+
             }else{
             	grid.jqGrid("clearGridData", true);
             }
-            return msgs;
         }
 
@@ -852,10 +872,10 @@
             } else {
                 // Pesquisa pelo Imap...
-                msgs = search_imap_messages(param, folder);
+                return search_imap_messages(param, folder);
             }
 
             // Monta as mensagens na grid...
             grid.jqGrid("clearGridData", true);
-            grid.jqGrid('setGridParam',{datatype: "local",data: msgs})
+            grid.jqGrid('setGridParam',{datatype: "local",data: msgs,mtype: 'POST'})
             .trigger("reloadGrid");
             //.jqGrid('setCaption', '<span class="'+child.attr('class')+'">'+child.attr('title')+'</span>');
@@ -872,4 +892,5 @@
 			if($.ui.keyCode.ENTER == e.keyCode){
 				search_messages($(this).val());
+				$(this).parents(".attach-message-search-div:first").find(".attach-message-search-input").val("");
 			}
 		});
@@ -889,4 +910,5 @@
 		$("#attach-message-search").click(function(){
 			search_messages($(this).parents(".attach-message-search-div:first").find(".attach-message-search-input").val());
+			$(this).parents(".attach-message-search-div:first").find(".attach-message-search-input").val("");
 		});
 	}
