  function selectText( ev ) {
    this.select();
    this.focus();
  }
  function enableForm( ev ) {
    var f = $(this.form);
    f.find('td input')
      .removeAttr('readonly')
      .removeAttr('disabled')
      .unbind('click', enableForm);
  }
  function updateForm( form ) {
    var f = $(form);
    var total = 0;
    f.find('input[id^=qty]').each(function () {
      var qty = Math.ceil(this.value);
      if (this.value != qty) {
        this.value= qty;
      }
      var type = this.id.substr(3);
      var price = $('th#price'+type).text().replace(/[^0-9.]/g, '');
      total += qty * price;
    });
    var cents = (total == 0) ? "00" : new String(Math.round(total * 100));
    total = Math.floor(total)+"."+cents.substr(cents.length-2);
    f.find('#total').val(total);
  }
  function updateFormEvent( ev ) {
    updateForm(this.form);
  }
  function updateCart( form ) {
    updateForm(form);
    var f = $(form);
    $.post(form.action, f.serialize(), null, 'script');
    return false;
  }
  function showFabricDetails( id, reload ) {
    $.get('/Fabrics.detail/'+id, null, function(html, textStatus) {
      var h = $(html);
      h.find('a.flyout').flyout({ loaderZIndex: 1500 });
      var d = h.dialog({
        title: 'Fabric Selection',
        closeOnEscape: true,
        position: 'center',
        close: function(event, ui) {
          $(this).dialog('destroy');
          $(this).remove();
          if (reload) {
            window.location.reload();
          }
        },
        modal: true
      });
      d.find('form').data('dialog', d)
        .find('input:text')
        //.click(enableForm)
        .click(selectText)
        .focus(updateFormEvent)
        ;
    }, 'html');
    return false;
  }
  function showBookDetails( id, reload ) {
    $.get('/Books.detail/'+id, null, function(html, textStatus) {
      var h = $(html);
      h.find('a.flyout').flyout({ loaderZIndex: 1500 });
      var d = h.dialog({
        title: 'Book Selection',
        closeOnEscape: true,
        position: 'center',
        close: function(event, ui) {
          $(this).dialog('destroy');
          $(this).remove();
          if (reload) {
            window.location.reload();
          }
        },
        modal: true
      });
      d.find('form').data('dialog', d)
        .find('input')
        //.click(enableForm)
        .click(selectText)
        .focus(updateFormEvent)
        ;
    }, 'html');
    return false;
  }

