﻿ 
 function BagItem(data){ 
        this.itemData = data.split("^");
        this.ProductId  = function (){  return this.itemData[0]; };
        this.ProductItemId = function (){  return this.itemData[1]; };
        this.ProductNumber = function (){  return this.itemData[2]; };
        this.Quantity = function (){  return this.itemData[3]; };
        this.Name = function (){  return this.itemData[4]; };
        this.Size = function (){  return this.itemData[5]; };
        this.Color = function (){  return this.itemData[6]; };
        this.Style = function (){  return this.itemData[7]; };
        this.Asset = function (){  return this.itemData[8]; };
        this.Price = function (){  return this.itemData[9]; };
        this.TotalPrice = function (){  return this.itemData[10]; };
        
    }
    
    
  function LoadShoppingBag(){
        // hide it
        hideShoppingbag();
        //load the bag contents
        BMW.Lifestyles.Web.Services.ShoppingBag.BagContents( CompletedLoadShoppingBag, FailedLoadShoppingBag);  
    }
    
    function LoadCheckoutBag(){
    
     
        BMW.Lifestyles.Web.Services.ShoppingBag.BagContents( CompletedLoadCheckOutShoppingBag);  
    }
    
    function FailedLoadShoppingBag(result, eventArgs){
        var shoppingBagContainer = $get('bagContainer');
        shoppingBagContainer.innerHTML = "<strong>Error Loading Bag!</strong>";
    }
    
    function CompletedLoadShoppingBag(result, eventArgs){
       var imageDirectory = "";
       var secureDirectory ="";
       var shoppingBagHTMLData = "";
       var shoppingBagContainer = $get('bagContainer');
       var subtotalItemFormat = "<div id=\"subtotal-value\"></div>";
       
    
       var shoppingBagHeader ="<tr class=\"table-head\">" +
                               "<th class=\"shoppingBag-item\">Item Details</th>" +
                               "<th class=\"shoppingBag-status\"></th>" +
                               "<th class=\"shoppingBag-price\">Price</th>" +
                               "<th class=\"shoppingBag-quantity\">Quantity</th>" +
                               "<th class=\"shoppingBag-total\">Total</th></tr>";
                                                  
        var shoppingBagItemFormat = "<tr><td><div class=\"thumbLink-product\"><a href=\"../ProductDetail.aspx?p={0}\">{1}</a></div>" +
                    "<div class=\"thumbLink-misc\">{8} {9} {10}</div>" +
                    "<div class=\"thumbLink-misc\">Item #: {2}</div></td>" +
                    "<td><div class=\"shoppingBag-remove\"><a href=\"javascript:updateCartItem({12},0,true)\"><img alt=\"Click to delete from bag\" src=\"{11}x_red.gif\" />Remove</a></div></td>" +
                    "<td>{3}</td><td><span class=\"price\"><a href=\"javascript:updateCartItem({12},{7},false)\">" +
                    "<img align=\"top\" alt=\"Click to decrease quantity\" border=\"0\" height=\"18\" src=\"{11}2002a_minus.gif\" " +
                    "width=\"18\" /></a>&nbsp;{4}&nbsp;<a href=\"javascript:updateCartItem({12},{6},false)\"><img align=\"top\" alt=\"Click to increase quantity\" " +
                    "border=\"0\" height=\"18\" src=\"{11}2002a_plus.gif\" width=\"18\" /></a></span></td>" +
                    "<td><span class=\"price\">{5}</span></td></tr>";
                    
        //need to change image directories if in a subdirectory
       if(location.pathname.lastIndexOf("secure") > 0){
          imageDirectory = "../images/";
          secureDirectory = "../secure/";
        }
        else{
          imageDirectory = "images/";
          secureDirectory = "secure/";
        }            
      
       
        if ( result.length == 0 ){
              shoppingBagContainer.innerHTML = "<strong>Your bag is empty.</strong>";
              
            //we have item so hide the checkout link
            var checkOutLink = $get('sbBeginCheckOut');
            checkOutLink.href='javascript:hideShoppingbag();';
            
        }
        else
        {
            var checkOutLink = $get('sbBeginCheckOut');
            checkOutLink.style.display = 'block';
            checkOutLink.href= secureDirectory + 'CheckOut.aspx';
            //reposition close link
            var closeLink = $get('closeWindow');
            closeLink.style.top = '-15px';
            
            shoppingBagContainer.innerHTML = "";
            shoppingBagHTMLData = shoppingBagHTMLData + "<table id=\"shoppingBag\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\" width=\"100%\">";
            //add header row
            shoppingBagHTMLData = shoppingBagHTMLData + shoppingBagHeader;
            
            for(i=0; i < result.length; i++){
               var bi = new BagItem(result[i]);
                var qtyPlus1 =   (parseInt(bi.Quantity())  +1);
                var qtyMinus1 = (parseInt(bi.Quantity())  -1);
                shoppingBagHTMLData = shoppingBagHTMLData + String.format(shoppingBagItemFormat,bi.ProductId(),bi.Name(),bi.ProductNumber(),bi.Price(),bi.Quantity(), bi.TotalPrice(),qtyPlus1 ,qtyMinus1 , bi.Color(), bi.Size(), bi.Style(), imageDirectory,bi.ProductItemId());
            }
          
            
            shoppingBagHTMLData = shoppingBagHTMLData + "</table>";
            //add subtotal
            shoppingBagHTMLData = shoppingBagHTMLData + subtotalItemFormat;
            shoppingBagContainer.innerHTML = shoppingBagHTMLData;
            
            //cart totla
            BMW.Lifestyles.Web.Services.ShoppingBag.GetCartTotal(bagTotal);
            
            
            
        }

        
    }
    //used for the checkout cart
     function CompletedLoadCheckOutShoppingBag(result, eventArgs){
   
        var imageDirectory = "";
       var shoppingBagHTMLData = "";
       var shoppingBagContainer = $get('CheckoutBagContainer');
       var subtotalItemFormat = "<div id=\"subtotalContainer\"/>";
       
       var shoppingBagHeader ="<tr class=\"table-head\">" +
                               "<th class=\"shoppingBag-item\">Item Details</th>" +
                               "<th class=\"shoppingBag-status\"></th>" +
                               "<th class=\"shoppingBag-price\">Price</th>" +
                               "<th class=\"shoppingBag-quantity\">Quantity</th>" +
                               "<th class=\"shoppingBag-total\">Total</th></tr>";
                                                  
        var shoppingBagItemFormat = "<tr><td><div class=\"thumbLink-product\"><a href=\"../ProductDetail.aspx?p={0}\">{1}</a></div>" +
                    "<div class=\"thumbLink-misc\">{8} {9} {10}</div>" +
                    "<div class=\"thumbLink-misc\">Item #: {2}</div></td>" +
                    "<td><div class=\"shoppingBag-remove\"><a href=\"javascript:updateCheckoutCartItem({12},0,true)\"><img alt=\"Click to delete from bag\" src=\"{11}x_red.gif\" />Remove</a></div></td>" +
                    "<td>{3}</td><td><span class=\"price\"><a href=\"javascript:updateCheckoutCartItem({12},{7},false)\">" +
                    "<img align=\"top\" alt=\"Click to decrease quantity\" border=\"0\" height=\"18\" src=\"{11}2002a_minus.gif\" " +
                    "width=\"18\" /></a>&nbsp;{4}&nbsp;<a href=\"javascript:updateCheckoutCartItem({12},{6},false)\"><img align=\"top\" alt=\"Click to increase quantity\" " +
                    "border=\"0\" height=\"18\" src=\"{11}2002a_plus.gif\" width=\"18\" /></a></span></td>" +
                    "<td><span class=\"price\">{5}</span></td></tr>";
        
       //need to change image directories if in a subdirectory
       if(location.pathname.lastIndexOf("secure") > 0){
          imageDirectory = "../images/";
        }
        else{
          imageDirectory = "images/";
        }
       
        if ( result.length == 0 ){
              shoppingBagContainer.innerHTML = "<strong>Your bag is empty.</strong>";
        }
        else
        {
            shoppingBagContainer.innerHTML = "";
            shoppingBagHTMLData = shoppingBagHTMLData + "<table id=\"shoppingBag\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\" width=\"100%\">";
            //add header row
            shoppingBagHTMLData = shoppingBagHTMLData + shoppingBagHeader;
            
            for(i=0; i < result.length; i++){
               var bi = new BagItem(result[i]);
                var qtyPlus1 =   (parseInt(bi.Quantity())  +1);
                var qtyMinus1 = (parseInt(bi.Quantity())  -1);
                shoppingBagHTMLData = shoppingBagHTMLData + String.format(shoppingBagItemFormat,bi.ProductId(),bi.Name(),bi.ProductNumber(),bi.Price(),bi.Quantity(),bi.TotalPrice(),qtyPlus1 ,qtyMinus1 , bi.Color(), bi.Size(), bi.Style(),imageDirectory,bi.ProductItemId());
            }
          
            
            shoppingBagHTMLData = shoppingBagHTMLData + "</table>";
            //add subtotal
            shoppingBagHTMLData = shoppingBagHTMLData + subtotalItemFormat;
            shoppingBagContainer.innerHTML = shoppingBagHTMLData;
            
            
            //cart totla
            BMW.Lifestyles.Web.Services.ShoppingBag.GetCartTotal(orderTotal);

        }

        
    }
    
    function bagTotal(result, eventArgs)
    {
        var subTotalContainer = $get('subtotal-value');
        subTotalContainer.innerHTML = "Subtotal: " + result;
    }
    
    
    function orderTotal(result, eventArgs)
    {
       var totalHTML = "";
       var finaltotal=0;
       var subTotalTableContainer = $get('subtotalContainer');
    
       var subTotalFormat = "<tr><td>SubTotal: </td><td>{0}</td></tr>";
       var estshippingFormat ="<tr><td>Estimated Shipping: </td><td>{0}</td></tr>"
   
       var subtotal =  result.replace (/\$|\,/g,'');
       
       finaltotal =  finaltotal + parseFloat(subtotal);
    
       totalHTML = totalHTML + String.format(subTotalFormat,formatCurrency(parseFloat(subtotal)));
    
       subTotalTableContainer.innerHTML = String.format("<table id=\"subtotal\">{0}</table>",totalHTML);
    }
    
    function updateCheckoutCartItem(productItemId, newQty, remove)
    {
 
        BMW.Lifestyles.Web.Services.ShoppingBag.UpdateCartItem( productItemId, newQty, remove,LoadCheckoutBag );
    }
    
    
    
    function updateCartItem(productItemId, newQty, remove)
    {
        BMW.Lifestyles.Web.Services.ShoppingBag.UpdateCartItem( productItemId, newQty, remove,showShoppingbag );
    }
 
    function showShoppingbag(){
    //relaod first
    LoadShoppingBag()
    
    var shoppingBagContainer = $get('shoppingbagContents');
    shoppingBagContainer.style.position='absolute';
    shoppingBagContainer.style.display='block';
    //move layer
    shoppingBagContainer.style.top='28px';
    shoppingBagContainer.style.left='100px';
    
    //set time to close
   setTimeout("hideShoppingbag()" , 9000);

    }
    
     function hideShoppingbag(){
     var shoppingBagContainer = $get('shoppingbagContents');
     shoppingBagContainer.style.position='absolute';
     shoppingBagContainer.style.display='none';
    }
    
    function formatCurrency(num) 
{
     num = num.toString().replace(/\$|\,/g,'');
     if(isNaN(num)) num = "0";
     sign = (num == (num = Math.abs(num)));
     num = Math.floor(num*100+0.50000000001);
     cents = num%100;
     num = Math.floor(num/100).toString();
     if(cents<10)  cents = "0" + cents;
     for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
          num = num.substring(0,num.length-(4*i+3))+','+
                 num.substring(num.length-(4*i+3));
     }
     
     // Comment next line you do not want 2 decimal places.
     return (((sign)?'':'-') + '$' + num + '.' + cents);
     
     // UnComment next line you do not want 2 decimal places.
     //return (((sign)?'':'-') + '$' + num);
}

    
    
    
   

