function onAddedToCartMessage(msg){
    // Alert
    alert(msg);

    uzRecalcBonusPoints();

    /*
    // Prepare total cart amount
    var cartTotal = amiSession.get("eshop_cart_total");
    var re = new RegExp("[\. 0-9]+");
    var found = cartTotal.match(re); 
    //alert(found);
    found += "";
    var found = found.replace(/ /i, '');
    //alert(found);
    cartTotal = parseFloat(found);
    
    //alert(cartTotal);
    //*/
}

/*
amiCart.ajaxCallback = function(status, content){
        if(status == 1 && (cartStatusPos = content.indexOf('cart updated')) >= 0){
            content = content.substr(cartStatusPos);
            amiSession.loadVariables();
            var aVarNames = content.split('|');
            alert(aVarNames[1]);
            alert(aVarNames[2]);
            amiCart.updateBlock(aVarNames[1], aVarNames[2]);

            var actionStatus = '';
            for(i = 3; i < aVarNames.length; i++){
                actionStatus += (i > 3 ? '|' : '') + aVarNames[i];
            }
            if(typeof(onAddedToCartMessage) == 'function'){
                onAddedToCartMessage(actionStatus);
            }else{
                alert(actionStatus);
            }
        }
    }
//*/


function uzRecalcBonusPoints(){
    var cartAmount, price, percent, amt;

    //alert("uzRecalcBonusPoints");
    // Calc points for products
    $("div.price_bonus").each( function(i){
        //alert(this.innerHTML);
        cartAmount = uzGetCartAmount();
        price = uzParseItemPrice(this.innerHTML);
        percent = uzGetBonusPercent(price + cartAmount);
        //alert(cartAmount + ", " + price + ", " + percent);

        //amt = Math.round(price / 100 * percent * bonusPointsCoef);

        amt = Math.round(price / 100 * percent * bonusPointsCoef * 100)/100;
        //this.innerHTML =  "cart = " + cartAmount + ", price = " + price + ", percent =" + percent + ", amt = " + amt;
        //this.innerHTML = '<span style="display: none;">' + price + '</span>' + "cart = " + cartAmount + ", price = " + price + ", percent =" + percent + ", amt = " + amt;
        this.innerHTML = '<span style="display: none;">' + price + '</span>' + amt;

        this.style.display = "inline";
        //this.innerHTML + " +++ ";
    });

    // Calc points for cart
    if(document.getElementById("cart_bonus_amount") || document.getElementById("cart_bonus_amount_head")){
        cartAmount = uzGetCartAmount();
        percent = uzGetBonusPercent(cartAmount);
        amt = Math.round(cartAmount / 100 * percent * bonusPointsCoef);
        if(document.getElementById("cart_bonus_amount")){
            document.getElementById("cart_bonus_amount").innerHTML = amt; 
        }
        if(document.getElementById("cart_bonus_amount_head")){
            document.getElementById("cart_bonus_amount_head").innerHTML = amt; 
        }
        
    }
}

function uzParseItemPrice(priceStr){
    var res = 0;
    //alert(priceStr);
    //<span class="price_price">&nbsp;1 905&nbsp;руб.&nbsp;</span>

    //var re = new RegExp("[\. 0-9]+");
    var re = /[0-9]+[\., 0-9]+/;
    var found = priceStr.match(re); 
    if(found != null){
        found = found.shift();
        found = found.replace(/ /i, '');
        found = found.replace(/,/i, '');
        res = parseFloat(found);
    }
    //alert(found);
    return res;
}

function uzGetBonusPercent(price){
    var curPerc = 0;
    if(typeof(aBonusPointsRanges) != 'undefined'){
        //alert("aBonusPointsRanges exist, " + aBonusPointsRanges.length);
        if(aBonusPointsRanges.length){
            var curPerc = aBonusPointsRanges[0].perc;
            for(var i = 0; i < aBonusPointsRanges.length; i++){
                if(aBonusPointsRanges[i].amount > price){
                    return curPerc;
                }
                curPerc = aBonusPointsRanges[i].perc;
            }
        }
    }
    return curPerc;
}

function uzGetCartAmount(){
    var cartTotal = amiSession.get("eshop_cart_total");
    var re = new RegExp("[\. 0-9]+");
    var found = cartTotal.match(re); 
    //alert(found);
    found += "";
    var found = found.replace(/ /i, '');
    //alert(found);
    if(isNaN(found)){
        found = 0;
    }
    cartTotal = parseFloat(found);
    
    //alert(cartTotal);
    return cartTotal;
}


function uzSetBonusHelpPopup(btnClassName){
    //uzBonusHelpPopup = "";
    $(document.body).append("<div id='uz_bonus_dest_dlg'></div>");

    var dlgOpt = {
        autoOpen: false,
        resizable: false
    };
    $(function() {
		$("#uz_bonus_dest_dlg").dialog(dlgOpt);
	});

    $("."+btnClassName).click( function(){
        // Item price
        var price = 0;
        aPrice = $(this).find("span.hidden_item_price");
        if(aPrice.length){
            price = uzParseItemPrice($(aPrice[0]).html());
        }

        var cartAmount = uzGetCartAmount();
        var percent = uzGetBonusPercent(price + cartAmount);

        var amt = Math.round(price / 100 * percent * bonusPointsCoef);

        // Prepare msg
        aMsg = $(this).find("span.hidden_item_bonus_desc");
        if(aMsg.length){
            var text = $(aMsg[0]).html();
            text = text.replace(/_bonus_amount_/i, amt);
            text = text.replace(/_cart_amount_/i, cartAmount);
            $("#uz_bonus_dest_dlg").html(text);
            $("#uz_bonus_dest_dlg").dialog("open");
            //alert(text);
        }
        //alert("title");
        //$(uzBonusHelpPopup).show();
    });    
}


