Kwo.Cart = {

  addPurchase: function(item_key, quantity) {
    Kwo.exec("/shop/cart.purchase.add", 
             {"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});
  },

  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-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 = {

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

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

  "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, $("kwo-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.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: 420});
  },
  
  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.Shop = {

  onOrderLoaded: function() {
    Kwo.Cart.updateAmounts();
    
    $("billing_profile[country_id]").observe("change", Kwo.Cart.updateAmounts);
    
/*     if ($("shipping_profile[country_id]")) {
      
      $("shipping_profile[country_id]").observe("change", function() { 
        if ($F("shipping_profile[country_id]") >= 1) {
          Kwo.Cart.updateAmounts();
        }
      });
      
     $("shipping_address_type[1]").observe("click", function() { 
        $("delivery_address").hide();
        if ($F("shipping_profile[country_id]") >= 1) {
          Kwo.Cart.updateAmounts();
        }
      });
      
      $("shipping_address_type[2]").observe("click", function() { 
        $("delivery_address").show();
        if ($F("shipping_profile[country_id]") >= 1) {
          Kwo.Cart.updateAmounts();
        }
      }); 
      

      
    }*/

      if ($("shipping[1]")) {
        $("shipping[1]").observe("click", Kwo.Cart.updateAmounts);
      }
      
      if ($("shipping[2]")) {
        $("shipping[2]").observe("click", Kwo.Cart.updateAmounts);
      }
    
    if ($("use_credit[0]")) {
      $("use_credit[0]").observe("click", Kwo.Cart.updateAmounts);
      $("use_credit[1]").observe("click", Kwo.Cart.updateAmounts);
    }
  }
  
}

