/*
 * Copyright (c) 2005 Absolutely Training Limited
 * 
 * Created on 01-Dec-2005
 */
 
/* JSLint global function comment */
/*global TestAnswer, AnswerChoice, TestUtils  */ 

/**
 * Translation of the Java TestQuestion class into JavaScript
 * 
 * @author paulb
 */
 
function TestQuestion( question, possibleAnswers ) {
    this.question = question;
    this.answerChoices = [];
    this.correctChoices = "";
    this.orderNum = 0;
    this.correctlyAnswered = false;
    this.possibleAnswers = [];
    for (var i = 0; i < possibleAnswers.length; i++) {
        var a = possibleAnswers[i];
        this.possibleAnswers.push(new TestAnswer(a, TestUtils.getNumeral(i)));
    }
} 

TestQuestion.prototype.getId = function() {
	return this.question.getId();
};

/**
 * Generates {@link AnswerChoice} objects for multiple choice and true and false questions.
 */
TestQuestion.prototype.generateAnswerChoices = function() {
    for (var i = 0; i < this.possibleAnswers.length; i++) {
    	var possibleAnswer = this.possibleAnswers[i];
        var a = possibleAnswer.getAnswer();
        var ac = new AnswerChoice(a.getAnswerText(), a.isCorrect(), a.getExplanation());
        ac.setLetter(TestUtils.getLetter(i));
        ac.setTestAnswers( [possibleAnswer] );
        this.answerChoices.push(ac);
    }
    this.setCorrectChoices();        
};

TestQuestion.prototype.setAnswerChoices = function( choices ) {
    this.answerChoices = choices;
};

TestQuestion.prototype.getAnswerChoices = function() {
    return this.answerChoices;
};
    
TestQuestion.prototype.getCorrectAnswerChoice = function() {
    for (var i = 0; i < this.answerChoices.length; i++) {
    	var ac = this.answerChoices[i];
        if (ac.isCorrect()) {
            return ac;
        }
    }
    throw new Error("TestQuestion without correct answerchoice: " + this.question.getId());
};
       

/**
 * Generates string that reflects correct choices for question. 2 means, third answer choice is
 * right, 2,3 means, third and forth answer choice is right.
 */
TestQuestion.prototype.setCorrectChoices = function() {
    var acs = "";
    for (var i = 0; i < this.answerChoices.length; i++) {
        if (this.answerChoices[i].isCorrect()) {
            acs += (i + ",");
        }
    }
    
    this.correctChoices = acs.substring(0, acs.length - 1);
};

TestQuestion.prototype.getCorrectChoices = function() {
    return this.correctChoices;
};

TestQuestion.prototype.getQuestionText = function() {
	if (DIV_ALIGN == 'right')
	{
		return this.getNumber(this.getOrderNum())+" - "+this.question.getQuestionText();
	}
    return "Q." + this.getOrderNum() + " " + this.question.getQuestionText();
};

TestQuestion.prototype.getNumber = function(number) {

	var fullNumber="";
	

	if (number>9)	
	{
		fullNumber = NUMBERS [Math.floor(number/10)]+NUMBERS [number % 10];
	} else {
	fullNumber =NUMBERS[number];
	}
	return  fullNumber;
};

TestQuestion.prototype.getContentObjectId = function() {
    return this.question.getContentObjectId();
};
    
TestQuestion.prototype.getScormHtmlPath = function() {
    return this.question.getScormHtmlPath();
};
    
TestQuestion.prototype.getQuestionTextWithoutNumber = function() {
    return this.getQuestion().getQuestionText();
};

TestQuestion.prototype.getOrderNum = function() {
    return this.orderNum;
};

TestQuestion.prototype.setOrderNum = function(orderNum) {
    this.orderNum = orderNum;
};

TestQuestion.prototype.isCorrectlyAnswered = function() {
    return this.correctlyAnswered;
};
    
TestQuestion.prototype.setCorrectlyAnswered = function( correctlyAnswered) {
    this.correctlyAnswered = correctlyAnswered;
};
    
TestQuestion.prototype.getPossibleAnswers = function() {
    return this.possibleAnswers;
};

TestQuestion.prototype.getQuestion = function() {
    return this.question;
};

 