Kwo.Cart = {

  addPurchase: function(item_key, quantity) {
    var args = {"quantity": 1};
    if (Object.isElement(item_key)) {
      item_key = $(item_key);
      if ($(item_key).tagName.toUpperCase() === "FORM") {
        args = item_key;
      }
      else if ($(item_key).readAttribute("data-item-key")) {
        args["item_key"] = $(item_key).readAttribute("data-item-key");
      }
    }
    else {
      args["item_key"] = item_key;
      args["quantity"] = quantity || 1;
    }
    Kwo.exec("/shop/cart.purchase.add", args,
             {callback: Kwo.Cart.onPurchaseCallback});
  },
  
  confirmPurchase: function(msg) {
    if ("purchaseConfirm" in window) {
      window.purchaseConfirm.call(this, msg);
    }
    else {
      Kwo.warn(msg);
    }
  },

  empty: function(arg) {
    Kwo.exec("/shop/cart.empty", null, 
             {callback: Kwo.Cart.onCartUpdate, "confirm": arg});
  },

  onPurchaseCallback: function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res);
    Kwo.Cart.updateWidget();
    new Kwo.Dialog("/shop/purchase.confirm", {"item_key": res["result"]["item_key"]},
                   {width:500, height:200, className:"layout-hbox"});
  },
  
  update: function(args) {
    Kwo.exec("/shop/cart.update", args, 
             {callback: Kwo.Cart.onCartUpdate});
  },

  onQuantityChange: function(elt) {
    elt = $(elt);
    elt.up("TABLE").select(".total .quantity A")[0].show();
  },

  onCartUpdate: function(res) {
    if (Kwo.hasError(res)) {
      Kwo.error(res); 
      Kwo.Cart.view();
      return ;
    }
    if (res["result"]["purchase_count"] >= 1) {
      Kwo.Cart.view();
    }
    else {
      Kwo.go("/shop");
    }    
  },
  
  updateAmounts: function() {
    Kwo.exec("/shop/order.update", $("kwo-order-form"), 
             {callback: function (res) {
               if (Kwo.hasError(res)) return Kwo.error(res);
               $("kwo-amounts-box").update('<img src="/app/sys/pix/throbbers/search.gif" />');
               Kwo.exec("/shop/cart.amounts", $("kwo-order-form"), 
                        {container: "kwo-amounts-box"});
             }});
    
  },
  
  view: function() {
    Kwo.go("/shop/cart");
  },

  updateWidget: function() {
    if ($("kwo-cart-widget")) {
      Kwo.exec("/shop/cart.widget", null, 
               {"container": "kwo-cart-widget"});
    }
  }
  
};

Kwo.TPE = {

  "onSubmit": function(args) {
    Kwo.exec("/shop/transaction.store", args, 
             {"container": "kwo-container-tpe"});
  }

}

Kwo.Order = {

  onFinalize: function(args) {
    Kwo.exec("/shop/order.finalize", args, 
             {callback: Kwo.Order.onCallback, disable: true});
  },

  compose: function() {
    Kwo.go("/shop/order");
  },

  onCallback: function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res);
    $("payment-container").previous("DIV").hide();
    Kwo.exec("/shop/payment.request", 
             {id: res["result"]["id"]}, 
             {container: "payment-container"});
  }
  
};

Kwo.Coupon = {
  
  "check": function(args) {
    Kwo.exec("/shop/coupon.check", [args, $("kwo-order-form")], 
             {callback: Kwo.Coupon.onCallback, disable:true});
  },

  "onCallback": function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res);
    Kwo.getDialog().apply(res["result"]["coupon_code"], 
                          res["result"]["coupon_id"]);
  }
  
};

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

  initialize: function($super, input) {
    this.name = "coupon";
    this.input = input;
    this.className = "layout-hbox";
    $super(this.refresh, {}, {height: 210});
  },
  
  refresh: function(args) {
    Kwo.exec("/shop/coupon.select", args, 
             {container: this.support});
  },
  
  apply: function(code, id) {
    this.close();
    $("coupon-link-box", "coupon-code-box").invoke("toggle");
    $("coupon_id").setValue(id);
    $("coupon-code-box").update(code);
    Kwo.Cart.updateAmounts();
  }

});


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

  initialize: function($super, args) {
    this.name = "addressee";
    this.args = {"id": args ? args : 0};
    this.className = "layout-hbox";
    $super("/shop/addressee.edit", this.args, {height: 430});
  },
  
  onSave: function(elt) {
    elt = $(elt);
    Kwo.exec("/shop/addressee.save", elt,
             {callback: this.onCallback.bind(this), disable:true});
  },

  onCallback: function(res) {
    if (Kwo.hasError(res)) return Kwo.error(res);
    Kwo.exec("/shop/order.section.addressee", {id: res["result"]["id"]},
             {container: $("addressees")});
    this.close();
    Kwo.Cart.updateAmounts();
  }

});



Kwo.Composer.Return = Class.create(Kwo.Dialog, {

  initialize: function($super, elt) {
    this.name = "return";
    this.layout = "hbox";
    this.args = {"id": $(elt).up("TR").readAttribute("data-id")};
    this.width = 500;
    this.height = 350;
    $super("/shop/order.return.compose");
  },

  onSubmit: function(elt) {
    Kwo.exec("/shop/order.return.save", [this.args, $(elt)], 
             {callback:function () { Kwo.reload(); }});
  }

});
