var global_valfield;	// retain valfield for timer thread

function setFocusDelayed() {
  global_valfield.focus();
}

function setfocus(valfield) {
  // save valfield in global variable so value retained when routine exits
  global_valfield = valfield;
  setTimeout( 'setFocusDelayed()', 100 );
}

function trim(str) {
  return str.replace(/^\s+|\s+$/g, '');
}

//document.getElementsByClassName workaround
function getElementsByClass(theForm, cl) {
  var retnode = new Array();
  var myclass = new RegExp('\\b'+cl+'\\b');
  var elem = theForm.getElementsByTagName('*');
  for (var i=0;i<elem.length;i++) {
    var classes = elem[i].className;
    if (myclass.test(classes)) retnode.push(elem[i]);
  }
  return retnode;
}

function validateArray(theForm, theClass) {
  var theArray=new Array();
  theArray=getElementsByClass(theForm, theClass);
  var total=0;
  for (var i=0;i<theArray.length;i++) {
    if (theArray[i].checked) total++;
  }
  if (total == 0) return false;
  return true;
}

function validateField(valfield) {
  if(valfield.value == "") {
    return false;
  }
  return true;
}

function validateEmail(valfield) {
  var valid=false;
  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
  if (!email.test(tfld)) {
    return false;
  }
  return true;
}

function validateSelect(valfield) {
  var selindex=valfield.selectedIndex;
  if(selindex == 0) {
    return false;
  }
  return true;
}

function validateButton(btn, desc) {
  var cnt = -1;
  for (var i=btn.length-1; i > -1; i--) {
    if (btn[i].checked) {cnt = i; i = -1;}
  }
  if (cnt > -1) {
    return true;
  }
  if(desc == 'offices') {
    alert("\nYou must select the office you work from");
    return false;
  } else if(desc == 'newdb') {
    alert("\nYou must select whether to add to or replace your existing database");
  }
}

function textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit) {
    // if too long...trim it!
    field.value = field.value.substring(0, maxlimit);
  } else {
    countfield.value = maxlimit - field.value.length;
  }
}

function validatePage(theForm) {
  var valid=false;
  var msg='';
  valid=validateField(theForm.ename);
  if (!valid) msg += "Your Name is required\n";
  valid=validateField(theForm.cname);
  if (!valid) msg += "Company Name is required\n";
  valid=validateEmail(theForm.email);
  if (!valid) msg += "Email Address is required\n";
  valid=validateField(theForm.phone);
  if (!valid) msg += "Phone Number is required\n";
  switch (theForm.name) {
    case "emfrm":
      valid=validateField(theForm.message);
      if (!valid) msg += "A Message is required\n";
      break;
    case "qtfrm":
      valid=validateField(theForm.projectTitle);
      if (!valid) msg += "Project Title is required\n";
      valid=validateSelect(theForm.product);
      if (!valid) msg += "Product is required\n";
      valid=validateField(theForm.trigger);
      if (!valid) msg += "Printing Due Date is required\n";
      valid=validateSelect(theForm.quantity);
      if (!valid) msg += "Quantity is required\n";
      // TODO: Add Flat/Final Size, Pages if required
      valid=validateSelect(theForm.paper);
      if (!valid) msg += "Paper Type is required\n";
      valid=validateSelect(theForm.fColorCnt);
      if (!valid) msg += "Number of Front Colors is required\n";
      if (theForm.dieCut.checked) {
        valid=validateField(theForm.dieCut_note);
        if (!valid) msg += "Description for Die Cutting Bindery is required\n";
      }
      if (theForm.dieScoring.checked) {
        valid=validateField(theForm.dieScoring_note);
        if (!valid) msg += "Description for Die Scoring Bindery is required\n";
      }
      if (theForm.scoring.checked) {
        valid=validateField(theForm.scoring_note);
        if (!valid) msg += "Description for Scoring Bindery is required\n";
      }
      if (theForm.drilling.checked) {
        valid=validateField(theForm.drilling_note);
        if (!valid) msg += "Description for Drilling Bindery is required\n";
      }
      if (theForm.perforate.checked) {
        valid=validateField(theForm.perforate_note);
        if (!valid) msg += "Description for Perforating Bindery is required\n";
      }
      if (theForm.gluing.checked) {
        valid=validateField(theForm.gluing_note);
        if (!valid) msg += "Description for Gluing Bindery is required\n";
      }
      if (theForm.folding.checked) {
        valid=validateField(theForm.folding_note);
        if (!valid) msg += "Description for Folding Bindery is required\n";
      }
      if (theForm.perfbound.checked) {
        valid=validateField(theForm.perfbound_note);
        if (!valid) msg += "Description for Perfect Bound Bindery is required\n";
      }
      if (theForm.saddle.checked) {
        valid=validateField(theForm.saddle_note);
        if (!valid) msg += "Description for Saddle Stitch Bindery is required\n";
      }
      if (theForm.other.checked) {
        valid=validateField(theForm.other_note);
        if (!valid) msg += "Description for Other Bindery is required\n";
      }
      valid=validateSelect(theForm.fileFormat);
      if (!valid) msg += "File Format is required\n";
      valid=validateField(theForm.state);
      if (!valid) msg += "Shipping State is required\n";
      valid=validateField(theForm.zip);
      if (!valid) msg += "Shipping Zip Code is required\n";
      break;
  }	
  if (!valid) alert(msg);
  return valid;
}