//functions added by Bob 3/8/2005

//set minimum play time in seconds
iMinPlayTime = 30
//Message to be displayed if student enters less than iMinPlayTime
sMinPlayTimeMsg = 'Nice try but you have to play for at least ' + iMinPlayTime + ' seconds.\nI\'ve reset the timer for you.\n\nHave fun!'
//set max play time in seconds
iMaxPlayTime = 300
//Message to be displayed if student enters more than iMaxPlayTime
sMaxPlayTimeMsg = 'Whoa!  Slow down there! Let\'s just do ' + iMaxPlayTime + ' seconds.\n' + Math.round(iMaxPlayTime/60) + ' minutes and ' + iMaxPlayTime%60 + ' seconds should be enough for anyone.\nI\'ve reset the timer for you.\n\nHave fun!'

//' UE can get confused by the tick count so this is here to make color coding look right

function checkForEnter(){
	var key = event.which ? event.which : event.keyCode;
	if (key == 13)
		document.mmForm.checkAns.click()
}
function checkTimeToPlay(oObj){
	if(oObj.value<iMinPlayTime){
		alert(sMinPlayTimeMsg);
		iDefaultValue = iMinPlayTime;
	} else if (oObj.value>iMaxPlayTime){
		alert(sMaxPlayTimeMsg);
		iDefaultValue = iMaxPlayTime;
	} else {
		iDefaultValue = oObj.value
	}
	oObj.value = iDefaultValue;
	oObj.defaultValue = iDefaultValue;
}

//Original functions
//Original functions
//Original functions

//Declare and initialize global variables
var expression = "";	//The equation to be solved
var A = 0;				//Random number for the equation
var B = 0;				//Random number for the equation
var answer;				//Hold the answer that is entered
var rightAns;			//Hold correct answer
var counter = 0;		//Count the number of equations generated
var correct = 0;		//Count the number of correct answers
var incorrect = 0;		//Count the number of incorrect answers
var L = 0;				//User request for lowest random number
var H = 12;				//User request for highest random number
var t = 0;				//Set starting value for timer countdown
var ticker;				//Variable for setTimeout

//Reinitialize global variables
function initialize() {
	expression = "";	//The equation to be solved
	A = 0;				//Random number for the equation
	B = 0;				//Random number for the equation
	counter = 0;		//Count the number of equations generated
	correct = 0;		//Count the number of correct answers
	incorrect = 0;		//Count the number of incorrect answers
	t = 1 + parseInt(document.mmForm.timeClock.defaultValue);				//Set starting value for timer countdown
	document.mmForm.timeClock.value = document.mmForm.timeClock.defaultValue;
	document.mmForm.question.value = expression;
	document.mmForm.answer.value = "";
	document.mmForm.correct.value = correct;
	document.mmForm.incorrect.value = incorrect;
}
//Create fuction to stop active game without re-initializing everything
function stopPlay() {
	expression = "";	//The equation to be solved
	A = 0;				//Random number for the equation
	B = 0;				//Random number for the equation
	document.mmForm.question.value = expression;
	document.mmForm.answer.value = "";
	document.mmForm.timeClock.value = document.mmForm.timeClock.defaultValue;
}
function countDown() {
	t--;
	if (t > 0) {
		ticker = setTimeout('countDown()', 1000);
		document.mmForm.timeClock.value = t;
	} else {
		//Time is up, so stop and show final score
		clearTimeout(ticker);
		document.mmForm.timeClock.value = document.mmForm.timeClock.defaultValue;
		stopPlay();
		showResult();
	}
}
//When time runs out, display the results
function showResult() {
		var z = eval(correct + incorrect);
		alert("Time's up! Out of " + z + " tries, you got " + correct + " right and " + incorrect + " wrong.");
}
//Generate two random numbers between L and H
function genNumbers() {
	H = eval(document.mmForm.hiNum.options[document.mmForm.hiNum.options.selectedIndex].value);
	L = eval(document.mmForm.loNum.options[document.mmForm.loNum.options.selectedIndex].value);
	//Make sure H >= L
	if (L > H) {
		var K = L;
		L = H;
		H = K;
		//Change hiNum and loNum readouts if needed
		document.mmForm.hiNum.options.selectedIndex = H-1;
		document.mmForm.loNum.options.selectedIndex = L;
	}

	A = Math.floor((Math.random() * (H - L + 1)) + L);
	B = Math.floor((Math.random() * (H - L + 1)) + L);
	//Make sure A and B do not both equal zero
	if ((A == 0) && (B == 0)) {
		while (A == 0) {
			A = Math.floor((Math.random() * (H - L + 1)) + L);
		}
	}
	getOperation();
}
//Match the two random numbers to the right operation
function getOperation() {
	if (document.mmForm.operation.options.selectedIndex == 0) {
		add(A, B);
	}
	if (document.mmForm.operation.options.selectedIndex == 1) {
		subtract(A, B);
	}
	if (document.mmForm.operation.options.selectedIndex == 2) {
		multiply(A, B);
	}
	if (document.mmForm.operation.options.selectedIndex == 3) {
		divide(A, B);
	}
	//If random operation is selected, generate a random number between 0 and 3
	if (document.mmForm.operation.options.selectedIndex == 4) {
		var K = Math.floor(4 * Math.random());
		if (K == 0) {
			add(A, B);
		}
		if (K == 1) {
			subtract(A, B);
		}
		if (K == 2) {
			multiply(A, B);
		}
		if (K == 3) {
			divide(A, B);
		}
	}
}
//Generate addition expression
function add(x, y) {
	expression = x + " + " + y;
	document.mmForm.question.value = expression;
	rightAns = eval(expression);
	document.mmForm.answer.value = "";
	document.mmForm.answer.focus();
}
//Generate subtraction expression
function subtract(x, y) {
	if (x < y) {
		var z = x;
		x = y;
		y = z;
	}
	expression = x + " - " + y;
	document.mmForm.question.value = expression;
	rightAns = eval(expression);
	document.mmForm.answer.value = "";
	document.mmForm.answer.focus();
}
//Generate multiplication expression
function multiply(x, y) {
	expression = x + " * " + y;
	document.mmForm.question.value = x + " x " + y;
	rightAns = eval(expression);
	document.mmForm.answer.value = "";
	document.mmForm.answer.focus();
}
//Generate division expression
function divide(x, y) {
	//Prevent division by zero
	if (eval(y) == 0) {
		var temp = y;
		y = x;
		x = temp;
	}
	var z = eval(x * y);
	expression = z + "/" + y;
	document.mmForm.question.value = z + " / " + y;
	rightAns = eval(expression);
	document.mmForm.answer.value = "";
	document.mmForm.answer.focus();
}
//Trim spaces and zeros from front of answer and spaces off the back
function trim(inputStr) {
	//Make sure the zero is not a legitimate answer
	if (inputStr != "0") {
		//Then trim zeros and spaces off the front
		while ((inputStr != "0") && ((inputStr.charAt(0) == " ") || (inputStr.charAt(0) == "0"))) {
			inputStr = inputStr.substring(1, inputStr.length);
			document.mmForm.answer.value = inputStr;
		}
		//Then trim spaces off the back
		while (inputStr.charAt(inputStr.length - 1) == " ") {
			inputStr = inputStr.substring(0, inputStr.length-1);
			document.mmForm.answer.value = inputStr;
		}
	}
}
//Check the answer to be sure it's all numbers
function isNum(inputStr) {
	for (var i = 0; i < inputStr.length; i++) {
		var aChar = inputStr.charAt(i);
		if ((aChar < "0") || (aChar > "9")) {
			alert("Please enter only numbers.");
	return false;
			break;
		}
	}
	return true
}
//Handle routing logistics of checking the answer to be sure it's all numbers
function checkIt(inputStr) {
	if (isNum(inputStr)) {
		//inputStr is a number
		 checkAnswer(inputStr);
	} else {
		document.mmForm.answer.focus();
		document.mmForm.answer.select();
	}
}
//Check the answer to see if it's right
function checkAnswer(answer) {
	//Make sure an expression has been generated and the question answered
	if ((document.mmForm.question.value != "") && (document.mmForm.answer.value != "")) {
		//When it's right, score it
		if(parseInt(answer) == rightAns) {
			correct++;
			counter++;
			document.mmForm.correct.value = correct;
			genNumbers();		 //If not, generate a new expression
		} else {	 
	 //If it's wrong, tell the user
			incorrect++;
			counter++;
			document.mmForm.incorrect.value = incorrect;
			alert("Sorry: " + document.mmForm.question.value + " = " + rightAns);
			genNumbers();		 //If not, generate a new expression
		}
	} else {
		if (document.mmForm.question.value == "") {
			alert("Click 'Go' to see your first problem.");
			initialize();
		} else {
		alert("Oops! You forgot to enter an answer.");
		document.mmForm.answer.focus();
		}
	}
}
