function updateTimer(timerElementId, time)
{
	var e = document.getElementById(timerElementId);
	if (!e) {
		return;
	}
	
	if (time) {
		e.value = time;
	}
	else {
		e.value = (e.value > 0) ? e.value - 1 : e.value;
	}
	e.onchange();
	setTimeout("updateTimer('" + timerElementId + "');", 1000);
}

function TestObject()
{ 

}

TestObject.prototype = 
{
	onDisplayQuestion : null,
	onDisplaySummation : null,
	onDisplayFinishConfirmation : null,
	
	elementTimer : null,
	
	testId : null,
	testStateId : null,
	test : null,
	
	actualTime : null,
	
	questionIndex : -1,
	
	service : null,
	
	finishTest: function(force)
	{
		force = force | false;
		var questions = this.test.questions;
		var qcnt = questions.length;
		/*
		var allanswered = true;
		if (!force)
		{
			for (var i=0; i<qcnt; i++)
			{
				if (questions[i].answered.length > 0)
				{
					allanswered = false;
					break;
				}
			}
		}
		*/
		
		if (force)
		{
			this.service.finishTest(this.testStateId);
			if (this.onDisplaySummation)
			{
				this.onDisplaySummation();
			}
		}
		else if (this.onDisplayFinishConfirmation)
		{
			this.onDisplayFinishConfirmation();
		}
	},
	
	setQuestionAnswered: function(questionId)
	{
		for (var i=0; i<this.test.questions.length; i++)
		{
			if (this.test.questions[i].id == questionId)
			{
				this.test.questions[i].answered = true;
				break;
			}
		}
	},
	
	answerQuestion: function(answers)
	{
		this.setQuestionAnswered(answers[0].question_id);
		
		this.service.answerQuestion(this.testStateId, answers);
		for (var i=0; i<this.test.answers.length; i++)
		{
			for (var j=0; j<answers.length; j++)
			{
				if (this.test.answers[i].id == answers[j].id)
				{
					this.test.answers[i].answered = answers[j].answered;
				}
			}
		}
	},
	
	nextUnansweredQuestion: function()
	{
		var questions = this.test.questions;
		var qcnt = questions.length;
		var question = null;
		
		var before = 0;
		var after = 0;
		
		for (var i=0; i<qcnt; i++)
		{
			if (i > this.questionIndex && !questions[i].answered)
			{
				question = questions[i];
				this.questionIndex = i;
				break;
			}
		}
		for (var i=0; i<qcnt; i++)
		{
			if (i < this.questionIndex && !questions[i].answered)
			{
				before++;
			}
			else if (i > this.questionIndex && !questions[i].answered)
			{
				after++;
			}
		}
		if (question && this.onDisplayQuestion)
		{
			this.onDisplayQuestion(question, this.getAnswers(question.id), (before <= 0), (after <= 0), questions.length, this.questionIndex);
		}
	},
	
	nextQuestion: function()
	{
		
		if (!this.isTestTimer())
		{
			return this.nextUnansweredQuestion();
		}
		var questions = this.test.questions;
		var qcnt = questions.length;
		var question = null;
		if (this.questionIndex < qcnt - 1)
		{
			this.questionIndex++;
			question = this.test.questions[this.questionIndex];
		}
		
		if (question && this.onDisplayQuestion)
		{
			this.onDisplayQuestion(question, this.getAnswers(question.id), (this.questionIndex <= 0), (this.questionIndex >= qcnt - 1), questions.length, this.questionIndex);
		}
	},
	
	gotoQuestion: function(index)
	{
		var questions = this.test.questions;
		var qcnt = questions.length;
		var question = null;
		if (index < qcnt && index >= 0)
		{
			this.questionIndex = index;
			question = this.test.questions[this.questionIndex];
		}
		
		if (question && this.onDisplayQuestion)
		{
			this.onDisplayQuestion(question, this.getAnswers(question.id), (this.questionIndex <= 0), (this.questionIndex >= qcnt - 1), questions.length, this.questionIndex);
		}
	},
	
	prevQuestion: function()
	{
		var questions = this.test.questions;
		var qcnt = questions.length;
		var question = null;
		
		if (this.questionIndex > 0)
		{
			this.questionIndex--;
			question = this.test.questions[this.questionIndex];
		}
		if (question && this.onDisplayQuestion)
		{
			this.onDisplayQuestion(question, this.getAnswers(question.id), (this.questionIndex <= 0), (this.questionIndex >= qcnt - 1), questions.length, this.questionIndex );
		}
	},
	
	getAnswers: function(questionId)
	{
		var answers = new Array();
		var j = 0;
		for (var i=0; i<this.test.answers.length; i++)
		{
			if (this.test.answers[i].question_id == questionId)
			{
				answers.length++;
				answers[j] = this.test.answers[i];
				j++;
			}
		}
		return answers;
	},
	
	initialize: function()
	{
		
	},
	
	start: function()
	{
		if (!this.testStateId && this.testId) {
    		this.testStateId = this.createNewTestState(this.testId);
    	}
    	
    	if (this.testStateId) {
    		this.test = this.service.getTestState(this.testStateId);
    		return true;
    	}
    	alert('dupa1');
    	return false;
	},
	
	createNewTestState: function(testId)
    {
		return this.service.newTestState(testId);
    },
    
    getTest: function(testId)
    {
    	return this.service.getTest(testId);
    },
    
    isTestTimer: function()
    {
    	if (this.test) {
    		//alert(this.test.test_time);
    		return (this.test.test_time > 0);
    	}
    },
	
	setTimer: function(sec)
	{
		updateTimer(this.elementTimer.id, sec);
	},
	
	onTimerChange: function()
	{
		if (this.elementTimer.value <= 0) {
			
			if (this.isTestTimer()) {
				this.goNext();
			}
			else {
				this.finish();
			}
			
		}
	}
}