Index: sandbox/2.2.0.2/expressoMail1_2/inc/class.imap_functions.inc.php
===================================================================
--- sandbox/2.2.0.2/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 4291)
+++ sandbox/2.2.0.2/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 4413)
@@ -2739,5 +2739,5 @@
 		$msg_uid = $params['msg_id'];
 		$body = $params['body'];
-		$body = str_replace("%nbsp;","&nbsp;",$params['body']);
+		$body = str_replace("%nbsp;","&nbsp;", $body);
 		$body = preg_replace("/\n/"," ",$body);
 		$body = preg_replace("/\r/","",$body);
Index: sandbox/2.2.0.2/expressoMail1_2/js/common_functions.js
===================================================================
--- sandbox/2.2.0.2/expressoMail1_2/js/common_functions.js	(revision 4062)
+++ sandbox/2.2.0.2/expressoMail1_2/js/common_functions.js	(revision 4413)
@@ -336,23 +336,35 @@
 		var countDivFiles = 1;
 
-	divFile = document.createElement('DIV');
-
-
-	divFile.innerHTML = "<input type='file' id_border='"+id_border+"' size='50' maxLength='255' onchange=\"function () {validateFileExtension(this.value, this.id.replace('input','div'), this.getAttribute('id_border'));};\" id='"+"inputFile_"+id_border+"_"+countDivFiles+"' name='file_"+countDivFiles+"'>";
-
-
-	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")); return false;};
-	linkFile.innerHTML=get_lang("Remove");
-	//divFile.innerHTML += "&nbsp;&nbsp;";
+	divFile = document.createElement('div'); 
+
+	var inputFile = document.createElement("input"); 
+	inputFile.id        = "inputFile_"+id_border+"_"+countDivFiles; 
+	inputFile.name      = "file_"+countDivFiles; 
+	inputFile.type      = "file"; 
+	inputFile.size      = 50; 
+	inputFile.maxlength = 255; 
+	inputFile.onchange  = function () { 
+		validateFileExtension(this.value, this.id.replace('input','div'), this.getAttribute('id_border')); 
+		openTab.autosave_timer[id_border] = setTimeout("save_msg("+id_border+")", autosave_time); 
+	}; 
+	
+	inputFile.onfocus = function () {	
+		if (openTab.autosave_timer[id_border]) 
+			clearTimeout(openTab.autosave_timer[id_border]); 
+	}; 
+	
+	divFile.appendChild(inputFile); 
+
+	var linkFile = document.createElement("a"); 
+	linkFile.id        = "linkFile_"+id_border+"_"+countDivFiles; 
+	linkFile.href      = 'javascript:void(0)'; 
+	linkFile.onclick   = function () {removeFile("divFile_"+id_border+"_"+countDivFiles); return false;}; 
+	linkFile.innerHTML = get_lang("Remove");
+	
 	divFile.appendChild(linkFile);
 	divFile.id = "divFile_"+id_border+"_"+countDivFiles;
 	divFiles.appendChild(divFile);
 
-
-
-	return document.getElementById("inputFile_"+id_border+"_"+countDivFiles);
+	return inputFile;
 }
 //	Remove Input File Dynamically.
Index: sandbox/2.2.0.2/expressoMail1_2/js/main.js
===================================================================
--- sandbox/2.2.0.2/expressoMail1_2/js/main.js	(revision 4364)
+++ sandbox/2.2.0.2/expressoMail1_2/js/main.js	(revision 4413)
@@ -3,5 +3,5 @@
 // tempo do auto save (em milisegundos)
 // 20000 = 20 segundos
-var autosave_time = 20000;
+var autosave_time = 5000;
 
 var cabecalho = '<h4>ExpressoLivre - ExpressoMail</h4>'; 
@@ -1745,5 +1745,7 @@
 	}
 	else
+	{
 		cExecuteForm("$this.imap_functions.send_mail", form, handler_send_message, ID);
+	}
 }
 function change_tr_properties(tr_element, newUid, newSubject){
@@ -1765,17 +1767,14 @@
 }
 
-function return_save(data,border_id,folder_name,folder_id,message_id)
-{
+function return_save(data,border_id,folder_name,folder_id,message_id){
 	Element("send_button_"+border_id).style.visibility="visible";
-	var handler_delete_msg = function(data){ refresh(preferences.alert_new_msg); };
-
-	if (data.append != true || !data)
+	var handler_delete_msg = function(data){refresh(preferences.alert_new_msg);};
+
+	if (data == null || data.append != true )
 	{
-		RichTextEditor.saveFlag = -1;
+
+                RichTextEditor.saveFlag = -1;
 		if (! data.append)
-			if(data == 'Post-Content-Length')
-				write_msg(get_lang('The size of this message has exceeded  the limit (%1B).', preferences.max_attachment_size ? preferences.max_attachment_size : Element('upload_max_filesize').value));
-			else
-				write_msg(get_lang('ERROR saving your message.'));
+			write_msg('ERROR saving your message.');
 		else
 		{
@@ -1798,118 +1797,94 @@
 		openTab.imapUid[border_id] = data.msg_no;
 		openTab.imapBox[border_id] = data.folder_id;
-
+		
 		var newTitle = document.getElementById('subject_'+border_id).value;
-		if (newTitle == '')
+		if (newTitle.length > 18)
+			newTitle = newTitle.substr(18) + '...';
+		else if (newTitle == '')
 			newTitle = get_lang("No subject");
-		set_border_caption('border_id_'+border_id, newTitle);
-
+		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);
-		var images_part = content_body.match(/msg_part=\d*/g);
-		if (body_images)
+		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);
+		var folder_images = content_body.match(/msg_folder=[^&]*&/g); 		
+		
+		if (body_images != null)
 		{
-			for (var i=0; i<body_images.length; i++){
-				content_body = 	content_body.replace(body_images[i],"msg_num="+openTab.imapUid[border_id]);
-			}
-			var allImgs = new Array (images_part.length);
-				var j=-1;
-				for (var i in images_part){
-
-					if (is_ie)
-						if (i == 0)
-							var image_number = parseInt(images_part[i].substr(9));
-						else
-							image_number = "null";
-					else
-						var image_number = parseInt(images_part[i].substr(9));
-
-				if (! isNaN(image_number))
-					{
-						if (! allImgs[image_number])
-						{
-							allImgs[image_number] = true;
-							j--;
-						}
-					content_body = content_body.replace(images_part[i],'msg_part='+j);
-					}
-				}
-			content_body = content_body.replace(/msg_part=-/g,'msg_part=');
-
-			mainField.document.getElementsByTagName('body').item(0).innerHTML = content_body;
-		}
-
+                    for (var i=0; i<body_images.length; i++)
+                    {
+                        if( folder_images != null)
+                           content_body = content_body.replace(folder_images[i], "msg_folder=INBOX" + cyrus_delimiter + draftsfolder +"&");
+
+                        content_body = content_body.replace(body_images[i],"msg_num="+openTab.imapUid[border_id]);
+                    }
+
+                    var images_part   = content_body.match(/msg_part=[0-9.]*/g);
+
+                    if(images_part != null)
+                    {
+                        for (var x = 0; x < images_part.length; x++)
+                        {
+                            var position = images_part[x].substr(9,images_part[x].length);
+                            content_body = content_body.replace(images_part[x],'msg_part(||.|||.||)='+data.imagesParts[position]);
+                        }
+
+                    }
+               	    content_body = content_body.replace(/msg_part\(\|\|\.\|\|\|\.\|\|\)=/g,'msg_part=');
+
+                    mainField.document.getElementsByTagName('body').item(0).innerHTML = content_body;
+                 }       
+		
+
+	
 		//Replace all files to new files
 		var divFiles = Element("divFiles_"+border_id);
-		elFiles = divFiles.getElementsByTagName("input");
-		var countCheck =0;
-		for (var i=0; i<elFiles.length; i++) {
-			if(elFiles[i].value !=""){
-				if (elFiles[i].type == "checkbox") {
-					countCheck++;
-				}
-				else {
-					elFiles[i].value ="";
-					parantNodeFile = elFiles[i].parentNode.parentNode;
-					parantNodeFile.removeChild(elFiles[i].parentNode);
-					i--;
-				}
-
-			}
-		};
-
+		divFiles.innerHTML = '';
+		
 		var attach_files = connector.unserialize(data.files);
 		if (attach_files != null) {
 			openTab.countFile[border_id] = attach_files.length;
-			att_index = countCheck;
-		for (att_index; 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 + " ("+borkb((parseInt(fileSize)*1024))+")";
-
-			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 = trim(s_tmp);
-
-                        if (!divFiles.childNodes[0])
-                        {
-                            divFiles.appendChild(document.createElement("BR"));
-                        }
-                        else
-                            {
-                                divFiles.insertBefore(document.createElement("BR"),divFiles.childNodes[0]);
-                            }
-
-                        divFiles.insertBefore(link_attachment,divFiles.childNodes[0]);
-                        divFiles.insertBefore(check_attachment,divFiles.childNodes[0]);
-
-			check_attachment.checked = true;
-		}
-	}
+			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].lastIndexOf('_SIZE_'));
+				var fileSize = parseInt(attach_files[att_index].substr(attach_files[att_index].lastIndexOf('_SIZE_')+6))/1024
+				link_attachment.innerHTML = fileName + " ("+parseInt(fileSize)+" kb)";
+
+				var href = "'"+folder_id+"','"+data.msg_no+"','"+(att_index)+"','0."+(att_index+1)+"','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;
+				
+				check_attachment.checked = true;
+				check_attachment.setAttribute("checked", "checked");
+				
+				divFiles.appendChild(check_attachment);
+				divFiles.appendChild(link_attachment);
+				divFiles.appendChild(document.createElement("br"));
+			}			
+		}
+		
+		
 		if (message_id)
 		{
 			cExecute ("$this.imap_functions.delete_msgs&folder="+openTab.imapBox[border_id]+"&msgs_number="+message_id,handler_delete_msg);
-			if (openTab.imapBox[0] == "INBOX/"+draftsfolder)
+			if (openTab.imapBox[0] == "INBOX" + cyrus_delimiter + draftsfolder)
 			{
 				//Update mailbox
-						var tr_msg = document.getElementById(message_id);
+				var tr_msg = document.getElementById(message_id);
 				change_tr_properties(tr_msg, data.msg_no, data.subject);
 			}
-		} else {
-			refresh();
-		}
-
+		
+		}
 		var save_link = Element("save_message_options_"+border_id);
 		save_link.onclick = '';
@@ -1917,5 +1892,5 @@
 		watch_changes_in_msg(border_id);
 		write_msg(get_lang('Your message was save as draft in folder %1.', lang_folder(folder_name)));
-	}
+	}		
 }
 
Index: sandbox/2.2.0.2/expressoMail1_2/js/draw_api.js
===================================================================
--- sandbox/2.2.0.2/expressoMail1_2/js/draw_api.js	(revision 4365)
+++ sandbox/2.2.0.2/expressoMail1_2/js/draw_api.js	(revision 4413)
@@ -3189,7 +3189,27 @@
 	}
 
+	/** 
+	* event to enable autosave_draft on file attachment 
+	*/ 
+	function save_onchange_attachment_handler () {
+		away = false; 
+		var save_link = Element("save_message_options_"+border_id); 
+		save_link.onclick = function onclick(event) { 
+		openTab.toPreserve[border_id] = true;  
+			save_msg(border_id); 
+		}; 
+		save_link.className = 'message_options'; 
+	}; 
+	
 	var add_files = document.createElement("A");
 	add_files.setAttribute("href", "javascript:void(0)");
-	add_files.onclick = function () { addFile(ID); return false;};
+ 	add_files.onclick = function () {
+		var obj = addFile(ID); 
+		if (preferences.auto_save_draft == 1 && obj) {
+			if ( obj.addEventListener ) 
+			obj.addEventListener('click', save_onchange_attachment_handler, false); 
+		} 
+		return false; 
+	}; 
 	add_files.innerHTML =  get_lang("Attachments: add+");
 	add_files.setAttribute("tabIndex","-1");
