var code = 0;
var pdvAllowed = false;
var catalogId = 0;

function send(params){
    window.location.href = "/my-carrefour/prenotazione-premi.html?" + params;
}

var wishlistManager = $.manageAjax.create('wishlistManager', {
    queue: true,
    maxRequests: 4,
    preventDoubbleRequests: true,
    cacheResponse: true
});

$(document).ready(function(){
$("span.ico_print").hide();
	var pdvCode = 'no';
	catalogId = getUrlParameter('catalogId');
	if(catalogId == null || catalogId == ""){
		$.getJSON("/AjaxRpc/c4UserValidationWeb.getGiftCatalogInfo?pdvCode="+pdvCode, null, function(data, textstatus){
		       if (data.result) {
					eval('res=' + data.data);
					catalogId = res.catalogId;					
		    	}				
			});
	}
    $.getJSON("/AjaxRpc/c4UserValidationWeb.retrieveCarrefourClientPoints?", null, function(data, textstatus){
        if (!data.result) {
            $("#myPoint").text("0");
            myPoint = 0;
        }
        else {
            myPoint = 0;
            eval('res=' + data.data);
            if (res['points'] != null && res['points'] != 'undefined') {
                $("#myPoint").text(res['points']);
                myPoint = parseInt(res['points']);
            }
            pdvAllowed = res['pdvAllowed'];
        }
        
        $.getJSON("/AjaxRpc/wishlistManager.retrieveGift?_=" + new Date().getMilliseconds().toString(), null, function(data, textstatus){
            if (!data.result) {
                $("#wishlistpremi").html('<h1>Nessun premio scelto.</h1>');
            }
            else {
                eval('bean=' + data.data);
                
                var gifts = bean[0].gifts;
                //            alert('dimensione g: ' + gifts.length);
                
                
                //set ordered gifts
                var ids = '';
                for (var i = 0; i < gifts.length; i++) {
                    ids += gifts[i] + '-';
                }
                $("#ordered_").attr('id', 'ordered_' + ids + '0');
                
                
                var links = bean[0].links;
                //            alert('dimensione l: ' + bean[0].gifts.length);

                if (links.length == 0) {
                    $("#wishlistpremi").html('<br></br><h1><center>Nessun premio scelto.</center></h1>');
                }
                
                for (var i = 0; i < links.length; i++) {
                
                    wishlistManager.add({
                        url: links[i],
                        dataType: 'html',
                        success: function(data, textstatus){
                            $("#wishlistpremi").append('<li>' + data + '</li>');
                        },
                        error: function(data, textstatus){
                            //						alert("errore! " + data.message);
                        }
                    });
                    
                }
            }
        });
		
    });
    
});

