var objXmlDOM;
var questionArray; 
var answerArray = new Array(); 
var userChoicesArray = new Array(); 
var count = 0; 
var userChoice = 0; 
var radioIndex = -1; 
var xmlObj;
var xmlfilename;

isIE=document.all;
isNN=!document.all&&document.getElementById;
isN4=document.layers;

function hide(){
  browser=isIE ? document.all.TriviaLayer : document.getElementById("TriviaLayer");
  if (isIE||isNN) browser.style.visibility="hidden";
  else if (isN4) document.TriviaLayer.visibility="hide";

  //This did not work
  answerArray = new Array(); 
  userChoicesArray = new Array(); 
  count = 0; 
  userChoice = 0; 
  radioIndex = -1; 

  initScreen = "<center><font face='Trebuchet MS' color='#0407F8' size='5'>";
  initScreen += "<b>Welcome to the Geography trivia challenge<b></font><br><br><br>";
  initScreen += "<input type=button name=startBtn value='Challenge me!' onClick='init2()'></center>";
  QuizChoices.innerHTML = initScreen;
  
  //location.href = "mgtHome.php" ;
}

function showMe(){
  browser=isIE ? document.all.TriviaLayer : document.getElementById("TriviaLayer");
  if (isIE||isNN) browser.style.visibility="visible";
  else if (isN4) document.TriviaLayer.visibility="show";
}

function init2(){

  init("geotrivia");

}
function init(filename){

xmlfilename = filename;
if (window.ActiveXObject)
{
  objXmlDOM =new ActiveXObject("Microsoft.XMLDOM");
  objXmlDOM.async="false";
  objXmlDOM.onreadystatechange=verify;

  objXmlDOM.load(xmlfilename + ".xml");
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
  //alert('firefox');
  objXmlDOM =document.implementation.createDocument("","",null);
  objXmlDOM.async=false;
  objXmlDOM.load(xmlfilename + ".xml");
  
}
else
{
  alert('Your browser cannot handle this script');
}   
 
//if (document.implementation && document.implementation.createDocument){
	//alert("Starting the TriviaTreat Trivia Challenge...");
//}
xmlObj=objXmlDOM.documentElement;

questionArray = xmlObj.getElementsByTagName("question");

for(i=0;i<questionArray.length; i++){
   answerArray[i] = 0; 
   userChoicesArray[i] = -1; 
}

if(count < questionArray.length) {
   parseXML();
}
    
}


function verify()
{
	if (objXmlDOM.readyState != 4)
	{
		return false;
	}
}

function showAnswer() {
    	verifyAnswer();
	var result;
	var score;
	var correctAns;
	
	var moreInfo = "";
	if(questionArray[count].getElementsByTagName("moreinfo")[0].hasChildNodes())
		moreInfo = questionArray[count].getElementsByTagName("moreinfo")[0].firstChild.nodeValue;
	else
		moreInfo = '';
	
	if (userChoice == document.frm.ansNo.value)
      		result = "You answered correctly. " + moreInfo;
	else{
		if(userChoice == 0)
			result = "You did not answer.";
		else
      			result = "You answered incorrectly.";

		var answer = questionArray[count].getElementsByTagName("answer")[0].firstChild.nodeValue;
		//alert("correct asnwer " + answer);
		var choices = questionArray[count].getElementsByTagName("choices");
		var corrChoice = choices[0].getElementsByTagName("choice")[answer-1].firstChild.nodeValue;

		result += "<br><br>The correct answer is " + corrChoice + ". " + moreInfo;

	}

    strOut  = "<table border=0 align=center width=80%>";
    
    strOut += "<tr><td colspan=2>&nbsp;</td></tr>";
    strOut += "<tr><td colspan=2>&nbsp;</td></tr>";

    rights = 0;
    verifyAnswer();

    for(i=0;i<answerArray.length;i++){
      if(answerArray[i] == 1)
      rights++;
    }
    score = "";
    score += "Your score: " + rights;
    score += " out of " + answerArray.length + "";

    strOut += "<tr><td colspan=2><b><font face='Georgia, Arial,Helvetica' color='#0407F8' size='4'>"+result+"</font></b>";            
    strOut += "</td></tr>";
    strOut += "<tr><td colspan=2>&nbsp;</td></tr>";
    strOut += "<tr><td colspan=2><b><font face='Georgia, Arial,Helvetica' color='#0407F8' size='4'>"+score+"</font></b>";
    strOut += "</td></tr>";
    strOut += "<tr><td colspan=2>&nbsp;</td></tr>";
    strOut += "<tr><td colspan=2>";

    if(count < questionArray.length-1){
      strOut += " <input type=button value='Next'";
      strOut += " onClick='getNextQ()'>";			
    }else if(count == questionArray.length-1){
      strOut += " <input type=button value='Finish'";
      strOut += " onClick='showFinalScore()'>";			
    }

    strOut += "</td></tr></table>";

    QuizChoices.innerHTML = strOut;
}

  function getNextQ() {
    count++;
    parseXML();
  }

  function parseXML(){

	var imageName;
	temp = questionArray[count].getElementsByTagName("questiontext")[0].firstChild.nodeValue;
	//alert("getting image " + questionArray[count].getElementsByTagName("questiontype")[0].hasChildNodes());
	if(questionArray[count].getElementsByTagName("image")[0].hasChildNodes()){
		imageName = questionArray[count].getElementsByTagName("image")[0].firstChild.nodeValue;
		//alert("getting image name " + imageName);
	}
	else
		imageName = '';
	
	//alert("imageName " + imageName);
    	strOut  = "<table border=0 width=100%>";	
	strOut += "<tr>";
	strOut += "<td>";
	strOut += "<table border=0 width=100%>";
	strOut += "<tr><td align='center' width=15%><img border='0' src='images/Q"+(count+1)+".gif'></td>";
	strOut += "<td colspan=2><b><font face='Georgia, Arial,Helvetica' color='#0407F8' size='4'>"+temp+"</b></font>";
	strOut += "</td></tr>";
	strOut += "<tr><td colspan=3>&nbsp;</td></tr>";
	Nodes = questionArray[count].getElementsByTagName("choices");

	var qtype = questionArray[count].getElementsByTagName("questiontype")[0].firstChild.nodeValue;
	if(qtype == 'TF')
		numQuestions = 2;
	else
		numQuestions = 4;
	for(i=0;i<numQuestions;i++){
		var choice = Nodes[0].getElementsByTagName("choice")[i].firstChild.nodeValue;
		strOut += "<tr><td>&nbsp;</td><td align=center width=10%>";
		strOut += "<input type=radio name=ansUsr ";
		strOut += " onClick='userChoice=" + (i+1);
		strOut += ";radioIndex=" + i + "' ";
		strOut += "value=" + (i+1) + "></td><td><b><font face='Georgia, Arial,Helvetica' color='#0407F8' size='3'>";
		strOut +=  choice + "</font></b></td></tr>";		
	}

	temp = questionArray[count].getElementsByTagName("answer")[0].firstChild.nodeValue;
	document.frm.ansNo.value = temp;

	strOut += "<tr><td colspan=3>&nbsp;</td></tr>";
	strOut += "<tr><td>&nbsp;</td><td colspan=2>";

	if(count <= questionArray.length-1){
		strOut += " <input type=button value='Check my answer'";
		strOut += " onClick='showAnswer()'>";			
	}

	strOut += "</td></tr></table>";
	strOut += "</td>";
	strOut += "<td>";

	//imageName = "HongKong.jpg";
	//alert("image : " + imageName);
	if(imageName == '')
		strOut += "&nbsp;";
	else
		strOut += "<img border='0' src='qimages/"+imageName+"'>";
	strOut += "</td>";
	strOut += "</tr></table>";


    	QuizChoices.innerHTML = strOut;

    	userChoice = 0;
    	radioIndex = -1;

    	
    
  }

  function verifyAnswer(){
    userChoicesArray[count] = radioIndex;

    if (userChoice == document.frm.ansNo.value)
      answerArray[count] = 1;
    else
      answerArray[count] = 0;
  }

function showFinalScore() {
    rights = 0;

    verifyAnswer();

    for(i=0;i<answerArray.length;i++){
      if(answerArray[i] == 1)
      rights++;
    }
    strRes = "";
    strRes += "Score: " + rights;
    strRes += " out of " + answerArray.length + "";
    strRes += "<br><br> Thank you for playing TriviaTreats.";

    strOut  = "<table border=0 align=center width=80%>";
    strOut += "<tr><td colspan=2>&nbsp;</td></tr>";
    strOut += "<tr><td colspan=2><b><font face='Georgia, Arial,Helvetica' color='#0407F8' size='4'>"+strRes+"</font></b>";   
    strOut += "</td></tr>";
    strOut += "<tr><td colspan=2>&nbsp;</td></tr>";

    strOut += "<tr><td colspan=2>&nbsp;</td></tr>";
    strOut += "<tr><td colspan=2>";

    strOut += "</td></tr></table>";

    QuizChoices.innerHTML = strOut;

  }

  


