//LSS 247 Diary Form Validation
function enableField() {
   document.dateForm.q_howhear_other.disabled=false;
}

function disableField() {
   document.dateForm.q_howhear_other.disabled=true;
}

function showOther(which) {
	if(which != "Other") {
		document.getElementById("q_howhear_other").style.display = "none";
	} else if (which == "Other") {
		document.getElementById("q_howhear_other").style.display = "block";
	}
}

function numeralsOnly(evt) {
    evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
	    ((evt.which) ? evt.which : 0));
	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
	    alert("You may only enter numerals in this field.");
		return false;
	}
	return true;
}

  function checkFields(form) {
  
    if ( isBlank(form.first.value) ) {
      alert("Please enter your first name.");
      form.first.focus();
      return false;
    }

    if ( isBlank(form.last.value)) {
      alert("Please enter your last name.");
      form.last.focus();
      return false;
    }

    if ( isBlank(form.address1.value) ) {
      alert("Please enter your street address.");
      form.address1.focus();
      return false;
    }
	
    if ( isBlank(form.city.value) ) {
      alert("Please enter your city.");
      form.city.focus();
      return false;
    }
	
    if(form.state.value == "none"){
      alert("Please select a state.");
      form.state.focus();
      return false;
    }

    if ( isBlank(form.zip.value) ) {
      alert("Please enter your zip code.");
      form.zip.focus();
      return false;
    }
	
    if(form.zip.value.length != 5) {
	  alert("Postal/ZIP code must contain 5 characters.");
	  form.zip.focus();
	  return false;
	}

    if ( !checkEmailAddress(form.email_addr.value) ) {
      form.email_addr.value = "";
      alert("Please enter a valid email address.");
      form.email_addr.focus();
      return false;
    }

    if(form.bmonth.value == "none"){
      alert("Please select a birth month.");
      form.bmonth.focus();
      return false;
    }
	
	if(form.bday.value == "none") {
	  alert("Please select a birth day.");
	  form.bday.focus();
	  return false;
	}
	
    if(form.byear.value == "none"){
      alert("Please select a birth year.");
      form.byear.focus();
      return false;
    }
	
	var learnHear = form.q_howhear.value; 
	if (learnHear == "none") { 
		alert('Please let us know how you learned about this site.'); 
		return false; 
	} else { 
		if (learnHear == "Other" && ! form.q_howhear_other.value) { 
			alert("Please specify how you learned about this site."); 
			form.q_howhear_other.focus(); 
			return false; 
		} 
	}
	
    if(under18(form)){
      setAgeCookie();
      window.location = form.underageURL.value;
      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;
  }

  // Will determine if a user is under 18
  function under18(form) {
  var now = new Date();
  var thisMonth = now.getMonth()+1;
  var thisDay = now.getDate();
  var thisYear = now.getFullYear();
  var birthMonth = form.bmonth.value;
  var birthDay = form.bday.value;
  var birthYear = form.byear.value;
  var user_age = thisYear - birthYear;
  var minimum_year = 18;
  var sameMonth = 0;
  var sameDay = 0;

  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 ((birthMonth == thisMonth) && (thisDay < birthDay)) {
	return true;  
  }
  
  if ((birthMonth == thisMonth) && (thisDay >= birthDay)) {
	return false;  
  }
  
  return false;
}

  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;
  }



  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 checkAgeCookie() {
    underage = document.cookie;
    if (underage.indexOf('EmailAge') > -1) {
      // Matt Bennett - I added this check to prevent the under 18 page from getting in a loop.
      if (window.location.search != "/app/LSS247/US/EN/247Sweepstakes/Under18.cwsp") { 
        window.location = "/app/LSS247/US/EN/247Sweepstakes/Under18.cwsp"; 
      }
    }

  checkAgeCookie();
}
