function checkAgeCookie(underage_file) {
  underage = document.cookie;

  if (underage.indexOf('EmailAge') > -1) {
    window.location = underage_file;
  }
}


function setAgeCookie() {
  today = new Date();
  tomorrow = new Date();
  tomorrow.setTime(today.getTime()+(60*60*24*1000));
  document.cookie='EmailAge=true; path=/; expires=' + tomorrow.toGMTString();
}

function under13(form) {
  var now = new Date();
  var thisMonth = now.getMonth() + 1;
  var thisDay = now.getDate();
  var thisYear = now.getFullYear();
  var birthMonth = form.sf_birth_month.value;
  var birthDay = form.sf_birth_date.value;
  var birthYear = form.sf_birth_year.value;
  var user_age = thisYear - birthYear;
  var minimum_year = 13;

  if (user_age < minimum_year) {
    return true;
  }
  if (user_age > minimum_year) {
    return false;
  }
  if (thisMonth < birthMonth) {
    return true;
  }
  if (thisMonth > birthMonth) {
    return false;
  }
  if (thisDay < birthDay) {
    return true;
  }
  return false;
}

function under13_2(form) {
  var now = new Date();
  var thisMonth = now.getMonth() + 1;
  var thisDay = now.getDate();
  var thisYear = now.getFullYear();
  var birthMonth = form.sf_birth_month.value;
  var birthYear = form.sf_birth_year.value;
  var user_age = thisYear - birthYear;
  var minimum_year = 13;

  if (user_age < minimum_year) {
    return true;
  }
  if (user_age > minimum_year) {
    return false;
  }
  if (thisMonth < birthMonth) {
    return true;
  }
  if (thisMonth > birthMonth) {
    return false;
  }

  return false;
}

function isBlank(s) {
  for( var i=0;i<s.length;i++ ) {
    var c = s.charAt(i);
    if( (c != ' ') && (c != '\n') && (c != '\t') )
      return false;
  }
  return true;
}



/* I got this function from "http://javascript.internet.com/forms/check-email.html".  I 
   took the comments out to conserve space.  You can look at the comments from the example
   at the URL above.  This basically checks ALL standard W3C email address formats.
*/
function checkEmailAddress( emailStr ) {

  var emailPat=/^(.+)@(.+)$/
  var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
  var validChars="\[^\\s" + specialChars + "\]"
  var quotedUser="(\"[^\"]*\")"
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
  var atom=validChars + '+'
  var word="(" + atom + "|" + quotedUser + ")"
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

  var matchArray=emailStr.match(emailPat)

  if (matchArray==null) {
    return false
  }

  var user=matchArray[1]
  var domain=matchArray[2]

  if (user.match(userPat)==null) {
      return false
  }

  var IPArray=domain.match(ipDomainPat)
  if (IPArray!=null) {
      for (var i=1;i<=4;i++) {
        if (IPArray[i]>255) {
      return false
        }
      }
      return true
  }

  var domainArray=domain.match(domainPat)
  if (domainArray==null) {
      return false
  }


  var atomPat=new RegExp(atom,"g")
  var domArr=domain.match(atomPat)
  var len=domArr.length
  if (domArr[domArr.length-1].length<2 || 
      domArr[domArr.length-1].length>3) {
     return false
  }

  // Make sure there's a host name preceding the domain.
  if (len<2) {
     return false
  }

  return true;
}

/* Changes the sf_title field to match the gender field provided. */    
function set_title(form)    
{   
  if(form.sf_gender.options[form.sf_gender.options.selectedIndex].value == "M") 
  { 
    form.sf_title.value = "Mr.";
  } 
  else  
  { 
    form.sf_title.value = "Ms.";
  } 
}   

function check_contactus(form){
  //First check the user's age and redirect if they are under 13.
  if (form.sf_age.options[form.sf_age.options.selectedIndex].value == "under13") {
    setAgeCookie();
    window.location = "/app/Colgate2in1/US/ContactUs/U13.cvsp";
    return false;
  }

  if ( isBlank(form.sf_first_nme.value) ) {
    window.alert("Please enter your first name.");
    form.sf_first_nme.focus();
    return false;
  }
  if ( isBlank(form.sf_last_nme.value) ) {
    window.alert("Please enter your last name.");
    form.sf_last_nme.focus();
    return false;
  }
  if ( isBlank(form.sf_city.value) ) {
    window.alert("Please enter your city.");
    form.sf_city.focus();
    return false;
  }
  if (form.sf_state_cd.options[form.sf_state_cd.options.selectedIndex].value == "-1") {
    window.alert("Please choose your state.");
    form.sf_state_cd.focus();
    return false;
  }
  if (form.sf_age.options[form.sf_age.options.selectedIndex].value == "none") {
    window.alert("Please choose your age.");
    form.sf_age.focus();
    return false;
  }
  if ( isBlank(form.sf_email_addr.value) ) {
    alert("Please enter your email address.");
    form.sf_email_addr.focus();
    return false;
  }
  if ( isBlank(form.email_addr_confirm.value) ) {
    alert("Please verify your email address.");
    form.email_addr_confirm.focus();
    return false;
  }
  if ( form.sf_email_addr.value != form.email_addr_confirm.value ) {
    alert("Your email address does not match. Please verify your email.");
    form.sf_email_addr.focus();
    return false;
  }
  if ( !checkEmailAddress(form.sf_email_addr.value) ) {
    form.email_addr_confirm.value = "";
    window.alert("Please enter a valid email address.");
    form.sf_email_addr.focus();
    return false;
  }
  if ( isBlank(form.cf_comment.value) ) {
    window.alert("Please enter a comment.");
    form.cf_comment.focus();
    return false;
  }

  set_title(form);

  return true;
}


function check_newsletter(form) {

  //First check the user's age and redirect if they are under 13.
  if (form.sf_age.options[form.sf_age.options.selectedIndex].value == "under13") {
    setAgeCookie();
    window.location = "enewsletter_13.html";
    return false;
  }

  if ( isBlank(form.sf_first_nme.value) ) {
    window.alert("Please enter your first name.");
    form.sf_first_nme.focus();
    return false;
  }
  if ( isBlank(form.sf_last_nme.value) ) {
    window.alert("Please enter your last name.");
    form.sf_last_nme.focus();
    return false;
  }
  if ( isBlank(form.sf_email_addr.value) ) {
    alert("Please enter your email address.");
    form.sf_email_addr.focus();
    return false;
  }
  if ( isBlank(form.email_addr_confirm.value) ) {
    alert("Please verify your email address.");
    form.email_addr_confirm.focus();
    return false;
  }
  if ( form.sf_email_addr.value != form.email_addr_confirm.value ) {
    alert("Your email address does not match. Please verify your email.");
    form.sf_email_addr.focus();
    return false;
  }
  if ( !checkEmailAddress(form.sf_email_addr.value) ) {
    form.email_addr_confirm.value = "";
    window.alert("Please enter a valid email address.");
    form.sf_email_addr.focus();
    return false;
  }
  if ( isBlank(form.sf_postal_cd.value) ) {
    window.alert("Please enter a zip.");
    form.sf_postal_cd.focus();
    return false;
  }
  if (form.sf_age.options[form.sf_age.options.selectedIndex].value == "none") {
    window.alert("Please choose your age.");
    form.sf_age.focus();
    return false;
  }

  set_title(form);

  return true;
}

function unChecked(s)
{
  var isSelected = false;
  for (var i = 0; i < s.length; i++)
  {
    if (s[i].checked)
    {
      isSelected = true;
    }
  }
  if(isSelected)
  {
    return true;
  }
  return false;
}

function check_coupon(form) {
try{
  
  if(!check_generic_body(form)){
    return false;
  }else if(form.sf_birth_month.value == "none"){
    window.alert("Please enter your birth month.");
    form.sf_birth_month.focus();
    return false;
  }else if(form.sf_birth_year.value == "none"){
    window.alert("Please enter your birth year.");
    form.sf_birth_year.focus();
    return false;
  }//if/else

  //First check the user's age and set cookie if they are under 13.
  if (under13_2(form)) {
    setAgeCookie();
    history.go();
    return false;
  }//if

   set_title(form);
   
   if(form.sf_gender.value == 'M') {
	form.sf_title.value = "Mr.";
	} else {
	form.sf_title.value = "Ms.";
	}

   return true;
}catch(e){alert(e.description);return false;}
}

function check_generic_body(form)
{

  if ( isBlank(form.sf_first_nme.value) ) {
    window.alert("Please enter your first name.");
    form.sf_first_nme.focus();
    return false;
  }else if ( isBlank(form.sf_last_nme.value) ) {
    window.alert("Please enter your last name.");
    form.sf_last_nme.focus();
    return false;
  }else if (!validEmail(form)) {
    return false;
  }else if ( isBlank(form.sf_address1.value) ) {
    alert("Please enter your street address.");
    form.sf_address1.focus();
    return false;
  }else if (!validZipcode(form)){
    return false;
  }else if(form.sf_gender.value == "none"){
    window.alert("Please enter your gender.");
    form.sf_gender[0].focus();
    return false;
    }//if/else

  return true;
}

/** 
 *  Validate email has been entered and is of proper format
 *  Optionally validate the email against the confirm field
 */
function validEmail(form) {
    if (isBlank(form.sf_email_addr.value)) {
        alert("Please enter your email address.");
        form.sf_email_addr.focus();
        return false;
  }
  if (form.email_addr_confirm) {
    if (isBlank(form.email_addr_confirm.value)) {
      alert("Please confirm your email address.");
      form.email_addr_confirm.focus();
      return false;
    }
    if (form.sf_email_addr.value != form.email_addr_confirm.value) {
      alert("The email addresses do not match. Please try again.");
      form.sf_email_addr.focus();
      return false;
    }
  }
  if (!checkEmailAddress(form.sf_email_addr.value)) {  
        alert("Please enter a valid email address.");
        form.sf_email_addr.focus();
        return false;
  }
  return true;
}


/** Validate that a zip code of proper format has been entered */
function validZipcode(form) {
    if (isBlank(form.sf_postal_cd.value)) {
        alert("Please enter your zip code.");
        form.sf_postal_cd.focus();
        return false;
  }
  if(!isZipcode(form.sf_postal_cd.value)) {
    window.alert("Please enter a valid zip code.  This is a number of 5 digits.");
    form.sf_postal_cd.focus();
    return false;
  }
  return true;
}

// TRUE if the args value if a 5-digit integer
function isZipcode(zip){
    // number pattern
    var numberPattern = /\d{5}/;

    // make sure it is a number
  var zipArray=zip.match(numberPattern)
  if (zipArray==null) {
      return false
  }
  // make sure the length is 5 characters
  if(zip.length != 5){
      return false
  }
    return true
}