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



function formVal()

{

  if (document.theForm.First_Name.value == "")

  {

    alert("Please enter your First Name.");
    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.focus();
    return (false);

  }

  if (document.theForm.First_Name.value.length > 24)

  {
    alert("Please enter at most 24 characters in the \"First Name\" field.");
    document.theForm.First_Name.focus();
    return (false);
  }

  if (document.theForm.Last_Name.value == "")

  {
    alert("Please enter your Last Name.");
    document.theForm.Last_Name.focus();
    return (false);
  }

  if (document.theForm.Last_Name.value.length < 1)

  {
    alert("Please enter your Last Name.");
    document.theForm.Last_Name.focus();
    return (false);
  }

  if (document.theForm.Last_Name.value.length > 24)

  {
    alert("Please enter at most 24 characters in the \"Last Name\" field.");
    document.theForm.Last_Name.focus();
    return (false);
  }

  if (document.theForm.email.value == "")

  {
    alert("Please enter your email address.");
    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.focus();
    return (false);
  }

  if (document.theForm.email.value.length > 50)

  {
    alert("Please enter at most 50 characters in the \"email\" field.");
    document.theForm.email.focus();
    return (false);
  }

if (document.theForm.email.value.indexOf("@", 0) < 0)

 {
    alert("Please enter a valid email address.");
    document.theForm.email.focus();
    return (false);
 }

if (document.theForm.email.value.indexOf(".", 0) < 0)

 {
    alert("Please enter a valid email address.");
    document.theForm.email.focus();
    return false;
 }

  if (document.theForm.Address1.value == "")

  {

    alert("Please enter your Address.");
    document.theForm.Address1.focus();
    return (false);
  }

  if (document.theForm.Address1.value.length < 1)

  {
    alert("Please enter your Address.");
    document.theForm.Address1.focus();
    return (false);
  }

  if (document.theForm.Address1.value.length > 24)

  {
    alert("Please enter at most 24 characters in the Address field.");
    document.theForm.Address1.focus();
    return (false);
  }

  

  if (document.theForm.Address2.value.length > 24)

  {
    alert("Please enter at most 24 characters in the Address field.");
    document.theForm.Address2.focus();
    return (false);
  }



  if (document.theForm.City1.value == "")

  {
    alert("Please enter your City.");
    document.theForm.City1.focus();
    return (false);
  }



  if (document.theForm.City1.value.length < 1)

  {
    alert("Please enter your City.");
    document.theForm.City1.focus();
    return (false);
  }



  if (document.theForm.City1.value.length > 24)
  
  {
    alert("Please enter at most 24 characters in the City field.");
    document.theForm.City1.focus();
    return (false);
  }



  if (document.theForm.state1.selectedIndex < 0)

  {
  alert("Please select your State.");
    document.theForm.state1.focus();
    return (false);
  }



  if (document.theForm.state1.selectedIndex == 0)

  {

    alert("Please select your State.");
    document.theForm.state1.focus();
    return (false);

  }



  if (document.theForm.Zip1.value == "")

  {

    alert("Please enter your Zip Code.");
    document.theForm.Zip1.focus();
    return (false);

  }



  if (document.theForm.Zip1.value.length < 5)

  {

    alert("Please enter your Zip Code.");
    document.theForm.Zip1.focus();
    return (false);

  }



  if (document.theForm.Zip1.value.length > 5)

  {

    alert("Please enter at most 5 characters in the Zip Code field.");
    document.theForm.Zip1.focus();
    return (false);

  }



  var checkOK = "0123456789";
  var checkStr = document.theForm.Zip1.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.Zip1.focus();
    return (false);
  }

if (document.theForm.month_of_birth.selectedIndex < 0)
  {
    alert("The first \"month_of_birth\" option is not a valid selection.  Please choose one of the other options.");
    document.theForm.month_of_birth.focus();
    return (false);
  }

  if (document.theForm.month_of_birth.selectedIndex == 0)

  {
    alert("The first \"month_of_birth\" option is not a valid selection.  Please choose one of the other options.");
    document.theForm.month_of_birth.focus();
    return (false);
  }

  if (document.theForm.day_of_birth.selectedIndex < 0)
  {
    alert("The first \"day_of_birth\" option is not a valid selection.  Please choose one of the other options.");
    document.theForm.day_of_birth.focus();
    return (false);
  }

  if (document.theForm.day_of_birth.selectedIndex == 0)
  {
    alert("The first \"day_of_birth\" option is not a valid selection.  Please choose one of the other options.");
    document.theForm.day_of_birth.focus();
    return (false);
  }

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

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

  if (document.theForm.birthyear.value.length > 4)

  {
    alert("Please enter at most 4 characters in the \"birthyear\" field.");
    document.theForm.birthyear.focus();
    return (false);
  }
  
  var checkOK = "0123456789";
  var checkStr = document.theForm.birthyear.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  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 (ch == ".")

    {

      allNum += ".";
      decPoints++;

    }

    else if (ch == "," && decPoints != 0)

    {

      validGroups = false;
      break;

    }

    else if (ch != ",")
      allNum += ch;

  }

  if (!allValid)

  {
    alert("Please enter only digit characters in the \"birthyear\" field.");
    document.theForm.birthyear.focus();
    return (false);
  }

  if (decPoints > 1 || !validGroups)

  {
    alert("Please enter a valid number in the \"birthyear\" field.");
    document.theForm.birthyear.focus();
    return (false);
  }

var today = new Date();

//alert("Today's date is: " + today);

var milli = today.valueOf();

//alert("Today's date in milliseconds is: " + milli);
//var thirteen = (1000*60*60*24*365.242199*13);
//alert("The number of milliseconds in 13 years is: " + thirteen);

var oneyr = (1000*60*60*24*365.242199);

//alert("The number of milliseconds in 1 years is: " + oneyear);

var by, bm, bd, birthmilli, OK;
by = document.theForm.birthyear.value
bm = document.theForm.month_of_birth.value
bm = (bm-1);
bd = document.theForm.day_of_birth.value

//alert("The birth year is: " + by);
//alert("The birth month is: " + bm);
//alert("The birth day is: " + bd);

birthmilli = new Date(by,bm,bd);
//alert("birthmilli is: " + birthmilli);

var birthmilliconv = birthmilli.valueOf();
//alert("birthmilliconv is: " + birthmilliconv);

var agetest = ((milli - birthmilliconv) / oneyr);
//alert("agetest is: " + agetest);

if (agetest < 13){
 window.location = "http://www.rewardscountry.com/rc_underaged.htm";
return (false);

}

if (document.theForm.rewards.selectedIndex < 0)
  {
    alert("Please select which kinds of rewards you are interested in.");
    document.theForm.rewards.focus();
    return (false);
  }

  if (document.theForm.rewards.selectedIndex == 0)
  {
    alert("Please select which kinds of rewards you are interested in.");
    document.theForm.rewards.focus();
    return (false);
  }
  
  var mainezip;
mainezip = document.theForm.Zip1.value;
mainezip = mainezip.toString();
//alert(agetest);
if (mainezip >= "03901" & mainezip <= "04990" & agetest < 18)
  {
     window.location.replace("http://www.rewardscountry.com/rc_underaged_me.htm");
    return (false);
  }

//set email cookie for autoresponder
var signup = document.theForm.email.value;
//alert("signup = " + signup);
document.theForm.email.value = signup
document.cookie = "signupcookie" + "=" + escape(document.theForm.email.value) + "; path=/"

  return (true);
}

