Kwo.Sponsorship = {

  onSend: function(elt) {
    elt = $(elt);
    Kwo.exec("/account/share/sponsorship.send", elt,
//             {callback:true, disable:elt});
             {disable:elt, callback: function () { Kwo.go("/account/share/sponsorship.new"); }});
  }

}

Kwo.Class.Share = Class.create(Kwo.Dialog, {

  initialize: function($super, elt) {
    elt = $(elt);
    this.name = "share";
    this.width = 400;
    this.height = 550;
    this.className = "layout-hbox";
    this.args = {"item_key": elt.readAttribute("data-item-key"),
                 "mode": elt.readAttribute("data-mode")};
    $super(this.refresh, this.args);
  },

  onRedirect: function(service_id) {
    this.args["service_id"] = service_id;
    Kwo.go("/share/notification.redirect", this.args,
           {target: "blank"});
    return false;
  },

  onSend: function(elt) {
    form = $(elt);
    errors = 0;
    $$('.input-verif').each(function(el) {
      el = $(el);
      if(UAD.Form.checkElt(el) || el.value == '') {
        errors++;
        el.next('div.form-error', 0).show();
        el.addClassName('error');
      }
      else {
        el.next('div.form-error', 0).hide();
        el.removeClassName('error');
      }
    });

    if(errors > 0) {
      return false;
    }

    elt = $(elt);
    if ($("recipients").value.empty()) {
      $("recipients").addClassName("error");
      return ;
    }
    Kwo.exec("/share/notification.send", [elt, this.args],
             {disable:elt, reset:elt, callback:elt.down("DIV")});
  },

  refresh: function() {
    Kwo.exec("/share/notification.prompt", this.args,
             {container: this.support});
  }

});


Kwo.Class.Importer = Class.create(Kwo.Dialog, {

  initialize: function($super, elt) {
    this.name = "importer";
    this.width = 400;
    this.height = 550;
    this.className = "layout-hbox";
    $super(this.refresh, this.args);
  },

  onSend: function(elt) {
    elt = $(elt);
    if ($("recipients").value.empty()) {
      $("recipients").addClassName("error");
      return ;
    }
    Kwo.exec("/share/notification.send", [elt, this.args],
             {disable:elt, reset:elt, callback:elt.down("DIV")});
  },

  onAuth: function(elt) {
    $("import-contact-login").removeClassName("error");
    $("import-contact-pass").removeClassName("error");
    if ($("import-contact-login").value == "") {
      $("import-contact-login").addClassName("error");
    }
    if ($("import-contact-pass").value == "") {
      $("import-contact-pass").addClassName("error");
    }
    elt = $(elt);
    Kwo.exec("/share/import.contacts", elt,
             {container: $("contact-import-result")});
  },

  onSelect: function(elt) {
    elt = $(elt);
    var n = 0;
    elt.select("INPUT[type=checkbox]").each(function (e) {
      if (e.checked) n++;
    });
    if (n < 1) {
      alert("Veuillez sélectionner ou plusieurs contacts.");
      return ;
    }
    Kwo.go("/account/share/sponsorship.new", elt,
           {method: "post"});
  },

  refresh: function() {
    Kwo.exec("/share/import", null,
             {container: this.support});
  }

});

