// inqv.js. /lib/
// Inquiry form validation
// (c) 2002-2008 Network Design Consulting
// http://www.NetDCon.com
// --------------------------------------------------------------------
//  1.0  12/14/2003  rmg  Working model adopted
//  1.1  04/01/2003  rmg  First overhaul
// --------------------------------------------------------------------

// Initialize form --------------------------------------------
function inqload() {
  document.inqe.name.focus();
  lastValid = 1
  return;
}

// Inquiry form submit ----------------------------------------
function vfINQE() {
  var qfma = document.inqe.qfma.value;
  // Verify nation ----------------------------
  if (!vfIDNA()) {
    if (qfma < 7) { document.inqe.idna.focus(); }
    return false; }
  // Verify name ------------------------------
  if (!vfNAME()) {
    if (qfma < 7) { document.inqe.name.focus(); }
    return false; }
  // Verify company ---------------------------
  if (!vfCORP()) {
    if (qfma < 7) { document.inqe.corp.focus(); }
    return false; }
  // Verify telephone -------------------------
  if (!vfTELV()) {
    if (qfma < 7) { document.inqe.telv.focus(); }
    return false; }
  // Verify email -----------------------------
  if (!vfMAIL()) {
    if (qfma < 7) { document.inqe.mail.focus(); }
    return false; }
  // Verify subject ---------------------------
  if (!vfSUBJ()) {
    if (qfma < 7) { document.inqe.subj.focus(); }
    return false; }
  return;
}

// Validate nation selection ----------------------------------
function vfIDNA() {
  if (document.inqe.idna.value == '') {
    dmesg("Please select your home country.");
    return false; }
  return true;  
}

// Validate name ----------------------------------------------
function vfNAME() {
  rgxCHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-'` ";
  var luNAME = document.inqe.name.value;          // get form input
  var strLEN = luNAME.length;                     // string index length
  // Strip invalid characters -----------------
  ckname = '';
  for (ndx = 0; ndx < strLEN; ndx++) {
    checkByte = luNAME.substr(ndx, 1);
      if (rgxCHAR.indexOf(checkByte) >= 0) { 
      ckname = ckname + checkByte; } }
  // Check name -------------------------------
  document.inqe.name.value = ckname;
  if (ckname.length < 3) {
    dmesg("Please enter your name.");
    return false; }                               // name must be at least 3 characters
  return true;
}

// Validate company -------------------------------------------
function vfCORP() {
  var luCORP = document.inqe.corp.value;          // get form input
  var strLEN = luCORP.length;                     // string index length
  if (strLEN < 2) {
    dmesg("Please enter your company name.");
    return false; }                               // company name must be at least 2 characters
  return true;
}

// Validate telephone -----------------------------------------
function vfTELV() {
  rgxCHAR = "0123456789()-.xX";
  var luTELV = document.inqe.telv.value;          // get form input
  var strLEN = luTELV.length;                     // string index length
  // Strip invalid characters -----------------
  ckname = '';
  for (ndx = 0; ndx < strLEN; ndx++) {
    checkByte = luTELV.substr(ndx, 1);
      if (rgxCHAR.indexOf(checkByte) >= 0) { 
      ckname = ckname + checkByte; } }
  // Check name -------------------------------
  document.inqe.telv.value = ckname;
  if (ckname.length < 10) {
    dmesg("Please enter your telephone number.");
    return false; }                               // number must be at least 10 characters
  return true;
}

// Validate email address -------------------------------------
function vfMAIL(resetVar) {
  var rgxCHAR = "abcdefghijklmnopqrstuvwxyz1234567890_-.";
  var luMAIL = document.inqe.mail.value;          // get form input
  var luMAIL = luMAIL.toLowerCase();              // force to all lower case
  var locSEP = luMAIL.indexOf('@');               // location of username sep symbol
  var strLEN = luMAIL.length;                     // string index length is from 0 to length-1
  var luunam = luMAIL.slice(0, locSEP);           // set username portion
  var ludoma = luMAIL.slice(locSEP + 1,  strLEN); // set domain portion
  // Check basics -----------------------------
  if (luMAIL.lastIndexOf('@') != locSEP) {
    dmesg("Please enter a valid email address.");
    return false; }                               // more than one arrobat
  if (ludoma.indexOf('.') < 0) {
    dmesg("Please enter a valid email address.");
    return false; }                               // no dot in domain name
  // Check username portion -------------------
  var ckunam = '';
  for (ndx = 0; ndx < luunam.length; ndx++) {
    checkByte = luunam.substr(ndx, 1);
    if (rgxCHAR.indexOf(checkByte) >= 0) {
      ckunam = ckunam + checkByte; } }
  if ((ckunam != luunam) || (luunam.length < 1)) {
    dmesg("Please enter a valid email address.");
    return false; }                               // illegal characters in username
  // Check domain name portion ----------------
  var ckdoma = '';
  for (ndx = 0; ndx < ludoma.length; ndx++) {
    checkByte = ludoma.substr(ndx, 1);
    if (rgxCHAR.indexOf(checkByte) >= 0) {
      ckdoma = ckdoma + checkByte; } }
  if ((ckdoma != ludoma) || (ludoma.length < 1)) {
    dmesg("Please enter a valid email address.");  
    return false; }                               // illegal characters in domain name
  return true;
}

// Validate subject -------------------------------------------
function vfSUBJ() {
  var luSUBJ = document.inqe.subj.value;          // get form input
  var strLEN = luSUBJ.length;                     // string index length
  if (strLEN < 2) {
    dmesg("Please enter the subject of your message.");
    return false; }                               // subject must be at least 2 characters
  return true;
}
