//Haniff Murray 5/7/2010: DOM Storage functions
function set_meta(name, value, json) {

    if (value === null) {
        value = '';
    }

    value = json ? encodeURIComponent($.JSON.encode(value)) : encodeURIComponent(value);

    datastore.set(name, value);
}

function get_meta(name, json) {
    // get data back from store, and prompt user with it
    datastore.get(name, function (ok, val) {
        if (ok) {
            cookieValue = json ? $.JSON.decode(decodeURIComponent(val)) : decodeURIComponent(val);
        } else
            cookieValue = null;
    });

    return cookieValue;
}

//<![CDATA[
function updateList() {
    var checkList = $('#slist input:checked');
    checkList.each(function () {
        curItem = $(this).val();
        iparts = curItem.split("_");
        if (iparts[0] == "on") {
            onid = (iparts[1]);
            for (var l = 0; l < shoppingListJSON.listItems.length; l++) {
                curItem = shoppingListJSON.listItems[l];
                if (curItem.itemid == onid) {
                    curItem.state = "On";
                }
            }
        }
    });

    if ($("#additems").val() != "") {
        shoppingListJSON.additionalItems = $("#additems").val();
        shoppingListJSON.additionalItems = shoppingListJSON.additionalItems.replace(/\n/g, "@@n");
        shoppingListJSON.additionalItems = shoppingListJSON.additionalItems.replace(/\r/g, "@@r");
    }

    set_meta('slist-' + slistID, shoppingListJSON, true);

    alert("Your shopping list for this project has been saved.");
}

function makePrintList() {
    var tmphtml = "";
    var slistname = "";
    var categories = new Array;

    // get the slist name based on the page
    pagestore.get(sPage, function (ok, val) {
        if (ok) {
            slistname = val;
        } else
            slistname = null;
    });

    // get the shopping list
    var shoppingListJSON = get_meta(slistname, true);
    if (shoppingListJSON != undefined) {
        if (shoppingListJSON.listItems.length > 0) {
            for (var l = 0; l < shoppingListJSON.listItems.length; l++) {
                curItem = shoppingListJSON.listItems[l];
                if (curItem != null) {
                    if (curItem.category != null) {
                        if (curItem.category != "") {
                            incatarray = false;
                            for (var c = 0; c < categories.length; c++) {
                                if (categories[c] == curItem.category) incatarray = true;
                            }
                            if (incatarray == false) categories[categories.length] = curItem.category;
                        }
                    }
                }
            }

            if (categories.length > 0) {
                for (var c = 0; c < categories.length; c++) {
                    if (categories[c] != "") tmphtml = tmphtml + "<p><strong>" + categories[c] + "<\/strong><\/p>";
                    tmphtml = tmphtml + "<ul>";
                    for (var l = 0; l < shoppingListJSON.listItems.length; l++) {
                        if (shoppingListJSON.listItems[l] != null) {
                            if (shoppingListJSON.listItems[l].category != null) {
                                if (shoppingListJSON.listItems[l].category == categories[c]) tmphtml = tmphtml + formatPrintListItem(shoppingListJSON.listItems[l], l);
                            }
                        }
                    }
                    tmphtml = tmphtml + "<\/ul>";
                }
            } else {
                tmphtml = tmphtml + "<ul>";
                for (var l = 0; l < shoppingListJSON.listItems.length; l++) {
                    tmphtml = tmphtml + formatPrintListItem(shoppingListJSON.listItems[l], l);
                }
                tmphtml = tmphtml + "<\/ul>";
            }
        }
        if (shoppingListJSON.additionalItems != undefined) {
            if (shoppingListJSON.additionalItems != "") {
                tmphtml = tmphtml + "<p><strong>Additional Items:<\/strong><\/p><ul>";
                shoppingListJSON.additionalItems = shoppingListJSON.additionalItems.replace(/@@n/g, "\n");
                shoppingListJSON.additionalItems = shoppingListJSON.additionalItems.replace(/@@r/g, "\n");
                shoppingListJSON.additionalItems = shoppingListJSON.additionalItems.replace(/\n\n/g, "\n");
                var addtl_items = shoppingListJSON.additionalItems.split("\n");
                for (var l = 0; l < addtl_items.length; l++) {
                    tmphtml = tmphtml + "<li>" + addtl_items[l] + "<\/li>";
                }
                tmphtml = tmphtml + "<\/ul>";
            }
        }
        var foo = document.getElementById("slist");
        foo.innerHTML = tmphtml;
    }
}

function formatPrintListItem(listItem, lnum) {
    var tmplitem = "";

    if (listItem.state.toLowerCase() == "on") {
        tmplitem = tmplitem + "<li><del>" + listItem.desc + "<\/del><\/li>\r";
    } else {
        tmplitem = tmplitem + "<li>" + listItem.desc + "<\/li>\r";
    }
    return tmplitem;
}

function makeShoppingList() {
    var tmphtml = "";
    var categories = new Array;
    // var slistFromCookie = $.cookie.get('slist-'+slistID,true);
    //alert('Initializing: Cookie Reading');
    //if (slistFromCookie != undefined){
    //	shoppingListJSON = slistFromCookie;
    //	alert('Able to read Cookie');
    //} 
    //else
    //{
    //	alert('Cookie NOT read');
    //}
    if (shoppingListJSON != undefined) {
        if (shoppingListJSON.listItems.length > 0) {
            for (var l = 0; l < shoppingListJSON.listItems.length; l++) {
                curItem = shoppingListJSON.listItems[l];
                if (curItem != null) {
                    if (curItem.category != null) {
                        if (curItem.category != "") {
                            incatarray = false;
                            for (var c = 0; c < categories.length; c++) {
                                if (categories[c] == curItem.category) incatarray = true;
                            }
                            if (incatarray == false) categories[categories.length] = curItem.category;
                        }
                    }
                }
            }

            if (categories.length > 0) {
                for (var c = 0; c < categories.length; c++) {
                    if (categories[c] != "") tmphtml = tmphtml + "<p>" + categories[c] + "</p>\r\r";
                    for (var l = 0; l < shoppingListJSON.listItems.length; l++) {
                        if (shoppingListJSON.listItems[l] != null) {
                            if (shoppingListJSON.listItems[l].category != null) {
                                if (shoppingListJSON.listItems[l].category == categories[c]) tmphtml = tmphtml + formatListItem(shoppingListJSON.listItems[l], l);
                            }
                        }
                    }
                }
            } else {
                for (var l = 0; l < shoppingListJSON.listItems.length; l++) {
                    tmphtml = tmphtml + formatListItem(shoppingListJSON.listItems[l], l);
                }
            }
            tmphtml = tmphtml + "<div class='clearfloat'><\/div>\r\r";
            $("#slist").html(tmphtml);
        }
        if (shoppingListJSON.additionalItems != undefined) {
            if (shoppingListJSON.additionalItems != "") {
                shoppingListJSON.additionalItems = shoppingListJSON.additionalItems.replace(/@@n/g, "\n");
                shoppingListJSON.additionalItems = shoppingListJSON.additionalItems.replace(/@@r/g, "\r");
                $("#additems").val(shoppingListJSON.additionalItems);
            }
        }
    }
}

function formatListItem(listItem, lnum) {
    var tmplitem = "";
    tmplitem = tmplitem + "<div class='clearfloat'><\/div>\r<label class='shoppingItemLabel'>" + listItem.desc + "<\/label>\r";
    onstate = "";
    offstate = "checked='checked'";
    if (listItem.state.toLowerCase() == "on") {
        onstate = offstate;
        offstate = "";
    }
    tmplitem = tmplitem + "<input value='on_" + listItem.itemid + "' name='radiostyle" + listItem.itemid + "' class='styled' type='radio' " + onstate + " />\r";
    tmplitem = tmplitem + "<input value='off_" + listItem.itemid + "' name='radiostyle" + listItem.itemid + "' class='styled' type='radio' " + offstate + " />\r";
    tmplitem = tmplitem + "<div class='clearfloat'><\/div>\r\r";
    return tmplitem;
}

function loadShoppingList(jfile) {
    listPath = "/data/";
    listFile = listPath + 'shopping_list_' + jfile + '.js';
    //alert(listFile);
    $.ajax({ type: 'GET', url: listFile, dataType: 'script', success: makeShoppingList });
}

function setShoppingCookie(cData) {
    var listname = 'slist-' + slistID;

    //append the page id to each iFlow url
    $("a.iFlowLink").each(function () {
        this.href = this.href + "&id=" + slistID;
    });

    shoppingListJSON = get_meta(listname, true);
    pagestore.set(sPage, listname);

    if (shoppingListJSON == null) {
        shoppingListJSON = $.JSON.decode(decodeURIComponent(cData));
        set_meta('slist-' + slistID, shoppingListJSON, true);
    }
}

//]]>

