Index: /trunk/expressoMail1_2/js/main.js
===================================================================
--- /trunk/expressoMail1_2/js/main.js	(revision 7638)
+++ /trunk/expressoMail1_2/js/main.js	(revision 7639)
@@ -2110,493 +2110,558 @@
 }
 
-function new_message(type, border_ID, flagged){
-	// if there's a reply already open, just switch to it
-	reply_tab_id = border_ID.replace('_r', '');
-	if (openTab.type[reply_tab_id] == tabTypes[type])
-	{
-		alternate_border(reply_tab_id);
-		resizeWindow();
-		return;
-	}
-
-		if(RichTextEditor.editorReady === false) return false; 
- 		             
- 	    RichTextEditor.editorReady = false; 
-
-		
-		if (Element('show_img_link_'+border_ID))
-		{
-			show_msg_img(border_ID.match(/^\d*/)[0], Element('input_folder_'+border_ID).value);
-		}
-        var new_border_ID = draw_new_message(border_ID.replace('_r',''));
-      	
-        if(typeof(openTab.type[new_border_ID]) != "undefined") {
-		if(tabTypes[type] == openTab.type[new_border_ID]) {
-                    if (type != 'edit')
+function new_message(type, border_ID, flagged)
+{
+    // if there's a reply already open, just switch to it
+    reply_tab_id = border_ID.replace('_r', '');
+    if (openTab.type[reply_tab_id] == tabTypes[type])
+    {
+        alternate_border(reply_tab_id);
+        resizeWindow();
+        return;
+    }
+
+    if (RichTextEditor.editorReady === false) return false;
+
+    RichTextEditor.editorReady = false;
+
+
+    if (Element('show_img_link_' + border_ID))
+    {
+        show_msg_img(border_ID.match(/^\d*/)[0], Element('input_folder_' + border_ID).value);
+    }
+    var new_border_ID = draw_new_message(border_ID.replace('_r', ''));
+
+    if (typeof (openTab.type[new_border_ID]) != "undefined")
+    {
+        if (tabTypes[type] == openTab.type[new_border_ID])
+        {
+            if (type != 'edit')
+            {
+                delete_border(currentTab);
+                new_border_ID = draw_new_message(border_ID);
+            }
+        }
+        else
+        {
+            var a_types = {
+                6: get_lang("Forward"),
+                7: get_lang("Reply"),
+                8: get_lang("Reply to all with history"),
+                9: get_lang("Reply without history"),
+                10: get_lang("Reply to all without history")
+            };
+            if (new_border_ID != 0) if (!confirm(get_lang("Your message to %1 has not been saved or sent. " +
+                "To %2 will be necessary open it again. Discard your message?",
+            a_types[openTab.type[new_border_ID]].toUpperCase(),
+            a_types[tabTypes[type]].toUpperCase())))
+            {
+                return new_border_ID;
+            }
+            else
+            {
+                delete_border(currentTab);
+                new_border_ID = draw_new_message(border_ID);
+            }
+        }
+    }
+    if (new_border_ID == 'maximo')
+    {
+        RichTextEditor.editorReady = true;
+        return false;
+    }
+    if (new_border_ID == false)
+    {
+        RichTextEditor.editorReady = true;
+        setTimeout('new_message(\'' + type + '\',\'' + border_ID + '\',\'' + flagged + '\');', 500);
+        return false;
+    }
+    openTab.type[new_border_ID] = tabTypes[type];
+
+    // Salva a pasta da mensagem respondida ou encaminhada:
+    var folder_message = Element("input_folder_" + border_ID);
+    if (folder_message)
+    {
+        var input_current_folder = document.createElement('input');
+        input_current_folder.id = "new_input_folder_" + border_ID;
+        input_current_folder.name = "input_folder";
+        input_current_folder.type = "hidden";
+        input_current_folder.value = folder_message.value;
+        Element("content_id_" + new_border_ID).appendChild(input_current_folder);
+    } //Fim.
+    var title = '';
+    data = [];
+
+
+    if (Element("from_" + border_ID))
+    {
+        if (document.getElementById("reply_to_" + border_ID))
+        {
+            data.to = document.getElementById("reply_to_values_" + border_ID).value;
+            data.to = data.to.replace(/&lt;/gi, "<");
+            data.to = data.to.replace(/&gt;/gi, ">");
+        }
+        else
+        {
+            if (document.getElementById("sender_values_" + border_ID))
+            {
+                data.to = document.getElementById("sender_values_" + border_ID).value;
+                data.to = data.to.replace(/&lt;/gi, "<");
+                data.to = data.to.replace(/&gt;/gi, ">");
+            }
+            else if (document.getElementById("from_values_" + border_ID))
+            {
+                data.to = document.getElementById("from_values_" + border_ID).value;
+                data.to = data.to.replace(/&lt;/gi, "<");
+                data.to = data.to.replace(/&gt;/gi, ">");
+            }
+        }
+        if (document.getElementById("to_values_" + border_ID))
+        {
+            data.to_all = document.getElementById("to_values_" + border_ID).value;
+            data.to_all_alternative = document.getElementById("user_email_alternative").value;
+            data.to_all = data.to_all.replace(/\n/gi, " ");
+            data.to_all = data.to_all.replace(/&lt;/gi, "<");
+            data.to_all = data.to_all.replace(/&gt;/gi, ">");
+            var _array_to_all = data.to_all.split(",");
+            var _array_to_alternative = data.to_all_alternative.split(",");
+        }
+    }
+    if (document.getElementById("cc_" + border_ID))
+    {
+        data.cc = document.getElementById("cc_values_" + border_ID).value;
+        data.cc = data.cc.replace(/&lt;/gi, "<");
+        data.cc = data.cc.replace(/&gt;/gi, ">");
+        var _array_cc = data.cc.split(",");
+    }
+    if (document.getElementById("cco_" + border_ID))
+    {
+        data.cco = document.getElementById("cco_values_" + border_ID).value;
+        data.cco = data.cco.replace(/&lt;/gi, "<");
+        data.cco = data.cco.replace(/&gt;/gi, ">");
+    }
+    if ($("#subject_" + border_ID)) data.subject = $("#subject_" + border_ID).text();
+    if (data.subject == get_lang("(no subject)   ")) data.subject = '';
+    if (document.getElementById("body_" + border_ID)) data.body = document.getElementById("body_" + border_ID).innerHTML;
+    if (document.getElementById("from_values_" + border_ID)) data.from = document.getElementById("from_values_" + border_ID).value;
+    if (Element('date_' + border_ID))
+    {
+        data.date = Element('date_' + border_ID).innerHTML;
+    }
+    if (Element('date_day_' + border_ID))
+    {
+        data.date_day = Element('date_day_' + border_ID).value;
+    }
+    if (Element('date_hour_' + border_ID))
+    {
+        data.date_hour = Element('date_hour_' + border_ID).value;
+    }
+
+    var signature = RichTextEditor.getSignatureDefault();
+
+    if (type != "new" && type != "edit" && document.getElementById("is_local_" + border_ID) != null) data.is_local_message = (document.getElementById("is_local_" + border_ID).value == "1") ? true : false;
+
+    if (typeof ($.fn.elastic) == "undefined")
+    {
+        $.lazy(
+        {
+            src: '../prototype/plugins/jquery-elastic/jquery.elastic.source.js',
+            name: 'elastic'
+        });
+    }
+    var content = $("#content_id_" + new_border_ID);
+
+    //It is verifying if the message was modified or not.
+    var btnSaveVerify = function ()
+    {
+        $("#content_id_" + currentTab + " .save").button("disable");
+
+        dataBtn = new Array(".to", ".cc", ".cco", "input[name=input_subject]", ".reply-to");
+
+        for (var i in dataBtn)
+        {
+            $("#content_id_" + currentTab + " " + dataBtn[i]).keydown(function ()
+            {
+                $("#content_id_" + currentTab + " .save").button("enable");
+            });
+        }
+
+        RichTextEditor.keydown(new_border_ID);
+    }
+
+    switch (type)
+    {
+        case "reply_without_history":
+            btnSaveVerify();
+
+            RichTextEditor.replyController = true; //Seta o editor como modo reply
+            content.find('[name="input_to"]').val(data.to);
+
+            draw_reply_boxes_by_field("to", data.to, content);
+
+            title = "Re: " + html_entities(data.subject);
+            content.find(".subject").val("Re: " + data.subject);
+            useOriginalAttachments(new_border_ID, border_ID);
+            content.find('[name="msg_reply_from"]').val($("#msg_number_" + border_ID).val());
+
+            // Insert the signature automaticaly at message body if use_signature preference is set
+            if (preferences.use_signature == "1")
+            {
+                RichTextEditor.setInitData(new_border_ID, '<div><br type="_moz"></div>' + signature, true);
+            }
+            break;
+        case "reply_with_history":
+            btnSaveVerify();
+
+            RichTextEditor.replyController = true; //Seta o editor como modo reply 
+            title = "Re: " + html_entities(data.subject);
+            content.find(".subject").val("Re: " + data.subject);
+            content.find('[name="input_to"]').val(data.to);
+
+            draw_reply_boxes_by_field("to", data.to, content);
+            content.find('[name="msg_reply_from"]').val($("#msg_number_" + border_ID).val());
+            block_quoted_body = make_body_reply(data.body, data.from, data.date_day, data.date_hour);
+
+            useOriginalAttachments(new_border_ID, border_ID);
+
+            // Insert the signature automaticaly at message body if use_signature preference is set
+            if (preferences.use_signature == "1")
+            {
+                var body_text = '<div><br type="_moz"></div>' + signature + '<div><br type="_moz"></div>' + block_quoted_body;
+                if (preferences.plain_text_editor == "1")
+                {
+                    body_text = "\n\n" + remove_tags(body_text);
+                    $("#body_" + new_border_ID).val(body_text, true);
+                }
+                else
+                {
+                    RichTextEditor.setInitData(new_border_ID, body_text, true);
+                }
+            }
+            else
+            {
+                body_text = '<div><br type="_moz"></div>' + block_quoted_body;
+                if (preferences.plain_text_editor == "1")
+                {
+                    body_text = "\n\n" + remove_tags(body_text);
+                    $("#body_" + new_border_ID).val(body_text, true);
+                }
+                else
+                {
+                    RichTextEditor.setInitData(new_border_ID, body_text, true);
+                }
+            }
+            break;
+        case "reply_to_all_without_history":
+            btnSaveVerify();
+
+            RichTextEditor.replyController = true; //Seta o editor como modo reply
+            // delete user email from to_all array.
+            data.to_all = new Array();
+            data.to_all = removeUserEmail(_array_to_all);
+            data.to_all = removeAlternative(data.to_all, _array_to_alternative);
+            content.find('[name="msg_reply_from"]').val($("#msg_number_" + border_ID).val());
+
+            data.to_all = data.to_all.join(",");
+
+            title = "Re: " + html_entities(data.subject);
+            content.find(".subject").val("Re: " + data.subject);
+
+            if (data.to.indexOf(Element("user_email").value) > 0)
+            {
+                draw_reply_boxes_by_field("to", data.to_all, content);
+                content.find('[name="input_to"]').val(data.to_all);
+            }
+            else
+            {
+                draw_reply_boxes_by_field("to", data.to + ',' + data.to_all, content);
+                content.find('[name="input_to"]').val(data.to + ',' + data.to_all);
+            }
+
+            if (data.cc)
+            {
+                data.cc = new Array();
+                data.cc = removeUserEmail(_array_cc);
+                data.cc = removeAlternative(data.cc, _array_to_alternative);
+                if (data.cc != get_lang("undisclosed-recipient")) data.cc = data.cc.join(",");
+                else data.cc = "";
+                if (data.cc != "")
+                {
+                    content.find('[name="input_cc"]').val(data.cc);
+                    input_binds(content.find('[name="input_cc"]').parent(), new_border_ID);
+                    content.find(".cc-tr").show(); //cc-button
+                    //document.getElementById("a_cc_link" + new_border_ID).value = data.cc;
+                    content.find(".cc-button").toggleClass("expressomail-button-icon-ative");
+                    content.find(".cc-button").find("span").html("Remover CC");
+                    draw_reply_boxes_by_field("cc", data.cc, content);
+                }
+            }
+
+            useOriginalAttachments(new_border_ID, border_ID);
+            if (preferences.use_signature == "1")
+            {
+                RichTextEditor.setInitData(new_border_ID, '<div><br type="_moz"></div>' + signature, true);
+            }
+
+            break;
+        case "reply_to_all_with_history":
+            btnSaveVerify();
+
+            RichTextEditor.replyController = true; //Seta o editor como modo reply 
+            //delete user email from to_all array.
+            data.to_all = new Array();
+            data.to_all = removeUserEmail(_array_to_all);
+            data.to_all = removeAlternative(data.to_all, _array_to_alternative);
+            content.find('[name="msg_reply_from"]').val($("#msg_number_" + border_ID).val());
+
+            if (data.to_all != get_lang("undisclosed-recipient")) data.to_all = data.to_all.join(",");
+            else data.to_all = "";
+
+            title = "Re: " + html_entities(data.subject);
+
+            if (data.to.indexOf(Element("user_email").value) > 0)
+            {
+                draw_reply_boxes_by_field("to", data.to_all, content);
+                content.find('[name="input_to"]').val(data.to_all);
+            }
+            else
+            {
+                draw_reply_boxes_by_field("to", data.to + ',' + data.to_all, content);
+                content.find('[name="input_to"]').val(data.to + ',' + data.to_all);
+            }
+
+            if (data.cc)
+            {
+                data.cc = new Array();
+                data.cc = removeUserEmail(_array_cc);
+                data.cc = removeAlternative(data.cc, _array_to_alternative);
+                if (data.cc != get_lang("undisclosed-recipient")) data.cc = data.cc.join(",");
+                else data.cc = "";
+                if (data.cc != "")
+                {
+                    content.find('[name="input_cc"]').val(data.cc);
+                    input_binds(content.find('[name="input_aux_cc"]').parent(), new_border_ID);
+                    content.find(".cc-tr").show();
+                    content.find(".cc-button").toggleClass("expressomail-button-icon-ative");
+                    content.find(".cc-button").find("span").html("Remover CC");
+
+                    draw_reply_boxes_by_field("cc", data.cc, content);
+                }
+            }
+            content.find(".subject").val("Re: " + data.subject);
+
+            block_quoted_body = make_body_reply(data.body, data.from, data.date_day, data.date_hour);
+
+            useOriginalAttachments(new_border_ID, border_ID);
+
+            if (preferences.use_signature == "1")
+            {
+                var body_text = '<div><br type="_moz"></div><div><br type="_moz"></div>' + signature + '<div><br type="_moz"></div>' + block_quoted_body;
+                if (preferences.plain_text_editor == "1")
+                {
+                    body_text = "\n\n" + remove_tags(body_text);
+                    $("#body_" + new_border_ID).val(body_text, true);
+                }
+                else
+                {
+                    RichTextEditor.setInitData(new_border_ID, body_text, true);
+                }
+            }
+            else
+            {
+                var body_text = '<div><br type="_moz"></div><div><br type="_moz"></div>' + block_quoted_body;
+                if (preferences.plain_text_editor == "1")
+                {
+                    body_text = "\n\n" + remove_tags(body_text);
+                    $("#body_" + new_border_ID).val(body_text, true);
+                }
+                else
+                {
+                    RichTextEditor.setInitData(new_border_ID, body_text, true);
+                }
+            }
+
+            break;
+        case "forward":
+            btnSaveVerify();
+
+            title = "Fw: " + html_entities(data.subject);
+            content.find(".subject").val("Fw: " + data.subject);
+            var divFiles = Element("divFiles_" + new_border_ID);
+            var campo_arquivo;
+            content.find('[name="msg_forward_from"]').val($("#msg_number_" + border_ID).val());
+
+            if (Element("attachments_" + border_ID)) addOriginalAttachments(new_border_ID, border_ID);
+
+            // Insert the signature automaticaly at message body if use_signature preference is set
+            if (preferences.use_signature == "1")
+            {
+                var body_text = '<div><br type="_moz"></div><div><br type="_moz"></div>' + signature + '<div><br type="_moz"></div>' + make_forward_body(data.body, data.to, data.date, data.subject, data.to_all, data.cc);
+                if (preferences.plain_text_editor == "1")
+                {
+                    body_text = "\n\n" + remove_tags(body_text);
+                    $("#body_" + new_border_ID).val(body_text);
+                }
+                else
+                {
+                    RichTextEditor.setInitData(new_border_ID, body_text, true);
+                }
+            }
+            else
+            {
+                var body_text = '<div><br type="_moz"></div><div><br type="_moz"></div>' + make_forward_body(data.body, data.to, data.date, data.subject, data.to_all, data.cc);
+                if (preferences.plain_text_editor == "1")
+                {
+                    body_text = "\n\n" + remove_tags(body_text);
+                    $("#body_" + new_border_ID).val(body_text);
+                }
+                else
+                {
+                    RichTextEditor.setInitData(new_border_ID, body_text, true);
+                }
+            }
+            RichTextEditor.dataReady(new_border_ID, 'forward');
+
+            break;
+        case "new":
+
+            btnSaveVerify();
+
+            title = get_lang("New Message");
+            if (Element('msg_number').value)
+            {
+                var _to = Element('msg_number').value;
+                var reEmail = /^[A-Za-z\d_-]+(\.[A-Za-z\d_-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
+                if (!reEmail.test(_to))
+                {
+                    var array_contacts = contacts.split(',');
+                    for (i = 0; i < array_contacts.length; i++)
                     {
-						delete_border(currentTab);
-						new_border_ID = draw_new_message(border_ID);
-                    }			
-		} else {
-			var a_types = {6 : get_lang("Forward"),7 : get_lang("Reply"), 
-					8 : get_lang("Reply to all with history"),
-					9 : get_lang("Reply without history"),
-					10: get_lang("Reply to all without history")};
-			if(new_border_ID != 0)
-			if(!confirm(get_lang("Your message to %1 has not been saved or sent. "+
-						"To %2 will be necessary open it again. Discard your message?",
-						a_types[openTab.type[new_border_ID]].toUpperCase(), 
-						a_types[tabTypes[type]].toUpperCase()))){
-				return new_border_ID;
-			} else {
-				delete_border(currentTab);
-				new_border_ID = draw_new_message(border_ID);
-			}
-		}
-	}
-	if (new_border_ID == 'maximo'){
-		RichTextEditor.editorReady = true;
-		return false;
-	}
-	if (new_border_ID == false)
-	{
-		RichTextEditor.editorReady = true; 
- 	    setTimeout('new_message(\''+type+'\',\''+border_ID+'\',\''+flagged+'\');',500);
-		return false;
-	}
-	openTab.type[new_border_ID] = tabTypes[type];
-
-	// Salva a pasta da mensagem respondida ou encaminhada:
-	var folder_message = Element("input_folder_"+border_ID);
-	if(folder_message) {
-		var input_current_folder = document.createElement('input');
-		input_current_folder.id = "new_input_folder_"+border_ID;
-		input_current_folder.name = "input_folder";
-		input_current_folder.type = "hidden";
-		input_current_folder.value = folder_message.value;
-		Element("content_id_" + new_border_ID).appendChild(input_current_folder);
-	}//Fim.
-	var title = '';
-	data = [];
-
-	
-	if (Element("from_" + border_ID)){
-		if (document.getElementById("reply_to_" + border_ID)){
-				data.to = document.getElementById("reply_to_values_" + border_ID).value;
-				data.to = data.to.replace(/&lt;/gi,"<");
-				data.to = data.to.replace(/&gt;/gi,">");
-		}
-		else {
-			if (document.getElementById("sender_values_"+border_ID))
-			{
-				data.to = document.getElementById("sender_values_"+border_ID).value;
-				data.to = data.to.replace(/&lt;/gi,"<");
-				data.to = data.to.replace(/&gt;/gi,">");
-			}
-			else if(document.getElementById("from_values_"+border_ID)) {
-				data.to = document.getElementById("from_values_"+border_ID).value;
-				data.to = data.to.replace(/&lt;/gi,"<");
-				data.to = data.to.replace(/&gt;/gi,">");
-			}
-		}
-		if (document.getElementById("to_values_" + border_ID)){
-			data.to_all = document.getElementById("to_values_" + border_ID).value;
-			data.to_all_alternative = document.getElementById("user_email_alternative").value;
-			data.to_all = data.to_all.replace(/\n/gi," ");
-			data.to_all = data.to_all.replace(/&lt;/gi,"<");
-			data.to_all = data.to_all.replace(/&gt;/gi,">");
-			var _array_to_all = data.to_all.split(",");
-			var _array_to_alternative = data.to_all_alternative.split(",");
-		}
-	}
-	if (document.getElementById("cc_" + border_ID)){
-		data.cc = document.getElementById("cc_values_" + border_ID).value;
-		data.cc = data.cc.replace(/&lt;/gi,"<");
-		data.cc = data.cc.replace(/&gt;/gi,">");
-		var _array_cc = data.cc.split(",");
-	}
-	if (document.getElementById("cco_" + border_ID)){
-		data.cco = document.getElementById("cco_values_" + border_ID).value;
-		data.cco = data.cco.replace(/&lt;/gi,"<");
-		data.cco = data.cco.replace(/&gt;/gi,">");
-	}
-	if ($("#subject_" + border_ID))
-		data.subject = $("#subject_" + border_ID).text();
-	if(data.subject == get_lang("(no subject)   "))
-		data.subject = '';
-	if (document.getElementById("body_" + border_ID))
-		data.body = document.getElementById("body_" + border_ID).innerHTML;
-	if (document.getElementById("from_values_" + border_ID))
-		data.from = document.getElementById("from_values_"+border_ID).value;
-	if (Element('date_' + border_ID)){
-		data.date = Element('date_' + border_ID).innerHTML;
-	}
-	if (Element('date_day_' + border_ID)){
-		data.date_day = Element('date_day_' + border_ID).value;
-	}
-	if (Element('date_hour_' + border_ID)){
-		data.date_hour = Element('date_hour_' + border_ID).value;
-	}
-        
-	var signature = RichTextEditor.getSignatureDefault();
-        
-	if(type!="new" && type!="edit" && document.getElementById("is_local_"+border_ID) != null)
-		data.is_local_message = (document.getElementById("is_local_"+border_ID).value=="1")?true:false;
-		
-	if(typeof($.fn.elastic) == "undefined"){
-		$.lazy({
-			src: '../prototype/plugins/jquery-elastic/jquery.elastic.source.js',
-			name: 'elastic'
-		});
-	}
-	var content = $("#content_id_"+new_border_ID);
-
-//It is verifying if the message was modified or not.
-var btnSaveVerify = function(){
-    $("#content_id_"+currentTab+" .save").button("disable");
-
-    dataBtn = new Array(".to",".cc",".cco","input[name=input_subject]",".reply-to");
-
-    for(var i in dataBtn){
-      $("#content_id_"+currentTab+" "+dataBtn[i]).keydown(function() {  
-        $("#content_id_"+currentTab+" .save").button("enable");
-      });
+                        if (array_contacts[i].lastIndexOf(_to) != "-1")
+                        {
+                            var _group = array_contacts[i].split(";");
+                            _to = '"' + _group[0] + '" <' + _group[1] + '>';
+                            break;
+                        }
+                    }
+                }
+                content.find('[name="input_to"]').val(_to + ',');
+                draw_email_box(_to, content.find(".to").filter("input"));
+                Element('msg_number').value = '';
+            }
+
+            // Insert the signature automaticaly at message body if use_signature preference is set
+            if (preferences.use_signature == "1")
+            {
+                var signature_text = '<div><br type="_moz"></div><div><br type="_moz"></div>' + signature;
+                if (preferences.plain_text_editor == "1")
+                {
+                    signature_text = "\n\n" + remove_tags(signature_text);
+                    $("#body_" + new_border_ID).val(signature_text);
+                }
+                else
+                {
+                    RichTextEditor.setInitData(new_border_ID, signature_text, true);
+                }
+                content.find(".to").focus();
+            }
+            else content.find('[name="input_to"]').focus();
+
+            RichTextEditor.dataReady(new_border_ID, 'new');
+
+            break;
+        case "edit":
+            btnSaveVerify();
+
+            if (flagged == 'F') $(".important").addClass("expressomail-button-icon-ative");
+
+            openTab.imapBox[new_border_ID] = folder_message.value;
+            document.getElementById('font_border_id_' + new_border_ID).innerHTML = data.subject;
+            title = "Edição: " + html_entities(data.subject);
+
+            data.to = Element("to_values_" + border_ID).value;
+            if (data.to != get_lang("without destination"))
+            {
+                data.to = data.to.replace(/&lt;/gi, "<");
+                data.to = data.to.replace(/&gt;/gi, ">");
+            }
+            else
+            {
+                data.to = "";
+            }
+
+            draw_reply_boxes_by_field("to", data.to, content);
+
+            content.find('[name="input_to"]').val(data.to);
+            if (data.cc)
+            {
+                data.cc = data.cc.replace(/&lt;/gi, "<");
+                data.cc = data.cc.replace(/&gt;/gi, ">");
+                content.find('[name="input_cc"]').val(data.cc);
+                input_binds(content.find('[name="input_cc"]').parent(), new_border_ID);
+                content.find(".cc-tr").show();
+                content.find(".cc-button").toggleClass("expressomail-button-icon-ative");
+                content.find(".cc-button").find("span").html(get_lang('Remove CC'));
+                draw_reply_boxes_by_field("cc", data.cc, content);
+            }
+            if (data.cco)
+            {
+                if (content.find('[name="input_cco"]').length)
+                {
+                    content.find('[name="input_cco"]').val(data.cco);
+                    content.find(".cco-tr").show();
+                    content.find(".cco-button").toggleClass("expressomail-button-icon-ative");
+                    content.find(".cco-button").find("span").html(get_lang('Remove CCo'));
+                    input_binds(content.find('[name="input_cco"]').parent(), new_border_ID);
+                    draw_reply_boxes_by_field("cco", data.cco, content);
+                }
+            }
+            content.find(".subject").val(data.subject);
+
+            if ($("#disposition_notification_" + border_ID).length)
+            {
+                content.find('[name="input_return_receipt"]').attr("checked", true);
+                content.find(".return-recept").toggleClass("expressomail-button-icon-ative");
+                //Element("return_receipt_" + new_border_ID).checked = true;
+            }
+
+            var element_important_message = Element("important_message_" + new_border_ID);
+            if (element_important_message)
+            {
+
+                if ($("#disposition_important_" + border_ID).length)
+                {
+                    content.find('[name="input_important_message"]').attr("checked", true);
+                    content.find(".important").toggleClass("expressomail-button-icon-ative");
+                }
+            }
+
+            if (Element("attachments_" + border_ID)) addOriginalAttachments(new_border_ID, border_ID);
+
+            if (preferences.plain_text_editor == "1")
+            {
+                data.body = remove_tags(data.body);
+                $("#body_" + new_border_ID).val(data.body);
+            }
+            else
+            {
+                RichTextEditor.setInitData(new_border_ID, data.body, 'edit');
+            }
+
+            uidsSave[new_border_ID].push(new_border_ID);
+            close_delete(border_ID);
+
+            break;
+        default:
     }
 
-    RichTextEditor.keydown(new_border_ID);
-}
-
-switch(type){
-		case "reply_without_history":
-            btnSaveVerify();
-
-			RichTextEditor.replyController = true; //Seta o editor como modo reply
-			content.find('[name="input_to"]').val(data.to);			
-			
-			draw_reply_boxes_by_field("to", data.to, content);
-			
-			title = "Re: " + html_entities(data.subject);
-			content.find(".subject").val("Re: " + data.subject);			
-			useOriginalAttachments(new_border_ID,border_ID);
-			content.find('[name="msg_reply_from"]').val($("#msg_number_" + border_ID).val());			
-
-			// Insert the signature automaticaly at message body if use_signature preference is set
-			if (preferences.use_signature == "1"){  
-				RichTextEditor.setInitData(new_border_ID,'<div><br type="_moz"></div>' + signature ,true) ;      
-			} 
-			break;
-		case "reply_with_history":
-            btnSaveVerify();
-
-			RichTextEditor.replyController = true; //Seta o editor como modo reply 
-			title = "Re: " + html_entities(data.subject);
-			content.find(".subject").val("Re: " + data.subject);
-			content.find('[name="input_to"]').val(data.to);
-			
-			draw_reply_boxes_by_field("to", data.to, content);
-			content.find('[name="msg_reply_from"]').val($("#msg_number_" + border_ID).val());	
-			block_quoted_body = make_body_reply(data.body, data.from, data.date_day, data.date_hour);
-			
-			useOriginalAttachments(new_border_ID,border_ID);
-			
-			// Insert the signature automaticaly at message body if use_signature preference is set
-			if (preferences.use_signature == "1"){  
-				var body_text = '<div><br type="_moz"></div>' + signature + '<div><br type="_moz"></div>' + block_quoted_body;
-				if (preferences.plain_text_editor == "1"){
-					body_text = "\n\n"+remove_tags(body_text);
-					$("#body_"+new_border_ID).val(body_text, true);
-				} else {
-					RichTextEditor.setInitData(new_border_ID, body_text, true); 				
-				}
-			}else{
-				body_text = '<div><br type="_moz"></div>'+ block_quoted_body;
-				if (preferences.plain_text_editor == "1"){
-					body_text = "\n\n"+remove_tags(body_text);
-					$("#body_"+new_border_ID).val(body_text, true);
-				} else {
-					RichTextEditor.setInitData(new_border_ID, body_text, true);
-				}
-			}
-			break;
-		case "reply_to_all_without_history":
-            btnSaveVerify();
-
-			RichTextEditor.replyController = true; //Seta o editor como modo reply
-			// delete user email from to_all array.
-			data.to_all = new Array();
-			data.to_all = removeUserEmail(_array_to_all);
-			data.to_all = removeAlternative(data.to_all,_array_to_alternative);
-			content.find('[name="msg_reply_from"]').val($("#msg_number_" + border_ID).val());	
-
-			data.to_all = data.to_all.join(",");
-
-			title = "Re: " + html_entities(data.subject);
-			content.find(".subject").val("Re: " + data.subject);
-			
-			if (data.to.indexOf(Element("user_email").value) > 0 ){
-				draw_reply_boxes_by_field("to", data.to_all, content);
-				content.find('[name="input_to"]').val(data.to_all);
-			}else{
-				draw_reply_boxes_by_field("to", data.to + ',' + data.to_all, content);
-				content.find('[name="input_to"]').val(data.to + ',' + data.to_all);
-			}
-			
-			if (data.cc){
-				data.cc = new Array();
-				data.cc = removeUserEmail(_array_cc);
-				data.cc = removeAlternative(data.cc,_array_to_alternative);
-					if (data.cc != get_lang("undisclosed-recipient"))
-						data.cc = data.cc.join(",");
-					else
-						data.cc = "";	
-				if(data.cc != ""){
-					content.find('[name="input_cc"]').val(data.cc);
-					input_binds(content.find('[name="input_cc"]').parent(), new_border_ID);
-					content.find(".cc-tr").show();//cc-button
-					//document.getElementById("a_cc_link" + new_border_ID).value = data.cc;
-					content.find(".cc-button").toggleClass("expressomail-button-icon-ative");
-					content.find(".cc-button").find("span").html("Remover CC");
-					draw_reply_boxes_by_field("cc", data.cc, content);
-				}
-			}
-                        
-			useOriginalAttachments(new_border_ID,border_ID);  
-			if (preferences.use_signature == "1"){  
-				RichTextEditor.setInitData(new_border_ID,'<div><br type="_moz"></div>' + signature,true);
-			}
-				
-			break;
-		case "reply_to_all_with_history":
-            btnSaveVerify(); 
-
-			RichTextEditor.replyController = true; //Seta o editor como modo reply 
-			//delete user email from to_all array.
-			data.to_all = new Array();
-			data.to_all = removeUserEmail(_array_to_all);
-			data.to_all = removeAlternative(data.to_all,_array_to_alternative);
-			content.find('[name="msg_reply_from"]').val($("#msg_number_" + border_ID).val());
-		
-			if (data.to_all != get_lang("undisclosed-recipient"))
-				data.to_all = data.to_all.join(",");
-			else
-				data.to_all = "";
-			
-			title = "Re: " + html_entities(data.subject);
-			
-			if (data.to.indexOf(Element("user_email").value) > 0 ){
-				draw_reply_boxes_by_field("to", data.to_all, content);
-				content.find('[name="input_to"]').val(data.to_all);
-			}else{
-				draw_reply_boxes_by_field("to", data.to + ',' + data.to_all, content);
-				content.find('[name="input_to"]').val(data.to + ',' + data.to_all);			
-			}
-			
-			if (data.cc){
-				data.cc = new Array();
-				data.cc = removeUserEmail(_array_cc);
-				data.cc = removeAlternative(data.cc,_array_to_alternative);
-					if (data.cc != get_lang("undisclosed-recipient"))
-						data.cc = data.cc.join(",");
-					else
-						data.cc = "";			
-				if(data.cc != ""){
-					content.find('[name="input_cc"]').val(data.cc);
-					input_binds(content.find('[name="input_aux_cc"]').parent(), new_border_ID);
-					content.find(".cc-tr").show();
-					content.find(".cc-button").toggleClass("expressomail-button-icon-ative");
-					content.find(".cc-button").find("span").html("Remover CC");
-					
-					draw_reply_boxes_by_field("cc", data.cc, content);
-				}
-			}
-			content.find(".subject").val("Re: " + data.subject);
-			
-			block_quoted_body = make_body_reply(data.body, data.from, data.date_day, data.date_hour);
-			
-			useOriginalAttachments(new_border_ID,border_ID);
-			
-			if (preferences.use_signature == "1"){  
-				var body_text = '<div><br type="_moz"></div><div><br type="_moz"></div>' + signature + '<div><br type="_moz"></div>'+ block_quoted_body;
-				if (preferences.plain_text_editor == "1"){
-					body_text = "\n\n"+remove_tags(body_text);
-					$("#body_"+new_border_ID).val(body_text, true);
-				}
-				else {
-					RichTextEditor.setInitData(new_border_ID, body_text, true); 
-				}
-			}else{                
-				var body_text = '<div><br type="_moz"></div><div><br type="_moz"></div>' + block_quoted_body;
-				if (preferences.plain_text_editor == "1"){
-					body_text = "\n\n"+remove_tags(body_text);
-					$("#body_"+new_border_ID).val(body_text, true);
-				}
-				else {
-					RichTextEditor.setInitData(new_border_ID, body_text, true); 
-				}
-			}
-				
-			break;
-		case "forward":
-            btnSaveVerify();
-
-			title = "Fw: " + html_entities(data.subject);
-			content.find(".subject").val("Fw: " + data.subject);
-			var divFiles = Element("divFiles_"+new_border_ID);
-			var campo_arquivo;
-			content.find('[name="msg_forward_from"]').val($("#msg_number_" + border_ID).val());
-                  
-			if (Element("attachments_" + border_ID))
-                            addOriginalAttachments(new_border_ID,border_ID);
-
-			// Insert the signature automaticaly at message body if use_signature preference is set
-			if (preferences.use_signature == "1"){ 
-				var body_text = '<div><br type="_moz"></div><div><br type="_moz"></div>' + signature + '<div><br type="_moz"></div>'+ make_forward_body(data.body, data.to, data.date, data.subject, data.to_all, data.cc);
-				if (preferences.plain_text_editor == "1"){
-					body_text = "\n\n"+remove_tags(body_text);
-					$("#body_"+new_border_ID).val(body_text);
-				}
-				else {
- 		        	RichTextEditor.setInitData(new_border_ID, body_text, true); 
- 		        }
-			}
-			else{   
-				var body_text = '<div><br type="_moz"></div><div><br type="_moz"></div>'+make_forward_body(data.body, data.to, data.date, data.subject, data.to_all, data.cc);
-				if (preferences.plain_text_editor == "1"){
-					body_text = "\n\n"+remove_tags(body_text);
-					$("#body_"+new_border_ID).val(body_text);
-				}
-				else {
-					RichTextEditor.setInitData(new_border_ID, body_text, true);   
-				}
-			}
-			RichTextEditor.dataReady(new_border_ID,'forward');
-
-			break;
-		case "new":
-        
-        btnSaveVerify();
-
-			title = get_lang("New Message");
-			if(Element('msg_number').value) {
-				var _to = Element('msg_number').value;
-				var reEmail = /^[A-Za-z\d_-]+(\.[A-Za-z\d_-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
-				if(!reEmail.test(_to)){
-					var array_contacts = contacts.split(',');
-					for(i = 0; i < array_contacts.length;i++) {
-						if(array_contacts[i].lastIndexOf(_to) != "-1"){
-							var _group = array_contacts[i].split(";");
-							_to = '"'+_group[0]+'" <'+_group[1]+'>';
-							break;
-						}
-					}
-				}
-				content.find('[name="input_to"]').val(_to +',');
-				draw_email_box(_to, content.find(".to").filter("input"));
-				Element('msg_number').value = '';
-			}
-			
-			// Insert the signature automaticaly at message body if use_signature preference is set
-			 if (preferences.use_signature == "1"){ 
-			 	var signature_text = '<div><br type="_moz"></div><div><br type="_moz"></div>' + signature;
-				if (preferences.plain_text_editor == "1"){
-					signature_text = "\n\n"+remove_tags(signature_text);
-					$("#body_"+new_border_ID).val(signature_text);
-				} 
-				else {
-					RichTextEditor.setInitData(new_border_ID, signature_text, true);
-				}
-				content.find(".to").focus();
-			 }
-			 else
-			   content.find('[name="input_to"]').focus(); 
-
-				RichTextEditor.dataReady(new_border_ID,'new');
-
-			break;
-		case "edit":
-			btnSaveVerify();
-
-			if(flagged == 'F')
-				$(".important").addClass("expressomail-button-icon-ative");
-			
-			openTab.imapBox[new_border_ID] = folder_message.value;
-			document.getElementById('font_border_id_'+new_border_ID).innerHTML = data.subject;
-			title = "Edição: "+ html_entities(data.subject);
-			
-			data.to = Element("to_values_" + border_ID).value;
-			if( data.to != get_lang("without destination") ) {
-				data.to = data.to.replace(/&lt;/gi,"<");
-				data.to = data.to.replace(/&gt;/gi,">");
-			} else {
-				data.to = "";
-			}
-
-			draw_reply_boxes_by_field("to", data.to, content);
-			
-			content.find('[name="input_to"]').val(data.to);
-			if (data.cc){
-				data.cc = data.cc.replace(/&lt;/gi,"<");
-				data.cc = data.cc.replace(/&gt;/gi,">");
-				content.find('[name="input_cc"]').val(data.cc);
-				input_binds(content.find('[name="input_cc"]').parent(), new_border_ID);
-				content.find(".cc-tr").show();
-				content.find(".cc-button").toggleClass("expressomail-button-icon-ative");
-				content.find(".cc-button").find("span").html(get_lang('Remove CC'));
-				draw_reply_boxes_by_field("cc", data.cc, content);
-			}
-			if (data.cco){
-				if(content.find('[name="input_cco"]').length){
-					content.find('[name="input_cco"]').val(data.cco);
-					content.find(".cco-tr").show();
-					content.find(".cco-button").toggleClass("expressomail-button-icon-ative");
-					content.find(".cco-button").find("span").html(get_lang('Remove CCo'));
-					input_binds(content.find('[name="input_cco"]').parent(), new_border_ID);
-					draw_reply_boxes_by_field("cco", data.cco, content);
-				}
-			}
-			content.find(".subject").val(data.subject);
-			
-			if( $("#disposition_notification_" + border_ID).length ){
-				content.find('[name="input_return_receipt"]').attr("checked", true);
-				content.find(".return-recept").toggleClass("expressomail-button-icon-ative");
-				//Element("return_receipt_" + new_border_ID).checked = true;
-			}
-
-			var element_important_message = Element("important_message_" + new_border_ID);
-			if(element_important_message) {
-					
-				if($("#disposition_important_" + border_ID).length){
-					content.find('[name="input_important_message"]').attr("checked", true);
-					content.find(".important").toggleClass("expressomail-button-icon-ative");	
-				}
-			}
-
-			if (Element("attachments_" + border_ID))
-				addOriginalAttachments(new_border_ID,border_ID);
-
-			if (preferences.plain_text_editor == "1"){
-				data.body = remove_tags(data.body);
-				$("#body_"+new_border_ID).val(data.body);
-			} 
-			else {
-				RichTextEditor.setInitData(new_border_ID, data.body, 'edit');
-			}
-			
-			uidsSave[new_border_ID].push(new_border_ID);
-			close_delete(border_ID);
-			
-			break;
-		default:
-	}
-	
-	content.find('[name="input_to"]').trigger("update");	
-	var txtarea = $('#body_'+new_border_ID);
-	var height = document.body.scrollHeight - 330;
-	txtarea.css("overflowY","auto");
-	txtarea.css("height",height);
-	$("#border_id_" + new_border_ID).attr("title", title);
-	set_border_caption("border_id_" + new_border_ID, title);
-	resizeWindow();
-	return new_border_ID; //Preciso retornar o ID da nova mensagem.
+    content.find('[name="input_to"]').trigger("update");
+    var txtarea = $('#body_' + new_border_ID);
+    var height = document.body.scrollHeight - 330;
+    txtarea.css("overflowY", "auto");
+    txtarea.css("height", height);
+    $("#border_id_" + new_border_ID).attr("title", title);
+    set_border_caption("border_id_" + new_border_ID, title);
+    resizeWindow();
+    return new_border_ID; //Preciso retornar o ID da nova mensagem.
 }
 
