function XListBox_selctedItem(oRow)
{
	var el = window.document.createElement("INPUT");
	el.name = oList.name;
	el.type = "hidden";
	el.value = oRow.value;
}

function XListBox_unselectItems(oList)
{
	var oRow;
	//this.print(oList.selectedRows.length);
	for(var iRow = 0; iRow < oList.selectedRows.length; iRow++) {
		oRow = oList.selectedRows[iRow];
		//this.print("unselectRow:"+oRow.cells[1]);
		oRow.className = oList.classItemNormal;
		oRow.bSelected = false;
	}
	oList.selectedRows.length = 0;
}

function XListBox_selectItem(oRow)
{
	oList = this;
	if(oList.multiple == "no") {
		if(oList.itemsTable.SelectedRow != null && oList.itemsTable.SelectedRow != oRow) {
			oList.itemsTable.SelectedRow.className = oList.classItemNormal;
			oList.itemsTable.SelectedRow.bSelected = false;
		}
	} else {
		//this.print("ctrlKey="+window.event.ctrlKey);
		if(window.event.ctrlKey == false) { 
			XListBox_unselectItems(oList);
		}
		
	}
	
	oRow.bSelected = !oRow.bSelected;
	//this.print("selcted="+oRow.bSelected);
	oList.itemsTable.SelectedRow = oRow;
//	oList.itemsTable.SelectedRow.scrollIntoView("true");
	if(oRow.bSelected) {
		if(oList.multiple == "yes")
			oList.selectedRows[oList.selectedRows.length] = oRow;
		oRow.className = oList.classItemSelected;
//		oRow.style.frame=1;
		//this.print(oList.itemsTable.onSelectItem);
		//alert(oList.onchange);
		if(oList.itemsTable.onSelectItem) oList.itemsTable.onSelectItem();
		
		if(oList.onchange) {
			
			if(oList.hiddenInput == null) {
				oList.hiddenInput = document.createElement("<input type=hidden name="+oList.id+">");
				oList.appenChild(oList.hiddenInput);
			}
			oList.hiddenInput.value = oRow.cellData[oList.boundValue-1];
			oList.onchange();
		}
	} else {
		if(oList.multiple == "yes") {
			for(var iRow = 0; iRow < oList.selectedRows.length; iRow++) {
				if(oList.selectedRows[iRow] == oRow) {
					for(; iRow < oList.selectedRows.length-1; iRow++)
						oList.selectedRows[iRow] = oList.selectedRows[iRow + 1];
					oList.selectedRows.length--;
					break;
				}
			}
		}
		oRow.className = oList.classItemHover;
		if(oList.itemsTable.onUnSelectItem) oList.itemsTable.onUnSelectItem();
	}
}
function onMouseOver(oList, oTable)
{
	var oRow = oTable.document.parentWindow.event.srcElement.parentElement;
	//this.print(oList.classItemHover);
	oRow.className = oList.classItemHover;
};

function onMouseDown(oList, oTable)
{
	var oRow = oTable.document.parentWindow.event.srcElement.parentElement;
	//return;
	//this.print("Mousedown ");
//	window.event.Element.focus();
	if(oList.multiple == "no") {
		if(oList.itemsTable.SelectedRow != null && oList.itemsTable.SelectedRow != oRow) {
			oList.itemsTable.SelectedRow.className = oList.classItemNormal;
			oList.itemsTable.SelectedRow.bSelected = false;
		}
	} else {
		//this.print("ctrlKey="+window.event.ctrlKey);
		if(window.event.ctrlKey == false) { 
			XListBox_unselectItems(oList);
		}
		
	}
	
	oRow.bSelected = !oRow.bSelected;
	//this.print("selcted="+oRow.bSelected);
	oList.itemsTable.SelectedRow = oRow;
	if(oRow.bSelected) {
		if(oList.multiple == "yes")
			oList.selectedRows[oList.selectedRows.length] = oRow;
		oRow.className = oList.classItemSelected;
//		oRow.style.frame=1;
		//this.print(oList.itemsTable.onSelectItem);
		if(oList.itemsTable.onSelectItem) oList.itemsTable.onSelectItem();
		if(oList.onchange) {
			
			//if(oList.hiddenInput == null) {
				oList.hiddenInput = document.createElement("<input type=hidden name="+oList.id+">");
				oList.appendChild(oList.hiddenInput);
			//}
			oList.hiddenInput.value = oRow.cellData[oList.boundColumn-1];
			oList.onchange();
		}
		
	} else {
		if(oList.multiple == "yes") {
			for(var iRow = 0; iRow < oList.selectedRows.length; iRow++) {
				if(oList.selectedRows[iRow] == oRow) {
					for(; iRow < oList.selectedRows.length-1; iRow++)
						oList.selectedRows[iRow] = oList.selectedRows[iRow + 1];
					oList.selectedRows.length--;
					break;
				}
			}
		}
		oRow.className = oList.classItemHover;
		if(oList.itemsTable.onUnSelectItem) oList.itemsTable.onUnSelectItem();
	}
//	return false;
};

function onMouseOut(oList, oTable)
{
	var oRow = oTable.document.parentWindow.event.srcElement.parentElement;
	if(!oRow.bSelected) 
		oRow.className = oList.classItemNormal;
	else
		oRow.className = oList.classItemSelected;
		
};

function XListBox_Init()
{
	//this.print("XListBox_Init()");
}
function XListBox_print(msg)
{
	if(this.onPrint)
		this.onPrint(msg);
}
function XListBox_onLoadingComplete()
{
	alert("loading complete "+this.dataArr.length+",fields="+this.fields.length+","+this.errorStr);
	if(!this.errorStr) {
		for(var i = 0; i < this.dataArr.length; i+=this.fields.length) {
			s = "";
			for(var f = 0; f < this.fields.length; f++) {
				if(s.length) s += ",";
				s += "this.dataArr["+(i+f)+"]";
			}
			eval("this.addRow("+s+");");
		}
		if(this.dataArr.length/this.fields.length >= this.dataLoaderMaxAtOnce) {
			this.print("loading next:"+this.dataArr.length);
			//void(window.document.getElementsByTagName("head")[0].removeChild(this.dataLoaderScript));
			this.dataLoaderScript.removeNode();
			this.dataLoaderScript = document.createElement("SCRIPT");
			this.cur += this.dataLoaderMaxAtOnce;
			this.onLoadingComplete = XListBox_onLoadingComplete;
			var el = this;
			//this.print("/utils/dbaccess.php?sql="+this.dataSource+" LIMIT "+this.cur+","+this.dataLoaderMaxAtOnce+"&objID="+this.createInWindow+el.id+"&dbHandle="+this.dbHandle);
			el.dataLoaderScript.src = "/utils/dbaccess.php?sql="+el.dataSource+" LIMIT "+el.cur+","+el.dataLoaderMaxAtOnce+"&objID="+el.createInWindow+el.id+"&dbHandle="+el.dbHandle;
			
//			this.o.src = "http://192.168.0.2/utils/dbaccess.php?sql="+this.dataSource+" LIMIT "+this.cur+",100&objID="+this.id+"& objName=resultData";
			
			void(window.document.getElementsByTagName("head")[0].appendChild(this.dataLoaderScript));
			return;
		} else {
			
			this.loadingComplete = true;
			this.deleteRow(0);
		}
	} else
		this.addRow(this.errorStr);
	this.xevent.Unlock();

}
function XListBox_loadData()
{
	this.print("XListBox_loadData()");
	this.db = new XDB;
	this.db.onPrint = this.onPrint;
	this.db.dbHandle = this.dbHandle;
	this.cur = 0;
	//this.db.onQueryDone = XListBox_onLoadingComplete;
	this.dataLoaderMaxAtOnce = 100;
	if(this.db.query(this.dataSource+" LIMIT 0,"+this.dataLoaderMaxAtOnce) == false)
		alert(this.db.errorStr);
	this.print("loading complete "+this.db.dataArr.length+",fields="+this.db.fields.length+","+this.db.errorStr);
	while(!this.db.errorStr) {
		for(var i = 0; i < this.db.dataArr.length; i+=this.db.fields.length) {
			s = "";
			for(var f = 0; f < this.db.fields.length; f++) {
				if(s.length) s += ",";
				s += "this.db.dataArr["+(i+f)+"]";
			}
			eval("this.addRow("+s+");");
		}
		if(this.db.dataArr.length/this.db.fields.length >= this.dataLoaderMaxAtOnce) {
			this.print("loading next:"+this.db.dataArr.length);
			//void(window.document.getElementsByTagName("head")[0].removeChild(this.dataLoaderScript));
			//this.dataLoaderScript.removeNode();
			//this.dataLoaderScript = document.createElement("SCRIPT");
			this.cur += this.dataLoaderMaxAtOnce;
			if(this.db.query(this.dataSource+" LIMIT "+this.cur+","+this.dataLoaderMaxAtOnce) == false)
				alert(this.db.errorStr);			
		} else {
			
			this.loadingComplete = true;
			this.deleteRow(0);
			break;
		}
	} 
	if(this.db.errorStr)
		this.addRow(this.db.errorStr);
	this.print("XListBox_loadData done");		
	//this.onLoadingComplete = XListBox_onLoadingComplete;
	//this.onLoadingComplete();
}
function _XListBox_loadData()
{
	if(this.dataArr)
		return;
	var el = this;
	this.xevent = new XEvent();
	
	this.addRow("Loading...");
	this.dataLoaderMaxAtOnce = 500;
	this.dataLoaderScript = document.createElement("SCRIPT");
	this.cur = 0;
	//this.onLoadingComplete = XListBox_onLoadingComplete;
	//eval("window.document.all."+el.id+".addRow('hallo');");
	this.dataLoaderScript.src = "/utils/dbaccess.php?sql="+el.dataSource+" LIMIT 0,"+this.dataLoaderMaxAtOnce+"&objID="+this.createInWindow+el.id+"&dbHandle="+this.dbHandle;
	void(window.document.getElementsByTagName("head")[0].appendChild(this.dataLoaderScript));
	this.xevent.Lock(20000);
	this.XListBox_onLoadingComplete();
	
}


function XListBox(paramStr)
{
		//this.print("ob="+(paramStr));
	if(typeof(paramStr) == "object") {
		
		var el = paramStr;
		var oTable = el.itemsTable;
		
	} else {
		
		params = parseStr(paramStr);	
		params["useFrame"] = 0;
		createInWindow = getValueOrDef(params["createInWindow"], "window");
		if(createInWindow != "")
			createInWindow += ".";
		eval("var el = "+createInWindow+"document.createElement(\"DIV\")");
		if(params["useFrame"] == 1) {
			el.xevent = new XEvent;
			window.top.thisElement = el;
			/*
			el.frame = document.createElement("<iframe marginheight=0 frameborder=0 id=iframe"+
					el.uniqueID+
				" scrolling=no style='position:absolute;top:0;width:100;height:100;visibility:visible' src='/utils/XListBox.htm'></iframe>");*/
			//el.appendChild(el.frame);
			window.top.document.body.insertAdjacentHTML("afterBegin", "<iframe marginheight=0 frameborder=0 id=iframe"+
					el.uniqueID+
				" scrolling=yes style='position:absolute;top:300;width:100;visibility:visible' src='/utils/XListBox.htm'></iframe>");
			//void(window.top.document.getElementsByTagName("head")[0].appendChild(el));
			//el.id = iframe
			
			//alert(el);
			//alert(window.top.newElement);
			el.xevent.Lock(4000);
			
			eval("el.frame = window.top.document.all.iframe"+el.uniqueID+";");
			el.frame.contentWindow.document.body.insertAdjacentElement("afterBegin", el);
			//el.appendChild(el.frame);
			//eval("window.newElement.frame = "+createInWindow+".document.all.iframe"+el.uniqueID+";");
			el = el.frame;//window.top.newElement;
			//eval("var oTable = "+createInWindow+".document.createElement(\"TABLE\")");
			oTable = window.top.newElement.itemsTable;
		} else		
			eval("var oTable = "+createInWindow+"document.createElement(\"TABLE\")");
		el.createInWindow = createInWindow;
	}

// function binding
	el.addRow = XListBox_addRow;
	el.deleteRow = XListBox_deleteRow;
	el.getRow = XListBox_getRow;
	el.getItemsTable = function anonymus() { return el.itemsTable;}
	el.getFrameDIV = function anonymus() { return el; }
	el.selectItem = XListBox_selectItem;
	el.loadData = XListBox_loadData;
	el.print = XListBox_print;
	oTable.onmouseover = function anonymus() { onMouseOver(el, oTable); }
	oTable.onmousedown = function anonymus() { onMouseDown(el, oTable); }
	oTable.onmouseout = function anonymus() { onMouseOut(el, oTable); }
	if(typeof(paramStr) == "object")
		return el;
	el.dbHandle = getValueOrDef(params["dbHandle"]);
	el.id = getValueOrDef(params["name"], "DIV_"+el.uniqueID);
	el.boundColumn = getValueOrDef(params["boundColumn"], "1");
	//el.name = getValueOrDef(params["name"], "DIV_"+el.uniqueID);
	//el.id = getValueOrDef(params["name"], "DIV_"+el.uniqueID);
//	el.id = getValueOrDef(params["id"], "DIV_"+el.uniqueID);
	el.multiple = getValueOrDef(params["multiple"], "no");
	el.selectedRows = new Array();
	
//	var oTable = document.createElement("TABLE");

	oTable.cellSpacing = 0;
	oTable.width = "100%";
	eval("el.colWidthsArray = new Array(" + getValueOrDef(params["colWidths"],"") + ");");
	
	//this.print(el.colWidthsArray.length);
	el.itemsTable = oTable;
	el.boundColIdx = new Array();
	if(params["useFrame"] != 1)
		el.appendChild(oTable);
	//el.innerHTML = outStr;
	el.style.left = 0;
	el.style.top = 0;
	el.style.width = getValueOrDef(params["width"],"1");
	el.style.height=getValueOrDef(params["height"],"100");
	el.style.visibility = "visible";
	//el.style.backgroundColor='#ffffff';
	
	el.style.overflowY = "auto"; 
	el.style.overflowX = "auto"; 
	el.style.position = "absolute"; 
	// class styles voreinstellungen
	attachEvent("onload", XListBox_Init);
	el.style.border = getValueOrDef(params["border"], "black 1px solid");
	el.db = new XDB();
	el.className = getValueOrDef(params["class"], null);
	el.classItemSelected = el.className+"ItemSelected";//listItemSelected";
	el.classItemHover = el.className+"ItemHover"; //"listItemHover";
	el.classItemNormal = el.className+"ItemNormal";//"listItemNormal";

	return el;
}
function DoPropChange()
{
	//this.print("propChanges"+window.event.propertyName);
}
function XListBox_deleteRow(index)
{
	var oItemsTable = this.getItemsTable();
	oItemsTable.deleteRow(index);
}
function XListBox_addRow()
{	
	//this.print(this.id+".addRow()");
	var oList = this;
	var oItemsTable = this.getItemsTable();
	//this.print(oItemsTable.tagName);
	var oRow = oItemsTable.insertRow();
	//oRow.onmouseover = function anonymus() { onMouseOver(oList, oRow); }
	//oRow.onmousedown = function anonymus() { onMouseDown(oList, oRow); }
	//oRow.onmouseout = function anonymus() { onMouseOut(oList, oRow); }
	oRow.bSelected = false;
	oRow.width = "100%";
	oRow.cellData = new Array();
	oRow.className = oList.className + "ItemNormal";
	for(var i = 0; i < XListBox_addRow.arguments.length; ++i) {
		oRow.cellData[i] = XListBox_addRow.arguments[i];
		if(i+1 == this.boundColumn)
			this.boundColIdx[oRow.cellData[i]] = oRow;
		
		//this.print(oList.colWidthsArray[i]);
		if(oList.colWidthsArray[i] == null ||  oList.colWidthsArray[i] > 0) { 
			var oCell = oRow.insertCell();
			oCell.innerHTML = XListBox_addRow.arguments[i];
			//oCell.value = XListBox_addRow.arguments[i];
//			this.print("width="+oList.colWidthsArray[i]);
			if(oList.colWidthsArray[i] != null)
				oCell.width =  oList.colWidthsArray[i];
		}		
	}	  

	return oRow;
}
function XListBox_getRow(col, searchValue)
{
	//var oItemsTable = this.getItemsTable();
	if(col+1 == this.boundColumn) {
		//this.print("return from idx");
		return this.boundColIdx[searchValue];
	}
	for(var row = 0; row < this.itemsTable.rows.length; row++) {
		//this.print(oItemsTable.rows[row].cells[col].innerH)
		if(this.itemsTable.rows[row].cellData[col] == searchValue)
			return this.itemsTable.rows[row];
	}
	return null;
}

