// YouTube Age Gate
// Coded by Jason Leyanna
// webmaster@realmofgaming.com
// http://www.realmofgaming.com


function readCookie()
{
var name = "age";
var ca = document.cookie.split(';');
var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  }
return null;
}

function setCookie()
{

	var name = "age";
	var value = "true";
	var date = new Date();
	var days = 1;
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
 
	//debug
	//alert(document.cookie);

	
}
function displayAgeForm(video, divname)
{
	
				var thecontent = "";
				var currentTime = new Date();
				var currentYear = currentTime.getFullYear();
				var i;
				
				
				thecontent = thecontent + '<center><br /><br /><br /><br /><br /><br />Please Enter Your Birth Date:<br />';
				thecontent = thecontent + '	<select id="Month">';
				thecontent = thecontent + '		<option value="1">January</option><option value="2">February</option><option value="3">March</option><option value="4">April</option><option value="5">May</option><option value="6">June</option><option value="7">July</option><option value="8">August</option><option value="9">September</option><option value="10">October</option><option value="11">November</option><option value="12">December</option>';
				thecontent = thecontent + '	</select>';
				thecontent = thecontent + '	<select id="Day">';
				
				for (j = 1;j <= 31;j++)
				{			
					thecontent = thecontent + '<option value="' + j + '">' + j +'</option>';
					
				}
		
				thecontent = thecontent + '	</select>';
				thecontent = thecontent + '	<select id="Year">';
						
			
				for (i = currentYear;i >= (currentYear - 100);i--)
				{			
					thecontent = thecontent + '<option value="' + i + '">' + i +'</option>';
					
				}
				
				thecontent = thecontent + '	</select>';
				
				var passedvars = "\'" + video + "\',\'" + divname + "\'";
				//alert(passedvars);
				thecontent = thecontent + "	<input type=\"submit\" value=\"Submit\" onClick=\"doAge(" +  passedvars +  ")\">";
				thecontent1 = "	<input type=\"submit\" value=\"Submit\" onClick=\"doAge(" +  passedvars +  ")\">";
				//thecontent = thecontent + '</center>';
				//alert(thecontent1);
				document.getElementById(divname).innerHTML = thecontent;
				
}
function showContent(video, divname)
{
	
	//alert("1");
	
	
	var video2 = video.substring(video.indexOf('=') + 1, video.length);
	
	//alert(video2);
	
	var thecode = '<object width="600" height="300"><param name="movie" value="http://www.youtube.com/v/' + video2 +  '"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + video2 + '" type="application/x-shockwave-flash" width="600" height="300" allowscriptaccess="always" allowfullscreen="true"></embed></object>';
	//alert(thecode);
	document.getElementById(divname).innerHTML = thecode;
	//alert("done");
	}
function showForm(video)
{

	//generate random number
	var divname = Math.floor(Math.random()*10001)

	document.write('<div id="' + divname + '" style="background-color: #000000;color: #ffffff;width: 600px;height: 300px;"><form name="age"></form></div>');
				
	if (readCookie() != null)
	{
		//old enough
		showContent(video, divname);
	}
	else{
		displayAgeForm(video, divname);
	}

				
}
function doAge(video, divname)
{


var minAge = 18;
var inputMonth = document.getElementById('Month').value;
var inputDay = document.getElementById('Day').value;
var inputYear = document.getElementById('Year').value;

var currentTime = new Date();
var currentMonth = currentTime.getMonth() + 1;
var currentDay = currentTime.getDate();
var currentYear = currentTime.getFullYear();

//alert('Selected Year' + inputYear);
//alert('Selected Month' + inputMonth);
//alert('Selected Day' + inputDay);



	if(inputYear > currentYear - minAge){
		//youngerthan 18
		//alert('Younger than 18');
	}
	else if(inputYear < currentYear - minAge)
	{
		//of age
		setCookie();
		showContent(video, divname);
	
	}
	else if(inputYear == currentYear - minAge)
	{
		if(inputMonth > currentMonth)
		{
			//of age
			setCookie();
			showContent(video, divname);
		
		}
		else if(inputMonth == currentMonth && inputDay >= currentDay)
		{
			//of age
			setCookie();
			showContent(video, divname);
	
		}
		else
		{
			//NOT of age
		}


	}	


}	

