Kwo.Cart = {

  "addPurchase": function(item_key, quantity) {
    Kwo.exec("/shop/cart.add_purchase", 
             {"item_key": item_key, "quantity": quantity || 1}, 
             {"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.warn(res);
    Kwo.Cart.updateWidget();
  },
  
  "update": function(args) {
    Kwo.exec("/shop/cart.update", args, 
             {"callback": Kwo.Cart.onCartUpdate});
  },

  "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.home();
    }    
  },
  
  "updateAmounts": function() {
    Kwo.exec("/shop/cart.amounts", $("_order_form"), 
             {"container": "_order_amount_box", "signal": true});
  },
  
  "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 = {

  "button": false,
  
  "create": function(args) {
    Kwo.Order.button = $(args).select("input[type=submit]")[0];
    Kwo.Order.button.disable(); 
    Kwo.exec("/shop/order.create", args, {"callback": Kwo.Order.onCallback});
  },

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

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

Kwo.Coupon = {
  
"button": false,

  "check": function(args) {
    Kwo.Coupon.button = $(args).select("input[type=submit]")[0];
    Kwo.exec("/shop/coupon.check", [args, $("_order_form")], 
             {"callback": Kwo.Coupon.onCallback});
  },

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

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

  "initialize": function($super, input) {
    this.input = input;
    $super(this.refresh, {}, {"height": 210});
    Kwo.setDialog(this);
  },
  
  "refresh": function(args) {
    Kwo.exec("/shop/dialog.couponpicker", args, 
             {"container": "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();
  }

});