// Parameters for the questions
// Parameters are (xLower, xUpper, yLower, yUpper, #ofQuestions, add, sub, mult, div)
 /*q1param = new Array(  1,   5,   1,   5, 10, 1, 0, 0, 0);
 q2param = new Array(  1,  10,   1,  10, 10, 1, 0, 0, 0);
 q3param = new Array(  1,   5,   1,   5, 10, 0, 1, 0, 0);
 q4param = new Array(  1,  10,   1,  10, 10, 0, 1, 0, 0);
 q5param = new Array(  1,  15,   1,   5, 10, 1, 0, 0, 0);
 q6param = new Array(  1,  15,   1,  10, 10, 1, 0, 0, 0);
 q7param = new Array(  1,  15,   1,  15, 10, 1, 0, 0, 0);
 q8param = new Array(  1,  15,   1,   5, 10, 0, 1, 0, 0);
 q9param = new Array(  1,  15,   1,  10, 10, 0, 1, 0, 0);
q10param = new Array(  1,  15,   1,  15, 10, 0, 1, 0, 0);
q11param = new Array(  1,  10,   1,  20, 10, 1, 0, 0, 0);
q12param = new Array(  1,  20,   1,  20, 10, 1, 0, 0, 0);
q13param = new Array(  1,  10,  11,  20, 10, 0, 1, 0, 0);
q14param = new Array(  1,  20,   1,  20, 10, 0, 1, 0, 0);
q15param = new Array(  1,  50,   1,  50, 10, 1, 0, 0, 0);
q16param = new Array(  1,  50,   1,  50, 10, 0, 1, 0, 0);
q17param = new Array(100, 120,   1,  50, 10, 1, 0, 0, 0);
q18param = new Array(100, 140, 100, 140, 10, 1, 0, 0, 0);
q19param = new Array(  1,   6,   1,   6, 10, 0, 0, 1, 0);
q20param = new Array(  1,  12,   1,  12, 10, 0, 0, 1, 0);
// Parameters are (xLower, xUpper, yLower, yUpper, #ofQuestions, add, sub, mult, div, maxDivisionAnswer)
q21param = new Array(  1,  36,   1,   6, 10, 0, 0, 0, 1, 6);
q22param = new Array(  1, 144,   1,  12, 10, 0, 0, 0, 1, 12);*/
 q1param = new Array(  1,   5,   1,   5, 10, 0, 0, 1, 0);
 q2param = new Array(  1,   5,   1,  10, 10, 0, 0, 1, 0);
 q3param = new Array(  1,  10,   1,  10, 10, 0, 0, 1, 0);
 q4param = new Array(  1,  12,   1,  12, 10, 0, 0, 1, 0);
 q5param = new Array(  1,  10,   1,  10, 20, 0, 0, 1, 0);
 q6param = new Array(  1,  12,   1,  12, 20, 0, 0, 1, 0);
 
 q7param = new Array(  1,  10,   1,  10, 10, 0, 0, 0, 1,10);
 q8param = new Array(  1,  20,   1,   9, 10, 0, 0, 0, 1,20);
 q9param = new Array(  1,  40,   1,   9, 10, 0, 0, 0, 1,40);
q10param = new Array(  1,  90,   1,   9, 10, 0, 0, 0, 1,90);
q11param = new Array(  2, 100,   1,  10, 10, 0, 0, 0, 1,100);
q12param = new Array(  3, 100,   1,  20, 10, 0, 0, 0, 1,100);
q13param = new Array(  3, 200,   1,  12, 10, 0, 0, 0, 1,200);


// Writes to the document
function makeQuestions(questionNumber)
{
	
	xLower = eval("q" + questionNumber + "param[0]");
	xUpper = eval("q" + questionNumber + "param[1]");
	yLower = eval("q" + questionNumber + "param[2]");
	yUpper = eval("q" + questionNumber + "param[3]");
	numberOfQuestions = eval("q" + questionNumber + "param[4]");

	possibilitiesForSign = new Array;
	currentNumberOfSigns = 0;
	if (eval("q" + questionNumber + "param[5]") == 1)
	{
		possibilitiesForSign[currentNumberOfSigns] = " + ";
		currentNumberOfSigns++;
	}
	if (eval("q" + questionNumber + "param[6]") == 1)
	{
		possibilitiesForSign[currentNumberOfSigns] = " &#150; "
		currentNumberOfSigns++
	}
	if (eval("q" + questionNumber + "param[7]") == 1)
	{
		possibilitiesForSign[currentNumberOfSigns] = " × ";
		currentNumberOfSigns++;
	}
	if (eval("q" + questionNumber + "param[8]") == 1)
	{
		possibilitiesForSign[currentNumberOfSigns] = " ÷ ";
		currentNumberOfSigns++;
	}

	xVar = new Array
	yVar = new Array
	zAns = new Array

	// Writes the questions themselves
	document.write("<FORM NAME='question' onSubmit='checkAnswers();return false;'>");
	document.write("<TABLE BORDER=0>");
	currentCellIsEnd = 0;
	xVar[0] = 0;
	yVar[0] = 0;
	zAns[0] = 0;
	for (currentQuestion=1; currentQuestion<=numberOfQuestions; currentQuestion++)
	{
		newSign = possibilitiesForSign[Math.floor(Math.random() * possibilitiesForSign.length)];
		xVar[currentQuestion] = Math.floor(Math.random() * (xUpper - xLower + 1)) + xLower;
		yVar[currentQuestion] = Math.floor(Math.random() * (yUpper - yLower + 1)) + yLower;
		if (newSign == " + "){
			zAns[currentQuestion] = xVar[currentQuestion] + yVar[currentQuestion];
		}
		if (newSign == " &#150; "){
			if (xVar[currentQuestion] < yVar[currentQuestion]){
				tempVariable = xVar[currentQuestion];
				xVar[currentQuestion] = yVar[currentQuestion];
				yVar[currentQuestion] = tempVariable;
			}
			zAns[currentQuestion] = xVar[currentQuestion] - yVar[currentQuestion];
		}
		if (newSign == " × "){
			zAns[currentQuestion] = xVar[currentQuestion] * yVar[currentQuestion];
		}
		if (newSign == " ÷ "){
			maxDivisionAnswer = eval("q" + questionNumber + "param[9]");
			while (!((xVar[currentQuestion] % yVar[currentQuestion] == 0) && (zAns[currentQuestion] < maxDivisionAnswer)))
			{
				xVar[currentQuestion] = Math.floor(Math.random() * (xUpper - xLower + 1)) + xLower;
				yVar[currentQuestion] = Math.floor(Math.random() * (yUpper - yLower + 1)) + yLower;
				zAns[currentQuestion] = xVar[currentQuestion] / yVar[currentQuestion];
			}
			
		}
		if (currentCellIsEnd == 0) document.write("<TR>")
		document.write("<TD>");
		document.write(xVar[currentQuestion] + newSign + yVar[currentQuestion] + " = ");
		document.write("</TD>");
		document.write("<TD>");
		document.write("<INPUT TYPE='' NAME='answer" + currentQuestion + "' SIZE='5'>");
		document.write("</TD>");
		if (currentCellIsEnd == 1){
			document.write("</TR>");
			currentCellIsEnd = 0;
		}
		else currentCellIsEnd = 1;
	}
	document.write("</TABLE>");

	document.writeln("<input type='submit' default ='true' name ='submit' onClick='checkAnswers(); return false;' value='Check Answers'>");
	document.writeln("</FORM>");

	 

	document.question.answer1.focus();
}



// Checks if the answer is right
function checkAnswers()
{
	numberRight = 0;
	numberWrong = 0;
	for (currentQuestion=1; currentQuestion<=numberOfQuestions; currentQuestion++)
	{
		if (eval("document.question.answer" + currentQuestion + ".value") == zAns[currentQuestion]) numberRight++;
		else numberWrong++;
	}
	alert("You got " + numberRight + " correct and " + numberWrong + " wrong.");
	document.location.href="index.html";
	return false;
}