//This script runs state of the art validations

function setCookie(cookieName, cookieValue, cookiePath, cookieExpires)
{
   cookieValue = escape(cookieValue);

   if (cookieExpires == "")
   {
      var nowDate = new Date();
      nowDate.setMonth(nowDate.getMonth() + 1);
      cookieExpires = nowDate.toGMTString();
   }

   if (cookiePath != "")
   {
      cookiePath = ";Path=" + cookiePath;
   }

   document.cookie = cookieName + "=" + cookieValue + 
      ";expires=" + cookieExpires + cookiePath;
}

function BGVal()
{

  if (document.theForm.firstname.value == "")
  {
    alert("Please enter your First Name.");
    document.theForm.firstname.focus();
    return (false);
  }

  if (document.theForm.firstname.value.length < 1)
  {
    alert("Please enter your First Name.");
    document.theForm.firstname.focus();
    return (false);
  }

  if (document.theForm.firstname.value.length > 50)
  {
    alert("Please enter at most 50 characters in the \"First Name\" field.");
    document.theForm.firstname.focus();
    return (false);
  }
  
   if (document.theForm.firstname.value.indexOf(".", 0) >= 0)
 {
    alert("Please do not enter a period in the first name field.");
    document.theForm.firstname.focus();
    return (false);
 }
  
  if (document.theForm.lastname.value == "")
  {
    alert("Please enter your Last Name.");
    document.theForm.lastname.focus();
    return (false);
  }

  if (document.theForm.lastname.value.length < 2)
  {
    alert("Please enter your Last Name.");
    document.theForm.lastname.focus();
    return (false);
  }

  if (document.theForm.lastname.value.length > 50)
  {
    alert("Please enter at most 50 characters in the \"Last Name\" field.");
    document.theForm.lastname.focus();
    return (false);
  }
    
  if (document.theForm.lastname.value.indexOf(".", 0) >= 0)
 {
    alert("Please do not enter a period in the last name field.");
    document.theForm.lastname.focus();
    return (false);
 }

 if (document.theForm.lastname.value.indexOf(",", 0) >= 0)
 {
    alert("Please do not enter a comma in the last name field.");
    document.theForm.lastname.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 > 75)
  {
    alert("Please enter at most 75 characters in the Address field.");
    document.theForm.address1.focus();
    return (false);
  }

 
 if (document.theForm.address1.value.indexOf(",", 0) >= 0)
 {
    alert("Please do not enter a comma in the Address field.");
    document.theForm.address1.focus();
    return (false);
 }

 if (document.theForm.address2.value.indexOf(",", 0) >= 0)
 {
    alert("Please do not enter a comma in the Address field.");
    document.theForm.address2.focus();
    return (false);
 }

  if (document.theForm.address2.value.length > 25)
  {
    alert("Please enter at most 25 characters in the second Address field.");
    document.theForm.address2.focus();
    return (false);
  }


if (document.theForm.address2.value == document.theForm.address1.value)
  {
    
    document.theForm.address2.value == "";
    
  }


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

  if (document.theForm.city.value == "")
  {
    alert("Please enter your City.");
    document.theForm.city.focus();
    return (false);
  }

  if (document.theForm.city.value.length < 1)
  {
    alert("Please enter your City.");
    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.focus();
    return (false);
  }

  if (document.theForm.state.selectedIndex < 0)
  {
    alert("Please select your State.");
    document.theForm.state.focus();
    return (false);
  }

  if (document.theForm.state.selectedIndex == 0)
  {
    alert("Please select your State.");
    document.theForm.state.focus();
    return (false);
  }

  if (document.theForm.zip.value == "")
  {
    alert("Please enter your Zip Code.");
    document.theForm.zip.focus();
    return (false);
  }

  if (document.theForm.zip.value.length < 5)
  {
    alert("Please enter your Zip Code.");
    document.theForm.zip.focus();
    return (false);
  }

  if (document.theForm.zip.value.length > 5)
  {
    alert("Please enter at most 5 characters in the Zip Code field.");
    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.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.email.value.indexOf("www.", 0) >= 0)
 {
	alert ("Please enter a valid e-mail address. Valid email addresses do not start with \" www.\"");
	document.theForm.email.focus();
	return(false);
}


//email confirmation
var emailchk2, emailck1;
emailck1 = document.theForm.email.value;
emailck2 = document.theForm.emailconf.value;
if (emailck1 != emailck2){
alert("Please check to see that you entered the same valid email address in both email address fields.");
document.theForm.emailconf.focus();
return false;
}

  if (document.theForm.month_of_birth.selectedIndex == 0)
  {
    alert("Please enter your month of birth.");
    document.theForm.month_of_birth.focus();
    return (false);
  }

  if (document.theForm.day_of_birth.selectedIndex == 0)
  {
    alert("Please enter your day of birth.");
    document.theForm.day_of_birth.focus();
    return (false);
  }

  if (document.theForm.birthyear.value == "")
  {
    alert("Please enter your year of birth.");
    document.theForm.birthyear.focus();
    return (false);
  }

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

  if (document.theForm.birthyear.value.length > 4)
  {
    alert("Please enter at most 4 numbers in the year of birth 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 numbers in the year of birth field.");
    document.theForm.birthyear.focus();
    return (false);
  }

  if (decPoints > 1 || !validGroups)
  {
    alert("Please enter a valid number in the year of birth 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, bmf, bd, birthmilli, OK;
by = document.theForm.birthyear.value
bm = document.theForm.month_of_birth.value
bmf = (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,bmf,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){

//var expireDate = new Date();
//expireDate.setMonth(expireDate.getMonth() + 1);
//setCookie("13cookie",1,"/",expireDate.toGMTString());

document.cookie = "13cookie" + "=" + escape(1) + "; path=/"


//var testcook = document.cookie;
//alert(testcook);
window.location = "http://www.teenfreeway.com/pg/0707b/BG_under.htm";

return (false);
}



if (agetest >= 19){
 
 document.cookie = "13cookie" + "=" + escape(2) + "; path=/"
 window.location = "http://www.teenfreeway.com/pg/0707b/BG_over.htm";

return (false);
}


//converts birthdate to mm-dd-yyyy format
var datefix = bm + "-" + bd + "-" + by;
//alert(datefix);
document.theForm.dob.value = datefix;


//converts order date to mm-dd-yyyy format
var subdate, day, month, newmonth, newday, year;

var today = new Date();
day = today.getDate();
month = today.getMonth() + 1;
year = today.getYear();

if (day < 10)
{
newday = "0" + day;
}
else
{
newday = day;
}

if (month < 10)
{
newmonth = "0" + month;
}
else
{
newmonth = month;
}

subdate = newmonth + "-" + newday + "-" + "2008";

document.theForm.order_date.value = subdate;


//converts optins to either y or null
var optin1hold, optin2hold, optin3hold;
if (document.theForm.optin1a.checked == true){
optin1hold = "y";
}
else
{
optin1hold = "null";
}

/*
if (document.theForm.optin2a.checked == true){
optin2hold = "y";
}
else
{
optin2hold = "null";
}

if (document.theForm.optin3a.checked == true){
optin3hold = "y";
}
else
{
optin3hold = "null";
}
*/
document.theForm.optin1.value = optin1hold;

/*
document.theForm.optin2.value = optin2hold;
document.theForm.optin3.value = optin3hold;
*/

//set cookie with email value
var em = document.theForm.email.value;
//alert("em = " + em);
document.theForm.email.value = em
document.cookie = "emcookie" + "=" + escape(document.theForm.email.value) + "; path=/"

//set cookie with zip value
var zip = document.theForm.zip.value;
//alert("zip = " + zip);
document.theForm.zip.value = zip
document.cookie = "zipcookie" + "=" + escape(document.theForm.zip.value) + "; path=/"


  return (true);
}