/**
 * Adding single item to cart, without checking for necessary fields filling
 * @param {integer} Passing item id to the function
 */
function cartAdd(item){

    $.post('/actions/addtocart.php', {
        item: item
    }, updateMiniCart, 'json');
}
/**
 *  Updating minicart with the provided data. If json object has status setting as 'error', then message will be alerted,
 *  in other case it will be written to the minicart section of the site 
 * @param {json} 
 * 
 */
function updateMiniCart(data){
    if (data.status == 'error') {
        alert(data.message);
    }
    else {
	    $('#cart>div').slideUp('fast',function() { $('#cart>div').html(data.message); $('#cart>div').fadeIn('slow'); });
    }
}
function updateMiniCart2(data){
  $('#cart>div').slideUp('fast',function() { $('#cart>div').html(data); $('#cart>div').fadeIn('slow'); });
}

$(document).ready(function(){
    $('#form_addtocart').ajaxForm({
        dataType: 'json',
        success: updateMiniCart
    });
});
function updateMini(){
    $.get('/actions/minicart.php', updateMiniCart2, 'html' );
}


function cartUpdate(data){
    if (data.status == 'error') {
        alert(data.message);
    }
    else {
        $('#cartSubTotal').html(data.sum);
        $('#cartShipping').html(data.shipping);
        $('#cartTotal').html(data.total);
        $('#amount').val(data.sumC);
        $('#handling_cart').val(data.shippingC);
        updateMini();
    }
}


function cartRemove(item,question){
    if (confirm(question)) {
        $.get('/actions/removecart.php', {
            item: item
        }, function(data){
            if (data.status == 'error') {
                alert(data.message);
            }
            else {
                if (data.status == 'replace') {
                   $('#item_' + data.removed ).slideUp('slow',function() { $('#fullcart').html(data.message); updateMini()});
                }
                else {
                    $('#item_' + data.removed ).slideUp('slow',updateMini());
                    $('#cartSubTotal').html(data.sum);
                    $('#cartShipping').html(data.shipping);
                    $('#cartTotal').html(data.total);
                    $('#amount').val(data.sumC);
                    $('#handling_cart').val(data.shippingC);
                    
//Getting the number in the list of the products
                    name=$('#name_'+item).attr('name');
                    index=name.lastIndexOf('_');
                    number=name.substring(index+1);
                    
//Removing hidden fields that holding an item
                    $('#name_'+item).remove();
                    $('#quantity_'+item).remove();
                    $('#amount_'+item).remove();
                     
                    number++;
                    
//Iterating through all the items which number was greater than removed one and decreasing their number to 1
                    while($('input[name=quantity_'+number+']').attr('name')==('quantity_'+number)) {
                       $('input[name=quantity_'+number+']').attr('name','quantity_'+(number-1));
                       $('input[name=amount_'+number+']').attr('name','amount_'+(number-1));
                       $('input[name=item_name_'+number+']').attr('name','item_name_'+(number-1));
                       number++;
                    }
                }
            }
        }, 'json');
    }
    else {
        return false;
    }
}