/* CyanLine LLC 2010
 * jsHome.js
 * C. Cardio
 */

var questionSchema;
var GQuestionList = new Array();

function startHome(){
	if (document.getElementById('navSpot')) {
		var spot = document.getElementById('navSpot');
// 		GPageBody=pageBody[0];
		var d = document.createElement('div');
		d.id='divFooter';
		d.style.backgroundColor='gray';
		// -----
		var tmplink='<a href="javascript:;" onClick="buildQuiz()">Quiz Me</a>';
		var span=document.createElement('span');
// 		span.style.display='none';
		span.innerHTML=tmplink;
		d.appendChild(span);

// 		buildLogin();

		spot.appendChild(d);
	}
	else
		alert('dont got it ' );
}

function buildQuiz(whichQuiz){
	if (document.getElementById('contentSpot')) {
		var spot = document.getElementById('contentSpot')
		spot.innerHTML = '';
		var d = document.createElement('div');
		d.style.borderStyle='solid';
		d.style.borderWidth='1px';
		d.style.position='relative';
		d.style.top='70px';
		d.style.borderColor='#99ff00';
		d.style.padding='5px';
		d.style.overflow='hidden';

		status_text(stripExt(whichQuiz));

		processQuestions(d,whichQuiz);

		buildQuizFooter();

		spot.appendChild(d);
	}
}

function status_clear()
{
	if (document.getElementById('statusSpot'))
	{
		var spot = document.getElementById('statusSpot');
		spot.innerHTML = '';
	}
}

function status_text(txt)
{
	if (document.getElementById('statusSpot'))
	{
		var spot = document.getElementById('statusSpot');
		spot.innerHTML = '';
		spot.innerHTML = txt;
	}
}

function processQuestions(d,whichQuiz)
{
	buildQuestionSchema();
	var url = '/quizes/'+whichQuiz+'?random='+Math.random();
	var xmldoc=ajaxPHP(url,2);
	buildQuestionList(xmldoc);
	for (var y = 0; y < GQuestionList.length; y++){
		d.appendChild(buildQuestion(GQuestionList[y]));
	}
}

function buildQuestionSchema(){
	questionSchema = {  
	rowtag: "item",
	columns: [
		{tagname: "question", label: "Q:"},
		{tagname: "a1", label: "A:"},
		{tagname: "a2", label: "B:"},
		{tagname: "a3", label: "C:"},
		{tagname: "a4", label: "D:"},
		{tagname: "answer", label: "A:"}
	]
	};
}

function buildQuestion(qSet){
	var d = document.createElement('div');
	d.setAttribute('align','center');
	d.style.borderBottom='solid';
	d.style.borderWidth='1px';

	var t = document.createElement('table');
	t.border=0;
	t.setAttribute('width','50%');
	var b = document.createElement('tbody');
	t.appendChild(b);
	var r = document.createElement('tr');
	var c = document.createElement('td');

	// ------ Answer span
	var s = document.createElement('span');
	var spanID = 'spanAnswer_'+qSet.qID;
	s.setAttribute('id',spanID);
	s.innerHTML = 'Correct Ans: '+ansStruct[qSet.correctAnswer-1];
	s.style.color='#cc0000';
	s.style.display='none';
	d.appendChild(s);

	// ----- question row
	var l = document.createElement('label');
	l.innerHTML= qSet.qID+'. Q: '+qSet.question+'?';
	c.setAttribute('colspan',2);
	c.appendChild(l);
	r.appendChild(c);
	b.appendChild(r);

	// ----- answer row
	r = document.createElement('tr');
	c = document.createElement('td');
// 	alert(qSet.arrAnswers.length);
	for (var i = 1; i < qSet.arrAnswers.length+1; i++){
		var id = qSet.qID+'_'+i;
		var val = qSet.arrAnswers[i-1];
		var radio = document.createElement('input');
		radio.id=id+'_radio';
		radio.type='radio';
		var radioGroupName = 'answers_'+qSet.qID;
// 		radio.name= radioGroupName;
		radio.setAttribute('name',radioGroupName);
		radio.value=val;
		l = document.createElement('label');
		l.innerHTML = ansStruct[i-1]+' - '+val;
		l.htmlFor=id+'_radio';
		c.appendChild(radio);
		c.appendChild(l);
		r.appendChild(c);
		c = document.createElement('td');
		if (i == 2 || i == 4) {
			b.appendChild(r);
			r = document.createElement('tr');
		}
	}
	// ----- footer row
	r = document.createElement('tr');
	c = document.createElement('td');
	c.setAttribute('colspan',2);
	c.setAttribute('align','right');
	var tmplink = '<a href="#" onClick="previewDone()">Done</a>';
	c.innerHTML = tmplink;
	r.appendChild(c);
// 	b.appendChild(r);
	var myForm = document.createElement('form');
	myForm.appendChild(t);
	d.appendChild(myForm);

	return d;
}

function buildQuestionList(xmldoc){
	var q;
	var ansOps = new Array();
	var a;

	var xmlrows = xmldoc.getElementsByTagName(questionSchema.rowtag);
	for(var k=0; k < xmlrows.length; k++) {
		var qID = xmlrows[k].getAttribute('id');
		var xmlrow = xmlrows[k];
		for(var i = 0; i < questionSchema.columns.length; i++) {
			var sc = questionSchema.columns[i];
			var tagname = (typeof sc == "string")?sc:sc.tagname;
			var xmlcell = xmlrow.getElementsByTagName(tagname)[0];
			//CHECK if the xmlcell.firstChild.data exists
			if (xmlcell.firstChild)
				var celltext = xmlcell.firstChild.data;
			else 
				var celltext = "N/A";
			if (celltext==''){ celltext='N/A';}
// 			alert(tagname+' = '+celltext);
			//-----------------------------
			switch (tagname){
			case 'question':
				q = celltext;
			break;
			case 'a1': case 'a2': case 'a3': case 'a4':
				ansOps.push(celltext);
			break;
			case 'answer':
				a = celltext;
			break;
			}
		}
		var qItem = new questionItem(qID,q,ansOps,a);
		GQuestionList.push(qItem);
	}
}

function buildQuizFooter() {
	if (document.getElementById('divFooter')){
		var footer = document.getElementById('divFooter');
		var s = document.createElement('span');
		footer.innerHTML = '';
// 		var tmplink = '<a href="javascript:;" onClick="eventReset()">Reset</a>';

		var tmplink = '<a href="javascript:;" onClick="eventSubmit()">Submit</a>';
		s.innerHTML = tmplink;
		footer.appendChild(s);		
	}
}

function clear_footer()
{
	if (document.getElementById('divFooter'))
	{
		var footer = document.getElementById('divFooter');
		footer.innerHTML = '';
	}
}

function eventSubmit(){
	var unansweredQuestionId = new Array();
	var correctQuestionId = new Array();
	var incorrectQuestionId = new Array();

	var currScore = new quizScore();
	for (var y = 0; y < GQuestionList.length; y++){
		var radioGroupName = 'answers_'+GQuestionList[y].qID;
		currScore.totalQuestions = GQuestionList[y].qID;
		if (document.getElementsByName(radioGroupName)){
			var radioGroup = document.getElementsByName(radioGroupName);
			var groupChecked = false;
			for (var g = 0; g < radioGroup.length; g++){
				if (radioGroup[g].checked){
					groupChecked = true;
					if (GQuestionList[y].correctAnswer == g+1){
						var id = 'spanAnswer_'+GQuestionList[y].qID;
						correctQuestionId.push(id);
						currScore.correct++;
					}
					else{
						var id = 'spanAnswer_'+GQuestionList[y].qID;
						incorrectQuestionId.push(id);
						currScore.incorrect++;
					}
				}
			}
			if (!groupChecked){
				unansweredQuestionId.push(getRadioGroupId(radioGroupName));
			}
		}
	}
	if (unansweredQuestionId.length != 0)
		alert('Please answer all questions');
	else
	{
		// calculate score
// 		alert(currScore.correct+'/'+currScore.totalQuestions);
		markQuiz(correctQuestionId,incorrectQuestionId);
		displayQuizScore(currScore);
	}
}

function markQuiz(arrCorrect,arrIncorrect)
{
	for (var i = 0; i < arrCorrect.length; i++)
	{
		var s = document.getElementById(arrCorrect[i]);
		s.innerHTML = 'Correct!';
		s.style.color = '#99ff00'
		s.style.display = 'block';
	}
	for (var y = 0; y < arrInCorrect.length; y++)
	{
		alert(arrIncorrect[y]);
		var s = document.getElementById(arrIncorrect[y]);
		s.style.display = 'block';
	}
}

function displayQuizScore(aScore)
{
	clear_footer();
	if (document.getElementById('statusSpot'))
	{
		var spot = document.getElementById('statusSpot');
		spot.innerHTML = '';
		var D = document.createElement('div');
		D.style.borderStyle='solid';
		D.style.borderWidth='1px';
		D.style.borderColor='#ffff66';
		D.style.padding='5px';
		D.style.overflow='hidden';
		var d = document.createElement('div');
		var s = document.createElement('span');
		aScore.percentage = (aScore.correct/aScore.totalQuestions)*100;
		s.innerHTML = aScore.percentage+'%';
		s.setAttribute('class','percentage');
		d.appendChild(s);
		D.appendChild(d);
		// ------
		d = document.createElement('div');
		s = document.createElement('span');
		s.innerHTML = 'Good work! You scored '+aScore.correct+' out of '+aScore.totalQuestions+' questions correct.';
		d.appendChild(s);
		D.appendChild(d);
		// ------
		d = document.createElement('div');
		d.setAttribute('id','userCollectionSpot');
		s = document.createElement('span');
		s.innerHTML = '<a href="javascript:;" onClick="collectUserEmail()">here</a>';
		var anotherS = document.createElement('span');
		anotherS.innerHTML = 'Click '+s.innerHTML+' to see how you compared to others.';
		d.appendChild(anotherS);
		D.appendChild(d);
		// ------
		spot.appendChild(D);
	}
}

function collectUserEmail()
{
	if (document.getElementById('userCollectionSpot'))
	{
		var spot = document.getElementById('userCollectionSpot');
		spot.innerHTML = '';
		var input = document.createElement('input');
		spot.appendChild(input);
	}
	else alert('no userCollectionSpot');
}

function quizScore()
{
	this.correct=0;
	this.incorrect=0;
	this.totalQuestions=0;
	this.percentage=0;
}

function getRadioGroupId(gName){
	var p = gName.indexOf('_');
	var id='';
	for (var i = p+1 ; i < gName.length; i++){
		id+=gName.charAt(i);
	}
// 	alert('id='+id);
	return id;
}

