function LookupObjects(lookupField, lookupDialog, ltable, lview, lookupStyle, lookupBrowse, bindings, additionalParams) {
	if (lookupStyle == "" || lookupStyle == "single") lookupStyle= "Xsingle";
	var url;
	url= fmDataViewerPath + "Form/Lookup/Lookup";
	url+= lookupStyle;
	url+= ".aspx?dialog=" + lookupDialog;
	url+= "&t=" + ltable + "&v=" + lview;

	if (additionalParams) url+= "&" + additionalParams;
	if (bindings) url += "&_data=" + bindings;

	var args= new LookupArgsClass();
	if (lookupField != null) {
		args.items= lookupField.getElementsByTagName("SPAN");
	}
	var features= BuildFeatures(lookupStyle);
	if (features == null)	return;
	//window.open(url);return;
	return window.showModalDialog(url, args, features);
}

function LookupArgsClass() { this.items= null; }

function BuildFieldSpan(lookupField, items) {
	var html= "";
	var len= items.length;
	var allowClick= (lookupField.handlermode != "off");
	for (var i= 0; i < len; ++i) {
		var item= items[i];
		html+= (i > 0 ? " " : "");

		var e= parent.document.createElement("SPAN");
		e.oid= item.value;
		e.otype= item.type;
		e.innerHTML= item.html;
		if (allowClick) {
			e.className= "lui";
			e.onclick= "fmOpenLookup();";
		} else {
			e.className= "ldi";
		}

		html+= e.outerHTML;
		if (lookupField.className == "mlu") html+= "<br/>";
	}
	if (html.length == 0) {
		html= "&nbsp;";
	}
	return html;
}

function Lookup_BuildField(lookupField, items) {
	lookupField.innerHTML= BuildFieldSpan(lookupField, items);
}

function BuildFeatures(lookupStyle) {
	var height; var width;
	switch (lookupStyle) {
		case "multi":  height= "490px"; width= "750px"; break;
		case "Xsingle":
		case "single": height= "490px"; width= "600px"; break;
		case "tree": height= "500px"; width= "430px"; break;
		case "multitree": height= "490px"; width= "650px"; break;
		default: alert("unknown style:" + lookupStyle); return null;
	}
	return "dialogHeight:" + height + " ;dialogWidth:" + width + ";resizable:yes;center:yes;status:no;help:no;scroll:no;";
}

// LOOKUP PARAM
//
function LookupParam(_val, _type, _text) {
	this.value= _val;
	this.type= _type;
	this.text= _text;
	this.html= "";
	this._separator= "|";
	this.Parse= LookupParam_Parse;
	this.GenerateData= LookupParam_GenerateData;
}

function LookupParam_Parse(_data) {
	if (_data == null || _data == "") return;
	this.value= _data;
	if (_data.indexOf(this._separator) == -1) return;
	var subitems= _data.split(this._separator);
	if (subitems.length == 3) {
		this.value= subitems[0];
		this.type= subitems[1];
		this.text= subitems[2];
	}
}

function LookupParam_GenerateData() {
	if (this.value == null || this.value == "") return "";
	return (this.value + this._separator + this.type + this._separator + this.text);
}

//	open a lookup item EDIT/PROPERTIES dialog
//
function fmOpenLookup() {
	var o= event.srcElement;
	if (o.tagName != "SPAN") return;
	var mode= o.parentElement.handlermode;
	if (mode == "off") return;
	var oImg= o.parentElement.parentElement.nextSibling.firstChild;
	var res= fmOpenLookupItem(null, oImg.lookupdialog, o.otype, (oImg.ltable + "/" + oImg.lview), o.oid, mode);
}

function fmOnLookupDataInput() { var o= event.srcElement; if (!IsNull(o.master)) o.master.checkEnteredValue(); }
function fmOnLookupDataInputComplete(res, resp, o) {o.onCheckEnteredValueComplete(res);}

function fmOnSuggestion(o) { if (!IsNull(o.master)) o.master.setSelectedValue(); }

function fmOpenLookupItem(wndData, dlg, type, lookup, id, mode, isModal) {
if (wndData == null) wndData= SL_DefineWndParams(mode, dlg, type, lookup, id);
//alert(mode + "\n" + wndData);
if (wndData == "") return;
var mas= wndData.split("|");
var url= mas[0]; 
var w= mas[1]; var h= mas[2]; var scroll= (mas[3] == "true");
if (isModal) {
	url+= "&isQuickForm=1&isModalContext=1&wndScroll=no";
	return openStdDlg(url, null, w, h, scroll);
} else {
	openStdWin(url, buildWinName("Wnd_" + mode + id), w, h, scroll);
	return null;
}
}

function SL_DefineWndParams(mode, dlg, type, lookup, id) {
	var s= SL_ReceiveData("GetLookupHandler", "mode=" + mode + "&dialog=" + dlg + "&type=" + type + "&lookup=" + lookup + "&id=" + id);
	if (s.indexOf("ERROR:") == 0) {alert(s); return "";}
	return s;
}

function LookupEventControl(o) {
	if (!IsNull(o.ImgBtn)) this.ImgBtn= o.ImgBtn;
	else this.ImgBtn= o;
	this.Data= (this.ImgBtn.nextSibling != null) ? this.ImgBtn.nextSibling.firstChild.value : "";
	this.Item= null;
	if (this.Data != "") { this.Item= new LookupParam(); this.Item.Parse(this.Data); }
	
	this.GetRecordInfo= L_GetRecordInfo;
}

function L_GetRecordInfo(columns) {
	if (this.Item == null) return null; // none
	var o= this.ImgBtn;
	var params= "dialog=" + o.lookupdialog + "&ltable=" + o.ltable + "&columns=" + columns + "&id=" + this.Item.value + "&type=" + this.Item.type;
	var xd= SL_ReceiveData("GetRecordInfo", params, true);
	var xn= xd.documentElement;
	if (xn.nodeName == "error") { alert(xn.text); return null;	}
	var xnCell, c;
	var aColumns= columns.split(",");
	var a= new Array();
	for (var i= 0; i < aColumns.length; i++) {
		c= aColumns[i];
		xnCell= xn.selectSingleNode(c);
		a[c]= (xnCell != null) ? xnCell.text : "";
	}
	return a;
}

function SL_ReceiveData(cmd, ps, returnXml) {
	var url= fmCommonPath + "../DataViewer/Form/Lookup/Resolver.aspx?cmd=" + cmd + "&" + ps;
	return Server_ReceiveData(url, returnXml); // utils.js
}

