/* /* 
  Age Gate Javascript. This script is *borrowed* from ea.com/easports.com title sites.
*/

    ageGatePageURL = "/agegate.html";
	homePageURL = "/";

	var displayTimer=null;
	window.omniTimer=null;
	var isAgeCheckPage = (top.location.href.indexOf(ageGatePageURL) != -1 ) ? true : false;
	var cstart = document.cookie.indexOf(cookieName+"=");
   	if(cstart==-1){
   		if(!isAgeCheckPage){
   			// redirect to the index page where an age must be selected
			var escaped_page = currentPage.replace( "&", "%26" );
			location.replace(ageGatePageURL+'?ref='+escaped_page);
   		}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){
	    // omniture disabled on BW sites
    }


	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();
			var cookie_domain = "bioware.com";
			dtExp.setFullYear(dtToday.getFullYear()+3);
			var cookieValue = escape( bdM +"/"+bdD+"/"+bdY);
			document.cookie = cookieName + "=" + cookieValue + "; expires=" + dtExp.toGMTString() + "; path=; domain=" + cookie_domain;
			
			// 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(!isAgeCheckPage){
				// redirect to the index page, where an underage msg will be displayed
				location.replace(ageGatePageURL+'?ref='+currentPage);
			}else{
				// we are at the index page. Display the underage message
       		makeOmniCall("AgeGateUnderAge");
		document.location = "/tooyoung.html";
			}
		}else if(isAgeCheckPage){
			// 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 */
			var targetPageContainer = document.getElementById("targetPage");
			var targetPage = "";
			if (targetPageContainer != null) targetPage=targetPageContainer.value;
			if (targetPage == "") targetPage = homePageURL;
			document.location = targetPage;
		}	// 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'>\n<option value=''>Month</option>\n"+
	   			    "<option value='1' selected='selected'>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'>\n<option value=''>Day</option>\n";
					 htmlDay += "<option value='1' selected='selected'>1</option>\n";
	   		    for(var i=2;i<32;i++){
	   			    htmlDay += "<option value='"+i+"'>"+i+"</option>\n";
	   		    }
	   		    htmlDay += "</select>";
    			
			    var htmlYear = "<select id='ageSelector_year'>\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";
			    htmlYear += "</select>";
    				 var htmlSubmit = "<a href='#' id='ageCheckSubmit' onclick='onAgeSelect();'>Submit</a>";
		   		 container.innerHTML += htmlMonth + "&nbsp;" + htmlDay + "&nbsp;" + htmlYear + "&nbsp;" + htmlSubmit;
	   		    container.style.visibility = "visible";
	   		    container.style.display = "block";
	   		}
	}
