﻿function formatPrice(price)
{
    var newPrice="";
    if(document.getElementById('hCulture').value!="hu-HU")
    {
        newPrice = price.toFixed(2);
    }
    else
    {
        newPrice = price.toFixed(0);
    }
    return newPrice;
}

function AddToCart(vID, quantity, optionID, optionName)
{
    
    window.scrollTo(0, 0);
    //(function($) { 
        if (quantity) {
            var cant = quantity;
        } else { 
            var cant = document.getElementById('txQty'+vID).value;
        }
        if(optionID==undefined)
        {
            var optionID = document.getElementById('ctl00_cpBody_ddSize').value;
        }
        if(optionName==undefined)
        {
            var mylist=document.getElementById("ctl00_cpBody_ddSize");
            var optionName = mylist.options[mylist.selectedIndex].text;
        }
        if(optionID == 0)
        {
            alert("Trebuie sa selectati o marime");
            return;
        }
        var result = Layout.AddToCartWithOptions(vID, cant, optionID, optionName); 
        if(result!=null && result.value!=null)
        {
            update = 'add';
            updateSmallShoppingCart(result, update);
        }
    //})(jQuery);
    
}

function removeFromCart(vID, optionID)
{
    var result = Layout.RemoveProduct(vID, optionID); 
    if(result!=null && result.value!=null)
    {
        updateSmallShoppingCart(result);
    }
}

function updateCart(vID, quantity)
{
    var result = Layout.UpdateProduct(vID, quantity); 
    if(result!=null && result.value!=null)
    {
        updateSmallShoppingCart(result);
    }
}

function updateSmallShoppingCart(result, update){
    (function($) { 
         
        //totalPrice
        var totalPrice = 0;
        var totalQuantity = 0;
        
        var list = $('#inlineCart > ul').empty();
        $.each(result.value, function(i, n){
           // console.log(i, n);
            //n.Price = n.Price.toFixed(2);
            var Price = formatPrice(eval(n.Price*n.Quantity))+' '+getJSPH('Lei')+' ('+n.Quantity+' x '+formatPrice(n.Price);
            if (n.PriceOld != 0) Price +=' <del>'+formatPrice(n.PriceOld)+'</del>';
            Price += ')';
            
            $('<li><div class="text"><a href="/detaliuProdus.aspx?produs='+n.ProductID+'"><strong>'+n.ProductName + " (" + n.OptionName + ")"+'</strong></a>'+Price+'</div><div class="cant"><a href="#-1" onclick="AddToCart('+n.VariantID+',-1,'+n.OptionID+',\''+n.OptionName+'\')" class="minus">-1</a><input id="txCartQ'+n.VariantID+'" disabled=disabled type="text" onkeyup="updateCart('+n.VariantID+',this.value)" value="'+n.Quantity+'"/><a href="#+1" onclick="AddToCart('+n.VariantID+',1,'+n.OptionID+',\''+n.OptionName+'\')" class="plus">+1</a><br/><a href="#sterge" onclick="removeFromCart('+n.VariantID+','+n.OptionID+');">'+getJSPH("Produs_sterge")+'</a></div></li>')
            .appendTo(list);
           
           //Force numeric
           list.find('input').numeric();
           
           //totalPrice
           totalPrice += eval(n.Price*n.Quantity);
           totalQuantity += n.Quantity;
        });
        
        //totalPrice UPDATE
        if (totalPrice == 0) {
            $('#noProducts').show();
            
            $('#inlineCart ul').hide();
             
            $('#buyButtons').hide();
            $('#inlineCart .total').hide();
            //$('#inlineCartBar .produseInCos').text(getJSPH('Produs_Nu aveti produse in cos'));
            $('#inlineCartBar .produseInCos').empty();
            $('#inlineCartBar h4').addClass('noProducts');

        } else {
            totalPrice = formatPrice(totalPrice) + " lei";//+ getJSPH('Produs_ Lei');
            $('#noProducts').hide();
            
	        $('#inlineCart ul').show();
	        
            $('#buyButtons').show();
            $('#inlineCart .total').show().find('strong').empty().append(totalPrice);
            if (totalQuantity==1)
            {
                $('#inlineCartBar .produseInCos').html(getJSPH('Aveti <strong>')+totalQuantity+getJSPH('Produs_</strong> produs (<span>')/*+totalPrice+getJSPH('Produs_ incl. TVA</span>)')*/);
            }
            else
            {
                $('#inlineCartBar .produseInCos').html(getJSPH('Aveti <strong>')+totalQuantity+getJSPH('Produs_</strong> produse (<span>')/*+totalPrice+getJSPH('Produs_ incl. TVA</span>)')*/);
            }
            //$('#inlineCartBar .produseInCos').html('<strong>'+totalQuantity+ ((totalQuantity == 1) ? " produs" : " produse")+'</strong> (<span>'+totalPrice+'</span>)');
            $('#inlineCartBar h4').removeClass('noProducts');
        }
        
        if (!$('#inlineCart').is(':visible') && (update == 'add')) {
            $('#inlineCartWrapper').addClass('active');
            $('#inlineCart').show();
        }
        
    })(jQuery);
}

jQuery(function(){
    //jQuery('#noProducts').hide();
    removeFromCart(0);
});
