window.onload=function() { init(); }

//INITALIZATION
function init() {
	questionDivs = document.getElementsByName("question");
	for(var i=0; i<questionDivs.length; i++) { 	
		questionDivs.item(i).innerHTML = "";	//Remove the "Please enable JavaScript" message because JavaScript is running.
		getQuestion(questionDivs.item(i).getAttribute('number'), questionDivs.item(i));
	} 
}

//GET QUESTIONS
function getQuestion(qst, d) {
	new Ajax.Request('/wp-content/uploads/safety/safetyQuestion.php', {
		method:'get',
		parameters: {'qst': qst},
		onSuccess: function(transport){
			var response = transport.responseXML || "no response text";
			getQuestionCallback(response, qst, d);
		},
		onFailure: function(){ alert('Something went wrong...') }
	});
}

function getQuestionCallback(xml, qst, d) {
	var question = xml.getElementsByTagName( 'question' ).item(0).firstChild.nodeValue;
    	var options = xml.getElementsByTagName("option");

	var newFormElement;
	newFormElement = document.createElement( 'form' );
	newFormElement.setAttribute("id", "question"+qst);
	newFormElement.setAttribute("number", qst);
	newFormElement.setAttribute("action", "javascript:get(document.getElementById('question"+qst+"'));");

	d.appendChild(newFormElement);

	var newQuestionElement = document.createElement( 'div' );
	newQuestionElement.setAttribute("class", "thequestion");
	newQuestionElement.innerHTML = "<strong>"+question+"</strong>";
	newFormElement.appendChild(newQuestionElement);		

	for( var i=0; i<4; i++) {
		var newInputElement;

		try {						//v3
			newInputElement = document.createElement( "<input type='radio' name='ans'>" );
		} catch(e) { 
		}
		if(!newInputElement) {
			newInputElement = document.createElement( "input" );
			newInputElement.setAttribute("type", "radio");	
			newInputElement.setAttribute("name", "ans");
		}

		newInputElement.value = String.fromCharCode('A'.charCodeAt() + i); //i=0, A; i=1, B; ...
		newFormElement.appendChild(newInputElement);
		
		var newTextElement = document.createElement( 'text' );
		newTextElement.innerHTML = options.item(i).firstChild.nodeValue;		
		newFormElement.appendChild(newTextElement);

		newFormElement.appendChild(document.createElement( 'br' ));
		newInputElement=null;			//important to v3
	}

	var newSubmitElement = new Element('input', { 'type': 'submit', 'value': 'Submit' })
	newFormElement.appendChild(newSubmitElement);
}

function get(obj) {
	var getstr = "";
	for (i=0; i<obj.childNodes.length; i++) {
		if (obj.childNodes[i].tagName == "INPUT") {
			if (obj.childNodes[i].type == "radio") {
				if (obj.childNodes[i].checked) {
					getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value;
				}
			}
		}   
	}

	if(getstr!="") { 	
		getstr = "qst=" + obj.getAttribute("number") +"&"+ getstr;
		obj.setAttribute("action", "javascript:alert('Sorry, but you have already answered this question.');");
		getAnswer(getstr, obj);
	}
	else {
		alert("You must first select an answer.");
	}
}


//HANDLE ANSWERS
function getAnswer(x, obj) {
	new Ajax.Request('/wp-content/uploads/safety/safetyQuestion.php?'+x, {
		method:'get',
		onSuccess: function(transport){
			var response = transport.responseXML || "no response text";            
			getAnswerCallback(response, obj);    
	       	},
	        onFailure: function(){ alert('Something went wrong...') }
	});
}

function getAnswerCallback(xml, elem) {
	var answer = document.createElement( 'div' );
	if(xml.getElementsByTagName( 'correct' ).length>0) {
		answer.setAttribute("class", "answer correct");
		answer.innerHTML = xml.getElementsByTagName( 'correct' ).item(0).firstChild.nodeValue;
	}
	else if(xml.getElementsByTagName( 'incorrect' ).length>0) {
		answer.setAttribute("class", "answer incorrect");
		answer.innerHTML = xml.getElementsByTagName( 'incorrect' ).item(0).firstChild.nodeValue;
	}
	else {
		answer.innerHTML = "Oops! We've done something wrong.  Our apologies but we've made a mistake and can't verify your answer.  Please try reloading the page.";
	}
	elem.appendChild(answer);


	if(xml.getElementsByTagName( 'explain' ).length>0) { 
		var explain = document.createElement( 'div' );
		explain.setAttribute("class", "explain");
		explain.innerHTML = xml.getElementsByTagName( 'explain' ).item(0).firstChild.nodeValue;
		elem.appendChild(explain);	
	}


	var chart = document.createElement( 'img' );
	var str = xml.getElementsByTagName( 'str' ).item(0).firstChild.nodeValue;
	chart.setAttribute("src", "http://chart.apis.google.com/chart?cht=bhs&chs=300x160&chd=t:"+ str +"&chco=bfd0ea,205aa6&chxt=x,y,x&chxl=1:|D|C|B|A|2:||Correct+answer+is+shown+in+dark+blue|&chtt=Distribution+of+Answers");
	elem.appendChild(chart);
}
