var disabled = true;

function displayOptions()
{

	var _disabled = ( document.getElementById("frage").value == "");

	if( _disabled != disabled )
	{
		var inputs = document.getElementById("optionForm").getElementsByTagName("input");
		var selects = document.getElementById("optionForm").getElementsByTagName( "select" );

		for( var i = 0, s = 0; i < inputs.length || s < selects.length; i++,s++ )
		{
			if( _disabled ) 
			{
				if( i < inputs.length )
					inputs[i].setAttribute( "disabled", "disabled" );
				if( s < selects.length )
					selects[s].setAttribute( "disabled", "disabled" );
			}
			else
			{	
				if( i < inputs.length )
					inputs[i].removeAttribute( "disabled" );
				if( s < selects.length )
					selects[s].removeAttribute( "disabled" );
			}
		}

		disabled = _disabled;
		if( modelOptForm == null ) initOptForm();
		if( optionCount <= 2 ) document.getElementById("buttonRemoveOption").setAttribute( "disabled", "disabled" );

		disabledSwitch( "hasEnddate" );
	}
}

var optionCount = 2;
var modelOptForm = null;

function initOptForm(){
	modelOptForm = document.getElementById("option0").cloneNode(true);
	optionCount = document.getElementById("options").childNodes.length - 1;
	numMinChoices = document.getElementById("numMinChoices").firstChild.nodeValue;
	numMaxChoices = document.getElementById("numMaxChoices").firstChild.nodeValue;
}

function addOption(){
	if( modelOptForm == null ) initOptForm();
	var optionForm = modelOptForm.cloneNode( true );
	optionForm.setAttribute( "id", "option" + (optionCount+1) );
	optionForm.removeAttribute( "style" );
	replaceValue = optionCount+1;
	editChilds( optionForm );
	options = document.getElementById("options");
	options.insertBefore( optionForm, options.childNodes[optionCount+1] );

	var choice = document.createElement( "option" );
	choice.setAttribute( "value", (optionCount+1) );
	choice.appendChild( document.createTextNode( optionCount+1 ) );

	document.getElementById("maxChoices").appendChild( choice );
	optionCount++;
	if( optionCount > 2 ) document.getElementById( "buttonRemoveOption").removeAttribute( "disabled" );
}


function removeOption()
{
	if( modelOptForm == null ) initOptForm();
	options = document.getElementById("options" ).childNodes;
	if( options.length > 2 )
	{
		document.getElementById("options").removeChild( options[ options.length-1 ] );
		
		maxChoices = document.getElementById("maxChoices");
		minChoices = document.getElementById("minChoices");
		maxchoices = maxChoices.getElementsByTagName("option");
		minchoices = minChoices.getElementsByTagName("option");

		if( numMinChoices == optionCount )
		{
			minChoices.selectedIndex = numMinChoices-2;
			minChoices.removeChild( minchoices[ minchoices.length-1 ] );

			//maxChoices hat nur ein Kindelement; anstatt zu loeschen wird es ersetzt:
			maxchoices[0].value = maxchoices[0].value - 1;
			maxchoices[0].firstChild.nodeValue = parseInt( maxchoices[0].firstChild.nodeValue) - 1;

			numMinChoices--;
			numMaxChoices--;
		}
		else 
		{
			if( numMaxChoices == optionCount )
			{
				minChoices.removeChild( minchoices[ minchoices.length-1 ] );

				maxChoices.selectedIndex = numMaxChoices-numMinChoices-1;
				numMaxChoices--;
			}
			maxChoices.removeChild( maxchoices[ maxchoices.length-1 ] );

		}

		optionCount--;
		displayChoices();
		if( optionCount <= 2 ) document.getElementById("buttonRemoveOption").setAttribute( "disabled", "disabled" );
	}
}

var numMinChoices = 1;
var numMaxChoices = 1;

function displayChoices()
{
	if( modelOptForm == null ) initOptForm();
	var minChoices = document.getElementById( "minChoices");
	var maxChoices = document.getElementById( "maxChoices");

	var _numMinChoices = parseInt( minChoices.options[minChoices.selectedIndex].value);
	var _numMaxChoices = parseInt( maxChoices.options[maxChoices.selectedIndex].value );

	//alert( numMinChoices + ":" + numMaxChoices );
	//alert( _numMinChoices + ":" + _numMaxChoices );
	

	var choices = maxChoices.getElementsByTagName( "option" );

	for( c = optionCount-1; c >= 0; c-- )
	{
		if(c+1 < _numMinChoices)
		{
			if( c+1 >= numMinChoices ) maxChoices.removeChild( choices[c+1-numMinChoices] );
		}
		else
		{
			if( c+1 < numMinChoices )
			{
				choice = document.createElement( "option");
				choice.setAttribute( "value", c+1 );
				choice.appendChild( document.createTextNode( c+1 ) );
				maxChoices.insertBefore( choice, choices[0] );
			}
		}
			
	}

	choices = minChoices.getElementsByTagName( "option" );

	for( c = 0; c < optionCount; c++ )
	{
		if( c+1 > _numMaxChoices )
		{
			if( c+1 <= numMaxChoices ) minChoices.removeChild( choices[_numMaxChoices] );
		}
		else
		{
			if( c+1 > numMaxChoices ) 
			{
				choice = document.createElement( "option");
				choice.setAttribute( "value", c+1 );
				choice.appendChild( document.createTextNode( c+1 ) );
				minChoices.appendChild( choice );
			}
		}

	}

	numMinChoices = _numMinChoices;
	numMaxChoices = _numMaxChoices;

}

function showResults()
{
	results = document.getElementsByName( "result" );
	for( var r = 0; r < results.length; r++)
		results[r].style.visibility="visible";
	document.getElementById( "showResult" ).removeChild( document.getElementById( "showResult" ).firstChild );
}

function checkChecked()
{
	numMinChoices = document.getElementById("numMinChoices").firstChild.nodeValue;
	numMaxChoices = document.getElementById("numMaxChoices").firstChild.nodeValue;


	var checked = 0;
	umfrage = document.getElementById( "umfrage" );
	checkboxes = umfrage.getElementsByTagName( "input" );
	for( var c = 0; c < checkboxes.length; c++ )
	{
		if( checkboxes[c].checked ) checked++;
	}

	if( checked >= numMinChoices && checked <= numMaxChoices )
		document.getElementById( "submit" ).removeAttribute( "disabled" );
	else
		document.getElementById( "submit" ).setAttribute( "disabled", "disabled" );

	if( checked == numMaxChoices )
		for( c = 0; c < checkboxes.length; c++ )
		{
			if( !checkboxes[c].checked )
				checkboxes[c].setAttribute( "disabled", "disabled" );
		}
	else	
		for( c = 0; c < checkboxes.length; c++ )
		{
			if( !checkboxes[c].checked ) 
				checkboxes[c].removeAttribute( "disabled" );
		}
}

function umfrageSwitch()
{
	if( document.getElementById("umfrageAktiviert").checked )
	{
		document.getElementById("umfrage").style.display = "block";
	}
	else
	{
		document.getElementById("umfrage").style.display = "none";
	}
}

function checkOptions()
{
	if( document.getElementById( "umfrageAktiviert").checked )
	{
		options = document.getElementById("options").getElementsByTagName("input");
		var error = 0;
		for( var o = 1; o < options.length; o++)
			if( options[o].type=="text" && options[o].value == "" ) error++; 

		if( error > 0 ) 
		{
			alert( "Fehler! Eine oder mehrere Optionen sind nicht definiert." );
			return false;
		}
	}
	return true;
	
}
