Index: trunk/expressoMail1_2/js/main.js
===================================================================
--- trunk/expressoMail1_2/js/main.js	(revision 1035)
+++ trunk/expressoMail1_2/js/main.js	(revision 1121)
@@ -51,6 +51,37 @@
 	cExecute ("$this.functions.get_preferences", save_preferences);
 	setTimeout('auto_refresh()', time_refresh);
-}
-
+	if(window.google && google.gears)
+		if(expresso_local_messages.is_offline_installed())
+			check_mail_in_queue();
+}
+
+function init_offline(){
+	if (account_id != null) {
+		if (!is_ie) 
+			Element('tableDivAppbox').width = '100%';
+		else
+			connector.createXMLHTTP();
+		Element('divStatusBar').innerHTML = '<table height="16px" border=0 width=100% cellspacing=0 cellpadding=2>' +
+		'<tr>' +
+		'<td style="padding-left:17px" width=33% id="content_quota" align=left></td>' +
+		'<td width=33% height=16px align=center nowrap><font face=Verdana, Arial, Helvetica, sans-serif color=#000066 size=2>' +
+		'<b>ExpressoMail Offline</b><font size=1><b> - Versão 1.0</b></font></td>' +
+		'<td width=33% id="div_menu_c3" align=right></td>' +
+		'</tr></table>';
+			
+		ConstructMenuTools();
+
+		draw_tree_folders();
+				
+		proxy_mensagens.messages_list('local_Inbox', 1, preferences.max_email_per_page, 'SORTARRIVAL', null, 1, function handler(data){
+			draw_box(data, 'local_Inbox');
+		})
+		
+		// Get cyrus delimiter
+	cyrus_delimiter = Element('cyrus_delimiter').value;
+		
+	//cExecute ("$this.functions.get_preferences", save_preferences);
+	}
+}
 /**
  * Carrega a applet java no objeto search_div
@@ -406,5 +437,6 @@
 		
 	string_msgs_in_main = tmp.substring(0,(tmp.length-1));
-	cExecute ("$this.imap_functions.refresh&folder="+current_folder+"&msgs_existent="+string_msgs_in_main+"&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, handler_refresh);
+	if(!expresso_offline)
+		cExecute ("$this.imap_functions.refresh&folder="+current_folder+"&msgs_existent="+string_msgs_in_main+"&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, handler_refresh);
 }
 
@@ -1105,6 +1137,9 @@
 						nomeArquivo = nomeArquivo.substring(0,nomeArquivo.length - 4); //Anexos no gears são todos com extensão .php. tenho que tirar a extensão para ficar o nome real do arquivo.
 						campo_arquivo = addForwardedFile(new_border_ID,nomeArquivo,link_attachment.href);
+						if(!expresso_offline)
+							expresso_local_messages.getInputFileFromAnexo(campo_arquivo, tmp);
+						else //To offline, you just set the url on value of a hidden input.
+							campo_arquivo.value = tmp;
 						
-						expresso_local_messages.getInputFileFromAnexo(campo_arquivo, tmp);
 					}
 					else {
@@ -1340,5 +1375,4 @@
     var sign = false;
 	var crypt = false;
-
     if ((preferences.use_assinar_criptografar != '0') && (preferences.use_signature_digital_cripto != '0')){
 		var checkSign = document.getElementById('return_digital_'+ID)
@@ -1378,5 +1412,4 @@
 		return;
     }
-
 	if(data && data.success == true ){
 		// if send ok, set a flag as answered or forwarded
@@ -1385,14 +1418,21 @@
 
 		if (msg_number_replied){
-			set_message_flag(msg_number_replied.value, 'answered');
+			proxy_mensagens.proxy_set_message_flag(msg_number_replied.value, 'answered');
 		}
 		else if (msg_number_forwarded){
-			set_message_flag(msg_number_forwarded.value, 'forwarded');
-		}
-		if(wfolders.alert){
-			write_msg(get_lang('Your message was sent and save.'));
-			wfolders.alert = false;
+			proxy_mensagens.proxy_set_message_flag(msg_number_forwarded.value, 'forwarded');
+		}
+		if(expresso_offline){
+			write_msg(get_lang('Your message was sent to queue'));
+			delete_border(ID,'true');
+			return;
 		}else{
-			write_msg(get_lang('Your message was sent.'));
+			if (wfolders.alert) {
+				write_msg(get_lang('Your message was sent and save.'));
+				wfolders.alert = false;
+			}
+			else {
+				write_msg(get_lang('Your message was sent.'));
+			}
 		}
 		// If new dynamic contacts were added, update the autocomplete ....
@@ -1417,4 +1457,6 @@
 			write_msg(get_lang("Connection failed with %1 Server. Try later.", "Web"));
 	}
+	if(!expresso_offline)
+		connector.hideProgressBar();
 }
 
@@ -1523,5 +1565,24 @@
 	}
 	
-	cExecuteForm ("$this.imap_functions.send_mail", form, handler_send_message, ID);
+	if (expresso_offline) {		
+		stringEmail = Element("to_"+ID).value;
+		stringEmail += Element("cco_"+ID).value =='' ? "":", "+Element("cco_"+ID).value;
+		stringEmail += Element("cc_"+ID).value =='' ? "":", "+Element("cc_"+ID).value;
+		var invalidEmail = validateEmail(stringEmail);	 
+		if(Element("to_"+ID).value=="" && Element("cco_"+ID).value=="" && Element("cc_"+ID).value=="") {
+			write_msg(get_lang("message without receiver."));
+			return;
+		}else if(invalidEmail[0] == true){
+			write_msg("Os endereços de destinatário a seguir estão incorretos: "+invalidEmail[1]);
+			return;
+		}
+
+		sucess = expresso_local_messages.send_to_queue(form);
+		var data_return = new Array();
+		data_return.success = sucess;
+		handler_send_message(data_return,ID);
+	}
+	else 
+		cExecuteForm("$this.imap_functions.send_mail", form, handler_send_message, ID);
 }
 
@@ -2048,5 +2109,5 @@
 			set_messages_flag("unseen", "get_selected_messages");
 		else
-			set_message_flag(msgs_number, "unseen");
+			proxy_mensagens.proxy_set_message_flag(msgs_number, "unseen");
    	    proxy_mensagens.proxy_move_messages(folder, msgs_number, border_ID, new_folder, new_folder_name);
 	}
@@ -2221,2 +2282,91 @@
 	}
 }
+function hack_sent_queue(data,rowid_message) {
+
+	if (data.success != true) {
+		queue_send_errors = true;
+		expresso_local_messages.set_problem_on_sent(rowid_message,data);
+	}
+	else {
+		expresso_local_messages.set_as_sent(rowid_message);
+		if(document.getElementById('_action')) { //Não posso manter esse elemento, pois o connector irá criar outro com o mesmo id para a próxima mensagem.
+			el =document.getElementById('_action');
+			father = el.parentNode;
+			father.removeChild(el);
+		}
+		send_mail_from_queue(false);
+	}
+}
+
+function send_mail_from_queue(first_pass) {
+	if(first_pass)
+		modal('send_queue');
+	var num_msgs = expresso_local_messages.get_num_msgs_to_send();
+	if (num_msgs <= 0) {
+		close_lightbox();
+		return;
+	}
+	document.getElementById('text_send_queue').innerHTML = get_lang('Number of messages to send:')+' '+num_msgs;	
+	var handler_send_queue_message = function(data,rowid_message) {
+		hack_sent_queue(data,this.ID_tmp);
+	}
+	var msg_to_send = expresso_local_messages.get_form_msg_to_send();
+	if(!is_ie)
+		ID_tmp = msg_to_send.rowid.value;
+	else {//I.E kills me of shame...
+		for (var i=0;i<msg_to_send.length;i++) {
+			if(msg_to_send.elements[i].name=='rowid') {
+				ID_tmp = msg_to_send.elements[i].value;
+				break;
+			}
+		}
+	}
+
+	cExecuteForm("$this.imap_functions.send_mail", msg_to_send, handler_send_queue_message,"queue_"+ID_tmp);
+}
+
+function check_mail_in_queue() {
+	var num_msgs = expresso_local_messages.get_num_msgs_to_send();
+	if(num_msgs>0) {
+		control = confirm(get_lang('You have messages to send. Want you to send them now?'));
+		if(control) {
+			send_mail_from_queue(true);
+		}
+		return true;
+	}
+	else {
+		return false;
+	}
+}
+
+function force_check_queue() {
+	if(!check_mail_in_queue()) {
+		write_msg(get_lang("No messages to send"));
+	}
+}
+
+function validateEmail(emailString){
+		var arrayInvalidEmails = new  Array();
+		arrayInvalidEmails[1] = '';		
+		var email;
+		var arrayEmailsFull = new Array();
+		arrayEmailsFull = emailString.split(',');
+		var er_Email =  new RegExp("<(.*?)>");		
+		var er_ValidaEmail = new RegExp("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$");
+
+		for (i=0; i < arrayEmailsFull.length; i++){
+			email = er_Email.exec(arrayEmailsFull[i]);
+			tempEmail = email == null  ? arrayEmailsFull[i]:email[1];
+			tempEmail = tempEmail.replace(/^\s+|\s+$/g,"");
+			if (tempEmail != '') {
+				singleEmail = er_ValidaEmail.exec(tempEmail);
+				
+				if (singleEmail == null) {
+					arrayInvalidEmails[0] = true;
+					arrayInvalidEmails[1] += (email == null ? arrayEmailsFull[i] : email[1]) + "; ";
+				}
+			}	
+		}
+		
+		return arrayInvalidEmails;
+}
