Index: trunk/expressoMail1_2/js/main.js
===================================================================
--- trunk/expressoMail1_2/js/main.js	(revision 5588)
+++ trunk/expressoMail1_2/js/main.js	(revision 5604)
@@ -277,5 +277,5 @@
 			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_message_options_"+border_id).button({ disabled: false });
+			$("#save_message_options_"+border_id).button({disabled: false});
 			$(".header-button").button();
 		};
@@ -580,5 +580,5 @@
 								return $(DataLayer.render('../prototype/modules/mail/templates/draggin_box.ejs', {texto : $(this).find(".td_msg_subject").text(), type: "messages"}));
 						},
-						cursorAt: { cursor: "move", top: 5, left: 56 },
+						cursorAt: {cursor: "move", top: 5, left: 56},
 						refreshPositions: true ,
 						scroll: true, 
@@ -1805,6 +1805,4 @@
 		case "edit":
 			openTab.imapBox[new_border_ID] = folder_message.value;
-			//openTab.toPreserve[new_border_ID] = true;
-			openTab.imapUid[new_border_ID] = parseInt(border_ID.substr(0,border_ID.indexOf("_")));
 			document.getElementById('font_border_id_'+new_border_ID).innerHTML = data.subject;
 			title = "Edição: "+data.subject;
@@ -2073,7 +2071,5 @@
 			}
 		}
-		if ((! openTab.toPreserve[ID]) && (openTab.imapUid[ID] != 0))
-			cExecute ("$this.imap_functions.delete_msgs&folder="+openTab.imapBox[ID]+"&msgs_number="+openTab.imapUid[ID],function(data){return});
-		delete_border(ID,'true'); // Becarefull: email saved automatically should be deleted. delete_border erase information about openTab
+		delete_border(ID,'true'); 
  	}
 	else{
@@ -2087,5 +2083,5 @@
 		var save_link = Element("save_message_options_"+ID);
 		save_link.onclick = function onclick(event) {openTab.toPreserve[ID] = true;save_msg(ID);} ;
-		$("#save_message_options_"+ID).button({ disabled: false });
+		$("#save_message_options_"+ID).button({disabled: false});
 		//save_link.className = 'message_options';
 	}
@@ -2173,5 +2169,4 @@
 	save_link.onclick = '';
 	$("#save_message_options_"+ID).button({ disabled: true });
-	//save_link.className = 'message_options_inactive';
 
 	ID_tmp = ID;
@@ -2184,22 +2179,5 @@
 	mail_as_plain = ( mail_as_plain ) ? mail_as_plain.checked : false;
 
-       var content_body  = RichTextEditor.getData('body_'+ID);
-       //Remove imagens do corpo que estao com o checkbox desmarcados
-       var files_checkbox =  $("#divFiles_"+ID+" input:checkbox");
-       var files_unchecked = new Array();
-       for (var i = 0; i < files_checkbox.length; i++)
-            if(files_checkbox[i].checked !== true)
-                files_unchecked.push(connector.unserialize(unescape(trim(files_checkbox[i].value))));
-
-       var imagens = content_body.match(/<img[^>]*>/g);
-       
-       if(imagens != null)
-            for (var x = 0; x < imagens.length; x++)
-                for (var xx = 0; xx < files_unchecked.length; xx++)
-                    if(imagens[x].indexOf('indexPart='+files_unchecked[xx][3].replace(/'/g,'')) !== -1)
-                         content_body = content_body.replace(imagens[x],'');
-                
-        //--------------------------------------------------------------------------//
-        
+       var content_body  = RichTextEditor.getData('body_'+ID);        
 	var textArea = document.createElement("TEXTAREA");
 	textArea.style.display='none';
@@ -2210,9 +2188,20 @@
 	input_folder.style.display='none';
 	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 = openTab.imapUid[ID];
+	input_folder.value = folder;      
+        
+        var uids = document.createElement("INPUT");
+	uids.style.display='none';
+	uids.name = "uids_save";
+	uids.value = uidsSave[ID].toString();
+        
+        var save_folder = document.createElement("INPUT");
+	save_folder.style.display='none';
+	save_folder.name = "save_folder";
+	save_folder.value = (openTab.imapBox[ID] && openTab.type[ID] < 6) ? openTab.imapBox[ID]: "INBOX" + cyrus_delimiter + draftsfolder;
+        
+        var msg_attachments = document.createElement("INPUT");
+	msg_attachments.style.display='none';
+	msg_attachments.name = "attachments";
+	msg_attachments.value = listAttachment(ID);
 
 	if (is_ie){
@@ -2239,6 +2228,4 @@
 	form.appendChild(textArea);
 	form.appendChild(input_folder);
-	form.appendChild(msg_id);
-
 
 	// Implementação do In_Reply_To e References
@@ -2248,13 +2235,10 @@
 	msgId.value = currentTab;
 
-	var msgFolder = document.createElement("INPUT");
-	msgFolder.style.display = 'none';
-	msgFolder.name = 'messageFolder';
-	msgFolder.value = openTab.imapBox[currentTab];
+
 
 	form.appendChild(msgId);
-	form.appendChild(msgFolder);
-
-
+        form.appendChild(save_folder);
+        form.appendChild(uids);
+	form.appendChild(msg_attachments);
 
     var mail_type = document.createElement('input');
@@ -2350,164 +2334,4 @@
 }
 
-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);RichTextEditor.execPosInstance('body_'+border_id);};
-	
-	
-        if(data === null)
-	{
-            write_msg(get_lang('ERROR saving your message.'));
-            return null;
-        }
-	if(typeof(data.append)  == 'undefined')
-		data.append = false;
-		
-	if(data.append === "Over quota"){
-		write_msg(get_lang('ERROR saving your message over quota.'));
-		return;
-	}
-	else if ( data.append !== true )
-	{
-		if (data.append == null)
-                    write_msg(get_lang('ERROR saving your message.'));
-		else
-		{
-			if (data.append.match(/^(.*)TRYCREATE(.*)$/))
-			{
-				connector.loadScript('TreeS');
-				alert(get_lang('There is not %1 folder, Expresso is creating it for you... Please, repeat your request later.',draftsfolder));
-				connector.loadScript('TreeShow');
-				ttree.FOLDER = 'root';
-				ttreeBox.new_past(draftsfolder);
-				setTimeout('save_msg('+border_id+')',3000);
-			}
-			else
-				write_msg(data.append);
-		}
-	}
-	else
-	{
-		var newImage = false;
-		RichTextEditor.saveFlag = 1;
-		openTab.imapUid[border_id] = data.msg_no;
-		openTab.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;                 
-                Element('border_id_'+border_id).title = newTitle; 
-
-		// Replace the embedded images for new uids
-		var content_body  = RichTextEditor.getData('body_'+border_id);
-		var body_images   = content_body.match(/msgNumber=\d*/g);
-		var folder_images = content_body.match(/msgFolder=[^&]*&/g); 		
-
-                if (body_images != null)
-                {
-                    for (var i=0; i<body_images.length; i++)
-                    {
-                        if( folder_images != null)
-                          content_body = content_body.replace(folder_images[i], "msgFolder=INBOX" + cyrus_delimiter + draftsfolder +"&");
-
-
-                            
-                        
-                        content_body = content_body.replace(body_images[i],"msgNumber="+openTab.imapUid[border_id]);
-                    }
-
-                    var images_part   = content_body.match(/indexPart=[0-9.]*/g);
-
-                    if(images_part != null)
-                                {
-                        for (var x = 0; x < images_part.length; x++)
-                                {
-                            var position = images_part[x].substr(10,images_part[x].length);
-                            content_body = content_body.replace(images_part[x],'indexPart(||.|||.||)='+data.imagesParts[position]);
-                                }
-
-                        }
-               	    content_body = content_body.replace(/indexPart\(\|\|\.\|\|\|\.\|\|\)=/g,'indexPart=');             
-                    }
-
-                   
-
-		//Replace all files to new files
-		var divFiles = Element("divFiles_"+border_id);
-		divFiles.innerHTML = '';
-
-
-		var attach_files = connector.unserialize(data.files);
-		if (attach_files != null) {
-                    openTab.countFile[border_id] = attach_files.length;
-                    for (var att_index = 0; att_index < attach_files.length; att_index++){
-
-                        var link_attachment = document.createElement("a");
-
-                        var fileName = Base64.decode(attach_files[att_index].name);
-                        var fileSize = attach_files[att_index].size / 1024;
-                        
-                        
-                        link_attachment.innerHTML = fileName + " ("+parseInt(fileSize)+" kb)";
-						
-						var encoding = /\.eml$/.exec(fileName)? '7bit' : 'base64';
-                        var href = "'"+folder_id+"','"+data.msg_no+"','"+(att_index)+"','0."+(att_index+1)+"','"+encoding+"'";
-
-                        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;
-                        check_attachment.setAttribute("checked", "checked");
-                    }
-					} 
- 		                 
- 		                if(RichTextEditor.newImageId !== false) 
- 		                { 
- 		                    var rex = new RegExp('<img id="'+RichTextEditor.newImageId+'" src="" [^\/>]*\/>', 'i'); 
- 		                    var newImg = '<img src="./inc/get_archive.php?msgFolder=INBOX'+cyrus_delimiter+draftsfolder+'&msgNumber='+openTab.imapUid[border_id]+'&indexPart=0.'+(openTab.countFile[border_id])+'" />'; 
- 		                    content_body = content_body.replace(rex,newImg); 
- 		 
- 		                    RichTextEditor.newImageID = false;                     
- 		                    newImage = true; 
- 		                } 
- 		                 
- 		                if(body_images != null || newImage === true) 
- 		                {  
- 		                     RichTextEditor.setData('body_'+border_id,content_body); 
-                }
-
-		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" + cyrus_delimiter + draftsfolder)
-                    {
-                        //Update mailbox
-                        var tr_msg = document.getElementById(message_id);
-                        change_tr_properties(tr_msg, data.msg_no, data.subject);
-                    }
-
-	}
-                setTimeout( function(){RichTextEditor.saveFlag = 1;RichTextEditor.execPosInstance('body_'+border_id);}, 1000 );
-	}
-        return null;
-}
 
 function autoSave( ID )
@@ -2517,198 +2341,23 @@
 }
 
-function save_msg(border_id,withImage, out){
+function save_msg(border_id){
        
-       //seta o status do auto_save = true
-        if (preferences.auto_save_draft == 1)
-            autoSaveControl.status[border_id] = true;
-       ///////////////////////////////////////////
-      
-	if (typeof(withImage) == 'undefined')
-		withImage = false;
-
-	var sendButton = Element("send_button_"+border_id);
-	if (sendButton)
-		sendButton.style.visibility="hidden";
-
-	if (openTab.imapBox[border_id] && openTab.type[border_id] < 6) //Gets the imap folder
-		var folder_id = openTab.imapBox[border_id];
-	else
-		var folder_id = "INBOX" + cyrus_delimiter + 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 = openTab.imapUid[border_id];
-	var handler_save_msg = function(data){
-		if(typeof(out) == 'undefined'){
-			return_save(data,this.tmp_border_id,this.tmp_folder_name,this.tmp_folder_id,this.message_id);
-			var save_link = Element("save_message_options_"+border_id);
-			if(!withImage){
-				if(data.append === true){
-					$("#save_message_options_"+border_id).button();
-					$("#save_message_options_"+border_id).button({ disabled: true });
-					watch_changes_in_msg(border_id);	
-					write_msg(get_lang('Your message was save as draft in folder %1.', lang_folder(folder_name)));
-				}
-				if(auto){
-					auto = false;
-				}
-			}
-			else{
-				write_msg(get_lang('Wait a moment, your image is uploading ...'));
-				var auto = true;
-				setTimeout( function(){save_msg(border_id)}, 1000 );
-			}
-		}else{
-			if(data.append === true){
-				write_msg(get_lang('Your message was save as draft in folder %1.', lang_folder(folder_name)));
-			}else if(data.append == null){
-				write_msg(get_lang('ERROR saving your message.'));
-			}if(data.append === "Over quota"){
-				write_msg(get_lang('ERROR saving your message over quota.'));
-			}
-		}
-		
-	}
-
-        var mail_as_plain = document.getElementById( 'textplain_rt_checkbox_' + border_id );
-	mail_as_plain = ( mail_as_plain ) ? mail_as_plain.checked : false;
-
-       var content_body  = RichTextEditor.getData("body_"+border_id);
-       //Remove imagens do corpo que estao com o checkbox desmarcados
-       var files_checkbox =  $("#divFiles_"+border_id+" input:checkbox");
-       var files_unchecked = new Array();
-       for (var i = 0; i < files_checkbox.length; i++)
-            if(files_checkbox[i].checked !== true)
-                files_unchecked.push(connector.unserialize(unescape(trim(files_checkbox[i].value))));
-
-       var imagens = content_body.match(/<img[^>]*>/g);
-       if(imagens != null)
-            for (var x = 0; x < imagens.length; x++)
-                for (var xx = 0; xx < files_unchecked.length; xx++)
-                    if(imagens[x].indexOf('indexPart='+files_unchecked[xx][3].replace(/'/g,'')) !== -1)
-                         content_body = content_body.replace(imagens[x],'');
-        
-        if(files_unchecked.length > 0)
-            RichTextEditor.setData("body_"+border_id,content_body);
-            
-        
-	var textArea = document.createElement("TEXTAREA");
-	textArea.style.display='none';
-	textArea.name = "body";
-	textArea.value = content_body;
-	var input_folder = document.createElement("INPUT");
-	input_folder.style.display='none';
-	input_folder.name = "folder";
-	input_folder.value = folder_id;
-	var input_msgid = document.createElement("INPUT");
-	input_msgid.style.display='none';
-	input_msgid.name = "msg_id";
-	input_msgid.value = message_id;
-	var input_insertImg = document.createElement("INPUT");
-	input_insertImg.style.display='none';
-	input_insertImg.name = "insertImg";
-	input_insertImg.value = withImage;
-
-
-	if (is_ie)
+     //seta o status do auto_save = true
+    if (preferences.auto_save_draft == 1)
+        autoSaveControl.status[border_id] = true;
+    ///////////////////////////////////////////
+    
+    var idJavascript = DataLayer.put('message',DataLayer.form("#form_message_"+border_id));    
+    uidsSave[border_id] = [];
+    DataLayer.commit(false,false,function(data){
+        if(data != null && data['message://'+idJavascript].id !== undefined )
         {
-		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);
-	form.appendChild(input_msgid);
-	form.appendChild(input_insertImg);
-
-        var mail_type;
-        
-        if (is_ie)
-        {
-            mail_type = document.createElement('<input type="hidden" />');
+            uidsSave[border_id].push(data['message://'+idJavascript].id);
+             write_msg('Mensagem salva com sucesso!');
         }
         else
-            {
-                mail_type = document.createElement('input');
-                mail_type.setAttribute('type', 'hidden');
-            }
-        mail_type.setAttribute('name', 'type');
-        var mail_type_value = ( mail_as_plain ) ? 'plain' : 'html';
-	mail_type.setAttribute("value", mail_type_value);
-
-	form.parentNode.appendChild(mail_type);
-
-	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(get_lang('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 content_body  = RichTextEditor.getData("body_"+border_id);
-       //Remove imagens do corpo que estao com o checkbox desmarcados
-       var files_checkbox =  $("#divFiles_"+border_id+" input:checkbox");
-       var files_unchecked = new Array();
-       for (var i = 0; i < files_checkbox.length; i++)
-            if(files_checkbox[i].checked !== true)
-                files_unchecked.push(connector.unserialize(unescape(trim(files_checkbox[i].value))));
-
-       var imagens = content_body.match(/<img[^>]*>/g);
-       
-       if(imagens != null)
-            for (var x = 0; x < imagens.length; x++)
-                for (var xx = 0; xx < files_unchecked.length; xx++)
-                    if(imagens[x].indexOf('indexPart='+files_unchecked[xx][3].replace(/'/g,'')) !== -1)
-                         content_body = content_body.replace(imagens[x],'');
-        
-	var textArea = document.createElement("TEXTAREA");
-	textArea.style.display='none';
-	textArea.name = "body";
-	textArea.value = content_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);
+            write_msg('Erro ao salvar sua menssagem!');
+    });
+
 }
 
@@ -3874,2 +3523,43 @@
 		} 
 
+
+DataLayer.codec( "message", "detail", {
+  
+	decoder:function( form ){
+            var border_id = form.abaID;  
+            ///Dfininindo pasta a ser salva mensagem
+            var folder_id = (openTab.imapBox[border_id] && openTab.type[border_id] < 6) ? openTab.imapBox[border_id]: "INBOX" + cyrus_delimiter + draftsfolder;
+            form.folder = folder_id;
+            form.body = RichTextEditor.getData("body_"+border_id);
+            form.attachments = listAttachment(border_id);
+            form.uidsSave = uidsSave[border_id].toString();
+            return( form );
+      
+	},
+
+	encoder:function( pref ){
+              
+		return( pref );
+	}
+
+});
+
+DataLayer.codec( "mailAttachment", "detail", {
+  
+	decoder: function(evtObj){
+	
+		if( notArray = $.type(evtObj) !== "array" )
+			evtObj = [ evtObj ];
+
+		var res = $.map(evtObj, function( form){
+			return [$.map(form.files , function( files){
+					return { source: files , disposition : form['attDisposition'+form.abaID]};
+				})];
+		});
+	return notArray ? res[0] : res;
+	},
+      
+	encoder: function(){}
+
+      
+});
