﻿$(document).ready(function() {
  // the only text boxes allowed are for entering card qty's.
  $('input[type=text].itemqty').each(function() {

    this.onchange = function(event) {
      calcTotal();
    };
  });

  $('input[type=text].itemqty').bind('keypress', function(e) {
    var c = e.keyCode;
    var allowed = '0123456789';
    if (e.keyCode < 32)
      return true;
    else if (allowed.indexOf(String.fromCharCode(c)) != -1) 
      return true;
    else
      return false;
  });

  $('input[type=text].itemqty').bind('keydown', function(e) {
    if (e.keyCode == 13) {
      thisID = this.id;
      sendFocus(event, thisID)
    }
  });

  $('a.itemlink').bind('mousedown', function(event) {
    thisID = this.id;
    openItem(event, thisID)
  });

  $('input[type=button]#checkout').bind('click', function(event) {
    thisID = this.id;
    switch (thisID) {
      case 'checkout':
        calcTotal()
        if (grandtotal == 0)
          alert("You haven't ordered anything");
        else {
          setItemsOrdered();
          $('form#form1').submit();
        }
      default:
        break;
    }
  });

  $('input[type=button]#btnCancel2').bind('click', function(event) {
    thisID = this.id;
    if (confirm('Are You Sure?')) {
      $('input[type=button]#btnCancel').click();
    }
    else {
      event.preventDefault();
      return false;
    }
  }
    );
  calcTotal();
}
);

function sendFocus(event, thisID) {
  var x;
  inputs = $("input[type=text].itemqty");
  x = inputs.length;
  for (n = 0; n < x; n++) {
    if (inputs[n].id == thisID) {
      if (n < (x - 1)) {
        inputs[n + 1].focus();
        inputs[n + 1].select();
      }
      else {
        inputs[0].focus();
        inputs[0].select();
      }
      break;
    }
  }
}

function sendFocusImg(event, thisID) {
  var x;
  itemNbr = thisID.substr(-3);
  inputName = 'itemQty' + itemNbr;
  inputs = $("input[type=text].itemqty");
  x = inputs.length;
  for (n = 0; n < x; n++) {
    if (inputs[n].id == inputName) {
      //alert('hello focusimg');
      inputs[n + 1].focus();
      inputs[n + 1].select();
      break;
    }
  }
}


function openItem(event, thisID) {
  nbr = thisID.slice(-3);
  // create the dialog object
  $('#dlg' + nbr).dialog({ autoOpen: false,
    title: 'Item Detail',
    closeOnEscape: true,
    height: 580, width: 500,
    modal: true,
    buttons: { "Close": function() { $(this).dialog("close"); } }
  });
  $('#dlg' + nbr).dialog('open');
  // stop events from propagating
  //  event.preventDefault();
  //  return false; 
}

function setItemsOrdered() {
  var items = "";
  var qtys = "";

  $('input[type=text].itemqty').each(function() {
    nbr = this.id.slice(-3);
    qty = $(':text#itemQty' + nbr).val();
    if ((qty > 0) && !isNaN(nbr) && !isNaN(qty)) {
      items += (nbr + ',');
      qtys += (qty + ',');
    }
  }
    );
  $('input#txtItemsOrdered').val(items);
  $('input#txtQtysOrdered').val(qtys);
}

function calcTotal() {
  // update summary numbers 
  grandtotal = 0;
  $('input[type=text].itemqty').each(function() {
    nbr = this.id.slice(-3);
    qty = $(':text#itemQty' + nbr).val();
    if (qty > 0) {
      price = qty * parseFloat($('span#itemPrice' + nbr).text().slice(1));
      $('span#itemTotal' + nbr).text('$' + formatCurrency(price))
      grandtotal += price;
    }
    else
      $('span#itemTotal' + nbr).text('$' + formatCurrency(0))
  }
    );

  $('span#orderTotal').text('$' + formatCurrency(grandtotal));
}

function formatCurrency(num) {
  num = isNaN(num) || num === '' || num === null ? 0.00 : num;
  return parseFloat(num).toFixed(2);
}

function formatThree(num) {
  var s = "";
  if (num < 10)
    s = "00" + num.toString();
  else if (num < 100)
    s = "0" + num.toString();
  else
    s = num.toString();
  alert('num:' + num + ', s:' + s)
  return s

}  
