Index: /trunk/expressoMail1_2/js/search.js
===================================================================
--- /trunk/expressoMail1_2/js/search.js	(revision 5471)
+++ /trunk/expressoMail1_2/js/search.js	(revision 5480)
@@ -12,5 +12,6 @@
 		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.xtools 		= new xtools('../expressoMail1_2/templates/');	
+		this.xtools 		= new xtools('../expressoMail1_2/templates/');
+		this.divElement 	= null;
 	}
 
@@ -18,106 +19,110 @@
 	searchE.prototype.showForms = function(value)
 	{
-		var div  = null;
-		var args = null;
-
-		args = 
-		{
-			'button_left'  			: "<<",
-			'button_right' 			: ">>",
-			"Inform_your_search_in_the_text_fields" : get_lang('Inform your search in the text fields'),
-			"From" 					: get_lang('From'),
-			"To" 					: get_lang('To'),
-			"Cc" 					: get_lang('Cc'),
-			"Subject" 				: get_lang('Subject'),
-			"Message_body" 			: get_lang('Message body'),
-			"Since_Date" 			: get_lang('Since Date'),
-			"Before_Date" 			: get_lang('Before Date'),
-			"On_Date" 				: get_lang('On Date'),
-			"Flags" 				: get_lang('Flags'),
-			"Flagged" 				: get_lang('Flagged'),
-			"Unflagged" 			: get_lang('Unflagged'),
-			"Seen" 					: get_lang('Seen'),
-			"Unseen" 				: get_lang('Unseen'),
-			"Answered_Forwarded" 	: get_lang('Answered/Forwarded'),
-			"Unanswered_Unforwarded"	: get_lang('Unanswered/Unforwarded'),
-			"Recent" 					: get_lang('Recent'),
-			"Old" 						: get_lang('Old'),
-			"Search_the_messages_in_these_folders" : get_lang('Search the messages in these folders'),
-			"In_all_the_folders"	: get_lang('In all the folders')
-		}
-		
-		div					= document.createElement("div");
-		div.innerHTML		= this.xtools.parse(this.xtools.xml('searchMails'), 'searchMails.xsl', args );
-		div.setAttribute( "style","overflow:hidden");
-
-		$(div).dialog(
-		{
-			resizable	: false,
-			title		: get_lang("Search Email"),
-			position	: 'center',
-			width		: 795,
-			height		: 450,
-			modal		: false,
-			buttons		: [
-							{
-								text: get_lang("Close"),
-								click: function()
-								{
-									$(this).dialog("close");
-								} 
-							},
-							{
-								text: get_lang("Clean"),
-								click: function()
-								{
-									EsearchE.func_clean();
-								}
-							},
-							{
-								text: get_lang("Search"),
-								click: function()
-								{
-									/*EsearchE.func_search();*/
-									$(this).dialog("close");
-								}
-							}
-			],
-			beforeClose	: function()
-			{ 
-				$("#sel_search_nm_box1")[0].parentNode.removeChild($("#sel_search_nm_box1")[0]);
-				$("#divFoldersSearch")[0].parentNode.removeChild($("#divFoldersSearch")[0]);
-				$(this).dialog("destroy");
-				div.removeChild(div.firstChild);
-			}
-		});	
-		
-		if( !Element("table_layer") )
-		{
-			var table_layer    = "";
-		}
-
-		// Cria as caixas postais;
-		this.foldersTree();
-		
-		if(value)
+		if( trim(value) != "" )
 		{
 			Element("check_all_msg").checked = true;
 			EsearchE.all_mailboxes();
-			EsearchE.func_search(value, null, 'SORTDATE_REVERSE');
-		}
-		
-		var dates = $("#since_date, #before_date").datepicker({
-			onSelect: function( selectedDate ) {
-				var option = this.id == "since_date" ? "minDate" : "maxDate",
-					instance = $( this ).data( "datepicker" ),
-					date = $.datepicker.parseDate(
-						instance.settings.dateFormat ||
-						$.datepicker._defaults.dateFormat,
-						selectedDate, instance.settings );
-				dates.not( this ).datepicker( "option", option, date );
-			}
-		});
-		
-		$("#on_date").datepicker();
+			EsearchE.func_search( value, null, 'SORTDATE_REVERSE' );
+		}
+		else
+		{
+		
+			var div  = null;
+			var args = null;
+	
+			args = 
+			{
+				'button_left'  			: "<<",
+				'button_right' 			: ">>",
+				"Inform_your_search_in_the_text_fields" : get_lang('Inform your search in the text fields'),
+				"From" 					: get_lang('From'),
+				"To" 					: get_lang('To'),
+				"Cc" 					: get_lang('Cc'),
+				"Subject" 				: get_lang('Subject'),
+				"Message_body" 			: get_lang('Message body'),
+				"Since_Date" 			: get_lang('Since Date'),
+				"Before_Date" 			: get_lang('Before Date'),
+				"On_Date" 				: get_lang('On Date'),
+				"Flags" 				: get_lang('Flags'),
+				"Flagged" 				: get_lang('Flagged'),
+				"Unflagged" 			: get_lang('Unflagged'),
+				"Seen" 					: get_lang('Seen'),
+				"Unseen" 				: get_lang('Unseen'),
+				"Answered_Forwarded" 	: get_lang('Answered/Forwarded'),
+				"Unanswered_Unforwarded"	: get_lang('Unanswered/Unforwarded'),
+				"Recent" 					: get_lang('Recent'),
+				"Old" 						: get_lang('Old'),
+				"Search_the_messages_in_these_folders" : get_lang('Search the messages in these folders'),
+				"In_all_the_folders"	: get_lang('In all the folders')
+			}
+			
+			div					= document.createElement("div");
+			div.innerHTML		= this.xtools.parse(this.xtools.xml('searchMails'), 'searchMails.xsl', args );
+			div.setAttribute( "style","overflow:hidden");
+	
+			$(div).dialog(
+			{
+				resizable	: false,
+				title		: get_lang("Search Email"),
+				position	: 'center',
+				width		: 795,
+				height		: 450,
+				modal		: false,
+				buttons		: [
+								{
+									text: get_lang("Close"),
+									click: function()
+									{
+										$(this).dialog("close");
+									} 
+								},
+								{
+									text: get_lang("Clean"),
+									click: function()
+									{
+										EsearchE.func_clean();
+									}
+								},
+								{
+									text: get_lang("Search"),
+									click: function()
+									{
+										EsearchE.func_search();
+									}
+								}
+				],
+				beforeClose	: function()
+				{ 
+					$("#sel_search_nm_box1")[0].parentNode.removeChild($("#sel_search_nm_box1")[0]);
+					$("#divFoldersSearch")[0].parentNode.removeChild($("#divFoldersSearch")[0]);
+					$(this).dialog("destroy");
+					div.removeChild(div.firstChild);
+				}
+			});
+			
+			this.divElement = div.parentNode;
+			
+			if( !Element("table_layer") )
+			{
+				var table_layer    = "";
+			}
+	
+			// Cria as caixas postais;
+			this.foldersTree();
+			
+			var dates = $("#since_date, #before_date").datepicker({
+				onSelect: function( selectedDate ) {
+					var option = this.id == "since_date" ? "minDate" : "maxDate",
+						instance = $( this ).data( "datepicker" ),
+						date = $.datepicker.parseDate(
+							instance.settings.dateFormat ||
+							$.datepicker._defaults.dateFormat,
+							selectedDate, instance.settings );
+					dates.not( this ).datepicker( "option", option, date );
+				}
+			});
+			
+			$("#on_date").datepicker();
+		}
 	}
 	
@@ -864,5 +869,5 @@
 			else
 			{
-                                td_element3.onclick		= function(){ EsearchE.searchFor(border_id, 'SORTSUBJECT_REVERSE'); };
+				td_element3.onclick		= function(){ EsearchE.searchFor(border_id, 'SORTSUBJECT_REVERSE'); };
 				td_element3.innerHTML	= "<b>"+get_lang("subject")+"</b><img src='templates/"+template+"/images/arrow_descendant.gif'>";
 			}
@@ -1070,5 +1075,6 @@
 	{
 		var sel = Element("sel_search_nm_box1");
-		if(sel.length > 0)
+		
+		if( sel && ( sel.length > 0 ) )
 		{
 			for(var i=0; i < sel.options.length; i++)
@@ -1260,5 +1266,5 @@
 			}
 			
-			for(var i=0; i < sel_combo.options.length; i++)
+			for( var i=0; i < sel_combo.options.length; i++ )
 			{
 				if( sel_combo.options[i].selected == true )
@@ -1274,15 +1280,16 @@
         var handler = function( data )
         {
-            var allMsg 	= [3];
+        	var allMsg 	= [3];
 			var count  	= ( data['num_msgs'] ) ?  data['num_msgs'] : "0";
 			var tmp		= [];
 
 			// Gears - local
-			if ( local_folders.length > 0 ){
+			if ( local_folders.length > 0 )
+			{
 				expresso_local_messages.setSortType('SORTDATE');
 				tmp = expresso_local_messages.search( local_folders, fields );
 			}
 
-			if( data['num_msgs'] )
+			if ( data['num_msgs'] )
 			{
 				allMsg['data'] 		= data['data'];
@@ -1290,10 +1297,10 @@
 			}
 
-			if( tmp.length > 0 )
+			if ( tmp.length > 0 )
 			{
 				allMsg['gears_num_msgs'] = tmp.length ;
 			}
 
-			if( ( data['num_msgs'] ) == 0 )
+			if ( ( data['num_msgs'] ) == 0 )
 			{
 				alert( get_lang("None result was found.") );
@@ -1301,5 +1308,5 @@
 			else
 			{
-				if( (tmp.length > 0) && (!data['num_msgs']) ) 
+				if( ( tmp.length > 0) && ( !data['num_msgs'] ) ) 
 				{
 					EsearchE.viewLocalMessage();
@@ -1307,12 +1314,24 @@
 				else
 				{
-					if(count > 0)
+					if( count > 0 )
+					{
 						EsearchE.func_clean();
+					}
+					
 					write_msg( count + " " + get_lang("results found") );
+					
 					EsearchE.mount_result( allMsg, 'SORTDATE' );
 				}
 			}
-        }
-
+        };
+
+		// Close Dialog
+        if( this.divElement != null )
+		{
+			$(this.divElement.parentNode).dialog("destroy");
+			this.divElement.parentNode.removeChild(this.divElement);
+			this.divElement = null;
+		}
+        
         this.condition	= nm_box;
         this.page		= 0;
@@ -1329,7 +1348,11 @@
 	{
 	  if( !value )
-	      this.func_search_complex();
+	  {
+		  this.func_search_complex();
+	  }
 	  else
-	    this.quickSearchMail( value, page, sort, border_id );
+	  {
+		  this.quickSearchMail( value, page, sort, border_id );
+	  }
 	}
 	
@@ -1473,6 +1496,7 @@
 
 	    EsearchE.all_boxes.splice(0,(EsearchE.all_boxes.length));
-	  	EsearchE.del_mailboxes();
-		$("#since_date, #before_date").datepicker("option", "minDate", "");
+	    EsearchE.del_mailboxes();
+		
+	  	$("#since_date, #before_date").datepicker("option", "minDate", "");
 		$("#since_date, #before_date").datepicker("option", "maxDate", "");
 	}
