////////////////////////////////////////////////
// DO NOT TRY TO RUN OR MODIFY THIS FILE
//
// QUIZZLER JAVASCRIPT VERSION 1.0
// 
// Place this file, or a copy of this file, next to your exported HTML file
// when you export a quiz for the iPhone.
//
///////////////////////////////////////////////
var thisQuestion = 0;
var numAnswers = 0;
var choices = new Array(numQuestions); //-1 = no answer, 0 = incorrect, 1 = correct
var showFlashcard = false;
var correctIndex = -1;
var randomQuestions = new Array(numQuestions);

var answerFlds = new Array(5);
answerFlds[0] = "answer1";
answerFlds[1] = "answer2";
answerFlds[2] = "answer3";
answerFlds[3] = "answer4";
answerFlds[4] = "answer5";

for (i=0; i<numQuestions; i++) {
	choices[i] = -1;
	randomQuestions[i] = i;
}
randomQuestions.sort(randomOrder);

function Score(answer) {
	//move on to next question if currently showing flashcard answer
	if ((numAnswers == 1) && showFlashcard) {
		nextQuestion();
		return;
	}
   if (answer >= numAnswers) return;
   
  if(correctIndex != answer) {
  	choices[thisQuestion] = 0;
  	document.getElementById(answerFlds[answer]).bgColor = "salmon";
  }
  else {
  	if (numAnswers>1) {
  		document.getElementById(answerFlds[answer]).bgColor = "palegreen";
    	//alert("CORRECT!\n\nclick OK for next question...");
    	if (choices[thisQuestion] == -1) choices[thisQuestion] = 1;
    	setTimeout("nextQuestion()", 1000);
    }
    else {
    	if (showFlashcard)
    		nextQuestion();
    	else {
    		showFlashcard = true;
    		loadQuestion();
    	}
    }
  }
}

function randomOrder()	{
	return (Math.round(Math.random())-0.5);
}

function showStatus() {
	//compute score
	var gotRight = 0;
	var gotWrong = 0;
	for (i=0; i<numQuestions; i++) {
		if (choices[i] == 0) gotWrong++;
		else if (choices[i] == 1) gotRight++;
	}
	
	//display status
	var percent;
	if ((gotRight + gotWrong) == 0)
		percent = 0;
	else
		percent = gotRight/(gotWrong+gotRight) * 100;
	percent = Math.round(percent);
	document.getElementById('status').innerHTML = numQuestions + " questions; score: " + gotRight + "/" + (gotRight+gotWrong) + " (" + percent + "%)";
}

function loadQuestion() {
	showStatus();
	
	//display question
	text = questions[randomQuestions[thisQuestion]].split("\n");
	questionStr = text[0];
	pictureLocation = questionStr.indexOf("##");
	if (pictureLocation >= 0) {
		pictureStr = questionStr.substr(pictureLocation + 2);
		questionStr = questionStr.substr(0, pictureLocation-1);
	}
	else
		pictureStr = "";
	document.getElementById('picture').innerHTML = "";
	document.getElementById('question').innerHTML = (randomQuestions[thisQuestion]+1) + ") " + questionStr;
	
	//display answers
	numAnswers = text.length - 1;
	if (numAnswers>5) numAnswers = 5;

  	for (i=0; i<numAnswers; i++)
  		document.getElementById(answerFlds[i]).bgColor = "beige";
  	for (i=numAnswers; i<5; i++)
  		document.getElementById(answerFlds[i]).bgColor = "white";
	
	if (numAnswers == 1) {
		correctIndex = 0;
		if (showFlashcard) {
			document.getElementById('answer1').innerHTML = text[1];
			document.getElementById('answer2').innerHTML = "(click again to continue)";
		}
		else {
			document.getElementById('answer1').innerHTML = "(click here to reveal answer)";
			document.getElementById('answer2').innerHTML = "";
			document.getElementById('answer3').innerHTML = "";
			if (pictureStr != "")
				document.getElementById('answer3').innerHTML = "<img src = \"" + pictureStr + "\">";
			document.getElementById('answer4').innerHTML = "";
			document.getElementById('answer5').innerHTML = "";
		}
	}
	else {
		var randomAnswers = new Array(numAnswers);
		for (i=0; i<numAnswers; i++)
			randomAnswers[i] = i;
		randomAnswers.sort(randomOrder);
		for (i=0; i<numAnswers; i++)
			if ((randomAnswers[i]) == key[randomQuestions[thisQuestion]])
				correctIndex = i;

		if (numAnswers>0) document.getElementById('answer1').innerHTML = "A: " + text[randomAnswers[0]+1];
		else document.getElementById('answer1').innerHTML = "";
		
		if (numAnswers>1) document.getElementById('answer2').innerHTML = "B: " + text[randomAnswers[1]+1];
		else document.getElementById('answer2').innerHTML = "";
		
		if (numAnswers>2) document.getElementById('answer3').innerHTML = "C: " + text[randomAnswers[2]+1];
		else document.getElementById('answer3').innerHTML = "";
		
		if (numAnswers>3) document.getElementById('answer4').innerHTML = "D: " + text[randomAnswers[3]+1];
		else document.getElementById('answer4').innerHTML = "";
		
		if (numAnswers>4) document.getElementById('answer5').innerHTML = "E: " + text[randomAnswers[4]+1];
		else document.getElementById('answer5').innerHTML = "";

		if (pictureStr != "")
			document.getElementById('picture').innerHTML = "<img src = \"" + pictureStr + "\">";
	}
}

function nextQuestion() {
	thisQuestion = thisQuestion + 1;
	showFlashcard = false;
	if (thisQuestion < numQuestions)
		loadQuestion();
	else {
		showStatus();
		for (i=0; i<5; i++)
			document.getElementById(answerFlds[i]).bgColor = "beige";
		numAnswers = 0;
		document.getElementById('question').innerHTML= "FINISHED!";
		document.getElementById('answer1').innerHTML = "";
		document.getElementById('answer2').innerHTML = "";
		document.getElementById('answer3').innerHTML = "";
		document.getElementById('answer4').innerHTML = "<a href=\"http://www.quizzlerpro.com/ \">this quiz built with Quizzler Maker</a>";
		document.getElementById('answer5').innerHTML = "";
		document.getElementById('picture').innerHTML = "";
	}
}

function prevQuestion() {
	if (thisQuestion == 0) return;
	thisQuestion = thisQuestion - 1;
	showFlashcard = false;
	loadQuestion();
}
