﻿function addItemToCart(buttonObject, pId) {
    //Get the quantity based on the pId...
    pQuantity = $("#qty_" + pId).val();

    //Check if a zip code is already set
    szZipSet = $("#IS_ZIP_CODE_SET").val();
    if ("true" != szZipSet) {
        $("input[name=productId]").val(pId);
        $("input[name=quantity]").val(pQuantity);

        showZipCodeLayer();
        return;
    }

    //Hide previous add to cart
    if ($(buttonObject).prev() && $(buttonObject).prev().attr("type") == "addedMesg") {
        $(buttonObject).prev().remove(); //remove added message
    }

    //Hide the button
    $(buttonObject).hide();

    $("<p class='processing add-cart' type='processing'>Processing...</p>").insertBefore($(buttonObject));

    var addPath = getContextPath() + "addToCart.ashx?productId=" + pId + "&quantity=" + pQuantity;
    $.ajax({
        type: "GET",
        url: addPath,
        cache: false,
        success: function(data) {
            if ($(buttonObject).prev() && $(buttonObject).prev().attr("type") == "processing") {
                $(buttonObject).prev().remove(); //remove processing message
            }

            //Determine if the persistent cart is present on the page or not
            var bPersistentCartPresent = $("#PersistentCartContents").length > 0;

            retData = eval("(" + trimString(data) + ")");
            if (retData.itemAdded != null) {
                if (!bPersistentCartPresent) {
                    $("<p class='addedCartAjax' type='addedMesg'>Item added to cart.</p>").insertBefore($(buttonObject));
                }
                else {
                    $("#PersistentCartContents").replaceWith(retData.htmlCart);
                }
                recordAnalyticsForAdd(pId, retData.firstItem, retData.price);
            }
            else if (retData.outOfStock != null) {
                $("#stock_info").html(retData.outOfStock);
                $("#stock_info").show();
                $("<p class='msg_availability' type='addedMesg'>" + retData.outOfStock + "</p>").insertBefore($(buttonObject));
            }
            else {
                $("<p class='addedCartAjax' type='addedMesg'>Could not add item to your cart</p>").insertBefore($(buttonObject));
            }

            $(buttonObject).show();
        }
    });
}

function recordAnalyticsForAdd(pId, firstItem, price) {
    var s = s_gi(s_account);
    s.linkTrackVars = 'products,events';
    if (firstItem) {
        s.linkTrackEvents = 'scAdd,scOpen,event23';
        s.events = 'scAdd,scOpen,event23';
    }
    else {
        s.linkTrackEvents = 'scAdd,event23';
        s.events = 'scAdd,event23';
    }
    s.products = ';' + pId + ';;;event23=' + price;
    s.tl(this, 'o', 'Add to Cart');
}


function addItemToFavorites(pId) {
    //Hide the add button
    $("#ImageAddToFavorites_" + pId).hide();

    var addPath = getContextPath() + "account/addToFavorites.ashx?productId=" + pId;
    $.ajax({
        type: "GET",
        url: addPath,
        cache: false,
        success: function(data) {
            //Show the remove button
            $("#ImageRemoveFromFavorites_" + pId).show();

            //Show the burst for a favorite (need to hide the past purchase if both would be present)
            $("#myFav_" + pId).show();
            $("#pastPurchase_" + pId).hide;
        }
    });
}

function removeItemFromFavorites(pId) {
    //Hide the add button
    $("#ImageRemoveFromFavorites_" + pId).hide();

    var addPath = getContextPath() + "account/removeFromFavorites.ashx?productId=" + pId;
    $.ajax({
        type: "GET",
        url: addPath,
        cache: false,
        success: function(data) {
            //Show the remove button
            $("#ImageAddToFavorites_" + pId).show();

            //Hide the burst. show the past purchase if present
            $("#myFav_" + pId).hide();
            $("#pastPurchase_" + pId).show();
        }
    });
}