/* PROTOTYPE EXTENSIONS */

Array.prototype.indexOf = function (aNeedle) {
	for (var i=0; i<this.length; i++) {
		if (this[i] == aNeedle) return i;
	}
	return -1;
}

/* VARIABLES */

var attempts = 0
var score = 0
//var studentName = ""
//var instName = ""
//var section = ""
var mode = "quiz";  // alternately, "results"
var questions = new Array()
var choices = new Array()
var correctAnswers = new Array()
var answers = new Array()
var hints = new Array()
var qnum = -1
var cnum = 0

/* QUIZ FUNCTIONS */

function resetForms() {
	location.replace(location.href);
}

function showQuiz() {
	if (mode == "quiz") {
		document.write('<p id="quizTitle">' + quizTitle + '</p>')
		document.write('<table width="500">')
		document.write('<form id="studentInfo" name="studentInfo">')
		//document.write('<tr><td colspan="2"><table border="0"><tr><td>Student Name:</td><td><input name="studentName" type="text" size="40" value="' + studentName +'"></td></tr>')
		//document.write('<tr><td>Instructor Name:</td><td><input name="instName" type="text" size="40" value="' + instName +'"></td></tr>')
		//document.write('<tr><td>Section:</td><td><input name="section" type="text" size="40" value="' + section +'"></td></tr></table></td></tr>')
		document.write('<tr><td>&nbsp;</td></tr>')
		document.write('</form>')
		for (var i = 0; i < questions.length; i++) {
			document.write('<form name="Question ' + (i+1) + '">')
			document.write('<tr><td valign="top"><div id="question" align="right">' + (i + 1) + '.</div></td>')
			document.write('<td valign="top"><div id="question" align="left">' + questions[i] + '</div></td></tr>')
			document.write('<tr><td></td><td valign="top"><div align="left">')
			if (typeof(correctAnswers[i]) == "object") {
				// multi-answer type
				for (var j = 0; j < choices[i].length; j++) {
					document.write('<input type="checkbox" name="q">' + choices[i][j] + '<br>')
				}
			} else {
				// single-answer type
				for (var j = 0; j < choices[i].length; j++) {
					document.write('<input type="radio" name="q">' + choices[i][j] + '<br>')
				}
			}
			document.write('</div></td></tr>')
			document.write('<tr><td>&nbsp;</td></tr>')
			document.write('</form>')
		}
		document.write('</table>')
		document.write('<form><input type="Button" value="Check Answers" onclick="checkAnswers()"> ')
		document.write('<input type="Button" value="Reset Quiz" onclick="resetForms()"></form>')
	}
}

function checkAnswers() {
	//studentName = document.forms["studentInfo"].elements[0].value
	//instName = document.forms["studentInfo"].elements[1].value
	//section = document.forms["studentInfo"].elements[2].value
	var j = 0
	for (var i = 0; i < document.forms.length; i++) {
		if (document.forms[i].name.indexOf("Question") != -1) {
			var tIsMultiAnswer = (typeof(correctAnswers[j]) == "object")
			answers[j] = getGroupSel(document.forms[i], tIsMultiAnswer);
			if (answers[j] == 0) {
				alert ("Please answer ALL of the questions!"); return;
			}
			j++;
		}
	}
	var ok = 1
	var correct = 0
	for (i = 0; i < questions.length; i++) {
		if (setsAreSame(answers[i], correctAnswers[i])) {
			correct++
		} else {
			ok = 0
		}
	}
	score = Math.round(100 * correct/questions.length)
	showResults()
}

function showResults() {
	attempts++
	var resultsTxt = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'
	resultsTxt += '<html><head><title>Quiz Results</title>'
	//resultsTxt += '<link rel="stylesheet" href="quiz.css" type="text/css">'
	resultsTxt += '<style type="text/css">'
	resultsTxt += '#quizTitle { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 16px; font-weight: bold; color: #993300 }'
	resultsTxt += '#question { font-weight: bold; }'
	resultsTxt += '#choice { margin-left: 20; }'
	resultsTxt += '#hint { color: blue; font-style: italic }'
	resultsTxt += 'td { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px }'
	resultsTxt += 'div { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px }'
	resultsTxt += '</style>'
	resultsTxt += '</head>'
	resultsTxt += '<body bgcolor="#ffffff" leftmargin="10" topmargin="10" marginwidth=10 marginheight=10>'
	resultsTxt += '<div align="center">'
	resultsTxt += '<table width="650">'
	resultsTxt += '<tr><td colspan="2"><div align="center">Quiz Results<br>'
	resultsTxt += '<- 650 pixels (for printing) ->'
	resultsTxt += '</div></td></tr>'
	resultsTxt += '<tr><td colspan="2">&nbsp;</td></tr>'

	var dateObj = new Date();
	var dateStr = (dateObj.getMonth() + 1) + '/' + dateObj.getDate() + '/' + dateObj.getYear()
	// Begin subtable
	resultsTxt += '<tr><td colspan="2"><table border="0" width="100%" cellspacing="0" cellpadding="0">'
	// Student ... Instructor
	resultsTxt += '<tr><td>'
	//resultsTxt += (studentName == "") ? '' : ('<b>Student:</b> ' + studentName)
	resultsTxt += '</td><td align="right">'
	//resultsTxt += (instName == "") ? '' : ('<b>Instructor:</b> ' + instName)
	resultsTxt += '</td></tr>'
	// Date ... Section
	resultsTxt += '<tr><td>'
	resultsTxt += '<b>Date:</b> ' + dateStr
	resultsTxt += '</td><td align="right">'
	//resultsTxt += (section == "") ? '' : ('<b>Section:</b> ' + section)
	resultsTxt += '</td></tr>'
	// Score ... Attempts
	resultsTxt += '<tr><td>'
	resultsTxt += '<b>Score:</b> ' + score + '%'
	resultsTxt += '</td><td align="right">'
	resultsTxt += '<b>Attempts:</b> ' + attempts
	resultsTxt += '</td></tr>'
	// End of subtable
	resultsTxt += '</table></td></tr>'
	resultsTxt += '<tr><td colspan="2">&nbsp;</td></tr>'

	resultsTxt += '<tr><td colspan="2"><p id="quizTitle">' + quizTitle + '</p></td></tr>'
	resultsTxt += '<tr><td colspan="2">&nbsp;</td></tr>'
	var ok = 1
	var wrongCnt = 0
	for (var i = 0; i < questions.length; i++) {
		resultsTxt += '<form name="Question ' + (i+1) + '">'
		resultsTxt += '<tr><td valign="top"><div id="question" align="right">' + (i + 1) + '.</div></td>'
		resultsTxt += '<td valign="top"><div id="question" align="left">' + parent.questions[i] + '</div></td></tr>'
		resultsTxt += '<tr><td></td><td valign="top"><div align="left">'
		ok = 1
		if (typeof(correctAnswers[i]) == "object") {
			// multi-answer type
			for (var j = 0; j < choices[i].length; j++) {
				if (answers[i].indexOf(j + 1) != -1) {
					if (correctAnswers[i].indexOf(j + 1) != -1) {
						resultsTxt += '<input type="checkbox" name="q" checked>' + choices[i][j] + ' - <font color="Green">Correct!</font><br>'
					} else {
						ok = 0
						wrongCnt++
						resultsTxt += '<input type="checkbox" name="q" checked>' + choices[i][j] + ' - <font color="Red">Incorrect</font><br>'
					}
				} else {
					resultsTxt += '<input type="checkbox" name="q">' + choices[i][j] + '<br>'
					if (correctAnswers[i].indexOf(j + 1) != -1) {
						// this anwer is correct, but wasn't checked
						ok = 0
						wrongCnt++
					}
				}
			}
		} else {
			// single-answer type
			for (var j = 0; j < choices[i].length; j++) {
				if (answers[i] == j + 1) {
					if (correctAnswers[i] == j + 1) {
						resultsTxt += '<input type="Radio" name="q" checked>' + choices[i][j] + ' - <font color="Green">Correct!</font><br>'
					} else {
						ok = 0
						wrongCnt++
						resultsTxt += '<input type="Radio" name="q" checked>' + choices[i][j] + ' - <font color="Red">Incorrect</font><br>'
					}
				} else {
					resultsTxt += '<input type="Radio" name="q">' + choices[i][j] + '<br>'
				}
			}
		}
		resultsTxt += '</div></td></tr>'
		// show feedback (hint) here...
		if (typeof(hints[i]) == "object") {
			// one hint per choice
			var hint = hints[i][answers[i]-1]
			if (hint.length > 0) {
				resultsTxt += '<tr><td colspan="2"><div id="hint">' + hint + '</div></td></tr>'
			}
		} else {
			if (!ok) {
				// one hint for all wrong answers
				resultsTxt += '<tr><td colspan="2"><div id="hint">' + hints[i] + '</div></td></tr>'
			}
		}
		resultsTxt += '<tr><td colspan="2">&nbsp;</td></tr>'
		resultsTxt += '</form>'
	}
	if (wrongCnt > 0) {

	} else {
		resultsTxt += '<tr><td colspan="2">Congratulations! You\'ve answered every question correctly.</td></tr>'
	}
	resultsTxt += '<tr><td colspan="2">To print these results, right click and choose print, or use your the print feature in your browser. When you are finished, use the <b>Close Window</b> button to return to the training materials.'
	resultsTxt += '<form><input type="Button" value="Close Window" onclick="window.close()"></form></td></tr>'
	resultsTxt += '</table>'
	resultsTxt += '</div>'
	resultsTxt += '</body></html>'
	var resultsWin = window.open();
	resultsWin.document.open();
	resultsWin.document.write(resultsTxt)
	resultsWin.document.close()  // close layout stream
}

/* UTILITY FUNCTIONS */

function deleteAt (aarray, apos) {
	var r = new Array()
	var ri = 0;
	for (var i=0; i < aarray.length; i++) {
		if (i != apos) {
			r[ri] = aarray[i]
			ri++
		}
	}
	return r
}

function identityArray (alen) {
	var r = new Array()
	for (var i=0; i<alen; i++) {
		r[i] = i
	}
	return r
}

function scrambleQuestion (aqnum) {
	var rnd = 0
	var tchoices = choices[aqnum]
	var thints = hints[aqnum]
	var idarray = identityArray(tchoices.length)
	var correctAnswer = correctAnswers[aqnum]
	var caIsArray = (typeof(correctAnswer) == "object")
	var tCorrectAnswers = new Array()
	var scrambledChoices = new Array(tchoices.length)
	var hintsIsArray = (typeof(thints) == "object")
	if (hintsIsArray) var scrambledHints = new Array(thints.length)
	var sqi = 0
	while (tchoices.length > 0) {
		rnd = Math.round(Math.random() * (tchoices.length - 1))
		scrambledChoices[sqi] = tchoices[rnd]
		if (hintsIsArray) scrambledHints[sqi] = thints[rnd]
		if (caIsArray) { // multi-answer
			var tIdx = correctAnswer.indexOf(idarray[rnd]+1)
			if (tIdx != -1) {
				 tCorrectAnswers[tIdx] = sqi + 1
			}
		} else { // single-answer
			if ((idarray[rnd] + 1) == correctAnswer) {
				correctAnswers[aqnum] = sqi + 1
			}
		}
		tchoices = deleteAt(tchoices, rnd)
		if (hintsIsArray) thints = deleteAt(thints, rnd)
		idarray = deleteAt(idarray, rnd)
		sqi++
	}
	choices[aqnum] = scrambledChoices
	if (hintsIsArray) hints[aqnum] = scrambledHints
	if (caIsArray) correctAnswers[aqnum] = tCorrectAnswers
}

function getGroupSel(butGroup, aIsMultiAnswer) {
	if (aIsMultiAnswer) {
		var tSel = new Array()
		var j = 0
		for (var i = 0; i < butGroup.length; i++) {
			if (butGroup[i].checked) {
				tSel[j] = i+1
				j++
			}
		}
		return (tSel.length > 0) ? tSel : 0
	} else {
		for (var i = 0; i < butGroup.length; i++) {
			if (butGroup[i].checked) return i+1
		}
		return 0
	}
}

function setsAreSame (aAns, aCor) {
	if (typeof(aAns) == "object") {
		// multi-answer
		if (aAns.length == aCor.length) {
			for (var i=0; i<aAns.length; i++) {
				if (aCor.indexOf(aAns[i]) == -1) {
					return false
				}
			}
			return true
		}
		return false
	} else {
		// single-answer
		return (aAns == aCor)
	}
}
ÿÿ