$(document).ready(function(){
  if (window.rcmail) {
    rcmail.addEventListener('init', function(evt) {
    
      rcmail.register_command('plugin.hmail_forwarding-save', function() { 
        var input_address = $("[name='_forwardingaddress']");
        var input_enabled = rcube_find_object('_forwardingenabled');   

        if(input_address.val()){
          if(!rcube_check_email(input_address.val(), false)){
            parent.rcmail.display_message(rcmail.gettext('invalidaddress','hmail_forwarding'),'error');
            input_address.focus();
          }
          else{
            document.forms.forwardingform.submit();
         }
        }
        else{
          if(input_enabled.checked){
            input_enabled.checked = false;
          }
        }
      }, true);
    })
  }
});
