/**
 *
 * LCMS and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to champion open systems.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 champion open systems (http://www.chaosys.ch) All rights reserved.
 *
 */
/** 
* Load labels.
*/
function loadQuestions(){
	setTimeout("parseQuestions()",500);
}
function parseQuestions(){
	questionText=document.createTextNode(questionXml.getElementsByTagName("question")[0].firstChild.data);
	document.getElementById("questionContainer").appendChild(questionText);
	
	questions = questionXml.getElementsByTagName("question-element");	
	var solutionImage="";
	for (i=0; i< questions.length; i++){
	    answerText = document.createTextNode(questions[i].getElementsByTagName("answer")[0].firstChild.data);
        questionCorrect = questions[i].getElementsByTagName("correct")[0].firstChild.data;
        questionImage = questions[i].getElementsByTagName("image")[0].firstChild.data;
        questionImageAbsolute=webappHost+"/questions/images/"+questionImage+".tif.jpg";
        if (questionCorrect=="correct")
        {
        	solutionImage=questionImageAbsolute;
        }
		inputRadio=document.createElement("input");
	   	inputRadio.type="radio";
	   	inputRadio.name="q1";
	   	addOnClickListener(inputRadio, questionImageAbsolute, questionCorrect);
	   	
	    questionParagraph = document.createElement("p");
        questionParagraph.className = "question";
        
		questionParagraph.appendChild(inputRadio);
		questionParagraph.appendChild(answerText);
		document.getElementById("questionContainer").appendChild(questionParagraph);
	}
	if(questions.length > 0){
	    inputButton = document.createElement("input");
	   	inputButton.type = "button";
	   	inputButton.value  = "check";
	   	inputButton.onclick = function(evt){
			document.getElementById("solutionImageSolution").src = solutionImage;
			document.getElementById("solutionImageSolution").style.visibility = "visible";
			textElement = document.getElementById("confirmationText");
			if(testresult == "correct"){
				textElement.firstChild.data = "Congratulations! Your answer is correct.";
			}
			else{
				textElement.firstChild.data  = "Sorry, your answer is not correct!";
			}
			textElement.style.visibility="visible";
        }
		document.getElementById("questionContainer").appendChild(inputButton);
		
	}
}
function addOnClickListener(input, image, correct){
	   	input.onclick = function(evt){
			document.getElementById("solutionImage").src=image; 
			document.getElementById("solutionImage").style.visibility = "visible";
			testresult=correct;	
        }
}
function removeAllQuestions(){
	document.getElementById("confirmationText").style.visibility = "hidden";
	document.getElementById('solutionImageSolution').src="";
	document.getElementById('solutionImageSolution').style.visibility = "hidden";
	document.getElementById("solutionImage").src="";
	document.getElementById("solutionImage").style.visibility = "hidden";
	element = document.getElementById("questionContainer");
	while(element.childNodes.length > 0){
		element.removeChild(element.firstChild);
	}
}
