// Copyright 2005 by Lexiteria LLC. 
//for more information please contact sales@lexiteria.com

		//number of questions - to be counted later
		var numQ = 0

		//array to hold question IDs
		var Question = new Array()

		//records the current question
		var curQ = 0
		var score = 0
		var Qright = 0

		//after the window has loaded I enumerate the questions and 
		var quizchainonload = window.onload;
window.onload = function() {setupQ(); if(quizchainonload)  quizchainonload();}

		function setupQ() {
			var ranQ = 0
			var temp = 0

			var QuestionString = ''

			var ulList=document.getElementById('quizText').getElementsByTagName('div');
			var ulListLen = ulList.length;

			//count the number of questions
			for(i=0; i<ulListLen; i++){
				if (ulList.item(i).id!='') {
					numQ++;
					if (i>0) {
						QuestionString = QuestionString + ',' + ulList.item(i).id;
					}else{
						QuestionString = ulList.item(i).id;
					}
				}
			}
			
			//used to hold the order for the questions
			Question = QuestionString.split(',');

			//hide questions
			for(var i = 0; i <= numQ-1; i++) {
				oldQ = document.getElementById(Question[i]);
				oldQ.style.display = 'none';
			}

			//randomize questions
			for(var e=0; e<5; e++) {
				for(var i=0; i<numQ-1; i++) {
					ranQ= Math.round(Math.random()*(numQ-1));
					temp = Question[i];
					Question[i] = Question[ranQ];
					Question[ranQ] = temp;
				}
			}
			showQ();

		}

		function showQ() {
			//Remove old Question
			if (curQ >0) {
				var oldQ = document.getElementById(Question[curQ-1]);
				oldQ.style.display = 'none';
			}
			//find next question
			curQ++;
			var newQ = document.getElementById(Question[curQ-1]);
			//display question information
			var responseMessage = document.getElementById('quizMsg');
			responseMessage.innerHTML = '<span style="font-family: Verdana,Arial, sams serif; font-weight:bold;">Question number </span>' + curQ + '<span style="font-family: Verdana,Arial,sams serif; font-weight:bold;"> of </span>' + numQ;

			//show question
			newQ.style.display = 'block';
			if (window.Event) {document.captureEvents(Event.CLICK);} {
				newQ.onclick = grade;	
			}
		}

		//provide feedback for clicked answer
		function grade(e) {
			this.onclick=null;
			
			//find the class of the item clicked
			var targetClassName=null
			if (!e) {							//if IE
				targetClassName=event.srcElement.className;	
			}else{								//if Mozilla
				targetClassName=e.target.attributes[0].value;
			}
			//if the user was correct
			if (targetClassName == "correct") {
				score++;
				Qright++;
				displayGrade(1);
			}else{
				score--;
				displayGrade(0);
			}
		}

		function displayGrade(edsCorrect) {
			var gradeMessage = document.getElementById('msgBox');
			var ClassName=null
			
			//change background-color of correct and incorrect Class
			var div = document.getElementById(Question[curQ-1]);
			var ul = div.getElementsByTagName('ul');
			var liList = ul.item(0).childNodes;
			var liListLen = liList.length;
			for(i=0; i<liListLen; i++){
				ClassName=ul.item(0).childNodes[i].className;	
				if (ClassName=='correct') {
					liList.item(i).style.color="green";
				}else if (ClassName=='incorrect'){
					liList.item(i).style.color="red";
				}
			}
			
			var newPara = document.createElement('p');
			newPara.style.fontWeight="bold";
			newPara.style.fontFamily="Arial,sans serif";
			
			if (edsCorrect == 1) {
				newPara.setAttribute("id", "goodGrade");
				var newParaText = document.createTextNode('---Right! Nice going!');
				gradeMessage.style.background="#96C891";
				gradeMessage.style.marginTop="15px";
			}else{
				newPara.setAttribute("id", "badGrade");
				var newParaText = document.createTextNode('---Sorry! Try another!');
				gradeMessage.style.background="#DCAAB4";
				gradeMessage.style.marginTop="15px";
			}
			
			newPara.appendChild(newParaText);
			
			//Mozilla correctly verticly centers the text IE does not, so I add a return to IE
			if (navigator.appName == "Microsoft Internet Explorer") {
				var theBR = document.createElement('br');
				gradeMessage.appendChild(theBR);
			}
				
			gradeMessage.appendChild(newPara);
			gradeMessage.style.display = 'block';

			var scoreMessage = document.getElementById('scoreMsg');
			scoreMessage.innerHTML = '<span style="font-family: Verdana,Arial, sams serif; font-weight:bold;">Your Score:</span> ' + Math.round((Qright/curQ)*100) + ' %';
			setTimeout('hideGrade()', 2000);
		}

		function hideGrade() {
			var gradeMessage = document.getElementById('msgBox');
			gradeMessage.style.display = 'none';
			gradeMessage.innerHTML = 'Miss Spelling says';
		
			if (curQ < numQ) {
				showQ();
			}else{
				var newPara = document.createElement('p');
				newPara.setAttribute("id", "goodGrade");
				newPara.style.fontWeight="bold";
				var newParaText = document.createTextNode('Great! You finshed the Bee!');
				newPara.appendChild(newParaText);
				//Mozilla correctly verticly centers the text IE does not, so I add a return to IE
				if (navigator.appName == "Microsoft Internet Explorer") {
					var theBR = document.createElement('br');
					gradeMessage.appendChild(theBR);
				}
				
				gradeMessage.appendChild(newPara);
				gradeMessage.style.display = 'block';
				
				for(var i = 0; i <= numQ-1; i++) {
					oldQ = document.getElementById(Question[i]);
					oldQ.style.display = 'block';
				}
			}
		}
