var day = 0;			//day that was selected
var months = 0;			//month that was selected
var years = 0;			//year that was selected
var ageRange = 0;		//age group that user is in, gets put in database
var error = "We're sorry. This site does not collect personally identifiable information from children under the age of 13.";  //error message
var error18 = "We're sorry, this promotion is only available to U.S. legal residents 18 or older.";  //error message
var error_le = "Lo sentimos.  Este sitio no recopila informacion personal de usuarios menores de 13 anos de edad.";  //error message
var ageDays = 0;		//actual day - selected day
var ageMonths = 0;		//actual month - selected month
var ageYears = 0;		//actual year - selected year

/**
 * Checks if the user is at least 13 years old
 * 
 * True:  The form is submitted
 * False: The form cannot be submitted and a session cookie is created
 *        that keeps the user from submitting the form for the remainder of
 *        the session.
 */
function verify()
{
	
	if(getCookie("name")){
		alert(error);
		return false;
	}else{
		if(document.getElementById("age")){
			if(document.getElementById("age").value == 12){
				alert(error);
				setCookie("name","under13");
				return false;
			}
		}else{
				document.getElementById("submitButton").click();
				return true;
		}
		

/*Age Calculation was simplified to age
		if((months!=0) && (day!=0) && (years!=0)){
			var ckyDate = new Date;
			var aYear = ckyDate.getFullYear();
			ageYears = aYear - years;			
			ckyDate.setFullYear(aYear-13);
			var selectedDate = new Date(years,months-1,day);
			var foo = ckyDate.getTime()-selectedDate.getTime();
			if(foo < 0){
				alert(error);
				setCookie("name","under13");
				return false;
			}else{
				setAgeRange();
				document.getElementById("submitButton").click();
				return true;
			}
		}else{
			alert("Please enter your Year of Birth");
			return false;
		}
*/
	}
}

/* Tylenol Year of Birth Check */

function verify_ty_yob()
{
	var currentDate = new Date();
	var yearage = currentDate.getFullYear()-document.getElementById("yearbir").value;
	
	if(getCookie("name")){
		alert(error);
		return false;
	}else{
		if(yearage < 14){
			alert(error);
			setCookie("name","under13");
			return false;
		}else{
			document.getElementById("submitButton").click();
			return true;
		}
	}
}

/* Lactaid Espanol Year of Birth Check */

function verify_le()
{
	if(getCookie("name")){
		alert(error_le);
		return false;
	}else{
		if(document.getElementById("age").value == 12){
			alert(error_le);
			setCookie("name","under13");
			return false;
		}else{
			document.getElementById("submitButton").click();
			return true;
		}
	}
}

/**
 * Checks if the user is at least 18 years old
 * 
 * True:  The form is submitted
 * False: The form cannot be submitted and a session cookie is created
 *        that keeps the user from submitting the form for the remainder of
 *        the session.
 */
function verify18()
{
	if(getCookie("name")){
		alert(error18);
		return false;
	}else{
		if((document.getElementById("age").value == 12) || (document.getElementById("age").value == 5)){
			alert(error18);
			setCookie("name","under18");
			return false;
		}else{
			document.getElementById("submitButton").click();
			return true;
		}

/*Age Calculation was simplified to age
		if((months!=0) && (day!=0) && (years!=0)){
			var ckyDate = new Date;
			var aYear = ckyDate.getFullYear();
			ageYears = aYear - years;			
			ckyDate.setFullYear(aYear-18);
			var selectedDate = new Date(years,months-1,day);
			var foo = ckyDate.getTime()-selectedDate.getTime();
			if(foo < 0){
				alert(error18);
				setCookie("name","under18");
				return false;
			}else{
				setAgeRange();
				document.getElementById("submitButton").click();
				return true;
			}
		}else{
			alert("Please enter your Date of Birth");
			return false;
		}
*/
	}
}

/**
 * Sets the number of days in the dropdown by taking the month selected 
 * and getting the number of days in that month and adding all the 
 * options to the select object
 */
function setDays()
{
	var daysinmonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var dayz = daysinmonth[months-1];
	var x = document.getElementById("day");
	for(var j=1; j<x.length; j++){
		x.options[j]=null;
	}	
	for(var i=0; i<dayz; i++){
		x.options[i+1] = new Option(i+1, i+1);
	}
}

/**
 * Sets the day
 */
function selectedDay(selected)
{
	day = selected;		
}

/**
 * Sets the month and then displays the correct
 * number of days in the selected month
 */
function selectedMonth(selected)
{
	months = selected;	
	setDays();
}

/**
 * Sets the year
 */
function selectedYear(selected)
{
	years = selected;
}

/**
 * Calculates the Age Range values to be entered
 * into the database. This uses the values:
 *
 * 5: 13-17 years
 * 6: 18-24 years
 * 7: 25-34 years
 * 8: 35-49 years
 * 9: 50-54 years
 * 10: 55-64 years
 * 11: 65+ years
 *
 * It then sets the value of a hidden field which
 * gets picked up by the bean
 */
function setAgeRange()
{
	if(13<=ageYears && ageYears<=17){
		ageRange = 5;
	}else if(18<=ageYears && ageYears<=24){
		ageRange = 6;
	}else if(25<=ageYears && ageYears<=34){
		ageRange = 7;
	}else if(35<=ageYears && ageYears<=49){
		ageRange = 8;
	}else if(50<=ageYears && ageYears<=54){
		ageRange = 9;
	}else if(55<=ageYears && ageYears<=64){
		ageRange = 10;
	}else{
		ageRange = 11;
	}
	document.getElementById("age").value = ageRange;
}