//document.write("<script src=\"http://192.168.0.5/utils/parseStr.js\"></script>");
function getAbsolutePos(o) {

    var tNum = 0;
    var ret = new Object;
//    ret.top = window.document.documentElement.offsetLeft;
//    ret.left = window.document.documentElement.offsetTop;
    var el = o;
   var win = window;
	ret.top = 0;
	ret.left = 0;
	while (el != window.document.documentElement) {
		ret.left += parseInt(el.offsetLeft);//parseInt(el.style.left);
		ret.top += parseInt(el.offsetTop);//parseInt(el.style.top);
	  	if(el.offsetParent == null) break;
		el = el.offsetParent;
	}
	return ret;
	
    while(el) {
		//ret.top += parseInt(win.screenTop);
		//ret.left += parseInt(win.screenLeft);
		//this.print("docoffLeft="+win.screenLeft+"docoffTop="+win.screenTop);
		while (el != win.document.documentElement) {
			//this.print(el.tagName+","+el.uniqueID+":oL:"+el.offsetLeft+",oT:"+el.offsetTop+",cL:"+el.clientLeft+",cT:"+el.clientTop);
//			if(el.style.left) {
		   ret.left += parseInt(el.offsetLeft);//parseInt(el.style.left);
		   ret.top += parseInt(el.offsetTop);//parseInt(el.style.top);
//		   }
		   if(el.parentElement == null) 
			break;
		   el = el.parentElement;
		}
		return ret;
		if(el.document.parentWindow == el.document.parentWindow.parent)
			break;
//		this.print("parentWin:"+win.parent
		win = win.parent;
		el = win.document.parentWindow;
	}
	return ret;
}
/*
function getAbsolutePos(o, where) {
     var tNum = 0
     var el = o;
     while (el != window.document.documentElement) {
          tNum += eval("el.offset"+where+"-el.scroll"+where);
          if(el.offsetParent == null) break;
          el = el.offsetParent;

//          el = el.parentElement;
     }
	return tNum
}
*/
function XComboBox_print(msg)
{
	if(this.onPrint)
		this.onPrint(msg);
}
function XComboBox_Close()
{
	this.print(this.id+".Close");
	var oList = this.boundList;
	var listDataTable = oList.getItemsTable();
	oList.owner = null;
	oList.onblur = null;
	listDataTable.onSelectItem = null;	
	oList.style.visibility = "hidden";
	
}
function onbeforeactivate(oInp)
{
//	window.event.cancelBubble = false;
	oInp.focus();
//	this.setCapture();
	//this.print("onbeforeactivate "+oInp.id);
//	this.setActive();
}
function XComboBox_Open()
{
	this.print(this.id+".Open "+this.boundList.id);
	//this.print("combo oL:"+this.offsetLeft+" oT:"+this.offsetTop+" oW:"+this.offsetWidth+" oH:"+this.offsetHeight+", boundList="+this.boundList);
	var oComboTable = this;
	if(this.boundList == null) return;
	var el = this.boundList;
	this.focus();

	var listDataTable = this.boundList.getItemsTable();
	//this.print(listDataTable);
	var listDIV = this.boundList.getFrameDIV();
	//this.print("list set capture");
//	listDIV.onfocusin = function anonymus() {onbeforeactivate(oComboTable);}//releaseCapture();
	listDIV.style.position ="absolute";
	if(this.selectedRow) {
//		this.print("selectedRow="+this.selectedRow);
//		listDIV.selectItem(this.selectedRow);
//		this.selectedRow.scrollIntoView(true);
	}
	if(this.boundList.owner == this) {
		this.Close();
		return;
	}
	var nameList = this.id;
	this.boundList.owner = this;
	
	
	listDIV.scrollTop = 0;
	if(this.selectedRow) {
		//this.print("selectedRow="+this.selectedRow);
		listDIV.selectItem(this.selectedRow);
		this.selectedRow.scrollIntoView(true);
	}

	listWidth = parseInt(listDIV.offsetWidth);
	if(listWidth < this.offsetWidth)
		listWidth = this.offsetWidth;
	listHeight = parseInt(listDIV.offsetHeight);
	
	var abs = getAbsolutePos(this);
	
	//abs = new Object;
	
	//abs.top = window.event.screenY-window.event.y-window.screenTop+this.offsetHeight;//+window.top.document.documentElement.scrollTop;
	//abs.left = window.event.screenX-window.event.x-window.screenLeft;//+window.top.document.documentElement.scrollLeft;
	//abs.top -= window.screenTop;
	//abs.left += window.screenLeft;
	//abs.top += window.top.screenTop;
	
	this.print(window.event.screenX+":"+window.event.x+","+window.screenLeft);//+window.top.document.documentElement.scrollLeft;
	this.print(window.event.screenY+":"+window.event.y+","+window.screenTop);
//	var absLeft = getAbsolutePos(this, "Left");
//	var absTop = getAbsolutePos(this, "Top");

//	return;
//	var listAbsTop = getAbsolutePos(listDIV, "Top");
//	var listAbs = getAbsolutePos(listDIV);
	
	this.print("absTop:"+abs.top+" absLeftT:"+abs.left+", listHeight:"+listHeight+",listWidth:"+listWidth);
	//listDIV.style.top = listAbsTop + absTop;
	if(0&&abs.top + this.offsetHeight + listHeight > listDIV.document.documentElement.offsetHeight)
		listDIV.style.top = abs.top - listHeight;
	else
		listDIV.style.top = this.offsetHeight + abs.top;
	//this.print("lT:"+listDIV.style.top+" cT:"+absTop);
	if(0&&abs.left + this.offsetWidth > listWidth) {
		listDIV.style.left = abs.left + this.offsetWidth - listWidth;
	} else {
		listDIV.style.left = abs.left;
	}
	//listDIV.style.left=0;
	//listDIV.style.top=0;
	var oCombo = this;
	this.print("combo listDIV l="+listDIV.style.left+",t="+listDIV.style.top+",h="+listHeight+",w="+listWidth);
	listDIV.style.width = listWidth;
	
	listDIV.style.visibility = "visible";
	listDIV.focus();
	listDIV.onblur = function anonymus() { oCombo.Close(); }

	listDataTable.onSelectItem = function anonymus(){ XComboBox_SelectItem(oCombo, oCombo.boundList);}
	listDataTable.onUnSelectItem = listDataTable.onSelectItem;
	if(this.boundList.onShow) this.boundList.onShow();

}
function XComboBox_setValue(value)
{
	//this.print("XComboBox_setValue="+value);
	//alert("setv");
	var oCombo = this;
	oRow = oCombo.boundList.getRow(oCombo.boundColumn-1, value);
	//alert("XComboBox_setValue("+value+") found in boundList "+oRow);
	if(oRow != null) {
		oCombo.inputField.value = oRow.cellData[oCombo.viewColumn-1];
		oCombo.inputBoundField.value = oRow.cellData[oCombo.boundColumn-1];
	} else
		oCombo.inputField.value = value;
	oCombo.value = value;
	oCombo.boundValue = value;
	oCombo.selectedRow = oRow;
	oCombo.boundList.itemsTable.SelectedRow = oRow;
	if(oCombo.initDone && oCombo.onchange) oCombo.onchange();
	//this.print("setValue "+value);
	/*
	if(oCombo.boundList) {
		var items = oCombo.boundList.itemsTable;
		//this.print("length="+items.rows.length);
		for(var i = 0; i < items.rows.length; i++) {
			//this.print("check "+items.rows[i].cells[oCombo.viewColumn-1].innerText);
			//if(items.rows[i].cells[oCombo.viewColumn-1].value == value) {
			if(items.rows[i].cellData[oCombo.viewColumn-1] == value) {
				oCombo.inputBoundField.value = items.rows[i].cellData[oCombo.boundColumn-1];//cells[oCombo.boundColumn-1].value;
				oCombo.boundList.itemsTable.SelectedRow = items.rows[i];
				oCombo.selectedRow = items.rows[i];
				//this.print("found "+items.rows[i].cellData[oCombo.viewColumn-1]);
				break;
			}
		}
	}
	*/
}
function XComboBox_SelectItem(oCombo, oList)
{
	//this.print("XComboBox_SelectItem "+oList.itemsTable.SelectedRow);
	oCombo.setValue(oList.itemsTable.SelectedRow.cellData[oCombo.boundColumn-1]);
	
	//oCombo.value = oList.itemsTable.SelectedRow.cellData[oCombo.viewColumn-1];//[cells[oCombo.viewColumn-1].value;
	//oCombo.boundValue = oList.itemsTable.SelectedRow.cellData[oCombo.boundColumn-1];//cells[oCombo.boundColumn-1].value;
	//oCombo.Close();
	//oCombo.inputBoundField.value = oList.itemsTable.SelectedRow.cellData[oCombo.boundColumn-1];//cells[oCombo.boundColumn-1].value;
	//this.print("XComboBox_SelectItem boundValue="+oCombo.inputBoundField.value);
	oCombo.focus();
}

function XComboBox_Blur(oCombo)
{
	//this.print(this+".Blur()");
	var listDIV = oCombo.boundList;//.getFrameDIV();
	var mX = window.event.clientX;
	var mY = window.event.clientY;
	if(mX > listDIV.offsetLeft && mX < listDIV.offsetLeft + listDIV.offsetWidth &&
		mY > listDIV.offsetTop && mY < listDIV.offsetTop + listDIV.offsetHeight)
		return;
	if(oCombo.boundList.owner == oCombo) {
		oCombo.Close();
	}
}
function XComboBox_Init(name, el)
{
	//el.onpropertychange = function anonymus() {XComboBox_DoPropertyChange(el); }
	el.initDone = true;
	// properties setzen
	//el.setValue(el.value);
}
function XComboBox_DoPropertyChange(oCombo)
{
	//detachEvent("onpropertychange", oCombo.onpropertychange);
	//var stoOnPropertyChangeHandler = oCombo.onpropertychange;
	//this.print(oCombo.id+"pc:"+window.event.propertyName);
	//oCombo.onpropertychange = null;
	switch(window.event.propertyName) {
/*	case "value":
		oCombo.setValue(oCombo.value);
		if(oCombo.onchange) oCombo.onchange();
		break;*/
	case "width":
		oCombo.inputField.style.width = oCombo.width;
		break;
	case "height":
		this.print("change height to "+oCombo.height);
		oCombo.inputField.style.height = oCombo.height;
		oCombo.style.height = oCombo.height;
	}
	
	//attachEvent("onpropertychange", oCombo.onpropertychange);
	//oCombo.onpropertychange = stoOnPropertyChangeHandler;
}
function XComboBox(paramStr)
{
	params = parseStr(paramStr);	
	var el = document.createElement("DIV");
	el.initDone = false;
	el.id = getValueOrDef(params["name"], "")+"DIV_"+el.uniqueID;
	el.style.width = getValueOrDef(params["width"], 150);
	el.style.height = getValueOrDef(params["height"], 22)
	
	var oTable = document.createElement("TABLE");
	oTable.cellSpacing = 0;
	oTable.cellPadding = 0;
	//oTable.border=1;
	oTable.width = "100%";
	oTable.height = "100%";
	oTable.border = 0;
	var oRow = oTable.insertRow();
	oRow.height = "100%";
//	<TEXTAREA id=txtComments style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; OVERFLOW: hidden; BORDER-LEFT: 0px; WIDTH: 100%; BORDER-BOTTOM: 0px; HEIGHT: 100%" rows=1 wrap=off cols=1>   The patient is in stable condition after suffering an attack of the insatiable munchies.</TEXTAREA>
	var oInput = document.createElement("input");
	oInput.style.width = "100%";//parseInt(el.style.width) - 20;
	oInput.style.height = "100%";//parseInt(el.style.height);
	//oInput.width = "100%";
	oInput.style.wrap="off";
	oInput.style.border = "0";
	//oInput.onfocusin = function anonymus() { this.print("comboInp focus");el.onfocusin(oInput); }
	/*
	oInput.style.borderTop = 0;
	oInput.style.borderBottom = 0;
	oInput.style.borderLeft = 0;
	oInput.style.borderRight = 0;
	*/

	el.inputField = oInput;
	var oCell = oRow.insertCell(); // Daten Zelle
	oCell.width = "100%";
	oCell.appendChild(oInput);
	var oInput = document.createElement("INPUT");
	oInput.type = "hidden";
	el.inputBoundField = oInput;
	el.inputBoundField.name = getValueOrDef(params["name"], "");
	oCell.appendChild(oInput);
	var oCell = oRow.insertCell(); // openButton Zelle
	oCell.align = "right";
	//oCell.width = 20;
	oCell.innerHTML = "<img src=/utils/images/comboOpen.gif>";/*
	oCell.style.backgroundImage = "url(c:/projekte/www/call2000/images/comboOpen.gif)";
	oCell.style.backgroundRepeat = "no-repeat";
	oCell.style.backgroundAttachment = "scroll";
	oCell.style.backgroundPosition = "right top";*/
	el.appendChild(oTable);
	el.style.border = "solid gray 1px";
	/*
	el.style.borderTop = "gray 1px solid";
	el.style.borderBottom = "gray 1px solid";
	el.style.borderLeft = "gray 1px solid";
	el.style.borderRight = "gray 1px solid";
	*/
	el.style.overflow = "hidden";
	
	el.Open = XComboBox_Open;
	el.Close = XComboBox_Close;
	el.setValue = XComboBox_setValue;
	el.attachListBox = XComboBox_attachListBox;
	el.print = XComboBox_print;
//	el.getItemsTable = function anonymus() { return oTable;//document.getElementById(el.id+"TABLE"); }
	el.DoInit = function anonymus(){ XComboBox_Init(el.id, el);}
	
	el.value="";
	el.viewColumn = getValueOrDef(params["viewColumn"], 1);
	el.boundColumn = getValueOrDef(params["boundColumn"], 1);
	attachEvent("onload", el.DoInit);
	
	oTable.onmousedown = function anonymus() { el.Open(); };
	oTable.onblur = function anonymus() { XComboBox_Blur(el); };
	
	// properties setzen
	
	//el.setValue(el.value);
	//el.onpropertychange = function anonymus() {XComboBox_DoPropertyChange(el); }

	return el;
}

function XComboBox_attachListBox(oListBox)
{
	this.boundList = oListBox;
	oListBox.indexColumn = this.boundColumn;
	oListBox.style.visibility = "hidden";
}

