// This function is used in all protected page to check if the users age is OK
function CheckAgeValid(AgeGatePage, destURL, SorryPage, NoCookiePage)
{
		// return -1 : The use age is unknown and we need one to access the site
		// return 0 : The user is under age or an error occured
		// return 2 : The user browser refuse cookie
		
		//alert(isAgeOK());
		
				
		ageIsOKCookie = agegate_getCookie("grfs_gate");
		//alert('CheckAgeValid Before if: '+ageIsOKCookie);
		if (ageIsOKCookie != null && ageIsOKCookie != ""){
		  ageIsOKVar = ageIsOKCookie;
		  //alert('From Cookie: '+ageIsOKCookie);
		}else{
		  ageIsOKVar = isAgeOK();
		}
		
		switch(ageIsOKVar)
		{
			case -1 : location.href = AgeGatePage + "?destURL=" + destURL;
								break;
			case 0 :  location.href = SorryPage;
							  break;
			case 2 :  location.href = NoCookiePage;
								break;
		}
		
}


// This page is used only in the Get Birthday Page
function CheckAgeGateOK(IndexPage, SorryPage, NoCookiePage)
{
		// return 0 : The user is under age or an error occured
		// return 1 : The user has the right to see the page or there is no AgeGate
		// return 2 : The user browser refuse cookie
		//alert(isAgeOK());
		
		ageIsOKCookie = agegate_getCookie("grfs_gate");
		//alert('CheckAgeGateOK Before if: '+ageIsOKCookie);
		if (ageIsOKCookie != null && ageIsOKCookie != ""){
		  ageIsOKVar = ageIsOKCookie;
		  //alert('From Cookie: '+ageIsOKCookie);
		}else{
		  ageIsOKVar = isAgeOK();
		  if (ageIsOKVar == 1){
		    agegate_setCookie("grfs_gate",ageIsOKVar,365);
		  }
		}
		
		switch(ageIsOKVar)
		{
			case 0 : location.href = SorryPage;
							 break;
			case 1 : location.href = IndexPage;
							 break;
			case 2 : location.href = NoCookiePage;
							 break;
		}
}

function agegate_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 agegate_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);
    }
  }
}
