function webshop_cart_refresh_box()
{
    $.getJSON(
        webshop_url_cart + '?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);
                });
                $('#cartbox_elements').html(output);
                if($('#shoppingcart').css('display') != 'block')
                {
                    $('#shoppingcart').show();
                }
            } else {
                if($('#shoppingcart').css('display') != 'none')
                {
                    $('#shoppingcart').hide('slow');
                }
            }
        }
    );
}
function webshop_cart_refresh_total_price()
{
    $.getJSON(
        webshop_url_cart + '?xhr',
        function(response){
            $('#cart_total_price').html(response.total_price);
        }
    );
}

function webshop_cart_add(article) {
    $.getJSON(
        webshop_url_update +'?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?')) {
    $.getJSON(
        webshop_url_update +'?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);
    }
    
    $.getJSON(
        webshop_url_update +'?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;
            }               
        }
    );
}
