// 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 (!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 checkEntryForm(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.home.value && !mainForm.work.value)
  {
    alert("Please enter at least one phone number.");
    mainForm.home.focus();
    return false;
  }

  // If a phone number is entered for home or work
  // 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.email.value)
  {
    if (!isValidEmail(mainForm.email.value)) {
      mainForm.email.focus();
      return false; }
  }

// mainForm.maritalstatus[0] - Married
// mainForm.maritalstatus[1] - Single
// mainForm.maritalstatus[2] - Other

  if(!mainForm.maritalstatus[0].checked && !mainForm.maritalstatus[1].checked && !mainForm.maritalstatus[2].checked)
  {
   alert("Please select your Marital Status.");
   mainForm.maritalstatus[0].focus();
   return false;
  }
  if(!mainForm.age[0].checked && !mainForm.age[1].checked && !mainForm.age[2].checked && !mainForm.age[3].checked)
  {
   alert("Please select your Age group.");
   mainForm.age[0].focus();
   return false;
  }

  if((mainForm.maritalstatus[0].checked) &&
     (!mainForm.spouseage[0].checked && !mainForm.spouseage[1].checked &&
      !mainForm.spouseage[2].checked && !mainForm.spouseage[3].checked))
  {
   alert("Please select your Spouse's Age group.");
   mainForm.spouseage[0].focus();
   return false;
  }
  if(!mainForm.income[0].checked && !mainForm.income[1].checked &&
     !mainForm.income[2].checked && !mainForm.income[3].checked &&
     !mainForm.income[4].checked)
  {
   alert("Please select your Income level.");
   mainForm.income[0].focus();
   return false;
  }
  if(!mainForm.ownership[0].checked && !mainForm.ownership[1].checked &&
     !mainForm.ownership[2].checked)
  {
   alert("Please select your Home ownership.");
   mainForm.ownership[0].focus();
   return false;
  }
  if(!mainForm.buysell[0].checked && !mainForm.buysell[1].checked)
  {
   alert("Please select if interested in buying or selling home.");
   mainForm.buysell[0].focus();
   return false;
  }

// Qualified if the following conditions are true
// 1. Married with one spouse 25-75 years old and $35k+
// 2. Single/Other, 35-75 years old and $40k+

  var bQual = false;
  if(mainForm.maritalstatus[0].checked)
  {
    if((mainForm.age[1].checked || mainForm.age[2].checked || 
        mainForm.spouseage[1].checked || mainForm.spouseage[2].checked) &&
        !mainForm.income[0].checked) bQual = true;
  }
  else
  {
    if(mainForm.age[2].checked && 
      (!mainForm.income[0].checked && !mainForm.income[1].checked))
    bQual = true;
  }

  if(bQual)
  {
    var retVal = window.confirm("You have qualified to receive a FREE TRIP FOR 2 ON CARNIVAL CRUISE OR TO SAN DIEGO, RENO, LAKE TAHOE, LAS VEGAS OR DISNEYLAND, including Round-trip Airfare and 2 Nights Accommodations.  Click OK to have a live person contact you with more information, or click CANCEL to continue submitting your entry.");

    if (retVal)
    {
      mainForm.qual.value = "true";
    }
    else
    {
      mainForm.qual.value = "false";
    }
  }
  else
  {
    mainForm.qual.value = "false";
  }

  return true;
}
