// JavaScript quiz builder. Written for CadetStuff.org by Shawn Stanford.
// This code is released into the public domain.

function dumpQuiz() {
	for (x = 0; x < testElements.length; x++) {
		questionArray = testElements[x].split('|');
		document.write(x + '.<br>');
		for (y = 0; y < questionArray.length; y++) {
			document.write('&nbsp;&nbsp;&nbsp;' + y + '. ' + questionArray[y] + '<br>');
		};
	};
};

function buildQuiz() {
	referenceArray = new Array;
	for (x = 0; x < testElements.length; x++) {
		y = Math.floor((Math.random() * testElements.length));
		while (testElements[y] == 'used') {
			y++;
			if (y == testElements.length) {
				y = 0;
			};
		};
		questionArray = testElements[y].split('|');
		testElements[y] = 'used';
		questionText = questionArray[0];
		questionNumber = x + 1;
		document.writeln('<form><table><tr><td><font class="inline-h3">' + questionNumber + '. ' + questionText + '</font><p class="article-text">');

		answerCount = questionArray[1];
		correctAnswer = questionArray[2];

		for (y = 1; y <= answerCount; y++) {
			if (y == correctAnswer) {
				elementValue = 'correct';
			} 
			else {
				elementValue = 'incorrect';
			};
			questionIndex = y + 2;
			document.writeln('<input type="radio" name="q' + x + '" value="' + elementValue + '">' + questionArray[questionIndex] + '<br>');
		};
		document.writeln('</p></td></tr></table></form>');
		referenceIndex = questionArray.length - 1;
		referenceArray[questionNumber] = questionArray[referenceIndex];
	};
	document.writeln('<form onSubmit="checkScore();return false">');
	document.writeln('<p><input type="submit" value="Check"></p>');
	document.writeln('</form>');
};

function checkScore() {

	resultsWindow = window.open('', 'resultsWindow', 'scrollbars=yes, toolbar=yes, resizable=yes, width=400, height=300');
	resultsWindow.document.write('<html><head><title>Test results</title><link rel="stylesheet" type="text/css" href="http://www.cadetstuff.org/crushvel.css"></head><body><h2>Test Results</h2><p>What follows are the manual references for the questions you missed. It is suggested that you review the material in these sections. Good luck!</p>');

	errorCount = document.forms.length - 1;
	correctCount = 0;
	foundErrors = 0;
	for (i=0; i < document.forms.length - 1; i++){
		badResult = 'true';
		for (j = 0; j < document.forms[i].elements.length; j++) {
			if (document.forms[i].elements[j].checked) {
				if (document.forms[i].elements[j].value == 'correct') {
					errorCount--;
					correctCount++;
					badResult = 'false';
				};
			};
		};
		if (badResult == 'true') {
			referenceIndex = i + 1;
			resultsWindow.document.writeln(referenceArray[referenceIndex] + '<br>');
		};
	};

	resultsWindow.document.write('Correct: ' + correctCount + '<br>');
	resultsWindow.document.write('Incorrect: ' + errorCount + '<br>');
	resultsWindow.document.write('Percentage: ' + Math.floor((correctCount / testElements.length) * 100) + '<br>');
	resultsWindow.document.write('</body></html>');
	resultsWindow.document.close();

};
