﻿ 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_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";
    }
    
    pnlShoppingCartInfo.style.visibility = "visible";
//    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 in cart";
        }
        else if(numberOfItems == 1 || numberOfItems == 0)
        {
            pnlNumberOfItems.innerHTML = numberOfItems + " Item in cart";
        }
    }
    
    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;
}