/***************************************************************************************
10 Questions Management

- The user's interaction-data is stored in an object that is available until the browser is
  closed or a reload is triggered.
- The object that stores interaction-data is initialized the first time and only once when
  the user opens the start-page (index.html).
- When a question-page is opened it is MANDATORY to register the page in the "onLoad" handler
  of the body-tag. Sample: <body onload="interactionResults.register('b_1_1', 'choice', 1, 1)">

@copyright: Crealogix AG
@author: Christoph Künzler
@version: 1.0 / 2006-08-28
***************************************************************************************/

// INITIALIZE ---------------------------------------------------------------------------
function checkInteractionDataObject() {
	if (!parent.test10questions) {
		parent.test10questions = new InteractionResults();
	}
	return parent.test10questions;
}
var interactionResults = checkInteractionDataObject();

// CLASS InteractionResults -------------------------------------------------------------
function InteractionResults() {
	// properties
	this.interactions = new Array();
	this.actualID;
	this.oneOrMoreAnswered = false;
	
	// methods
	this.register = registerInteraction;
	this.setResponse = setLearnerResponse;
	this.getResponse = getLearnerResponse;
	this.atLeastOneAnswered = getOneOrMoreAnswered;
	this.getResult = getLearnerResult;
}

function registerInteraction(id, type, correct_responses, weighting) {
	// avoid duplicates
	if (!this.interactions[id]) {
		if (type == "choice") {
			this.interactions[id] = new InteractionChoice(id, type, correct_responses, weighting);
		}
	}
	this.actualID = id;
}

function setLearnerResponse(learner_response) {
	if (!this.actualID) return;
	
	if (this.interactions[this.actualID]) {
		this.interactions[this.actualID].evaluate(learner_response);
		this.oneOrMoreAnswered = true;
	}
}

function getLearnerResponse() {
	if (!this.actualID) return;
	
	if (this.interactions[this.actualID]) {
		return this.interactions[this.actualID].learner_response;
	}
	else return null;
}

function getOneOrMoreAnswered() {
	return this.oneOrMoreAnswered;
}

function getLearnerResult(id) {
	if (this.interactions[id]) {
		return this.interactions[id].result;
	}
	return null;
}

// CLASS Interaction ---------------------------------------------------------------------
function Interaction() {
	// properties
	this.id;
	this.type;
	this.correct_responses;
	this.weighting;
	this.learner_response;
	this.result;
	
	// methods
	this.initialize = initializeInteraction;
}

function initializeInteraction(id, type, correct_responses, weighting) {
	this.id = id;
	this.type = type;
	this.correct_responses = correct_responses;
	this.weighting = weighting;
}

// CLASS InteractionChoice (subclass of Interaction) ------------------------------------
function InteractionChoice(id, type, correct_responses, weighting) {
	this.initialize(id, type, correct_responses, weighting);
	
	// methods
	this.evaluate = evaluateLearnerResponse;
}
InteractionChoice.prototype = new Interaction;

function evaluateLearnerResponse(learner_response) {
	this.learner_response = learner_response;
	this.result = (this.learner_response == this.correct_responses);
}

// HELPERS FOR RADIO BUTTONS ------------------------------------------------------------
function setChoiceResponse(radioGroup) {
	var learner_response = "";
	
	for (var i=0; i<radioGroup.length; i++) {
		if (radioGroup[i].checked) {
			learner_response = radioGroup[i].value;
			break;
		}
	}
	interactionResults.setResponse(learner_response);
}

function initChoiceResponse(radioGroup) {
	var learner_response = interactionResults.getResponse();
	
	for (var i=0; i<radioGroup.length; i++) {
		if (radioGroup[i].value == learner_response) {
			radioGroup[i].checked = true;
			break;
		}
	}
}

