 //the script for handling an mcq - both single response and multiple response

var questionsArray = new Array()
var questionsArrayIndex = new Array()
var currentQuestion = 0
var totalScore = 0
var possScore = 0
var alreadyTried = false
var lastFeedback

// set up images to be used
//if (document.images) {
	correctOptionImg = new Image(15,15)
	correctOptionImg.src = "../../../../resource/generic/optioncorrect.gif"
	wrongOptionImg = new Image(15,15)
	wrongOptionImg.src = "../../../../resource/generic/optionwrong.gif"
//}

// this is a constructor for a question
function question(questCode,optionsArray) {
	this.questCode = questCode
	this.optionsArray = optionsArray
	questionsArrayIndex[questionsArrayIndex.length] = questCode
}

// this is a constructor function for a question option
function option(optionCode, optionContent, optionCorrect) {
	this.optionCode = optionCode
	this.optionContent = optionContent
	if (optionCorrect == "correct") {
		this.optionCorrect = true
	} else {
		this.optionCorrect = false
	}
}

// this function handles the response to the user choosing an option
function checkAnswer(userAnswer) {
	if(lastFeedback) {
		hide(lastFeedback)
	}
	var thisQuestionCode = questionsArrayIndex[currentQuestion]
	var thisOptionCode = questionsArray[thisQuestionCode].optionsArray[userAnswer].optionCode
	var thisFeedback = thisQuestionCode + thisOptionCode
	var thisFeedbackImage = thisFeedback + "Image"
	lastFeedback = thisFeedback
	// has the user already tried (only get score on first try)
	if (!alreadyTried) {
		alreadyTried=true
		possScore++
		if (questionsArray[thisQuestionCode].optionsArray[userAnswer].optionCorrect) {
			totalScore++
		}
	}	
	// whatever happens, we should display the feedback for this option
	if (questionsArray[thisQuestionCode].optionsArray[userAnswer].optionCorrect) {
		//setBGColor(thisFeedback, "rgb(102,255,153)")
		setBGColor(thisFeedback, "#66FF99")
		imageReplace(thisFeedbackImage,"correctOptionImg")
	} else {
		//setBGColor(thisFeedback, "rgb(255,153,153)")
		setBGColor(thisFeedback, "#FF9999")
		imageReplace(thisFeedbackImage,"wrongOptionImg")
	}
	show(thisFeedback)
}

// this function replaces one image with another
function imageReplace(imageID, newImage) {
	if (document.images) {
		document.images[imageID].src = eval(newImage + ".src")
	}	
}

// this function navigates the user to the next question
function nextQuestion() {
	hide(questionsArrayIndex[currentQuestion])
	hide("count" + questionsArrayIndex[currentQuestion])
	if (lastFeedback) {
		hide(lastFeedback)
	}	
	currentQuestion++
	alreadyTried=false
	show(questionsArrayIndex[currentQuestion])
	show("count" + questionsArrayIndex[currentQuestion])
	if (currentQuestion==(questionsArrayIndex.length-1)) {
		hide("next")
	}
	show("previous")
}

// this function navigates the user to the previous question
function previousQuestion() {
	hide(questionsArrayIndex[currentQuestion])
	hide("count" + questionsArrayIndex[currentQuestion])
	if (lastFeedback) {
		hide(lastFeedback)
	}	
	currentQuestion--
	show(questionsArrayIndex[currentQuestion])
	show("count" + questionsArrayIndex[currentQuestion])
	if (currentQuestion==0) {
		hide("previous")
	}
	show("next")
}

// this initialises all the quiz content arrays and variables
function init() {
	draginit()
	hide("loading")
	show(questionsArrayIndex[0])
	if (questionsArrayIndex.length>1) {
		show("count" + questionsArrayIndex[0])
		show("next")
	}
}
