
var basket = {

    //item(Array)
    //  0: Array
    //      0: id, 1: count
    //  1: Array
    //      0: id, 1: count
    //  ...
    item : new Array(),
    order : new Array(),

    addItem : function(artId, count){
        var exist = false;
        var index = -1;
        
        $.each(this.item, function(i, val){
            if (val[0] == artId){
                exist = true;
                index = i;                
            }
        });

        if(exist){
            this.item[index][1]+= count;
        }
        else{
            this.item.push(new Array(artId, count));
        }
    },

    addOrder : function(artId, count, extra){
        
    },

    itemToJson : function(){
        var string = '{ "item" : { ';
        $.each(this.item, function(index, entry){
            if (index>0)
                string+= ', ';
            string+= '"'+index+'" : {';
            $.each(entry, function(i, val){
                if (i>0)
                    string+= ', ';
                string+= '"'+i+'" : "'+val+'"';
            });
            string+= '}';
        });
        
        string+= '}}';

        return string;
    },

    changeQty : function (pos, qty){
        
        if (qty > 0){
            $.post("ajax.php", {"do" : "setCountOfArtikelInBasket", "pos" : pos, "qty" : qty}, function(data){
                loadWarenkorb();
            });
        }
        else{
            basket.remove(pos);
        }
    },

    remove : function (pos){

        $.post("ajax.php", {"do": "removeItemFromBasket", "pos" : pos}, function(data){
           loadWarenkorb();
           updateWKCounter();
        });
    },

    setPayment : function(payment){

        $.post("ajax.php", {"do": "setPayment", "payment" : payment});
    },

    sendOrder : function(comment){

        $.post("ajax.php", {"do": "sendOrder", "comment" : comment}, function(data){
            if (data.search("true") != -1){
                    window.location.hash = 'loadShoppingEnd()';
            }
            else{
                alert("Ein Fehler ist aufgetreten, bitte senden Sie Erneut");
            }            
        });
    }

}

