BwClasses["BwList"] = BwList;

function BwList ()
{
	this.isa = BwWidget;
	this.isa();
	
	this.columns = new Array();
	this.selection = new Array();
	this.primarySelection = null;
	this.naturalOrder = new Array();
	this.sortedOrder = new Array();
	this.sortedOrder._list = this;
	this.sortOrder = 0;
	this.sortedColumnIndex = -1;
	
	this.headerHeight = 0;
	this.header = null;
	this.list = null;
	this.headerTable = null;
	this.listTable = null;
	
	this.initFromDOM = BwList.initFromDOM;
	this.initColumns = BwList.initColumns;
	
	this.drawHeader = BwList.drawHeader;
	this.drawList = BwList.drawList;
	this.drawRows = BwList.drawRows;
	this.drawSelection = BwList.drawSelection;
	this.applyRowStyle = BwList.applyRowStyle;

	this.layout = BwList.layout;
	this.headerLayout = BwList.headerLayout;
	this.columnLayout = BwList.columnLayout;
	this.handleColumnHeaders = BwList.handleColumnHeaders;
	
	this.columnSort = BwList.columnSort;
	this.rowSelect = BwList.rowSelect;
	
	this.selectRowByValue = BwList.selectRowByValue;
	this.deselect = BwList.deselect;
	this.deselectAll = BwList.deselectAll;
	this.select = BwList.select;
	this.sort = BwList.sort;
	this._compare = BwList._compare;
	
	this.keyPressed = BwList.keyPressed;
	this.mouseDoubleClicked = BwList.mouseDoubleClicked;
	this.mouseClicked = BwList.mouseClicked;
	this.mouseMoved = BwList.mouseMoved;
	this.mousePressed = BwList.mousePressed;
	this.mouseReleased = BwList.mouseReleased;
	this.mouseOvered = BwList.mouseOvered;
	this.mouseOuted = BwList.mouseOuted;
	
	this.getSelection = BwList.getSelection;
	this.getCellValue = BwList.getCellValue;
	this.getValue = BwList.getValue;
	this.getLength = BwList.getLength;

	this.remove = BwList.remove;
	this.removeSelection = BwList.removeSelection;
	this.clear = BwList.clear;
	
	this.appendXML = BwList.appendXML;
	this.insertXML = BwList.insertXML;

	this.cellChanged = BwList.cellChanged;
	this.cellFocused = BwList.cellFocused;
	this.scrollToRow = BwList.scrollToRow;
}


BwList.newInstance = function (src, record, rowValue, multi, header, columns, width, height)
{
	var self = document.createElement ("div");
	
	self.BwClass = BwList;
	self.BwClass();
	
	self.className = "BwList";
	
	if (src != null) {
		self.setAttribute ("src", src);
	}

	self.setAttribute ("record", record);
	self.setAttribute ("rowValue", rowValue);
	self.setAttribute ("multi", multi);
	self.setAttribute ("showHeader", header);
	
	if (height && height != null) {
		self.style.height=height+"px";
	}
	
	if (width && width != null) {
		self.style.width=width+"px";
	}

	for (var i = 0; i < columns.length; i++) {
		self.appendChild (columns[i]);
	}
	
	self.initFromDOM();
	
	return self;
};


BwList.initFromDOM = function ()
{
	BwWidget.initFromDOM.call (this);
	
	var s = this.style;
	s.overflow="hidden";
	s.cursor = "default";
	//s.cursor = "hand";
	s.MozUserInput="enabled";
	s.MozUserFocus="normal";
	
	var att = this.getAttribute ("multi");
	this.multi = (att == null || att == 'false') ? false : true;
	
	att = this.getAttribute ("sortable");
	this.sortable = (att == null || att == 'true') ? true : false;
	
	this.record = this.getAttribute ("record");
	this.rowValue = this.getAttribute ("rowValue");
	this.onselect = this.getAttribute ("onselect");
	this.onaction = this.getAttribute ("onaction");
	this.onchange = this.getAttribute ("onchange");

	this.initColumns();
	
	att = this.getAttribute ("showHeader");
	if (att == null || att == 'true') {
		this.drawHeader();
	}

	this.drawList();

	var _this=this;
	this.onclick=function(e){_this.mouseClicked(e);};
	this.ondblclick=function(e){_this.mouseDoubleClicked(e);};
	this.onmousemove=function(e){_this.mouseMoved(e);};
	this.onmousedown=function(e){_this.mousePressed(e);};
	this.onmouseup=function(e){_this.mouseReleased(e);};
	this.onmouseover=function(e){_this.mouseOvered(e);};
	this.onmouseout=function(e){_this.mouseOuted(e);};
	
	if (this.header != null) {
		this.list.onscroll=function(e){_this.header.scrollLeft=_this.list.scrollLeft;};
	}

	var f = function(e){return _this.keyPressed(e);};
	if (navigator.product == "Gecko") {
		this.onkeypress=f;
	} else {
		this.onkeydown=f;
	}
	
	att = this.getAttribute ("src");
	if (att != null) {
		this.appendXML (att, true);
	}

	this.layout();
	BwUtil.forceRedraw();
	this.layout();
	
	return false;
};


BwList.appendXML = function (source, nolayout)
{
	this.insertXML (source, null, nolayout);
};


BwList.insertXML = function (source, beforeRow, nolayout)
{
	if (typeof source == "string")
	{
		var query = new BwQuery();
		query.get (source);
		source = query.getMessage();
	}
	
	this.drawRows (source, beforeRow);
	
	if (this.sortedColumnIndex != -1) {
		this.sort (this.sortedColumnIndex, this.sortOrder);
	}
	
	if (beforeRow) {
		this.scrollToRow (beforeRow);
	}
	
	if (typeof nolayout == "undefined") {
		this.columnLayout();
	}
	
	this.headerLayout();
};


BwList.initColumns = function ()
{
	var curr = this.firstChild;
	while (curr != null)
	{
		this.removeChild (curr);
		
		if (curr.className == "BwColumn")
		{
			var col = new BwColumn (this);
			col.setName (curr.getAttribute ("name"));
			col.setSize (curr.getAttribute ("size"));
			col.setAlign (curr.getAttribute ("align"));
			col.loadRenderCode (curr.getAttribute ("render"));
			
			var att = curr.getAttribute ("value");
			if (att && att != null) {
				col.loadValueCode (att);
			}
			
			this.columns.push (col);
		}
		
		curr = this.firstChild;
	}
};


BwList.drawHeader = function ()
{
	var h = document.createElement ("div");
	h.className = "header";

	var s = h.style;
	s.padding="0px";
	s.margin="0px";
	s.border="0px";
	s.position="absolute";
	s.overflow="hidden";
	
	this.appendChild (h);
	this.header = h;
	
	var ht = document.createElement ("table");
	ht.style.tableLayout="fixed";
	ht.style.borderCollapse="collapse";
	
	this.headerTable = ht;

	var tb = document.createElement ("tbody");
	ht.appendChild (tb);
	
	h.appendChild (ht);
	
	var tr = document.createElement ("tr");
	tb.appendChild (tr);
	
	var l = this.columns.length;
	for (var i = 0; i < l; i++)
	{
		var col = this.columns[i];
		var td = document.createElement ("td");
		s = td.style;
		if (col.size != null) {
			s.width = col.size;
		}
		s.padding="0px";
		s.overflow="hidden";
		
		var d = document.createElement ("div");
		d.className="column";
		d.align=col.align;
		d.style.whiteSpace="nowrap";
		td.appendChild (d);
		
		var a = BwLabel.newInstance (col.name);
		a.style.whiteSpace="nowrap";
		a.style.margin="3px";
		a.attach (d);
		
		a = BwStockIcon.newInstance ("asc");
		a.style.visibility="hidden";
		a.align="center";
		a.attach (d);
		
		tr.appendChild (td);
	}
};


BwList.drawList = function ()
{
	var l = document.createElement ("div");
	l.style.overflow = "auto";
	l.className="list";
	
	var lt = document.createElement ("table");
	
	lt.style.tableLayout="fixed";
	lt.style.borderCollapse="collapse";
	
	var cg = document.createElement ("colgroup");
	var len = this.columns.length;
	for (var i = 0; i < len; i++)
	{
		var c = document.createElement("col");
		cg.appendChild (c);
		this.columns[i].colPtr = c;
	}
	
	var tb = document.createElement ("tbody");
	lt.appendChild (cg);
	lt.appendChild (tb);
	l.appendChild (lt);
	this.appendChild (l);

	this.listTable = lt;
	this.list = l;
};


BwList.drawRows = function (source, beforeRow)
{
	var b = this.listTable.tBodies[0];
	
	var append = (beforeRow == null);
	var insertRow = (append) ? null : beforeRow;
	var rowIndex = (append) ? b.rows.length : beforeRow.rowIndex;
	var rows = new Array();
	
	var cols = this.columns;
	var clen = cols.length;
	
	var rec = source.lookup (this.record);
	if (rec == null) {
		return;
	}

	var p = rec.parentNode;
	var c = BwXml.childrenNamed (p, rec.nodeName);
	var l = c.length;
	
	var m = document.createElement ("tr");
	m.vAlign="middle";
	m.style.verticalAlign="middle";
	m.style.cursor="pointer";
	m.style.cursor="hand";
	
	for (var i = 0; i < clen; i++)
	{
		var td = document.createElement ("td");
		td.style.overflow="hidden";
		td.align = cols[i].align;
		m.appendChild (td);
	}
	
	this.applyRowStyle (m, rowIndex);
	
	for (var r = 0; r < l; r++) 
	{
		var tr = m.cloneNode (true);
		rows.push (tr);
		
		source.changeRoot (c[r]);
		
		if (this.rowValue != null) {
			tr.value = source.getCasted (this.rowValue);
		}
		
		var tds = tr.childNodes;
		for (var i = 0; i < clen; i++)
		{
			var col = cols[i];
			var td = tds[i];
			
			td.appendChild (col.cellContent (source));
			
			if (col.value != null) {

				td.value = source.getCasted (col.value);
			}
			
			td.onselectstart=function(){return typeof window.event.srcElement.editable!='undefined';};
		}
		
		source.changeRoot (null);
		
		if ((r%2) != 0) {
			this.applyRowStyle (tr, rowIndex, true);
		}
		
		if (append) {
			b.appendChild (tr);
		} else {
			b.insertBefore (tr, insertRow);
		}

		insertRow = tr.nextSibling;
		rowIndex++;
	}
	
	this.sortedOrder = this.sortedOrder.concat (rows);
	
	if (append) {
		this.naturalOrder = this.naturalOrder.concat (rows);
	}
	else
	{
		var i = this.naturalOrder.lookup (beforeRow);
		var a1 = this.naturalOrder.slice (0, i);
		var a2 = this.naturalOrder.slice (i);
		this.naturalOrder = a1.concat (rows, a2);
		
		var c = beforeRow;
		while (c != null)
		{
			this.applyRowStyle (c, c.rowIndex, true);
			c = c.nextSibling;
		}
		
		this.drawSelection();
	}
};


BwList.drawSelection = function ()
{
	var s = this.selection;
	for (var i = 0; i < s.length; i++) {
		s[i].className="rowSelected";
	}
};


BwList.applyRowStyle = function (r, i, excludingCells)
{
	var s = ((i%2) == 0) ? "rowEven" : "rowOdd";
	var c = r.childNodes;
	var l = c.length;
	
	r.className = s;
	
	if (!excludingCells)
	{
		for (var j = 0; j < l; j++)
		{
			if (this.sortOrder != 0 && j == this.sortedColumnIndex) {
				c[j].className="sorted";
			} else {
				c[j].className="";
			}
		}
	}
};


BwList.layout = function ()
{
	if (this.header != null) {
		this.headerHeight = this.header.offsetHeight;
		this.list.style.paddingTop = this.headerHeight + 'px';
	}
	
	var h = this.clientHeight;
	h = (document.all) ? h : (h - this.headerHeight);
	if (h >= 0) {
		this.list.style.height = h + 'px';
	}
	
	var w = this.offsetWidth;
	if (navigator.product=="Gecko") {
		w--;
	}
	this.list.style.width = w + 'px';
	w = this.list.clientWidth;
	this.listTable.style.width = w + 'px';
	if (this.header != null)
	{
		this.headerTable.style.width = w + "px";
		this.headerLayout();
		this.columnLayout();
	}
};


BwList.headerLayout = function ()
{
	if (this.header == null) return;
	
	var w = this.list.clientWidth;
	if (w > 0) {
		this.header.style.width = (w-1) + 'px';
	}
};


BwList.columnLayout = function ()
{
	if (this.header == null) return;
	
	var rows = this.listTable.tBodies[0].rows;
	if (rows.length == 0 || !rows[0].cells) return;

	var headCells = this.headerTable.tBodies[0].rows[0].cells;
	var l = headCells.length;
	for (var i = 0; i < l; i++)
	{
		var h = headCells[i];
		var w = h.offsetWidth;
		var sw = w + 'px';
		h.style.width = sw;
		/*
		h.firstChild.style.width = sw;
		*/
		this.columns[i].colPtr.style.width = w;
	}
};


BwList.keyPressed = function (event)
{
	if (!event) event = window.event;

	var key = (event)?event.keyCode:window.event.keyCode;
	var rows = this.listTable.tBodies[0].rows;
	var row = (this.selection.length != 0) ? this.selection[this.selection.length-1] : null;
	var rowsPerPage = Math.floor ((this.list.clientHeight - this.headerHeight) / rows[0].offsetHeight);
	var last = rows.length - 1;

	switch (key)
	{
		case 38:
			row = (row != null) ? row.previousSibling : rows[0];
			break;
		case 40:
			row = (row != null) ? row.nextSibling : rows[0];
			break;
		case 33:
			var i = 0;
			if (row != null) {
				i = (row.rowIndex - rowsPerPage);
				if (i < 0) i = 0;
			}
			row = rows[i];
			break;
		case 34:
			var i = 0;
			if (row != null) {
				i = (row.rowIndex + rowsPerPage);
				if (i > last) i = last;
			}
			row = rows[i];
			break;
		case 36:
			row = rows[0];
			break;
		case 35:
			row = rows[last];
			break;
		case 13:
			BwUtil.call (this, this.onaction);
			/*this.scrollToRow (row);*/
			return;
		default:
			return true;
	}
	
	if (row)
	{
		this.rowSelect (row, event);
		this.scrollToRow (row);
	}

	return false;
};


BwList.mouseOvered =  function (event)
{
	if (!event) event = window.event;
	
	if (this.targetColumn != null) {
		return false;
	}

	//this.style.cursor ='hand';
	//this.style.cursor ='pointer';
	//this.className="rowSelected";


	var target = (event.target)?event.target:event.srcElement;
	
	if (BwUtil.findParentElement (target, "DIV", "header") != null)
	{
		/*	
		if (target.nodeName != "TD") {
			target = BwUtil.findParentElement (target, "TD");
		}
	
		if (target != null && this.sortable) {
			this.columnSort (target.cellIndex);
		}
		*/
	}
	else
	{
		var row = BwUtil.findParentElement (target, "TR");
		if (row != null && BwUtil.findParentElement (row, "DIV", "list") != null) {
			var r = this.selection.lookup (row);
			this.select (row);
			//this.rowSelect (row, event);
		}
	}

	return false;
};

BwList.mouseOuted =  function (event)
{
	if (!event) event = window.event;
	
	if (this.targetColumn != null) {
		return false;
	}

	var target = (event.target)?event.target:event.srcElement;
	
	if (BwUtil.findParentElement (target, "DIV", "header") != null)
	{
	/*	
		if (target.nodeName != "TD") {
			target = BwUtil.findParentElement (target, "TD");
		}
	
		if (target != null && this.sortable) {
			this.columnSort (target.cellIndex);
		}
	*/
	}
	else
	{
		var row = BwUtil.findParentElement (target, "TR");
		if (row != null && BwUtil.findParentElement (row, "DIV", "list") != null) {
			var r = this.selection.lookup (row);
			this.deselect (row);
			//this.rowSelect (row, event);
		}
	}

	return false;
};




BwList.mouseClicked = function (event)
{
	if (!event) event = window.event;
	
	if (this.targetColumn != null) {
		return false;
	}
	
	var target = (event.target)?event.target:event.srcElement;
	
	if (BwUtil.findParentElement (target, "DIV", "header") != null)
	{
		if (target.nodeName != "TD") {
			target = BwUtil.findParentElement (target, "TD");
		}
		
		if (target != null && this.sortable) {
			this.columnSort (target.cellIndex);
		}
	}
	else
	{
		var row = BwUtil.findParentElement (target, "TR");
		if (row != null && BwUtil.findParentElement (row, "DIV", "list") != null) {
			this.rowSelect (row, event);
			this.mouseOut;
		}
	}

	return false;
};

BwList.mouseDoubleClicked = function (event)
{
	if (!event) event = window.event;
	
	if (this.onaction == null) {
		return true;
	}
	
	if (this.targetColumn != null) {
		return true;
	}

	var target = (event.target)?event.target:event.srcElement;
	var row = BwUtil.findParentElement (target, "TR");
	if (row != null && BwUtil.findParentElement (row, "DIV", "list") != null) {
		return BwUtil.call (this, this.onaction);
	}

	return true;
};

BwList.mousePressed = function (event)
{
	if (!event) event = window.event;
	
	if (this.targetColumn == null) {
		return;
	}
	
	this.lastX = (event.pageX)?event.pageX:event.x;
	this.lastX += this.list.scrollLeft;
	this.resizing = true;
	
	if (this.setCapture) {
		this.setCapture();
	}
};

BwList.mouseReleased = function (event)
{
	if (!event) event = window.event;
	
	this.resizing = false;
	
	if (this.releaseCapture) {
		this.releaseCapture();
	}
	return true;
};

BwList.mouseMoved = function (event)
{
	if (!event) event = window.event;
	
	var target = (event.target)?event.target:event.srcElement;
	var x = (event.pageX)?event.pageX:event.x;
	x += this.list.scrollLeft;
	
	if (this.resizing)
	{
		var o = x - this.lastX;
		var w = this.targetColumnWidth + o;
		if (w <= 16) {
			return;
		}
		var w1 = w + "px";
		var w2 = (this.targetTableWidth + o) + "px";
		this.targetColumn.style.width = w1;
		/*
		this.targetColumn.firstChild.style.width = w1;
		*/
		this.columns[this.targetColumn.cellIndex].colPtr.style.width = w1;
		this.headerTable.style.width = w2;
		this.listTable.style.width = w2;
	}
	else if (BwUtil.findParentElement (target, "DIV", "header") != null)
	{
		if (target.nodeName != "TD") {
			target = BwUtil.findParentElement (target, "TD");
		}
		if (target != null) {
			this.handleColumnHeaders (target, x);
		}
	}
	else
	{
		this.style.cursor = "default";
		this.targetColumn = null;
	}
};


BwList.handleColumnHeaders = function (target, x)
{
	var prev = target.previousSibling;
	var next = target.nextSibling;
	var left = BwUtil.getElementLeftPosition (target);
	var right = left + target.offsetWidth;
	
	this.resizing = false;
	
	if (prev && x <= (left + 7)) {
		this.style.cursor = "e-resize";
		this.targetColumn = prev;
		this.targetColumnWidth = prev.offsetWidth;
		this.targetTableWidth = this.headerTable.offsetWidth;
	}
	else if (x >= (right - 7)) {
		this.style.cursor = "e-resize";
		this.targetColumn = target;
		this.targetColumnWidth = target.offsetWidth;
		this.targetTableWidth = this.headerTable.offsetWidth;
	}
	else {
		this.style.cursor = "default";
		this.targetColumn = null;
	}
};


BwList.columnSort = function (col)
{
	var pc = this.sortedColumnIndex;
	var order = this.sortOrder;
	if (col != pc) {
		order = 0;
	}
	
	order = (++order % 3);
	
	this.sort (col, order);
	
	var s = this.selection;
	var l = s.length;
	if (l > 0) {
		this.scrollToRow (s[l-1]);
	}
};


BwList.sort = function (col, order, excludingCell)
{
	var h = this.headerTable.tBodies[0].rows[0].childNodes;
	var b = this.listTable.tBodies[0];
	var l = b.rows.length;
	var pc = this.sortedColumnIndex;
	var ic, s;
	
	if (col != pc && pc != -1)
	{
		ic = h[pc].firstChild.firstChild.nextSibling;
		ic.style.visibility="hidden";
		h[pc].firstChild.className = "column";
	}
	
	this.sortOrder = order;
	this.sortedColumnIndex = col;
	
	ic = h[col].firstChild.firstChild.nextSibling;
	if (this.sortOrder != 0) 
	{
		var asc = (this.sortOrder == 1);
		h[col].firstChild.className = "column sorted";
		ic.style.visibility="visible";
		ic.setSource (asc ? "asc" : "desc");
		
		var _this = this;
		this.sortedOrder.sort (function (a,b){return _this._compare(a,b);});
		
		s = this.sortedOrder;
	}
	else
	{
		h[col].firstChild.className = "column";
		ic.style.visibility="hidden";
		s = this.naturalOrder;
	}
	
	this.listTable.removeChild (b);	
	for (var i = 0; i < l; i++)
	{
		var r = s[i];
		b.removeChild (r);
		this.applyRowStyle (r, i, excludingCell);
		b.appendChild (r);
	}
	this.listTable.appendChild (b);	
	
	this.drawSelection ();
};


BwList._compare = function (a, b)
{
	var i = this.sortedColumnIndex;
	a = this.getCellValue (a, i);
	b = this.getCellValue (b, i);
	if (a.toUpperCase && b.toUpperCase)
	{
		a = a.toUpperCase();
		b = b.toUpperCase();
	}
	
	var r;
	if (a > b) r = 1;
	else if (a < b) r = -1;
	else r = 0;

	if (this.sortOrder == 2) r = -r;
	
	return r;
};


BwList.deselectAll = function ()
{
	var l = this.selection.length;
	for (var i = 0; i < l; i++) {
		this.deselect (this.selection[0]);
	}
};


BwList.deselect = function (row)
{
	if (!row) return;

	var c = row.childNodes;
	var l = c.length;
	
	var i = this.selection.lookup (row);
	if (i == -1) return;
	
	this.selection.splice (i, 1);
	
	var r = row.rowIndex;
	this.applyRowStyle (row, r);
};


BwList.selectRowByValue = function (value)
{
	var rows = this.listTable.tBodies[0].rows;
	var l = rows.length;
	for (var i = 0; i < l; i++)
	{
		var r = rows[i];
		if (r.value == value) {
			this.rowSelect (r);
			this.scrollToRow (r);
			return;
		}
	}
};


BwList.select = function (row)
{
	var c = row.childNodes;
	var l = c.length;
	
	var i = this.selection.lookup (row);
	if (i != -1) return;
	
	this.selection.push (row);

	this.drawSelection();
};


BwList.rowSelect = function (row, e)
{
	var r = this.selection.lookup (row);
	
	if (e && e.ctrlKey)
	{
		if (r == -1) {
			if (!this.multi && this.selection.length != 0) {
				this.deselect (this.selection[0]);
			}
			
			this.select (row);
			this.primarySelection = row;
		} else {
			this.deselect (row);
			if (this.selection.length == 0) {
				this.primarySelection = null;
			}
		}
	}
	else if (this.multi && e && e.shiftKey && this.primarySelection != null)
	{
		var last = this.primarySelection;
		this.deselectAll();
		
		var i1 = last.rowIndex;
		var i2 = row.rowIndex;
		if (i1 < i2) {
			for (var i = i1; i <= i2; i++) {
				this.select (this.listTable.tBodies[0].rows[i]);
			}
		} else {
			for (var i = i1; i >= i2; i--) {
				this.select (this.listTable.tBodies[0].rows[i]);
			}
		}
	}
	else
	{
		this.deselectAll();
		this.select (row);
		this.primarySelection = row;
	}
	
	BwUtil.call (this, this.onselect);
};


BwList.getSelection = function ()
{
	return this.selection;
};


BwList.getCellValue = function (row, colIndex)
{
	var c = row.childNodes[colIndex];
	if (typeof c.value != "undefined") {
		return c.value;
	}
	c = c.firstChild;
	return c.getValue();
};


BwList.removeSelection = function ()
{
	var s = this.selection;
	while (s.length > 0) {
		this.remove (s[0], true);
	}
	
	var r = this.listTable.tBodies[0].rows;
	for (var i = 0; i < r.length; i++) {
		this.applyRowStyle (r[i], i, true);
	}

	this.headerLayout();
};


BwList.remove = function (row, freeze)
{
	var i;
	
	row.parentNode.removeChild (row);
	
	if (!freeze)
	{
		var next = row.nextSibling;
		i = row.rowIndex;
		while (next != null)
		{
			this.applyRowStyle (next, i, true);
			i++;
			next = next.nextSibling;
		}
	}

	i = this.selection.lookup (row);
	this.selection.splice (i, 1);

	i = this.naturalOrder.lookup (row);
	this.naturalOrder.splice (i, 1);
	
	i = this.sortedOrder.lookup (row);
	this.sortedOrder.splice (i, 1);
	
	if (!freeze) {
		this.headerLayout();
	}
};


BwList.clear = function ()
{
	var body = this.listTable.tBodies[0];
	var curr = body.rows[0];
	
	while (curr != null)
	{
		var next = curr.nextSibling;
		body.removeChild (curr);
		curr = next;
	}
	
	this.selection = new Array();
	this.naturalOrder = new Array();
	this.sortedOrder = new Array();

	this.headerLayout();
};


BwList.getValue = function ()
{
	var s = this.selection;
	var l = s.length;
	if (l == 0) {
		return null;
	}

	return s[l-1].value;
};


BwList.getLength = function ()
{
	return this.listTable.tBodies[0].rows.length;
};


BwList.cellFocused = function (widget)
{
	var td = widget.parentNode;
	var row = td.parentNode;
	this.deselectAll();
	this.rowSelect (row);
};


BwList.cellChanged = function (widget)
{
	var td = widget.parentNode;
	var i = td.cellIndex;
	var row = td.parentNode;
	
	if (this.sortedColumnIndex == i) {
		this.sort (i, this.sortOrder);
		this.scrollToRow (row);
	}
	
	if (this.onchange && this.onchange != null)
	{
		this.changedRow = row;
		this.changedCellIndex = i;
		BwUtil.call (this, this.onchange);
	}
	
};


BwList.scrollToRow = function (row)
{
	if (row.offsetTop < this.list.scrollTop) {
		this.list.scrollTop = row.offsetTop;
	}
	else if ((row.offsetTop + row.offsetHeight) > (this.list.scrollTop + this.list.clientHeight - this.headerHeight)) {
		this.list.scrollTop = row.offsetTop - this.list.clientHeight + this.headerHeight + row.offsetHeight;
	}
};

