var homeUrl = "http://www.zombiedriver.com";
var baseURL = "";
var age_gate_link = baseURL + "age_gate.html";
var too_young_link = baseURL + "too_young.html";


function checkAge()
{
    /* the minumum age you want to allow in */
    var min_age = 17;
    
    /* change "age_form" to whatever your form has for a name="..." */
    var year = parseInt(document.forms["age_gate_form"]["age_gate_form_year"].value);
    var month = parseInt(document.forms["age_gate_form"]["age_gate_form_month"].value) - 1;
    var day = parseInt(document.forms["age_gate_form"]["age_gate_form_day"].value);
    
    var theirDate = new Date((year + min_age), month, day);
    var today = new Date;
    
    if ( (today.getTime() - theirDate.getTime()) < 0) 
    {
	setCookie( "age_cookie", "NOT", 180 );
        document.location=too_young_link ;
        return false;
    }
    else 
    {
        setCookie( "age_cookie", "OK", 180 );

	if ( (document.referrer.indexOf( age_gate_link ) >=0 ) || ( typeof document.referrer === 'undefined' ) || !document.referrer )
        {
	    document.location = homeUrl;	    
        }
        else
        {
            document.location = document.referrer;
        }
        return false;
    }
}

function checkAgeCookie( )
{
    var cookie = getCookie( "age_cookie" );
    
    var currentUrl = document.URL;

    if ( typeof cookie === 'undefined' || !cookie )
    {
	if ( currentUrl.indexOf( age_gate_link ) == -1 )
	{
            document.location = age_gate_link ;
	}	
    }


    if ( cookie == "NOT" )
    {
        document.location = too_young_link ;
    }

}



function setCookie(c_name,value,exdays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
    {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==c_name)
        {
            return unescape(y);
        }
    }
}
