Kwo.Cart = {

  "addPurchase": function(model_id, record_id, quantity) {
    var args = {"model_id": model_id, "record_id": record_id};
    args["quantity"] = quantity || 1;
    Kwo.exec("/shop/cart.add_purchase", 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(h) {
    if (h["error"] >= 1) {
      Kwo.error(h["result"]["msg"]); 
      return ;
    }
    
    Kwo.warn(h["result"]["callback_msg"]);
    Kwo.Cart.updateWidget();
  },
  
  "update": function(args) {
    Kwo.exec("/shop/cart.update", args, 
             {"callback": Kwo.Cart.onCartUpdate});
  },

  "onCartUpdate": function(h) {
    if (h["error"] >= 1) {
      Kwo.error(h["result"]["msg"]); 
      Kwo.Cart.view();
      return ;
    }
    if (h["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", 
              "callback": function() { $("kwo-container-tpe").down("FORM").submit(); }});
  }

}

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(h) {
    if (h["error"] >= 1) {
      Kwo.Order.button.enable();
      return Kwo.error(h["result"]["msg"]);
    }
    Kwo.exec("/shop/order.payment", 
             {"id": h["result"]["id"]}, 
             {"container": "payment-container",
              "callback": function() { $("payment-container").down("FORM").submit(); }});
  }
  
};

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(h) {
    Kwo.Coupon.button.enable();
    if (h["error"] >= 1) {
      Kwo.error(h["result"]["msg"]);
      return ;
    }
    Kwo.getDialog().apply(h["result"]["coupon_code"], 
                          h["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();
  }

});