var timeout = null;

function addIndustry( id, name )
{

	var $countChilds = 0;
	
	for (var $i=0; $i<$('selectedIndustries').childNodes.length; $i++)
	{
		if ($('selectedIndustries').childNodes[$i].tagName == 'LI') $countChilds++;
	}
	
	if ($countChilds < 5)
	{
		if ($('industry_' + id) != null) return;
		
		$('selectedIndustries').innerHTML += '<li style="width:410px;"><div>' + name + '<a href="javascript:void(0);" onclick="this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);"><img src="images/cross.png" width="16" height="16" border="0" /></a></div><input type="hidden" id="industry_' + id + '" name="industry[]" value="' + id + '" /></li>';
	} else {
		alert("Maximal 5 Branchen möglich");
	}

	/*
	var container = document.getElementById('selectedIndustries');
	for( var i=0; i<container.childNodes.length; i++)
	{
		if( container.childNodes[i].tagName != 'LI')
		{
			container.removeChild( container.childNodes[i] );
		}
	}
	
	if( container.childNodes.length < 5 )
	{
		for( var i=1; i<=5; i++)
		{
			var industryField = document.getElementById('industry_' + i );
			if( industryField.value == id )
			{
				var num = null;
				break;
			}

			if( industryField.value == '' && !num )
			{
				var num = i;
			}
		}

		if( num )
		{
				document.getElementById('industry_' + num ).value = id;
				var node = Builder.node('li', {'style':'width: 410px;', 'id':'industry_li_' + num})
				node.innerHTML = '<div>' + name + ' <a href="javascript: void(0);" onclick="deleteIndustry(' + num + ');"><img src="images/cross.png" width="16" height="16" border="0" /></a></div>';
				container.appendChild( node );
		}
	}
	else
	{
		alert("Maximal 5 Branchen möglich");
	}
	*/
}

function deleteIndustry( num )
{
	confirm('Möchten Sie diesen Eintrag wirklich bearbeiten?');
	document.getElementById('industry_' + num ).value = '';
	var container = document.getElementById('selectedIndustries');
	container.removeChild( document.getElementById('industry_li_' + num ) );

	// cleanup
	for( var i=0; i<container.childNodes.length; i++)
	{
		if( container.childNodes[i].nodeType == 5 )
		{
			container.removeChild( container.childNodes[i] );
		}
	}
}

window.onload = function()
{
	//input.focus();
	//Event.observe(select, "keypress", select.onKeyPress.bindAsEventListener(select));
}