
var direction   = "";
var score_vraag = "";

if(document.getElementById("direction") != null)
	direction = document.getElementById("direction").value;

/**
 *
 */
function welkeStap(teller_stap)
{
	document.getElementById("stap_" + teller_stap).style.display = "block";
}

var punten_id         = "";
var punten_type       = "";
var enquete_id        = "";
var enquete_type      = "";
var ammoniak_vraag_id = null;

/*
*/
function enquetePunten(nge, enquete_id)
{
	punten_type = nge;
	
	if(punten_id != "")
	{
		document.getElementById("a_" + punten_id).style.backgroundImage = "url(" + ROOT_PATH + "image/radio_off.gif)";
	}
	
	document.getElementById("a_" + nge).style.backgroundImage = "url(" + ROOT_PATH + "image/radio_on.gif)";
		
	punten_id = nge;
	
	document.getElementById("punten_id").value  = nge;
	document.getElementById("enquete_id").value = enquete_id;
}

/*
*/
function enqueteType(enquete)
{
	enquete_type = enquete;
	
	if(enquete_id != "")
	{
		document.getElementById("a_" + enquete_id).style.backgroundImage = "url(" + ROOT_PATH + "image/radio_off.gif)";
	}
	
	document.getElementById("a_" + enquete).style.backgroundImage = "url(" + ROOT_PATH + "image/radio_on.gif)";
		
	enquete_id = enquete;
	
	document.getElementById("enquete_id").value = enquete;
}

/*
*/
function selecteerPunten()
{	
	if(punten_type != "")
		document.location.href = "index.php?stap=2&enquete=" + document.getElementById("enquete_id").value + "&punten=" + document.getElementById("punten_id").value;
	else
		alert("U heeft uw type bedrijf niet ingevuld");
}

/*
*/
function startEnquete()
{	
	if(enquete_type != "")
		document.location.href = "index.php?stap=2&enquete=" + document.getElementById("enquete_id").value;
	else
		alert("U heeft geen type stal geselecteerd");
}

/*
*/
function selecteerAntwoord(nieuw_id, huidig_id)
{
	//(de)selecteer antwoord
	if(huidig_id > 0)
		document.getElementById("a_" + huidig_id).style.backgroundImage = "url(" + ROOT_PATH + "image/radio_off.gif)";
	
	if(document.getElementById("a_" + nieuw_id) != null)
		document.getElementById("a_" + nieuw_id).style.backgroundImage = "url(" + ROOT_PATH + "image/radio_on.gif)";	
}

/*
*/
var maatlat = null;

function toonDetails(id)
{
    if(maatlat != null)
        document.getElementById(maatlat).style.display = "none";

    maatlat = id;

    document.getElementById(id).style.display = "block";
}

/*
*/
function toonOmschrijving()
{
	if(document.getElementById('omschrijving').style.display != "block")
		document.getElementById('omschrijving').style.display = 'block';
	else
		document.getElementById('omschrijving').style.display = 'none';
}


/* SCORE(S) */

/**
 *	ammoniak maatlat score 
 */
function maatlatScore(antwoord_id)
{	
	//selecteer antwoord
	selecteerAntwoord(antwoord_id, ammoniak_vraag_id);
	
	ammoniak_vraag_id = antwoord_id;
	
	document.getElementById("antwoord_id").value = antwoord_id;
	
	//score(s)
	var min_score = parseInt(document.getElementById("min_score_ammoniak").value);
	var score     = 0;
	
	if(document.getElementById("antwoord_" + antwoord_id + "_score") != null)
		score = parseInt(document.getElementById("antwoord_" + antwoord_id + "_score").value);
	
	document.getElementById("teller_1_behaald").innerHTML = score + " pnt";
	document.getElementById("score_ammoniak").value       = score;
	
	//tellen background
	if(min_score <= score)
		document.getElementById("teller_1").style.backgroundImage = "url(" + ROOT_PATH + "image/teller_ammoniak.gif)";
	else
		document.getElementById("teller_1").style.backgroundImage = "url(" + ROOT_PATH + "image/teller_ammoniak_rood.gif)";
		
	//vrije ruimte
	vrijeRuimte(score - min_score);
}

/**
 *	vraag
 */
function antwoord(teller_type, vraag_id, antwoord_id, update_score)
{
	var vraag_type         = document.getElementById("vraag_" + vraag_id + "_type").value;
	var vraag_antwoord_obj = document.getElementById("vraag_" + vraag_id + "_antwoord");
	var antwoord_score     = parseInt(document.getElementById("antwoord_" + vraag_id + "_" + antwoord_id + "_score").value);

	//score(s)
	var min_score          = parseInt(document.getElementById("min_score_" + teller_type).value);
	var score              = parseInt(document.getElementById("antwoord_" + vraag_id + "_" + antwoord_id + "_score").value);

	//selecteer antwoord
	if(vraag_type == "een_antwoord")
	{	//radio
		if(vraag_antwoord_obj.value != "")
		{
			document.getElementById("a_" + vraag_id + "_" + vraag_antwoord_obj.value).style.backgroundImage = "url(" + ROOT_PATH + "image/radio_off.gif)";
			document.getElementById("antwoord_" + vraag_id + "_" + vraag_antwoord_obj.value).value = "";
		}
		
		document.getElementById("a_" + vraag_id + "_" + antwoord_id).style.backgroundImage = "url(" + ROOT_PATH + "image/radio_on.gif)";
		document.getElementById("antwoord_" + vraag_id + "_" + antwoord_id).value = "1";
		
		vraag_antwoord_obj.value = antwoord_id;
	}
	else
	{	//checkbox
		var checkbox = document.getElementById("a_" + vraag_id + "_" + antwoord_id);
		var antwoord = document.getElementById("antwoord_" + vraag_id + "_" + antwoord_id);

		if(antwoord.value != "1")
		{
			checkbox.style.backgroundImage = "url(" + ROOT_PATH + "image/checkbox_on.gif)";
			antwoord.value = "1";
		}
		else
		{
			checkbox.style.backgroundImage = "url(" + ROOT_PATH + "image/checkbox_off.gif)";
			antwoord.value = "0";
		}
	}

	if(update_score == true)
	{
		//type teller	
		var teller_score = parseInt(document.getElementById("score_" + teller_type).value);
		
		if(direction == "back")
		{
			//teller_score                                          = (teller_score - parseInt(score_vraag));
			//document.getElementById("score_" + teller_type).value = teller_score;
			//direction                                             = "";		
		}		
		
		var nieuwe_score = 0;
		
		//antwoord opslaan
		if(vraag_type == "een_antwoord")
		{
			var totaal_score_vraag = document.getElementById("totaal_score_vraag").value;
			
			if(totaal_score_vraag == "")
				totaal_score_vraag = 0;
			else
				totaal_score_vraag = parseInt(totaal_score_vraag);
			
			document.getElementById("totaal_score_vraag").value = antwoord_score;
			
			nieuwe_score = (teller_score - totaal_score_vraag + antwoord_score);
			
			zetTellerScore(teller_type, nieuwe_score);
		}
		else
		{
			var totaal_score_vraag = document.getElementById("totaal_score_vraag").value;
			
			if(totaal_score_vraag == "")
				totaal_score_vraag = 0;
			else
				totaal_score_vraag = parseInt(totaal_score_vraag);
			
			//document.getElementById("totaal_score_vraag").value = antwoord_score;			
			
			if(antwoord.value == "1")
			{	//erbij	
				//document.getElementById("totaal_score_vraag").value = antwoord_score;			
				
				nieuwe_score = (teller_score - totaal_score_vraag + antwoord_score);
			
				zetTellerScore(teller_type, nieuwe_score);				
			}
			else
			{	//eraf
				//document.getElementById("totaal_score_vraag").value = antwoord_score;
				
				nieuwe_score = (teller_score - antwoord_score);
			
				zetTellerScore(teller_type, nieuwe_score);	
			}
		}
		
		//vrije ruimte
		var min_score = parseInt(document.getElementById("min_score_" + teller_type).value);
		
		if(min_score <= nieuwe_score)
		{
			document.getElementById("teller_" + typeTeller(teller_type)).style.backgroundImage = "url(" + ROOT_PATH + "image/teller_" + teller_type + ".gif)";
			
			//tel of het verschil of de totale score er bij op
			if(teller_score > min_score)
				vrijeRuimte(parseInt(document.getElementById("score_vrijeruimte").value) + antwoord_score);
			else
				vrijeRuimte(parseInt(document.getElementById("score_vrijeruimte").value) + nieuwe_score - min_score);
		}
		else
		{
			document.getElementById("teller_" + typeTeller(teller_type)).style.backgroundImage = "url(" + ROOT_PATH + "image/teller_" + teller_type + "_rood.gif)";
			
			vrijeRuimte(parseInt(document.getElementById("score_vrijeruimte").value));
		}
	}
}

/**
 *
 */
function zetTellerScore(teller_type, score)
{	
	document.getElementById("score_" + teller_type).value = score;
	
	document.getElementById("teller_" + typeTeller(teller_type) + "_behaald").innerHTML = score + " pnt";
}
 
/**
 *
 */
function typeTeller(type)
{
	if(type == "dierenwelzijn")
		return 2;
	if(type == "dierengezondheid")
		return 3;
	if(type == "energie")
		return 4;
		
}
 
/**
 *	vrijeruimte
 */
function vrijeRuimte(score)
{	
	var min_score     = parseInt(document.getElementById("min_score_vrijeruimte").value);
	var huidige_score = parseInt(document.getElementById("score_vrijeruimte").value);
	
	if(direction == "back")
		score = score - score_vraag;
	
	//score
	if(score >= 0)
		document.getElementById("teller_5_behaald").innerHTML = score + " pnt";

	//achtergrond
	if(min_score <= (huidige_score + score))
		document.getElementById("teller_5").style.backgroundImage = "url(" + ROOT_PATH + "image/teller_vrijeruimte.gif)";
	else
		document.getElementById("teller_5").style.backgroundImage = "url(" + ROOT_PATH + "image/teller_vrijeruimte_rood.gif)";
}

/**
 *
 */
function vraagopsturen(vraag_id, vraag_nr, richting)
{	
    if(vraag_nr != "")
        document.getElementById("goto").value = vraag_nr;
		
	if(richting != "")
		document.getElementById("direction").value = richting;
	
	document.getElementById("enquete").submit();
}

/**
 *
 */
function maatlatopsturen()
{
    var antwoord_id = document.getElementById("antwoord_id");

    if(antwoord_id.value == "")
        alert("U heeft geen maatlat geselecteerd.");
    else
        document.getElementById("maatlat").submit();
}

/**
 *
 */
function afronden()
{
	document.getElementById("enquete").submit();
}
