function sendItemToCart(itemId, itemType, amount, cartId)
{
    $.post(
        siteUrl+"shopping-cart/index/add-to-cart",
        {
            id          : itemId,
            type        : itemType,
            amount      : amount
        }, 
        function(data)
        {
            data = parseServerJSON(data);
            showCartMessage(data.success, data.message, cartId)
        }, 
        "html");
        
}

function showCartMessage(success, message, cartId)
{
    if (cartId != null) hideActionLoader(cartId);
    
    $.modal.close();
    if(success){
        $.modal('<div class="modalWindow" id="modal1"><div class="mtop"></div><div class="mmain"><div class="mcenter"><div class="center"><h1 class="boxTitle">Shopping Cart Message</h1><br/>'+message+'<br/><br/><div style="text-align:right"><br/><img src="'+siteUrl+'images/mycues/default/layouts/main/view_cart_btn.jpg" style="cursor:pointer" id="goToCartBtn" onclick="continueShopping();"  />&nbsp;&nbsp;<img src="'+siteUrl+'images/mycues/default/layouts/main/continue_shop_btn.jpg" onclick="$.modal.close();" style="cursor:pointer" id="calItemCloseBtn" /></div></div></div></div><div class="mbottom"></div></div>');
    } else {
        $.modal('<div class="modalWindow" id="modal1"><div class="mtop"></div><div class="mmain"><div class="mcenter"><div class="center"><h1 class="boxTitle">Shopping Cart Message</h1><br/>'+message+'<br/><br/><div style="text-align:right"><br/><img src="'+siteUrl+'images/mycues/default/layouts/main/view_cart_btn.jpg" style="cursor:pointer" id="goToCartBtn" onclick="continueShopping();"  />&nbsp;&nbsp;<img src="'+siteUrl+'images/mycues/default/layouts/main/continue_shop_btn.jpg" onclick="$.modal.close();"  style="cursor:pointer" id="calItemCloseBtn" /></div></div></div></div><div class="mbottom"></div></div>');
    }
}


function continueShopping(){
    window.location.href= siteUrl + 'shopping-cart';
}

function addToCart(itemId, itemType, cartId){
    sendItemToCart(itemId, itemType, 0, cartId);
}

function submitAddToCartAction(itemId, itemType, redirectUrl, cartId)
{
    if (redirectUrl != null) window.location = redirectUrl;
    else  addToCart(itemId, itemType, cartId);
}

function isPartOfBundlePromotionSuccess(bundleId, productTitle, bundleTitle, products, promotionSavings, 
                requestIsBundlePromotion, itemId, itemType, redirectUrl, cartId)
{
    if (requestIsBundlePromotion == 1) addToCart(bundleId, 3, cartId);
    else 
    {
        $.modal.close();               
        showBundlePanel(bundleId, productTitle, bundleTitle, products, promotionSavings, itemId, itemType, redirectUrl, cartId);
    }

    hideActionLoader(cartId);
}

function isPartOfBundlePromotionError(itemId, itemType, redirectUrl, cartId)
{                    
    //it's an event           
    if (itemType == 4 ) submitAddToCartAction(itemId, itemType, redirectUrl);
    else checkIsIsPWYCPromotion(itemId, itemType, redirectUrl, cartId);     
}

function checkIsIsPWYCPromotion(itemId, itemType, redirectUrl, cartId)
{
    //check if the item is part of PWYC promotion
    $.post(siteUrl+"shopping-cart/index/is-part-of-pwyc-promotion",
    {
        id          : itemId,
        type        : itemType
    }, 
    function(data)
    {
        data = parseServerJSON(data);
        if (data.isPartOfPwycPromotion)
        {
            showPwycPanel(itemId, itemType, data.cuesPrice, data.dirPrice, data.nonMemPrice, data.myPrice);
            hideActionLoader(cartId);

        }
        else  submitAddToCartAction(itemId, itemType, redirectUrl, cartId);
    }, 
    "html");       
}

function showShopLoginPopUp(itemId, itemType, redirectUrl, cartId, actionType)
{
    loginCallbackUrl = function(){processAddToCartAction(itemId, itemType, redirectUrl, cartId, actionType);}
    $("#loginPopup").modal();              
}

function processAddToCartAction(itemId, itemType, redirectUrl, cartId, actionType)
{
    showActionLoader(actionType, cartId);
    //check if the item is part of a bundle promotion
    $.post(siteUrl+"shopping-cart/index/is-part-of-bundle-promotion",
    {
        id          : itemId,
        type        : itemType
    }, 
    function(data)
    {
        data = parseServerJSON(data);
        if (data.isPartOfBundlePromotion)
            isPartOfBundlePromotionSuccess(data.bundleId, data.productTitle, data.bundleTitle, data.products, data.promotionSavings,
                data.requestIsBundlePromotion, itemId, itemType, redirectUrl, cartId);
        else isPartOfBundlePromotionError(itemId, itemType, redirectUrl, cartId);
    }, 
    "html");
}

function showActionLoader(actionType, cartId)
{
    $("#action_"+cartId).hide();
    $("#actionLoader_"+cartId).addClass("action_"+actionType+"_AjaxLoader");
    $("#actionLoader_"+cartId).show();    
}

function hideActionLoader(cartId)
{
    $("#actionLoader_"+cartId).hide();
    $("#action_"+cartId).show();
}

function addRenewalsToCart()
{
    var renewals = "";
    $('.renewals_chk').each(function(i, renewalElem){
        if ($(renewalElem).attr("checked") == true)
            renewals += ("#"+$(renewalElem).val());
    });
    
    if (renewals == "")
    {
        alert("Please select at least one renewal to be added to the cart.");
        return;
    }

    $.post(siteUrl+"shopping-cart/index/add-renewals-to-cart",
    {
        renewals    : renewals
    }, 
    function(data)
    {
        data = parseServerJSON(data);
        showCartMessage(data.success, data.message, null);
    }, 
    "html");
}
