/**
 * Funkcja sumująca wartość towaru w koszyku.
 */
function refreshSum(){
    var floatSum = 0.0;
    $(".sum").each(function(){
        floatSum += parseFloat($(this).html());
    });
    $("#allsum").html(floatSum);
    $("#spanShortCartSum").html(floatSum);
}

function decreaseAmount(id,refreshCartFlag){
    var value = Number($('#' + id).val());
    value = value - 1;
    if (value < 0) 
        $('#' + id).val(0);
    else 
        $('#' + id).val(value);
    if (refreshCartFlag){
        refreshCart();
    }
    else {
        if (isNaN(value) || value == 'NaN' || value < 1 || value > 20) {
            value = 4;
        }
        $('#' + id).val(value);
    }
}

function refreshCart(){
    var strCounters = '';
    var strIds = '';
    var intCounter = -1;
    var intId = -1;
    var summaryPrice = null;
    $(".cCount").each(function(){
        intCounter = parseInt($(this).val());
        var intCounter = parseInt($(this).val());
        if (isNaN(intCounter) || intCounter == 'NaN' || intCounter < 1 || intCounter > 20) {
            intCounter = 4;
        }
        $(this).val(intCounter);
        strCounters += intCounter + ',';
        intId = $(this).attr("id");
        intId = intId.split("_");
        intId = intId[1];
        strIds += intId + ',';
    });
    strIds = strIds.substr(0, strIds.length - 1);
    strCounters = strCounters.substr(0, strCounters.length - 1);
    strData = "ids=" + strIds;
    strData += "&counters=" + strCounters;
    $.ajax({
        type: "POST",
        url: "/cart/setcost",
        data: strData,
        dataType: 'json',
        success: function(json){
            $("#cart_price").html(json.DATA.PRODUCTS_SUM_COST+" zł");
            $("#span_short_cart_sum").html(json.DATA.PRODUCTS_SUM_COST);
            $("#products_amount").html(json.DATA.AMOUNT);
            if (json.DATA.SHIPMENT_COST == -1) {
                $("#cart_shipment_price").html('Ustalany indywidualnie.');
            }
            else {
                $("#cart_shipment_price").html(json.DATA.SHIPMENT_COST+" zł");
            }
            $.each(json.DATA.PRODUCTS_COST, function(i, prod){
                $("#spanProductCost_" + prod.DP_OID).html(prod.COST+" zł");
            });
            $('#summary_price').html(json.DATA.SUMMARY+" zł");
            $('#discount_price').html(json.DATA.DISCOUNT+" zł");
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            alert('błąd');
        }
    });
    strData = "ids=" + strIds;
    strData += "&counters=" + strCounters;
    $.ajax({
        type: "POST",
        url: "/cart/changecart",
        data: strData,
        dataType: 'json',
        success: function(json){
        
        }
    });
// koniec: zapisanie do bazy zmian
}

function increaseAmount(id,refreshCartFlag){
    var value = Number($('#' + id).val());
    value = value + 1;
    $('#' + id).val(value);
    if (refreshCartFlag){
        refreshCart();
    }
    else {
        if (isNaN(value) || value == 'NaN' || value < 1 || value > 20) {
            value = 4;
        }
        $('#' + id).val(value);
    }
}

$(document).ready(function(){
    /**
     * Zmiana ilości towaru na karcie towaru.
     */
    $(".cCount").change(function(){
        intCounter = parseInt($(this).val());
        var intCounter = parseInt($(this).val());
        if (isNaN(intCounter) || intCounter == 'NaN' || intCounter < 1 || intCounter > 20) {
            intCounter = 4;
        }
        $(this).val(intCounter);
    });
});
