function CountChecks(questionId, choiceId, maxchecked, choiceCount) {

var count = 0;
var i=0;
for(i=1; i <= choiceCount; i++){
	var checkboxId = "checkboxId" + questionId+'' + i +'';
	if(document.getElementById(checkboxId).checked == true) { count++; }
}
var currentCheckboxId = "checkboxId" + questionId+'' + choiceId +'';
if(count > maxchecked) {
	document.getElementById(currentCheckboxId).checked = false;
	alert('Maximaal ' + maxchecked + ' antwoorden mogen worden aangevinkt.');
	}
}
/*
 * Change to another page, but submit the existing dimension first
 */

function change_page(dest)
{
        var button = document.getElementById('dimension_submit');
        if (button)
        {    
             var hidden = document.getElementById('dimension_next_page');
             hidden.value = dest
             button.click();
        }
        else
        {       
             document.location = dest;
        }
}

// Popup a message box when answer a question with a certain value
// - do this once per page.
var didShowMultipleChoiceAlert = false;

function showMultipleChoiceAlert(msg)
{
        if (!didShowMultipleChoiceAlert)
        {
                didShowMultipleChoiceAlert = true;
                alert(msg);
        }       
}

function setFocusTo(id, clear)
{
        var input = document.getElementById(id);

        if (input)
        {       
                // Clear existing text from the element.
                if (clear && input.type == "text")
                   input.value = "";

                input.focus();
                if (input.type == "text")
                	input.select(); // Select all the text in the field
        }
}

function focusPopUp(id)
{
	window.location.hash = id;
}

function getIFrameDocument(aID)
{ 
 var rv = null; 

 // if contentDocument exists, W3C compliant (Mozilla) 
 if (document.getElementById(aID).contentDocument){ 
   rv = document.getElementById(aID).contentDocument; 
 } else { 
   // IE 
   rv = document.frames[aID].document; 
 } 

 return rv; 
  } 


function makeQuestionActive(questionId)
{
	// make all other questions elements on the page inactive
	questions = document.getElementsByClassName('question');
	
	// Don't do anything if the question was already active
	var activeQuestion = document.getElementsByClassName('active')[0];
	if (activeQuestion && (activeQuestion.id == questionId))
		return

	questions.each(function(question) 
	{
	 	questions = $('expert_mode_questions_' + question.id);
		if (question.id != questionId)
		{
			Element.removeClassName(question, 'active');
			if (questions)	
				Element.hide(questions);
		}
		else
		{
			Element.addClassName(question, 'active');
			if (questions)
				Element.show(questions);
				
			closeQuestion = $('close_question_' + questionId);
   	        Element.show(closeQuestion);
		}
				
		if (Element.hasClassName(question, "with_multiple_questions"))
		{
  			updateQuestion(question);
		}
	});  
}

function makeQuestionInactive(questionId)
{
	question = $(questionId);
	
 	expertQuestions = $('expert_mode_questions_' + questionId);
	Element.removeClassName(question, 'active');
	if (expertQuestions)	
		Element.hide(expertQuestions);
		
	closeQuestion = $('close_question_' + questionId);
	Element.hide(closeQuestion);
}

var globalQuestionId = "";
var globalAnswerPos = "";

function openVote(questionId, answerPos)
{
	var url = 'open_vote.do';
	var container = 'expert_mode_answer_report_' + questionId + '_' + answerPos;
	globalQuestionId = questionId;
	globalAnswerPos = answerPos;
	new Ajax.Updater(container, url,
	  { parameters : 'question=' + questionId + '&answer_pos=' + answerPos,
		onSuccess: updateVoteButtons,
		
	    onFailure: reportError }
	 );

}

function closeVote(questionId, answerPos)
{
	var url = 'close_vote.do';
	var container = 'expert_mode_answer_report_' + questionId + '_' + answerPos;
	globalQuestionId = questionId;
	globalAnswerPos = answerPos;
	
	if (intermediateVoteCountUpdater)
	{
		intermediateVoteCountUpdater.stop();
    }
	
	new Ajax.Updater(container, url,
	  { parameters : 'question=' + questionId + '&answer_pos=' + answerPos,
		onSuccess: updateQuestions,  // also updates the answer div
	    onFailure: reportError }
	 );
}

function updateAnswer(request)
{
	// Show the changed answer
	var url = 'include/answer.jsp';
	var container = 'answer_' + globalQuestionId;
	new Ajax.Updater(container, url,
	  { parameters: 'question=' + globalQuestionId,
	    onSuccess: updateVoteButtons,
	    onFailure: reportError }
	 );
}

function updateVoteButtons(request)
{
	var url = "include/expert_session/vote_buttons.jsp";
	var container = "vote_buttons_" + globalQuestionId + "_" + globalAnswerPos;
	new Ajax.Updater(container, url,
	  { parameters : 'question=' + globalQuestionId + '&answer_pos=' + globalAnswerPos,
	    onSuccess: updateIntermediateVoteCount,
	    onFailure: reportError }
	 );
}

var intermediateVoteCountUpdater;

function updateIntermediateVoteCount(request)
{
	var url = "intermediate_vote.do";
	var container = "intermediate_vote_count_" + globalQuestionId + "_" + globalAnswerPos;
	intermediateVoteCountUpdater = new Ajax.PeriodicalUpdater(container, url,
	  { frequency: 0.5,
	    parameters : '',
	    onFailure: reportError }
	 );
		
}

var nextFocus = '';

function setSimpleNumberAnswer(questionId, fieldId, focusElement)
{
	var url = 'set_simple_number_answer.do';
	var value = $(fieldId).value;
	var container = 'not_used';

	nextFocus = focusElement;
	new Ajax.Updater(container, url,
	  { parameters : 'question=' + questionId + '&value=' + value,
		onSuccess: updateQuestions,
	    onFailure: reportError }
	 );
}

// Redisplay all questions on the page.
function updateQuestions()
{
	// All divs containing a question.
	var questionDivs = document.getElementsByClassName('question');
	for (i = 0; i < questionDivs.length; i++)
	{
		var questionDiv = questionDivs[i];
		updateQuestion(questionDiv);
	}
}

function updateQuestion(questionDiv)
{
 		var url = "include/question.jsp";
		var active = Element.hasClassName(questionDiv, 'active');
		new Ajax.Updater(questionDiv.id, url,
		     { parameters: "question=" + questionDiv.id + (active ? '&active=1' : ''),
		       onSuccess: setFocusAfterQuestionUpdate,
		       onFailure: reportError
		     });
}

function setFocusAfterQuestionUpdate()
{
	setFocusOn(nextFocus, false /* clear */);
}
 
function initVotingSystem(params)
{
	var url = 'init_voting_system.do';
	var container = 'init_voting_system_report';
	
	
		
	Element.update($(container), "");
	new Ajax.Updater(container, url,
	    {
	     parameters: params,
	     onFailure: reportError 
	     }
	 );
}

function stopInitVotingSystem()
{
	var url = 'init_voting_system.do';
	var container = 'init_voting_system_report';
	
	new Ajax.Updater(container, url,
	    {
	     parameters: "stop=1",	  
	     onFailure: reportError }
	 );
}

function reportError(request)
{
	alert("error during request " + request);
}
