﻿function ShoppingCartClientFunctions_AddProductToCart(productGuid, productTypeEnum, quantity) {
    var pnlAddingToCart = document.getElementById("ShoppingCartModal_PnlAddingItemToCart");
    if (pnlAddingToCart != null) {
        pnlAddingToCart.style.display = "block";
    }
    var pnlDoneAddingToCart = document.getElementById("ShoppingCartModal_PnlDoneAddingItemToCart");
    if (pnlDoneAddingToCart != null) {
        pnlDoneAddingToCart.style.display = "none";
    }

    ModalDialog_revealModal("modalPage");


    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) {
        this.RefreshShoppingCartDisplay(result.TotalItemsInCart, result.CartPrice);

        var pnlAddingToCart = document.getElementById("ShoppingCartModal_PnlAddingItemToCart");
        if (pnlAddingToCart != null) {
            pnlAddingToCart.style.display = "none";
        }
        var pnlDoneAddingToCart = document.getElementById("ShoppingCartModal_PnlDoneAddingItemToCart");
        if (pnlDoneAddingToCart != null) {
            pnlDoneAddingToCart.style.display = "block";
        }

        //ModalDialog_hideModal("modalPage");

        //this.RefreshAddToCartButtons(result.TotalItemsInCart);     
    }
}

function ShoppingCartClientFunctions_OnAddToCartFailed(error, userContext, methodName) {
    alert("add failed");
}

function ModalDialog_revealModal(divID) {
    window.onscroll = function() { document.getElementById(divID).style.top = document.body.scrollTop; };
    document.getElementById(divID).style.display = "block";
    document.getElementById(divID).style.top = document.body.scrollTop;
}

function ModalDialog_hideModal(divID) {
    document.getElementById(divID).style.display = "none";
}



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;
}