/*
ver. 2009-04-02
remarks: uses single state, no built in reset function
*/

function Kontakty_TreeNode( item, select, children)
{
	this.Parent = null;
	this.Item = item;
	this.Select = select;
	this.OptionsSetBy = null;  
	if ( "undefined" == typeof(Kontakty_TreeNode.State))
	{
		Kontakty_TreeNode.State = new Object();
	}

	this.IsSelected = function()
	{
		var result;
		if ( null == this.Parent)
		{
			result = true;
		}
		else
		{
			if ( this.Parent.IsSelected())
			{
				result = this.Item.IsThisYou( Kontakty_TreeNode.State[this.Parent.Select.getAttribute( "name")]);
			}
			else
			{
				result = false;
			}
//if ( true == result) alert( this.Item.Value + ":" + this.Item.Text + " - " + this.Parent.Select.getAttribute( "name") + " - " + Kontakty_TreeNode.State[this.Parent.Select.getAttribute( "name")] + " - " + result);

		}
		return result;
	}
	
	this.AddChild = function( node) 
	{
		node.Parent = this;
		this.Children[this.Children.length] = node; 
	}
	
	this.Children = new Array();
	if ( children)
	{
		for ( var i = 0; i < children.length; ++i)
		{
			this.AddChild( children[i]);
		}
	}

	this.CreateOption = function()
	{
		return this.Item.CreateOption( this.IsSelected());
	}
	this.UnsetOptions = function()
	{
		if ( this.Select)
		{
			var is_selected = this.IsSelected();
			if ( ( false == is_selected) && ( this.OptionsSetBy == this))
			{
				this.OptionsSetBy = null;
				this.Select.options.length = 0;
				this.Select.disabled = true;
			}
			for ( var i = 0; i < this.Children.length; ++i)
			{
				this.Children[i].UnsetOptions();
			}
		}
	}
	this.SetOptions = function()
	{
		if ( this.Select)
		{
			var is_selected = this.IsSelected();
			if ( ( true == is_selected) && ( this.OptionsSetBy != this))
			{
				if ( this.Children.length > 0)
				{
					this.Select.disabled = false;
					this.Select.options[this.Select.options.length] = new Option( "", "");
					for ( var i = 0; i < this.Children.length; ++i)
					{
						this.Select.options[this.Select.options.length] = this.Children[i].CreateOption();
					}
				}
				else
				{
					this.Select.disabled = true;
				}
				this.OptionsSetBy = this;
			}
			for ( var i = 0; i < this.Children.length; ++i)
			{
				this.Children[i].SetOptions();
			}
		}
	}
	this.OnChange = function( select)
	{
		Kontakty_TreeNode.State[select.getAttribute( "name")] = select.options[select.selectedIndex].value;	
//alert( select.getAttribute( "name") + ": " + select.options[select.selectedIndex].value);
		this.UnsetOptions();
		this.SetOptions();
	}
}

function Kontakty_Item( text, value)
{
	this.Value = value;
	this.Text = text;

	this.CreateOption = function( selected)
	{
		return new Option( this.Text, this.Value, selected, selected);
	}

	this.IsThisYou = function( value)
	{
		return value == this.Value;
	}
}

function Kontakty_SetKraj(id)
{
	var ele = document.getElementById('kontaktyKraj_kraj');
	for ( var i = 0; i < ele.options.length; i++)
	{
		if ( id == ele.options[i].value)
		{
			ele.selectedIndex = i;
		}
	}
	tree.OnChange( ele);
}

function Kontakty_SetOkres(id)
{
	var ele = document.getElementById('kontaktyKraj_okresy');
	for ( var i = 0; i < ele.options.length; i++)
	{
		if ( id == ele.options[i].value)
		{
			ele.selectedIndex = i;
		}
	}
	tree.OnChange( ele);
}

