// The node that contains the question nodes
var questionsContainerName = 'questions';
var resultsContainerName = 'results';
var numberOfQuestions = 5;
var numberOfOutcomes = 3;
// The prefix on the "name" attribute of each question's radio buttons
var questionElementPrefix = 'question';
var resultOutcomePrefix = 'result';
var messageForNoneChosen = 'Please pick an answer before proceeding';
// The type of node that stores each question
var htmlQuestionTagName = 'div';
// Style states - turning on and off
var styleDisplayOff = 'none';
var styleDisplayOn = 'block';

//var myheighter = new fx.Height('questions', {duration: 700});
//var myfadesize = new fx.FadeSize('questions', {duration: 600});
//myfadesize.hide('height');

var answers = new Array(numberOfQuestions);

function save(questionId) {
  // Fetch the radio buttons for the given question id
  radioButtons = document.getElementsByName(questionElementPrefix + parseInt(questionId));
  choseAnswer = false;
  // Loop over each radio button
  for (i = 0; i < radioButtons.length; i++) {
    if (radioButtons[i].checked) {
      // Save the selected value (question 1 is first answer in array hence the "- 1")
      answers[questionId - 1] = radioButtons[i].value;
      choseAnswer = true
    }
  }
  // Make sure they chose one.
  if (!choseAnswer) {
    alert(messageForNoneChosen);
    return false;
  } else {
    return true;
  }
}

function next() {
  //var t = setTimeout('myToggle()', 1000);
  // Fetch needed nodes
  resultsContainerNode = document.getElementById(resultsContainerName);
  questionsContainerNode = document.getElementById(questionsContainerName);
  questionsContainerNode.style.display = "block";
  questionNodes = questionsContainerNode.getElementsByTagName(htmlQuestionTagName);
  
  // Show the next question that has no defined answer
  nextQuestion = -1;
  for (i = 0; i < answers.length; i++) {
    if (answers[i] === undefined) {
      nextQuestion = i;
      break;
    }
  }
  // All questions are answered, show the results.
  if (nextQuestion == -1) {
    // Set all outcome values to 0
    outcomeCounts = new Array(numberOfOutcomes);
    for (i = 0; i < numberOfOutcomes; i++) {
      outcomeCounts[i] = 0;
    }
    // Frequency analysis
    for (i = 0; i < answers.length; i++) {
      outcomeCounts[answers[i] - 1]++;
    }
    // The following works out the minimum count required to show an outcome.
    // Note: doesn't work in all combinations
    countForOutput = Math.round(numberOfQuestions / numberOfOutcomes);
    // Loop over the various outcomes, choosing which to select.
    resultCombinations = "";
    for (i = 0; i < outcomeCounts.length; i++) {
      if (outcomeCounts[i] >= countForOutput) {
        resultCombinations = resultCombinations + String(i + 1);
      }
    }
    // Readjust combination strings in case they are processed in reverse order.
    if (resultCombinations.length == 2) {
      if (resultCombinations == "21") {
        resultCombinations = "12";
      } else if (resultCombinations == "31") {
        resultCombinations = "13";
      } else if (resultCombinations == "32") {
        resultCombinations = "23";
      }
    }
    resultsOutputNode = document.getElementById(resultOutcomePrefix + resultCombinations);

    resultsOutputNode.style.display = styleDisplayOn;

    // Turn the questions off, and the results on.
    resultsContainerNode.style.display = styleDisplayOn;
    questionsContainerNode.style.display = styleDisplayOff;

    // Now wipe the values.
    clearAnswers();
    return true;
  } else {
    // Ensure results node is not being displayed by any bugs
    resultsContainerNode.style.display = styleDisplayOff;
  }
  // Hide all except the one we want to show.
  for (i = 0; i < questionNodes.length; i++) {
    questionNodes[i].style.display = (nextQuestion == i) ? styleDisplayOn : styleDisplayOff;
  }
  //var t = setTimeout('myToggle()', 1000);
  //var t = setTimeout('myToggle()', 1000);
}

function myToggle() {
	myfadesize.toggle('height');
}

function clearAnswers() {
  for (questionId = 1; questionId <= numberOfQuestions; questionId++) {
    radioButtons = document.getElementsByName(questionElementPrefix + String(questionId));
    for (radioButtonId = 0; radioButtonId < radioButtons.length; radioButtonId++) {
      radioButtons[radioButtonId].checked = false;
    }
  }
}

window.onload = function() {
	next();
}
