/**
 * 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
    });
    $('#formUpdateCart').ajaxForm({
       beforeSubmit: function() { $('#loader').show(); },
       dataType: 'json',
       success: cartUpdate
    });
});
function updateMini(){
    $.get('/actions/minicart.php', updateMiniCart2, 'html' );
}


function cartUpdate(data){
   $('#loader').hide();
   if (data.status == 'error') {
       alert(data.message);
       $('#checkoutFlag').remove();
   }
   else {
      if (data.status == 'replace') {
         $('#fullcart').html(data.message);
         updateMini();
      }
      else {
         $('#cartSubTotal').html(data.sum);
         $('#cartShipping').html(data.shipping);
         $('#cartTotal').html(data.total);
         $('#amount').val(data.sumC);
         $('#handling_cart').val(data.shippingC);
         $('#amountPaypal').html(data.total);
         $('#amountWMZ').html(data.wmz);
         $('#amountWMR').html(data.wmr);
         $('#amountWMU').html(data.wmu);
         $('#aWMZ').val(data.wmzC);
         $('#aWMR').val(data.wmrC);
         $('#aWMU').val(data.wmuC);
         if (data.toremove != '') {
            for (i = 0; i < data.toremove.length; i++) {
               item = data.toremove[i];
               $('#item_' + item).remove();
               //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++;
               }
            }
         }
         if (data.toupdate != '') {
            for (i in data.toupdate) {
               item = i;
               amount = data.toupdate[i];
               $('#quantity_' + item).attr('value',amount);
            }
         }

         if(data.checkout!='') {
            $('#'+data.checkout).submit();
         }
         else {
            $('#checkoutFlag').remove();
         }
      }
   }
   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);
                    $('#amountPaypal').html(data.total);
                    $('#amountWMZ').html(data.wmz);
                    $('#amountWMR').html(data.wmr);
                    $('#amountWMU').html(data.wmu);
                    $('#aWMZ').val(data.wmzC);
                    $('#aWMR').val(data.wmrC);
                    $('#aWMU').val(data.wmuC);
                    
//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;
    }
}

function switchValue (value) {
   $('#paymentTotal>h2>span').addClass('hiddenText');
   $('#'+value).removeClass('hiddenText');
}
function checkout() {
   $('#formUpdateCart').prepend('<input type="hidden" name="checkout" value="1" id="checkoutFlag">');
   $('#formUpdateCart').submit();
}
