//////////////////////////////////////////////////////////////////////////////////////////////////
//JENS'S AGE CHECK SCRIPT - Javascript part, it needs a php check on all pages to function: 	//
//if($_COOKIE['pAgeVer'] != 1){ include('ageCheck.php'); return; }								//
//ageCheck.php checks if cookies & javascript are enabled, if they are ageCheck(); is performed	//
//////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////
//START CODE FOR ageGate.php////
  var iYear;	//User birth year
  var iMonth;	//User birth month
  var iDay;		//User birth day
  
  /*Todays date
   */
  var today = new Date();
  var tYear = today.getFullYear();
  var tMonth = today.getMonth()+1;	//+1 to make the month be 1-12 instead of 0-11
  var tDay = today.getDate();
  
  /*Links
   */
  var sDenied = "http://www.penumbragame.com/ageDenied.php";
  var sGate = "http://www.penumbragame.com/ageGate.php";
  var sCookie = "http://www.penumbragame.com/ageCookie.php";
  var sIndex = "http://www.penumbragame.com/index.php";
  var sGame = "http://www.penumbragame.com/game.php";
  var sMedia = "http://www.penumbragame.com/media.php";
  var sBuy = "http://www.penumbragame.com/buy.php";
  var sDemo = "http://www.penumbragame.com/demo.php";
  
  /*Check year, month and day form input against current date to determine if the age is correct to enter
   */
  function DateChange(sButton)
  {
	if(sButton == "day") {
		var temp = document.getElementById("day");
		iDay=temp.options[temp.selectedIndex].text;
	
		document.getElementById("month").disabled=false;
		
	} else if(sButton == "month") {
		var temp = document.getElementById("month");
		iMonth=temp.options[temp.selectedIndex].text;
		
		document.getElementById("year").disabled=false;
		
	} else if(sButton == "year") {
		var temp = document.getElementById("year");
		iYear=temp.options[temp.selectedIndex].text;

		document.getElementById("year").disabled=true;
		document.getElementById("month").disabled=true;
		document.getElementById("day").disabled=true;
		
		if(iYear < tYear-17 || (iYear == tYear-17 && iMonth <= tMonth && iDay <= tDay)) {
			setCookie('pAgeVer',1,30);
			
			gotoPage();
		} else {
			setCookie('pAgeVer',2,30);
			
			window.location = sGate;
		}
	} 
  }
  
  /*When loading ageGate go directly to the next site if age has been previously verifed, or go to deny or enable the form for age verif if not done previously.
   */
  function ageVerified()
  {
	var pAgeVer=getCookie('pAgeVer');

	if(pAgeVer==1) gotoPage();
	
	else if(pAgeVer==2) {
		window.location = sDenied;
		return;
		
	} else {
		document.getElementById("day").disabled=false;
		return;
	}
  }
  /*Return user to the page coming from
   */
  function gotoPage()
  {
  	var pPage=getCookie('pPage');

	if(pPage == "1") window.location = sGame;
	else if(pPage == "2") window.location = sMedia;
	else if(pPage == "3") window.location = sBuy;
	else if(pPage == "4") window.location = sDemo;
	else window.location = sIndex;
			
	return;
  }
//STOP CODE FOR ageGate.php//
/////////////////////////////


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//START CODE FOR ageCheck.php - is included on all pages to verify age and is run if cookie not set to allow or cookie not set at all//
  /*Age check for the pages, deny, got to age verif or let user view pages
   */
  function ageCheck()
  {
	var pAgeVer=getCookie('pAgeVer');

	if(pAgeVer==1) return;
	
	else if(pAgeVer==2) {
		window.location = sDenied;
		return;
		
	} else {
		window.location = sGate;
		return;
	}
  }
//STOP CODE FOR ageCheck.php//
//////////////////////////////

  
  ///////////////////////////////////////////////////////////////////////////////////////////////
  //These last pieces of code are from http://www.w3schools.com - Creates cookie and reads cookie
  function setCookie(c_name,value,expiredays)
  {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
  }
  
  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 "";
  }