//-------------------------------
function increasecart(id){
    // Get the cart contents
    const cart = new Cart( "withinwebcartkey03" );       // ??? can this go somewhere else ???

    // Increment by 1
    cart.increase(id);
}
//-------------------------------
function decreasecart(id){
    // Get the cart contents
    const cart = new Cart( "withinwebcartkey03" );       // ??? can this go somewhere else ???

    // Decrement by 1
    cart.decrease(id);
}
//-------------------------------
// Add this id to the cart.
function addtocart(id) {    
    // Get the cart contents
    const cart = new Cart( "withinwebcartkey03" );       // ??? can this go somewhere else ???

    // Add the item
    cart.add(id);
    updatebaskettotal();
}
//-------------------------------
// Calculate the total price and add to total display
function updatebaskettotal() {

    // Read local storage using the given key
    const datacart = JSON.parse( localStorage.getItem("withinwebcartkey03") );       // ????? could this be put elsewhere

    var total = 0;
    var numberinbasket = 0;

    // Loop around the data array to calculate the total basket
    for (let i = 0; i < datacart.length; i++) {
        let item = datacart[i];
            numberinbasket++;
            total = Number(total) + Number(item.total);
    }

    $("#totalbasket").text(total.toFixed(2));
    $("#numberinbasket").text(numberinbasket);

    console.log("here");
    console.log(total.toFixed(2));

}
//-------------------------------
// From local storage update the cart display
function showCart() {

    // Read local storage using the given key
    const datacart = JSON.parse( localStorage.getItem("withinwebcartkey03") );       // ????? could this be put elsewhere
    
    // Loop around the data array to display the cart details
    for (let i = 0; i < datacart.length; i++) {
        let item = datacart[i];
          console.log('item_name: ' + item.item_number);
          console.log('item_number: ' + item.qty);
          console.log('item: ' + item.color);
          console.log('quantity: ' + item.mc_gross);
          console.log('total: ' + item.total);

          $('.cartresult').append (

            `
            <div class="row">

                <div class="col-md-3">
                    ${item.item_number}
                </div>

                <div class="col-md-2">
                    ${item.qty}
                </div>
                
                <div class="col-md-2">
                    ${item.mc_gross}
                </div>

                <div class="col-md-2">    
                    ${item.total}
                </div>

                <div class="col-md-3">
                   <a href="">+</a>&nbsp;&nbsp;<a href="">-</a>
                </div>

            </row>
            `
        );
    }
    
    updatebaskettotal();
}
