

////////////// FUNCTIONS ////////////////

function ageSuccess() {
	// show restricted content
	el = document.getElementById('ageRestricted');
	el.style.display = 'block';
	
	// hide age check
	el = document.getElementById('ageCheck');
	el.style.display = 'none';
}

function ageFail() {
	// show fail message
	el = document.getElementById('ageFailed');
	el.style.display = 'block';
	
	// hide age check
	el = document.getElementById('ageCheck');
	el.style.display = 'none';
}

		
function getCookie(name) {
	var re = new RegExp(name + "=([^;]+)");
	var value = re.exec(document.cookie);
	return (value != null) ? unescape(value[1]) : null;
}

function setCookie(name, value) {
	document.cookie=name + "=" + escape(value) + "; path=/;";
}
	

////////////////// INIT //////////////////////

if (!ageRestriction) ageRestriction = 18;
ageCookieName = "verified_"+ageRestriction;

// show age check:
el = document.getElementById('ageCheck');
el.style.display = 'block';

// hide age restricted content
el = document.getElementById('ageRestricted');
el.style.display = 'none';


// init form options
f = document.ageCheck_form;
if (f) {
	months = Array('Enero','Febrero','Marcha','Abril','Pueda','Junio','Julio','Augusto','Septiembre','October','Noviembre','Diciembre');
	today_date = new Date();
	if (f.ageCheck_month) for(m=0;m<months.length;m++) f.ageCheck_month.options[m] = new Option(months[m],(m+1));
	if (f.ageCheck_day) for(d=0;d<31;d++) f.ageCheck_day.options[d] = new Option((d+1),(d+1));
	if (f.ageCheck_year) for (year=(today_date.getFullYear()-5); year>=(today_date.getFullYear()-100); year--) f.ageCheck_year.options[f.ageCheck_year.options.length] = new Option(year,year);
}


function checkUserAge(age, month, day, year) {
	var todayDate = new Date(); //get today's date
	var currentMonth = todayDate.getMonth(); //today's month
	var currentDay = todayDate.getDate(); //today's day
	var currentYear = todayDate.getFullYear(); //today's year
	var userMonth = month;
	var userDay = day;
	var userYear = year;
	var yearDiff = currentYear - userYear;
	if (yearDiff == age) { //AGE IS EQUAL to VALID AGE ... need to check month and day
		var monthDiff = currentMonth - userMonth;
		if (monthDiff == 0) { //MONTH IS EQUAL ... need to check day
			var dayDiff = currentDay - userDay;
			if (dayDiff >= 0) { //DAY IS EQUAL OR GREATER .. PASS
				return true;
			} else { //DAY INVALID ... too young
				return false;
			}
		}
		else if(monthDiff < 0) { //MONTH INVALID ... too young
			return false;
		} else { //AGE PASS
			return true;
		}
	} else if (yearDiff < age) { //YEAR INVALID ... too young
		return false;
	} else { //OVER AGE in YEARS
		//trace("age pass year");
		return true;
	}
}

function checkAge() {
	f = document.ageCheck_form;
	month = f.ageCheck_month.options[f.ageCheck_month.selectedIndex].value;
	day = f.ageCheck_day.options[f.ageCheck_day.selectedIndex].value;
	year = f.ageCheck_year.options[f.ageCheck_year.selectedIndex].value;
	if (checkUserAge(ageRestriction, month, day, year)) {
		if (siteWideCheck) setCookie(ageCookieName, "SUCCESS");
		ageSuccess();
	} else {
		if (siteWideCheck) setCookie(ageCookieName, "FAIL");
		ageFail();
	}
}

// for simple yes / no links:
function verifyAge() {
	if (siteWideCheck) setCookie(ageCookieName, "SUCCESS");
	ageSuccess();
}


// startup check
if (siteWideCheck) {
	if (getCookie(ageCookieName)=='SUCCESS') {
		ageSuccess();
	}
}









