// JavaScript Document
var theDoc;
var theStylesheet;
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;
function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 								= "an unknown operating system";
	}
}
function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function setTheStylesheet(arg){
	theStylesheet = arg;
	//alert(theStylesheet);
}

function setTheDoc(arg){
	// use either setTheDoc('path to xml doc')
	// or
	// pass a query to the page, ie ?xmldoc.xml
	theDoc = (arg) ? arg : '../common/xml/'+location.search.substring(1);
}

function loadXML(_url_, handler, arg){
	/*
	if(document.implementation && document.implementation.createDocument){
		var xmldoc = document.implementation.createDocument("","",null);
		xmldoc.onload = function(){ handler(xmldoc, url, arg); }
		xmldoc.load(url);
	}
	else if(window.ActiveXObject){
		var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
		xmldoc.onreadystatechange = function(){
			if(xmldoc.readyState == 4) handler(xmldoc, url, arg);
		}
		xmldoc.load(url);
	}*/
	//alert(xmldoc);

	$.ajax({
		url: _url_,
		dataType: 'xml',
		type:'GET',
		success: function(a, b, c){handler(a);},
		data : "test=test"
	});
}

function getArgs(){
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split("&");
	for(var i = 0; i < pairs.length; i++){
		var pos = pairs[i].indexOf('=');
		if(pos == -1) continue;
			var argname = pairs[i].substring(0, pos);
		if(argname.length == 2){
			var argname = i;
		}
		else{
		}
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
	}
	args["cnt"] = pairs.length;
	return args;
}

function checkForCorrectness(correct, entered){
	var Ccnt = correct.length;
	var Ecnt = entered.length;
	var continueRunning = true;
	var correctCount = 0;
	
	if(Ccnt != Ecnt){
		alert('fail');
		continueRunning = false
	}
	if(continueRunning){
		for(var i = 0; i < Ccnt; i++){
			for(var j = 0; j < Ecnt; j++){
				if(correct[i] == entered[j]){
					correctCount++;
				}					
			}
		}
	}
	
	alert("correctCount = " + correctCount + ' - ' + "Ecnt = " + Ecnt);
	
	if(correctCount <= Ecnt){
		alert(correctCount);
	}
}
function decode(str) {
	if(str == null){
		return '';
	}else{
		return unescape(str.replace(/\+/g, " "));
	}
}

function checkCookie(cookie, checkfor){
	cookie = cookie.split(";");
	for(var i = 0; i < cookie.length; i++){
		if(cookie[i] == checkfor){
			return false;
		}
	}
	return true;
}

function doDebug(arg){
	if(toDebug){
		if(document.getElementById("debug") == null){
			var div = document.createElement("div");
			div.setAttribute("id", "debug");
			div.setAttribute("style", "border:1px solid #000; position:absolute; top:0; left:0;width:300px; height:400px; overflow:auto;");
			
			document.body.appendChild(div);
		}
		else{
			var div = document.getElementById("debug");
		}
		
		div.appendChild(document.createTextNode(arg));
		var br = document.createElement("br");
		div.appendChild(br);
	}
}

function loadTestQuestion(xmldoc, url, arg){
	
	//doDebug("begin loadTestQuestion");
	var quiz 		= xmldoc.getElementsByTagName("quiz")[0];
	//doDebug("quiz is set");
	var qaCount 	= quiz.getElementsByTagName("qa").length;
	//doDebug("qaCount is set");
	
	var randomTest;
	var goForward 	= false;
	var i = 0;
	var allTestTaken = false;
	var didNotPassAll = true;
	
	var question = xmldoc.getElementsByTagName("q");
	//doDebug("question is set");
	var answer = xmldoc.getElementsByTagName("a");
	//doDebug("answer is set");
		
	//	start arg.PassedTest == null
	//	check in PassedTest cookie is set, if not we set it to nothing
	if(arg.PassedTest == null) arg.PassedTest  = ""; arg.store();
	//	end arg.PassedTest == null
		
	//	start arg.TestTaken == null
	//	check in TestTaken cookie is set, if not we set it to nothing
	if(arg.TestTaken == null) arg.TestTaken  = ""; arg.store();
	//	end arg.TestTaken == null

/*	
	var TheArray = location.search.substring(1).split("&");
	var subArray = TheArray[0].split("=");
	var oldHidden = subArray[1];
	*/
	var args = getArgs();
	//doDebug("args is set");
	
	var queryCnt = location.search.substring(1).length;
	//doDebug("queryCnt is set");
		
//==================================================
//	start if(queryCnt != 0){
//	check in the query string in not equal to 0
//	other words, check if there is a querystring
//==================================================	
	if(queryCnt != 0){
	//doDebug("start queryCnt != 0");
		//	get the count of how many answers are associated with what is passed
		//	in the args.currentHidden that is from the test just taken
		var cnt = answer[args.currentHidden].getElementsByTagName("c").length;
		
		//	putting the answers into a variable for easier work
		var currentAs =answer[args.currentHidden].getElementsByTagName("c");
		
		// create two arrays that will be used in getting the correct answers
		var rightAnswersSingle = Array();
		var rightAnswers = Array();
		
		// create an array for getting the entered answers for the query string
		var enteredAnswers = Array();
		
		// this variable with be set to true if answer is correct for radio button
		//	and if all checkboxes are answered correctly
		var passedQuestion = false;
		
		//	to be used in the for loop
		var o = 0;
		
		// start loop thru the total possible answers from the current question
		doDebug("start for(var i =0; i < cnt; i++){");
		for(var i =0; i < cnt; i++){
			
			//	start currentAs[i].getAttribute("val") == 1
			//	check if the val attribute in the answer equals 1
			if(currentAs[i].getAttribute("val") == 1){
				//	currently for only the radio button answers/question
				//	maybe need to be recoded for better use
				rightAnswersSingle[99] = currentAs[i].firstChild.data;
			}
			//	end currentAs[i].getAttribute("val") == 1
			
			//	this will be used in multiple possible answers
			rightAnswers[o++] = currentAs[i].firstChild.data;
		}
		//doDebug("end for(var i =0; i < cnt; i++){");
		// end loop thru the total possible answers from the current question
		
		//	start args.hiddenGUI == 'rb'
		//	check if the question was a radio button question
		if(args.hiddenGUI == 'rb'){
		//doDebug("start if(args.hiddenGUI == 'rb'){");
			
			//	start rightAnswersSingle[99] == decode(args.q)
			//	check if our single answer equals our single right answer
			if(rightAnswersSingle[99] == decode(args.q)){
				//	 answer was correct
				passedQuestion = true;
			}
			//	end rightAnswersSingle[99] == decode(args.q)
		//doDebug("end if(args.hiddenGUI == 'rb'){");
		}
		//	end args.hiddenGUI == 'rb'
		//	start else args.hiddenGUI == 'rb'
		//	this will be for multiple answer questions
		else{
		//doDebug("start else args.hiddenGUI == 'rb'");
			
			//	variable for keeping track of correct answers
			var rigthCnt = 0;
			
			//	start for loop to go thru all the possible answers
			for(var i = 0; i < rightAnswers.length; i++){
				
				//	start args[i] != null
				//	this is to check if there is an answer for the variable
				if(args[i] != null){
					
					//	start rightAnswers[i] == decode(args[i])
					//	this is to compare the entered answer to the correct answer
					if(rightAnswers[i] == decode(args[i])){
						//	this is our variable and is incremented for each
						//	correct answer
						rigthCnt++;
					}
					//	end rightAnswers[i] == decode(args[i])
				}
				//	end args[i] != null
			}
			//	end for loop to go thru all the possible answers
			
			//	start rigthCnt == (args.cnt - 2)
			//	this is to compare the right amount answers to
			//	to the answers in the query string
			//	the - 2 is to subtract the two variables that are not
			//	answers
			if(rigthCnt == (args.cnt - 2) && args.cnt > 2){
				//	if things match up we set passedQuestion true
				passedQuestion = true;	
			}
			//	end rigthCnt == (args.cnt - 2)
		//doDebug("end else args.hiddenGUI == 'rb'");
		}
		//	end else args.hiddenGUI == 'rb'
		
		//	start passedQuestion == true
		//	Here we check if they passed the total question
		if(passedQuestion){
		doDebug("start if(passedQuestion){");
			if(checkCookie(arg.PassedTest, args.currentHidden)){
				//	here we set the cookie to the value of the question number
				//	and the already passed questions or blank cookie for the  
				//	first question
				if(arg.PassedTest.length != 0){
					arg.PassedTest =";" + arg.PassedTest;
				}
				arg.PassedTest = args.currentHidden + arg.PassedTest;
				arg.store();
			}
			
		//doDebug("end if(passedQuestion){");
		}
		//	end passedQuestion == true
	
		if(checkCookie(arg.TestTaken, args.currentHidden)){
			if(arg.TestTaken.length != 0) arg.TestTaken = ";" + arg.TestTaken;
			arg.TestTaken = args.currentHidden + arg.TestTaken;
			 arg.store();
		}
		//alert(randomTest + " + " + qaCount);
	//doDebug("end queryCnt != 0");
	}

	var tmp = arg.TestTaken.split(";");
	var pmp = arg.PassedTest.split(";");
	testNumber = (arg.TestTaken.length == 0) ? 1 : (tmp.length + 1);
	//doDebug("------------------------------");
	//doDebug("TestTaken: " + tmp.sort() + " - " + arg.TestTaken.length + " : " + tmp.length );
	//doDebug("------------------------------");
	
	// 	lets find out what test was taken and get a random test 
	//	and compare the two
	do{
	//doDebug("start }while(!goForward);" + goForward);
	//doDebug(tmp.length + " == " + qaCount + " ->" + goForward);
		//alert(tmp.length + " == " + qaCount);
		if(tmp.length >= qaCount){
			//doDebug("start tmp.length == qaCount" + tmp.length + "==" + qaCount);
			
			allTestTaken = true;
			goForward = true;
			didNotPassAll = compareArray(arg.PassedTest, arg.TestTaken, ";");
			
			//doDebug("end tmp.length == qaCount" + tmp.length + "==" + qaCount);
		}
		else if(arg.TestTaken){
			randomTest = randomNumber(qaCount);
			//alert(randomTest)
			if(checkArray(arg.TestTaken, randomTest, ";")){
				goForward = true;
			}		
		}
		else{
			randomTest = randomNumber(qaCount);
			goForward = true;
		}
	//doDebug("end }while(!goForward);" + goForward);
	}while(!goForward);
	
	
		var content = document.getElementById("content");

//==================================================
//	start if(!allTestTaken){
//	if all test were not taken, do this
//==================================================
	if(!allTestTaken){
		//==================================================
		//	set some variables
		//==================================================
		var numberedDiv = document.createElement("div");
		numberedDiv.setAttribute("id", "numbered");
		
		var theForm = document.createElement("form");
		theForm.setAttribute("method", "get");
		
		var currentPageName = document.location;
		theForm.setAttribute("action", currentPageName);
		
		var divQ	= document.createElement("div");
		divQ.setAttribute("id", "question");
		
		var divA	= document.createElement("div");
		divA.setAttribute("id", "answer");
		
		var divButton	= document.createElement("div");
		divButton.setAttribute("id", "button");
		

	if(browser == "Internet Explorer"){
		var subButton = document.createElement("div");
		subButton.innerHTML = "<input type='submit' value='next' class=\"btn\"/>";
	}
	else{
		var subButton = document.createElement("input");
		subButton.setAttribute("type", "submit");
		subButton.setAttribute("value", "next");
		subButton.setAttribute("class", "btn");
	}

		/*
		var resetButton = document.createElement("input");
		resetButton.setAttribute("type", "reset");
		resetButton.setAttribute("value", "reset");
		resetButton.setAttribute("class", "button");
		*/
		
		var hiddenCurrent = document.createElement("input");
		hiddenCurrent.setAttribute("type", "hidden");
		hiddenCurrent.setAttribute("name", "currentHidden");
		hiddenCurrent.setAttribute("value", randomTest);
		
		var quizTitle = quiz.getAttribute("title");
		var quizInstruction = quiz.getAttribute("instruction");
		
		var mycurrent_row, mycurrent_cell;
		
		divQ.innerHTML = question[randomTest].firstChild.data;
		
		var anwerLength = answer[randomTest].getElementsByTagName("c").length;
		var currentGui	= answer[randomTest].getAttribute("gui");
		
		var hiddenGUI = document.createElement("input");
		hiddenGUI.setAttribute("type", "hidden");
		hiddenGUI.setAttribute("name", "hiddenGUI");
		hiddenGUI.setAttribute("value", currentGui);
		//==================================================
		//	end: set some variables
		//==================================================

		//==================================================
		//	start to loop thru total answers that are in the
		//	xml file and build the input elements
		//==================================================
		for(var i = 0; i < anwerLength; i++){
			ans = answer[randomTest].getElementsByTagName("c")[i].firstChild.data;
			if(currentGui == "cb"){
				var name = "q"+ i;
			}
			else{
				var name = "q";
			}
			currenttext = document.createTextNode(ans);
			
			ans = createInput(name, ans, currentGui);			
			divA.appendChild(ans);
			divA.appendChild(currenttext);
			
			divA.appendChild(document.createElement("br"));
		}
		//==================================================
		//	end loop thru total answers....
		//==================================================
		
		
		//==================================================
		//	start build out all elements
		//==================================================
		var position = testNumber + " of " + qaCount;
		numberedDiv.appendChild(document.createTextNode(position));
		document.getElementById("container").appendChild(numberedDiv);
		theForm.appendChild(hiddenCurrent);
		theForm.appendChild(hiddenGUI);
		divButton.appendChild(subButton);
		divButton.appendChild(document.createTextNode(" "));
		//divButton.appendChild(resetButton);
		theForm.appendChild(divQ);
		theForm.appendChild(divA);
		theForm.appendChild(divButton);
		content.appendChild(theForm);
		//==================================================
		//	end build out all elements
		//==================================================
	}
//==================================================
//	end if(!allTestTaken){
//==================================================
	else if(allTestTaken && didNotPassAll){
		//doDebug("arg.PassedTest = " + arg.PassedTest);
		didNotPassAllFunction(xmldoc, url, arg.PassedTest);
	}
	else if(allTestTaken && !didNotPassAll){
		//alert("did pass all");
		var passMessage ="";
		 //passMessage += "<b>Congratulations </b><br/>";
		 passMessage += "Congratulations! You have successfully completed the Genie Aerial Platform Train the Trainer Quiz! To receive your course completion certificate click on the link below. ";
		content.innerHTML = passMessage;
	}

	// set cookie
	arg.store();

	doDebug("Current PassedTest:\t\t(" + pmp.length + ") " + arg.PassedTest);
	doDebug("Current TestTaken:\t\t(" + tmp.length + ") " + arg.TestTaken);
}

function didNotPassAllFunction(xmldoc, url, arg){
	setTheStylesheet('quizfinal.css');
	
	doDebug("start: didNotPassAllFunction();");
	
	var question 	= xmldoc.getElementsByTagName("q");
	var reason 		= xmldoc.getElementsByTagName("a");
	var missedQuestionArray = Array();
	
	
	var tempMessage = "Congratulations, you have completed Genie Aerial Platform Train the Trainer Quiz. Your answered {correct} out of {count} questions correctly. Here is additional information that will help you in retaking those questions you answered incorrectly. Read through these tips carefully and when finished, click the retake button to try these questions again. Good luck!";
	var tempLink	= "Try Again";
	
	var ul = document.createElement("ul");
	var li = document.createElement("li");
	
	//var ulP = document.createElement("ul");
	//var liP = document.createElement("li");
	
	
	doDebug("question.length: " + question.length);
	doDebug("arg = " + arg);
	doDebug("arg.length = " + arg.length);
	var wrongcnt = 0;
	var counter = 0;
	for(var i = 0; i < question.length; i++){
		if(checkArray(arg, i, ";")){
			var li = document.createElement("li");
			var blockquote = document.createElement("blockquote");
			blockquote.innerHTML = reason[i].getElementsByTagName("reason")[0].firstChild.data;
			li.innerHTML = question[i].firstChild.data;
			li.appendChild(blockquote);
			ul.appendChild(li);
			
			
			//var liP = document.createElement("li");
			//var blockquoteP = document.createElement("blockquote");
			//blockquoteP.innerHTML = reason[i].getElementsByTagName("reason")[0].firstChild.data;
			//liP.innerHTML = question[i].firstChild.data;
			//liP.appendChild(blockquoteP);
			//ulP.appendChild(liP);
			
			wrongcnt++;
		}
			counter++;
	}	
	

	wrongcnt = counter - wrongcnt;
	tempMessage = tempMessage.replace("{correct}", wrongcnt);
	tempMessage = tempMessage.replace("{count}", counter);
	
	var content = document.getElementById("content");
	
	var aPrint = document.createElement("div");
	aPrint.setAttribute("id", "print");
	aPrint.innerHTML = "<a href=\"quizprint.htm\" class=\"popup\" id=\"btn\">Print this page</a>";
	
	var messegeDiv		=	document.createElement("div");
	messegeDiv.setAttribute("id", "followUpMessege");
	messegeDiv.appendChild(document.createTextNode(tempMessage));
	
	var missedAnswersDiv	=	document.createElement("div");
	missedAnswersDiv.setAttribute("id", "missedAnswers");
	
	//var missedAnswersDivP	=	document.createElement("div");
	//missedAnswersDivP.setAttribute("id", "missedAnswersP");
	
	var startOverDiv	=	document.createElement("div");
	startOverDiv.setAttribute("id", "startOver");

	if(browser == "Internet Explorer"){
		var a = document.createElement("div");
		a.innerHTML = "<a href=\"quiz.htm\" onclick='retakeQuestions(visitordata);' class=\"btn\">"+tempLink+"</a>";
	}
	else{
		var a = document.createElement("a");
		a.setAttribute("href", "quiz.htm");
		a.setAttribute("class", "btn");
		a.setAttribute("onclick", "retakeQuestions(visitordata);");
		a.appendChild(document.createTextNode(tempLink));
	}

	

	//missedAnswersDivP.appendChild(ulP);
	startOverDiv.appendChild(a);
	missedAnswersDiv.appendChild(ul);
	content.appendChild(messegeDiv);
	content.appendChild(missedAnswersDiv);
	content.appendChild(startOverDiv);
	content.appendChild(aPrint);
	content.style.top = "100px";
	content.style.height = "360px";
	content.style.overflow = "auto";
	
	//var printer = document.createElement("div");
	//printer.setAttribute("id", "printer");
	//printer.setAttribute("style", "display:none");
	//printer.appendChild(missedAnswersDivP);
	
	
	//document.body.appendChild(printer);
	
	doPopups();
}

function printPage(xmldoc, url, arg){
	setTheStylesheet('quizfinal.css');
	
	doDebug("start: didNotPassAllFunction();");
	
	var question 	= xmldoc.getElementsByTagName("q");
	var reason 		= xmldoc.getElementsByTagName("a");
	var missedQuestionArray = Array();
	
	var ul = document.createElement("ul");
	var li = document.createElement("li");

	doDebug("question.length: " + question.length);
	doDebug("arg = " + arg);
	doDebug("arg.length = " + arg.length);

	for(var i = 0; i < question.length; i++){
		if(checkArray(visitordata.PassedTest, i, ";")){
			var li = document.createElement("li");
			var blockquote = document.createElement("blockquote");
			blockquote.innerHTML = reason[i].getElementsByTagName("reason")[0].firstChild.data;
			li.innerHTML = question[i].firstChild.data;
			li.appendChild(blockquote);
			ul.appendChild(li);
		}
	}	
	
	var content = document.getElementById("container");
	while (content.firstChild) {
  		content.removeChild(content.firstChild);
	}
	var div = document.createElement("div");
	div.appendChild(ul);

	content.appendChild(div);
}

function retakeQuestions(){
	visitordata.TestTaken = visitordata.PassedTest;
	visitordata.store();
	//alert(visitordata.PassedTest);	
	//window.location.reload(true);
}

function checkIfAllPassed(passed, taken){
	passed = passed.split(" ");
	passed = passed.sort();
	taken = taken.split(";");
	taken = taken.sort();
	//alert(passed);
	//alert(taken);
}

function randomNumber(UpTo){
	var ran_unrounded=Math.random()*UpTo;
	var ran_number=Math.floor(ran_unrounded);	
	
	return ran_number;
}

function getTextFromNodes(xml, who){
	try{
		if (document.implementation && document.implementation.createDocument){
			if(xml.getElementsByTagName(who)[0].firstChild.nextSibling){
				var text = xml.getElementsByTagName(who)[0].firstChild.nextSibling.data;
			}
			else{
				var text = xml.getElementsByTagName(who)[0].firstChild.data;
			}
		//var text = xmldoc.getElementsByTagName("English")[0].firstChild.data;
		} else {
			var text = xml.getElementsByTagName(who)[0].firstChild.data;
		}
		return text;
	}
	catch(e){
		alert(e +"\n js.js: getTextFromNodes");
	}
}
function getContent(xmldoc){

var visitordata2 = new Cookie(document, 'genie_ttt2');
visitordata2.load();
var visitordata = new Cookie(document, course+visitordata2.userid, 8760);
visitordata.load();
	switch(parseInt(visitordata.language)){
		case 0:
			var lang = "English";
			break;
		case 1:
			var lang = "french";
			break;
		case 2:
			var lang = "spanish";
			break;
		default:
			var lang = "English";
			break;			
	}

	var text = getTextFromNodes(xmldoc, lang);
	var img = xmldoc.getElementsByTagName("image")[0];
	var imgSrc = img.getElementsByTagName("src")[0].firstChild.data;
	var imgAlt = img.getElementsByTagName("alt")[0].firstChild.data;
	var imgType = img.getElementsByTagName("type")[0].firstChild.data;
	
	
	//var imgWidth = img.getElementsByTagName("width")[0].firstChild.data;
	//var imgHeight = img.getElementsByTagName("height")[0].firstChild.data;
	/**/
	/*
    var div = document.createElement("div");document.createTextNode()
    div.setAttribute("border", "1");
    document.body.appendChild(div);
	div.appendChild(document.createTextNode(text));*/
	document.getElementById("content").innerHTML = text;
	var d = document.getElementById("image");
	
	while (d.firstChild) {
  		d.removeChild(d.firstChild);
	}
	
	var theImage = document.createElement("img");//document.createTextNode(img)common/img/"+img+".gif
	theImage.setAttribute("src", "common/img/"+imgSrc+"."+imgType);
	//theImage.setAttribute("style", "background:#ccc; width:330px; height:250px;");
	/**/
	//theImage.setAttribute("width", imgWidth);
	//theImage.setAttribute("height", imgHeight);
	
	theImage.setAttribute("alt", imgAlt);
	theImage.setAttribute("title", imgAlt);
	
	d.appendChild(theImage);
}


function getTest(xmldoc){
	var quiz = xmldoc.getElementsByTagName("quiz")[0];
	var quizTitle = quiz.getAttribute("title");
	var quizInstruction = quiz.getAttribute("instruction");
	var qa = quiz.getElementsByTagName("qa");
	var qaCount = quiz.getElementsByTagName("qa").length;
	var mycurrent_row, mycurrent_cell;
	
	var theForm = document.createElement("form");
	var content = document.getElementById("content");
	var table 	= document.createElement("table");
	var tbody 	= document.createElement("tbody");
	table.setAttribute("border", "1");
	table.setAttribute("cellpadding", "1");
	table.setAttribute("cellspacing", "0");
	table.setAttribute("width", "500");
	
	var _row = document.createElement("tr");
	var _cell =	document.createElement("td");
	_cell.setAttribute("colspan", "2");
	
	_cell.appendChild(document.createTextNode(quizTitle));
	_row.appendChild(_cell);
	tbody.appendChild(_row);
	
	
	_row = document.createElement("tr");
	_cell =	document.createElement("td");
	_cell.setAttribute("colspan", "2");
	
	_cell.appendChild(document.createTextNode(quizInstruction));
	_row.appendChild(_cell);
	tbody.appendChild(_row);
	
	var question = xmldoc.getElementsByTagName("q");
	var answer = xmldoc.getElementsByTagName("a");
	
	for(var j = 0; j < qaCount; j++) {
		i = 0;
		mycurrent_row 		= document.createElement("tr");
		currentQuestion 	= document.createTextNode(question[j].firstChild.data);
		currentAnswer 		= answer[j];
		currentGui			= currentAnswer.getAttribute("gui");
		currentAnswerLength = currentAnswer.getElementsByTagName("c").length;
		
//**************************************************
//	start output the question
//**************************************************
		mycurrent_cell = document.createElement("td");
		mycurrent_cell.setAttribute("colspan", "2");
		mycurrent_cell.appendChild(currentQuestion);
		mycurrent_row.appendChild(mycurrent_cell);
		tbody.appendChild(mycurrent_row);		
//**************************************************
//	end output the question
//**************************************************
		
		
		
//**************************************************
//	start output the answers
//**************************************************
		mycurrent_row = document.createElement("tr");
		
		/* start empty cell */
		mycurrent_cell = document.createElement("td");
		mycurrent_cell.setAttribute("width", "35");
		mycurrent_row.appendChild(mycurrent_cell);
		/* end empty cell */
		
		var ans = "";
		
		mycurrent_cell = document.createElement("td");
		
		for(var i = 0; i < currentAnswerLength; i++){
			ans = currentAnswer.getElementsByTagName("c")[i].firstChild.data;
			
			currenttext = document.createTextNode(ans);
			
			ans = createInput("q"+j, ans, currentGui);			
			mycurrent_cell.appendChild(ans);
			mycurrent_cell.appendChild(currenttext);
			
			mycurrent_cell.appendChild(document.createElement("br"));
		}
		
		mycurrent_row.appendChild(mycurrent_cell);
		
		
		
		tbody.appendChild(mycurrent_row);
//**************************************************
//	end output the answers
//**************************************************
	}
	
	// put everything in the table
	table.appendChild(tbody);
	
	/*
	var row = table.insertRow(0);
	row.insertCell(0).appendChild(document.createTextNode(quizTitle));
	*/
	
	//make sure its clean
	while (content.firstChild) {
  		content.removeChild(content.firstChild);
	}
	
	content.appendChild(theForm);
	theForm.appendChild(table);
}

function createInput(name, value, type){
	if(type == "cb") type = "checkbox";
	if(type == "rb") type = "radio";
	var id = "idrad_" + name;
	if(browser == "Internet Explorer"){
		var objRadItem = document.createElement("<input type='"+type+"' value='"+value+"' value='"+value+"' id='"+id+"' name='"+name+"'>");
	}
	else{
		var objRadItem = document.createElement("input");
		objRadItem.type = type;
		objRadItem.name = name;
		objRadItem.id = id;
		objRadItem.value = value;
	}
	//alert(objRadItem);
	return objRadItem;
}

//==================================================
//	general functions
//==================================================

function checkArray(TheArray, TheFor, splitBy){
	//doDebug("start:  checkArray()");
	
	if(TheArray != null && TheArray.length != 0){
	//doDebug("start:  TheArray != null");
	//doDebug("TheArray = " + TheArray);
		TheArray = TheArray.split(splitBy)
		//doDebug("TheArray = " + TheArray);
		//alert(TheArray.length);
		for(var i = 0; i < TheArray.length; i++){
		//alert(TheArray[i]);
			if(TheArray[i] == TheFor){
				return false;
				//alert("false");
			}
		}
	}
	else{
		//alert("is null false");
		return false;
	}
	//alert("true");
	return true;
	//doDebug("end:  checkArray()");
}

function compareArray(passed, taken, splitBy){
	passed = passed.split(splitBy);
	passed = passed.sort();
	
	taken = taken.split(splitBy);
	taken = taken.sort();
	
	
	//alert(passed.length);
	//alert(taken.length);
	if(passed.length!=taken.length){
		return true;
	}
	else{
		return false;
	}
}

function doPopups() {
  if (!document.getElementsByTagName) return false;
  var links=document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("popup")) {
      links[i].onclick=function() {
      // Next two lines should be on one line
        window.open(this.href, "",
          "top=40,left=40,width=785,height=500");
        return false;
      }
    }
  }
}


	  
function launch(title, url, w, h, overlay_click_close) {
        var greybox = new GreyBox();
		greybox.overlay_click_close = overlay_click_close
        greybox.setDimension(w, h);
        greybox.setCenterWindow(true);
        return greybox.show(title, url)
}
