/*
function $(strId){
	return document.getElementById(strId);
}
*/

Quiz = function(xml_file, _quiz_div)
{
	this.quiz_xml = null;
	this.file = xml_file;
	this.current = 0;
	this.mistake = false;
	
	this.right = 0;
	this.wrong = 0;
	
	this.quiz_div = _quiz_div;

	this.request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP");
	this.request.open("GET", this.file, false);
	this.request.send(null);
	this.quiz_xml = this.request.responseXML;
	this.display();
}

Quiz.prototype.display = function()
{
	var type = this.getAttribute("type");
	var question = this.getQuestions();
	var answers = this.getAnswers();

	$(this.quiz_div).innerHTML = question[0].firstChild.data + "<br/><br/>";

	if (type == "tf")
	{
		$(this.quiz_div).innerHTML += "<input name='answer' type='radio'/> True<br/><input name='answer' type='radio'/> False<br/>";
	}
	else if(type == "abc")
	{
		for (var j = 0; j < answers.length; j++) {
			$(this.quiz_div).innerHTML += "<input name='answer' type='radio'/> " + answers[j].firstChild.data + "<br/>";
		}	
	}
	else if(type == "yn")
	{
		$(this.quiz_div).innerHTML += "<input name='answer' type='radio'/> Yes<br/><input name='answer' type='radio'/> No<br/>";
	}
}

Quiz.prototype.length = function() { return this.quiz_xml.getElementsByTagName("q").length; }
Quiz.prototype.getQuestions = function() { return this.quiz_xml.getElementsByTagName("q")[this.current].getElementsByTagName("question"); }
Quiz.prototype.getAnswers = function() { return this.quiz_xml.getElementsByTagName("q")[this.current].getElementsByTagName("answer"); }
Quiz.prototype.getAttribute = function(attr) { return this.quiz_xml.getElementsByTagName("q")[this.current].attributes.getNamedItem(attr).value; }

Quiz.prototype.disableInput = function()
{
	var input = $(this.quiz_div).getElementsByTagName("input");
	for(i in input) {
		input[i].disabled = "disabled";
	}
}

Quiz.prototype.next = function()
{
	var answer = false;
	var input = $(this.quiz_div).getElementsByTagName("input");

	if (input.length && !this.mistake)
	{
		if (this.getAttribute("type")=="tf" || this.getAttribute("type")=="yn")
		{
			if (input[0].checked) {
				answer = "T";
			} else if(input[1].checked) {
				answer = "F";
			}
		}
		else if(this.getAttribute("type")=="abc")
		{
			if (input[0].checked) {
				answer = "A";
			} else if(input[1].checked) {
				answer = "B";
			} else if(input[2].checked) {
				answer = "C";
			}
		}
	}
	
	var right_answer = this.getAttribute("right");
	if (!answer && !this.mistake) return;
	
	if (right_answer!="" && answer != right_answer.toUpperCase() && !this.mistake)
	{
		this.mistake = true;
		if (this.getAttribute("type")=="tf")
		{
			$(this.quiz_div).innerHTML += this.getAnswers()[0].firstChild.data;
		}
		else if(this.getAttribute("type")=="abc")
		{
			var answer_index = right_answer.toUpperCase().charCodeAt(0) - 65;
			$(this.quiz_div).innerHTML += "The correct answer is: " + this.getAnswers()[answer_index].firstChild.data;
		}
		this.disableInput();
	}
	else {
		this.mistake = false;
		right_answer.toUpperCase() == answer ? this.right++ : this.wrong++;
		
		if( this.current < this.length()-1)
		{
			this.current++;
			this.display();
		}
		else
		{
			if (this.getAttribute("type")=="tf" || this.getAttribute("type")=="abc")
			{
				if (this.wrong == 2) {
					message = "Pretty good - you got "+this.right+" right!";
				} else if (this.wrong == 1) {
					message = "Good job - you got "+this.right+" right!";
				} else if (this.wrong == 0) {
					message = "Wow - Very well done! You got them all right! Please go to your checksheet and tick this Quiz!";
				} else if (this.wrong == this.length()) {
					message = "Oh no - you got "+this.right+" correct answers. Flunk";
				} else if (this.wrong == this.length() - 1) {
					message = "Oh my - you only got "+this.right+" correct.";
				} else {
					message = "Ok - you got "+this.right+" correct.";
				}
				$(this.quiz_div).innerHTML = message + "<br/><br/><br/>Wrong Answers: "+this.wrong + "&nbsp;&nbsp;&nbsp;&nbsp;Right Answers: "+this.right;
			}
			else if(this.getAttribute("type")=="yn" && this.getAttribute("right")=="")
			{
				$(this.quiz_div).innerHTML = "If you answered Yes to any of the quiz questions, then this seminar is for you. <a href='webreg.php' target='_blank'>Sign-Up</a>";
			}
			$("quiz_button").style.display = "none";
		}
	}
} // next


