var quizData;
var score;
var debug=0;

function restoreAnswers()
{
	var i, j, k, n, qnostr, c, element;
	var answer;
	var alphabet = "abcdefghijklmnopqrstuvwxyz";

	answer = getCookieValue( quiz_name );
	if (answer == "")
	{
		return;
	}
	answer = answer.split("\t");
	
	for (i=1; i<=n_questions; i++)
	{
		// alert("Answer " + i + " is |" + answer[i] + "|");
		if (answer[i] != "")
		{
			qnostr = "m_q"+i;
			element = document.quizform[qnostr];
			c = qtype.charAt(i);
			if (c == "b") // boolean
			{
				element[(answer[i] == "T") ? 0 : 1].checked = true;
			}
			else if (c == "r")	// radio button
			{
				j = alphabet.indexOf( answer[i] );
				if (j >= 0)
				{
					if (!element)
					{
						n = 0;
						for (k=0;k<document.quizform.length ;k++ )
						{
							if (qnostr == document.quizform[k].name)
							{
								if (n == j)
								{
									document.quizform[k].checked = true;
									break;
								}
								n++;
							}
						}
					}
					else
						element[j].checked = true;
				}
			}
			else if (c == "c")	// checkbox
			{
				if (answer[i] == "Y")
				{
					element.checked = true;
				}
			}
			else if (c == "i")	// input
			{
				element.value = answer[i];
			}
		}
	}
}

function assembleCookie()
{
	var i, j, c, element, qnostr,qCount,aCount;
	var answer;
	var alphabet = "abcdefghijklmnopqrstuvwxyz";

	deleteCookie( quiz_name );
	answer = new Array( n_questions+1 );
	answer[0] = "x";
	qCount=0;
	aCount=0;
	for (i=1; i<=n_questions; i++)
	{
		qnostr = "m_q"+i;
		element = document.quizform[qnostr];
//		if (!element)
//			alert(qnostr+" not found!!");
		answer[i] = "";
		c = qtype.charAt(i);
		if (c == "b") // boolean
		{
			qCount = qCount +1;
			if (element[0])
			{
				if (element[0].checked) {answer[i] = "T"; aCount = aCount + 1;}
				else if (element[1].checked) {answer[i] = "F"; aCount = aCount + 1;}
			}
		}
		else if (c == "r")	// radio button
		{
			if (!element) 	// this  kludge is for ie4.5 mac
			{
				answer[i] = "";
				qCount = qCount +1;
				for (j=0;j<document.quizform.length ;j++ )
				{
					if (qnostr == document.quizform[j].name)
					{
						if (document.quizform[j].checked)
						{
						//	alert(qnostr+" found!!");
							aCount = aCount +1;
							answer[i] = document.quizform[j].value;
							break;
						}
					}
				}
			}
			else
			{
				answer[i] = "";
				qCount = qCount +1;
				for (j=0; j<element.length; j++)
				{
					if (element[j].checked)
					{
						aCount = aCount +1;
						answer[i] = element[j].value;
						break;
					}
				}
			}
		}
		else if (c == "c")	// checkbox
		{
			answer[i] = (element.checked) ? "Y" : "N";
		}
		else if (c == "i")	// input
		{
			qCount = qCount +1;
			if (element.value)
			{
				aCount = aCount +1;
				answer[i] = element.value;
			}
		}
	}
	ansval = answer.join("\t");
	setCookie( quiz_name, ansval );
	if (qCount > aCount)
		return(false);
	return(true);
}

function doCksum()
{
	var i, element;

	score = 0;
	for (i=1; i<=n_questions; i++)
	{
		element = document.quizform["m_q"+i];
		if (element.checked)
			score += parseInt(element.value);
	}
	document.quizform.score.value=score;
}

function checkQuiz()
{
	if (special_aa == "cksum")
		doCksum();
	else
	{
		if (assembleCookie()==false)
		{
			if (1)
			{
				alert("You must answer all the questions.");
				return;
			}
		}
		gotoPage( feedback_url );
	}
}

function initFeedback()
{
}

function extractQuizData()
{
	var	i;

	quizData= getCookieValue( quiz_name );
	if (debug) document.writeln("<p>quizData = " + quizData + "</p>");
	if (debug) document.writeln("<p>quiz_name = " + quiz_name + "</p>");
	if (quizData == "")
	{
		quizData = new Array( n_questions + 1);
	}
	else
	{
		quizData = quizData.split("\t");
	}
	for (i=1; i<=n_questions; i++)
	{
		if (qtype.charAt(i) == "b")
		{
		   if (quizData[i] == "T") {quizData[i] = "True";}
		   else if (quizData[i] == "F") {quizData[i] = "False"; }
		}
		if (debug) document.writeln("<p>quizData[" + i +"] = " + quizData[i] + "</p>");
	}
}

function checkAnswers()
{
	var i,correct_answer;

	score = 0;
	for (i=1; i<=n_questions; i++)
	{
		correct_answer = correct[i];
		if (qtype.charAt(i) == "b")
		{
			correct_answer = (correct_answer == "T") ? "True" : "False";
		}
		if (qtype.charAt(i) == "t")
		{
			score = score + 1;
		}
		if (quizData[i])
		{
			//alert(quizData[i].toLowerCase() + "==" +  correct_answer.toLowerCase());
			if (quizData[i].toLowerCase() == correct_answer.toLowerCase())
			{
				score = score + 1;
			}
		}
	}
}

function showFeedback( question )
{
	var correct_answer;
	var ipath;
	var iswrong=1;

	if (isWEB)
		ipath = imagePath;
	else
		ipath = "..";
	if (debug) document.writeln("<p>Question number " + question + "</p>");
	if (quizData[question])
	{
		if (debug) document.writeln("<p>Your answer was:");
		if (debug) document.writeln("<b>" + quizData[question] + "</b>");
		correct_answer = correct[question];
		if (qtype.charAt(question) == "b")
		{
			correct_answer = (correct_answer == "T") ? "True" : "False";
		}
		if (quizData[question] != "")
		{
			if (quizData[question].toLowerCase() == correct_answer.toLowerCase())
			{
				iswrong = 0;
				if (debug) document.writeln(" correct " + correct_answer);
			}
			else
			{
				if (debug) document.writeln(" incorrect " + correct_answer);
			}
		}
		if (debug) document.writeln("</p>");
	}
	else
	{
		if (debug) document.writeln("<p>Your answer was:");
		if (debug) document.writeln("<b>" + "NULL" + "</b>");
		if (debug) document.writeln("</p>");
	}
	if (WType != "sasf" && WType != "quizf")
	{
		document.writeln("<p>Your answer was:");
		document.writeln("<b>" + quizData[question] + "</b>");
		document.writeln("</p>");
	}
	if (iswrong)
	{
		if (feedback[question])
		{
			document.writeln("<img src=\""+ipath+"/images/star.gif\">" + feedback[question]);
			document.writeln("<br><br>");
		}
	}
	else if (WType != "sasf")
	{
		document.writeln("Your answer is correct.");
		document.writeln("<br><br>");
	}
}

function showSummary()
{
	var n,pct;
	var ipath;
	if (WBook=="ch")
	{

		if (isWEB)
			ipath = imagePath;
		else
			ipath = "..";
		if (score==n_questions)
			n=10;
		else
			n=score%10;
		if (n_questions>0)
			pct = Math.floor(score/n_questions * 100);
		else
			pct = 100;
		n = Math.floor(pct/10);
		if (n>=6)
			document.writeln("<span class=\"title_main\" align=\"left\">Your score: ", score, " out of  ", n_questions, "</span>");
		else
			document.writeln("<span class=\"title_main\" align=\"left\">Your score: ", score, " out of  ", n_questions, "</span>");
	}
}
function showAllFeedback()
{
	var i;

	if (debug) document.writeln("<p>showAllFeedback</p>");
	for (i=1; i<=n_questions; i++)
	{
		showFeedback(i);
	}
}


function showSASFeedback()
{
	var n;
	if (debug) document.writeln("<p>showSASFeedback</p>");
	if (WBook=="s" || WBook=='w' || WBook=='j')
	{
		n=0;
		while (sass_first[n]!="***")
		{
			document.writeln("<br><br>");
			document.writeln(sass_first[n]);
			n++;
		}
		showAllFeedback();
		n=0;
		while (sass_last[n]!="***")
		{
			document.writeln("<br><br>");
			document.writeln(sass_last[n]);
			n++;
		}
	}
	else
	{
		n = n_questions-score;
		if (debug) document.writeln("<p>n="+n+"</p>");
		if (debug) document.writeln("<p>n_questions="+n_questions+"</p>");
		if (debug) document.writeln("<p>score="+score+"</p>");
		showSummary();
		document.writeln("<br><br>");
		if (sass_first[n])
			document.writeln(sass_first[n] + "");
		showAllFeedback();
		document.writeln("<br><br>");
		if (sass_last[n])
			document.writeln(sass_last[n]);
	}
}

function setRightAnswers()
{
	var i, j, c, element, qnostr;

	for (i=1; i<=n_questions; i++)
	{
		qnostr = "m_q"+i;
		element = document.quizform[qnostr];
		c = qtype.charAt(i);
		if (c == "b") // boolean
		{
			if (element[0])
			{
				if (correct[i] == "T")
					element[0].checked = true;
				else 
					element[1].checked = true;
			}
		}
		else if (c == "r")	// radio button
		{
			if (!element) 	// this  kludge is for ie4.5 mac
			{
				for (j=0;j<document.quizform.length ;j++ )
				{
					if (qnostr == document.quizform[j].name)
					{
						if (document.quizform[j].value == correct[i])
						{
							document.quizform[j].checked = true;
							break;
						}
					}
				}
			}
			else
			{
				for (j=0; j<element.length; j++)
				{
					if (element[j].value == correct[i])
					{
						element[j].checked = true;
						break;
					}
				}
			}
		}
		else if (c == "c")	// checkbox
		{
			if (correct[i] == "Y")
				element.checked = true;
			else
				element.checked = false;
		}
		else if (c == "i")	// input
		{
			element.value = correct[i];
		}
	}
}

function setWrongAnswers()
{
	var i, j, c, element, qnostr;

	for (i=1; i<=n_questions; i++)
	{
		qnostr = "m_q"+i;
		element = document.quizform[qnostr];
		c = qtype.charAt(i);
		if (c == "b") // boolean
		{
			if (element[0])
			{
				if (correct[i] = "T")
					element[1].checked = true;
				else 
					element[0].checked = true;
			}
		}
		else if (c == "r")	// radio button
		{
			if (!element) 	// this  kludge is for ie4.5 mac
			{
				for (j=0;j<document.quizform.length ;j++ )
				{
					if (qnostr == document.quizform[j].name)
					{
						if (document.quizform[j].value != correct[i])
						{
							document.quizform[j].checked = true;
							break;
						}
					}
				}
			}
			else
			{
				for (j=0; j<element.length; j++)
				{
					if (element[j].value != correct[i])
					{
						element[j].checked = true;
						break;
					}
				}
			}
		}
		else if (c == "c")	// checkbox
		{
			if (correct[i] == "Y")
				element.checked = false;
			else
				element.checked = true;
		}
		else if (c == "i")	// input
		{
			element.value = "";
		}
	}
}

