﻿


 function Item(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]; };
        this.CartTotal = function (){  return this.itemData[11]; };
        
    }
    
 
  
  function CompletedProductItem(result, eventArgs){
    
  var qty = $get("itemQuantity").value;

  BMW.Lifestyles.Web.Services.ShoppingBag.itemexistsinCart(result,IsproductExists);   
    
 }
 function IsproductExists(result,eventArgs)
 {
  
  if(document.getElementById('btnaddtocart')!=null)
    {  
        if(result==true)
         { 
           document.getElementById('btnaddtocart').value="Change Quantity";
         }
        else
        {
         document.getElementById('btnaddtocart').value="Add to Shopping Bag";
        }
    }
  }
 
 
 // with this test document.cookie.indexOf( name + "=" );
function Get_Cookie_shoppingCart(check_name) {

	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{	
	
	if(a_all_cookies[i].indexOf('ShoppingBag=')>-1)
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( 'ShoppingBag=' );
		
		
		// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{   b_cookie_found = true;
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
				return cookie_value;
				
			    break;
			}
			// note that in cases where cookie is initialized but no value, null is returned
			
		//}
	}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return 0;
	}
}	
 
 
 
 
 function CompletedGetProductItem(result, eventArgs){
   
  var qty = $get("itemQuantity").value;

  BMW.Lifestyles.Web.Services.ShoppingBag.AddItemToCart(result,qty,CompletedAddToCart);   
    
 }

 
    
 function CompletedAddToCart(result, eventArgs){
 
 if(result!=null)
 {
  
  if(document.getElementById('btnaddtocart')!=null)
    {  
           document.getElementById('btnaddtocart').value="Change Quantity";
    }
 
    
    var basketContainerHTMLdata = "";
        var basketContainer = $get('basket');
       
               //0 = gty 1=asset 2= name 3 = total  4= price
              var tableFormat = 
              "<table bgcolor=\"#e6ecf2\" border=\"0\" width=\"250\">" + 
              "<tr><td bgcolor=\"#64819f\" colspan=\"2\" nowrap=\"nowrap\" style=\"padding-right: 2px; padding-left: 2px;font-weight: bold; padding-bottom: 2px; color: white; padding-top: 2px; text-align: center\">" +
              "<span class=\"WhiteBold\">1 Item(s) Added to Cart</span></td></tr>" +
              "<tr><td bgcolor=\"#ffffff\" colspan=\"2\"><div align=\"center\"><img src=\"images/assets/medium/{1}\" /></div></td></tr>" +
              "<tr bgcolor=\"#cccccc\"><td style=\"padding-right: 1px; padding-left: 1px; padding-bottom: 1px; padding-top: 1px; text-align: left\" valign=\"top\">" +
              "<span class=\"copyright\">{2} {5} {6} {7}</span></td><td bgcolor=\"#c0cdde\" nowrap=\"nowrap\" style=\"padding-right: 1px; padding-left: 1px;padding-bottom: 1px; padding-top: 1px; text-align: right\" valign=\"top\">" +
              "<span class=\"copyright\">Qty: {0}<br />{4}</span></td></tr>" +
              "<tr bgcolor=\"#a7c8d8\"><td class=\"Bold_black\" colspan=\"2\" nowrap=\"nowrap\" style=\"padding-right: 2px; padding-left: 2px;" +
              "padding-bottom: 2px; padding-top: 2px; text-align: center\" valign=\"top\">Your Cart totals {3} so far</td></tr>" +
              "<tr><td class=\"BlackBold\" colspan=\"2\" nowrap=\"nowrap\" style=\"padding-right: 2px; padding-left: 2px;" +
              "padding-bottom: 2px; padding-top: 5px; background-color: #e6ecf2; text-align: right\" align=\"top\">" +
              "<a href=\"secure/CheckOut.aspx\">Begin Checkout</a></td></tr>" +
              "</table>";

        if ( result.length == 0 ){
            hideBasket();
        }
        else
        {
            basketContainer.innerHTML = "";
            var itm = new Item(result);
            basketContainerHTMLdata = basketContainerHTMLdata + String.format(tableFormat,itm.Quantity(),itm.Asset(), itm.Name(),itm.CartTotal(),itm.Price(), itm.Color(), itm.Size(), itm.Style());
         
     
            basketContainer.innerHTML =  basketContainerHTMLdata;
           
            setTimeout("hideBasket()" , 7000);
            showBasket();
  
        }
        }
 }
 

    
  function showBasket(){
    var basketContainer = $get('basket');
    basketContainer.style.position='absolute';
    basketContainer.style.display='block';
    //move layer
    basketContainer.style.top='20%';
    basketContainer.style.left='25%';
    
    //set time to close
   setTimeout("hideBasket()" , 9000);

    }
    
  function hideBasket(){
     var basketContainer = $get('basket');
     basketContainer.style.position='absolute';
     basketContainer.style.display='none';
    }
    
 
 function getQueryVariable(variable) { 
     var query = window.location.search.substring(1); 
      var vars = query.split("&");  
          for (var i=0;i<vars.length;i++) {   
           var pair = vars[i].split("=");    
               if (pair[0] == variable) {      
               return pair[1];    
               }  
          } 
    }

