Index: trunk/prototype/modules/mail/js/followupflag.js
===================================================================
--- trunk/prototype/modules/mail/js/followupflag.js	(revision 5625)
+++ trunk/prototype/modules/mail/js/followupflag.js	(revision 5640)
@@ -163,95 +163,101 @@
 	
 	.filter('.menu-configure-followupflag .save').click(function(){	
+		var saveFollowupflagged = function(){
+				var isDone = winElement.find('[name="done"]').is(':checked') ? 1 : 0;
+				var alarmDate = false;
+				var doneDate  = false;
+				var folder_name;
+				var folders = [];
+				var messages = [];
+				var roles = get_selected_messages_search_role().split(',');
+				for (var i=0; i < selectedMessageIds.length; i++ ){
+					if (currentTab == 0) {
+						folder_name = current_folder;
+						var messageNumber = selectedMessageIds[i];
+					}else{
+						var tr = $('[role="'+roles[i]+'"]');
+						folder_name = $(tr).attr('name'); 
+						var id = $(tr).attr('id'); 
+						var messageNumber = id.replace(/_[a-zA-Z0-9]+/,"");
+					}
+					folders.push(folder_name);
+					
+					var idFollowupflagged = winElement.find('[name="followupflagId"]').val();
+					var followupflagged = DataLayer.merge({
+						uid : User.me.id,
+						followupflagId : followupflagId, 
+						folderName : folder_name, 
+						messageNumber : messageNumber,
+						isDone: isDone,
+						backgroundColor : backgroundColor
+					}, !!idFollowupflagged ? {id: idFollowupflagged} : {});
+					
+					if (alarmDate = winElement.find('[name="alarmDate"]').datepicker("getDate")) {
+						if (alarmTime = winElement.find('[name="alarmTime"]').datepicker("getDate")) {
+							alarmDate.set({hour:alarmTime.getHours(), minute:alarmTime.getMinutes()});
+						}
+						followupflagged.alarmDeadline = alarmDate.toString('yyyy-MM-dd hh:mm:ss');
+					}
+
+					if (doneDate = winElement.find('[name="doneDate"]').datepicker("getDate")) {
+						if (doneTime = winElement.find('[name="doneTime"]').datepicker("getDate")) {
+							doneDate.set({hour:doneTime.getHours(), minute:doneTime.getMinutes()});
+						}
+						followupflagged.doneDeadline = doneDate.toString('yyyy-MM-dd hh:mm:ss');
+					}
+					
+					/**
+					 * Aplica o ícone correspondente na lista de mensagens do expressoMail
+					 */
+					if(current_folder == folder_name){
+						var flagged = $('#td_message_followup_' + messageNumber + ', tr[role="'+messageNumber+'_'+folder_name+'"] #td_message_followup_search_' + messageNumber).find(".flag-edited");
+					} else{
+						var flagged = $('tr[role="'+messageNumber+'_'+folder_name+'"] #td_message_followup_search_' + messageNumber).find(".flag-edited");
+					}
+					if(isDone){
+						flagged.find("img").attr("src", "../prototype/modules/mail/img/flagChecked.png").css("margin-left","-3px");
+					}else{
+						flagged.find("img").attr("src", "../prototype/modules/mail/img/flagEditor.png").css("margin-left","0");			
+					}
+					
+
+					if(current_folder == folder_name){
+						$('#td_message_followup_' + messageNumber + ', ' + 
+						'tr[role="'+messageNumber+'_'+folder_name+'"] #td_message_followup_search_' + messageNumber).find(".flag-edited").css("background", backgroundColor); 
+					}else{
+						$('tr[role="'+messageNumber+'_'+folder_name+'"] #td_message_followup_search_' + messageNumber).find(".flag-edited").css("background", backgroundColor); 
+					}  
+					/**
+					 * Salva ou, caso já exista, atualiza
+					 */
+					DataLayer.put('followupflagged', followupflagged);
+				}
 				
+				DataLayer.commit(false, false, function(data){
+					followupflags = DataLayer.get('followupflagged',{criteria:{order: 'id'}}, true);		
+					var id = followupflags[followupflags.length - 1].id;
+					winElement.find('[name="followupflagId"]').val(id);
+					winElement.find('.menu-configure-followupflag .save').button("option", "disabled", true);
+					winElement.find('.menu-configure-followupflag .delete').button("option", "disabled", false);
+					updateCacheFollowupflag(selectedMessageIds, folders, true);
+				});
+		
+		}		
 		var backgroundColor = winElement.find('[name="backgroundColor"]').val();
 		var followupflagId  = winElement.find('[name="name"] option:selected').val();
 		if (followupflagId == 'custom') {
 			DataLayer.put('followupflag', {name:winElement.find('[name="name"] option:selected').text(), uid:User.me.id});
-			DataLayer.commit();
-			
-			followupflags = DataLayer.get('followupflag',{criteria:{order: 'id'}}, true);		
-			followupflagId = followupflags[followupflags.length - 1].id;
-			winElement.find('[name="name"] option[value="custom"]').val(followupflagId);
-		}
-		
-		
-		var isDone = winElement.find('[name="done"]').is(':checked') ? 1 : 0;
-		var alarmDate = false;
-		var doneDate  = false;
-		var folder_name;
-		var folders = [];
-		var messages = [];
-		var roles = get_selected_messages_search_role().split(',');
-		for (var i=0; i < selectedMessageIds.length; i++ ){
-			if (currentTab == 0) {
-				folder_name = current_folder;
-				var messageNumber = selectedMessageIds[i];
-			}else{
-				var tr = $('[role="'+roles[i]+'"]');
-				folder_name = $(tr).attr('name'); 
-				var id = $(tr).attr('id'); 
-				var messageNumber = id.replace(/_[a-zA-Z0-9]+/,"");
-			}
-			folders.push(folder_name);
-			
-			var idFollowupflagged = winElement.find('[name="followupflagId"]').val();
-			var followupflagged = DataLayer.merge({
-				uid : User.me.id,
-				followupflagId : followupflagId, 
-				folderName : folder_name, 
-				messageNumber : messageNumber,
-				isDone: isDone,
-				backgroundColor : backgroundColor
-			}, !!idFollowupflagged ? {id: idFollowupflagged} : {});
-			
-			if (alarmDate = winElement.find('[name="alarmDate"]').datepicker("getDate")) {
-				if (alarmTime = winElement.find('[name="alarmTime"]').datepicker("getDate")) {
-					alarmDate.set({hour:alarmTime.getHours(), minute:alarmTime.getMinutes()});
-				}
-				followupflagged.alarmDeadline = alarmDate.toString('yyyy-MM-dd hh:mm:ss');
-			}
-
-			if (doneDate = winElement.find('[name="doneDate"]').datepicker("getDate")) {
-				if (doneTime = winElement.find('[name="doneTime"]').datepicker("getDate")) {
-					doneDate.set({hour:doneTime.getHours(), minute:doneTime.getMinutes()});
-				}
-				followupflagged.doneDeadline = doneDate.toString('yyyy-MM-dd hh:mm:ss');
-			}
-			
-			/**
-			 * Aplica o ícone correspondente na lista de mensagens do expressoMail
-			 */
-			if(current_folder == folder_name){
-				var flagged = $('#td_message_followup_' + messageNumber + ', tr[role="'+messageNumber+'_'+folder_name+'"] #td_message_followup_search_' + messageNumber).find(".flag-edited");
-			} else{
-				var flagged = $('tr[role="'+messageNumber+'_'+folder_name+'"] #td_message_followup_search_' + messageNumber).find(".flag-edited");
-			}
-			if(isDone){
-				flagged.find("img").attr("src", "../prototype/modules/mail/img/flagChecked.png").css("margin-left","-3px");
-			}else{
-				flagged.find("img").attr("src", "../prototype/modules/mail/img/flagEditor.png").css("margin-left","0");			
-			}
-			
-
-			if(current_folder == folder_name){
-				$('#td_message_followup_' + messageNumber + ', ' + 
-				'tr[role="'+messageNumber+'_'+folder_name+'"] #td_message_followup_search_' + messageNumber).find(".flag-edited").css("background", backgroundColor); 
-			}else{
-				$('tr[role="'+messageNumber+'_'+folder_name+'"] #td_message_followup_search_' + messageNumber).find(".flag-edited").css("background", backgroundColor); 
-			}  
-			/**
-			 * Salva ou, caso já exista, atualiza
-			 */
-			DataLayer.put('followupflagged', followupflagged);
-		}
-		
-		DataLayer.commit(false, false, function(data){
-			followupflags = DataLayer.get('followupflagged',{criteria:{order: 'id'}}, true);		
-			var id = followupflags[followupflags.length - 1].id;
-			winElement.find('[name="followupflagId"]').val(id);
-			winElement.find('.menu-configure-followupflag .save').button("option", "disabled", true);
-			winElement.find('.menu-configure-followupflag .delete').button("option", "disabled", false);
-			updateCacheFollowupflag(selectedMessageIds, folders, true);
-		});
+			DataLayer.commit(false, false, function(data){
+				DataLayer.remove('followupflag', false);
+				DataLayer.get('followupflag');		
+				followupflags = DataLayer.get('followupflag',{criteria:{order: 'id'}}, true);		
+				followupflagId = followupflags[followupflags.length - 1].id;
+				winElement.find('[name="name"] option[value="custom"]').val(followupflagId);
+				saveFollowupflagged();
+			});
+		}else{
+			saveFollowupflagged();
+		}	
+
 	}).end()
 	
