/* 
  This script makes some assumptions:
    - This page is to be included with any and all pages that require a certain minimum age, inclding index.jsp
    - index.jsp serves as the age gateway page, home.jsp serves as the home page
	- two javascript variables must be defined as follows prior to inclusion of this file
		- var requiredAge = 18; // int value defining the minimum age to view this site
		- var cookieName = "myGameTitleAge"; // string value defining the cookie name to use (to avoid disambiguities between titles)
	- index.jsp must have a div with id="ageGate" and style="display:none;" which this script will use to display the birthday selector. 
	  The selector will be appended to any existing content in that div
	- index.jsp must have a div with id="underAge" and style="display:none;". If the user is underage, this div
	  will be made visible. It should hence contain the underage message (e.g. "You are too young to view this site")
	- In order for omniture reporting to function correctly, the initial page report flag must be set to disabled for the index page

  This is the behaviour of this page
   - If no age has been selected (no age cookie present), then redirect to index.jsp (unless we're already there of course)
   		once at index.jsp, display the birthday selector
   - If an an age cookie was already set...
  		* if older then the required age, and we are at index.jsp, redirect to home.jsp
  		* if older then the required age, and we are at anywhere else, do nothing
  		* if younger then the required age, redirect to index.jsp, unless we're already there, 
  		once at index.jsp, display the underage message
  		
  Omniture
   The following events are logged:
       - when the user comes to the index page and has no age cookie set (AgeGatePrompt)
       - when the user selects a non-qualifying age from the drop down (AgeGateUnderAge)
       - when the user comes to the index page and has a non-qualifying age cookie set (AgeGateUnderAge)
       
       - when the user either selects a qualifying age or has the cookie set with a qualifying age, *no* omni call 
            is made, and the user is redirected to home.jsp.
       

*/
	var requiredAge = 17;
	var cookieName = "gf2_age";
	
	var displayTimer=null;
    window.omniTimer=null;
	var agegateURL = "agegate.action"
	var isIndex = (top.location.href.indexOf("/"+agegateURL) != -1) ? true : false;
	var cstart = document.cookie.indexOf(cookieName+"=");
  	if(cstart==-1){

  		if(!isIndex){
  			// redirect to the index page where an age must be selected
  			location.replace(agegateURL);
  		}else{
  			// this is the index page. Show the age selector
  			makeOmniCall("AgeGatePrompt");
			displayAgeSelector();
   	}
  	}else{
  		// age cookie present. extract age and act accordingly
  		var cend=document.cookie.indexOf(";",cstart);
  		if(cend==-1) cend = document.cookie.length;
  		checkAndActOnAge(document.cookie.substring(cstart+cookieName.length+1,cend));
  	}

    /* 
     * subPage={"AgeGatePrompt"|"AgeGateUnderAge"|"AgeGatePass"}
     * intervalId=null always!
    */
    function makeOmniCall(subPage){
		/*
        if(window.omniTimer!=null) clearInterval(window.omniTimer);
    
        if(window.s_ea!=undefined && window.setOmniValues){
            var pageName = window.s_ea.pageName.substr(0, window.s_ea.pageName.lastIndexOf(":"))+":"+subPage;
            window.setOmniValues('','','','','','',1,'','',pageName);
        }else{
            // the omni script may not have been executed yet. Defer the call.
            window.omniTimer = setInterval("makeOmniCall('"+subPage+"');",1000,"JavaScript");
        }
		*/
    }

	function onAgeSelect(){
		// assemble the birthdate 
		var bdM = document.getElementById("ageSelector_month").value;
		var bdD = document.getElementById("ageSelector_day").value;
		var bdY = document.getElementById("ageSelector_year").value;
		
		if(bdD.length>0 && bdM.length>0 && bdY.length>0){
			// set the cookie with this birtday info
			var dtExp = new Date();
			var dtToday = new Date();
			dtExp.setFullYear(dtToday.getFullYear()+3);
			var cookieValue = escape( bdM +"/"+bdD+"/"+bdY);
			//did not set expiry date in order to make cookie expire at the end of a browser session.
			document.cookie = cookieName + "=" + cookieValue + "; expires=" + dtExp.toGMTString();
			
			// hide the selector
            var container = document.getElementById("ageGate");
		    container.style.visibility="hidden";
		    container.style.display="none";
		    container.innerHTML="&nbsp;";			
			// now check the date
			checkAndActOnAge(cookieValue);
		}
	}
	
	function checkAndActOnAge(ageCookieValue){
		dtNow = new Date();
		dtBD = new Date(unescape(ageCookieValue));
		
		age = dtNow.getFullYear() - dtBD.getFullYear();
		if(dtNow.getMonth() < dtBD.getMonth()) age--;
		else if(dtNow.getMonth() == dtBD.getMonth() && dtNow.getDate() < dtBD.getDate()) age--;
		
		if(age < requiredAge){
					if(!isIndex){
						// redirect to the index page, where an underage msg will be displayed
						location.replace(agegateURL);
					}else{
						// we are at the index page. Display the underage message
		       			makeOmniCall("AgeGateUnderAge");
		               displayUnderAgeMsg();
					}
				}else if(isIndex){
					// the person is old enough, no need to deal with age -> go to home page
		   			//makeOmniCall("AgeGatePass"); /* decided not to log this. the user will first be logged at the home page, which implied this call */
					document.location = "home.action";
				}	// fyi, if the person is old enough and is not at the index page, do nothing
	}

    function displayUnderAgeMsg(){
        if(displayTimer!=null) clearInterval(displayTimer);

        var container = document.getElementById("underAge");
        var container2 = document.getElementById("ageGate");
        if(container==null || container2==null){
            // the required div may not have loaded yet. Wait for a while
            displayTimer = setInterval("displayUnderAgeMsg();",500,"JavaScript");
        }else{
            container2.style.visibility = "hidden";
            container2.style.display = "none";
        
		    container.style.visibility="visible";
		    container.style.display="block";
	    }
    }

	function displayAgeSelector(){
            var container = document.getElementById("ageGate");
            var container2 = document.getElementById("underAge");
            if(displayTimer!=null) clearInterval(displayTimer);
            if(container==null || container2==null){
                // the required div may not have loaded yet. Wait for a while
                displayTimer = setInterval("displayAgeSelector();",500,"JavaScript");
            }else{
                container2.style.visibility = "hidden";
                container2.style.display = "none";
	   		    var htmlMonth = "<select id='ageSelector_month' onchange='onAgeSelect();'>\n<option value=''>Month</option>\n"+
	   			    "<option value='1'>Jan</option>\n"+
	   			    "<option value='2'>Feb</option>\n"+
	   			    "<option value='3'>Mar</option>\n"+
	   			    "<option value='4'>Apr</option>\n"+
	   			    "<option value='5'>May</option>\n"+
	   			    "<option value='6'>Jun</option>\n"+
	   			    "<option value='7'>Jul</option>\n"+
	   			    "<option value='8'>Aug</option>\n"+
	   			    "<option value='9'>Sep</option>\n"+
	   			    "<option value='10'>Oct</option>\n"+
	   			    "<option value='11'>Nov</option>\n"+
	   			    "<option value='12'>Dec</option>\n"+
	   			    "</select>";
    	
	   		    var htmlDay = "<select id='ageSelector_day' onchange='onAgeSelect();'>\n<option value=''>Day</option>\n";
	   		    for(var i=1;i<32;i++){
	   			    htmlDay += "<option value='"+i+"'>"+i+"</option>\n";
	   		    }
	   		    htmlDay += "</select>";
    			
			    var htmlYear = "<select id='ageSelector_year' onchange='onAgeSelect();'>\n<option value=''>Year</option>\n";
			    var dtToday = new Date();
	   		    for(var i=dtToday.getFullYear();i>=1900;i--){
	   			    htmlYear += "<option value='"+i+"'>"+i+"</option>\n";
	   		    }
	   		    htmlYear += "<option value='1900'>Before 1900</option>\n</select>";
    			
	   		    container.innerHTML += htmlMonth + "&nbsp;" + htmlDay + "&nbsp;" + htmlYear + "&nbsp;";
	   		    container.style.visibility = "visible";
	   		    container.style.display = "block";
	   		}
	}
