
/* game core */

function createMathExpr() { // Create Math Expression

	var mExpr = new Array();
	var mExprIdx = 0;
	
	for(var n = 0; n < pref.complexity*2; n += 2) {
	
		pref.negative ? mExpr[n] = dice(pref.range*2)-pref.range : mExpr[n] = dice(pref.range);
		
		if(n < pref.complexity*2-2) {
			mExpr[n+1] = getOperator();
		}
	}


	return mExpr.join(' ');
}


function getOperator() { // return a random operator of which are selected in pref

	var ops = new Array();
	
	if(pref.subt) {
		ops[ops.length] = '-';
	}
	if(pref.mult) {
		ops[ops.length] = '*';
	}
	if(pref.divi) {
		ops[pref.divi] = '/';
	}
	if((pref.addi) || (ops.length == 0)) { // there has to be atleast one operator or error will occur..
		ops[ops.length] = '+';
	}	

	return ops[dice(ops.length-1)];
}


function doAnswer(theAnswer) {

	tests[testPos].givenAnswer = theAnswer;

	
	if(tests[testPos].correct == theAnswer ) {
	
		top.status = 'correct answer!';
	}
	else {
		top.status = 'incorrect answer!';
	}
	
	progressGame();
	
}


function progressGame() {

	if(++testPos > pref.numTests) {

		endGame();
	}
	else {
		renderQuestion(testPos);
	}
}


function endGame() {
	
	top.status = 'End Of Game!';
	testInProgress = false;
	testPos = 1;
		
	var tmpSum = "<table>";
	var tmpClass;
	
	var correctAnswers = 0;
	
	for(var t = 1; t <= pref.numTests; t++) {
		
		tmpClass = 'redLight';
	
		if(tests[t].correct == tests[t].givenAnswer) {
			tmpClass = 'greenLight';
			correctAnswers++;
		}
		
		if(tests[t].givenAnswer == null) {
			break;
		}
		
		if(tests[t].givenAnswer == '???') {

			tmpClass = 'yellowLight';
		}
		
		var tmpGiven = ' ' + tests[t].givenAnswer;
		
		if(tmpGiven.indexOf('.') != -1) {
			tmpGiven = tmpGiven.substring(0, tmpGiven.indexOf('.')+4);
		}
		
		
		tmpSum += '<tr><td valign="top" class="headline2" width="35" align="right">' + t + '</td><td>&nbsp;&nbsp;&nbsp;</td><td align="left">';
		tmpSum += tests[t].question + ' = <span class="' + tmpClass + '">' + tmpGiven + '</span>';
		
		if(tests[t].correct != tests[t].givenAnswer) {
		
			var tmpCorrect = ' ' + tests[t].correct;
		
			if(tmpCorrect.indexOf('.') != -1) {
				tmpCorrect = tmpCorrect.substring(0, tmpCorrect.indexOf('.')+4);
			}
			
			tmpSum += "<br />( " + tmpCorrect + " )";
		}			
		
		tmpSum += '<br /><br /></td></tr>';

	}		
	tmpSum += '<tr><td colspan="3">' + makeButton( 'Proceed', "resetGame();", 'answerBut' ) + '</td></tr>';
	tmpSum += "</table>";

	var pCent = ' ' + ((correctAnswers / pref.numTests) * 100);
	if(pCent.indexOf('.') != -1) {
		pCent = pCent.substring(0, pCent.indexOf('.')+3);
	}	
	
	var cheatStr = "Correct: ";
	if(cheatEnabled) {
		cheatStr = "Cheater!!: ";
	}
	getEl("wScore").innerHTML = cheatStr + correctAnswers + ' / ' + pref.numTests + ' ( ' + pCent + '% )<br /><br />';	
	getEl("wSummary").innerHTML = tmpSum;

	
	getEl("game").style.display = "none";	
	getEl("results").style.display = "block";
}


/* control code */


var testInProgress = false;
var testPos = 0;

var pref;
var tests = new Array();

var cheatEnabled = false;

function initMathTest()	{

	if((pref = getSettings(document.forms[0]))) { // begin game control code
	
		getEl("init").style.display = "none";
		getEl("game").style.display = "block";
		
		for(var t = 1; t <= pref.numTests; t++) {
		
			tests[t] = new Array();
			tests[t].question = createMathExpr();
			tests[t].correct = eval(tests[t].question);
		}
		
		testInProgress = true;
		testPos = 1;
		renderQuestion(testPos);
	
	}
	else { // abort
	
		alert('Aborting Math Test...');
		resetGame();
		return false;
	}

}


function resetGame() {

	top.status = 'Warp\'s MathTest v1.x by Fini A. Alring (c) 2002';

	testInProgress = false;
	getEl("init").style.display = "block";
	getEl("results").style.display = "none";
}

function initPage() {
    document.styleSheets[1].disabled = true;
	document.forms[0].reset();
	resetGame();
}

window.onload = initPage;