  var pageHasBeenReloadedByJs = false;
  
  function goToByScroll(id){
      if(isScrollEnabled)
    jQuery("html,body").animate({scrollTop: jQuery("#"+id).offset().top},"slow");
    }

function enableWaitMode(){
   jQuery('body').css('cursor','wait');
  jQuery('body').blur(); 
  jQuery('body').focus();
  }
  
  function disableWaitMode(){
    jQuery('body').css('cursor','auto');
     jQuery('body').blur(); 
    jQuery('body').focus();
  }
  
  var isScrollEnabled = true;
  function enableScroll(flag) {
  isScrollEnabled = flag;
  }
  
  var callBackFunctionForCartProductManagement = function(map){
    var MAP_KEY_INFO = "INFO";
    var MAP_KEY_ERROR = "ERROR";
    var MAP_KEY_WARNING = "WARNING";
    var MAP_KEY_PRODUCTS_ROWS = "PRODUCTSROW";
    var MAP_KEY_PRODUCTS_HEADER = "PRODUCTSHEADER";
    var MAP_KEY_PRODUCTS_NUMBER = "PRODUCTSNUMBER";
    var MAP_KEY_ADD_CART_COOKIE_REQUEST = "ADDCARTCOOKIE";
    var MAP_KEY_REQUEST_PAGE_RELOAD = "RELOADPAGE";
    var MAP_KEY_REQUEST_PRODUCT_NUMBER_UPDATE = "PRODUCTNUMBERUPDATE";
    var MAP_KEY_PAGE_HAS_BEEN_RELOADED = "PAGEHASBEENRELOADED";
	
    // clearing wait mode
    disableWaitMode();
	//alert('Test');
	
    var separatorNeeded = false;
  var someProductsInCart = false;
    if (map){
   goToByScroll("messagesToUserPopup");
     resetMessages();
     
     for (key in map){
       if (key == "INFO"){
        var actualHtml = jQuery("#messagesAndCartReviewFrame_messages_body").html();
         actualHtml = actualHtml + "<p>" + map[key] + "</p>"
         jQuery("#messagesAndCartReviewFrame_messages_title").html("<p><strong>Notifica:</strong></p>");
		 jQuery("#messagesAndCartReviewFrame_messages_body").html(actualHtml);
         messageBoxOut = true;
         separatorNeeded = true;
         showHideMessageBox();
       }
       if (key == "ERROR"){
         var actualHtml = jQuery("#messagesAndCartReviewFrame_errors_body").html();
         actualHtml = actualHtml + "<p>" + map[key] + "</p>"
     jQuery("#messagesAndCartReviewFrame_errors_title").html("Attenzione:");
         jQuery("#messagesAndCartReviewFrame_errors_body").html(actualHtml);
         messageBoxOut = true;
         separatorNeeded = true;
         showHideMessageBox();
       }
       if (key == "WARNING"){
         var actualHtml = jQuery("#messagesAndCartReviewFrame_warnings_body").html();
         actualHtml = actualHtml + "<p>" + map[key] + "</p>"
     jQuery("#messagesAndCartReviewFrame_warnings_title").html("Attenzione:");
         jQuery("#messagesAndCartReviewFrame_warnings_body").html(actualHtml);
         messageBoxOut = true;
         separatorNeeded = true;
         showHideMessageBox();
       }
       if (key == "PRODUCTSNUMBER"){
         if (isNaN(parseInt(map[key]))){
           jQuery("#productInCartBox").html("0");
           jQuery("#messagesAndCartReviewFrame_cart_body").html(map[key]);
         } else {
           jQuery("#productInCartBox").html(map[key]);
       someProductsInCart = true;
         }
       }
       if (key == "PRODUCTSROW"){
		 var jsonOBJ = jQuery.parseJSON(map[key]);
		 var html = "";
		 var totalPrice = 0;
		 jQuery.each(jsonOBJ,function(i,jsonProdRow){
			var link = "/" + jsonProdRow.seoUrl;
			var isLast = jsonProdRow.isLast;
			var strong = "";
			if (isLast && isLast == 'true'){
             strong=" class='lastInserted'";
           }
		   // i campi della mappa sono variabili in base alle impostazioni di backoffice
		   html = html + "<p" + strong +"><a href='"+link+"'><strong>"+jsonProdRow.marca+"</strong><br />"+jsonProdRow.productName + "<br /><span style='font-style:italic'>"+jsonProdRow.variantName+"</span> (" + jsonProdRow.quantity  + ")</a></p><br />";
		   totalPrice = parseFloat(totalPrice) + ( parseFloat(jsonProdRow.price) * parseInt(jsonProdRow.quantity) );
		 });
		 var strTotalPrice = "";
		 strTotalPrice += totalPrice;
		 strTotalPrice = strTotalPrice.replace(".",",");
		 /*
		 var strTotalPriceSplitted = strTotalPrice.split(",");
		 if(strTotalPriceSplitted[1].length == 1) {
			strTotalPrice = strTotalPriceSplitted[0] + "," + strTotalPriceSplitted[1] + "0 &euro;";
		 } else {
			strTotalPrice = strTotalPriceSplitted[0] + "," + strTotalPriceSplitted[1] + " &euro;";
		 }
		 */
		 jQuery("#messagesAndCartReviewFrame_cart_body").html(html);
		 jQuery("#priceTot").html(strTotalPrice + " &euro;");
       }
       if (key == "ADDCARTCOOKIE"){         
         document.cookie = map[key];
       }
       if (!map["PAGEHASBEENRELOADED"] && (key == "PRODUCTNUMBERUPDATE")){
         countProductsInCart();
       }
     }
   
   if (someProductsInCart){
    var buttonLabel = "Vai al carrello";
    var htmlForButtonBuy = "<!--<br/><strong><a href='/ecommerce/cartcheckout/'>" + buttonLabel + "</a></strong>-->";
   var newBodyCartHtml = jQuery("#messagesAndCartReviewFrame_cart_body").html() + htmlForButtonBuy;
    jQuery("#messagesAndCartReviewFrame_cart_body").html(newBodyCartHtml);
     
    
   }
   
   if (map["RELOADPAGE"]){
         location.reload();
   }
   if (map["PAGEHASBEENRELOADED"]){
        pageHasBeenReloadedByJs = true;
  } else {
    pageHasBeenReloadedByJs = false;
  }
   
    }
    
    if(separatorNeeded){
       jQuery("#cartBoxSeparator").html("<br/>");
    }else {
      jQuery("#cartBoxSeparator").html("");
    }
  }
  
  function removeProductFromCart (variantId){
    CartAjaxApi.removeProductFromCart( variantId, true,{ callback: callBackFunctionForCartProductManagement, async: false });

  }
  function updateProductInCart (variantId, quantityToAdd){
    CartAjaxApi.updateProductInCart(variantId,quantityToAdd, true,{ callback: callBackFunctionForCartProductManagement, async: false });

  }
  function countProductsInCart(){
    CartAjaxApi.getProductsInCartForSmallBox( { callback: callBackFunctionForCartProductManagement, async: false });
  }
  
  function addProductToCart(variantId, quantity){
	CartAjaxApi.addProductToCart(variantId,quantity, false,{ callback: callBackFunctionForCartProductManagement, async: false });
  }
  
   function checkForReloadingData (){
    CartAjaxApi.checkForReloadingData({ callback: callBackFunctionForCartProductManagement, async: false });

  }
  
  var messageBoxOut = false;
  function showHideMessageBox(){
    if (messageBoxOut){
       jQuery("#messagesAndCartReviewFrame").show("fast");
    } else {
       jQuery("#messagesAndCartReviewFrame").hide("fast");
    }
  }
  
  function resetMessages(){
    jQuery("#messagesAndCartReviewFrame_messages_title").html("");
    jQuery("#messagesAndCartReviewFrame_errors_title").html("");
    jQuery("#messagesAndCartReviewFrame_warnings_title").html("");
    jQuery("#messagesAndCartReviewFrame_messages_body").html("");
    jQuery("#messagesAndCartReviewFrame_errors_body").html("");
    jQuery("#messagesAndCartReviewFrame_warnings_body").html("");
  }
  
  
  
  function setupmessagesAndCartReviewFrame(){
    // jQuery("#messagesAndCartReviewButton").hover(function(){messageBoxOut = true; showHideMessageBox();},function(){});
    // jQuery("#messagesAndCartReviewFrame_closeButton").click(function(){messageBoxOut = false; showHideMessageBox(); resetMessages();});
    // jQuery("#messagesAndCartReviewFrame").hide();
  
  // checking for reload data
  if (!pageHasBeenReloadedByJs){
     checkForReloadingData();
  }
  }
