$(document).ready(function(){
    quantity = localStorage["quantity"];
    people = JSON.parse(localStorage["people"]);
    orders = JSON.parse(localStorage["orders"]);

    if(quantity < 2 || !people || !orders){
        localStorage.removeItem("quantity");
        localStorage.removeItem("people");
        localStorage.removeItem("orders");
        window.location.href = "index.html";
    }

    total = 0;

    html = '';
    for(var i = 1 ; i <= quantity ; i++){
        html = html + '<h1>' + people[i-1][1] + ':</h1>';
        orders.forEach(function(order){
            eaters = order[3];
            eaters.forEach(function(eater){
                if(eater == i){
                    html = html + '<h4>' + order[1] + ' - $' + order[2] + '</h4>';
                }
            });
        });
        html = html + '<h2>Total: $' + people[i-1][2] + '</h2>';
        html = html + '<br><br><hr>';

        total = total + people[i-1][2];
    }
    html = html + '<br><br><h1>Total: $' + total + '</h1>';
    $('#list').prepend($(html));

    $('#add').click(function(){
        window.location.href = "add.html";
    });
});

document.addEventListener("backbutton", function(e){
    navigator.app.exitApp();
}, false);