Index: trunk/expressoMail1_2/js/draw_api.js
===================================================================
--- trunk/expressoMail1_2/js/draw_api.js	(revision 673)
+++ trunk/expressoMail1_2/js/draw_api.js	(revision 689)
@@ -53,5 +53,49 @@
 			}
 		}
-		cExecute ("$this.imap_functions.get_folders_list", update_tree_folders);
+		cExecute ("$this.imap_functions.get_folders_list&folder="+current_folder, update_tree_folders);
+		
+		
+		/**
+		 * Pastas locais
+		 */
+		if(preferences.use_local_messages==1) {
+			var local_folders = expresso_local_messages.list_local_folders();
+			var has_changes = false;
+			for (var i in local_folders) { //Coloca as pastas locais.
+				var folder_unseen = Element('dftree_local_'+local_folders[i][0]+'_unseen');
+				
+				if ((folder_unseen) && (local_folders[i][1] > 0))
+				{
+					folder_unseen.innerHTML = local_folders[i][1];
+					has_changes = true;
+				}
+				else if (local_folders[i][1] > 0)
+				{
+					tree_folders.getNodeById("local_"+local_folders[i][0]).alter({caption:lang_folder(local_folders[i][0]) + '<font style=color:red>&nbsp(</font><span id="dftree_local_'+local_folders[i][0]+'_unseen" style=color:red>'+local_folders[i][1]+'</span><font style=color:red>)</font>'});
+					tree_folders.getNodeById("local_"+local_folders[i][0])._refresh();
+					has_changes = true;
+				}
+				else if (local_folders[i][1] <= 0)
+				{
+					tree_folders.getNodeById("local_"+local_folders[i][0]).alter({caption:lang_folder(local_folders[i][0])});
+					tree_folders.getNodeById("local_"+local_folders[i][0])._refresh();
+					has_changes = true;
+				}
+	
+				if("local_"+local_folders[i][0] == get_current_folder()){					
+					var old_new_m = isNaN(parseInt(Element('new_m').innerHTML)) ? parseInt(Element('new_m').firstChild.innerHTML) : parseInt(Element('new_m').innerHTML);
+					if(!isNaN(old_new_m) && old_new_m < local_folders[i][1]){
+						Element('tot_m').innerHTML = parseInt(Element('tot_m').innerHTML) + (parseInt(local_folders[i][1])-old_new_m);
+					}
+					Element('new_m').innerHTML = local_folders[i][1] ? '<font color="RED">'+local_folders[i][1]+'</font>' : 0;
+					draw_paging(Element('tot_m').innerHTML);
+					has_changes = true;
+				}
+			}
+			if(has_changes)
+				tree_folders.getNodeById("local_root").open();
+
+		}
+		
 		return;
 	}
@@ -83,4 +127,48 @@
 			tree_folders.add(nn,folders[i].folder_parent);
 		}
+		//tree_local_folders = new dFTree({name: 'tree_local_folders'});
+		/**
+		 * Pastas locais
+		 */
+		if (preferences.use_local_messages==1) {
+			if (!window.google || !google.gears) {
+				temp = confirm(get_lang("To use local messages you have to install google gears. Would you like to be redirected to gears installation page?"));
+				if (temp) {
+					location.href = "http://gears.google.com/?action=install&message="+
+					get_lang("To use local messages, install Google Gears")+"&return=" + document.location.href;
+				}
+				else {
+					preferences.use_local_messages=0;
+				}
+			}
+			else{
+				var root_local = new dNode({
+					id: "local_root",
+					caption: get_lang('local messages')
+				});
+				tree_folders.add(root_local, "root"); //coloca root local
+				var local_folders = expresso_local_messages.list_local_folders();
+				for (var i in local_folders) { //Coloca as pastas locais.
+					if (local_folders[i][1] > 0) 
+						var nodeLocal = new dNode({
+							id: "local_" + local_folders[i][0],
+							caption: lang_folder(local_folders[i][0]) + '<font style=color:red>&nbsp(</font><span id="local_unseen" style=color:red>' + local_folders[i][1] + '</span><font style=color:red>)</font>',
+							onClick: "change_folder('local_" + local_folders[i][0] + "','" + local_folders[i][0] + "')",
+							plusSign: 0
+						});
+					else 
+						var nodeLocal = new dNode({
+							id: "local_" + local_folders[i][0],
+							caption: lang_folder(local_folders[i][0]),
+							onClick: "change_folder('local_" + local_folders[i][0] + "','" + local_folders[i][0] + "')",
+							plusSign: 0
+						});
+					tree_folders.add(nodeLocal, "local_root");
+				}
+			}
+		}
+
+		
+		
 		tree_folders.draw(Element('content_folders'));
 		n_root.changeState();
@@ -183,6 +271,8 @@
 			return true;
 		}
-		cExecute ("$this.imap_functions.get_range_msgs2&folder="+current_folder+"&msg_range_begin=1&msg_range_end="+preferences.max_email_per_page+"&sort_box_type="+sort_box_type+ "&search_box_type="+ search_box_type +"&sort_box_reverse="+sort_box_reverse+"", handler_draw_box);
-	} else
+		proxy_mensagens.messages_list(current_folder,1,preferences.max_email_per_page,sort_box_type,search_box_type,sort_box_reverse,handler_draw_box);
+//		cExecute ("$this.imap_functions.get_range_msgs2&folder="+current_folder+"&msg_range_begin=1&msg_range_end="+preferences.max_email_per_page+"&sort_box_type="+sort_box_type+ "&search_box_type="+ search_box_type +"&sort_box_reverse="+sort_box_reverse+"", handler_draw_box);
+	}
+	else
 		alternate_border(0);
 }
@@ -246,5 +336,5 @@
   	if(current_page != 1) {
 	  	lnk_page = document.createElement("A");
-		lnk_page.setAttribute("href", "javascript:current_page=1;kill_current_box(); draw_paging("+num_msgs+"); cExecute ('$this.imap_functions.get_range_msgs2&folder='+get_current_folder()+'&msg_range_begin="+msg_range_begin+"&msg_range_end="+msg_range_end+"&sort_box_type="+sort_box_type+ "&search_box_type="+ search_box_type +"&sort_box_reverse="+sort_box_reverse+"', function handler(data){draw_box(data, get_current_folder());})");
+		lnk_page.setAttribute("href", "javascript:current_page=1;kill_current_box(); draw_paging("+num_msgs+"); proxy_mensagens.messages_list(get_current_folder(),"+msg_range_begin+","+msg_range_end+",'"+sort_box_type+"','"+search_box_type+"',"+sort_box_reverse+",function handler(data){draw_box(data, get_current_folder());});");
   	}
   	else {
@@ -285,5 +375,5 @@
   			msg_range_begin = ((i*preferences.max_email_per_page)-(preferences.max_email_per_page-1));
 			msg_range_end = (i*preferences.max_email_per_page);
-			lnk_page.setAttribute("href", "javascript:current_page="+i+";kill_current_box(); draw_paging("+num_msgs+"); cExecute ('$this.imap_functions.get_range_msgs2&folder='+get_current_folder()+'&msg_range_begin="+msg_range_begin+"&msg_range_end="+msg_range_end+"&sort_box_type="+sort_box_type+ "&search_box_type="+ search_box_type +"&sort_box_reverse="+sort_box_reverse+"', function handler(data){draw_box(data, get_current_folder());})");
+			lnk_page.setAttribute("href", "javascript:current_page="+i+";kill_current_box(); draw_paging("+num_msgs+"); proxy_mensagens.messages_list(get_current_folder(),"+msg_range_begin+","+msg_range_end+",'"+sort_box_type+"','"+search_box_type+"',"+sort_box_reverse+",function handler(data){draw_box(data, get_current_folder());});");
   		}
   		
@@ -302,5 +392,5 @@
   		msg_range_begin = ((total_pages*preferences.max_email_per_page)-(preferences.max_email_per_page-1));
 		msg_range_end = (total_pages*preferences.max_email_per_page);  		
-		lnk_page.setAttribute("href", "javascript:current_page="+total_pages+";kill_current_box(); draw_paging("+num_msgs+"); cExecute ('$this.imap_functions.get_range_msgs2&folder='+get_current_folder()+'&msg_range_begin="+msg_range_begin+"&msg_range_end="+msg_range_end+"&sort_box_type="+sort_box_type+ "&search_box_type="+ search_box_type +"&sort_box_reverse="+sort_box_reverse+"', function handler(data){draw_box(data, get_current_folder());})");
+		lnk_page.setAttribute("href", "javascript:current_page="+total_pages+";kill_current_box(); draw_paging("+num_msgs+"); proxy_mensagens.messages_list(get_current_folder(),"+msg_range_begin+","+msg_range_end+",'"+sort_box_type+"','"+search_box_type+"',"+sort_box_reverse+",function handler(data){draw_box(data, get_current_folder());});");
 	}
 	else {
@@ -347,10 +437,10 @@
 			{
 				if ((window.event.keyCode) == 46)
-					delete_msgs(get_current_folder(),'selected','null');
+					proxy_mensagens.delete_msgs(get_current_folder(),'selected','null');
 			}
 			else
 			{
 				if ((e.keyCode) == 46)
-					delete_msgs(get_current_folder(),'selected','null');
+					proxy_mensagens.delete_msgs(get_current_folder(),'selected','null');
 			}
 		};
@@ -416,5 +506,5 @@
 			{
 				//delete_all_selected_msgs_imap();
-				delete_msgs(get_current_folder(),'selected','null');
+				proxy_mensagens.delete_msgs(get_current_folder(),'selected','null');
 			}
 		}
@@ -424,5 +514,5 @@
 			{
 				//delete_all_selected_msgs_imap();
-				delete_msgs(get_current_folder(),'selected','null');
+				proxy_mensagens.delete_msgs(get_current_folder(),'selected','null');
 			}
 		}
@@ -614,5 +704,5 @@
 		td_element3.id = "td_who_"+ headers_msgs.msg_number;
 		td_element3.setAttribute("width", "30%");
-		var _onclick = function(){InfoContact.hide();cExecute("$this.imap_functions.get_info_msg&msg_number="+ headers_msgs.msg_number + "&msg_folder=" + msg_folder, show_msg);};
+		var _onclick = function(){InfoContact.hide();proxy_mensagens.get_msg(headers_msgs.msg_number, msg_folder,true, show_msg);};
 		td_element3.onclick = _onclick;
 		td_element3.innerHTML = '&nbsp;';
@@ -734,5 +824,6 @@
 	search_box_type = search;
 
-	cExecute ("$this.imap_functions.get_range_msgs2&folder="+current_folder+"&msg_range_begin=1&msg_range_end="+preferences.max_email_per_page+"&sort_box_type="+sort+"&search_box_type="+search+"&sort_box_reverse="+sort_box_reverse+"", handler_draw_box);
+	proxy_mensagens.messages_list(current_folder,1,preferences.max_email_per_page,sort,search,sort_box_reverse,handler_draw_box);
+//	cExecute ("$this.imap_functions.get_range_msgs2&folder="+current_folder+"&msg_range_begin=1&msg_range_end="+preferences.max_email_per_page+"&search_box_type="+search+"&sort_box_type="+sort+"&sort_box_reverse="+sort_box_reverse+"", handler_draw_box);
 	current_page = 1;
 	//Comentado para nao sobrepor o numero de msgs nao lidas ao utilizar os filtros
@@ -960,5 +1051,5 @@
 	option_delete.id = 'msg_opt_delete_'+ID;
 	option_delete.className = 'message_options';
-	option_delete.onclick = function(){delete_msgs(info_msg.msg_folder, info_msg.msg_number, ID);};
+	option_delete.onclick = function(){proxy_mensagens.delete_msgs(info_msg.msg_folder, info_msg.msg_number, ID);};
 	option_delete.innerHTML = get_lang('Delete');
 	options.appendChild(space3);
@@ -982,7 +1073,7 @@
 
 	var option_mark_as						= '<span>'+get_lang("Mark as")+'</span>: ';
-	var option_mark_as_unseen				= '<span class="message_options" onclick="set_messages_flag(\'unseen\','+info_msg.msg_number+');write_msg(\''+get_lang('Message marked as ')+get_lang("Unseen")+'.\');">'+get_lang("Unseen")+'</span>, ';
-	var option_mark_as_important			= '<span class="message_options" onclick="set_messages_flag(\'flagged\','+info_msg.msg_number+');write_msg(\''+get_lang('Message marked as ')+get_lang("Important")+'.\');">'+get_lang("Important")+'</span>, ';
-	var option_mark_as_normal				= '<span class="message_options" onclick="set_messages_flag(\'unflagged\','+info_msg.msg_number+');write_msg(\''+get_lang('Message marked as ')+get_lang("Normal")+'.\');">'+get_lang("Normal")+'</span> | ';
+	var option_mark_as_unseen				= '<span class="message_options" onclick="proxy_mensagens.proxy_set_messages_flag(\'unseen\','+info_msg.msg_number+');write_msg(\''+get_lang('Message marked as ')+get_lang("Unseen")+'.\');">'+get_lang("Unseen")+'</span>, ';
+	var option_mark_as_important			= '<span class="message_options" onclick="proxy_mensagens.proxy_set_messages_flag(\'flagged\','+info_msg.msg_number+');write_msg(\''+get_lang('Message marked as ')+get_lang("Important")+'.\');">'+get_lang("Important")+'</span>, ';
+	var option_mark_as_normal				= '<span class="message_options" onclick="proxy_mensagens.proxy_set_messages_flag(\'unflagged\','+info_msg.msg_number+');write_msg(\''+get_lang('Message marked as ')+get_lang("Normal")+'.\');">'+get_lang("Normal")+'</span> | ';
 	
 	var option_move							= '<span class="message_options" onclick=wfolders.makeWindow("'+ID+'","move_to");>'+get_lang("Move")+'</span> | ';
@@ -990,5 +1081,5 @@
 	var option_reply_without_history		= '<span class="message_options" onclick=new_message("reply_without_history","'+ID+'");>'+get_lang("Reply without history")+'</span> | ';
 	var option_reply_to_all_without_history	= '<span class="message_options" onclick=new_message("reply_to_all_without_history","'+ID+'");>'+get_lang("Reply to all without history")+'</span> | ';
-	var option_source_msg					= '<span class="message_options" onclick=source_msg("'+ID+'","'+url_encode(info_msg.msg_folder)+'");>'+get_lang("Message Font")+'</span>';	
+	var option_source_msg					= '<span class="message_options" onclick=proxy_mensagens.proxy_source_msg("'+ID+'","'+url_encode(info_msg.msg_folder)+'");>'+get_lang("Message Font")+'</span>';	
 
 	div_other_options.innerHTML = option_mark_as + option_mark_as_unseen + option_mark_as_important + option_mark_as_normal + option_move + option_reply_to_all + option_reply_without_history + option_reply_to_all_without_history + option_source_msg;
@@ -1019,5 +1110,5 @@
 		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="proxy_mensagens.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';
@@ -1105,7 +1196,18 @@
 	from_values.type = "HIDDEN";
 	from_values.value = info_msg.from.full; //Veio do IMAP, sem images nem links.
+	
+	var local_message = document.createElement("INPUT");
+	local_message.id = "is_local_"+ID;
+	local_message.name = "is_local";
+	local_message.type = "HIDDEN";
+	local_message.value = (info_msg.local_message)?"1":"0";
+	
 	tr1.appendChild(td1);
 	tr1.appendChild(from);
 	tr1.appendChild(from_values);
+	tr1.appendChild(local_message);
+	
+
+	
 	if (info_msg.reply_to){
 		var tr11 = document.createElement("TR");
@@ -1329,5 +1431,6 @@
 		for (var i=0; i<info_msg.attachments.length; i++){
 			var link_attachment = document.createElement("A");
-			link_attachment.setAttribute("href", "javascript:download_attachments('"+info_msg.msg_folder+"','"+info_msg.msg_number+"',"+i+",'"+info_msg.attachments[i].pid+"','"+info_msg.attachments[i].encoding+"')");
+			link_attachment.setAttribute("href", proxy_mensagens.link_anexo(info_msg,i));
+//			link_attachment.setAttribute("href", "javascript:download_attachments('"+info_msg.msg_folder+"','"+info_msg.msg_number+"',"+i+",'"+info_msg.attachments[i].pid+"','"+info_msg.attachments[i].encoding+"')");
 			link_attachment.innerHTML = url_decode(info_msg.attachments[i].name);
 			msg_size = info_msg.attachments[i].fsize;
@@ -1587,4 +1690,11 @@
 	}
 	td_to.appendChild(input_to);
+	
+	var forwarded_local_message = document.createElement("INPUT"); //Hidden para indicar se é um forward de uma mensagem local
+	forwarded_local_message.id = "is_local_forward"+ID;
+	forwarded_local_message.name = "is_local_forward";
+	forwarded_local_message.type = "HIDDEN";
+	forwarded_local_message.value = "0";
+	td_to.appendChild(forwarded_local_message);
 	
 	var img_search = document.createElement("IMG");
@@ -2150,5 +2260,5 @@
 			{
 				//delete_all_selected_msgs_imap();
-				delete_msgs(get_current_folder(),'selected','null');
+				proxy_mensagens.delete_msgs(get_current_folder(),'selected','null');
 			}
 		}
@@ -2158,5 +2268,5 @@
 			{
 				//delete_all_selected_msgs_imap();
-				delete_msgs(get_current_folder(),'selected','null');
+				proxy_mensagens.delete_msgs(get_current_folder(),'selected','null');
 			}
 		}
@@ -2257,5 +2367,5 @@
 			{
 				//delete_all_selected_msgs_imap();
-				delete_msgs(get_current_folder(),'selected','null');
+				proxy_mensagens.delete_msgs(get_current_folder(),'selected','null');
 			}
 		}
@@ -2265,5 +2375,5 @@
 			{
 				//delete_all_selected_msgs_imap();
-				delete_msgs(get_current_folder(),'selected','null');
+				proxy_mensagens.delete_msgs(get_current_folder(),'selected','null');
 			}
 		}
@@ -2350,4 +2460,5 @@
 }
 function draw_footer_box(num_msgs){
+	folder = get_current_folder();
 	var span_R = Element("table_message");	
 	var span_options = Element("span_options");	
@@ -2360,10 +2471,11 @@
 
 	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("Delete")+'" style="margin-left:8px" class="message_options" onclick=proxy_mensagens.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 title="'+get_lang("Import")+'" class="message_options" onclick="import_window()">'+get_lang("Import")+'</span>';
-	
+	if(preferences.use_local_messages==1)
+		span_options.innerHTML += ' | <span title="'+get_lang("Archive")+'" class="message_options" onclick="archive_msgs(\''+folder+'\',null)">'+get_lang("Archive")+'</span>';
 	if (use_spam_filter) {
 		if ( get_current_folder() == 'INBOX'+cyrus_delimiter+'Spam' )	{
