Index: trunk/expressoMail1_2/js/main.js
===================================================================
--- trunk/expressoMail1_2/js/main.js	(revision 800)
+++ trunk/expressoMail1_2/js/main.js	(revision 827)
@@ -104,13 +104,12 @@
 
 function show_msg_img(msg_info){
-	delete_border(msg_info.msg_number+"_r");
-	msg_info.showImg = true;
-	show_msg(msg_info);
-}
-
-function show_msg(msg_info){	
+       delete_border(msg_info.msg_number+"_r");
+       msg_info.showImg = true;
+       show_msg(msg_info);
+}
+
+function show_msg(msg_info){
 	if(!verify_session(msg_info))
 		return;
-	
 	if (typeof(msg_info) != 'object')
 		alert(get_lang("Error in show_msg param is not object"));
@@ -147,5 +146,16 @@
 			var border_id = create_border(msg_info.subject, id_msg_read);
 			if(border_id)
+			{
+				openTab.type[border_id] = 2;
 				draw_message(msg_info,border_id);
+				var unseen_sort = document.getElementById('span_flag_UNSEEN').getAttribute('onclick');
+				unseen_sort = unseen_sort.toString();
+				if ( !(unseen_sort.indexOf("'UNSEEN' == 'UNSEEN'") < 0) )
+				{
+					var sort_type = sort_box_type;
+					sort_box_type = null;
+					sort_box('UNSEEN', sort_type);
+				}
+			}
 			else
 				return;
@@ -206,10 +216,11 @@
 				eval(data[i].command);
 			}
-			
-			for (var i=0; i<data.msgs_to_delete.length; i++){
-				var remove_msg = document.getElementById(data.msgs_to_delete[i]);
-				if (remove_msg)
-					remove_msg.parentNode.removeChild(remove_msg);
-			}
+			if (! typeof(data.msgs_to_delete) == 'undefined')
+				for (var i=0; i<data.msgs_to_delete.length; i++){
+					var remove_msg = document.getElementById(data.msgs_to_delete[i]);
+					if (remove_msg)
+						remove_msg.parentNode.removeChild(remove_msg);
+				}
+
 			if(parseInt(alert_new_msg) && data.new_msgs > 0)
 				alert(get_lang("You have %1 new", data['new_msgs'])+" "+(data['new_msgs'] > 1 ? get_lang("messages") : get_lang("message")) +"!");
@@ -247,7 +258,19 @@
 }
 
-function delete_msgs2(folder, msgs_number, border_ID, show_success_msg) {
+function delete_msgs(folder, msgs_number, border_ID, show_success_msg)
+{
+	if (arguments.length < 4){
+		show_success_msg = true;
+	}
+
 	if (folder == 'null')
-		folder = get_current_folder();
+		folder = current_folder;
+
+	if(openTab.type[currentTab] == 1)
+		return move_search_msgs('content_id_'+currentTab);
+
+	if(currentTab.toString().indexOf("_r") != -1){
+			msgs_number = currentTab.toString().substr(0,currentTab.toString().indexOf("_r"));
+	}
 
 	if ((parseInt(preferences.save_deleted_msg)) && (folder!='INBOX'+cyrus_delimiter+trashfolder)){
@@ -258,8 +281,11 @@
 	var handler_delete_msgs = function(data){
 		Element('chk_box_select_all_messages').checked = false;
-		mail_msg = Element("tbody_box");
+		if (currentTab)
+			mail_msg = Element("tbody_box_"+currentTab);
+		else
+			mail_msg = Element("tbody_box");
 		
 		if ( preferences.use_shortcuts == '1') {
-			var all_messages = Element('tbody_box').childNodes;
+			var all_messages = mail_msg.childNodes;
 
 			//Last msg is selected
@@ -282,15 +308,18 @@
 		}
 		if (show_success_msg) {
-			if (data.msgs_number.length == 1) 
+			if (data.msgs_number.length == 1)
 				write_msg(get_lang("The message was deleted."));
-			else 
+			else
 				write_msg(get_lang("The messages were deleted."));
 		}
 
-		if (data.border_ID != 'null'){
+		if (openTab.type[currentTab] > 1){
 			if (parseInt(preferences.delete_and_show_previous_message))
+			{
+				delete_border(currentTab,'false');
 				show_msg(data.previous_msg);
+			}
 			else
-				delete_border(data.border_ID,'false');
+				delete_border(currentTab,'false');
 		}
 		Element('tot_m').innerHTML = parseInt(Element('tot_m').innerHTML) - data.msgs_number.length;
@@ -305,8 +334,4 @@
 		write_msg(get_lang('No selected message.'));		
 }
-function delete_msgs(folder, msgs_number, border_ID)
-{ 
-	delete_msgs2(folder, msgs_number, border_ID, true);
-}
 
 
@@ -314,5 +339,5 @@
 	var selected_messages = '';
 	var temp_msg;
-	var main_list = Element("tbody_resultsearch_"+border_id.substr(18)).childNodes;	
+	var main_list = Element("tbody_box_"+currentTab.substr(7)).childNodes;
 	for (j = 0; j < main_list.length; j++)	{
 		var check_box = main_list[j].firstChild.firstChild; 
@@ -346,6 +371,6 @@
 			var tr = Element(selected_messages[i]);
 			tr.parentNode.removeChild(tr);
-		}			
-	}	
+		}
+	}
 
 	if (selected_messages){
@@ -383,5 +408,4 @@
 
 function move_msgs2(folder, msgs_number, border_ID, new_folder, new_folder_name,show_success_msg){
-
 	if (! folder || folder == 'null')
 		folder = Element("input_folder_"+msgs_number+"_r") ? Element("input_folder_"+msgs_number+"_r").value : get_current_folder();
@@ -398,5 +422,5 @@
 				return false;
 			}else{
-				alert(data);
+				alert(get_lang('Error moving message.')+":\n"+data);
 				return false;
 			}
@@ -406,8 +430,29 @@
 			return false;
 		}
-		
+		mail_msg = ( Element("divScrollMain_"+numBox) ) ? Element("divScrollMain_"+numBox).firstChild.firstChild : Element("divScrollMain_0").firstChild.firstChild;
+		if (data.msgs_number.length == 1)
+			write_msg(get_lang("The message was moved to folder ") + lang_folder(data.new_folder_name));
+		else
+			write_msg(get_lang("The messages were moved to folder ") + lang_folder(data.new_folder_name));
+
+		if (openTab.type[currentTab] > 1){
+			msg_to_delete = Element(msgs_number);
+			if (parseInt(preferences.delete_and_show_previous_message) && msg_to_delete)
+			{
+				if (msg_to_delete.previousSibling){
+					var previous_msg = msg_to_delete.previousSibling.id;
+					cExecute("$this.imap_functions.get_info_msg&msg_number="+previous_msg+"&msg_folder=" + current_folder, show_msg);
+				}
+				else
+					delete_border(currentTab,'false');
+			}
+			else
+				delete_border(currentTab,'false');
+			if(msg_to_delete)
+				mail_msg.removeChild(msg_to_delete);
+			return;
+		}
+
 		Element('chk_box_select_all_messages').checked = false;
-		
-		mail_msg = ( Element("divScrollMain_"+numBox) ) ? Element("divScrollMain_"+numBox).firstChild.firstChild : Element("divScrollMain_0").firstChild.firstChild;
 		if (! mail_msg)
 				mail_msg = Element("tbody_box");
@@ -417,5 +462,5 @@
 		if( typeof(msgs_number) == 'string' )
 			all_search_msg = msgs_number.split(',');
-		else
+		else if( typeof(msgs_number) == 'number')
 			all_search_msg = msgs_number;
 
@@ -446,12 +491,5 @@
 			if (msg_to_delete)
 				mail_msg.removeChild(msg_to_delete);
-		}
-		
-		if (show_success_msg) {
-			if (data.msgs_number.length == 1)
-				write_msg(get_lang("The message was moved to folder ") + lang_folder(data.new_folder_name));
-			else 
-				write_msg(get_lang("The messages were moved to folder ") + lang_folder(data.new_folder_name));
-		}
+		}	
 
 		if (data.msgs_number.length == 1)
@@ -477,6 +515,15 @@
 	}
 
-	if (msgs_number == 'selected')
+	if (msgs_number == 'selected'){
 		msgs_number = get_selected_messages();
+		}
+
+	if(openTab.type[currentTab] == 1){
+		return move_search_msgs('content_id_'+currentTab,new_folder,new_folder_name);
+		}
+
+	if(currentTab.toString().indexOf("_r") != -1){
+		msgs_number = currentTab.toString().substr(0,currentTab.toString().indexOf("_r"));
+	}
 
 	if (parseInt(msgs_number) > 0 || msgs_number.length > 0)
@@ -503,17 +550,22 @@
 		var update_tree;
 
-
 		for (var i = 0; i < data.length; i++) {
 			msg_info = connector.unserialize(data[i]);
 			temp = expresso_local_messages.insert_mail(msg_info, msg_info['header'],msg_info['array_attach'],folder_dest );
-			Element("check_box_message_" + msg_info.msg_number).checked = false;
+			if ( Element("check_box_message_" + msg_info.msg_number) )
+				Element("check_box_message_" + msg_info.msg_number).checked = false;
+
 			if(!temp)
 				sucess = false;
 			else
-				msgs_to_remove.push(msg_info.msg_number);//As mensagens arquivadas devem ser removidas, caso o usuï¿œrio tenha isso configurado.
+				msgs_to_remove.push(msg_info.msg_number);//As mensagens arquivadas devem ser removidas, caso o usuario tenha isso configurado.
+
+			if ( Element(msg_info.msg_number) )
+				remove_className(Element(msg_info.msg_number), 'selected_msg');
+
 		}
 		
 		if (preferences.keep_archived_messages == 0) { //Remove as mensagens
-				delete_msgs2(folder,msgs_to_remove,'null',false);
+				delete_msgs(folder,msgs_to_remove,'null',false);
 		}
 		update_tree=(!has_local_messages_before) && expresso_local_messages.has_local_mails();
@@ -849,10 +901,10 @@
 						tmp = link_attachment.innerHTML.substring(0,link_attachment.innerHTML.lastIndexOf("("));
 						a_tmp[2] = "'"+tmp.substring(0,tmp.length-1)+"'";
-						s_tmp = escape(connector.serialize(a_tmp));			
+						s_tmp = escape(connector.serialize(a_tmp));
 
 						divFiles.innerHTML += "<input style='border:0' type='CHECKBOX' name='forwarding_attachments[]' checked value=\""+s_tmp+"\"/>";
 						divFiles.innerHTML += "<link style='border:0' name='file_"+i+"' id='inputFile_"+border_ID+i+"'/>";
 						divFiles.appendChild(link_attachment);
-					}	
+					}
 				}
 			}
@@ -903,5 +955,5 @@
 			if (is_ie)
 				body.contentWindow.document.onkeypress = function(){if(body.contentWindow.event.keyCode == 13){changeEnterKey(body.contentWindow)}};
-			Element("to_" + new_border_ID).focus();			
+			Element("to_" + new_border_ID).focus();
 			break;
 		case "edit":
@@ -957,15 +1009,15 @@
 
 	if (preferences.auto_save_draft == 1)
-    {
+	{
 		openTab.autosave_timer[new_border_ID] = false;
 		var save_link = document.getElementById("save_message_options"+new_border_ID);
 
         function auto_sav()
-        {
-        	if (openTab.autosave_timer[new_border_ID])
-			clearTimeout(openTab.autosave_timer[new_border_ID]);
-
-	openTab.autosave_timer[new_border_ID] = setTimeout("save_msg("+new_border_ID+")", autosave_time);
-        }
+			{
+				if (openTab.autosave_timer[new_border_ID])
+					clearTimeout(openTab.autosave_timer[new_border_ID]);
+
+				openTab.autosave_timer[new_border_ID] = setTimeout("save_msg("+new_border_ID+")", autosave_time);
+	}
         config_events(body.contentWindow.document,'keypress', auto_sav);
     }
@@ -999,5 +1051,5 @@
 		title = title.substring(0, parseInt(resize)*0.08);
 
-	Element("border_id_" + new_border_ID).innerHTML = "<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td id=font_border_id_" + new_border_ID +" class='font-menu' width=*>" +title + "</td><td width=1px><img onmousedown='javascript:return false' style='cursor:pointer' onclick=delete_border('" + new_border_ID + "','false') src='templates/default/images/close_button.gif'></td></tr></table>";
+	Element("border_id_" + new_border_ID).innerHTML = "<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td id=font_border_id_" + new_border_ID +" class='font-menu' width=*>" +title + "</td><td width=1px><img onmousedown='javascript:return false' style='cursor:pointer' onclick=delete_border('" + new_border_ID + "','false') src='templates/"+template+"/images/close_button.gif'></td></tr></table>";
 	return new_border_ID; //Preciso retornar o ID da nova mensagem.
 }
@@ -1447,7 +1499,7 @@
 		if (data.status && Element("td_message_answered_"+msg_number)) {	
 			if(flag == 'answered')
-				Element("td_message_answered_"+msg_number).innerHTML = '<img src=templates/default/images/answered.gif title=Respondida>';
+				Element("td_message_answered_"+msg_number).innerHTML = '<img src=templates/'+template+'/images/answered.gif title=Respondida>';
 			else if(flag == 'forwarded')
-				Element("td_message_answered_"+msg_number).innerHTML = '<img src=templates/default/images/forwarded.gif title=Encaminhada>';
+				Element("td_message_answered_"+msg_number).innerHTML = '<img src=templates/'+template+'/images/forwarded.gif title=Encaminhada>';
 		}
 	}
@@ -1456,4 +1508,7 @@
 
 function print_all(){
+	if (openTab.type[currentTab] == 2)
+		return print_msg(current_folder,currentTab.substr(0,currentTab.indexOf("_r")),currentTab);
+
 	var folder= Element('border_id_0').innerHTML;
 	var body = Element('divScrollMain_'+numBox).innerHTML;
@@ -1576,5 +1631,8 @@
 
 function export_all_selected_msgs(){
-	
+	if (openTab.type[currentTab] > 1){
+		source_msg(currentTab,current_folder);
+		return;
+	}
 	var msgs_to_export = this.get_selected_messages();
 	
