//  ############  MATH FLASH CARDS - JAN 17, 2003

//  ############  UPDATED APR 7, 2003

//  ############  CREATED BY PATRICK LEWIS

//  ############  WWW.PATRICKLEWIS.NET

//  ############  VERSION 2.0





function BadBrowser () {

alert("           * * * * * A T T E N T I O N * * * * * \n\nThis page will not work with your browser.  \n\nUse either Netscape 6x or Internet Explorer 5x or higher.    ");

}



//   ##############  SIMPLE  BROWSER SNIFFER

if (document.layers) {navigator.family = "nn4"; BadBrowser();}

if (window.navigator.userAgent.toLowerCase().indexOf('opera') != -1) {navigator.family = "opera"; BadBrowser();}

if (document.all) {navigator.family = "ie4"}

if (window.navigator.userAgent.toLowerCase().indexOf('gecko') != -1) {navigator.family = "gecko";

	document.Flash.diff[0].setting = "Easy";

	document.Flash.diff[1].setting = "Medium";

	document.Flash.diff[2].setting = "Hard";

	document.Flash.diff[3].setting = "Expert";



	document.Flash.func[0].sign = 1;

	document.Flash.func[1].sign = 2;

	document.Flash.func[2].sign = 4;

	document.Flash.func[3].sign = 8;

	}

if (window.navigator.platform.toLowerCase().indexOf('mac') != -1) {navigator.OS = "mac";}





// fraction format <sup>2</sup>/<sub>3</sub>

// division symbol is div.jpg	





farray = new Array(

"","+","-","+ -","*","+ *",

"- *","+ - *","/","+ /","- /",

"+ - /","* /","+ * /","- * /","+ - * /"

);



fSignarray = new Array();

gamestart=false;

named=false;



good = new Array("Correct!","You got that one right.","Another one right.","You're doing great!",

"You're going to the head of the class!","I knew you could get that one.","That was easy, wasn't it?",

"That's the right answer.","Good going!","");



bad = new Array("Oops, try again.","Better luck next time","Try a different answer.","Nope, not that one.",

"nope, that one was a little hard.","Hmmm, that is incorrect.","Nope, try again.");





function ClickBoxGame (button) {

if (gamestart)	{

	WriteMessage("Response" , "<font color=#aa0000>Cannot change this during a game.");

	if (button.checked) {button.checked = false;}

	else {button.checked = true;}

	}

}



function ClickRadioGame () {

if (gamestart)	{

	WriteMessage("Response" , "<font color=#aa0000>Cannot change this during a game.");

	OrigButton.checked = true;

	}

}





function ResetGame() {

name=prompt("Enter your name.","");

if (name == "") {

	WriteMessage("Response" , "<font color=#aa0000>Please enter your name or click on 'Cancel'.");

	ResetGame();

	}

else if (name == "null") {return;}



WriteMessage("Player" , name);

WriteMessage("Correct" , "0");

WriteMessage("Missed" , "0");

WriteMessage("Accuracy" , "0%");

WriteMessage("Speed" , "0 sec");

Timer = new Array();

named=true;

WriteMessage("Response" , "<font color=#0000aa>Click on 'Start game' to begin.");

}





function StartGame() {

if (!named) {

	WriteMessage("Response" , "<font color=#0000aa>Click on 'New player' and enter your name first.");

	return;

	}

if (gamestart) {return;}

WriteMessage("Correct" , "0");

WriteMessage("Missed" , "0");

WriteMessage("Accuracy" , "0%");

WriteMessage("Speed" , "0 sec");

Timer = new Array();

gamestart=true;

WriteMessage("Response" , "<font color=#0000aa>Click on an answer button.");

showProblem();

}



function StopGame () {

if (!gamestart) {return;}

else {

	if (window.navigator.userAgent.toLowerCase().indexOf('gecko') != -1) {navigator.family = "gecko";}

	if (navigator.family == "ie4") {

		WriteMessage("HighName" , Player.innerHTML +"<br>","add");



		for (ss=0;ss<4;ss++) {

			if (document.Flash.diff[ss].checked) {difficulty = document.Flash.diff[ss].setting;}

			}

		RSign = 0;

		for (ss=0;ss<4;ss++) {

			if (document.Flash.func[ss].checked) {

				RSign = Math.floor(RSign) + Math.floor(document.Flash.func[ss].sign);

				}

			}

		RSign = farray[RSign];

		RSign = RSign.toString().replace("*","x");

		RSign = RSign.toString().replace("/","<img src=divSM.jpg>");



		WriteMessage("HighDiff" , difficulty +" "+ RSign +"<br>","add");

		WriteMessage("HighAcc" , Accuracy.innerHTML +" ("+ Correct.innerHTML +"/"+ 			(Math.floor(Correct.innerHTML)+Math.floor(Missed.innerHTML)) +")<br>","add");

		WriteMessage("HighSpeed" , Speed.innerHTML +"<br>","add");

		WriteMessage("Response" , "<font color=#0000aa>Look at your Highscore!");	

		gamestart=false;

		}

	else if (navigator.family == "gecko") {



		WriteMessage("HighName" ,	document.getElementById("Player").innerHTML +"<br>","add");

		for (ss=0;ss<4;ss++) {

			if (document.Flash.diff[ss].checked) {difficulty = document.Flash.diff[ss].setting;}

			}

		RSign = 0;

		for (ss=0;ss<4;ss++) {

			if (document.Flash.func[ss].checked) {

				RSign = Math.floor(RSign) + Math.floor(document.Flash.func[ss].sign);

				}

			}

		RSign = farray[RSign];

		RSign = RSign.toString().replace("*","x");

		RSign = RSign.toString().replace("/","<img src=divSM.jpg>");



		WriteMessage("HighDiff" , difficulty +" "+ RSign +"<br>","add");

		WriteMessage("HighAcc" , 	document.getElementById("Accuracy").innerHTML +" ("+

 			document.getElementById("Correct").innerHTML +"/"+

 			(Math.floor(document.getElementById("Correct").innerHTML)+

			Math.floor(document.getElementById("Missed").innerHTML)) +")<br>","add");

		WriteMessage("HighSpeed" ,	document.getElementById("Speed").innerHTML +"<br>","add");

		WriteMessage("Response" , 	"<font color=#0000aa>Look at your Highscore!");	

		gamestart=false;

		}

	}

}





function showProblem () {

StartTime = new Date().getTime();

RSign = 0;

for (ss=0;ss<4;ss++) {

	if (document.Flash.func[ss].checked) {

		RSign = Math.floor(RSign) + Math.floor(document.Flash.func[ss].sign);

		}

	}

RSign = farray[RSign].split(" ");



MathSign = RSign[Math.floor(Math.random() * RSign.length)];

for (ss=0;ss<4;ss++) {

	if (document.Flash.diff[ss].checked) {

		difficulty = document.Flash.diff[ss].value;

		OrigButton = document.Flash.diff[ss];

		}

	}



num1 = Math.floor(Math.random() * difficulty);

num2 = Math.floor(Math.random() * difficulty);



if (MathSign == "-") {

	if (num1 < num2) {

		temp=num1;

		num1=num2;

		num2=temp;

		}

	}



if (MathSign == "/") {

	CorrectAns = num1 * num2;

	if (CorrectAns == 0 && num2 == 0) {

		//  ####  THIS WILL PUT THE NON-ZERO NUMBER SECOND

		temp=num1;

		num1=num2;

		num2=temp;

		}

	else {

		tempy = num1;

		num1 = CorrectAns;

		CorrectAns = tempy;

		}

	}

else {	CorrectAns = eval(num1 + MathSign + num2);

	}



WriteMessage("Q1" , num1);

WriteMessage("Q2" , num2);



if (MathSign == "*") 		{WriteMessage("Sign", "x");}

else if (MathSign == "/") 	{WriteMessage("Sign", "<img src=div.jpg>");}

else				{WriteMessage("Sign", MathSign);}





//  #####  PUTS IN FAKE ANSWERS

fakie=",";

for (x=1;x<5;x++) {

	if (MathSign == "+")	{fakeans = Math.floor(Math.random() * (difficulty*2));}

	if (MathSign == "-")	{fakeans = Math.floor(Math.random() * difficulty);}

	if (MathSign == "*")	{fakeans = Math.floor(Math.random() * difficulty) * Math.floor(Math.random() * difficulty);}

	if (MathSign == "/")	{fakeans = Math.floor(Math.random() * (difficulty*2));}

	if (fakie.match(","+fakeans+",") || fakeans == CorrectAns) {x--;}

	else {	fakie += fakeans+",";

		WriteMessage("Ans"+x , fakeans);

		if (navigator.family == "ie4") {document.all["A"+x].guess = fakeans;}

		else if (navigator.family == "gecko") {document.getElementById("A"+x).guess = fakeans;}

		}

	}







CPick = Math.floor(Math.random() * 4)+1;  //  ### Picks which answer gets the correct answer

WriteMessage("Ans"+CPick , CorrectAns);

if (navigator.family == "ie4") {document.all["A"+CPick].guess = CorrectAns;}

else if (navigator.family == "gecko") {document.getElementById("A"+CPick).guess = CorrectAns;}

}



function guessThis (number) {

if (!gamestart) {

	WriteMessage("Response" , "<font color=#0000aa>Click 'Start game' first.");

	return;

	}

if (number == CorrectAns) {

	EndTime=new Date().getTime();

	Timer[Timer.length] = EndTime-StartTime;

	WriteMessage("Response" , "<font color=#00aa00>"+ good[Math.floor(Math.random() * (good.length-1))]);

	if (navigator.family == "ie4") 		{WriteMessage("Correct" , Math.floor(Correct.innerHTML)+1);}

	else if (navigator.family == "gecko") 	{WriteMessage("Correct" , Math.floor(document.getElementById("Correct").innerHTML)+1);}

	updateScore();

	showProblem();

	}

else {	WriteMessage("Response" , "<font color=#aa0000>"+ bad[ Math.floor(Math.random() * (bad.length-1))]);

	if (navigator.family == "ie4") 		{WriteMessage("Missed" , Math.floor(Missed.innerHTML)+1);}

	else if (navigator.family == "gecko") 	{WriteMessage("Missed" , Math.floor(document.getElementById("Missed").innerHTML)+1);}

	updateScore();

	}

}





function updateScore () {

if (navigator.family == "ie4") 		{

	Perc = (Math.floor(Correct.innerHTML ) / (Math.floor(Missed.innerHTML )+ Math.floor(Correct.innerHTML))) * 100;


	}

else if (navigator.family == "gecko") 	{

	Perc = (Math.floor(document.getElementById("Correct").innerHTML ) / (Math.floor(document.getElementById("Missed").innerHTML )+ Math.floor(document.getElementById("Correct").innerHTML))) * 100;

	}

WriteMessage("Accuracy" , Perc.toString().substring(0,4) +"%");

temptimer = 0;

for (x=0;x<Timer.length;x++) {

	temptimer += Math.floor(Timer[x]);

	}



newSpeed = (temptimer*.001/(Timer.length)).toString().substring(0,(temptimer*.001/(Timer.length)).toString().indexOf(".")+3);

if (newSpeed != "Na") {

	WriteMessage("Speed" , (temptimer*.001/(Timer.length)).toString().substring(0,(temptimer*.001/(Timer.length)).toString().indexOf(".")+3) +" sec");

	}

}





function WriteMessage(where,what,addto) {

if (window.navigator.userAgent.toLowerCase().indexOf('gecko') != -1) {navigator.family = "gecko";}

if (addto) {

	if (navigator.family == "ie4")		{document.all[where].innerHTML += what;}

	if (navigator.family == "gecko")	{document.getElementById(where).innerHTML += what;}

	}

else {

	if (navigator.family == "ie4")		{document.all[where].innerHTML = what;}

	if (navigator.family == "gecko")	{document.getElementById(where).innerHTML = what;}

	}

}
