
/* GUI related functions */

function getSettings(guiForm) {

	var pref = new Array();

	try {
		/* Get settings from GUI */
				
		pref.addi = guiForm["addi"].checked;
		pref.subt = guiForm["subt"].checked;
		pref.mult = guiForm["mult"].checked;
		pref.divi = guiForm["divi"].checked;
		pref.mulChoices = guiForm["mulChoices"].checked;
		pref.numChoices = guiForm["numChoices"].options[ guiForm["numChoices"].selectedIndex ].value
		pref.mulDiff = guiForm["mulDiff"].options[ guiForm["mulDiff"].selectedIndex ].value		
		pref.range = guiForm["range"].options[ guiForm["range"].selectedIndex ].value
		pref.negative = guiForm["negative"].checked;
		pref.complexity = guiForm["complexity"].options[ guiForm["complexity"].selectedIndex ].value
		pref.numTests = guiForm["numTests"].options[ guiForm["numTests"].selectedIndex ].value			
	} catch(ex) {
	
		alert('Error: ' + ex);
		return false;
	}

	return pref;
}


function renderQuestion(testPos) {

	getEl("wState").innerHTML = '(' + testPos + '/' + pref.numTests + ')'
		
	getEl("wQuest").innerHTML = tests[testPos].question + " = ?";

	
	if(pref.mulChoices) { // multiple choices (i.e generate buttons, where one has the correct answer)

		var tmpAnswers = new Array();
		var tmpHtmlButtons = "<br />";
		var correctBut = dice(pref.numChoices-1);
		var foundUnique;
		
		for(var a = 0; a < pref.numChoices; a++) { // generate multiple answers (choices)
			
			if(a == correctBut) {
				tmpAnswers[a] = tests[testPos].correct;
			}
			else {
			
				var foundUnique = false
				var attempts = 0;
				while(!foundUnique) {
					
					attempts++;
					var tmpA = tests[testPos].correct + (dice(pref.mulDiff*2) - pref.mulDiff);
					
					if((!arrayContains(tmpAnswers, tmpA) && (tmpA != tests[testPos].correct))) {
						foundUnique = true;
					}
					if(attempts > 1000) {
						
						tmpA = 0;
						foundUnique = true;

					}
				}
				tmpAnswers[a] = tmpA;
			}

			var butClass = 'answerBut';
			
			if((cheatEnabled) && (a == correctBut)) {
			
				butClass = 'answerBut greenLight';
			}

			var tmpButVal = ' ' + tmpAnswers[a];
			
			if(tmpButVal.indexOf('.') != -1) {
				tmpButVal = tmpButVal.substring(0, tmpButVal.indexOf('.')+4);
			}
			
			tmpHtmlButtons += makeButton( tmpButVal, 'doAnswer(' + tmpAnswers[a] + ');', butClass ) + "<br /><br />";

		}
		
		tmpHtmlButtons += makeButton( '???', "doAnswer('???');", 'answerBut' ) + "<br /><br />";
		tmpHtmlButtons += makeButton( 'Abort', "endGame();", 'answerBut' );		
		//alert(tmpHtmlButtons)
		getEl("wAnswers").innerHTML = tmpHtmlButtons;
	
	} else { // manual result input
	
	
		var tmpOut = '<form action="javascript:;"><br /><input type="text" value="" name="manualInput" class="inputText headline1" style="width: 80%; text-align: center;"><br /><br />';
		tmpOut += makeButton( 'Answer!', "if((document.forms[1].manualInput.value.length > 0) && (validateNumeric(document.forms[1].manualInput.value))) { doAnswer(document.forms[1].manualInput.value); } else { alert('Please type a numeric answer!');document.forms[1].manualInput.focus();}", 'answerBut' ) + "<br /><br />";
		tmpOut += makeButton( '???', "doAnswer('???');", 'answerBut' ) + "<br /><br />";	
		tmpOut += makeButton( 'Abort', "endGame();", 'answerBut' ) + '</form>';				
		getEl("wAnswers").innerHTML = tmpOut;
		setTimeout("document.forms[1].manualInput.focus();", 500);
	}
	
}


function makeButton(butValue, onClick, butClass) {

	return '<button type="button" onclick="' + onClick + '" class="' + butClass + '">' + butValue + '</button>';

}


function toggleStyleSheet(cssToggle) {

	currCss = document.forms[0].pickedCSS.options[document.forms[0].pickedCSS.selectedIndex].value;
  if(!cssToggle) {
    document.styleSheets[0].disabled = true;
    document.styleSheets[1].disabled = true;
    document.styleSheets[2].disabled = true;	
  }
  else {
    document.styleSheets[0].disabled = true;
    document.styleSheets[1].disabled = true;
    document.styleSheets[2].disabled = true;		
    document.styleSheets[currCss].disabled = false;
	getEl('init').style.visibility = 'hidden';
	getEl('init').style.visibility = 'visible';	
	
  }

}

