
	var COOKIE_NAME = 'age_cookie';
	
	var COOKIE_VALUE = 'monster';
	
	var AGE_VALIDATION_PAGE = 'index.php';	
	
	var HOME_PAGE = 'budlight-country.php';		 
	
	/**
	 * If the cookie is set, the age has been checked, otherwise redirect
	 * back to the age check page.
	 */
	function checkAge(){
		if(readCookie() != COOKIE_VALUE){
			document.location = AGE_VALIDATION_PAGE;
		}
	}
	
	/**
	 * Read the age check form and set the cookie if the user is of age.
	 */
	function validateAge(){
	
		// get values
		var mm = $('#DOB_MM').val();
		var dd = $('#DOB_DD').val();
		var yyyy = $('#DOB_YY').val();
		
		var errorStr = "";
		
		//	validate input
		if(!mm.match(/[0-9]{1,2}/)){ errorStr += 'Please enter your birth month. Ex 09'; }
		if(!dd.match(/[0-9]{1,2}/)){ errorStr += '\nPlease enter your birth day. Ex 22'; }
		if(!yyyy.match(/[0-9]{4}/)){ errorStr += '\nPlease enter your birth year. Ex 1974'; }
		
		// error?
		if(errorStr.length){
			alert(errorStr);
			return false;
		}
		
		
		var today = new Date();
		var ofAge = new Date();
		var userAge = new Date(yyyy, mm-1, dd);
		ofAge.setFullYear(today.getFullYear() - 21, today.getMonth(), today.getDate(), 0, 1, 0);
		if(userAge.getTime() > ofAge.getTime()){
			alert('You must be 21 years old to view this site. ');
		}
		else{
			setCookie();
			document.location = HOME_PAGE;
		}
	}
	
	/**
	 * Set the age cookie.
	 */
	function setCookie(){
		var today = new Date();
		today.setTime( today.getTime() );
		expires = 1 * 1000 * 86400; // one day
		var expires_date = new Date( today.getTime() + (expires) );		
		
		document.cookie = COOKIE_NAME + "=" +escape( COOKIE_VALUE ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" )	;		
	}
	
	/**
	 * Read the age cookie
	 */
	function readCookie(){
		var nameEQ = COOKIE_NAME + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0)
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}