﻿ function ShoppingCartClientFunctions_AddProductToCart(productGuid, productTypeEnum, quantity) 
 {
       InksWebUI.WebServices.ShoppingCartService.AddProductToCart(productGuid, productTypeEnum, quantity, ShoppingCartClientFunctions_OnAddToCartSucceeded, ShoppingCartClientFunctions_OnAddToCartFailed);
       
       var pnlLoadingIndicator = GetElementsByAttribute(document, "div", "name", "pnlLoadingIndicator" + productGuid)[0];
       if(pnlLoadingIndicator != null)
       {
            pnlLoadingIndicator.style.visibility = "visible";
       }
 }
 
 
 function ShoppingCartClientFunctions_AddOmnitureTracking(productGuid, omnitureTrackingType, quantity, productPrice, pageName, omnitureRepositoryName, buy2Get1FreeEnabled) 
 {       
       /*****************************************************
       / exactQuantity - Included Buy 2 Get 1 Free Qty
       /    If User select, 'Buy 2 Get 1 Free Qty'
       /        exactQuantity = 2 + 1 = 3
       /****************************************************/
       //http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
       
       var btnAddToCartTracking = GetElementsByAttribute(document, "img", "name", "btnAddToCartTracking" + productGuid)[0];
       var random_no = Math.random();
       var str_random_no = random_no.toString().replace(".","");
        
       if (btnAddToCartTracking != null)
       {
            if (omnitureTrackingType == "scAdd")
            {   
                var exactQuantity = parseInt(quantity);
                if (buy2Get1FreeEnabled == true && quantity >= 2)
                {
                    exactQuantity = parseInt(quantity) + parseInt(Math.floor((quantity / 2)));
                }                
                btnAddToCartTracking.src = "https://102.112.2O7.net/b/ss/" + omnitureRepositoryName + "/1/G.5--NS/" + str_random_no + "?pageName=" + escape(pageName) + "&events=" + omnitureTrackingType + "&products=" + escape(";") + productGuid.toString() + escape(";") + exactQuantity.toString() + escape(";") + productPrice.toString();                
            }
            else if (omnitureTrackingType == "scRemove")
            {
                //alert(omnitureTrackingType);
                var lblQuantityInOrder = GetElementsByAttribute(document, "span", "name", "lblQuantityInOrder" +  productGuid)[0];
                if(lblQuantityInOrder != null)
                {
                    btnAddToCartTracking.src = "https://102.112.2O7.net/b/ss/" + omnitureRepositoryName + "/1/G.5--NS/" + str_random_no + "?pageName=" + escape(pageName) + "&events=" + omnitureTrackingType + "&products=" + escape(";") + productGuid.toString() + escape(";") + lblQuantityInOrder.innerHTML;
                }
                
            }       
       }
       
 } 
 
 function ShoppingCartClientFunctions_OnAddToCartSucceeded(result, userContext, methodName) 
 {
       if(result != null)
       {
           var pnlLoadingIndicator = GetElementsByAttribute(document, "div", "name", "pnlLoadingIndicator" +  result.ProductId)[0];
           if(pnlLoadingIndicator != null)
           {
                pnlLoadingIndicator.style.visibility = "hidden";
           }
           
           var pnlRemoveItem = GetElementsByAttribute(document, "div", "name", "pnlRemoveItem" +  result.ProductId)[0];
           //var pnlRemoveItem = document.getElementsByName("pnlRemoveItem" + result.ProductId)[0];
           if(pnlRemoveItem != null)
           {
                pnlRemoveItem.style.display = "block";
           }
           
            //var lblQuantityInOrder = document.getElementsByName("lblQuantityInOrder" + result.ProductId)[0];
            var lblQuantityInOrder = GetElementsByAttribute(document, "span", "name", "lblQuantityInOrder" +  result.ProductId)[0];
            if(lblQuantityInOrder != null)
            {
                lblQuantityInOrder.innerHTML = result.ProductQuantityInCart;
            }
            
            this.RefreshShoppingCartDisplay(result.TotalItemsInCart, result.CartPrice);
            this.RefreshAddToCartButtons(result.TotalItemsInCart);     
       }  
 }
 
 function ShoppingCartClientFunctions_OnAddToCartFailed(error, userContext, methodName) 
 {
       //alert("add failed");
 }
 
 
 
 function ShoppingCartClientFunctions_RemoveProductFromCart(productGuid) 
 {
       InksWebUI.WebServices.ShoppingCartService.RemoveProductFromCart(productGuid, ShoppingCartClientFunctions_OnRemoveFromCartSucceeded, ShoppingCartClientFunctions_OnRemoveFromCartFailed);
       
       //var pnlLoadingIndicator = document.getElementsByName("pnlLoadingIndicator" + productGuid)[0];
       var pnlLoadingIndicator = GetElementsByAttribute(document, "div", "name", "pnlLoadingIndicator" + productGuid)[0];
       if(pnlLoadingIndicator != null)
       {
            pnlLoadingIndicator.style.visibility = "visible";
       }
 }
 
 
 function ShoppingCartClientFunctions_OnRemoveFromCartSucceeded(result, userContext, methodName) 
 {
       if(result != null)
       {
           var pnlLoadingIndicator = GetElementsByAttribute(document, "div", "name", "pnlLoadingIndicator" +  result.ProductId)[0];
           //var pnlLoadingIndicator = document.getElementsByName("pnlLoadingIndicator" + result.ProductId)[0];
           if(pnlLoadingIndicator != null)
           {
                pnlLoadingIndicator.style.visibility = "hidden";
           }
           
           //var pnlRemoveItem = document.getElementsByName("pnlRemoveItem" + result.ProductId)[0];
           var pnlRemoveItem = GetElementsByAttribute(document, "div", "name", "pnlRemoveItem" +  result.ProductId)[0];
           if(pnlRemoveItem != null)
           {
                pnlRemoveItem.style.display = "none";
           } 
           
           this.RefreshShoppingCartDisplay(result.TotalItemsInCart, result.CartPrice);   
           this.RefreshAddToCartButtons(result.TotalItemsInCart);      
       } 
 }
 
 function ShoppingCartClientFunctions_OnRemoveFromCartFailed(error, userContext, methodName) 
 {
       alert("remove failed");
 }
 
 function RefreshShoppingCartFromServer()
 {
        InksWebUI.WebServices.ShoppingCartService.GetShoppingCartStatus(ShoppingCartClientFunctions_OnRefreshShoppingCartFromServerSucceeded, ShoppingCartClientFunctions_OnRefreshShoppingCartFromServerFailed);
 } 
 
 function ShoppingCartClientFunctions_OnRefreshShoppingCartFromServerSucceeded(result, userContext, methodName)
 {
        if(result != null)
        {
            this.RefreshShoppingCartDisplay(result.TotalNumberOfItems, result.ItemsTotalPrice);
        }
 }
 
 function ShoppingCartClientFunctions_OnRefreshShoppingCartFromServerFailed(error, userContext, methodName)
 {
        alert("refresh failed");
 }
 
 function RefreshShoppingCartDisplay(numberOfItems, itemsTotalPrice)
 {
    var pnlNumberOfItems = $get("ctl00_Header1_lblTotalItemsInCurrentOrder");
    var pnlItemsTotalPrice = $get("ctl00_Header1_lblTotalOrderPrice");
    var pnlShoppingCartInfo = $get("ctl00_Header1_pnlCurrentOrderInfo");    
    var pnlShoppingCartLoadingIndicator = $get("ctl00_Header1_pnlShoppingCartLoadingIndicator");    
    
    if(pnlShoppingCartLoadingIndicator != null)
    {
        pnlShoppingCartLoadingIndicator.style.display = "none";
    }
    
    if(pnlShoppingCartInfo != null)
    {
        if(numberOfItems > 0)
        {
            pnlShoppingCartInfo.style.visibility = "visible";
        }
        else
        {
            pnlShoppingCartInfo.style.visibility = "hidden";
        }
    }
    
    if(pnlNumberOfItems != null)
    {
        if(numberOfItems > 1)
        {
            pnlNumberOfItems.innerHTML = numberOfItems + " Items";
        }
        else if(numberOfItems == 1)
        {
            pnlNumberOfItems.innerHTML = numberOfItems + " Item";
        }
    }
    
    if(pnlItemsTotalPrice != null)
    {
        pnlItemsTotalPrice.innerHTML = itemsTotalPrice;
    }
 }
 
 function RefreshAddToCartButtons(numberOfItemsInCart)
 {
    //var addToCartPanels = document.getElementsByName("pnlViewCartButton");
    var addToCartPanels = GetElementsByAttribute(document, "div", "name", "pnlViewCartButton");

    if(addToCartPanels != null && addToCartPanels.length > 0)
    {
        var display = "none";
        if(numberOfItemsInCart > 0)
        {
            display = "inline";
        }
        
        for(var i = 0; i<addToCartPanels.length; i++)
        {
            addToCartPanels[i].style.display = display;
        }
    }
    //alert("add to cart panels: " + addToCartPanels.length);
 }
 
 
 
function GetElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue)
{
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
    var oCurrent;
    var oAttribute;
    for(var i=0; i<arrElements.length; i++)
    {
        oCurrent = arrElements[i];        
        oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
        if(typeof oAttribute == "string" && oAttribute.length > 0)
        {        
	        if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute)))
	        {
		        arrReturnElements.push(oCurrent);
	        }
        }
    }

    return arrReturnElements;
}