﻿//This script runs email, age (13 and over) and zipcode validations

function formVal()
{

/*
	var radiovis, radioSeleted;
	//radioSelected = false;
  for (i = 0;  i < document.theForm.Q77_IMPROVE_VISION.length;  i++)
  {
    if (document.theForm.Q77_IMPROVE_VISION[i].checked)
   		//radioSelected = true;
        radiovis = document.theForm.Q77_IMPROVE_VISION[i].value;
        document.theForm.Q77_IMPROVE_VISION[i].value = radiovis;
  }

  if (!radioSelected)
  {
          radiovis = "99";
          document.theForm.Q77_IMPROVE_VISION.value = radiovis;
          //alert("document.theForm.Q77_IMPROVE_VISION.value = " + document.theForm.Q77_IMPROVE_VISION.value);
          radiovis = document.theForm.Q77_IMPROVE_VISION.value;
          document.theForm.Q77_IMPROVE_VISION.value = radiovis;          
}
    
  	var radiovis2;
  for (i = 0;  i < document.theForm.OPT_IN_STATEMENT.length;  i++)
  {
    if (document.theForm.OPT_IN_STATEMENT[i].checked)
    {
        radiovis2 = document.theForm.OPT_IN_STATEMENT[i].value;
        document.theForm.OPT_IN_STATEMENT[i].value = radiovis2;
    }     
 
  }
 */

//FIRST NAME
 if (document.theForm.FIRST_NAME.value == "")
  {
    alert("Please enter your first name.");
    //document.theForm.FIRST_NAME.style.backgroundColor = '#FFFFCC';
    document.theForm.FIRST_NAME.focus();
    return (false);
  }

  if (document.theForm.FIRST_NAME.value.length < 1)
  {
    alert("Please enter your first name.");
    //document.theForm.FIRST_NAME.style.backgroundColor = '#FFFFCC';    
    document.theForm.FIRST_NAME.focus();
    return (false);
  }

  if (document.theForm.FIRST_NAME.value.length > 50)
  {
    alert("Please enter at most 50 characters in the first name field.");
    //document.theForm.FIRST_NAME.style.backgroundColor = '#FFFFCC';
    document.theForm.FIRST_NAME.focus();
    return (false);
  }

  if (document.theForm.FIRST_NAME.value.indexOf(".", 0) >= 0)
 {
    alert("Please do not enter a period in the first name field.");
    //document.theForm.FIRST_NAME.style.backgroundColor = '#FFFFCC';
    document.theForm.FIRST_NAME.focus();
    return (false);
 }

  if (document.theForm.FIRST_NAME.value.indexOf(",", 0) >= 0)
 {
    alert("Please do not enter a comma in the first name field.");
   // document.theForm.FIRST_NAME.style.backgroundColor = '#FFFFCC';
    document.theForm.FIRST_NAME.focus();
    return (false);
 }

//LAST NAME
  if (document.theForm.LAST_NAME.value == "")
  {
    alert("Please enter your last name.");
    //document.theForm.LAST_NAME.style.backgroundColor = '#FFFFCC';
    document.theForm.LAST_NAME.focus();
    return (false);
  }

  if (document.theForm.LAST_NAME.value.length < 2)
  {
    alert("Please enter your last name.");
    //document.theForm.LAST_NAME.style.backgroundColor = '#FFFFCC';    
    document.theForm.LAST_NAME.focus();
    return (false);
  }

  if (document.theForm.LAST_NAME.value.length > 50)
  {
    alert("Please enter at most 24 characters in the last name field.");
    //document.theForm.LAST_NAME.style.backgroundColor = '#FFFFCC';
    document.theForm.LAST_NAME.focus();
    return (false);
  }

 if (document.theForm.LAST_NAME.value.indexOf(".", 0) >= 0)
 {
    alert("Please do not enter a period in the last name field.");
    //document.theForm.LAST_NAME.style.backgroundColor = '#FFFFCC';
    document.theForm.LAST_NAME.focus();
    return (false);
 }

 if (document.theForm.LAST_NAME.value.indexOf(",", 0) >= 0)
 {
    alert("Please do not enter a comma in the last name field.");
    //document.theForm.LAST_NAME.style.backgroundColor = '#FFFFCC';
    document.theForm.LAST_NAME.focus();
    return (false);
 }

//ADDRESS_1
 if (document.theForm.ADDRESS_1.value == "")
  {
    alert("Please enter your address.");
    //document.theForm.ADDRESS_1.style.backgroundColor = '#FFFFCC';
    document.theForm.ADDRESS_1.focus();
    return (false);
  }

  if (document.theForm.ADDRESS_1.value.length < 1)
  {
    alert("Please enter your address.");
    //document.theForm.ADDRESS_1.style.backgroundColor = '#FFFFCC';
    document.theForm.ADDRESS_1.focus();
    return (false);
  }

  if (document.theForm.ADDRESS_1.value.length > 100)
  {
    alert("Please enter at most 100 characters in the address field.");
    //document.theForm.ADDRESS_1.style.backgroundColor = '#FFFFCC';
    document.theForm.ADDRESS_1.focus();
    return (false);
  }

 if (document.theForm.ADDRESS_1.value.indexOf(",", 0) >= 0)
 {
    alert("Please do not enter a comma in the address field.");
    //document.theForm.ADDRESS_1.style.backgroundColor = '#FFFFCC';
    document.theForm.ADDRESS_1.focus();
    return (false);
 }

//ADDRESS_2

 if (document.theForm.ADDRESS_2.value.length > 100)
  {
    alert("Please enter at most 100 characters in the address field.");
    //document.theForm.ADDRESS_2.style.backgroundColor = '#FFFFCC';
    document.theForm.ADDRESS_1.focus();
    return (false);
  }

 if (document.theForm.ADDRESS_2.value.indexOf(",", 0) >= 0)
 {
    alert("Please do not enter a comma in the address field.");
    //document.theForm.ADDRESS_2.style.backgroundColor = '#FFFFCC';
    document.theForm.ADDRESS_2.focus();
    return (false);
 }

//city
if (document.theForm.CITY.value.indexOf(",", 0) >= 0)
 {
    alert("Please only enter your city in the city field. No commas please!");
    //document.theForm.CITY.style.backgroundColor = '#FFFFCC';
    document.theForm.CITY.focus();
    return (false);
 }

  if (document.theForm.CITY.value == "")
  {
    alert("Please enter your city.");
   // document.theForm.CITY.style.backgroundColor = '#FFFFCC';
    document.theForm.CITY.focus();
    return (false);
  }

  if (document.theForm.CITY.value.length < 1)
  {
    alert("Please enter your city.");
    //document.theForm.CITY.style.backgroundColor = '#FFFFCC';
    document.theForm.CITY.focus();
    return (false);
  }

  if (document.theForm.CITY.value.length > 24)
  {
    alert("Please enter at most 24 characters in the city field.");
    //document.theForm.CITY.style.backgroundColor = '#FFFFCC';
    document.theForm.CITY.focus();
    return (false);
  }

 if (document.theForm.CITY.value.indexOf(",", 0) >= 0)
 {
    alert("Please do not enter a comma in the city field.");
    //document.theForm.CITY.style.backgroundColor = '#FFFFCC';
    document.theForm.CITY.focus();
    return (false);
 }

//state
  if (document.theForm.STATE.selectedIndex < 0)
  {
    alert("Please select your state.");
    //document.theForm.STATE.style.backgroundColor = '#FFFFCC';
    document.theForm.STATE.focus();
    return (false);
  }

  if (document.theForm.STATE.selectedIndex == 0)
  {
    alert("Please select your state.");
    //document.theForm.STATE.style.backgroundColor = '#FFFFCC';
    document.theForm.STATE.focus();
    return (false);
  }

//zip
  if (document.theForm.ZIP.value == "")
  {
    alert("Please enter your zip code.");
    //document.theForm.ZIP.style.backgroundColor = '#FFFFCC';
    document.theForm.ZIP.focus();
    return (false);
  }

  if (document.theForm.ZIP.value.length < 5)
  {
    alert("Please enter your zip code.");
    //document.theForm.ZIP.style.backgroundColor = '#FFFFCC';
    document.theForm.ZIP.focus();
    return (false);
  }

  if (document.theForm.ZIP.value.length > 5)
  {
    alert("Please enter at least 5 numbers in the \"zip code\" field. Sorry, U.S. residents only..");
    //document.theForm.ZIP.style.backgroundColor = '#FFFFCC';
    document.theForm.ZIP.focus();
    return (false);
  }

  var checkOK = "0123456789";
  var checkStr = document.theForm.ZIP.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only numbers in the zip code field. Sorry, U.S. residents only.");
    //document.theForm.ZIP.style.backgroundColor = '#FFFFCC';
    document.theForm.ZIP.focus();
    return (false);
  }

//AGE
if (document.theForm.AGE.value == "")
  {
    alert("Please enter your Age.");
   // document.theForm.AGE.style.backgroundColor = '#FFFFCC';
    document.theForm.AGE.focus();
    return (false);
  }

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

  if (document.theForm.AGE.value.length > 2)
  {
    alert("Please enter at most 2 characters in the \"Age\" field.");
    document.theForm.AGE.focus();
    return (false);
  }

  var checkOK = "0123456789";
  var checkStr = document.theForm.AGE.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  
if (!allValid)
  {
    alert("Please enter only numbers in the \"Age\" field.");
    //document.theForm.AGE.style.backgroundColor = '#FFFFCC';
    document.theForm.AGE.focus();
    return (false);
  }

if (document.theForm.AGE.value < 13)
  {
    window.location = "http://www.teenfreeway.com/hcunderaged.htm";
return (false);
  }

//email
if (document.theForm.EMAIL.value == "")
  {
    alert("Please enter a valid email address.");
    //document.theForm.EMAIL.style.backgroundColor = '#FFFFCC';
    document.theForm.EMAIL.focus();
    return (false);
  }

  if (document.theForm.EMAIL.value.length < 7)
  {
    alert("Please enter at least 7 characters in the \"email\" field.");
    //document.theForm.EMAIL.style.backgroundColor = '#FFFFCC';
    document.theForm.EMAIL.focus();
    return (false);
  }

  if (document.theForm.EMAIL.value.length > 50)
  {
    alert("Please enter at most 50 characters in the \"email\" field.");
    //document.theForm.EMAIL.style.backgroundColor = '#FFFFCC';
    document.theForm.EMAIL.focus();
    return (false);
  }

if (document.theForm.EMAIL.value.indexOf("@", 0) < 0)
 {
    alert("Please enter a valid email address.");
    //document.theForm.EMAIL.style.backgroundColor = '#FFFFCC';
    document.theForm.EMAIL.focus();
    return (false);
 }

if (document.theForm.EMAIL.value.indexOf(".", 0) < 0)
 {
    alert("Please enter a valid email address.");
    //document.theForm.EMAIL.style.backgroundColor = '#FFFFCC';
    document.theForm.EMAIL.focus();
    return false;
 }

  if (document.theForm.EMAIL.value.indexOf("www.", 0) >= 0)
 {
	alert ("Please enter a valid e-mail address. Valid email adresses do not start with \" www.\"");
    //document.theForm.EMAIL.style.backgroundColor = '#FFFFCC';
    document.theForm.EMAIL.focus();
	return(false);
}

  if (document.theForm.EMAIL.value.indexOf(",", 0) >= 0)
 {
	alert ("Please enter a valid e-mail address. Valid email adresses do not contain commas.");
    //document.theForm.EMAIL.style.backgroundColor = '#FFFFCC';
    document.theForm.EMAIL.focus();
	return(false);
}


//Maine compliance w. age
var mainezip, agecomp;
mainezip = document.theForm.ZIP.value;
mainezip = mainezip.toString();
agecomp = document.theForm.AGE.value;
agecomp = agecomp.toString();
//alert(agetest);
if (mainezip >= "03901" & mainezip <= "04990" & agecomp < "18")
  {
   window.location.replace("http://www.teenfreeway.com/hcunderaged_maine.htm");
    return (false);
  }

  return (true);
}