function Validator(theForm) {
  if (theForm.SHIP_TO_COMP.value == "") {
    alert("Please enter your name or the name of the business.");
    theForm.SHIP_TO_COMP.focus();
    return (false);
  }
  if (theForm.SHIP_TO_CONTACT1.value == "") {
    alert("Please enter a Contact Name.");
    theForm.SHIP_TO_CONTACT1.focus();
    return (false);
  }
  if (theForm.SHIP_TO_PHONE1.value.length < 10) {
    alert("Please enter a valid Phone Number including area code.");
    theForm.SHIP_TO_PHONE1.focus();
    return (false);
  }
  if (theForm.SHIP_TO_EMAIL1.value.length < 6) {
    alert("Please enter a valid Email Address.");
    theForm.SHIP_TO_EMAIL1.focus();
    return (false);
  }
  if (theForm.SHIP_TO_ADDR_1.value == "") {
    alert("Please enter your Address.");
    theForm.SHIP_TO_ADDR_1.focus();
    return (false);
  }
  if (theForm.SHIP_TO_CITY.value == "") {
    alert("Please enter your City.");
    theForm.SHIP_TO_CITY.focus();
    return (false);
  }
  if (theForm.SHIP_TO_STATE.value == "") {
    alert("Please select your State.");
    theForm.SHIP_TO_STATE.focus();
    return (false);
  }
  if (theForm.SHIP_TO_ZIP.value == "") {
    alert("Please enter your Zip Code.");
    theForm.SHIP_TO_ZIP.focus();
    return (false);
  }
//Validate serial numbers here
  return (true);
}

function CommaFormatted(amount) {
  var delimiter = ",";
  var a = amount.split('.',2)
  var d = a[1];
  var i = parseInt(a[0]);
  if(isNaN(i)) { 
    return '';
  }
  var minus = '';
  if(i < 0) {
    minus = '-';
  }
  i = Math.abs(i);
  var n = new String(i);
  var a = [];
  while(n.length > 3) {
    var nn = n.substr(n.length-3);
    a.unshift(nn);
    n = n.substr(0,n.length-3);
  }
  if(n.length > 0) {
    a.unshift(n);
  }
  n = a.join(delimiter);
  if(d.length < 1) {
    amount = n;
  } else {
    amount = n + '.' + d;
  }
  amount = minus + amount;
  return amount;
}
function addSerial() {
  var tbl = document.getElementById('SerialTable');
  var priceCell = document.getElementById('SerialPrice');
  var totalCell = document.getElementById('SerialTotal');
  var lastRow = tbl.rows.length;
  var SerialCount = lastRow - 1;
  var row = tbl.insertRow(lastRow - 1);

  // left cell
  var cellLeft = row.insertCell(0);
  cellLeft.setAttribute('align','right');
  cellLeft.setAttribute('class','subfeature');
  var textNode = document.createTextNode(SerialCount);
  cellLeft.appendChild(textNode);

  // Middle cell
  var cellMiddle = row.insertCell(1);
  cellMiddle.setAttribute('align','right');
  cellMiddle.setAttribute('class','subfeature');
  var el = document.createElement('input');
  el.type = 'text';
  el.size = 9;
  el.name = 'serial';
  el.id = 'Serial' + SerialCount;
  el.setAttribute('style','text-align: center');
  el.maxLength = 10;
  cellMiddle.appendChild(el);

  // Right cell
  var cellRight = row.insertCell(2);
  cellRight.setAttribute('align','right');
  cellRight.setAttribute('class','subfeature');
  var textNode = document.createTextNode(priceCell.innerHTML);
  cellRight.appendChild(textNode);

  // Update total
  var NewTotal = priceCell.innerHTML.substring(1) * SerialCount;
  NewTotal = "" + NewTotal.toFixed(2);
  totalCell.innerHTML = '$' + CommaFormatted( NewTotal );

  // Set focus to new field
  // Serial???.focus();
}
function removeSerial() {
  var tbl = document.getElementById('SerialTable');
  var priceCell = document.getElementById('SerialPrice');
  var totalCell = document.getElementById('SerialTotal');
  var lastRow = tbl.rows.length;
  var SerialCount = lastRow - 2;
  if (SerialCount < 2) return;
  tbl.deleteRow(lastRow - 2);

  // Update total
  var NewTotal = priceCell.innerHTML.substring(1) * (SerialCount - 1);
  NewTotal = "" + NewTotal.toFixed(2);
  totalCell.innerHTML = '$' + CommaFormatted( NewTotal );
}

