//
// Help function, clears ids from node and children
//
function clearChildrenIds(node)
{
    if (node.nodeType == 1) {
        node.id = "";
        var nodeChildren = node.childNodes;

        for (var i = 0; i < nodeChildren.length; i++) {
            clearChildrenIds(nodeChildren[i]);
        }
    }
}


//
// Function to add a new ingredient
//
function appendProduct(id,name)
{

    // get the main table tbody
    theTable = document.getElementById("item_table");

    // name
    p_type = document.getElementById("template_product");
    p_type.innerHTML = name;
    product_id = document.getElementById("template_product_id");
    product_id.value = id;
//    product_id.id = id;

    // size
    p_size = document.getElementById("template_size");
    this_size = document.getElementById("size_"+id).options[document.getElementById("size_"+id).selectedIndex];
    p_size.innerHTML = this_size.text;
    size_id = document.getElementById("template_size_id");
    size_id.value = this_size.value;
    // reset size
    document.getElementById("size_"+id).selectedIndex = 0;

    // quantity
    p_quantity = document.getElementById("template_quantity");
    this_quantity = document.getElementById("quantity_"+id).value*1;
    p_quantity.innerHTML = this_quantity;
    product_quantity = document.getElementById("template_product_quantity");
    product_quantity.value = this_quantity;
    document.purchase_summary_form.product_count.value = document.purchase_summary_form.product_count.value*1 + this_quantity*1;
    // reset quantity
    document.getElementById("quantity_"+id).value = 0;

    // price
    p_price = document.getElementById("template_price");
    this_price = document.getElementById("price_"+id).innerHTML;
    p_price.innerHTML = '$' + dollarFormat(this_price);

    // subtotal
    p_subtotal = document.getElementById("template_subtotal");
    this_subtotal = this_quantity * this_price;
    p_subtotal.innerHTML = '$' + dollarFormat(this_subtotal);

    tr = document.getElementById("template_row").cloneNode(true);
    clearChildrenIds(tr);

    tr.className = "nrow";

    trs = theTable.getElementsByTagName("tr");
    last_tr = trs[trs.length-1];

    theTable.insertBefore(tr, last_tr);

    // renumber the rows
    renameItems();

    // feedback message
    if (this_quantity > 1) {
        plural = 's';
    } else {
        plural = '';
    }
    feedback = document.getElementById('message_' + id);
    feedback.innerHTML = feedback.innerHTML + '<br />' + this_quantity + ' ' + this_size.text + ' ' + name + plural + ' added to order';

    // change add item link text
    document.getElementById('add_item_' + id).innerHTML = 'add another item';
    document.getElementById('add_item_href_' + id).style.background = 'url(/images/add_another_item.gif)';
    document.getElementById('add_item_href_' + id).style.width = '99px';
    document.getElementById('add_item_href_' + id).rollPosition = '-101px';
}


//
// Function to add a new recipe
//
function appendRecipe(id,name,type, type_txt,view_url, valid_iu)
{

    // get the main table tbody
    theTable = document.getElementById("item_table");

    // name & link
    a = document.getElementById("template_name");
    a_children = a.childNodes;
    for (var i = 0; i < a_children.length; i++) {
        a.removeChild(a_children[i]);
    }
    a.href = view_url;
    a_text = document.createTextNode(name);
    a.appendChild(a_text);

    // type
    p_type = document.getElementById("template_type");
    p_type.innerHTML = type_txt;

    input_type = document.getElementById("template_item_type");
    input_type.value = type;

    // item id
    type = document.getElementById("template_item_id");
    type.value = id;

    tr = document.getElementById("template_row").cloneNode(true);
    clearChildrenIds(tr);

    tr.className = "nrow";

    //
    // Add any custom units
    //

    // get unit select
    selects = tr.getElementsByTagName("select");
    if (selects[0]) {
        for (var i = 0; i < valid_iu.length; i++) {
            selects[0].options.add(new Option(valid_iu[i].name, valid_iu[i].id));
        }
    }

    // get last (total) row
    trs = theTable.getElementsByTagName("tr");
    last_tr = trs[trs.length-1];

    theTable.insertBefore(tr, last_tr);

    // renumber the rows
    renameItems();

}


//
// Deletes table row given a child object
//
function deleteUnitRow(child_obj)
{

    while (child_obj.tagName != "TR") {
        child_obj = child_obj.parentNode;
    }

    child_obj.parentNode.removeChild(child_obj);
    renameItems();

}


function moveRow(child_obj, direction)
{

    while (child_obj.tagName != "TR") {
        child_obj = child_obj.parentNode;
    }

    current_row = child_obj;
    c_index = current_row.rowIndex;

    tbl = document.getElementById("item_table");

    if (direction == "up") {
        if (c_index > 1) {
            // copy the row above the clicked row
            test = tbl.rows[c_index - 1];
            // replace the row above with clicked row
            tbl.replaceChild(tbl.rows[c_index],tbl.rows[c_index - 1]);
            // add a new row
            tbl.insertRow(c_index);
            // insert the copied row
            tbl.replaceChild(test,tbl.rows[c_index]);
        }
    } else {
        if (c_index < tbl.rows.length -2) {

            // copy the row above the clicked row
            test = tbl.rows[c_index + 1];
            // replace the row above with clicked row
            tbl.replaceChild(tbl.rows[c_index],tbl.rows[c_index + 1]);
            // add a new row
            tbl.insertRow(c_index);
            // insert the copied row
            tbl.replaceChild(test,tbl.rows[c_index]);
        }
    }

    renameItems();

}


//
// Loops through recipes and sets the name and class
//
function renameItems()
{

    theTable = document.getElementById("item_table");
    rows = theTable.getElementsByTagName("tr");

    for (var i = 1; i < rows.length; i++) {

        inputs = rows[i].getElementsByTagName("input");
        for (var j = 0; j < inputs.length; j++) {
            input_name = inputs[j].getAttribute("name");
            input_name = input_name.replace(/\[\d+\]/, "[" + i + "]");
            inputs[j].setAttribute("name", input_name);
        }

        selects = rows[i].getElementsByTagName("select");
        for (var j = 0; j < selects.length; j++) {
            select_name = selects[j].getAttribute("name");
            select_name = select_name.replace(/\[\d+\]/, "[" + i + "]");
            selects[j].setAttribute("name", select_name);
        }
    }
}


//
// Format a number as a dollar amount
//
function dollarFormat(dolFloat)
{
    var string = new String(Math.floor((dolFloat * 100))); 
    return string.substring(0, string.length - 2) +"." + string.substring(string.length - 2, string.length)
}
