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
