/* handling for shopping cart */
/*
requires: preceding include of getCartContents.php or orderStatus.php:

  <script language="JavaScript" type="text/javascript"
          src="../php/getCartContents.php"></script>

usage:

1. onload="init(allowEdit);"

2. table with id "orderTable" and a header like:
	 <tr>
	  <th></th>                 <-- only if allowEdit
	  <th>Product</th>
	  <th>Size</th>
	  <th>Color</th>
	  <th>Quantity</th>
	  <th></th>                 <-- only if allowEdit
	  <th>Unit Price</th>
	  <th>Total</th>
	 </tr>

3. To support order update, table must be wrapped in a form with id "updateCart" and posted to "../php/updateCart.php". It must
   also contain a field with name & id of 'cmd':

   <form method="POST" action="../php/updateCart.php" id="updateCart">
    <input type="hidden" id="cmd" name="cmd" value="update"/>
 
4. To support checkout, checkout() must be wired up somewhere.
*/

// add current cart contents to orderTable
function init(allowEdit) {
  var row;
  var table = document.getElementById("orderTable");
  var data;
  var itemsInCart = false;
  for(idx in g_cartContents) {
      itemsInCart = true;
      row = table.insertRow(-1);
      var data = g_cartContents[idx];
      if(allowEdit) {
	      cell = row.insertCell(-1);
		  cell.innerHTML = "<input type='submit' value='Remove' onclick='removeItem(" + idx + ");'/>";
		  cell.align = "center";
	  }
	  cell = row.insertCell(-1);
	  cell.innerHTML = data.description;
	  cell = row.insertCell(-1);
	  if(data.size == "N/A") {
	  	  cell.innerHTML = data.style;
	  } else {
		  cell.innerHTML = data.size + " - " + data.style;
	  }
	  cell = row.insertCell(-1);
	  cell.innerHTML = data.color;
	  cell = row.insertCell(-1);
	  if(allowEdit) {
		  variantIdField = "<input type='hidden' name='variantId[" + idx + "]' value='" + data.variantId + "' />";
		  sizeField      = "<input type='hidden' name='size["      + idx + "]' value='" + data.size      + "' />";
		  quantityField  = "<input type='text'   name='qty["       + idx + "]' value='" + data.quantity  + "' id='qty" + idx + "' + style='text-align: right;'/>";
		  cell.innerHTML = variantIdField + sizeField + quantityField;
	  } else {
	  	  cell.innerHTML = data.quantity;
	  }
	  if(allowEdit) {
		  cell = row.insertCell(-1);
		  cell.innerHTML = "<input type='submit' value='Update' onclick='updateCart();'/>";
	      cell.align = "center";
	  }
	  cell = row.insertCell(-1);
	  if(allowEdit) {
		  priceField = "<input type='hidden' name='price[" + idx + "]' value='" + data.price + "' />";
		  cell.innerHTML = priceField;
	  } else {
		  cell.innerHTML = data.price;
	  }
	  cell.align = "right";
	  cell = row.insertCell(-1);
	  cell.innerHTML = data.extPrice;
	  cell.align = "right";
  }
  if(itemsInCart) {
      var span = allowEdit ? 7 : 5;
      // subtotal
	  row = table.insertRow(-1);
	  cell = row.insertCell(-1);
	  cell.colSpan = span;
	  cell.innerHTML = "<div style='font-weight: bold;'>Subtotal</div>";
	  cell.align = "right";
	  cell = row.insertCell(-1);
	  cell.innerHTML = g_subTotal;
	  cell.align = "right";
      // shipping
	  row = table.insertRow(-1);
	  cell = row.insertCell(-1);
	  cell.colSpan = span;
	  cell.innerHTML = "<div style='font-weight: bold;'>Shipping</div>";
	  cell.align = "right";
	  cell = row.insertCell(-1);
	  cell.innerHTML = g_shipping;
	  cell.align = "right";
      // Total
	  row = table.insertRow(-1);
	  cell = row.insertCell(-1);
	  cell.colSpan = span;
	  cell.innerHTML = "<div style='font-weight: bold;'>Total</div>";
	  cell.align = "right";
	  cell = row.insertCell(-1);
	  cell.innerHTML = g_total;
	  cell.align = "right";
  } else {
	  row = table.insertRow(-1);
	  cell = row.insertCell(-1);
	  cell.colSpan = 8;
	  cell.innerHTML = "<div style='font-weight: bold;'>No items in cart</div>";
	  cell.align = "center";  
  }
  if(g_message.length > 0) {
  	alert(g_message);
  }
}
 
 // update cart
function updateCart() {
     var form = document.getElementById("updateCart");
     normalize(form);
     var cmd = document.getElementById("cmd");
     cmd.value = "update";
     form.submit();
}
 
//  remote item from cart
function removeItem(i) {
 	qtyInput = document.getElementById("qty" + i);
 	qtyInput.value = 0;
 	updateCart();
}
 
// on to checkout
function checkout() {
    var form = document.getElementById("updateCart");
    normalize(form);
    var cmd = document.getElementById("cmd");
    cmd.value = "checkout";
    form.submit();
}

// sanity check
function normalize(form) {
	var i = 0;
    while(qtyInput = document.getElementById("qty" + i++)) {
    	qtyInput.value = Math.floor(qtyInput.value);
    	if(qtyInput.value < 0) {
    		qtyInput.value = 0;
    	}
    }
}

// populate order history table
function showOrderHistory() {
	var orderIdElem = document.getElementById("orderId");
	orderIdElem.innerHTML = g_orderId;

  var row;
  var table = document.getElementById("orderHistory");
  var data;
  for(idx in g_orderHistory) {
      row = table.insertRow(-1);
      var data = g_orderHistory[idx];
	  cell = row.insertCell(-1);
	  cell.innerHTML = data.date;
	  cell = row.insertCell(-1);
	  cell.innerHTML = data.action;
  }
}
