
var itemCount = 0;
var totalPounds = 0;


function addItemToCart(fieldName, includeShipping)
{
    itemCount++

    var table = document.getElementById("googleTable");
    var tbody = table.getElementsByTagName("TBODY")[0];

   
    var form = document.getElementById("itemsForm");
    var name = eval("form."+fieldName+"Name.value");
    //alert("Name is : " + name);

    var quantity = eval("form."+fieldName+"Quantity.value");
    //alert("Quantity is : " + quantity);

    var weight = 0;
    if(includeShipping) {
        weight = parseInt(quantity);
        totalPounds=parseInt(totalPounds)+weight;
    }
    //alert("Total Weight : " + totalPounds);

    var description = eval("form."+fieldName+"Description.value");
    //alert("Description is : " + description);

    var price = eval("form."+fieldName+"Price.value");
    //alert("Price is : " + price);

    var totalPrice = quantity * price;
    //alert("Total Price is : " + totalPrice);

    calculateShipping();

    var index = tbody.rows.length + 1;

    var row = document.createElement("TR")
    row.id=index

    var td1 = document.createElement("TD")
    //Set the style attribute of the td
    //td1.className='style2'
    td1.appendChild(document.createTextNode(name));
    td1.appendChild(document.createElement("BR"));
    if(includeShipping) {
         td1.appendChild(document.createTextNode(quantity + ' lbs. ' + description + ' $' + totalPrice));
    } else {
        td1.appendChild(document.createTextNode(quantity + ' ' + description + ' $' + totalPrice));
    }

    var itemName = document.createElement("INPUT")
    itemName.type='hidden'
    itemName.name='item_name_' + itemCount
    itemName.value=name;
    td1.appendChild(itemName);

    var itemDescription = document.createElement("INPUT")
    itemDescription.type='hidden'
    itemDescription.name='item_description_' + itemCount
    itemDescription.value=description;
    td1.appendChild(itemDescription);

    var itemQuantity = document.createElement("INPUT")
    itemQuantity.type='hidden'
    itemQuantity.name='item_quantity_' + itemCount
    itemQuantity.value=quantity;
    td1.appendChild(itemQuantity);

    var itemPrice = document.createElement("INPUT")
    itemPrice.type='hidden'
    itemPrice.name='item_price_' + itemCount
    itemPrice.value=price;
    td1.appendChild(itemPrice);
	
    if (fieldName == 'GiftCard') {
        var itemTax = document.createElement("INPUT");
        itemTax.type = 'hidden';
        itemTax.name='shopping-cart.items.item-'+itemCount+'.tax-table-selector';
        itemTax.value = 'tax_exempt';
        td1.appendChild(itemTax);
    }

    var removeBox = document.createElement("INPUT")
    removeBox.type='checkbox'
    removeBox.name=index;
    removeBox.value=weight;

    var td2 = document.createElement("TD")
    td2.appendChild (removeBox)

    row.appendChild(td1);
    row.appendChild(td2);
    tbody.appendChild(row);

}

function calculateShipping() {
    var shipping;
    if(totalPounds>=10) {
        shipping = parseFloat(13.95);
    } else if (totalPounds >= 6) {
        shipping = parseFloat(10.95);
    } else if (totalPounds >= 2) {
        shipping = parseFloat(8.95);
    } else if (totalPounds == 1) {
        shipping = parseFloat(5.95);
    } else {
        shipping = parseFloat(0);
    }
    //alert("Shipping cost : $" + shipping);
    var googleForm = document.getElementById("googleForm");
    googleForm.ship_method_price_1.value=shipping;
}

function removeItemsFromCart()
{
  var form = document.getElementById("googleForm");
  for(var i=0;i<form.elements.length;i++) {
        var element = form.elements[i]
        if(element.type=='checkbox' && element.checked) {
            var index = element.name
            //alert('Need to remove ' + index);
            
            var weight=element.value;
            //alert("Item wight to be removed : " + weight);

            totalPounds = totalPounds-parseInt(weight);
            calculateShipping();

            //Remove from table
            var tbody = document.getElementById("googleTableBody");
            //alert("Google Table has " + tbody.rows.length + " rows");
            for(var i=0;i<tbody.rows.length;i++) {
                var row = tbody.rows[i];
                if(row.id == element.name) {
                    tbody.deleteRow(row.rowIndex);
                }
            }
            
        }
    }
}

function displayForm(form)
{
    for(var i=0;i<form.elements.length;i++) {
        var element = form.elements[i]
            alert(element.name + '=' + element.value);
    }
}


