// When the document is finished loading:
//  - populate the order form with the items from the menu
//  - register
$(function() {
  populateFormContentFromMenu();
  $("#group-ordering form").bind('keyup', calculateTotals);
  $("#group-ordering form").bind('submit', submitForm);
//  $("#group-ordering form").show();
  calculateTotals();
});


SALES_TAX = 0.09;
function calculateTotals() {

  // Calculate the line total for each row in the table.
  // Add them up to compute the subtotal.
  var subtotal = 0;
  var rows = $("#group-ordering form tr");
  rows.each(function(i, element) {
    var row = $(element);
    var price    = parseFloat(row.find('.price').text())   || 0;
    var quantity = parseFloat(row.find('.quantity').val()) || 0;
    var lineTotal = price * quantity;
    subtotal += lineTotal;

    row.find('.line-total').val(lineTotal.toFixed(2));
  });

  // Compute the sales tax and total
  var tax = subtotal * SALES_TAX;
  var total = subtotal + tax;
  $("input.subtotal").val(subtotal.toFixed(2));
  $("input.tax").val(tax.toFixed(2));
  $("input.total").val(total.toFixed(2))
}


function clearForm() {
  $("#group-ordering form .quantity").val(0);
  $("#group-ordering form").keyup();
}


function submitForm() {
  clearForm();
  return false;
}


function populateFormContentFromMenu() {

  // Get all of the foods from this week's menu, and their prices.
  // Drinks are hard coded into the form, because we don't deliver chai.
  var curryNames = [], curryPrices = [],
      sideNames = [], sidePrices = [],
      dessertNames = [], dessertPrices = [];
  $('#menu #curries dt').each(function(i, element) {
    var text = $(element).text();
    var priceText = $(element).find(".price").text();
    curryNames[i] = text.replace(priceText, '');
    curryPrices[i] = parseFloat(priceText).toFixed(2);
  });
  $('#menu #sides dt').each(function(i, element) {
    var text = $(element).text();
    var priceText = $(element).find(".price").text();
    sideNames[i] = text.replace(priceText, '');
    sidePrices[i] = parseFloat(priceText).toFixed(2);
  });
  $('#menu #desserts dt').each(function(i, element) {
    var text = $(element).text();
    var priceText = $(element).find(".price").text();
    dessertNames[i] = text.replace(priceText, '');
    dessertPrices[i] = parseFloat(priceText).toFixed(2);
  });

  // Find the sections of the form where the curries, sides and desserts go.
  var form = $("#group-ordering form");
  var table = form.find('table');
  var templateRow = table.find("tr.template");

  // Make a row in the form for each curry, side, and dessert.
  var i, n, row, previousRow;
  previousRow = table.find(".curries.subheader");
  for (i = 0, n = curryNames.length; i < n; i++) {
    row = templateRow.clone().removeClass('template');
    row.find('.name').text(curryNames[i]);
    row.find('.price').text(curryPrices[i]);
    row.insertAfter(previousRow);
    previousRow = row;
  }
  previousRow = table.find(".sides.subheader");
  for (i = 0, n = sideNames.length; i < n; i++) {
    row = templateRow.clone().removeClass('template');
    row.find('.name').text(sideNames[i]);
    row.find('.price').text(sidePrices[i]);
    row.insertAfter(previousRow);
    previousRow = row;
  }
  previousRow = table.find(".desserts.subheader");
  if (dessertNames.length === 0) previousRow.hide();
  for (i = 0, n = dessertNames.length; i < n; i++) {
    row = templateRow.clone().removeClass('template');
    row.find('.name').text(dessertNames[i]);
    row.find('.price').text(dessertPrices[i]);
    row.insertAfter(previousRow);
    previousRow = row;
  }

  // set the tabindices so that you can tab through the form
  var tabindex = 1;
  table.find(".quantity").each(function(i, element) {
    $(element).attr('tabindex', tabindex++);
  });
  form.find('input#combine-containers').attr('tabindex', tabindex++);
  form.find('textarea#additional-information').attr('tabindex', tabindex++);
  form.find('input[type="submit"]').attr('tabindex', tabindex++);
}

