/************************************************************
File:		/includes/rating_age-gate.js
Purpose:	Functions for ESRB age gating
Author:		Julia Herten-Greaven
Created:	2007-10-18
Modified:	
Project:	Generic code library
************************************************************/

/************************************************************
Changes the classname of the passed entity
************************************************************/
function ag_validate(element, langtxt) {
	if (getCookie("age-gate") == "underage") {
		element.href="javascript:alert('"+ langtxt +"')";}
	else {
		if (getCookie("age-gate") != "approved") {
			element.href="javascript:ag_onclick('"+ element.href +"')";}
	}
}

function ag_windowOnSubmit(link) {
	if (getCookie("age-gate") == "underage") {
		document.getElementById("ag_active").className="unavailable";}
	else {
		if (getCookie("age-gate") == "approved"){
			window.close();
			window.open(link);}
		else {
			;}
	}
}

function ag_restore(element, link) {
	element.href=link;
}

function ag_onclick(link) {
	if (getCookie("age-gate") == "approved") {
		window.open(link);}
	else {
		if (getCookie("age-gate") == "underage") {
			return false;}
		else {
			window.open("/support/agegate_window.asp?sFileLink="+ link,"agegate","height=350,width=400,resizable=0,statusbar=0,toolbar=0");}
		}
}

function ag_mouseover(container, ESRBCode)
{
	container.id="ag_active";
	if (getCookie("age-gate") == "underage") {
		container.className="unavailable";}
	else {
		if (getCookie("age-gate") == "approved") {
			container.className="";}
		else {
			container.className="Age-rating-" + ESRBCode;}
	}
}

function ag_mouseout(container)
{
	if (getCookie("age-gate") > 0 || getCookie("age-gate") == "underage") {
		container.className="unavailable";}
	container.id="";
}

function ag_onsubmit(formdata, today)
{
	// first we validate the form data
		bdyear = formdata.year.value;
		bdmonth = formdata.month.value;
		bdday = formdata.day.value;
	// then we define some variables like limits to age, cookie expiration, etc.
		var age17 = 17*365.25*24*60*60*1000;
		var yFloor = 0;
		var expCookie = 2;
	// if the form data is numerical then any value times zero should be zero
		if (bdyear*0 != 0 || bdmonth*0 != 0 || bdday*0 != 0 ) {
			if (bdyear*0 != 0) {formdata.year.value=formdata.year.defaultValue;};
			if (bdmonth*0 != 0) {formdata.month.value=formdata.month.defaultValue;};
			if (bdday*0 != 0) {formdata.day.value=formdata.day.defaultValue;};
		}
	// then we create the user's birthdate 
	// Note that javascript months start at 0 and end at 11
	bddate = new Date(formdata.year.value,formdata.month.value-1,formdata.day.value);

	if (
		(bdyear < yFloor || bdyear > today.getFullYear() || bdyear*0 != 0) || 
		(bdmonth < 1 || bdmonth > 12 || bdmonth*0 != 0) || 
		(bdday < 1 || bdday > 31 || bdday*0 != 0) || 
		(bddate > today)
		) {
		if (bddate > today) {
			formdata.year.style.border="2px ridge red";
			formdata.month.style.border="2px ridge red";
			formdata.day.style.border="2px ridge red";
			}
		else {	
			if (bdyear < yFloor || bdyear > today.getFullYear() || bdyear*0 != 0) {
				formdata.year.style.border="2px ridge red";}
			else {
				formdata.year.style.border="";};
	
			if (bdmonth < 1 || bdmonth > 12 || bdmonth*0 != 0) {
				formdata.month.style.border="2px ridge red";}
			else {
				formdata.month.style.border="";};
	
			if (bdday < 1 || bdday > 31 || bdday*0 != 0) {
				formdata.day.style.border="2px ridge red";}
			else {
				formdata.day.style.border="";};
			}
		}
		else {
			formdata.year.style.border="";
			formdata.month.style.border="";
			formdata.day.style.border="";
			bddate=new Date(bddate-(bddate.getTimezoneOffset()*60*1000));
			today=new Date(today-(today.getTimezoneOffset()*60*1000));
			var expDate = new Date(today.getFullYear(),today.getMonth(),today.getDate()+expCookie);
			if (bddate <= today-age17) {
				document.getElementById("ag_active").className="";
				document.cookie = "age-gate=approved; expires="+ expDate.toUTCString() +"; path=/";}
			else {
				document.getElementById("ag_active").className="unavailable";
				document.cookie = "age-gate=underage; expires="+ expDate.toUTCString() +"; path=/";}
		};
}


function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}