function webshop_cart_refresh_box()
{
	var now = new Date().getMilliseconds(); // Cache-Breaker
	
    $.getJSON(
        webshop_url_cart + '?t='+now+'&xhr',
        function(response){
            if(response.cart.length > 0)
            {
                output = ''
                $.each(response.cart, function(i, row){
                    oline = webshop_cart_block_line
                    output += oline.replace('#id#', row.id).replace('#slug#', row.slug).replace('#title#', row.title).replace('#amount#', row.amount).replace('#price#', row.price.replace('.', ','));
                });
                
                
                $('#cartbox_elements').html(output);
                if($('#shoppingcart').css('display') != 'block')
                {
                    $('#shoppingcart').show();
                }
                if($('#minicart').css('display') != 'block')
                {
                    $('#minicart').show();
                }
            } else {
                if($('#shoppingcart').css('display') != 'none')
                {
                    $('#shoppingcart').hide('slow');
                }
                if($('#minicart').css('display') != 'none')
                {
                    $('#minicart').hide('slow');
                }
            }
        }
    );
}
function webshop_cart_refresh_total_price()
{
	var now = new Date().getMilliseconds(); // Cache-Breaker
	
    $.getJSON(
        webshop_url_cart + '?t='+now+'&xhr',
        function(response){
            $('#cart_total_price').html(response.total_price);
        }
    );
}

function webshop_cart_add(article) {
	var now = new Date().getMilliseconds(); // Cache-Breaker
    $.getJSON(
        webshop_url_update +'?t='+now+'&action=add&article='+article,
        function(response){
        	if(response.state == 'failed')
            {
                $('#cart_info_text').html('Der Artikel konnte nicht in den Warenkorb gelegt werden.');
                $('#cart_info').show('slow');
                return false;
            } else {
                $('#cart_info_text').html('Der Artikel "' + response.article.title + '" wurde in den Warenkorb gelegt.');
                webshop_cart_refresh_box();
                webshop_cart_refresh_total_price();
                $('#cart_info').show('slow');
                return true;
            }               
        }
    );
}

function webshop_cart_delete(article) {
    if(confirm('Wollen Sie den Artikel wirklick löschen?')) {
    	var now = new Date().getMilliseconds(); // Cache-Breaker
    	
    $.getJSON(
        webshop_url_update +'?t='+now+'&action=del&article='+article,
        function(response){
            if(response.state == 'failed')
            {
                $('#cart_info_text').html('Der Warenkorb konnte nicht aktualisiert werden.');
                $('#cart_info').show('slow');
                webshop_cart_update(article, '1');
                return false;
            } else {
                $('#cart_info_text').html('Der Warenkorb wurde aktualisiert.');
                $('#element_' + article).hide('slow');
                webshop_cart_refresh_box();
                webshop_cart_refresh_total_price();
                $('#cart_info').show('slow');
                return true;
            }               
        }
    );
    }
}

function webshop_cart_update(article, amount)
{
    if(amount.length < 1 || amount.toString().search(/^[0-9]+$/) != 0)
    {
        return false;
    }
    if(amount < 1)
    {
        return webshop_cart_delete(article);
    }
    
    var now = new Date().getMilliseconds(); // Cache-Breaker
    $.getJSON(
        webshop_url_update +'?t='+now+'&action=mod&article='+article+'&amount='+amount,
        function(response){
            if(response.state == 'failed')
            {
                $('#cart_info_text').html('Der Warenkorb konnte nicht aktualisiert werden.');
                $('#cart_info').show('slow');
                return false;
            } else {
                $('#cart_info_text').html('Der Warenkorb wurde aktualisiert.');
                $('#cart_box_tprice_'+article+'_box').html(response.tprice_article);
                webshop_cart_refresh_box();
                webshop_cart_refresh_total_price();
                $('#cart_info').show('slow');
                return true;
            }               
        }
    );
}

