
var Loreal = Loreal || {};
Loreal.PromotionPropximity = {
    AJAX_SHOPPINGCART_PROMO_MESSAGE: ".AJAX_SHOPINGCART_PROMO_MESSAGE",
    AJAX_SHOPPINGCART_PROMO_DISTANCE_COUNT: 20,
    AJAX_SHOPPINGCART_PROMO_DISTANCE_TYPES: "BasketPriceEvaluator",
    AJAX_SHOPPINGCART_PROMO_DISTANCE_REGEX_MESSAGE: /gastos/,
    AJAX_SHOPPINGCART_PROMO_DISTANCE_ELIGIBLE_MESSAGE: "vous êtes eligible pour la promo free shipping",
    AJAX_SHOPPINGCART_PROMO_PICTO: "#divPicto",

    GetPromotionProximity: function () {
        //alert("GetPromotionProximity");
        var $obj = $(Loreal.PromotionPropximity.AJAX_SHOPPINGCART_PROMO_MESSAGE);
        if ($obj != null) {
            $obj.css("display", "");
        }
        MicrosoftFrance.MCS.Commerce.WS.AJAXProject.ShoppingCartWebService.GetPromotionProximity(CurrentCustomerID, Loreal.PromotionPropximity.AJAX_SHOPPINGCART_PROMO_DISTANCE_COUNT, Loreal.PromotionPropximity.AJAX_SHOPPINGCART_PROMO_DISTANCE_TYPES, Loreal.PromotionPropximity.DisplayPromotionProximityOnComplete, Loreal.PromotionPropximity.onError);
    },

    DisplayPromotionProximityOnComplete: function (result) {
        //alert("DisplayPromotionProximityOnComplete");
        var $obj = $(Loreal.PromotionPropximity.AJAX_SHOPPINGCART_PROMO_MESSAGE);
        var $picto = $(Loreal.PromotionPropximity.AJAX_SHOPPINGCART_PROMO_PICTO);
        var $RegExDistancePromotion = Loreal.PromotionPropximity.AJAX_SHOPPINGCART_PROMO_DISTANCE_REGEX_MESSAGE;
		if (result != null) {
            if ($obj != null) {
                $obj.css("display", "");
            }
            //alert("result != null");
            if (result.length != 0) {
                for (var i = 0; i < result.length; i++) {
                    /*if ($picto != null && $picto.length != 0) {
                        if (result[i].Current + productPrice > result[i].Target) {
                            if ($RegExDistancePromotion.test(result[i].PromotionTextWithComputedDistance) == true) {
                                $picto.css("display", ""); 
                            }
                        }
                    }
                    else {*/
                        if (result[i].Current < result[i].Target) {
                            if ($RegExDistancePromotion.test(result[i].PromotionTextWithComputedDistance) == true) {
                                if ($obj != null) {
                                    $obj.html(result[i].PromotionTextWithComputedDistance);
                                }

                            }
                        }
                    /*}*/
                }
            }

        }
    },
    onError: function (response) {
        alert("erreur");
    },

    onReady: function () {
		Loreal.PromotionPropximity.GetPromotionProximity();

    }

};
$(Loreal.PromotionPropximity.onReady);

