/*
 * $Id:$
 *
 * This is a javascript file used for working with our item page, and supporting
 * adding items to the cart as either regular ship or multi-ship.
 */

$(document).ready(function() {

  if($('#add-to-cart').length == 0) {
    return;
  }

  var params = currentQueryParams();

  function singleShipSubmit() {
    $('#action-input').attr('name', 'add');
    $('#add-to-cart-form').submit();
  }

  function multiShipSubmit() {
    $('#action-input').attr('name', 'mshipadd');
    $('#add-to-cart-form').submit();
  }

  function show(elt, shouldAppend) {
    var root = $('#add-to-cart').get(0);
    if(shouldAppend) {
      root.appendChild(elt);
    } else {
      while(root.firstChild) {
        root.removeChild(root.firstChild);
      }
      root.appendChild(elt);
    }
  }


  function singleShip(qty, onClick) {
    var onClick = onClick ? onClick : singleShipSubmit;
    var clicker = function() {
      var qty = $('#qty-input').val();
      onClick(qty);
    };
    return $E('div', {},
              $E('div', {},
                 $E('span', {}, "Quantity: "),
                 $E('input', { className: 'qty', type: 'text', id: 'qty-input', name: 'qty', value: qty, size: 3 }),
                 " ",
                 $E('input',  { className: 'button', type: 'button', onclick: clicker,
                                value: 'Add to Cart'})));
  }

  function multiShip(qty, onClick) {
    var onClick = onClick ? onClick : multiShipSubmit;
    var clicker = function() {
      var qty = $('#qty-input').val();
      onClick(qty);
    }
    function row(index, name, qty) {
      var name = name ? name : '';
      var qty = qty ? qty : '';
      return $E('tr', {},
                $E('input', {type: 'hidden', name: 'multiname' + index, value: '!NewName!' }),
                $E('td', {}, $E('input', { size: '30', name: "multinewname" + index, value: name })),
                $E('td', {}, $E('input', { name: 'multiqty' + index,
                                           id: index == 0 ? 'qty-input' : 'qty-input' + index,
                                           size: '10', value: qty })));

    }
    return $E('div', {},
              $E('table', {},
                 $E('tbody', {},
                    $E('tr', {},
                       $E('th', {}, "Ship the book to:"),
                       $E('th', {}, "Quantity")),
                    row(0, "Myself", qty), row(1), row(2))),
              $E('input', {type: 'hidden', name: 'numrecipients', value: '3' }),
              $E('input',  { className: 'button', type: 'button',
                             onclick: clicker,
                             value: 'Add to Cart'}));

  }

  function chooseShipping(qty) { 
   return $E('span', {}, "Ship this order to multiple addresses? ",
              $E('input', {value: qty, name: 'qty', type: 'hidden' }),
              $E('input', {value: 'Yes', className: 'button', type: 'button',
                           onclick: function() {
                             show(multiShip(qty));
                           }}), " ",
              $E('input', {value: 'No', className: 'button', type: 'button',
                           onclick: singleShipSubmit}));
  }

  show(singleShip(1,
                  function(qty) {
                    if(qty == 0) {
                      alert("You must enter a quantity");
                    } else if(qty == 1) {
                      singleShipSubmit();
                    } else {
                      show(chooseShipping(qty));
                    }
                  }));

});

