/* include common.js to get function emailtest(str) */

function iswhitespace(str)
{
  return (/^\s*$/.test(str));
}

function containsNonAlpha(str)
// designed to detect errors in People's names
{
  return (/[^-a-zA-Z' ]/.test(str));
}

// Audition Validation Script (altered)
// =====================================

// called when form submittted
function FrontPage_Form2_Validator(theForm)
{
  if (theForm.fname.value == "")
  {
    genericAlert("first name");
    theForm.fname.focus();
    return (false);
  }

  if (theForm.lname.value == "")
  {
    genericAlert("last name");
    theForm.lname.focus();
    return (false);
  }

  if (   theForm.parentname.value == ""
      || theForm.parentname.value.length < 1)
  {
    genericAlert("parent's name");
    theForm.parentname.focus();
    return (false);
  }

  if (theForm.addr.value == "")
  {
    genericAlert("address");
    theForm.addr.focus();
    return (false);
  }

  if (theForm.city.value == "")
  {
    genericAlert("city");
    theForm.city.focus();
    return (false);
  }

  if (theForm.state.selectedIndex < 0)
  {
    alert("Please select one of the state options.");
    theForm.state.focus();
    return (false);
  }

  if (theForm.state.selectedIndex == 0)
  {
    noFirstAlert("state");    
    theForm.state.focus();
    return (false);
  }

  if (theForm.zip.value == "")
  {
    genericAlert("zip code");
    theForm.zip.focus();
    return (false);
  }

  if (  theForm.zip.value.length < 5
     || !zip_xxxxx(theForm.zip.value))
  {
    alert("Please correct the zip code.");
    theForm.zip.focus();
    return (false);
  }
  

  //if (theForm.zip.value.length > 5)
  //{
  //  alert("Please enter at most 5 characters in the \"zip\" field.");
  //  theForm.zip.focus();
  //  return (false);
  //}

  if (theForm.LiveWithinThreeHourDrive.selectedIndex < 0)
  {
    alert("Please select one of the verification options.");
    theForm.LiveWithinThreeHourDrive.focus();
    return (false);
  }

  if (theForm.LiveWithinThreeHourDrive.selectedIndex == 0)
  {
    noFirstAlert("3 hour driving distance");
    theForm.LiveWithinThreeHourDrive.focus();
    return (false);
  }
  else if (theForm.LiveWithinThreeHourDrive.value == "No")
  {
    alert("We are unable to set up an audition. You should consider looking for an affiliated agency near you.");
    return (false);
  }

  if (theForm.phone.value == "")
  {
    genericAlert("phone number");
    theForm.phone.focus();
    return (false);
  }
  else if (theForm.phone.value.length < 12)
  {
    alert("Please enter at least 12 characters in the phone number.");
    theForm.phone.focus();
    return (false);
  }
  else if (!phone_xxx_xxx_xxxx(theForm.phone.value))
  {
    alert ("Please correct the phone number.")
    theForm.phone.focus();
    return (false);
  }
  
  if (theForm.email.value == "")
  {
    genericAlert("e-mail");
    theForm.email.focus();
    return (false);
  }
  else if (!emailtest(theForm.email.value))
  {
    alert("Please correct your e-mail address.");
    theForm.email.focus();
    return (false);
  }

  if (theForm.age.selectedIndex < 0)
  {
    alert("Please select an age.");
    theForm.age.focus();
    return (false);
  }

  if (theForm.age.selectedIndex == 0)
  {
    noFirstAlert("age");
    theForm.age.focus();
    return (false);
  }

  if (theForm.dob.value == "")
  {
    genericAlert("date of birth");
    theForm.dob.focus();
    return (false);
  }

  if (theForm.dob.value.length < 10)
  {
    alert("Please enter at least 10 characters for the birth date.");
    theForm.dob.focus();
    return (false);
  }

//  if (theForm.dob.value.length > 10)
//  {
//    alert("Please enter at most 10 characters in the \"dob\" field.");
//    theForm.dob.focus();
//    return (false);
//  }

  if (theForm.height.selectedIndex < 0)
  {
    alert("Please select a height.");
    theForm.height.focus();
    return (false);
  }

  if (theForm.height.selectedIndex == 0)
  {
    noFirstAlert("height");
    theForm.height.focus();
    return (false);
  }

  if (theForm.gender.selectedIndex < 0)
  {
    alert("Please select a gender.");
    theForm.gender.focus();
    return (false);
  }

  if (theForm.gender.selectedIndex == 0)
  {
    noFirstAlert("gender");
    theForm.gender.focus();
    return (false);
  }

//  if (theForm.note.value == "")
//  {
//    alert("Please enter a value for the \"note\" field.");
//    theForm.note.focus();
//    return (false);
//  }

//  if (theForm.note.value.length < 1)
//  {
//    alert("Please enter at least 1 characters in the \"note\" field.");
//    theForm.note.focus();
//    return (false);
// }

  if (theForm.note.value.length > 500)
  {
    alert("There is a 500 character limit for 'Question or Comment'.");
    theForm.note.focus();
    return (false);
  }
  
  if (  theForm.age.value < 18
     && (  containsNonAlpha(theForm.parentname.value)
        || iswhitespace(theForm.parentname.value)        
     )  )
  {
    alert("We need the name of a parent we can contact before setting up an audtion for someone under 18.");
    theForm.parentname.focus();
    return (false);
  }
  alert("It will take a minute to send your request.");
  return (true);
}
