Index: trunk/expressoMail1_2/js/search.js
===================================================================
--- trunk/expressoMail1_2/js/search.js	(revision 163)
+++ trunk/expressoMail1_2/js/search.js	(revision 271)
@@ -266,4 +266,9 @@
 		table.id    = "table_resultsearch_" + search_tabs;
 		table.width = "100%";
+		table.borderColorDark = "#bbbbbb";
+		table.frame = "void";
+		table.rules = "rows";
+		table.cellPadding = "0";
+		table.cellSpacing = "0";
 
 		var tbody = document.createElement("TBODY");
@@ -275,6 +280,5 @@
 		for( var i=0; i < nw_array.length; i++){
 			var tr = document.createElement("TR");
-	    	//var color = "#F0F0F0";
-			//tr.style.background  =  color;
+			tr.style.height = "30px";
 			tr.className = 'tr_msg_read';
 
@@ -326,4 +330,6 @@
 					if( j == 3){
 						var subject_encode = url_encode(aux[2]);
+						if (! subject_encode)
+							aux[2] = get_lang("no subject") + "...";
 						td.width = "30%";					
 						var td1  = '<div style="width:100%;overflow:hidden"><a href=javascript:void(0) onclick=EsearchE.open_msg(\"' + url_encode(mailbox) + '\",\"' + uid_msg + '\",\"' + subject_encode + '\")>' + aux[2] +'</a></div>';
@@ -345,6 +351,11 @@
 				}
 			}
-			aux.splice(0,(aux.length));
-			tbody.appendChild(tr);
+			aux.splice(0,(aux.length));	
+
+		subject = tr.firstChild;
+		subject = subject.nextSibling.nextSibling.nextSibling.firstChild.firstChild.innerHTML;
+//		_dragArea.makeDragged(tr, msg_number, subject, true);
+		_dragArea.makeDragged(tr, tr.firstChild.firstChild.id.substr(tr.firstChild.firstChild.id.indexOf(';')+1), subject, true);
+		tbody.appendChild(tr);
 		}
 		table.appendChild(tbody);
@@ -376,5 +387,4 @@
 		var table_element = document.createElement("TABLE");
 		var tbody_element = document.createElement("TBODY");
-		table_element.border = 1;
 		table_element.setAttribute("id", "table_message_header_box");
 		table_element.className = "table_message_header_box";
@@ -447,5 +457,5 @@
 		content_search.appendChild(div_scroll_result);	
 		if (span_options)
-			content_search.appendChild(span_options);	
+			content_search.appendChild(span_options);
 	}
 
Index: trunk/expressoMail1_2/js/abas.js
===================================================================
--- trunk/expressoMail1_2/js/abas.js	(revision 245)
+++ trunk/expressoMail1_2/js/abas.js	(revision 271)
@@ -69,28 +69,4 @@
 	}
 	
-	/*var span_options = document.getElementById("span_options");	
-	if (span_options){
-		span_options.parentNode.removeChild(span_options);
-		var span_options = document.createElement("SPAN");	
-		span_options.setAttribute("id","span_options");	
-	}
-		
-	if ((span_options) && (ID == 0)){
-		var span_D = Element("span_D");
-		span_options.innerHTML = 
-		'<span title="'+get_lang("Delete")+'" style="margin-left:8px;" class="message_options" onclick=delete_msgs(\'null\',\'selected\',\'null\')>'+get_lang("Delete")+'</span> | '+
-		'<span title="'+get_lang("Move")+'" class="message_options" onclick=wfolders.makeWindow(\"\",\"move_to\")>'+get_lang("Move")+'</span> | '+
-   		'<span title="'+get_lang("Print")+'" class="message_options" onclick=print_all()>'+get_lang("Print")+'</span> | '+
-	   	'<span title="'+get_lang("Export")+'" class="message_options" onclick="export_all_selected_msgs()">'+get_lang("Export")+'</span>';	
-	   	span_options.setAttribute('style',"font-size:5px;");
-		span_D.parentNode.insertBefore(span_options,span_D);	
-	} else
-		if ((span_options) && (ID != 0)){		
-			span_options.innerHTML = '<span title="'+get_lang("Delete")+'" style="margin-left:8px" class="message_options" onclick=delete_search_msgs('+content.id+')>'+get_lang("Delete")+'</span> | '+
-				'<span title="'+get_lang("Move")+'" class="message_options" onclick=wfolders.makeWindow(\"border_id_'+content.id.substr(11)+'\",\"move_to\")>'+get_lang("Move")+'</span>';
-		var span_D = Element("span_D");				
-		span_D.parentNode.insertBefore(span_options,span_D);	
-	   	}
-	*/
 	return ID;
 }
@@ -108,5 +84,12 @@
 	}
 	Element("footer_menu").style.display = 'none';
-	var ID = id_value ? id_value : parseInt(BordersArray[(BordersArray.length-1)].sequence) + 1;
+	
+	if (! id_value) // Is new message?
+		var ID = parseInt(BordersArray[(BordersArray.length-1)].sequence) + 1;
+	else{
+		var ID = id_value;
+		if (document.getElementById("border_id_"+id_value+"_r")) // Edit message? if yes treat like new
+					var id_value = null;
+	}
 	
 	td = document.createElement("TD");
@@ -204,4 +187,5 @@
 	if(!ID)
 		return false;
+	
 	if (msg_sent == 'false')
 	{
@@ -211,5 +195,6 @@
 			try
 			{
-				if (body.contentWindow.document.designMode.toLowerCase() == 'on')
+				var save_link = Element("save_message_options");
+				if ((body.contentWindow.document.designMode.toLowerCase() == 'on') && (save_link.onclick != ''))
 				{
 					var discard_msg = confirm(get_lang("Your message has not been sent. Discard your message?"), "");
@@ -220,4 +205,6 @@
 					else
 					{
+						if (! newMessageTab.toPreserve[ID])
+							cExecute ("$this.imap_functions.delete_msgs&folder="+newMessageTab.imapBox[ID]+"&msgs_number="+newMessageTab.imapUid[ID],function(data){ return });
 						Element('to_'+ID).focus();
 					}
@@ -229,4 +216,7 @@
 		}
 	}
+
+	newMessageTab.toPreserve[ID] = false;
+	newMessageTab.imapUid[ID] = 0;
 
 	hold_session = false;
Index: trunk/expressoMail1_2/js/common_functions.js
===================================================================
--- trunk/expressoMail1_2/js/common_functions.js	(revision 197)
+++ trunk/expressoMail1_2/js/common_functions.js	(revision 271)
@@ -45,5 +45,14 @@
 // onUnload edit message
 if(!is_ie)
-	window.onbeforeunload = unloadMess;
+{
+	var beforeunload = window.onbeforeunload;
+	window.onbeforeunload = function()
+	{
+		if ( typeof beforeunload == 'function' )
+			beforeunload();
+
+		return unloadMess();
+	};
+}
 function unloadMess(){
 	var mess = get_lang("Your message has not been sent and will be discarted.");
@@ -154,5 +163,5 @@
 }
 
-function download_attachments(msg_folder, msg_number, idx_file, msg_part, encoding, new_file_name){
+function download_attachments(msg_folder, msg_number, idx_file, msg_part, encoding, new_file_name, show_iframe){
 	div_attachment = document.getElementById("id_div_attachment");
 	var params = '';
@@ -167,6 +176,8 @@
 	if(encoding)
 		params += "&encoding="+encoding;
+
 	div_attachment.innerHTML="<iframe style='display:none;width:0;height:0' name='attachment' src='inc/gotodownload.php?msg_folder="+msg_folder+"&msg_number="+msg_number+"&idx_file="+idx_file+"&msg_part="+msg_part+params+"'></iframe>";
 }
+
 function download_all_attachments(msg_folder, msg_number){
 	var handler_source = function(data){		
@@ -176,23 +187,32 @@
 }
 // Add Input File Dynamically.
-var countFiles = 0;
 function addFile(id_border){
 	divFiles = document.getElementById("divFiles_"+id_border);
-	countFiles++;
+	
+	if (divFiles.lastChild)
+		var countDivFiles = parseInt(divFiles.lastChild.id.split('_')[2]) + 1;
+
+	if (! countDivFiles)
+		var countDivFiles = 1;
+	
 	divFile = document.createElement('DIV');
-	inputFile = document.createElement("INPUT");
+	
+	var inputFile = document.createElement("INPUT");
 	
 	var tmp_id_border = document.createAttribute('id_border');
 	tmp_id_border.value = id_border;
+	
 	inputFile.setAttributeNode(tmp_id_border);
-	inputFile.id = "inputFile_"+id_border+countFiles;
+	inputFile.id = "inputFile_"+id_border+"_"+countDivFiles;
 	inputFile.onchange = function () {validateFileExtension(this.value, this.id.replace("input","div"), this.getAttribute('id_border'));};
 	inputFile.type = 'file';
 	inputFile.size = 50;
 	inputFile.maxLength = 255;
-	inputFile.name='file_'+countFiles;
+	inputFile.name='file_'+countDivFiles;
+	
 	divFile.appendChild(inputFile);
-	linkFile = document.createElement("A");
-	linkFile.id = "linkFile_"+id_border+countFiles;
+	
+	var linkFile = document.createElement("A");
+	linkFile.id = "linkFile_"+id_border+"_"+countDivFiles;
 	linkFile.href='javascript:void(0)';
 	linkFile.onclick=function () {removeFile(this.id.replace("link","div"));};
@@ -200,9 +220,10 @@
 	//divFile.innerHTML += "&nbsp;&nbsp;";
 	divFile.appendChild(linkFile);
-	divFile.id = "divFile_"+id_border+countFiles;	
+	divFile.id = "divFile_"+id_border+"_"+countDivFiles;	
 	divFiles.appendChild(divFile);
 }
 //	Remove Input File Dynamically.
 function removeFile(id){
+	var border_id = id.substr(8,1);
 	var el = Element(id);
 	el.parentNode.removeChild(el);
@@ -210,16 +231,33 @@
 
 function validateFileExtension(fileName, id, id_border){
-	var fileExtension = fileName.split(".");
+	var error_flag  = false;
+	if (fileName[0] != '/'){ // file name is windows format?
+		var file = fileName.substr(fileName.lastIndexOf('\\') + 1, fileName.length);
+		if ((fileName.indexOf(':\\') != 1) && (fileName.indexOf('\\\\') != 0)) // Is stored in partition or a network file?
+			error_flag = true;	
+	}
+	else // is nix
+		var file = fileName.substr(fileName.lastIndexOf('/') + 1, fileName.length);						
+
+	var fileExtension = file.split(".");
 	fileExtension = fileExtension[(fileExtension.length-1)];
-	
 	for(var i=0; i<denyFileExtensions.length; i++)
 	{
 		if(denyFileExtensions[i] == fileExtension)
 		{
-			alert(get_lang('File extension forbidden') + '.');
-			removeFile(id);
-			addFile(id_border);
-		}
-	}
+			error_flag = true;
+			break;
+		}
+			
+	}
+
+	if (error_flag == true){
+		alert(get_lang('File extension forbidden or invalid file') + '.');
+		removeFile(id);
+		addFile(id_border);
+		return false;
+	}
+	return true;
+	
 }
 
Index: trunk/expressoMail1_2/js/main.js
===================================================================
--- trunk/expressoMail1_2/js/main.js	(revision 245)
+++ trunk/expressoMail1_2/js/main.js	(revision 271)
@@ -1,4 +1,6 @@
 // Tempo do auto refresh (em milisegundos)
 var time_refresh = 300000;
+var autosave_time = 60000;
+
 function init(){
 	if (!is_ie)
@@ -38,4 +40,77 @@
 	setTimeout('auto_refresh()', time_refresh);
 }
+
+var away = false;
+var away_timer;
+function setAway ()
+{
+	away_timer = setTimeout("setAway()", 5000);
+	away=true;
+}
+
+function auto_save_msg(border_id)
+{
+	if (document.getElementById('border_id_'+border_id))
+	{	
+		var	 keypress_handler = function stop_counting ()
+		{
+			clearTimeout(away_timer);
+			away=false;
+			away_timer = setTimeout('setAway()',5000);
+		};
+		
+		var obj = document.getElementById('body_'+border_id).contentWindow.document;
+		if ( obj.addEventListener )
+				obj.addEventListener('keypress', keypress_handler, false);
+		else if ( obj.attachEvent )
+			obj.attachEvent('onkeypress', keypress_handler);
+
+		if (away)
+			{
+				var save_link = Element("save_message_options");
+				if (save_link.onclick !=  '')
+					save_msg(border_id);
+				else
+					setTimeout('auto_save_msg('+border_id+')',autosave_time);
+			}
+			else
+				setTimeout('auto_save_msg('+border_id+')',3000);
+	}
+}
+
+function watch_changes_in_msg(border_id)
+{
+	if (document.getElementById('border_id_'+border_id))
+	{	
+		function keypress_handler ()
+		{
+			away=false;
+			var save_link = Element("save_message_options");
+			save_link.onclick = function onclick(event) { newMessageTab.toPreserve[border_id] = true; save_msg(border_id); } ;
+			save_link.style.color = '#0000ff';
+		};
+		
+		var obj = document.getElementById('body_'+border_id).contentWindow.document;
+		if ( obj.addEventListener )
+				obj.addEventListener('keypress', keypress_handler, false);
+		else if ( obj.attachEvent )
+			obj.attachEvent('onkeypress', keypress_handler);
+			
+		var subject_obj = document.getElementById('subject_'+border_id);
+		if ( subject_obj.addEventListener )
+				subject_obj.addEventListener('keypress', keypress_handler, false);
+		else if ( subject_obj.attachEvent )
+			subject_obj.attachEvent('onkeypress', keypress_handler);
+			
+		var to_obj = document.getElementById('to_'+border_id);
+		if ( to_obj.addEventListener )
+				to_obj.addEventListener('keypress', keypress_handler, false);
+		else if ( to_obj.attachEvent )
+			to_obj.attachEvent('onkeypress', keypress_handler);
+		
+	}
+}
+
+
 
 function show_msg(msg_info){
@@ -105,14 +180,19 @@
 			var box = Element("tbody_box");
 			table_element.appendChild(box);
-			var len = data.length;
-			for (var i=0; i<len; i++){
+			for (var i=0; i<data.length; i++){
 				var new_msg = this.make_tr_message(data[i], current_folder);
 				_dragArea.makeDragged(new_msg, data[i].msg_number, data[i].subject, true);
 				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(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")) +"!");
 			if(data.new_msgs > 0){
-			Element('tot_m').innerHTML = parseInt(Element('tot_m').innerHTML) + parseInt(data.new_msgs);
+			Element('tot_m').innerHTML = parseInt(Element('tot_m').innerHTML) + parseInt(data.new_msgs); //refresh tab title
 			update_quota();
 			}
@@ -368,5 +448,9 @@
 
 function new_message(type, border_ID){
-	var new_border_ID = draw_new_message();	
+
+	var new_border_ID = draw_new_message(parseInt(border_ID));
+	if (preferences.auto_save_draft == 1)
+		setTimeout('auto_save_msg('+new_border_ID+')', autosave_time);		
+	
 	// Salva a pasta da mensagem respondida ou encaminhada:
 	var folder_message = Element("input_folder_"+border_ID);
@@ -596,4 +680,7 @@
 			break;
 		case "edit":
+			newMessageTab.imapBox[new_border_ID] = folder_message.value
+			newMessageTab.imapUid[new_border_ID] = parseInt(border_ID.substr(0,border_ID.indexOf("_")));
+			document.getElementById('font_border_id_'+new_border_ID).innerHTML = data.subject;
 			data.to = Element("to_values_" + border_ID).value;
 			data.to = data.to.replace(/&lt;/gi,"<");
@@ -610,4 +697,20 @@
 			Element("subject_" + new_border_ID).value = data.subject;
 			
+			var divFiles = Element("divFiles_"+new_border_ID);
+			if (Element("attachments_" + border_ID)){
+				var attachments = document.getElementById("attachments_" + border_ID).childNodes;
+				for (var i = (attachments.length > 1 ? 1 : 0); i < attachments.length; i++){
+					var link_attachment = document.createElement("A");
+					link_attachment.setAttribute("href", attachments[i].href);
+					link_attachment.innerHTML = attachments[i].innerHTML;
+					var tmp = link_attachment.href.replace("javascript:download_attachments(", "").replace(")", "");
+					var a_tmp = tmp.split(',');
+					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));
+					divFiles.innerHTML += "<input style='border:0' type='CHECKBOX' name='forwarding_attachments[]' checked value=\""+s_tmp+"\"/>";
+					divFiles.appendChild(link_attachment);
+				}
+			}
 			var body = Element("body_" + new_border_ID);
 			body.contentWindow.document.open();
@@ -682,5 +785,4 @@
 			set_message_flag(msg_number_forwarded.value, 'forwarded');
 		}
-		
 		delete_border(ID,'true');
 		if(wfolders.alert){
@@ -699,13 +801,19 @@
 			write_msg(data);
 	}
+	if ((! newMessageTab.toPreserve[ID]) && (newMessageTab.imapUid[ID] != 0))
+		cExecute ("$this.imap_functions.delete_msgs&folder="+newMessageTab.imapBox[ID]+"&msgs_number="+newMessageTab.imapUid[ID],function(data){ return });
 }
 
 function send_message(ID, folder, folder_name){
+	var save_link = Element("save_message_options");
+	save_link.onclick = '';
+	save_link.style.color = '#999';
+	
 	ID_tmp = ID;
-	
+
 	var handler_send_message = function(data){
-		send_message_return(data, this.ID_tmp);
-	}
-
+		send_message_return(data, this.ID_tmp); // this is a hack to escape quotation form connector bug
+	};
+		
 	var textArea = document.createElement("TEXTAREA");
 	textArea.style.display='none';
@@ -719,4 +827,8 @@
 	input_folder.name = "folder";
 	input_folder.value = folder;
+	var msg_id = document.createElement("INPUT");
+	msg_id.style.display='none';
+	msg_id.name = "msg_id";
+	msg_id.value = newMessageTab.imapUid[ID];
 	
 	if (is_ie){
@@ -730,4 +842,5 @@
 	form.appendChild(textArea);
 	form.appendChild(input_folder);
+	form.appendChild(msg_id);
 
 	var _subject = trim(Element("subject_"+ID).value);
@@ -736,43 +849,182 @@
 		return;
 	}
-
-	cExecuteForm ("$this.imap_functions.send_mail", form, handler_send_message);
-}
-
-function save_msg(border_id, folder_id, folder_name){
-	var handler_save_msg = function(data){
-		if(!verify_session(data))
-			return;
-		if (data.append){
-			write_msg(get_lang('Your message was save as draft in folder %1.', data.folder_name));
-			delete_border(data.border_id,'true');
-		}
-		else
-			write_msg('ERRO saving your message.');
-	}
-	
-	var msg_data = new Array();
-	if (document.getElementById("to_" + border_id))
-		msg_data.to = Element("to_" + border_id).value;
-	
-	if (document.getElementById("cc_" + border_id))
-		msg_data.cc = Element("cc_" + border_id).value;
-	
-	if (document.getElementById("cco_" + border_id))	
-		msg_data.cco = Element("cco_" + border_id).value;	
-	
-	if (document.getElementById("subject_" + border_id))	
-		msg_data.subject = Element("subject_" + border_id).value;
-	
-	if (document.getElementById("body_" + border_id))		
-		msg_data.body = Element("body_" + border_id).contentWindow.document.body.innerHTML;
-		
-	var args   = "$this.imap_functions.save_msg";	
-	var body = msg_data.body.replace(/&nbspl/g, "%nbsp;");
-	body = url_encode(msg_data.body);
-	msg_data.subject = url_encode(msg_data.subject);
-	var params = "folder_id="+folder_id+"&folder_name="+folder_name+"&border_id="+border_id+"&to="+msg_data.to+"&cc="+msg_data.cc+"&cco="+msg_data.cco+"&subject="+msg_data.subject+"&body="+body;
-	cExecute( args, handler_save_msg, params);
-}
+	
+	cExecuteForm ("$this.imap_functions.send_mail", form, handler_send_message, ID);
+}
+
+function return_save(data,border_id,folder_name,folder_id,message_id)
+{
+	var handler_delete_msg = function(data){ refresh(preferences.alert_new_msg); };
+	RichTextEditor.saveFlag = 1;
+
+	if (! data)
+		write_msg('ERROR saving your message.');
+	else
+	{
+		newMessageTab.imapUid[border_id] = data.msg_no;
+		newMessageTab.imapBox[border_id] = data.folder_id;
+		
+		var newTitle = document.getElementById('subject_'+border_id).value;
+		if (newTitle.length > 18)
+			newTitle = newTitle.substr(18) + '...';
+		else if (newTitle == '')
+				newTitle = get_lang("No subject");
+		document.getElementById('font_border_id_'+border_id).innerHTML = newTitle;
+		
+		// Replace the embedded images for new uids
+		var mainField = document.getElementById('body_'+border_id).contentWindow;
+		var content_body =  mainField.document.getElementsByTagName('body').item(0).innerHTML;
+		var body_images = content_body.match(/msg_num=\d*/g);
+		if (body_images)
+		{
+			for (var i=0; i<body_images.length; i++){
+				content_body = 	content_body.replace(body_images[i],"msg_num="+newMessageTab.imapUid[border_id]);
+			}
+			mainField.document.getElementsByTagName('body').item(0).innerHTML = content_body;
+			// This is not just stupid, it refresh screen to avoid broken images
+			mainField.document.getElementsByTagName('body').item(0).innerHTML = mainField.document.getElementsByTagName('body').item(0).innerHTML;	
+		}	
+				
+		//Replace all files to new files
+		var divFiles = Element("divFiles_"+border_id);
+		var divParent = divFiles.parentNode;
+		divParent.removeChild(divFiles);
+		divFiles = document.createElement('DIV');
+		divFiles.id = "divFiles_"+border_id;
+		divParent.appendChild(divFiles);
+		
+		var attach_files = connector.unserialize(data.files);
+		if (attach_files != null) {
+			newMessageTab.countFile[border_id] = attach_files.length;
+		for (att_index = 0; att_index < attach_files.length; att_index++){
+	
+			var link_attachment = document.createElement("A");
+			var fileName = attach_files[att_index].substr(0,attach_files[att_index].indexOf('_SIZE_'));
+			var fileSize = parseInt(attach_files[att_index].substr(attach_files[att_index].indexOf('_SIZE_')+6))/1024
+			link_attachment.innerHTML = fileName + " ("+parseInt(fileSize)+" kb)";
+
+			var href = "'"+folder_id+"','"+data.msg_no+"','"+(att_index)+"','"+(att_index+2)+"','base64'";
+
+			link_attachment.setAttribute("href", "javascript:download_attachments("+href+")");
+
+			var a_tmp = href.split(',');
+			a_tmp[2] = fileName;
+			s_tmp = escape(connector.serialize(a_tmp));
+					
+			var check_attachment = document.createElement("INPUT");
+			check_attachment.type = 'CHECKBOX';
+			check_attachment.name = 'forwarding_attachments[]';
+			check_attachment.value = s_tmp;
+
+			divFiles.appendChild(check_attachment);
+			divFiles.appendChild(link_attachment);
+			divFiles.appendChild(document.createElement("BR"));
+
+			check_attachment.checked = true;
+		}
+	}
+		if (message_id)
+			cExecute ("$this.imap_functions.delete_msgs&folder="+newMessageTab.imapBox[border_id]+"&msgs_number="+message_id,handler_delete_msg);
+		
+		if (preferences.auto_save_draft == 1)
+				setTimeout('auto_save_msg('+border_id+')', autosave_time);		
+
+		var save_link = Element("save_message_options");
+		save_link.onclick = '';
+		save_link.style.color = '#999';
+		watch_changes_in_msg(border_id);
+		
+		write_msg(get_lang('Your message was save as draft in folder %1.', folder_name));
+	}		
+}
+
+function save_msg(border_id){
+	if (newMessageTab.imapBox[border_id]) //Gets the imap folder
+		var folder_id = newMessageTab.imapBox[border_id];
+	else
+		var	folder_id = "INBOX/"+draftsfolder;
+
+	if (folder_id == 'INBOX') // and folder name from border
+		var folder_name = get_lang(folder_id);
+	else
+		var folder_name = folder_id.substr(6);
+		
+	// hack to avoid form connector bug,  escapes quotation. Please see #179
+	tmp_border_id=border_id;
+	tmp_folder_name=folder_name;
+	tmp_folder_id=folder_id;
+	message_id = newMessageTab.imapUid[border_id];
+	var handler_save_msg = function(data){ return_save(data,this.tmp_border_id,this.tmp_folder_name,this.tmp_folder_id,this.message_id); }
+	
+	var textArea = document.createElement("TEXTAREA");
+	textArea.style.display='none';
+	textArea.name = "body";
+	body = document.getElementById("body_"+border_id);
+	textArea.value += '<body>\r\n';
+	textArea.value += body.contentWindow.document.body.innerHTML;
+	textArea.value += '\r\n</body>';
+	var input_folder = document.createElement("INPUT");
+	input_folder.style.display='none';
+	input_folder.name = "folder";
+	input_folder.value = folder_id;
+	
+	if (is_ie){
+		var i = 0;
+		while (document.forms(i).name != "form_message_"+border_id){i++}
+		form = document.forms(i);
+	}
+	else
+		form = document.forms["form_message_"+border_id];
+	form.appendChild(textArea);
+	form.appendChild(input_folder);
+
+	cExecuteForm ("$this.imap_functions.save_msg", form, handler_save_msg,border_id);	
+}
+
+function return_saveas(data,border_id,folder_name)
+{
+	if(!verify_session(data))
+		return;
+	if (data.append)
+	{
+		delete_border(border_id,null);
+		write_msg(get_lang('Your message was save as draft in folder %1.', folder_name));
+	}
+	else
+		write_msg('ERROR saving your message.');					
+}
+
+
+function save_as_msg(border_id, folder_id, folder_name){
+	// hack to avoid form connector bug,  escapes quotation. Please see #179
+	tmp_border_id=border_id;
+	tmp_folder_name=folder_name;
+	var handler_save_msg = function(data){ return_saveas(data,this.tmp_border_id,this.tmp_folder_name); }	
+	var textArea = document.createElement("TEXTAREA");
+	textArea.style.display='none';
+	textArea.name = "body";
+	body = document.getElementById("body_"+border_id);
+	textArea.value += '<body>\r\n';
+	textArea.value += body.contentWindow.document.body.innerHTML;
+	textArea.value += '\r\n</body>';
+	
+	var input_folder = document.createElement("INPUT");
+	input_folder.style.display='none';
+	input_folder.name = "folder";
+	input_folder.value = folder_id;
+	
+	if (is_ie){
+		var i = 0;
+		while (document.forms(i).name != "form_message_"+border_id){i++}
+		form = document.forms(i);
+	}
+	else
+		form = document.forms["form_message_"+border_id];
+	form.appendChild(textArea);
+	form.appendChild(input_folder);
+	
+	cExecuteForm ("$this.imap_functions.save_msg", form, handler_save_msg,border_id);
+}
+
 
 // Get checked messages
Index: trunk/expressoMail1_2/js/wfolders.js
===================================================================
--- trunk/expressoMail1_2/js/wfolders.js	(revision 197)
+++ trunk/expressoMail1_2/js/wfolders.js	(revision 271)
@@ -47,5 +47,5 @@
 			wfolders.arrayWin[el.id].close();
 			if (type == 'save'){
-				save_msg(border_id, wfolders_tree._selected.id, wfolders_tree._selected.caption);
+				save_as_msg(border_id, wfolders_tree._selected.id, wfolders_tree._selected.caption,true);
 			}
 			else if (type == 'send_and_file'){
Index: trunk/expressoMail1_2/js/filter.js
===================================================================
--- trunk/expressoMail1_2/js/filter.js	(revision 246)
+++ trunk/expressoMail1_2/js/filter.js	(revision 271)
@@ -370,6 +370,5 @@
 				_this.out_officeF = false;
 				//Save outoffice in prefs:
-				if(!prefe)
-					connector.loadScript("preferences");
+				connector.loadScript("preferences");
 				prefe.save("outoffice", _this.out_officeF);
 			}
@@ -530,6 +529,5 @@
 				_this.out_officeF = (aux[4].replace("\n","") == "off") ? false : true;
 				//Save outoffice in prefs:
-				if(!prefe)
-					connector.loadScript("preferences");
+				connector.loadScript("preferences");
 				prefe.save("outoffice", _this.out_officeF);
 		}
Index: trunk/expressoMail1_2/js/draw_api.js
===================================================================
--- trunk/expressoMail1_2/js/draw_api.js	(revision 252)
+++ trunk/expressoMail1_2/js/draw_api.js	(revision 271)
@@ -871,6 +871,30 @@
 		var option_edit	  = '<span class="message_options" onclick="new_message(\'edit\',\''+ID+'\');">'+get_lang('Edit')+'</span> | ';
 		var option_print = '<span class="message_options" onclick="print_msg(\''+info_msg.msg_folder+'\',\''+info_msg.msg_number+'\',\''+ID+'\');">'+get_lang('Print')+'</span> | ';
-		var option_delete = '<span class="message_options" onclick="delete_msgs(\''+info_msg.msg_folder+'\',\''+info_msg.msg_number+'\',\''+ID+'\');">'+get_lang('Delete')+'</span>';
+		var option_delete = '<span class="message_options" onclick="delete_msgs(\''+info_msg.msg_folder+'\',\''+info_msg.msg_number+'\',\''+ID+'\');">'+get_lang('Delete')+'</span> | ';		
+		var option_hide_more = document.createElement("SPAN");
+		option_hide_more.className = 'message_options';
+		options.align = 'right';
+		option_hide_more.value = 'more_options';
+		option_hide_more.id = 'option_hide_more_'+ID;
+		option_hide_more.innerHTML = get_lang('Options');
+		option_hide_more.onclick = function(){
+			var _height = Element("div_message_scroll_"+ID).style.height;
+			_height = parseInt(_height.replace("px",""));
+			var _offset = 35;
+			if (this.value == 'more_options'){
+				this.innerHTML = "<b><u>"+get_lang('Options')+"</u></b>";
+				this.value = 'hide_options';
+				Element("div_message_scroll_"+ID).style.height = (_height - _offset)+"px";
+				Element('table_message_others_options_'+ID).style.display = '';
+			}
+			else{
+				this.innerHTML = get_lang('Options');
+				this.value = 'more_options';
+				Element("div_message_scroll_"+ID).style.height = (_height + _offset)+"px";
+				Element('table_message_others_options_'+ID).style.display = 'none';
+			}
+		};
 		options.innerHTML = option_edit + option_print + option_delete;
+		options.appendChild(option_hide_more);
 		tr.appendChild(td);
 		tr.appendChild(options);
@@ -896,4 +920,5 @@
 	var td1 = document.createElement("TD");
 	td1.innerHTML = get_lang("From: ");
+	td1.appendChild(deny_email(info_msg.from.email));
 	td1.width = "7%";
 	
@@ -903,4 +928,5 @@
 		var td111 = document.createElement("TD");
 		td111.innerHTML = get_lang("Sent by")+": ";
+		td111.appendChild(deny_email(info_msg.sender.email));
 		td111.setAttribute("noWrap","true");
 		var sender = document.createElement("TD");
@@ -911,5 +937,4 @@
 		sender_values.value = info_msg.sender.full; //Veio do IMAP, sem images nem links.
 		sender.innerHTML += draw_plugin_cc(ID, info_msg.sender.full);
-		sender.appendChild(deny_email(info_msg.sender.email));		
 		sender.className = "header_message_field";
 		tr111.appendChild(td111);
@@ -925,5 +950,4 @@
 	if (info_msg.Draft != "X"){
 		from.innerHTML += draw_plugin_cc(ID, info_msg.from);
-		from.appendChild(deny_email(info_msg.from.email));
 		tbody_message_others_options.appendChild(tr1);
 	}
@@ -1197,6 +1221,6 @@
 }
 
-function draw_new_message(){
-	var ID = create_border("");
+function draw_new_message(border_ID){
+	var ID = create_border("",border_ID);
 	hold_session = true;
 /////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -1241,5 +1265,6 @@
 		wfolders.alert = true;
 	}
-	var option_save			= '<span class="message_options" onclick=wfolders.makeWindow("'+ID+'","save");>'+get_lang("Save")+'</span> | ';
+//	var option_save_as		= '<span class="message_options" onclick=wfolders.makeWindow("'+ID+'","save");>'+get_lang("Save as")+'</span> | ';
+	var option_save			= '<span id="save_message_options" class="message_options" onclick="newMessageTab.toPreserve['+ID+'] = true; save_msg('+ID+');">'+get_lang("Save")+'</span> | ';
 	var option_search		= '<span class="message_options" onclick=openListUsers("'+ID+'");>'+get_lang("Search")+'</span>';
 	td_menu_new_message.innerHTML = option_send + option_send_and_file + option_save + option_search;
@@ -2046,5 +2071,5 @@
 		dn_em.setAttribute("title",get_lang("Block Sender"));
 		dn_em.style.cursor = "pointer";		
-		dn_em.innerHTML = "<img onclick=filter.new_rule('"+email+"') src='./templates/default/images/deny.gif'>";
+		dn_em.innerHTML = "<img align='top' onclick=filter.new_rule('"+email+"') src='./templates/default/images/deny.gif'>";
 	return dn_em;
 	
Index: trunk/expressoMail1_2/js/connector.js
===================================================================
--- trunk/expressoMail1_2/js/connector.js	(revision 197)
+++ trunk/expressoMail1_2/js/connector.js	(revision 271)
@@ -506,5 +506,5 @@
 	//	url: 'module.class.method'
 	//	form: form element (for upload files)	
-	function cExecuteForm(url, form, handler){
+	function cExecuteForm(url, form, handler,id){
 		connector.buildBar();
 		isExecuteForm = true;		
@@ -526,9 +526,15 @@
 		}
 
-		if(countFiles) {			
+		var divFiles = Element("divFiles_"+id);		
+		if (divFiles.firstChild) {
 			el			= document.createElement('input');
 			el.type	= 'hidden';	
 			el.name	= 'countFiles';
-			el.value 	= countFiles;
+			var countDivFiles = 0;
+			try{
+				countDivFiles = parseInt(divFiles.lastChild.firstChild.id.split('_')[2]) + 1; // The id of last file 
+				}
+			catch (e) { var countDivFiles = 0; };
+			el.value	= countDivFiles ;
 			form.appendChild(el);						
 		}		
Index: trunk/expressoMail1_2/js/QuickCatalogSearch.js
===================================================================
--- trunk/expressoMail1_2/js/QuickCatalogSearch.js	(revision 128)
+++ trunk/expressoMail1_2/js/QuickCatalogSearch.js	(revision 271)
@@ -11,5 +11,4 @@
 		_this = this;
 
-		var title = get_lang('Select a name') + ':';
 		var el = document.createElement("DIV");
 		el.style.visibility = "hidden";
@@ -23,10 +22,15 @@
 		el.innerHTML = "";
 		
-		if (data.quickSearch_only_in_userSector)
-			title += "<font color='BLACK' nowrap> ("+get_lang('Showing only the results found in your organization')+".)</font>"
-		
-		el.innerHTML = "&nbsp;&nbsp;<b><font color='BLUE' nowrap>"+title+"</font></b><br>&nbsp;&nbsp;";
-		
 		if (document.getElementById('select_QuickCatalogSearch') == null){
+
+			var title_innerHTML = get_lang('Select a name') + ':';
+			if (data.quickSearch_only_in_userSector)
+				title_innerHTML += "<font color='BLACK' nowrap> ("+get_lang('Showing only the results found in your organization')+".)</font>"
+
+			var title = document.createElement("SPAM");
+			title.id = 'window_QuickCatalogSearch_title';
+			title.innerHTML = "&nbsp;&nbsp;<b><font color='BLUE' nowrap>"+title_innerHTML+"</font></b><br>&nbsp;&nbsp;";
+			el.appendChild(title);
+			
 			var cmb = document.createElement("SELECT");
 			cmb.id = "select_QuickCatalogSearch";
@@ -83,4 +87,11 @@
 		}
 		else{
+			var title_innerHTML = get_lang('Select a name') + ':';
+			if (data.quickSearch_only_in_userSector)
+				title_innerHTML += "<font color='BLACK' nowrap> ("+get_lang('Showing only the results found in your organization')+".)</font>"
+
+			var title = Element('window_QuickCatalogSearch_title');
+			title.innerHTML = "&nbsp;&nbsp;<b><font color='BLUE' nowrap>"+title_innerHTML+"</font></b><br>&nbsp;&nbsp;";
+			
 			var cmb = document.getElementById('select_QuickCatalogSearch');
 			cmb.onkeypress = function (e)
@@ -109,6 +120,5 @@
 				}
 			};
-			
-			
+
 			for (i=0; i<cmb.length; i++)
 				cmb.options[i--] = null;
Index: trunk/expressoMail1_2/js/rich_text_editor.js
===================================================================
--- trunk/expressoMail1_2/js/rich_text_editor.js	(revision 163)
+++ trunk/expressoMail1_2/js/rich_text_editor.js	(revision 271)
@@ -1,7 +1,18 @@
+var newMessageTab = {
+		'imapUid' : [], // Stores the imap email number of current tab
+		'countFile' : [0,0,0,0,0,0,0,0,0,0], // Stores the number of files attached in current tab
+		'imapBox' : [], // Stores the folder name
+		'toPreserve' : [] // Check if the message should be removed from draft after send
+		};
+		
+var defaultDraftFolder = '';
+
 function cRichTextEditor(){
+	this.emwindow   = new Array;
 	this.editor;
 	this.table;
 	this.id;
 	this.buildEditor();
+	this.saveFlag = 0;
 }
 
@@ -151,5 +162,5 @@
 	
 	var buttons = ['bold', 'italic', 'underline', 'forecolor', 'justifyleft', 'justifycenter', 'justifyright', 'justifyfull',
-				   'undo', 'redo', 'insertorderedlist', 'insertunorderedlist', 'outdent', 'indent', 'link', 'signature'];
+				   'undo', 'redo', 'insertorderedlist', 'insertunorderedlist', 'outdent', 'indent', 'link', 'signature', 'image', 'table'];
 
 	for (var i=0; i<buttons.length; i++){
@@ -166,4 +177,8 @@
 		else if (buttons[i] == 'link')
 			img.onclick = function () {RichTextEditor.createLink();};
+		else if (buttons[i] == 'image')
+			img.onclick = function () {RichTextEditor.createImage();};
+		else if (buttons[i] == 'table')
+			img.onclick = function () {RichTextEditor.createTable();};
 		else
 			img.onclick = function () {RichTextEditor.editorCommand(this.id,'');};
@@ -181,6 +196,6 @@
 
 cRichTextEditor.prototype.editorCommand = function(command, option) {
-	var mainField = document.getElementById(this.editor).contentWindow;
 	try {
+		var mainField = document.getElementById(this.editor).contentWindow;
 		mainField.focus();
 		if (command == 'signature'){
@@ -198,8 +213,23 @@
 			}
 		}
-		else
-			mainField.document.execCommand(command, false, option);
+		else if (command == 'CreateLink')
+			mainField.document.execCommand('CreateLink', false, option);
+		else if (command == 'Table'){
+			if (is_ie){
+				var sel = document.selection;
+				if (sel!=null)
+				{
+				    var rng = sel.createRange();
+				    if (rng!=null)
+			        rng.pasteHTML(option);
+				}
+			}
+			else 
+				mainField.document.execCommand('inserthtml', false, option);
+			}
+		else // command == Image
+			mainField.document.execCommand('InsertImage', false, option);
 		//mainField.focus();
-    } catch (e) {}
+    } catch (e) { alert(e.description); }
 }
 
@@ -222,4 +252,172 @@
 		this.editorCommand("CreateLink", szURL);
 	}
+}
+
+// It include the image file in emails body
+// It saves and attach in drafts folder and open it
+cRichTextEditor.prototype.addInputFile = function()
+{
+	var id = this.editor.substr(5); // border_id
+	divFiles = document.getElementById("divFiles_"+id);
+	var countDivFiles = divFiles.childNodes.length + 1;
+
+	var inputFile = document.getElementById('inputFile_img');
+	var divFiles = document.getElementById('divFiles_'+id);
+	inputFile.id = 'inputFile_'+id +"_"+countDivFiles;
+	inputFile.name = 'file_'+countDivFiles;
+	divFile = document.createElement('DIV');
+	divFile.appendChild(inputFile);
+	divFiles.appendChild(divFile);
+
+	var form_upload = document.getElementById('form_upload');
+	form_upload.parentNode.removeChild(form_upload);
+	win.close();
+	
+	if (! validateFileExtension(inputFile.value, inputFile.id , id))
+	return false;
+
+	RichTextEditor.saveFlag = 0; // See if save function finished
+	save_msg(id);
+	setTimeout("RichTextEditor.insertImgHtml("+id+")",1000);
+}
+
+cRichTextEditor.prototype.insertImgHtml = function (id){
+	if (RichTextEditor.saveFlag == 0)
+		setTimeout("RichTextEditor.insertImgHtml("+id+")",500);
+	else
+			this.editorCommand('Image', './inc/show_embedded_attach.php?msg_folder=INBOX/'+draftsfolder+'&msg_num='+newMessageTab.imapUid[id]+'&msg_part='+(newMessageTab.countFile[id]+1));
+			// this.editorCommand('Image', '.inc/gotodownload.php?msg_folder="+msg_folder+"&msg_number="+msg_number+"&idx_file="+idx_file+"&msg_part="+msg_part+params'));
+}
+
+cRichTextEditor.prototype.insertTableHtml = function (){
+	var id = this.editor.substr(5); // border_id
+	var	rows = document.getElementById('rows').selectedIndex;
+	var	cols = document.getElementById('cols').selectedIndex;
+	var insertTable = '<table border="1px"><tbody>';
+	for (var i = 0; i <= rows; i++){
+		insertTable += "<tr>";	
+		for (var j = 0; j <= cols; j++)
+			insertTable += "<td>&nbsp;</td>";	
+		insertTable += "</tr>";
+	}
+	insertTable += "</tbody></table>";
+	this.editorCommand('Table', insertTable);
+}
+
+cRichTextEditor.prototype.createTable = function(){
+	var form = document.getElementById("table_window");
+	if (form == null){
+		form = document.createElement("DIV");
+		form.id  = "table_window";
+		form.style.visibility = "hidden";
+		form.style.position = "absolute";
+		form.style.background = "#eeeeee";
+		form.style.left = "0px";
+		form.style.top  = "0px"; 
+		form.style.width = "0px";
+		form.style.height = "0px";
+		document.body.appendChild(form);
+	}
+		
+		var form_table = document.createElement("DIV");
+		form_table.id = "form_table";
+		form_table.style.position = "absolute";
+		form_table.style.top = "5px";
+		form_table.style.left = "5px";
+		form_table.style.width = "170px";
+		form_table.style.height = "120px";
+		form_table.name = get_lang("Insert Table");
+		form_table.innerHTML = get_lang('Select the table size')+':<br><br>'+
+								get_lang('Rows')+':&nbsp;&nbsp;&nbsp;'+	get_lang('Cols')+':<br>'+
+									'<select name="rows" id="rows"><option value="1">1</option>'+
+									'<option value="2">2</option>'+
+									'<option value="3">3</option>'+
+									'<option value="4">4</option>'+
+									'<option value="5">5</option>'+
+									'<option value="6">6</option>'+
+									'<option value="7">7</option>'+
+									'<option value="8">8</option>'+
+									'<option value="9">9</option></select>&nbsp;&nbsp;&nbsp;'+
+									'<select name="cols" id="cols"><option value="1">1</option>'+
+									'<option value="2">2</option>'+
+									'<option value="3">3</option>'+
+									'<option value="4">4</option>'+
+									'<option value="5">5</option>'+
+									'<option value="6">6</option>'+
+									'<option value="7">7</option>'+
+									'<option value="8">8</option>'+
+									'<option value="9">9</option></select><br><br>'+
+  								'<input title="Close"  value="' + get_lang('Close') + '" type="button" onclick="win.close()">&nbsp;'+
+								'<input title="Include"  value="' + get_lang('Include') + '" type="button" onclick="RichTextEditor.insertTableHtml();win.close();">';
+		form.appendChild(form_table);
+		
+		this.showWindow(form);
+		}
+
+cRichTextEditor.prototype.createImage = function(){
+	var form = document.getElementById("attachment_window");
+	if (form == null){
+		form = document.createElement("DIV");
+		form.id  = "attachment_window";
+		form.style.visibility = "hidden";
+		form.style.position = "absolute";
+		form.style.background = "#eeeeee";
+		form.style.left = "0px";
+		form.style.top  = "0px"; 
+		form.style.width = "0px";
+		form.style.height = "0px";
+		document.body.appendChild(form);
+	}
+		
+		var form_upload = document.createElement("DIV");
+		form_upload.id = "form_upload";
+		form_upload.style.position = "absolute";
+		form_upload.style.top = "5px";
+		form_upload.style.left = "5px";
+		form_upload.name = get_lang("Upload File");
+		form_upload.style.width = "450px";
+		form_upload.style.height = "75px";
+		form_upload.innerHTML = get_lang('Select the desired image file')+':<br>'+
+								'<input name="image_at" maxlength="255" size="50" id="inputFile_img" type="file"><br>' +
+								'<input title="Include"  value="' + get_lang('Include') + '"' + 'type="button" onclick="RichTextEditor.addInputFile();">&nbsp;' +
+								'<input title="Close"  value="' + get_lang('Close') + '"' +
+								' type="button" onclick="win.close()">';
+		form.appendChild(form_upload);
+		
+		this.showWindow(form);
+}
+cRichTextEditor.prototype.showWindow = function (div){
+
+		if(! div) {
+			return;
+		}
+		
+		if(! this.emwindow[div.id]) {
+			div.style.width  =  div.firstChild.style.width;
+			div.style.height = div.firstChild.style.height;
+			div.style.zIndex = "10000";			
+			var title = div.firstChild.name;
+			var wHeight = div.offsetHeight + "px";
+			var wWidth =  div.offsetWidth   + "px";
+			div.style.width = div.offsetWidth - 5;
+
+			win = new dJSWin({
+				id: 'win_'+div.id,
+				content_id: div.id,
+				width: wWidth,
+				height: wHeight,
+				title_color: '#3978d6',
+				bg_color: '#eee',
+				title: title,
+				title_text_color: 'white',
+				button_x_img: '../phpgwapi/images/winclose.gif',
+				border: true });
+			
+			this.emwindow[div.id] = win;
+			win.draw();
+		}
+		else
+			win = this.emwindow[div.id];
+		win.open();	
 }
 
Index: trunk/expressoMail1_2/js/preferences.js
===================================================================
--- trunk/expressoMail1_2/js/preferences.js	(revision 244)
+++ trunk/expressoMail1_2/js/preferences.js	(revision 271)
@@ -73,4 +73,6 @@
 				"<option value='15'>"+get_lang("Big")+"</option>"+
 				"</select></td></tr>"+
+				"<tr><td> - " + get_lang("Auto save draft") + "?</td>"+
+				"<td><input type='checkBox' id='auto_save_draft' name='auto_save_draft'></td></tr>"+
 				"<tr><td> - " + get_lang("Use shortcuts?") + "</td>"+
 				"<td><input type='checkBox' id='use_shortcuts' name='use_shortcuts'></td></tr>"+
@@ -131,5 +133,6 @@
 			txt_signature.value = preferences.signature;
 		
-		if( preferences.use_shortcuts == 1){Element("use_shortcuts").checked = true;}
+		if( preferences.use_shortcuts == 1) Element("use_shortcuts").checked = true;
+		if( preferences.auto_save_draft == 1) Element("auto_save_draft").checked = true;
 	}
 	
@@ -152,4 +155,6 @@
 			reload_page = true;
 	    else if(preferences.use_shortcuts != Element("use_shortcuts").checked)
+			reload_page = true;
+	    else if(preferences.auto_save_draft != Element("auto_save_draft").checked)
 			reload_page = true;
 				
@@ -166,4 +171,6 @@
 		preferences.signature = Element("txt_signature").value;
 		preferences.use_shortcuts = Element("use_shortcuts").checked ? 1 : 0;
+		preferences.auto_save_draft = Element("auto_save_draft").checked ? 1 : 0;
+		
 		var _this = this;
 		
@@ -217,5 +224,5 @@
 		
 		if(! this.prefeW[div.id]) {
-			div.style.height = is_ie ? "355px" : "365px";
+			div.style.height = is_ie ? "385px" : "370px";
 			div.style.width = "510px";
 			div.style.visibility = "hidden";
