﻿// JScript File

// Copyright 2001-2006 2night Entertainment Corp.
// The total or partial reproduction of its contents is prohibited

/*--------------------------------------------------------
 *  QuickPhoto.Order: Ajax style album management js Class 
 *  manage album, photos and individal photo
 */ 
Type.registerNamespace("QuickPhotoClient");

QuickPhotoClient.Cart = function(cartCtrl,sumCtrl)
{
    this._cartPage = "StoreCart.aspx";
    this._order = null;
    this._curCtrl = null;
    this._sumCtrl = null;
    this._cartCtrl = null; 
    if(cartCtrl) this._cartCtrl = cartCtrl;
    if(sumCtrl) this._sumCtrl = sumCtrl;
    QuickPhotoClient.Cart.initializeBase(this);
}

QuickPhotoClient.Cart.prototype={ 
    set_Order:function(v){this._order = v;this.Update();},
    get_Order:function(){return this._order;},
    get_CurCtrl:function(){return this._curCtrl;},
    set_CartPage:function(v){this._cartPage = v;},
    get_CartPage:function(){return this._cartPage;},
    
    initialize:function(){
       this._order = new QuickPhotoEntity.Order();
       this._order.Orders = [];
    },
    
    Update:function(){
        if(this._sumCtrl)this._sumCtrl.innerHTML = "";
       if(this._cartCtrl) this._cartCtrl.innerHTML = "";
       if(this._order.Orders.length < 1) {
            if($get("carts"))
                $get("carts").innerHTML = "<p><h3>No tienes productos en tu carreta.</h3></p>";
            if($get("carthead"))
                $get("carthead").style.display = "none"; 
              return;  
       }
       
        if($get("carthead"))
                $get("carthead").style.display = "block"; 
       
       if(this._cartCtrl){

           var n = document.createElement("i");
           n.style.width = "150px";
           var img = document.createElement("img");
           img.setAttribute("class","thumb");
           img.style.height="40px";
           img.style.width= "40px";
           img.style.marginRight="7px";
               
           for(i=0; i< this._order.Orders.length;i++){
              var itm = this._order.Orders[i];
              var pp = n.cloneNode(false);
              var p = img.cloneNode(false);
              itm.Photo = itm.Photo.split(".jpg")[0]+"a.jpg"
              p.setAttribute("src",itm.Photo);
              pp.appendChild(p);
              var s =itm.PriceStr;
             var ss = document.createElement("span");
             ss.innerHTML = s;
              pp.appendChild(ss);
              
              var remove = document.createElement("a");
                remove.setAttribute("href","javascript:cart.Remove2("+itm.OrderItemID+");"); 
               remove.setAttribute("class","cartlink"); 
              remove.style.color = "#fff";
              remove.style.marginLeft="3px"; 
              remove.innerHTML ="Quitar";
              pp.appendChild(remove);
              
              this._cartCtrl.appendChild(pp);
           }
      }
      
      if(this._sumCtrl){         
          var itms = document.createElement("i");
          itms.appendChild(document.createTextNode(this._order.TotalItem));
          
          this._sumCtrl.appendChild(itms);
          
          var price = itms.cloneNode(false);
           price.appendChild(document.createTextNode( this._order.TotalStr));
           this._sumCtrl.appendChild(price);
      }           
    },
   
    Add : function(photo,up){
        if (typeof( this._order.Orders) == "undefined")
            this._order.Orders = [];
                       
         var o = new QuickPhotoEntity.OrderItem();
         o.ProductID = up.split(':')[1];
         o.Quantity = 1;
         o.Photo = photo;
         o.UnitPrice = up.split(':')[0];
         o.UnitPrice = Math.round(o.UnitPrice*100)/100;
        
         // already have. 
         for(i = 0; i < this._order.Orders.length;i++)
         {
             if(this._order.Orders[i].ProductID == o.ProductID)
             {
                alert("Ya tienes este producto en tu carreta.");
                return; 
             } 
         }   
         
          $get("cartLoading").style.display = "block";
         var srv = QuickPhotoWebSite.OrderService;
         srv.AddToStoreCart(o.ProductID ,1,this.SubmitOnComplete,this.OnFailed,this);
    },
   
     Add2 : function(p){
        if (typeof( this._order.Orders) == "undefined")
            this._order.Orders = [];
                       
         var o = new QuickPhotoEntity.OrderItem();
         o.ProductID = p.ProductID;
         o.Quantity = 1;
         o.Photo = p.Thumbnail;
         o.UnitPrice = p.UnitPrice;
         o.UnitPrice = Math.round(o.UnitPrice*100)/100;
        
         // already have. 
         for(i = 0; i < this._order.Orders.length;i++)
         {
             if(this._order.Orders[i].ProductID == o.ProductID)
             {
                alert("Ya tienes este producto en tu carreta.");
                return; 
             } 
         }   
         
          $get("cartLoading").style.display = "block";
         var srv = QuickPhotoWebSite.OrderService;
         srv.AddToStoreCart(o.ProductID ,1,this.SubmitOnComplete,this.OnFailed,this);
    }, 
    
    Submit:function(){
        var srv = QuickPhotoWebSite.OrderService;
             $get("cartLoading").style.display = "block";
        srv.AddToStoreCarts(this._order,this.SubmitOnComplete,this.OnFailed,this);
    },

    SubmitOnComplete: function(result,a){
         if(result)cart.set_Order(result);  
          $get("cartLoading").style.display = "none";
    },    
    
     SubmitOrderOnComplete: function(result,a){
        if(result)cart.set_Order(result);  
          $get("cartLoading").style.display = "none";
    },    
    
    SubmitOrder:function(){
     // window.location.href="checkout.aspx?from=store";
       var srv = QuickPhotoWebSite.OrderService;
        $get("cartLoading").style.display = "block";
        srv.AddToStoreCarts(this._order,this.SubmitOrderOnComplete,this.OnFailed,this);
    },
       
    Remove:function(orderItemID, orderCtrl){
     $get("cartLoading").style.display = "block";
      var srv = QuickPhotoWebSite.OrderService;
      srv.Remove(orderItemID,this.RemoveOnComplete,this.OnFailed,orderCtrl);
    },
   
     Remove2:function(orderItemID){
     $get("cartLoading").style.display = "block";
      var srv = QuickPhotoWebSite.OrderService;
      srv.Remove(orderItemID,this.RemoveOnComplete,this.OnFailed);
    }, 
    
    RemoveOnComplete:function(result, context){
         $get("cartLoading").style.display = "none";
              if(context){
               var ctrl = $get(context);
               if(ctrl){
                  ctrl.parentNode.removeChild(ctrl);
               }
            }
            cart.set_Order(result); 
    },
    
    Decrease:function(oid,qtyCtrl){
       for(var i = 0; i<this._order.Orders.length; i++){
          if(this._order.Orders[i].OrderItemID == oid){
            if( this._order.Orders[i].Quantity>0){
              var o = this._order.Orders[i];
              if(o.Quantity == 0){return;}
              
              o.Quantity --;
              
              if(qtyCtrl){
                 var q =  $get(qtyCtrl);
                 if(q){
                    q.value = this._order.Orders[i].Quantity;
                  }
              }
              
                   $get("cartLoading").style.display = "block";
               var srv = QuickPhotoWebSite.OrderService;
               srv.AdjustQuantity(o.OrderItemID, o.Quantity,this.AdjustOnComplete,this.OnFailed,this); 
            }
          }
       }
    },
    
    Increase:function(oid, qtyCtrl){
     $get("cartLoading").style.display = "block";
        for(var i = 0; i<this._order.Orders.length; i++){
          if(this._order.Orders[i].OrderItemID == oid){
             var o = this._order.Orders[i];
               o.Quantity ++;
              if(qtyCtrl){
                 var q =  $get(qtyCtrl);
                 if(q){
                    q.value = this._order.Orders[i].Quantity;
                  }
              }
              
               var srv = QuickPhotoWebSite.OrderService;
               srv.AdjustQuantity(o.OrderItemID, o.Quantity,this.AdjustOnComplete,this.OnFailed,this); 
          }
       }
    },
   
   adjustQuantity:function(oid,qtyCtrl){
          for(var i = 0; i<this._order.Orders.length; i++){
             if(this._order.Orders[i].OrderItemID == oid){
                var o = this._order.Orders[i];
                if(o.Quantity == qtyCtrl.value) return; 
             }
          }
            $get("cartLoading").style.display = "block";
          var srv = QuickPhotoWebSite.OrderService;
          srv.AdjustQuantity(o.OrderItemID, qtyCtrl.value,this.AdjustOnComplete,this.OnFailed,this);  
   }, 
   
     captureInput:function(oid,evt,qtyCtrl){
            var key;

         if(window.event)
              key = window.event.keyCode;     //IE
         else
              key = evt.which;     //firefox
    
        if (key == 13)
        {
           evt.preventDefault(); 
           cart.adjustQuantity(oid,qtyCtrl); 
        }else
       {
          if(key == 127 || key == 8) return true;
          else if ((key < 48) || (key > 57)) {
                   evt.preventDefault(); 
                  return false; 
           } else 
          return true; 
       } 
   }, 
    
    AdjustOnComplete:function(result,a){
      cart.set_Order(result);  
         $get("cartLoading").style.display = "none";
         cart.Update(); 
    },
    
    Clear:function()
    {
       $get("cartLoading").style.display = "block";
       this._order.Total = 0.00;
       this._order.TotalItem  = 0;
       var srv = QuickPhotoWebSite.OrderService;
       srv.Clear(1,this.ClearOnComplete,this.OnFailed);
    },
    
    ClearOnComplete:function(result, cartCtrl){
     cart.set_Order(result);   
         $get("cartLoading").style.display = "none";
        cart.Update();
    },
    
    OnFailed: function(e){
     $get("cartLoading").style.display = "none";
       if(e){
          if(e.Message){
             alert(e.Message);
          }
       }
    },
   
    stopCheckout:function(){
             if (typeof( this._order.Orders) == "undefined" || this._order.TotalItem < 1){
              if(typeof( lnkCheckout) != "undefined"){
                if($get(lnkCheckout)){
                    $addHandler($get(lnkCheckout),"click",function (e){
                        e.preventDefault();
                        alert("Para iniciar tu compra debes tener productos en tu carreta.");
                    });
                }
              } 
              }
    }, 
    
    dispose : function() {
    }
}

QuickPhotoClient.Cart.registerClass('QuickPhotoClient.Cart', Sys.Component);

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

var cart = false;

$addHandler(window, "load", function(){
        cart = new QuickPhotoClient.Cart($get("spnCart"),$get("cartSum"));
         if(typeof(cartOrder)!="undefined")
            cart.set_Order(Sys.Serialization.JavaScriptSerializer.deserialize(cartOrder));
         cart.stopCheckout();
});