function formValid() {
var missingFields = false;
var missingMessage = "";

if (document.mainForm.first.value == "") {
if (missingMessage != "") {missingMessage = missingMessage + ", ";}  missingMessage += "first name"; missingFields = true;
}

if (document.mainForm.last.value == "") {
if (missingMessage != "") {missingMessage = missingMessage + ", ";}  missingMessage += "last name"; missingFields = true;
}

if (document.mainForm.email.value == ""){
if (missingMessage != "") {missingMessage = missingMessage + ", ";}  missingMessage += "email address"; missingFields = true;
}

if (document.mainForm.postal_code.value == "") {
if (missingMessage != "") {missingMessage = missingMessage + ", ";} missingMessage += "postal code (if no postal code enter \"none\")"; missingFields = true;
}

if (document.mainForm.country.value == "") {
if (missingMessage != "") {missingMessage = missingMessage + ", ";} missingMessage += "location"; missingFields = true;
}

if (document.mainForm.month.value == "") {
if (missingMessage != "") {missingMessage = missingMessage + ", ";} missingMessage += "birth month"; missingFields = true;
}

if (document.mainForm.day.value == "") {
if (missingMessage != "") {missingMessage = missingMessage + ", ";} missingMessage += "birth day"; missingFields = true;
}

if (document.mainForm.comment.value == "") {
if (missingMessage != "") {missingMessage = missingMessage + " and ";} missingMessage += "message or question for Coca-Cola"; missingFields = true;
}
if (document.getElementById('recaptcha_response_field')!= null) {
	if (($('recaptcha_response_field').get('value') == "")){
		if (missingMessage != "") {missingMessage = missingMessage + ", ";} missingMessage += "Captcha Response"; missingFields = true; 
 	}
 }	
if (missingFields == true) {
  alert("Please enter your " + missingMessage + ".");
  return false;
} else {
return checkEmail();
}
}
function checkEmail() {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.mainForm.email.value)){
  return pcodeCheck(document.mainForm.postal_code.value);
}
  alert("Please enter a valid email address.");
  document.mainForm.email.focus();
  return false;
}

function validateYear(year) {
var okYear = document.mainForm.year;

if (okYear.value == "") {
  alert("Please enter a valid 4 digit year.");
  return false;
} else {
if (isNaN(okYear.value)) {
  alert("Please enter a valid 4 digit year.");
  okYear.select();
  okYear.focus();
  return false;
}

var year = parseInt(okYear.value);
var minYear = "1894";
var maxYear = "2004";
  if (okYear.value.length != 4 || year == 0 || year < minYear || year > maxYear) {
  alert("Please enter a valid 4 digit year.");
  okYear.select();
  okYear.focus();
  return false;
  } else {
  return checkDate();
  }
}
return true;
}

function checkDate(){
var form = document.mainForm;
var day = form.day.value;
var month = form.month.value;
var year = form.year.value;

  if (((month == 4) || (month == 6) || (month == 9) || (month == 11)) && (day == 31)) { 
  alert("Sorry, the birthdate you selected is not valid."); 
  return false;
  } else if ((month == 2) && ((day == 30) || (day == 31))) {
  alert("Sorry, the birthdate you selected is not valid."); 
  return false;   
  } else if ((month == 2) && (day == 29) && (((year % 4) != 0) && ((year % 100) != 0))) {
  alert("Sorry, the birthdate you selected is not valid."); 
  return false;
  } else {
  return thatWorks();
  }
}

function findCommunication() {
var phone = document.mainForm.phone.value;
var perfd = document.mainForm.preferred;
var nums = perfd.selectedIndex;
  if (nums != 0 && perfd.options[nums] && phone == "") { 
    window.alert('You selected to be reached by ' + (perfd.value) + ', yet did not provide a phone number.');
    document.mainForm.phone.focus();
  } else {
  return true;
  }
}

function pcodeCheck(field){
zip = document.mainForm.postal_code;
var country = document.mainForm.country;
var sel = country.selectedIndex;
var valid = "0123456789";
var count = 0;

for (var i=0; i < field.length; i++) {
  temp = "" + field.substring(i, i+1);
	if (temp == "-") count++;
	if ((sel == 233) && (isNaN(zip.value))) {
	  alert('You selected United States as your location, yet did not supply a correct postal code.  Please enter 5 numeric characters in the Postal Code field.');
	  zip.focus();
	  zip.select();
	  return false;
	} else if (sel == 233 && zip.value.length != 5) {
	  alert('Please enter 5 numeric characters in the Postal Code field.');
	  zip.focus();
	  zip.select();
	  return false;
	} else {
	return validateYear();
	}
  }
}

function readCookie(cookieName) {
var cookieString = document.cookie;
var cookieSet = cookieString.split (';');
var setSize = cookieSet.length;
var returnValue = "";
var cookiePieces;
var c = 0;
  
for (c = 0; ((c < setSize) && (returnValue == "")); c++) {
cookiePieces = cookieSet[c].split ('=');
  if (cookiePieces[0].substring (0,1) == ' ') {
    cookiePieces[0] = cookiePieces[0].substring (1, cookiePieces[0].length);
  }
  if (cookiePieces[0] == cookieName) {
    returnValue = cookiePieces[1];
  }
}
  if (returnValue == 'yes') {
    return true;
  }
  else if (returnValue == 'no') {
    alert("Although we are grateful that you want to send us a message, the Children's Online Privacy Protection Act prevents us from collecting information from consumers under the age of 13.  However, you are welcome to contact us by phone or mail. We regret any inconvenience this may cause.  If you would like us to respond to your inquiry via email, please include your email address in your letter.\n\nIndustry and Consumer Affairs\nThe Coca-Cola Company\n1 Coca-Cola Plaza\nAtlanta, GA 30313\n800-438-2653 (phone)");
    document.location.href='/ssldocs/mail/coppa.shtml';
    return false;
  }
}

function writeCookie(cookieName,cookieValue) {
var expDate = new Date();
  expDate.setTime (expDate.getTime() + (1000 * 60 * 60 * 24));
  document.cookie = cookieName + "=" + escape (cookieValue) + "; expires=" + expDate.toGMTString();
}

function dateFix(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0) { date.setTime(date.getTime() - skew); }
}

function howOld(month,day,year) {
var a = new Date();

dateFix(a);
	 
var thisDay = a.getDate();
var thisMonth = a.getMonth() + 1;
var thisYear = a.getFullYear();
var yearsOld = thisYear - year; 
var monthsOld = 0;
var daysOld = 0;
var age = '';

  if (thisMonth >= month) {
    monthsOld = thisMonth - month;
    } else {
      yearsOld--;
      monthsOld = thisMonth + 12 - month;
    }
  if (thisDay >= day) {
    daysOld = thisDay - day;
    } else {
  if (monthsOld > 0) {
    monthsOld--;
    } else {
      yearsOld--;
      monthsOld += 11;
    }
    daysOld = thisDay + 31 - day;
    }
    if (yearsOld < 0) return '';
    if ((yearsOld == 0) && (monthsOld == 0) && (daysOld == 0)) return '';
    if (yearsOld > 0) {
      age = yearsOld;
      if (yearsOld > 1) age;
      age += ' ';
    }
    return age;
}

function thatWorks() {
var form = document.mainForm;
var age = howOld(form.day.value, form.month.value, form.year.value);
var dob;
 
 if((age > 12) && (age <= 110)) {
    dob = form.month.value + "-" + form.day.value + "-" + form.year.value;
	writeCookie('submit','yes');
 } else {
   writeCookie('submit','no');
   alert("Although we are grateful that you want to send us a message, the Children's Online Privacy Protection Act prevents us from collecting information from consumers under the age of 13.  However, you are welcome to contact us by phone or mail. We regret any inconvenience this may cause.  If you would like us to respond to your inquiry via email, please include your email address in your letter.\n\nIndustry and Consumer Affairs\nThe Coca-Cola Company\n1 Coca-Cola Plaza\nAtlanta, GA 30313\n800-438-2653 (phone)");
   document.location.href='/ssldocs/mail/coppa.shtml';
   return false;
   }
return true;
}
