Index: trunk/expressoMail1_2/js/search.js
===================================================================
--- trunk/expressoMail1_2/js/search.js	(revision 4993)
+++ trunk/expressoMail1_2/js/search.js	(revision 5134)
@@ -16,5 +16,4 @@
 		this.txtfields 		= new Array("txt_ass","txt_de","txt_body","txt_para","txt_cc","txt_cco", "since_date", "before_date", "on_date");
 		this.selectFields	= new Array("flagged", "seen", "answered", "recent");
-                this.localResults   = 0;
 	}
 
@@ -227,5 +226,5 @@
 
     	// Gears - local
-		if (preferences.use_local_messages == 1 && EsearchE.localResults != 0)
+		if ( preferences.use_local_messages == 1 )
 		{
 			temp = expresso_local_messages.list_local_folders();
@@ -234,14 +233,12 @@
 				local_folders.push(temp[x][0]);
 			}
+ 		}
+		
                     if ( local_folders.length > 0 )
 			_gears = expresso_local_messages.search( local_folders, expresso_local_messages.getFilter() );
-                        _data['gears_num_msgs']	= _gears.length;
-		}else{
-                    _data['gears_num_msgs']	= 0;
-                }
 
 		_data['data'] 			= data['data'];
 		_data['num_msgs']		= data['num_msgs'];
-		
+		_data['gears_num_msgs']	= _gears.length;
 
 		delete_border( data['currentTab'], false);
@@ -263,5 +260,5 @@
  
 		if(size > parseInt(preferences.max_email_per_page)) { 
-			this.totalPages = parseInt(size/preferences.max_email_per_page); 
+			this.totalPages = Math.ceil(size/preferences.max_email_per_page); 
 			if((size/preferences.max_email_per_page) > this.totalPages) 
 			this.totalPages++; 
@@ -270,6 +267,10 @@
 		if(this.page != 0) { 
 			_link = document.createElement("A"); 
+		if( this.value )
+		  _link.href  = 'javascript:EsearchE.quickSearchMail( false, '+0+', false, "'+currentTab+'" )'; 
+		else{
             _link.href  = 'javascript:EsearchE.page=0;'; 
             _link.href += 'cExecute("$this.imap_functions.search_msg",openpage,"condition='+this.condition+'&sort_type='+this.sort_type+'&page=0&current_tab='+currentTab+'");'; 
+        } 
         } 
         else { 
@@ -319,6 +320,10 @@
 				var _link = document.createElement('A'); 
 				_link.innerHTML = ( _page + 1 ) + "&nbsp;&nbsp;"; 
+				if( this.value )
+				_link.href = 'javascript: EsearchE.quickSearchMail( false, '+i+', false, "'+currentTab+'" )';
+				else{
 				_link.href  = 'javascript:EsearchE.page='+i+';'; 
 				_link.href += 'cExecute("$this.imap_functions.search_msg",openpage,"condition='+this.condition+'&sort_type='+this.sort_type+'&page='+_page+'&current_tab='+currentTab+'");'; 
+			} 
 			} 
 			_link.innerHTML = "&nbsp;...&nbsp;"; 
@@ -339,6 +344,10 @@
 			if(this.page != (this.totalPages - 1)) { 
 				_link = document.createElement("A"); 
+				if( this.value )
+				_link.href = 'javascript: EsearchE.quickSearchMail( false, '+(this.totalPages-1)+', false, "'+currentTab+'" )';
+				else{
 				_link.href  = 'javascript:EsearchE.page='+(this.totalPages-1)+';'; 
 				_link.href += 'cExecute("$this.imap_functions.search_msg",openpage,"condition='+this.condition+'&sort_type='+this.sort_type+'&page='+(this.totalPages-1)+'&current_tab='+currentTab+'");'; 
+			} 
 			} 
         	else { 
@@ -360,4 +369,7 @@
 		var sort_type	= sortType;
 
+		if( this.value )
+		  return this.quickSearchMail( false, false, sortType, border_id );
+ 
 		var is_local = border_id.match('.*_local_.*');
 
@@ -383,5 +395,5 @@
     			if ( local_folders.length > 0 ){
                             expresso_local_messages.setSortType(sortType);
-                            gears = expresso_local_messages.search( expresso_local_messages.getFolders(), expresso_local_messages.getFilter() );
+                            gears = expresso_local_messages.search( local_folders, expresso_local_messages.getFilter() );
                         }
     		}
@@ -396,8 +408,5 @@
             if (gears.length > 0)
             {
-                this.localResults = gears.length;
                 allMsg['data_gears']                    = gears;
-            }else{
-                 this.localResults = 0;
             }
         	
@@ -434,5 +443,5 @@
 
 			if ( local_folders.length > 0 )
-				gears = expresso_local_messages.search( expresso_local_messages.getFolders(), expresso_local_messages.getFilter() );
+				gears = expresso_local_messages.search( local_folders, expresso_local_messages.getFilter() );
 
 			data['data_gears']	= gears;
@@ -473,4 +482,5 @@
 		{
 			var td = document.createElement("TD");
+                        add_className(td, 'td_msg');
 			if (j == 0)
 			{
@@ -480,6 +490,6 @@
 					chk_onclick = "changeBgColor(window.event,"+uid_msg+");";
 				else
-					chk_onclick = "changeBgColor(event,"+uid_msg+");";
-				var td1 = '<input type="checkbox" onclick="' + chk_onclick + '" id="check_box_message_'+tr.id+'"></input>';
+					chk_onclick = "changeBgColor(event,"+uid_msg+");";				//'search_' + numBox
+				var td1 = '<input type="checkbox" onclick="' + chk_onclick + '" id="' + border_id + '_check_box_message_'+uid_msg+'"></input>';
 
 			}
@@ -510,5 +520,5 @@
 			{
 				td.setAttribute("width", colSizes[1][3]);
-				td.id = "td_message_important_"+tr.id;
+				td.id = "td_message_important_"+uid_msg;
 				if (aux.flag.match("F"))
 				{
@@ -551,5 +561,5 @@
 			if( j == 6 )
 			{
-				if (aux.from.length > 29)
+				if ( aux.from !== null && aux.from.length > 29)
 					aux.from = aux.from.substr(0,29) + "...";
 
@@ -634,5 +644,5 @@
 
 	// Form resultado
-	searchE.prototype.mount_result = function( Data, sort_type, keep_border )
+	searchE.prototype.mount_result = function( Data, sort_type, keep_border, keep_filled, division )
 	{
 		var data = ( Data['data'] ) ? Data['data'] : Data['data_gears'];
@@ -648,17 +658,15 @@
 			sort_type = this.sort_type;
 
-                var tab = numBox;
-
 		if ( keep_border ) {
 			var border_id = currentTab;
 		}
 		else {
-			tab = count_borders() + 1;
+			numBox++;
 
 			if( Data['data'] )
-				var border_id = create_border(get_lang("Server Results"), "search_" + tab);
+				var border_id = create_border(get_lang("Server Results"), "search_" + numBox);
 
 			if( Data['data_gears'])
-				var border_id = create_border(get_lang("Local Results"), "search_local_msg" + tab);
+				var border_id = create_border(get_lang("Local Results"), "search_local_msg" + numBox);
 		}
 
@@ -667,5 +675,5 @@
 
         currentTab = border_id;
-        openTab.content_id[currentTab] = Element('content_id_search_' + tab);
+        openTab.content_id[currentTab] = Element('content_id_search_' + numBox);
         openTab.type[currentTab] = 1;
 		openTab.condition[currentTab] = this.condition;
@@ -673,10 +681,14 @@
 		if ( keep_border ) {
 			var content_search =  Element('content_id_' + border_id);
-			var div_scroll_result = Element("divScrollMain_"+tab);
+			var div_scroll_result = Element("divScrollMain_"+numBox);
+
 			content_search.removeChild(div_scroll_result);
+
+			if( !keep_filled )
+			    div_scroll_result = false;
 		}
 
 		var table = document.createElement("TABLE");
-			table.id    = "table_resultsearch_" + tab;
+			table.id    = "table_resultsearch_" + numBox;
 			table.frame = "void";
 			table.rules = "rows";
@@ -686,12 +698,13 @@
 
 		var tbody		= document.createElement("TBODY");
-			tbody.id	= "tbody_box_" + tab;
+			tbody.id	= "tbody_box_" + numBox;
 
 		for( var i=0; i < data.length; i++)
 		{
+			if(data[i] !== null){
 			var tr = EsearchE.make_tr_message(data[i],border_id,i);
-
             tbody.appendChild(tr);
 		}
+		}
 		
 		//global_search++; //Tabs from search must not have the same id on its tr's // use numBox instead of this!
@@ -700,10 +713,13 @@
 
 		var colgr_element = buildColGroup(1);
-		colgr_element.setAttribute("id","colgroup_main_"+tab);
+		colgr_element.setAttribute("id","colgroup_main_"+numBox);
 		table.appendChild(colgr_element);
 
 		var content_search =  Element('content_id_' + border_id);
+		
+		if( !div_scroll_result )
+		{
 		var div_scroll_result = document.createElement("DIV");
-		div_scroll_result.id = "divScrollMain_"+tab;
+		    div_scroll_result.id = "divScrollMain_"+numBox;
 		div_scroll_result.style.overflowY = "scroll";
 		div_scroll_result.style.overflowX = "hidden";
@@ -714,4 +730,12 @@
 			div_scroll_result.style.width	="100%";
 		}
+		}
+		if( division )
+		{
+		    var _div = document.createElement("div");
+		    _div.className = 'local-messages-search-warning';
+		    _div.innerHTML = division;
+		    div_scroll_result.appendChild(_div);
+		}
 
 		if(is_ie)
@@ -730,5 +754,5 @@
 			table_element.addEventListener("mousedown",startColResize,false);
 		}
-		table_element.setAttribute("id", "table_message_header_box_"+tab);
+		table_element.setAttribute("id", "table_message_header_box_"+numBox);
 		table_element.className = "table_message_header_box";
 		if (!is_ie)
@@ -772,5 +796,5 @@
 		
 		// Ordernar Pasta
-		if ( sort_type == 'SORTBOX')
+		if ( sort_type == 'SORTBOX' /*|| sort_type == 'SORTBOX_REVERSE'*/ )
 		{
 			if( Data['data'] )
@@ -801,5 +825,5 @@
 		td_element2 = createTDElement(1,6,"th_resizable","left");
 
-		if (sort_type == 'SORTFROM')
+		if (sort_type == 'SORTFROM' || sort_type == 'SORTWHO' /*|| sort_type == 'SORTWHO_REVERSE' || sort_type == 'SORTFROM_REVERSE'*/ )
 		{
 			if(Data['data'])
@@ -830,5 +854,5 @@
 		td_element3 = createTDElement(1,7,"th_resizable","left");
 		
-		if (sort_type == 'SORTSUBJECT')
+		if (sort_type == 'SORTSUBJECT' /*|| sort_type == 'SORTSUBJECT_REVERSE'*/)
 		{
 			if( Data['data'])
@@ -859,5 +883,5 @@
 		td_element4 = createTDElement(1,8,"th_resizable","center");
 		
-		if ( sort_type == 'SORTDATE' )
+		if ( sort_type == 'SORTDATE' /*|| sort_type == 'SORTDATE_REVERSE'*/ )
 		{
 			if( Data['data'] )
@@ -888,5 +912,5 @@
 		td_element5 = createTDElement(1,9,"th_resizable","center");
 		
-		if ( sort_type == 'SORTSIZE' )
+		if ( sort_type == 'SORTSIZE' /*|| sort_type == 'SORTSIZE_REVERSE'*/ )
 		{
 			if( Data['data'] )
@@ -928,8 +952,8 @@
 
 		var colgr_element = buildColGroup(1);
-		colgr_element.setAttribute("id","colgroup_head_"+tab);
+		colgr_element.setAttribute("id","colgroup_head_"+numBox);
 		table_element.appendChild(colgr_element);
 
-		if( parseInt( Data['gears_num_msgs'] ) > 0 && !Data['data_gears'])
+		if( parseInt( Data['gears_num_msgs'] ) > 0 && !keep_filled)
 		{
 			var _div_gears = document.createElement("div");
@@ -941,9 +965,13 @@
 		}		
 
-		var _divScroll = document.getElementById("divScrollHead_"+tab);
-
-		if(!_divScroll){
+		var _divScroll = document.getElementById("divScrollHead_"+numBox);
+		
+		if( _divScroll ){
+			content_search.removeChild(_divScroll);
+			_divScroll = false;
+		}
+
 			_divScroll = document.createElement("DIV");
-			_divScroll.id = "divScrollHead_"+tab;
+		_divScroll.id = "divScrollHead_"+numBox;
 			_divScroll.style.overflowY = "hidden";
 			_divScroll.style.overflowX = "hidden";
@@ -955,5 +983,5 @@
 			_divScroll.appendChild(table_element);
 			content_search.appendChild(_divScroll);
-		}
+
 		/*end of "put header"*/
 		if ( !expresso_offline )
@@ -1067,5 +1095,5 @@
 
 	// Search;
-	searchE.prototype.func_search = function(value)
+	searchE.prototype.func_search_complex = function()
 	{
 		var fields = "##";
@@ -1111,5 +1139,10 @@
                     }
             }
-
+			if ((trim(Element("since_date").value) != "") && (trim(Element("before_date").value) != "")){
+				if(!(validate_date_order(trim(Element("since_date").value), trim(Element("before_date").value)))){
+					alert(get_lang('Invalid date on field %1', get_lang('Before Date')));
+                    return false;
+				}
+			}
             if(trim(Element("on_date").value) != "")
             {
@@ -1171,9 +1204,4 @@
                 }
             }
-
-            if ( value )
-            {
-				fields = "##ALL " +  "<=>" +url_encode(value) + "##";
-			}
 
 		if(fields == "##")
@@ -1234,4 +1262,5 @@
             {
                 var allMsg 	= [3];
+        	var count  	= ( data['num_msgs'] ) ?  data['num_msgs'] : "0";
                 var tmp		= [];
 
@@ -1241,6 +1270,5 @@
                     tmp = expresso_local_messages.search( local_folders, fields );
                 }
-                var count = data['num_msgs'] ?  data['num_msgs'] : tmp.length > 0 ? tmp.length : "0";
-                 EsearchE.localResults = tmp.length;
+
                 if( data['num_msgs'] )
                 {
@@ -1254,5 +1282,5 @@
                 }
 
-                if( data['num_msgs'] == 0 && tmp.length < 0)
+        	if( ( data['num_msgs'] ) == 0 )
                 {
                     alert( get_lang("None result was found.") );
@@ -1260,13 +1288,16 @@
                 else
                 {
-                    if(tmp.length > 0 && !data['num_msgs'])
+            	if( (tmp.length > 0) && (!data['num_msgs']) ) 
                     {
-                        allMsg['data_gears'] = tmp;
+            		EsearchE.viewLocalMessage();
                     }
-                   
+            	else
+            	{
+					if(count > 0)
+						EsearchE.func_clean();
                      write_msg( count + " " + get_lang("results found") );
                     EsearchE.mount_result( allMsg, 'SORTDATE' );
                 }
-
+            }
             }
 
@@ -1280,7 +1311,134 @@
             else
                     cExecute( args, handler, params);
+	}
+	
+	searchE.prototype.func_search = function(value, page, sort, border_id)
+	{
+	  if( !value )
+	      this.func_search_complex();
+	  else
+	    this.quickSearchMail( value, page, sort, border_id );
+	}
+	
+	searchE.prototype.quickSearchMail = function(value, page, sort, border_id)
+	{
+		var local_folders = new Array();
+		var temp;
+		var not_found_corrent_folder = true;
+
+//		if( Element("check_all_msg").checked )
+//		{
+			this.all_mailboxes();
+			var nm_box = new Array;
+			for(var i=0; i < EsearchE.all_boxes.length; i++)
+			{
+				nm_box[i] = EsearchE.all_boxes[i]/* + fields*/;
+			}
+			if (preferences.use_local_messages == 1)
+			{
+				temp = expresso_local_messages.list_local_folders();
+				for (var x in temp)
+				{
+					local_folders.push(temp[x][0]);
+				}
+			}
+		this.sort		= sort || this.sort || "SORTDATE";
+		this.page		= isNaN(page) ? ( value ? 0 : this.page ) : page;
+		this.value		= value || this.value || false;
+		
+		if( !this.value )
+		    return alert( "Busca sem caracteres." );
+
+		var args		= "$this.imap_functions.quickSearchMail";
+		var params		= { page: this.page, sort: this.sort, search: this.value };
+
+		var selection1 = [], selection2 = [];
+
+		for( var i = 0; i < nm_box.length; i++ )
+		    if( nm_box[i] === current_folder )
+			continue;
+		    else if( /^user/.test(nm_box[i]) )
+			selection2[selection2.length] = nm_box[i];
+		    else
+			selection1[selection1.length] = nm_box[i];
+
+		 if( expresso_offline )
+			    handler('none');
+		 else
+		 {
+			    var url = [], labels = [];
+
+			    params['folder'] = current_folder;
+			    url[0] = args + "&" +  $.param( params );
+			    labels[0] = "mensagem na sua pasta atual";
+
+			    if(selection1.length)
+			    {
+					params['folder'] = selection1;
+					url[1] = args + "&" +  $.param( params );
+					labels[1] = "mensagens nas suas outras pastas";
+				}
+				if(selection2.length)
+			    {
+					params['folder'] = selection2;
+					url[2] = args + "&" +  $.param( params );
+					labels[2] = "mensagens nas suas pastas compartilhadas";
+				}
+			    var link = ' <a href="#" onclick="connector.cancelRequest(); clean_msg(); return false;">cancelar<a/>';
+
+			    write_msg( "pesquisando " + labels[0] + link, true );
+			    
+			    var keepFilled = false;
+
+			    var handler = function( data )
+			    {
+					data = $.parseJSON( Base64.decode(data) );
             
+					var allMsg 	= {
+						num_msgs: ( data['num_msgs'] || 0 ),
+						data: ( data['data'] || data['msgs'] || data )
+					};
+
+					// Gears - local
+					if ( local_folders.length > 0 ){
+						expresso_local_messages.setSortType('SORTDATE');
+						var tmp = expresso_local_messages.search( local_folders, "##ALL <=>" + url_encode(value) + "##" ); 
+
+						if( tmp.length > 0 )
+							if( !allMsg['num_msgs'] ){
+								clean_msg();
+							    not_found_corrent_folder = false;
+								if(tmp.length)
+									allMsg['gears_num_msgs'] = tmp.length;
+							}else
+								allMsg['gears_num_msgs'] = tmp.length;
         }
-	// clean;
+
+					if( !allMsg['num_msgs'] && !url.length && not_found_corrent_folder){
+						clean_msg();
+						//alert( get_lang("None result was found.") );
+						return EsearchE.viewLocalMessage();
+					}
+
+					if( allMsg['num_msgs'] )
+						EsearchE.total = allMsg['num_msgs'] = Math.max( (EsearchE.total || 0), allMsg['num_msgs'] );	
+
+					EsearchE.mount_result( allMsg, EsearchE.sort, ( keepFilled || border_id === currentTab ), keepFilled, "Foram encontradas " + data['msgs'].length + " "+labels.shift()+"." );
+					
+					keepFilled = true;
+					
+					write_msg( "pesquisando " + labels[0] + link, true );
+
+					if( url.length )
+						cExecute( url.shift(), handler );
+					else
+						clean_msg();
+			    }
+
+			    cExecute( url.shift(), handler );
+		    }
+	}
+	
+// clean;
 	searchE.prototype.func_clean = function()
 	{
