function addToShoppingBasket(offerId, isFamilyPage, anchor) 
{
	var ajaxRan = false;
	$.ajax({
	    type: "post",
	    url: "/ajax/offers/ShoppingBasket.aspx",
	    dataType: "text",
	    cache: false,
	    data: {
	        'offerId': offerId
	    },
	    beforeSend: function() {
	        anchor.className = "checkBoxHourglass";
	    },
	    error: function(request, error) {
	        anchor.className = "checkBoxError";
	        ajaxRan = true;
	    },
	    success: function(resp) {
	        if (resp == 'false') {
	            anchor.className = "checkBoxError";
	        } else if (resp == 'nocookie') {
	            anchor.className = "checkBoxNoCookie";
	        } else {
	            anchor.className = "checkBoxChecked";
	            if (resp != 'nosavingchange') {
	                $(".offersSavingsModule").wrap(resp);
	            }
	        }
	        ajaxRan = true;
	    }
	}); 
	return ajaxRan;
}

/* Ajax loader for all required JS functions on Offers */
if (typeof(jQuery) != 0) {
    $(function() {
        /* Add class hasJs to wrapper div to target js centric styles - use to show-hide relevant parts in accessible pages */
        $('#wrapper').addClass('hasJs');
        /* print list insert */
        var listPrintClick = $('.offersDontForget .offersPrintList');
        if (listPrintClick && listPrintClick.length > 0) {
            listPrintClick.click(function() {
                $.ajax({
                        type: "post",
                        url: "/ajax/offers/InsertShoppingListPrint.aspx",
                        dataType: "text",
                        cache: false,
                        data: {
                            'listId': $('.offersDontForget .offersListId').val()
                        },
                        error: function(request, error) {
                            // nothing required
                        },
                        success: function(resp) {
                            // nothing required
                        }
                    });
            });
        }
        /* update notes function on shopping list page */
        var notesField = $('.offersDontForget .notes');
        if (notesField && notesField.length > 0) {
            var origText = notesField.val();
            notesField.blur(function() {
                if (notesField.val() == origText) {
                    return true;
                }
                else {
                    var ajaxRan = false;
                    $.ajax({
                        type: "post",
                        url: "/ajax/offers/SaveShoppingBasketNotes.aspx",
                        dataType: "text",
                        cache: false,
                        data: {
                            'notes': notesField.val()
                        },
                        error: function(request, error) {
                            // don't show an error as if you leave the page an error may be thrown but the ajax will be called
                            ajaxRan = true;
                        },
                        success: function(resp) {
                            ajaxRan = true;
                        }
                    });
                    return ajaxRan;
                }
            });
        }
        $('div.offersMain div.offersPromoModule a.offerUnitMore').click(function(a) {//add click handler to offerUnitMore links
            $('.ajaxLoading', currModule).remove(); //remove if present or we will get error msgs compounding
            if (($(this).hasClass('disabled')) != 1) {
                $(this).after('<div class="ajaxLoading"></div>').blur();
                var currModule = $(this).parent();
                var lastOffer = $(this).prev().prev(); //cache lookup
                var thisUrl = a.target.href;
                if (thisUrl.indexOf('#') > 0) {
                    thisUrl = thisUrl.substr(0, thisUrl.indexOf('#'));
                }
                thisUrl += '&ajax=true';
                $("div.shaded").each(function(i) {
                    $(this).removeClass("shaded");
                });
                $.ajax({
                    type: "get",
                    url: thisUrl,
                    cache: false,
                    error: function(request, error) {
                        $('.ajaxLoading', currModule).addClass('ajaxError').text('Please try again');
                    },

                    success: function(newOffers) {
                        lastOffer.after('<div class="ajaxWrapper">');
                        var aWrapper = $('.ajaxWrapper', currModule); //cache lookup
                        aWrapper.css("display", "none");
                        aWrapper.append(newOffers);

                        if ($('.middle', currModule).hasClass('lastone')) {
                            $('a.offerUnitMore', currModule).addClass('disabled');
                        }

                        $('.middle', aWrapper).addClass('shaded');
                        $('.middle', aWrapper).addClass('added');
                        $('.bottom', currModule).css('background', '#fff url(/graphics/offers/offers_recommended_promo_bottom_shaded.gif) no-repeat');

                        aWrapper.slideDown(1000, function() {
                            aWrapper.replaceWith(aWrapper.html());
                        });
                        $('a.offerUnitLess', currModule).addClass('enabled');
                        $('.ajaxLoading', currModule).remove();
                        lastOffer.removeClass('last');
                        $('div.middle:last', currModule).addClass('last');
                    }
                });
            } else {
                $(this).blur();
            }
            return false;
        });
        // function for "less links"
        $('div.offersMain div.offersPromoModule a.offerUnitLess').click(function(a) {//add click handler to offerUnitLess links
            if (a.target.className.indexOf('nojs') > 0) {
                return true;
            }
            var thisUrl = a.target.href;
            if (thisUrl.indexOf('#') > 0) {
                thisUrl = thisUrl.substr(0, thisUrl.indexOf('#'));
            }
            thisUrl += '&ajax=true';
            var currModule = $(this).parent();
            var openedOffers = $('.added', currModule);
            openedOffers.wrapAll('<div class="collapseMe"></div>');
            var allOpened = $('.collapseMe', currModule); //cache lookup
            allOpened.slideUp(1000, function() {
                allOpened.remove();
                $('div.middle:last', currModule).addClass('last');
                $('.bottom', currModule).css('background', '#fff url(/graphics/offers/offers_recommended_promo_bottom.gif) no-repeat');
                $('a.offerUnitLess', currModule).removeClass('enabled');
                $('a.offerUnitMore', currModule).removeClass('disabled');
                $.ajax({
                    type: "get",
                    url: thisUrl,
                    cache: false,
                    error: function(request, error) {
                        //alert("ack! "+error);
                    }
                });
            });
            return false;
        });
    });
}

