
// Shortcut
function $(id) {
	return document.getElementById(id);
}

// Load the XML
var xmlDoc;
function loadXML(xmlFile){
	if(window.ActiveXObject){
		xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if(window.XMLHttpRequest) {
		xmlDoc = new XMLHttpRequest();
	}
	
	if(xmlDoc){
		xmlDoc.onreadystatechange = function(){
			if(xmlDoc.readyState == 4){
				XMLPreload();
			}
		}
		xmlDoc.open("GET", xmlFile, true);
		xmlDoc.send("");
	}
}

// Counter variable for questions
var q = 0
var c = 0

//colorfade variables
var rhex = 255
var ghex = 255
var bhex = 255

var ropt = 163
var bopt = 80
var gopt = 44

// Just what it says - loads the first question
function XMLPreload() {
	$("number").innerHTML = "<h2>Question 1:</h2><br />";
	$("question").innerHTML += xmlDoc.responseXML.getElementsByTagName("question")[0].firstChild.nodeValue;
	
	$("qb1").innerHTML += "<input type='radio' id='q1' onClick='checkAnswer(" + xmlDoc.responseXML.getElementsByTagName("score1")[0].firstChild.nodeValue + "); fadeOptions(1);'>" + 
	xmlDoc.responseXML.getElementsByTagName("option1")[0].firstChild.nodeValue + "</input><br />";
	
	$("qb2").innerHTML += "<input type='radio' id='q2' onClick='checkAnswer(" + xmlDoc.responseXML.getElementsByTagName("score2")[0].firstChild.nodeValue + "); fadeOptions(2);'>" + xmlDoc.responseXML.getElementsByTagName("option2")[0].firstChild.nodeValue + "</input><br />";
	
	$("qb3").innerHTML += "<input type='radio' id='q3' onClick='checkAnswer(" + xmlDoc.responseXML.getElementsByTagName("score3")[0].firstChild.nodeValue + "); fadeOptions(3);'>" + xmlDoc.responseXML.getElementsByTagName("option3")[0].firstChild.nodeValue + "</input><br />";
	
	$("qb4").innerHTML += "<input type='radio' id='q4' style='display:none;'></input><br />";
}

// Also what it says - progresses through subsequent questions
function goNext() {
	if (q <= 5){
		clearOptions();
		
		$("number").innerHTML = "<h2>Question " + xmlDoc.responseXML.getElementsByTagName("id")[q].firstChild.nodeValue + ":</h2><br />";
		$("question").innerHTML += xmlDoc.responseXML.getElementsByTagName("question")[q].firstChild.nodeValue;
	
		$("qb1").innerHTML += "<input type='radio' id='q1' onClick='checkAnswer(" + xmlDoc.responseXML.getElementsByTagName("score1")[q].firstChild.nodeValue + "); fadeOptions(1);'>" + 
		xmlDoc.responseXML.getElementsByTagName("option1")[q].firstChild.nodeValue + "</input>";
	
		$("qb2").innerHTML += "<input type='radio' id='q2' onClick='checkAnswer(" + xmlDoc.responseXML.getElementsByTagName("score2")[q].firstChild.nodeValue + "); fadeOptions(2);'>" + xmlDoc.responseXML.getElementsByTagName("option2")[q].firstChild.nodeValue + "</input>";
	
		checkQuestion3();
	}
	else {
		loadSummary()
	}
}

// Clears old data when loading a new question
function clearOptions(){
	var option1 = $("qb1");
	while (option1.firstChild){
		option1.removeChild(option1.firstChild);
	}
	var option2 = $("qb2");
	while (option2.firstChild){
		option2.removeChild(option2.firstChild);
	}
	var option3 = $("qb3");
	while (option3.firstChild){
		option3.removeChild(option3.firstChild);
	}
	
	var option4 = $("qb4");
	while (option4.firstChild){
		option4.removeChild(option4.firstChild);
	}
	
	var evaluation = $("evaluation");
	while (evaluation.firstChild){
		evaluation.removeChild(evaluation.firstChild);
	}
	
	var question = $("question");
	while (question.firstChild){
		question.removeChild(question.firstChild);
	}
	
	var rec = $("rec");
	while (rec.firstChild){
		rec.removeChild(rec.firstChild);
	}
		
	var intro = $("intro");
	while (intro.firstChild){
		intro.removeChild(intro.firstChild);
	}
	
	rhex = 255
	ghex = 255
	bhex = 255
	
	ropt = 163
	bopt = 80
	gopt = 44
	
	clearTimeout(text);
	$("evaluation").style.color="rgb(255,255,255)";
	
	c = 0
	
	$("qb1").style.color="rgb(163,44,80)";
	$("qb2").style.color="rgb(163,44,80)";
	$("qb3").style.color="rgb(163,44,80)";
	$("qb4").style.color="rgb(163,44,80)";
	
	$("next").style.visibility = "hidden";
	$("intro").style.visibility = "hidden";
}

//This is a silly hack right now
function checkQuestion3(){
	if (q == 1 || q == 3) {
		$("qb3").innerHTML += "<input type='radio' id='q3' style='display:none;'></input>";
		$("qb4").innerHTML += "<input type='radio' id='q4' style='display:none;'></input>";
	}
	else if (q == 5){
		$("qb3").innerHTML += "<input type='radio' id='q3' onClick='checkAnswer(" + xmlDoc.responseXML.getElementsByTagName("score3")[q].firstChild.nodeValue + "); fadeOptions(3);'>" + xmlDoc.responseXML.getElementsByTagName("option3")[q].firstChild.nodeValue + "</input>";
		
		$("qb4").innerHTML += "<input type='radio' id='q4' onClick='checkAnswer(" + xmlDoc.responseXML.getElementsByTagName("score4")[0].firstChild.nodeValue + "); fadeOptions(4);'>" + xmlDoc.responseXML.getElementsByTagName("option4")[0].firstChild.nodeValue + "</input>";
	}
		
	else{
		$("qb3").innerHTML += "<input type='radio' id='q3' onClick='checkAnswer(" + xmlDoc.responseXML.getElementsByTagName("score3")[q].firstChild.nodeValue + "); fadeOptions(3);'>" + xmlDoc.responseXML.getElementsByTagName("option3")[q].firstChild.nodeValue + "</input>";
		$("qb4").innerHTML += "<input type='radio' id='q4' style='display:none;'></input>";
	}
}

//This silly thing fades in the correct/incorrect text
function fadetext(){ 
	
	text = setTimeout("fadetext()",20); 
	
	if(rhex > 163) {
		rhex -= 11;
	}
	if(bhex > 80) {
		bhex -= 11; 
	}
	if(ghex > 44) {
		ghex -= 11; 
		}
	$("evaluation").style.color="rgb("+rhex+","+ghex+","+bhex+")";
	
}

//This fades the answers, var c is which answer number was chosen

function greyShift(sObj, tObj, uObj){

	grey = setTimeout("greyShift('" + sObj + "', '" + tObj + "', '" + uObj + "');",20);
	
	if(gopt < 180) {
		gopt += 11;
	}
	if(bopt < 180) {
		bopt += 11; 
	}
	if(ropt < 180) {
		ropt += 11; 
		}
	eval("document.getElementById('" + sObj + "').style.color = 'rgb("+ropt+","+gopt+","+bopt+")'");
	eval("document.getElementById('" + tObj + "').style.color = 'rgb("+ropt+","+gopt+","+bopt+")'");
	eval("document.getElementById('" + uObj + "').style.color = 'rgb("+ropt+","+gopt+","+bopt+")'");
}

function fadeOptions(c){ 
	
	if(c == 1){ 
		greyShift('qb2', 'qb3', 'qb4');
	}
	else if(c == 2){
		greyShift('qb1', 'qb3', 'qb4');
	}
	else if(c == 3){
		greyShift('qb1', 'qb2', 'qb4');
	}
	else if(c == 4){
		greyShift('qb1', 'qb2', 'qb3');
	}
	stripMouseEvent();
}

//This checks whether or not an answer is correct (1) or incorrect(0)
//variable s = score
var x = 0;
var s = 0;
function checkAnswer(x) {
	if (x <= 0) {
		fadetext();
		$("evaluation").innerHTML = "Incorrect";
		s == s;
	}
	else if (x >= 1) {
		fadetext();
		$("evaluation").innerHTML = "Correct";
		s == s++;
	}
	else {
		$("evaluation").innerHTML = "Error: Missing Answer Value";
	}
	displayRec()
	q == q++;
}

//Displays reccomendation, regardless of answer
function displayRec() {
	
	if(q == 0){
		$("rec").innerHTML += "<p>" + xmlDoc.responseXML.getElementsByTagName("reccomendation")[0].firstChild.nodeValue + "<a href='" + xmlDoc.responseXML.getElementsByTagName("href")[0].firstChild.nodeValue + "' target='_blank'>" + xmlDoc.responseXML.getElementsByTagName("text")[0].firstChild.nodeValue + "</a></p>";
	}
	else if(q == 2){
		$("rec").innerHTML += "<p>" + xmlDoc.responseXML.getElementsByTagName("reccomendation")[2].firstChild.nodeValue + "<a href='" + xmlDoc.responseXML.getElementsByTagName("href")[1].firstChild.nodeValue + "' target='_blank'>" + xmlDoc.responseXML.getElementsByTagName("text")[1].firstChild.nodeValue + "</a> page.</p>";
	}
	else {
		$("rec").innerHTML += "<p>" + xmlDoc.responseXML.getElementsByTagName("reccomendation")[q].firstChild.nodeValue + "</p>";
	}
	$("next").style.visibility = "visible";
}

//Disables onClick option events after one answer has been selected
function stripMouseEvent() {
	$("q1").removeAttribute("onClick");
	$("q1").setAttribute('disabled',true);
	$("q2").removeAttribute("onClick");
	$("q2").setAttribute('disabled',true);
	$("q3").removeAttribute("onClick");
	$("q3").setAttribute('disabled',true);
	$("q4").removeAttribute("onClick");
	$("q4").setAttribute('disabled',true);
}

//Clears out all the junk and loads the summary
function loadSummary(){
	clearOptions();
	var number = $("number");
	while (number.firstChild){
		number.removeChild(number.firstChild);
	}
	var option = $("option");
	while (option.firstChild){
		option.removeChild(option.firstChild);
	}

	$("option").style.height = "0px";
	$("rec").style.height = "270px";
	$("rec").innerHTML += "<p><strong style='color:#414141;'>Your assessment summary</strong></p>"; 
	summaryLoop();
	//$("rec").innerHTML += "</ol>";
	
	$("evaluation").style.height = "0px";
	$("number").style.height = "0px";
	$("intro").style.visibility = "visible";
	$("intro").innerHTML += "<p><strong style='color: #414141;'>You correctly answered " + s + " of " + q + " questions.</strong></p>";
	finalScore();
}

function summaryLoop(){
	var i = 0
	do {
		$("rec").innerHTML += "<p style='color:#7D7D7D;'><span class='red'>" + xmlDoc.responseXML.getElementsByTagName("id")[i].firstChild.nodeValue + ".</span>" + xmlDoc.responseXML.getElementsByTagName("reccomendation")[i].firstChild.nodeValue + "</p>";
		i == i++;
	}
	while (i <= 5);
}

//Checks and displays score results
function finalScore(){
	if(s <= 2){
		$("question").innerHTML += "<p>Danger, you are putting your personal identity at risk when you are online. Please take the time to learn how to be a safe shopper, and always remember to verify before you buy.</p>";
	}
	else if(s > 2 && s <= 4){
		$("question").innerHTML += "<p>Be careful, you have made some good decisions, but are not being as cautious as you could be. Just remember it is always important to verify before you buy.</p>";
	}
	else if(s > 4 && s<= 6){
		$("question").innerHTML += "<p>Congratulations, you are a safe Web surfer, especially when making online purchases. Keep up the good work and don't forget to verify before you buy.</p>";
	}
}

//Apes a hoverstate for the "next question" microlink, could be easily expanded for multiple links
function fakeLink(){
	$("next").style.textDecoration = "underline";
}

//Removes fake hoverstate
function fakeLinkOut(){
	$("next").style.textDecoration = "none";
}
