// JavaScript Document
//ClarksonGene.com - use cookies to track whether user has "passed the genetics test", and control access to games page
//we need to handle the case where the user does not accept cookies
//solution: test for the goodGenes string. If NOT present, they either are 1st time visitors, or are blocking cookies
//if this is the case, try setting the cookie to the string TEST. Then continue on with the current page.
//we can then test the cookie against an empty string. If it is empty, we know the user is blocking, so we can then send them to the games page

if(document.cookie == "")
{
	document.cookie = "TEST";
}

function acceptsCookies()
{
	if(document.cookie == "")//the above code should ensure there is always a value for cookie, unless they don't accept them
	{
		return false;
	}
	else return true;
}


function checkAnswer(answer)//called from dnaSequencing page
{
	//clear existing values
	document.getElementById("answerResponse").innerHTML="";
	document.getElementById("answerResponse").style.visibility="hidden";
	//answer is one of following {29,15,6,20}
	var res = "";
	if(answer == 29 || answer == '29')
	{
		setTimeout("loadGames()",2000);
		res = "ACCESSING GAMES";
		document.getElementById('answerResponse').style.color="#015D4A";
	}
	else{res = "SORRY...TRY AGAIN";}
	
	setTimeout("document.getElementById('answerResponse').innerHTML='"+res+"'",500);
	setTimeout("document.getElementById('answerResponse').style.visibility='visible'",500);

}

function checkCode(answer)//called from dnaSequencing page
{
	//clear existing values
	document.getElementById("codeResponse").style.visibility="hidden";
	document.getElementById("codeResponse").innerHTML="";
	//yes, I know this is public...
	var res = "";
	if(answer == "CLKSN")
	{
		setTimeout("loadGames()",2000);	
		res = "ACCESSING GAMES";
		document.getElementById('answerResponse').style.color="#015D4A";
	}
	else
	{
		//clear answers?
		setTimeout("document.getElementById('accessCode').value = ''",500);
		res = "SORRY...TRY AGAIN";
	}
	
	setTimeout("document.getElementById('codeResponse').innerHTML='"+res+"'",500);
	setTimeout("document.getElementById('codeResponse').style.visibility='visible'",500);


}

function loadGames()
{
	setGeneCookie(1);
	document.location = "games.htm";
	
}

//called by the Flash movie when the sound is toggled
//cookie should be read each time a page is loaded
function setGeneCookie()
{
	document.cookie = document.cookie+",goodGenes=1";
}

function hasGoodGenes()
{
	if(typeof document.cookie == "undefined" || document.cookie == "")
	{
		return false;
	}
	else if(document.cookie.indexOf("goodGenes") == -1)
	{
		return false;
	}
	else return true;
}

function checkForEnter(evt)//called from onkeypress hendler on accessCode text field
{
	if(document.getElementById("accessCode").value.length == 5)//once they've typed in the 5th character, run the test
	{
		checkCode(document.getElementById('accessCode').value);
	}
}

function writeTrackingPixel(u)//u is url including vars
{
	//set a cookie as well, be sure not to overwrite the existing value
	var imageSrc = "";
	u = u.toLowerCase();
	if(u.indexOf("yahoo") != -1)
	{
		imageSrc = "yahoo.gif";
		document.cookie = document.cookie+",yahoo=1";
	}
	else if(u.indexOf("burstmedia") != -1)
	{
		imageSrc = "burst.gif";
		document.cookie = document.cookie+",burst=1";
	}
	else if(u.indexOf("revsci") != -1)
	{
		imageSrc = "revsci.gif";
		document.cookie = document.cookie+",revsci=1";
	}
	
	if(imageSrc != "")
	{
		document.write("<img src='trackingpixels/"+imageSrc+"' class='trackingImg'>");
		
	}
}

function writeConversionPixel()//u is url including vars
{
	var imageSrc = "";
	if(document.cookie.indexOf("yahoo") != -1)
	{
		imageSrc = "yahoo.gif";
	}
	else if(document.cookie.indexOf("burst") != -1)
	{
		imageSrc = "burst.gif";
	}
	else if(document.cookie.indexOf("revsci") != -1)
	{
		imageSrc = "revsci.gif";
	}
	
	if(imageSrc != "")
	{
		document.write("<img src='conversionpixels/"+imageSrc+"' class='trackingImg'>");
	}
}



/*********************************/
//use the following var to remember an image's original src value
//the imageRoll function will constantly update this value, allowing the imageOut function to use its current value
var imageSourceValue = '';
function imageRoll(imageObject)
{
	//store the current src value
	imageSourceValue = imageObject.src;
	//grab the directory from the src attribute
	var folderIndex = imageObject.src.lastIndexOf("/");
	var theDirectory = imageObject.src.substring(0,folderIndex+1);//returns the DIR in the form http://domain.com/folder
	//test for image file type, jpg v. gif
	var theFileType = imageObject.src.substring(imageObject.src.length-3);//grab the last 3 characters, they represent the file type

	imageObject.src=theDirectory+imageObject.name+'-on.'+theFileType;
}
function imageOut(imageObject)
{
	//retrieve the original image src attribute
	imageObject.src = imageSourceValue;
}

