﻿function rdGetCart() {

    new Ajax.Request(_appPath + 'callbacks/Cart/GetShoppingCart.aspx' + rdGetRandomNumber(),
	{
	    method: 'post',
	    onSuccess: function(transport) {
	    rdGetCartSuccess(transport.responseText);
	    },
	    onFailure: function(transport) {
	        rdHideLoader();
	        alert('Error retrieving shopping cart');
	    }
	});
}

function rdGetCartSuccess(retVal) {
    if ($('Cart')) {
        $('Cart').innerHTML = retVal;
    }
}

function rdAddToCart(itemid, qty) {
    new Ajax.Request(_appPath + 'callbacks/Cart/AddCartItem.aspx' + rdGetRandomNumber(),
	{
	    method: 'post',
	    parameters: { item_id: itemid, qty: qty },
	    onSuccess: function(transport) {
	        rdAddToCartSuccess(transport.responseText, itemid);
	    },
	    onFailure: function(transport) {
	        alert('Error adding cart item');
	    }
	});
}

//Promotion
function rdAddPromotionToCart(itemid, qty, promoID) {
    new Ajax.Request(_appPath + 'callbacks/Cart/AddCartItem.aspx' + rdGetRandomNumber(),
	{
	    method: 'post',
	    parameters: { item_id: itemid, qty: qty, promotion_id: promoID },
	    onSuccess: function(transport) {
	    rdAddPromotionToCartSuccess(transport.responseText, itemid, promoID);
	    },
	    onFailure: function(transport) {
	        alert('Error adding cart item');
	    }
	});
}

function rdAddPromotionToCartSuccess(retval, itemid, promoID) {
    if ("success" == retval) {

        if ($('pi' + itemid)) {
            $('pi' + itemid).innerHTML = "<span class=\"FeedbackNote\">added to cart</span>"
        }

        //rdGetCart();
        rdShowPromotionCart(promoID);

    }
    else {
        alert(retval);
    }
}

function rdShowPromotionCart(promoID) {
    window.location = _appPath + 'ShoppingCart.aspx?pid=' + promoID + "&rnd=" + rdGetRandomNumber();
}

function rdAddToCartSuccess(retval, itemid) {
    if ("success" == retval) {

        if ($('pi' + itemid)) {
            $('pi' + itemid).innerHTML = "<span class=\"FeedbackNote\">added to cart</span>"
        }

        //rdGetCart();
        rdShowCart();
        
    }
    else {
        alert(retval);
    }
}


function rdEmptyCart() {
    if (confirm('Are you sure you want to remove all items from your cart?')) {
        new Ajax.Request(_appPath + 'callbacks/Cart/EmptyCart.aspx' + rdGetRandomNumber(),
	    {
	        method: 'post',
	        onSuccess: function(transport) {
	            rdRefreshPage();
	        },
	        onFailure: function(transport) {
	            alert('Error clearing cart');
	        }
	    });
    }
}

function rdShowCart() {
    window.location = _appPath + 'ShoppingCart.aspx' + rdGetRandomNumber();
}

function rdGetRandomNumber(append)
{
    var d = new Date();
    var time = d.getTime();

    if (arguments.length > 0)
    {
        return "&rnd=" + time;
    }
    else
    {
        return "?rnd=" + time;
    }
}
