
function trim(s) {
  return s.replace(/^\s*|\s*$/g,"");
}


function validateOrder() {
  
  // check that spelling book quantity is greater than 10
  var spellBookQ = document.getElementById('productid1').value;
  if (spellBookQ > 0 && spellBookQ < 10) {
    alert("You must order at least a minimum of 10 I'm Spelling Now! books");
    return false;   
  }
/*
  var workbookQ = document.getElementById('productid3').value;
  if (workbookQ > 0 && workbookQ > 30) {
    alert("Please contact us directly to order more than 30 workbooks.");
    return false;   
  }
*/
  // check that conversation workbook quantity is greater than 10
  var convWBookQ = document.getElementById('productid9').value;
  if (convWBookQ > 0 && convWBookQ < 10) {
    alert("You must order at least a minimum of 10 Conversation Peace Workbooks");
    return false;   
  }

  // loop through all products
  numItems = 0;
  for (i=0; i<numProducts; i++) {
    x = document.getElementById('productid'+i);
    if (!x) continue;
    q = x.value;
    q = trim(q);
    if (q != '') {
      // check that quantity is a valid non-zero integer
      if (q.search(/^[1-9][0-9]*$/) == -1) {
        alert("Invalid quantity specified (" + q + ")");
        return false;
      }
      numItems += 1;
    }
  }
  // at least one item must have been ordered
  if (numItems == 0) {
    alert("You must order at least one item");
    return false;
  }
  // fname, lname, address1, city, country must be specified
  req = new Array('fname', 'lname', 'address1', 'city', 'zip');
  for (i=0; i<req.length; i++) {
    field = document.getElementById(req[i]).value;
    field = trim(field);
    if (field == '') {
      alert("Please enter your complete name and shipping address");
      return false;
    }
  }

  zip = trim(document.getElementById('zip').value);
  if (zip.search(/^V[0-9][A-Z] ?[0-9][A-Z][0-9]$/i) == -1) {
    alert("Invalid BC postal code");
    return false;
  }

  return true;
}
