//Init "constant" variables. 
var maxQuestions = 20;
var minQuestions = 5;
var minAnswerOptions = 2;
var maxAnswerOptions = 5;

//Set current question to minimum number of questions.
var questionNo = minQuestions;

//Function to add new questions to the form.
function addQuestion()
{
  questionNo++;
  document.getElementById("question"+questionNo).style.display = document.all ? "block" : "table";
  
  //if there are more questions than the minimum, display remove link
  if(questionNo >= minQuestions)
    document.getElementById("removeQuestion").style.display = document.all ? "block" : "block";
  
  //if there are more questions than the maximum, hide the add link
  if(questionNo >= maxQuestions) 
    document.getElementById("addQuestion").style.display = "none";
}

//Function to remove questions from the form.
function removeQuestion()
{
  document.getElementById("question"+questionNo).style.display = "none";
  document.getElementById("question["+(questionNo-1)+"]").value = "";
  for (var option = 0; option < 5; option++)
  {
	document.getElementById("answerOption["+(questionNo-1)+"]["+option+"]").value = "";
  }
  questionNo--;
  
  //if there are less questions than the minimum, hide the remove link
  if(questionNo <= minQuestions) 
    document.getElementById("removeQuestion").style.display = "none";
  
  //if there are less questions than the maximum, display add link
  if(questionNo < maxQuestions) 
    document.getElementById("addQuestion").style.display = document.all ? "block" : "block";
}

//Set all answer options to minimum number of options.
var answerOption = new Array ();
for (var i = 0; i < maxQuestions; i++)
{
  answerOption[i] = minAnswerOptions;
}

//Function to add new answer options to the form.
function addAnswerOption(question)
{
  answerOption[question]++;
  document.getElementById("answer"+question+answerOption[question]).style.display = document.all ? "block" : "table-row";
  
  //if there are more options than the minimum, display remove link
  if(answerOption[question] >= minAnswerOptions)
    document.getElementById("removeAnswerOption"+question).style.display = document.all ? "block" : "block";
  
  //if there are more options than the maximum, hide the add link
  if(answerOption[question] >= maxAnswerOptions) 
    document.getElementById("addAnswerOption"+question).style.display = "none";
}

//Function to remove answer options from the form.
function removeAnswerOption(question)
{
  document.getElementById("answer"+question+answerOption[question]).style.display = "none";
  document.getElementById("answerOption["+(question)+"]["+(answerOption[question]-1)+"]").value = "";
  answerOption[question]--;
  
  //if there are less options than the minimum, hide the remove link
  if(answerOption[question] <= minAnswerOptions) 
    document.getElementById("removeAnswerOption"+question).style.display = "none";
  
  //if there are less options than the maximum, display add link
  if(answerOption[question] < maxAnswerOptions) 
    document.getElementById("addAnswerOption"+question).style.display = document.all ? "block" : "block";
}
