// isValidName checks for the following conditions:
//   - has anything been entered for the name
//   - does the name only contain spaces
//   - are the characters valid
//
function isValidName(str)
{
  var NameRE = /^[a-z`\'-. ]+$/i;
  var allSpaces = true;

  for (i=0; i<str.length; i++)
  {
    if (str.charAt(i) != ' ')	// does str contain only spaces?
      allSpaces = false;
    if (!NameRE.test(str))	// does str match reg expr?
    {
      alert ("Invalid characters in name.");
      return false;
    }
  }

  if (allSpaces) { return false }

  return true;
}

// isValidNumber checks that str contains only numeric values
//
function isValidNumber(str)
{
  var NumRE = /^[0-9]+$/;

  /*
  if (!(str))
  {
    alert("Please enter a number.");
    return false;
  }
  */

  if (!NumRE.test(str))		// does str match reg expr?
  {
    alert ("Please enter only numbers.");
    return false;
  }
  else
    return true;
}

// isValidPhNum and isValidWkPh checks that the phone number
// contains only numbers.  The phone can be entered as all
// numbers or separated by dashes.  The work extension can
// only be numbers.
//
// There is no 0 in the first digit of the area code.
// The prefix can only be 3 digits in length, followed by
// a 4 digit number.
//
function isValidPhNum(str)
{
  var PhNum = /^[1-9]{1}[\d]{2}[-]?[\d]{3}[-]?[\d]{4}$/;

  if (!PhNum.test(str))
  {
    alert ("Invalid format for phone number.");
    return false;
  }

  return true;
}

function isValidWkPh(str1, str2)
{
  var PhNum = /^[1-9]{1}[\d]{2}[-]?[\d]{3}[-]?[\d]{4}$/;
  var ExtNum = /^[\d]*$/;

  if (!PhNum.test(str1))
  {
    alert ("Invalid format for work number.");
    return false;
  }
  if (!ExtNum.test(str2))
  {
    alert ("Please enter only numbers for extension.");
    return false;
  }

  return true;
}

// isValidEmail does not check if the address actually exists,
// it merely makes sure that the address has the correct syntax
// and could exist.
//
// The address should start with a bunch of alphanumerical
// (letters or numbers), underscores, dots or dashes. This is
// the user name.
//
// Followed by the @ sign.
//
// After that comes the domain name, which may include several
// sub-domains (mail.international.company). Therefore we now
// allow several series of alphanumerical characters and dashes,
// followed by a dot.
//
// Finally the top level domain, once again we check for
// alphanumerical characters, but now without the dash. Also,
// a top level domain name must be between 2 and 4 characters.
// Digits are included in the top level domain because IP
// addresses are also valid domains.
//
function isValidEmail(str)
{
  var eAddr = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/i;

  if (!(str))
  {
    alert("Please enter your email address.");
    return false;
  }

  if (!eAddr.test(str))
  {
    alert ("Invalid format for email address.");
    return false;
  }

  return true;
}

function formCheck(mainForm)
{
  if (!isValidName(mainForm.first.value))
  {
    alert("Please enter your first name.");
    mainForm.first.focus();
    return false;
  }
  if (!isValidName(mainForm.last.value))
  {
    alert("Please enter your last name.");
    mainForm.last.focus();
    return false;
  }
  if (mainForm.zip.value == "")
  {
    alert("Please enter your zip code.");
    mainForm.zip.focus();
    return false;
  }
  if (!isValidNumber(mainForm.zip.value))
  {
    mainForm.zip.focus();
    return false;
  }
  if (!mainForm.home.value && !mainForm.work.value && !mainForm.cell.value)
  {
    alert("Please enter at least one phone number.");
    mainForm.home.focus();
    return false;
  }
 
  // If a phone number is entered for home, work, or cell,
  // or an email address is entered, check that is has the
  // correct format

  if (mainForm.home.value)
  {
    if (!isValidPhNum(mainForm.home.value)) {
      mainForm.home.focus();
      return false; }
  }
  if (mainForm.work.value)
  {
    if (!isValidWkPh(mainForm.work.value, mainForm.ext.value)) {
      mainForm.work.focus();
      return false; }
  }
  if (mainForm.cell.value)
  {
    if (!isValidPhNum(mainForm.cell.value)) {
      mainForm.cell.focus();
      return false; }
  }

  if (mainForm.email.value)
  {
    if (!isValidEmail(mainForm.email.value)) {
      mainForm.email.focus();
      return false; }
  }
  if (mainForm.year.value == "")
  {
    alert("Please enter the year you were born.");
    mainForm.year.focus();
    return false;
  }

  // Checks to make sure something was selected for job location
  jobsite = false;
  for (i=0 ; i<mainForm.site.length ; i++)
  {
    if (mainForm.site[i].checked)
    {
      jobsite = true;
      break;
    }
  }
  if (jobsite == false)
  {
    alert("Please select your preferred working location.");
    mainForm.site[0].focus();
    return false;
  }

  if (!mainForm.tix.checked && !mainForm.com.checked &&
      !mainForm.photo.checked && !mainForm.merch.checked &&
      !mainForm.build.checked && !mainForm.design.checked &&
      !mainForm.light.checked && !mainForm.act.checked &&
      !mainForm.makeup.checked && !mainForm.wardrobe.checked &&
      !mainForm.efx.checked && !mainForm.sec.checked &&
      !mainForm.staff.checked && !mainForm.tech.checked )
  {
    alert("Please select at least one area of interest.");
    mainForm.tix.focus();
    return false;
  }
}

