var questionSet01;
createQuestions();

function createQuestions()
{
	// Check that the getElementById method is available - otherwise the page won't work
	if (document.getElementById) 
	{
		questionSet01 = new questionSet();
	}
	else
	{
		window.location.replace('browserProblem.html');
	}
}

function questionSet()
// Constructor for the questionSet object
{
	// instance variables
	this.questions = new Array();
	this.numberOfQuestions = 0;
	// methods
	this.setUpQuestions = setUpQuestions;
	this.showLatestScores = showLatestScores;
	// run methods to initialise variables
	this.setUpQuestions();
}

function question(questionId)
{
	// Constructor for the question object
	// instance variables
	this.questionId = questionId;
	this.button = null;
	this.feedbackItems = new Array();
	this.finalFeedback = null;
	this.inputsInQuestion = document.getElementById(questionId).getElementsByTagName('input');
	this.divsInQuestion = document.getElementById(questionId).getElementsByTagName('div');
	this.animal = null;
	this.optionChosen = null;
	// methods
	this.findButton = findButton;
	this.findFeedbackItems = findFeedbackItems;
	this.findFinalFeedback = findFinalFeedback;
	this.findOptionChosen = findOptionChosen;
	this.hideAllFeedback = hideAllFeedback;
	this.displayFeedback = displayFeedback;
	this.findAnimal = findAnimal;
	this.submit = submit;
	this.reset = reset;
	// run methods to initialise variables
	this.findButton();
	this.findFinalFeedback();
	this.findFeedbackItems();
}
function findButton()
{
	for (j = 0; j < this.divsInQuestion.length; j++) 
	{
		if(this.divsInQuestion[j].className.toLowerCase() == 'button')
		{
			this.button = this.divsInQuestion[j];
		}
	}
}
function findFinalFeedback()
{
	for (j = 0; j < this.divsInQuestion.length; j++) 
	{
		if(this.divsInQuestion[j].className.toLowerCase() == 'finalfeedback')
		{
			this.finalFeedback = this.divsInQuestion[j];
		}
	}
}
function setUpQuestions()
{
	var divsInDocument = document.getElementsByTagName('div');
	for (i = 0; i < divsInDocument.length; i++) 
	{
		if(divsInDocument[i].className.toLowerCase() == 'question')
		{
			this.questions[this.numberOfQuestions] = new question(divsInDocument[i].id);
			this.numberOfQuestions ++;
		}
	}
}
function findOptionChosen()
{
	for (j = 0; j < this.inputsInQuestion.length; j++)
	{
		if (this.inputsInQuestion[j].checked == true)
		{
			this.optionChosen = j;
		}
	}
}
function displayFeedback()
{
	this.feedbackItems[this.optionChosen].className = 'ppFeedbackDisplayed';
	this.finalFeedback.innerHTML = '<img src="' + this.animal + '.gif" alt="' + this.animal + '"/>';
	
}
function hideAllFeedback()
{
	for (j = 0; j < this.divsInQuestion.length; j++)
	{
		if (this.divsInQuestion[j].className.toLowerCase() == 'ppfeedbackdisplayed')
		{
			this.divsInQuestion[j].className = 'ppFeedback';
		}
	}
	
}
function findFeedbackItems()
{
	var counter = 0;
	for (j = 0; j < this.divsInQuestion.length; j++)
	{
		if (this.divsInQuestion[j].className.toLowerCase() == 'ppfeedback')
		{
			this.feedbackItems[counter] = this.divsInQuestion[j];
			counter ++;
		}
	}
}
function findAnimal() 
{
	var spansInFeedback = this.feedbackItems[this.optionChosen].getElementsByTagName('span');
	for (j = 0; j < spansInFeedback.length; j++)
	{
		if (spansInFeedback[j].className == 'animal')
		{
			this.animal = spansInFeedback[j].innerHTML.toLowerCase();
		}
	}
}
function submit()
{
	this.hideAllFeedback();
	this.findOptionChosen();
	if (this.optionChosen == null)
	{
		this.finalFeedback.innerHTML = '<p class="alert">Please make a selection.</p>';
		return;
	}
	this.findAnimal();
	this.displayFeedback();
}
function reset()
{
	this.hideAllFeedback();
	this.finalFeedback.innerHTML = '';
	this.animal = null;
	this.optionChosen = null;
	for (j = 0; j < this.inputsInQuestion.length; j++)
	{
		this.inputsInQuestion[j].checked = false;
	}

}
function showLatestScores()
{
	var donkeyAnswers = 0;
	var sheepAnswers = 0;
	var foxAnswers = 0;
	var owlAnswers = 0;
	for (i = 0; i < this.questions.length; i++)
	{
		switch (this.questions[i].animal)
		{
			case 'donkey' :
				donkeyAnswers ++;
				break;
			case 'sheep' :
				sheepAnswers ++;
				break;
			case 'fox' :
				foxAnswers ++;
				break;
			case 'owl' :
				owlAnswers ++;
				break;
		}
	}
	var questionsAnswered = donkeyAnswers + sheepAnswers + foxAnswers + owlAnswers;
	var highestScore = donkeyAnswers;
	var highestScoringAnimal = 'donkey';
	if (highestScore <= sheepAnswers)
	{
		highestScore = sheepAnswers;
		highestScoringAnimal = 'sheep';
	}
	if (highestScore <= foxAnswers)
	{
		highestScore = foxAnswers;
		highestScoringAnimal = 'fox';
	}
	if (highestScore <= owlAnswers)
	{
		highestScore = owlAnswers;
		highestScoringAnimal = 'owl';
	}
	document.getElementById('profileButton').innerHTML = 'Update overall profile';
	// Assemble the results HTML
	var resultsHTML = '<h2>Your negotiation profile</h2><ul><li>Donkey answers: ' + donkeyAnswers + '</li><li>Sheep answers: ' + sheepAnswers + '</li><li>Fox answers: ' + foxAnswers + '</li><li>Owl answers: ' + owlAnswers + '</li></ul>';
	if (questionsAnswered > 0)
	{
		resultsHTML += '<p>Your negotiation style is:<br /><img src="' + highestScoringAnimal + '.gif" alt="' + highestScoringAnimal + '"/></p>';
	}
	document.getElementById('negotiationprofile').innerHTML = resultsHTML;
}

function showHide(element) {
	if (document.getElementById(element).style.display == 'block') {
		document.getElementById(element).style.display = 'none';
	}
	else {
		document.getElementById(element).style.display = 'block';
	}
}

