// SysComponents

// var _Listbox = new cHtmlListBox('Listbox')
function cBaseControl()
{
}

// Html ListBox
function cHtmlListBox()
{
}
// Inheritance
cHtmlListBox.prototype = new cBaseControl();

// Constructor
cHtmlListBox.prototype.constructor = function cHtmlListBox()
{
	// Fields
	this.id = '';
	this.table = null;
	this.input = null;
	this.selectedIndex = -1;

	// Constructor
	if ( arguments.length )
	{
		this.id = arguments[0];
		this.input = document.getElementById(this.id);
		this.input.Ctl = this;
		this.table = document.getElementById(this.id + '_lbx');
		this.table.Ctl = this;
	}

	// Events
	this.table.onmousedown = this.DoSelect;
	this.table.onkeydown = this.DoKeyDown;
	var ds = this.table.getAttribute('DoDragStart');
	if (ds!=null)
	{
		this.table.onmousemove = this.DoMouseMove;
		this.table.ondragstart = this.DoDragStart;
		this.table.ondragend = this.DoDragEnd;
	}
	return this;
}

// Methods and Properties
cHtmlListBox.prototype.Add = function(value, text)
{
	var tr = this.table.insertRow(this.Length());
	var td = tr.insertCell();
	var o = new cOption(tr);
	o.Value(value);
	o.Text( text );
}
cHtmlListBox.prototype.Clear = function()
{
	while(!this.IsEmpty())
		this.Remove(0);
}
cHtmlListBox.prototype.IsEmpty = function()
{
	return ( this.Length() == 0 ) ? true : false;
}
cHtmlListBox.prototype.Length = function()
{
	return this.table.rows.length-1;
}
cHtmlListBox.prototype.Remove = function( index )
{
	this.table.deleteRow(index)
}
cHtmlListBox.prototype.SelectedIndex = function(index)
{
	if ( arguments.length )
	{
		var o = this.Options(this.selectedIndex);
		if (o!=null)
		{
			o.Selected(false);
			o.ClassName(null);
		}
		if (index < 0 || index >= this.Length())
			this.selectedIndex = -1;
		else
			this.selectedIndex = index;
		o = this.Options(this.selectedIndex);
		if (o!=null) 
		{ 
			o.Selected(true);
			o.ClassName('Selected');
			o.SetActive();
		}
	}
	return this.selectedIndex;
}
cHtmlListBox.prototype.SelectedItem = function()
{
	if (this.selectedIndex < 0)
		return null;
	return this.Options(this.selectedIndex)
}
cHtmlListBox.prototype.Options = function(index)
{
	if (index < 0 || index > this.Length())
		return null;
	var tr = this.table.rows(index);
	return new cOption(tr);
}
cHtmlListBox.prototype.DoKeyDown = function()
{
	var c = this.Ctl;
	var key = SysGetKey(event);
	if (key == 38) // up
	{
		if (c.selectedIndex > 0)
		{
			c.SelectedIndex(c.selectedIndex - 1);
			SysCancelBubble();
		}
	}
	else if(key == 40)
	{
		if (c.selectedIndex < (c.Length()-1))
		{
			c.SelectedIndex(c.selectedIndex + 1);
			SysCancelBubble();
		}
	}
}
cHtmlListBox.prototype.DoSelect = function()
{
	var c = this.Ctl;
	var td = event.srcElement;
	var tr = td.parentElement;
	c.SelectedIndex(tr.rowIndex);
}
cHtmlListBox.prototype.DoMouseMove = function()
{	
	var c = this.Ctl;
	if (event.button==1 && c.selectedIndex>=0)
	{
		var td = event.srcElement;
		var tr = td.parentElement;
		tr.dragDrop()
	}
}
cHtmlListBox.prototype.DoDragStart = function()
{
	var ds = this.getAttribute('DoDragStart');
	if (ds!=null)
	{
		var f = new Function("o", ds + "(o)");
		var c = this.Ctl;
		var o = c.SelectedItem();
		f(o);
	}
}	
cHtmlListBox.prototype.DoDragEnd = function()
{
	var ds = this.getAttribute('DoDragEnd');
	if (ds!=null)
	{
		var f = new Function("o", ds + "(o)");
		var c = this.Ctl;
		var o = c.SelectedItem();
		f(o);
	}
}	

// cOptions
function cOption()
{
	this.tr = null;
	this.td = null;
	this.selected = false;
	if ( arguments.length )
	{
		this.tr = arguments[0];
		this.td = this.tr.cells[0];
	}
	return this
}
cOption.prototype.Selected = function(value)
{
	if ( arguments.length )
	{
		this.tr.setAttribute('Selected', value)
	}
	return this.tr.getAttribute('Selected');
}
cOption.prototype.Value = function(value)
{
	if ( arguments.length )
	{
		this.tr.setAttribute('value', value)
	}
	return this.tr.getAttribute('value')
}
cOption.prototype.Text = function(text)
{
	if ( arguments.length )
	{
		this.td.innerText = text;
	}
	return this.td.innerText
}
cOption.prototype.ClassName = function(className)
{
	if ( arguments.length )
	{
		this.tr.className = className;
	}
	return this.tr.className
}
cOption.prototype.ScrollIntoView = function()
{
	this.tr.scrollIntoView()
}
cOption.prototype.SetActive = function()
{
	this.tr.setActive();
	this.tr.focus()
}

// Color Field
function cColorField()
{
}
// Inheritance
cColorField.prototype = new cBaseControl();

// Constructor
cColorField.prototype.constructor = function cColorField()
{
	// Fields
	this.id = '';
	this.input;
	this.span;
	// Constructor
	if ( arguments.length )
	{
		this.id = arguments[0];
		this.input = document.getElementById(this.id);
		this.input.Ctl = this;
		this.span = document.getElementById(this.id + '_color');
	}
	return this;
}
cColorField.prototype.Value = function(value)
{
	if ( arguments.length )
	{
		this.input.setAttribute('value', value);
		var n = new Number(value);
		var x = '000000' + n.toString(16);
		x = "#" + x.substr(x.length -6);
		this.span.style.backgroundColor = x;
		this.span.title = x + '(' + value +')';
	}
	return this.input.getAttribute('value')
}

