
var textlist=[];
	var choicelist=[];
	var feedbacklist=[];
	var correctlist=[];
	var correctlist2=[];
	var textlist2=[];

	var choicelist2=[];
	var feedbacklist2=[];
	var correctlist2=[];
		var imagelist=[];
		var imagelist2=[];
	var audiolist=[];
	var audiolist2=[];
	var instructions="";
	var scriptlist=[];
	var scriptlist2=[];
	var score=0;
	var questionnumber=0;
	
	var questionanswered=0;
	var answered=0;
	var correct=0;
	var audiofile=""
	var numberofquestions=25;

	function restart(){
		 choicelist=[];
	feedbacklist=[];
	 correctlist=[];
 correctlist2=[];
	 textlist2=[];

	 choicelist2=[];
	 feedbacklist2=[];
	 correctlist2=[];
		 imagelist=[];
		 imagelist2=[];
	 audiolist=[];
	 audiolist2=[];
	 instructions="";
 scriptlist=[];
	 scriptlist2=[];
	 score=0;
 questionnumber=0;
	
	 questionanswered=0;
	 answered=0;
	 correct=0;
	 testSetUp();
	}
	
	function reset(){
	
		for (i=0;i<gaplist.length;i++)
		{
		var z="q"+i;
		
		document.getElementById(z).value ='';
		}
		clearScores();
	}
	function testSetUp(){
		
	var txt="";
	 answers = [];

 useranswers = [];
		
	
	
	
	
	if (window.XMLHttpRequest)
	  {
	  xhttp=new XMLHttpRequest();
	  }
	else
	  {
	  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	
	xhttp.open("GET",testfile,false);
	xhttp.send("");
	xmlDoc=xhttp.responseXML;
	

var rand=xmlDoc.getElementsByTagName("random")[0].childNodes[0].nodeValue;
	instructions=xmlDoc.getElementsByTagName("instructions")[0].childNodes[0].nodeValue;

	
	var x=xmlDoc.getElementsByTagName("item");
	
	var qnum=0;

 for (i=0;i<x.length;i++)
 {
 textlist.push(x[i].getElementsByTagName("text")[0].childNodes[0].nodeValue)  ;	
  audiolist.push(x[i].getElementsByTagName("audio")[0].childNodes[0].nodeValue)  ;	
   imagelist.push(x[i].getElementsByTagName("image")[0].childNodes[0].nodeValue)  ;	
   //scriptlist.push(x[i].getElementsByTagName("script")[0].childNodes[0].nodeValue)  ;	
    for (j=0;j<3;j++)
	{
   
  choicelist.push(x[i].getElementsByTagName("choice")[j].childNodes[0].nodeValue)  ;

  var feedback=x[i].getElementsByTagName("choice")[j].getAttribute("feedback");
				
feedbacklist.push(feedback);
 var correct=x[i].getElementsByTagName("choice")[j].getAttribute("correct");
				
correctlist.push(correct);
 }
  
  
  
 txt=txt+feedbacklist[i];
 }
		
	
randomquestions();




}
function randomquestions(){
	
	var i = 0;
	var totalquestions = textlist.length;
	
	var questions = totalquestions;
	 numberofchoices=3;
	while (i<totalquestions) {
		var	x = Math.floor(Math.random()*(questions));
		var	nextquestion = textlist[x];
		var	nextImage=imagelist[x];
		var	nextaudio=audiolist[x];
		var nextscript=scriptlist[x];
		
		var r=Math.floor(Math.random()*numberofchoices)
		
		
		if (r==0){
			choice1=choicelist[x*numberofchoices];
			choice2=choicelist[x*numberofchoices+1];
			choice3=choicelist[x*numberofchoices+2];
			
			
			
			feedback1=feedbacklist[x*numberofchoices]
			feedback2=feedbacklist[x*numberofchoices+1];
			feedback3=feedbacklist[x*numberofchoices+2];
			
			
			
			correctitem=choice1;
		}
		if (r==1){
			choice1=choicelist[x*numberofchoices+1];
			choice2=choicelist[x*numberofchoices];
			choice3=choicelist[x*numberofchoices+2];
			
		
			
			feedback1=feedbacklist[x*numberofchoices+1];
			feedback2=feedbacklist[x*numberofchoices];
			feedback3=feedbacklist[x*numberofchoices+2];
			
			
		}
		if (r==2){
			choice1=choicelist[x*numberofchoices+1];
			choice2=choicelist[x*numberofchoices+2];
			choice3=choicelist[x*numberofchoices];
			
			
		
			feedback1=feedbacklist[x*numberofchoices+1];
			feedback2=feedbacklist[x*numberofchoices+2];
			feedback3=feedbacklist[x*numberofchoices];
			
			
		}
		
			
		imagelist2.push(nextImage);
		textlist2.push(nextquestion);
		scriptlist2.push(nextscript);
		choicelist2.push(choice1);
		choicelist2.push(choice2);
		choicelist2.push(choice3);
		
		audiolist2.push(nextaudio);
		
		
		feedbacklist2.push(feedback1);
		feedbacklist2.push(feedback2);
		feedbacklist2.push(feedback3);
		
	
		textlist.splice(x, 1);
		audiolist.splice(x, 1);
		scriptlist.splice(x, 1);
		feedbacklist.splice(x*numberofchoices, numberofchoices);
		choicelist.splice(x*numberofchoices, numberofchoices);
		imagelist.splice(x,1);
		
		i++;
		var	questions = questions-1;
	}
	
	document.getElementById("text").innerHTML="test";
	showQuestion();
}

function loadQuestion(){
	questionnumber++
	 showQuestion();
	
}

function showQuestion(){
	
	
	
document.getElementById("instructions").innerHTML= "There are "+numberofquestions+ " questions in this quiz. "+instructions  ;	

	document.getElementById("nextbutton").innerHTML ="";
questionanswered=0;	
txt=""

txt=txt +'<p class="question">' + (questionnumber+1) + '&nbsp;&nbsp; <span id="result_' + questionnumber + '"><img src="blank.gif" style="border:0" alt="" /></span><span id="feedback"></span></p>';
	txt=txt + textlist2[questionnumber]+"<p></p><form id='form1' nme='form1'><div id='choices'>";
	

	
	 for(j=0;j<numberofchoices;) {
     
		
		txt=txt+ '<input type="radio" value="'+j+'" name="choices" onClick="submitAnswer('+questionnumber+','+j+')"><label id="label_' + questionnumber + '_' + j + '" for="answer_' + questionnumber + '_' + j + '"><span id="'+questionnumber + '_' + j + '">&nbsp;<a   class="achoice" onClick="submitAnswer('+questionnumber+','+j+')">'  + choicelist2[(3*questionnumber)+j] + '</a><span></label> <br>';
		
		j++;

				 }
				  txt=txt+'</form>';

	 
document.getElementById("score").innerHTML='<span id="score">&nbsp;&nbsp;&nbsp;Score: '+score+ '/'+questionnumber +'</span>';
	
	document.getElementById("text").innerHTML=txt;
	
	
	
	
	
}
function setCheckedValue(radioObj, newValue) {
	
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function submitAnswer(id,labelId) {

	
	setCheckedValue(document.forms['form1'].elements['choices'], labelId);
	
		useranswers[id] = choicelist2[(3*(id)+(1*labelId))];	
	var x =answers[id] ;
	var y = useranswers[id];
	var last_char=labelId;
	
	var feedback3=feedbacklist2[(id*3)+1*last_char];
			
	
	
	
	if (feedback3=="Correct"){
		
	feedback3="  " +"<span id='green'>"+feedback3+"</span>";
	 document.getElementById("feedback").innerHTML = feedback3;
		
	document.getElementById('result_' + id).innerHTML = '<img src="correct.gif" style="border:0" alt="Correct!" />';

	if (questionanswered==0){
			
		score++;
		
	
		
		answered++;
		
		
		
	
	}
		
		if (questionnumber<numberofquestions-1){
		
		document.getElementById("nextbutton").innerHTML ='<input type="submit" onClick="javascript:loadQuestion()" name="next" id="next" value="Next" />';
	
		//questionnumber++;
		
		}else{
			document.getElementById("nextbutton").innerHTML ='<input type="submit" onClick="javascript:showResults()" name="next" id="next" value="Finish" />';
			
		}
	
	
	}
	else
	{
		if (questionanswered==0){
		
		answered++;
	}
		 document.getElementById('result_' + id).innerHTML = '<img src="incorrect.gif" style="border:0" alt="Incorrect!" />';
		feedback3="  " +"<span id='red'>"+feedback3+"</span>";
		
		
	}
 
 
	
  document.getElementById("score").innerHTML = ' &nbsp;&nbsp;&nbsp;Score: ' + score + '/' + (id+1) ; 
   document.getElementById("feedback").innerHTML = feedback3;
	questionanswered=1;
  
}
function showResults(){
document.getElementById("instructions").innerHTML="";
document.getElementById("scriptbutton").innerHTML ="";
txt="";
	txt=txt + "<div id='results'>";
	
if ((score/numberofquestions)>0.5){
	resultsmessage="<p>Congratulations! You have completed the test.</p><p> Do you wish to start again?</p>";
}else{
	resultsmessage="<p>You have completed the test.</p><p> Do you wish to start again?</p>";
}
	

     
		txt=txt+ '<label id="results"><p>'  + resultsmessage + '</label><p><span id="score">Score: '+score+ '/'+(1*(questionnumber+1)) +'</span></p>';
		
				 
	

	
	
	document.getElementById("text").innerHTML=txt;
	document.getElementById("nextbutton").innerHTML ='<input type="submit" onClick="javascript:restart()" name="restart" id="restart" value="Restart" />';
}
function beep(){
	debug.log("beep");
	navigator.notification.beep(1);
}
function vibrate(){
	debug.log("vibrate");
	navigator.notification.vibrate(0);
}

function showScript()
{
	var newwindow;
	if (questionanswered==0){
	var url="scripts/"+scriptlist2[questionnumber];
	}else{
	var url="scripts/"+scriptlist2[questionnumber];	
	}

	newwindow=window.open(url,'name','height=300,width=400,resizable=yes,scrollbars=yes');
	if (window.focus) 
	{
		newwindow.focus();
		}
}

