function formValid() {
var missingFields = false;
var missingMessage = "";

if (mainForm.month.value == "") {
if (missingMessage != "") {missingMessage = missingMessage + ", ";} missingMessage += "Birth Month"; missingFields = true;
}

if (mainForm.day.value == "") {
if (missingMessage != "") {missingMessage = missingMessage + ", ";} missingMessage += "Birth Day"; missingFields = true;
}

if (mainForm.year.value == "Year" || mainForm.year.value == "") {
if (missingMessage != "") {missingMessage = missingMessage + ", ";} missingMessage += "Birth Year"; missingFields = true;
}

if (missingFields == true) {
  alert("Please enter your " + missingMessage + ".");
  return false;
} else {
return validateYear();
}
}

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 birth date you selected is not valid."); 
  return false;
  } else if ((month == 2) && ((day == 30) || (day == 31))) {
  alert("Sorry, the birth date you selected is not valid."); 
  return false;   
  } else if ((month == 2) && (day == 29) && (((year % 4) != 0) && ((year % 100) != 0))) {
  alert("Sorry, the birth date you selected is not valid."); 
  return false;
  } else {
  return validation();
  }
}

function validation() {
var error = ""; 
if (mainForm.terms.checked == false) {
  error += "\n" +
  "---------------------------------------------------------------------------------------------------------------------\n" +
  "Before you can send us photos of your collection, you must agree to our Terms and Conditions.\n                  If you agree, please check the acceptance box.\n" +
  "---------------------------------------------------------------------------------------------------------------------";
}
if (error != "") {
  alert(error);
  return false;
  } else {
  return thatWorks();
  } 
}

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 would like to share your collection with us, participation is limited to collectors 21 and over.");
    document.location.href='/collectorscorner/collectors_corner_coppa.html';
    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 > 20) && (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 would like to share your collection with us, participation is limited to collectors 21 and over.");
    document.location.href='/collectorscorner/collectors_corner_coppa.html';
   return false;
   }
return true;
}
