
/**
 * Add / Edit incoming mailbox form behavior
 */
App.widgets.AddEditIncomingMailboxForm = function() {

  // Public interface
  return {
    
    /**
     * Initialize behavior
     *
     * @param String wrapper_id
     * @param String test_mailbox_connection_url
     */
    init : function(wrapper_id, test_mailbox_connection_url) {
      var mailbox_form = $('#' + wrapper_id);
      var result_container = $('#test_connection .test_connection_results',mailbox_form);
      var result_image = $('img:eq(0)', result_container);
      var result_output = $('span:eq(0)', result_container);
      
      $('#test_connection button').click(function () {
    	  result_container.removeClass('connection_ok connection_error');
    	  result_image.attr('src', App.Wireframe.Utils.indicatorUrl());
    	  result_output.text(App.lang('Testing mailbox...'));
    	  $.ajax({
	    	'type' : 'post',
	    	'url' : App.extendUrl(test_mailbox_connection_url, { 'async' : 1 }), 
	    	'data' : {
	            "mailbox[host]" : mailbox_form.find("#hostName").val(),
	            "mailbox[server_type]" : mailbox_form.find("#mailboxType input:checked").val(), 
	            "mailbox[security]" : mailbox_form.find("#mailboxSecurity input:checked").val(), 
	            "mailbox[port]" : mailbox_form.find("#mailboxPort").val(), 
	            "mailbox[mailbox]" : mailbox_form.find("#mailboxName").val(), 
	            "mailbox[username]" : mailbox_form.find("#username").val(), 
	            "mailbox[password]" : mailbox_form.find("#password").val(), 
	            'submitted' : 'submitted' 
	          },
	          'success' : function(response) {
	        	  result_output.text(App.lang('Connected to mailbox. You have :num unread messages',{'num' : response }));
	        	  result_image.attr('src', App.Wireframe.Utils.indicatorUrl('ok'));
	              result_container.removeClass('connection_error');
	              result_container.addClass('connection_ok');
	          },
	          'error' : function(response) {
	        	  var message = App.Wireframe.Utils.responseToErrorMessage(response);
	        	  result_output.text(message);
	              result_image.attr('src', App.Wireframe.Utils.indicatorUrl('error'));
	              result_container.removeClass('connection_ok');
	              result_container.addClass('connection_error');
	          }
	      });
	      
	      return false;
      });
         
      
      // Change port value when type is change and port value is empty or 
      // uses default value of other type
      var type_change_handler = function() {
        var port_field = $('#mailboxPort');
        
        if($(this).val() == 'POP3') {
          if(port_field.val() == '' || port_field.val() == '143') {
            port_field.val('110').parent().highlightFade();
          } // if
        } else {
          if(port_field.val() == '' || port_field.val() == '110') {
            port_field.val('143').parent().highlightFade();
          } // if
        } // if
      };
      
      $('#mailboxType').click(type_change_handler).change(type_change_handler);
    }
    
  };
}();


/**
 * Add / Edit incoming mailbox form behavior
 */
App.widgets.RenderProjectElement = function() {

  // Public interface
  return {
    
    /**
     * Initialize behavior
     *
     * @param String wrapper_id
     */
    init : function(wrapper_id, filter_id, action_class_name, to_url) {
	  
	  var action_form_box = $("#" + wrapper_id);
	  
	  var project_select_box = $("#" + wrapper_id + " .project_box");
	  var insert_into_div = $("#" + wrapper_id + " .render_project_settings");
	  project_select_box.change(function(){
	  	renderProjectElement();
	  });
	  renderProjectElement();
	  
	  function renderProjectElement() {

	  	var project_id = project_select_box.val();

	  	var incoming_mail_object_name = $('.working_box #incoming_mail_action_name').val();
	  	
	  	$.ajax({
	        'url' : App.extendUrl(to_url, { async : 1 }),
	        'type' : 'post',
	        'data' : 
	            { 
	              'submitted' : 'submitted', 
	              'project_id' : project_id, 
	              'filter_id' : filter_id,
	              'action_class_name' : action_class_name
	            },  
	        'success' : function(response) {
	      	  insert_into_div.html(response);
	      	}, 
	        'error' : function(error) {
	          
	        }
	      });
	  	
	  }//renderProjectElement
	  set_create_as_text();
	  function set_create_as_text() {
		var notify_sender = action_form_box.find("#notify_sender");
		var object_name = notify_sender.attr('object_name');
		if(action_form_box.find(".create_as:checked").val() == "sender") {
    	  notify_sender.next("label").html(App.lang('Notify sender about :object_name being created',{object_name : object_name}));
        } else {
           notify_sender.next("label").html(App.lang('Notify user about :object_name being created in his name',{object_name : object_name}));
        }
	  }
	 action_form_box.delegate(".create_as",'click', function() {
		set_create_as_text();
	 });
	 
	 set_notify_sender();
	 function set_notify_sender() {
		var create_public_page = action_form_box.find("#create_public_page");
		if(action_form_box.find("#notify_sender:checked").val() == 1){
			create_public_page.removeAttr('disabled');
		} else {
			create_public_page.attr('disabled','disabled');
		}
	 }
	 action_form_box.delegate('#notify_sender','click', function() {
		set_notify_sender();
	 });

		
	//priority section
	set_priority();
	function set_priority() { 
		var task_priority = action_form_box.find("#task_priority");
		if(action_form_box.find(".use_message_priority:checked").val() == "use_custom_priority"){
			task_priority.removeAttr('disabled');
		} else {
			task_priority.attr('disabled','disabled');
		}
	}
	action_form_box.delegate('.use_message_priority','click', function() {
		set_priority();
	});
	  
	  
  }//init
    
  };
}();