// --- call back object (cbo) --------------------------------------------- ---
// --- -------------------------------------------------------------------- ---
/*
	TODO: 
	- za predefinirane vrednosti multisearcha dolociti edit in delete pravice  
*/

var srchTimeout=0;
var reNbsp=/&nbsp;/g;
function ieV() { var v=99; if (navigator.appVersion.indexOf("MSIE")!=-1) v=parseFloat(navigator.appVersion.split("MSIE")[1]); return v; }
if( typeof XMLHttpRequest=="undefined" ) XMLHttpRequest=function() { try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e) {} try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e) {} try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} throw new Error("This browser does not support XMLHttpRequest."); };
function request() { this.XmlHttp=new XMLHttpRequest(); this.wd=new WddxDeserializer; }

request.prototype.doCall=function() {
	var uID=Math.round(Math.pow(10,18)*Math.random());
	var url=this.srch.cfc; var sTop=this.srch.resultTop; var sType=this.srch.srchType; if (this.srch.srchType=='EXACT' && this.srch.lastKey==40) sType='LIKESTART';
	var s=encodeURIComponent(this.srch.srchFld.value); //escape, encodeURI, encodeURIComponent
	if (this.srch.method=='' || this.srch.method==' ') return false; 
	url+='?method=srchCall'; url+='&srchMethod='+this.srch.method; url+='&srchString='+s;
	if (this.srch.resultStart>0) url+='&srchStart='+this.srch.resultStart;
	if (this.srch.multiple && !this.srch.suggest) { var lst=this.srch.multiLst(); if (lst!='') url+='&srchExclude='+ lst + '&srchExcludeDelim=' + this.srch.excludeDelim; }
	if ((this.srch.srchFld.value.length >= this.srch.minLength && this.srch.srchFld.value!=this.srch.lastVal) || (this.srch.resultStart!=0)) {
		var a=this.srch.srchFld.getAttribute('srchParams'); 
		if (a!=null && a.length>0) {
			var params=a.split(',');
			for (var i=0; i < params.length; i++) {
				if (params[i].indexOf('=')!=-1) {
					var p=params[i].split('=');
					if (p.length>1 && p[1].length) {
						switch(p[0]) {
							case 'srchType': sType=p[1]; break;
							case 'srchTop': sTop=p[1]; break;
							default: try { if (p[1].indexOf('ID:')!=-1) { var el=document.getElementById(p[1].split(':')[1]); if (el) p[1]=el.value; } else { p[1]=eval(p[1]); } } catch(e) {}; url+='&'+p[0]+'='+encodeURIComponent(p[1]); break;
						}
					} else { url+='&'+params[i]; }
				} else { url+='&'+params[i]+'='+encodeURIComponent(eval('document.'+this.srch.srchFld.form.name+'.'+params[i]+'.value')); }
			}
		}
		url+='&srchTop='+sTop+'&srchType='+sType+'&uID='+uID;
		if (this.XmlHttp) {
			if( this.XmlHttp.readyState==4 || this.XmlHttp.readyState==0 ) {
				this.srch.lastVal=this.srch.srchFld.value;
				if (!this.srch.suggest) this.srch.srchFldId.value='';
				var fThis=this;
				this.XmlHttp.open('GET', url, true);
				this.XmlHttp.onreadystatechange=function() { fThis.readyStateChange(); };
				this.XmlHttp.send(null);
			} else window.status+='[NOT READY]';
		}
	}	
	return true;
};
request.prototype.abortCall=function() { window.clearTimeout(srchTimeout); if (this.XmlHttp) this.XmlHttp.abort(); };
//request.prototype.onComplete=function(responseText, responseXml) {};
//request.prototype.onError=function(status, statusText, responseText) { };
request.prototype.readyStateChange=function() { if (this.XmlHttp.readyState==4) { if (typeof this.XmlHttp!='undefined' && typeof this.XmlHttp.status!='undefined') { this.srch.searching=false; if (this.XmlHttp.status==200 && this.XmlHttp.statusText=='OK') this.onComplete(this.XmlHttp.responseText, this.XmlHttp.responseXML); } } };

// --- -------------------------------------------------------------------- ---
// --- edtSrch ------------------------------------------------------------ ---
// --- -------------------------------------------------------------------- ---
function initSrchArray(lst,d) { if (lst!='') { if (typeof d=='undefined') d=';'; if (lst.indexOf("DATA=")==0) { var dt=lst.replace('DATA=', ''); return eval(dt); } var result=new Array(); var ar=lst.split(d); var p=''; var i=''; for (var a=0; a<ar.length; a++) { p=ar[a].split(':'); i=new Object(); i.value=p[0]; i.text=p[0]; if (p.length>1) i.text=p[1]; i.info=''; if (p.length>2) i.info=p[2]; i.clss=''; if (p.length>3) i.clss=p[3]; i.icon=''; if (p.length>4) i.icon=p[4]; i.rdonly=true; if (p.length>5) i.rdonly=p[5]; result[result.length]=i; } return result; } else return null; }
function initSrchArrayLst(val,txt,d) { if (val!='') { if (typeof d=='undefined') d=';'; var result=new Array(); var aV=val.split(d); var aT=txt.split(d); var i; for (var a=0; a<aV.length; a++) { i=new Object(); i.value=aV[a]; i.text=(aT.length) ? aT[a] : i.value; i.info=''; i.clss=''; i.icon=''; i.rdonly=true; result[result.length]=i; } return result; } else return null; }

var edtSrchCount=0;
function bool(b) { if (typeof b=='boolean') return b; else { b=b.toLowerCase(); if (b=='true' || b=='1') return true; if (b=='false' || b=='0') return false; } return false; }
function edtSrch(id, sMethod) { this.init(id, sMethod); }

function srchFormChange() { if (typeof dt_change=='function') dt_change(); if (typeof formOnChange=='function') formOnChange(); }
function srchTimeoutCallback(id) { if (id!='') document.getElementById(id).srch.search(); }
function srchTimeoutItemSelect(id) { if (id!='') document.getElementById(id).srch.sdEnter(); }
function srchMultiDel(id,c) { var s=document.getElementById(id+"Srch"); if (s && !s.readOnly) { for (var i=s.srch.multiEls.length-1;i>-1;i--) { if (s.srch.multiEls[i]==id+"_"+c) { var d=document.getElementById(s.srch.multiEls[i]+"_div"); if (d) { s.srch.multiOnSelect('DEL',c); d.parentNode.removeChild(d); s.srch.multiEls.splice(i,1); s.srch.multiChk(); srchFormChange(); break; } } } } }
function srchMultiUp(id,c) { var s=document.getElementById(id+"Srch"); if (s && !s.readOnly) { for (var i=s.srch.multiEls.length-1;i>-1;i--) { if (s.srch.multiEls[i]==id+"_"+c) { var d=document.getElementById(s.srch.multiEls[i]+"_div"); if (d) { s.srch.multiOnSelect('MOVE_UP',c); var p=d.previousSibling; if (p&&p.id&&p.id.indexOf(id+"_")==0) { d.parentNode.insertBefore(d.cloneNode(true),p); d.parentNode.removeChild(d); } } srchFormChange(); break; } } } }
function srchMultiDn(id,c) { var s=document.getElementById(id+"Srch"); if (s && !s.readOnly) { for (var i=s.srch.multiEls.length-1;i>-1;i--) { if (s.srch.multiEls[i]==id+"_"+c) { var d=document.getElementById(s.srch.multiEls[i]+"_div"); if (d) { s.srch.multiOnSelect('MOVE_DOWN',c); var n1=d.nextSibling; var n2=n1.nextSibling; if (n1.id.indexOf(id+"_")==0) { d.parentNode.insertBefore(d.cloneNode(true),n2); d.parentNode.removeChild(d); } } srchFormChange(); break; } } } }
// --- -------------------------------------------------------------------- ---
edtSrch.prototype.init=function(id, sMethod) {
	edtSrchCount++;
	this.srchId=id;  
	this.srchFld=document.getElementById(id);
	this.srchFldId=document.getElementById(id.replace(/Srch$/, ''));	
	this.cfc='cfc/editorSearch.cfc'; this.method=sMethod; this.imgDir='img/';
	this.srchType='LIKE'; //EQ, LIKE, LIKESTART, LIKEEND, EXACT
	this.timeout=1000; this.minLength=1; this.suggest=0; this.srchSetId=0; this.valMinLength=0; this.resultTop=10;
	this.resultCount=0; this.resultStart=0; this.onSelect=null; this.clearId=true; this.autoSelect=false; this.lastKey='';
	this.visible=false; this.searching=false; this.lastVal=''; this.srchQuery=null;
	this.srchDelim=';'; this.excludeDelim='_'; this.mouseOver=0; this.hoX=22; this.hoY=2; this.selected=0; this.enter=0; this.srchPos=-1; //this.align='left'; // right
	this.multiple=false; this.multiMin=0; this.multiMax=100; this.multiCnt=0; this.multiReorder=false; this.multiEls=null; this.multiPos=-1; this.multiAction='';
	
	this.str_searching='iskanje...'; this.str_error='ups, napaka pri iskanju :('; this.str_nomatch='ni zadetkov';
	this.str_showing1='zadetkov: '; this.str_showing2=' od '; this.str_showing3='.';

	var a;
	a=this.srchFld.getAttribute('srchCFC'); if (a!=null && a.length>0) this.cfc=a;
	a=this.srchFld.getAttribute('srchType'); if (a!=null && a.length>0) this.srchType=a;
	a=this.srchFld.getAttribute('srchMin'); if (a!=null && a.length>0) this.valMinLength=a;
	a=this.srchFld.getAttribute('srchTop'); if (a!=null && a.length>0) this.resultTop=a;
	a=this.srchFld.getAttribute('srchSuggest'); if (a!=null && a.length>0) this.suggest=a;
	a=this.srchFld.getAttribute('srchDelim'); if (a!=null && a.length>0) this.srchDelim=a;
	a=this.srchFld.getAttribute('srchList'); if (a!=null && a.length>0) this.srchQuery=initSrchArray(a, this.srchDelim); 
	a=this.srchFld.getAttribute('srchOnSelect'); if (a!=null) a=a; else a=this.method; if (a!='') try { this.onSelect=eval(a); } catch (er) { window.status=id+'-Search Error: Invalid onSelect method !'; }
	a=this.srchFld.getAttribute('srchType'); if (a!=null && a.length>0) this.srchType=a.toUpperCase();
	a=this.srchFld.getAttribute('srchTimeout'); if (a!=null && a.length>0) this.timeout=a;
	a=this.srchFld.getAttribute('srchImgDir'); if (a!=null && a.length>0) this.imgDir=a;
	a=this.srchFld.getAttribute('srchClearInvalidId'); if (a!=null && a.length>0) { this.clearId=bool(a); }
	a=this.srchFld.getAttribute('srchAutoSelectOneMatch'); if (a!=null && a.length>0) { this.autoSelect=bool(a); }
	a=this.srchFld.getAttribute('srchAutoSelect'); if (a!=null && a.length>0) { this.autoSelect=bool(a); }
	a=this.srchFld.getAttribute('srchMulti'); if (a!=null && a.length>0) this.multiple=bool(a);
	a=this.srchFld.getAttribute('srchMultiMin'); if (a!=null && a.length>0) this.multiMin=Number(a);
	a=this.srchFld.getAttribute('srchMultiMax'); if (a!=null && a.length>0) this.multiMax=Number(a);
	a=this.srchFld.getAttribute('srchMultiReorder'); if (a!=null && a.length>0) this.multiReorder=bool(a);
	a=this.srchFld.getAttribute('srchClass'); if (a!=null && a.length>0) this.divClass=a;
	a=this.srchFld.getAttribute('srchParams'); if (a!=null && a.length>0) this.setParams(a);
	
	if (this.method!='srchList') this.initReq();
	this.initInput(); this.initDiv('sd', 'searchDiv'); this.initDiv('hd', 'searchHintDiv');
};

edtSrch.prototype.setParams=function(params) {
	if (params=='') return; 
	var a=params.split(',');
	for (var i=0; i < a.length; i++) {
		if (a[i].indexOf('=')!=-1) {
			var p=a[i].split('=');
			switch(p[0]) {
				case 'srchCFC': this.cfc=p[1]; break;
				case 'srchType': this.srchType=p[1]; break;
				case 'srchMin': this.valMinLength=p[1]; break;
				case 'srchTop': this.resultTop=p[1]; break;
				case 'srchSuggest': this.suggest=p[1]; break;
				case 'srchDelim': this.srchDelim=p[1]; break;
				case 'srchList': this.srchQuery=initSrchArray(p[1], this.srchDelim); break; 
				case 'excludeDelim': this.excludeDelim=p[1]; break;
				case 'srchSetId': this.srchSetId=bool(p[1]); break;
				case 'srchType': this.srchType=p[1].toUpperCase(); break;
				case 'srchTimeout': this.timeout=p[1]; break;
				case 'srchImgDir': this.imgDir=p[1]; break;
				case 'srchClearInvalidId': this.clearId=bool(p[1]); break;
				case 'srchAutoSelectOneMatch': this.autoSelect=bool(p[1]); break;
				case 'srchAutoSelect': this.autoSelect=bool(p[1]); break;
				case 'srchClass': this.divClass=p[1]; break;
				case 'srchMulti': this.multiple=bool(p[1]); break;
				case 'srchMultiMin': this.multiMin=Number(p[1]); break;
				case 'srchMultiMax': this.multiMax=Number(p[1]); break;
				case 'srchMultiReorder': this.multiReorder=bool(p[1]); break;
				case 'hintOffsetX': this.hoX=Number(p[1]); break;
				case 'hintOffsetY': this.hoY=Number(p[1]); break;
			}
		}
	}
};

edtSrch.prototype.initInput=function() {
	this.srchFld.srch=this; this.srchFld.setAttribute('autocomplete', 'OFF');
	addEvent(this.srchFld, 'keydown', edtSrch.prototype.fldKeydown);	
	addEvent(this.srchFld, 'paste', edtSrch.prototype.fldPaste);	
	addEvent(this.srchFld, 'blur', edtSrch.prototype.fldBlur);	
	addEvent(this.srchFld, 'click', edtSrch.prototype.fldClick);
	if (!this.suggest) { addEvent(this.srchFld, 'focus', edtSrch.prototype.fldFocus); this.startVal=''; this.startId=''; }
	if (this.multiple) this.multiInit();
};

edtSrch.prototype.multiInit=function() { if (this.srchFld.value!='') { if (!this.suggest) var ids=this.srchFldId.value.split(this.srchDelim); var vals=this.srchFld.value.split(this.srchDelim); var i,v=''; for (var j=0; j<vals.length; j++) { v=vals[j]; i=''; if (!this.suggest && j < ids.length) i=ids[j]; this.newInput(i,v, false); } } };
edtSrch.prototype.multiChk=function(f) { if (typeof f=='undefined') f=true; if (!this.srchFld.readOnly && (this.multiMax>0 && this.multiEls.length < this.multiMax)) { if (f) { this.srchFld.className="search focus"; this.srchFld.focus(); } else this.srchFld.className="search"; } else { this.srchFld.className="search readonly"; if (f) this.finish(); } };
edtSrch.prototype.multiLst=function(d) { var lst=''; if (typeof d=='undefined') d=this.excludeDelim; if (this.multiEls!=null) { l=this.multiEls.length; for(var i=0; i<l; i++) { e=document.getElementById(this.multiEls[i]); if (e && e.value!='') { if (lst!='' && i < l ) lst+=d; lst+=e.value; } } } return lst; };
edtSrch.prototype.multiSet=function(id, val, c) { this.multiClear(); this.srchFldId.value=id; this.srchFld.value=val; this.multiInit(); }
edtSrch.prototype.multiClear=function() { if (this.multiEls!=null) { var d; for (var i=this.multiEls.length-1; i >= 0; i--) { d=document.getElementById(this.multiEls[i]+"_div"); if (d) d.parentNode.removeChild(d); } this.multiEls.length=0; } };
edtSrch.prototype.multiOnSelect=function(a,p) { if (this.onSelect) { this.multiAction=a; this.multiPos=p; this.onSelect(); this.multiAction=''; this.multiPos=-1;} };

edtSrch.prototype.newInput=function(i,v,f) {
	if (this.multiEls==null) this.multiEls=new Array();
	if (!this.suggest) { var id=this.srchFldId.id; var ih=document.createElement("input"); ih.setAttribute('type', this.srchFldId.type); ih.setAttribute('id', this.srchFldId.id+"_"+this.multiCnt); ih.setAttribute('name', this.srchFldId.name); if (this.srchFldId.size) ih.size=this.srchFldId.size; ih.value=i; ih.readOnly=this.srchFldId.readOnly; ih.className=this.srchFldId.className; } else { var id=this.srchFld.id; }
	var is=document.createElement("input"); is.setAttribute('id', this.srchFld.id+"_"+this.multiCnt); is.setAttribute('name', this.srchFld.name); is.size=this.srchFld.size; is.value=v; is.className="readonly"; is.readOnly=true;
 	if (this.multiReorder) {
 		var dn=document.createElement("a"); dn.setAttribute("border", 0); dn.href="javascript:srchMultiDn('"+id+"',"+this.multiCnt+")";
 		var di=document.createElement("img"); di.setAttribute("width", 16); di.setAttribute("height", 16); di.setAttribute("border", 0); di.setAttribute("src", this.imgDir+"btn_down.png"); dn.appendChild(di);
 		if (this.srchFld.readOnly) { di.style.display='none'; dn.style.display='none'; }

 		var up=document.createElement("a"); up.setAttribute("border", 0); up.href="javascript:srchMultiUp('"+id+"',"+this.multiCnt+")";
 	 	var ui=document.createElement("img"); ui.setAttribute("width", 16); ui.setAttribute("height", 16); ui.setAttribute("border", 0); ui.setAttribute("src", this.imgDir+"btn_up.png"); up.appendChild(ui);
 	 	if (this.srchFld.readOnly) { ui.style.display='none'; up.style.display='none'; }
 	}
 	var a=document.createElement("a"); a.setAttribute("border", 0); a.href="javascript:srchMultiDel('"+id+"',"+this.multiCnt+")";
 	var ai=document.createElement("img"); ai.setAttribute("width", 16); ai.setAttribute("height", 16); ai.setAttribute("border", 0); ai.setAttribute("src", this.imgDir+"btn_dataDel.png"); a.appendChild(ai);
 	if (this.srchFld.readOnly) { ai.style.display='none'; a.style.display='none'; }

 	var p=document.getElementById(id+"Multi"); var d=document.createElement("div"); d.setAttribute("id", id+"_"+this.multiCnt+"_div"); d.style.marginBottom="1px";
 	if (!this.suggest) d.appendChild(ih); d.appendChild(is); if (this.multiReorder) { d.appendChild(dn); d.appendChild(up); } d.appendChild(a); p.parentNode.insertBefore(d, p);
 	this.multiEls[this.multiEls.length]=id+"_"+this.multiCnt; this.multiChk(f); this.srchFldId.value=""; this.srchFld.value=""; this.multiCnt++;
 	if (f) { this.multiOnSelect('ADD', this.multiCnt-1); srchFormChange(); }
};

edtSrch.prototype.initDiv=function(n, c) { var d=document.getElementById('DIV_SRCH_'+n); if (!d) { var d=document.createElement('DIV'); d.id='DIV_SRCH_'+n; d.className=c; d.style.position='absolute'; d.style.display='none'; d.style.zIndex=1000000 + edtSrchCount; d.srch=this; document.body.appendChild(d); } this[n]=d; };
edtSrch.prototype.initReq=function() { this.req=new request(); this.req.srch=this; this.req.onComplete=edtSrch.prototype.srchReqComplete; this.req.onError=edtSrch.prototype.reqError; };

edtSrch.prototype.search=function() {
	var txt,val,incl,e,row,ico=''; var rc, qc;
	this.sd.innerHTML=''; this.hd.innerHTML=''; this.resultCount=0; this.srchPos=-1;
	if (this.srchFld.value=='') this.srchFld.value='*'; 
	if (this.method=='srchList') {
		try {
			this.searching=true; window.clearTimeout(srchTimeout);
			var ss=encodeURIComponent(this.srchFld.value);
			var rs=(this.srchType=='LIKESTART') ? '^' : ''; if (ss!='' && ss!='*') var re=new RegExp(rs+'('+ss+')', 'i');
			this.resultEnd=Number(this.resultStart)+Number(this.resultTop);
			if (Number(this.resultEnd)>this.srchQuery.length) this.resultEnd=this.srchQuery.length; 
			this.resultShown=0;
			for (var a=0; a<this.srchQuery.length; a++) {
				txt=this.srchQuery[a].text; val=this.srchQuery[a].value;
				incl=true; if (this.multiEls!=null) { var l=this.multiEls.length; for(var i=0; i<l; i++) { e=document.getElementById(this.multiEls[i]); if (e && e.value==val) { incl=false; break; } } }
				if (incl && (ss=='' || ss=='*' || txt.match(re))) {
					this.resultCount++;
					if (this.resultStart+1 <= this.resultCount && this.resultCount <= this.resultEnd) {
						this.resultShown++;
						if (txt==this.srchFld.value) this.srchPos=this.resultCount;
						if (txt!='') try { txt=txt.replace(re, '<b>$1</b>'); } catch (er) { }
						ico=this.srchQuery[a].icon; if (ico!='') { if (!ico.match(/\.png|\.gif$/)) ico=this.imgDir+'srch_'+ico+'.png'; else txt='<img src="'+ico+'" width="16" height="12">'+txt; }
						if (this.resultCount) { row=this.resultCount + ' '; if (row.length < 3) row='0' + row; txt='<span class="row">' + row + '</span>' + txt; }	
						if (this.srchQuery[a].info!='') { txt+=' <span class="info">'+this.srchQuery[a].info+'</span>'; }
						if (val=='') this.srchQuery[a].clss='disabled'; this.newSrchItem(txt, this.resultShown-1, this.srchQuery[a].clss, false, a);
					}
				} 
			}
			rc=this.resultCount; qc=this.resultShown;
			if (rc>0) {
				if (this.srchPos>-1) this.sd.childNodes[this.srchPos].className='selected';
				txt=this.str_showing1 + qc;
				if (rc==qc) { txt+=this.str_showing3; this.newSrchItem(txt,-1,'info'); } 
				else { if (Number(this.resultStart)>0) { var s=Number(this.resultStart)+1; var e=s+qc-1; txt=this.str_showing1 + s + ' - ' + e; } txt+=this.str_showing2 + rc + this.str_showing3; this.newSrchItem(txt,-1,'info', true); }
				if (this.resultCount==1) {
					var v=this.srchQuery[0].value;
					if (typeof v!='undefined' && (v.toString()!='')) { this.srchPos=0; this.sdSelect(this.srchPos); if (this.autoSelect) srchTimeout=window.setTimeout("srchTimeoutItemSelect('"+this.srchFld.id+"');", 70); }
				}
			} else this.noMatch(this.srchFld.value);
			this.lastVal=this.srchFld.value;
			this.searching=false;
			this.sdShow();
		} catch(e) { this.sd.innerHTML=''; this.resultCount=0; this.error(); }
	} else { this.showSearch(); this.req.abortCall(); this.req.doCall(); }
	if (this.srchFld.value=='*') this.srchFld.value='';
};

edtSrch.prototype.srchReqComplete=function(responseText, responseXML) { 
	var row,txt,val,cls,ico; var rc,qc=0;
	this.srch.sd.innerHTML=''; 
	try {
		if (responseText.indexOf('!EXPIRED!') >= 0) { alert('Seja je potekla.'); top.document.location.reload(); } 
		if (responseText.indexOf('<!-- " --->') >= 0 || responseText.indexOf('cfdump_') >= 0) { this.srch.error(); return; } 
		var response=this.srch.req.wd.deserialize(responseText);
		var em=(typeof response.EXACTMATCH!='undefined') ? response.EXACTMATCH : false;
		if (response.EMPTY=='error') {
			this.srch.error();
		} else if (response.EMPTY=='false' || !response.EMPTY) {
			var q=this.srch.srchQuery=response.Q; 
			if (em) { this.srch.srchPos=0; this.srch.sdSetInput(q.getField(0, 'SRCHVALUE'), q.getField(0, 'SRCHTEXT')); this.srch.finish(); return; }
			qc=q.getRowCount(); if (qc>this.srch.resultTop) qc=this.srch.resultTop; 
			rc=(typeof response.COUNT!='undefined') ? Number(response.COUNT) : qc;
			this.srch.resultStart=(typeof response.STARTROW!='undefined') ? Number(response.STARTROW) : 1; 
			this.srch.resultCount=rc;
			if (this.srch.resultCount>0) {
				var txt=this.srch.srchFld.value; txt=txt.replace('*',''); txt=txt.replace('?','');
				if (txt!='') var re=new RegExp('('+txt+')', 'ig');
				for (var i=0; i<qc; i++) {
					txt=q.getField(i, 'srchtext'); if (txt==this.srch.srchFld.value) this.srch.srchPos=i;
					val=(q.isColumn('srchvalue')) ? q.getField(i, 'srchvalue') : '';
					try { if (re && val!='') txt=txt.replace(re, '<b>$1</b>'); } catch (er) { }
					if (q.isColumn('srchicon')) { ico=q.getField(i, 'srchicon'); if (ico!='' && !ico.match(/\.png|\.gif$/)) ico=this.srch.imgDir+'srch_'+ico+'.png'; if (ico!='') txt='<img src="'+ico+'" width="16" height="12">&nbsp;'+txt; }
					if (rc) { row=this.srch.resultStart+i+' '; if (row.length < 3) row='0'+row; txt='<span class="row">'+row+'</span>'+txt; }
					if (q.isColumn('srchinfo')) { hnt=q.getField(i, 'srchinfo'); hi=(typeof response.HIGHLIGHTINFO!='undefined') ? response.HIGHLIGHTINFO : false; if (hi) hnt=hnt.replace(re,'<b>$1</b>'); txt+=' <span class="info">'+hnt+'</span>'; }
					if (q.isColumn('srchclass')) cls=q.getField(i,'srchclass'); else cls='';
					if (val.toString()=='') cls='disabled'; this.srch.newSrchItem(txt,i,cls);
					if (q.isColumn('srchhint')) this.srch.newHintItem(q.getField(i, 'srchhint'), i); 
				}
				if (this.srch.srchPos>-1) this.srch.sdSelect(this.srch.srchPos);
				if (rc) {
					txt=this.srch.str_showing1 + qc;
					if (rc==qc) { txt+=this.srch.str_showing3; this.srch.newSrchItem(txt,-1,'info'); } else { if (Number(this.srch.resultStart)>0) { var s=Number(this.srch.resultStart); var e=s+qc-1; txt=this.srch.str_showing1 + s + ' - ' + e; } txt+=this.srch.str_showing2 + rc + this.srch.str_showing3; this.srch.newSrchItem(txt,-1,'info', true); }
				}
				if (this.srch.resultCount==1) { var v=q.getField(0, 'srchvalue'); if (typeof v!='undefined' && (v.toString()!='')) { this.srch.srchPos=0; this.srch.sdSelect(0); if (this.srch.autoSelect) srchTimeout=window.setTimeout("srchTimeoutItemSelect('"+this.srch.srchFld.id+"');", 70); } }
				this.srch.sdShow();
			} else this.srch.noMatch(this.srch.srchFld.value);
		} else { if (em) this.srch.finish(); else this.srch.noMatch(this.srch.srchFld.value); }
	} catch(e) { this.srch.sd.innerHTML=''; this.srch.resultCount=0; this.srch.error(); }
};

edtSrch.prototype.reqError=function(status, statusText, responseText) { this.srch.error(); window.status='Search Error: status=' + status + ', statusText=' + statusText; };
edtSrch.prototype.showSearch=function() { if (!this.searching) { this.searching=true; this.sd.innerHTML=''; this.hd.innerHTML=''; var d=document.createElement('DIV'); d.innerHTML=this.str_searching; d.className='info'; this.sd.appendChild(d); this.sdShow(); } };
edtSrch.prototype.noMatch=function(txt) { this.sd.innerHTML=''; this.hd.innerHTML=''; var src=this.str_nomatch; if (txt!='') src+=' ('+txt+')'; src+='.'; var d=document.createElement('DIV'); d.innerHTML=src; d.className='info'; this.sd.appendChild(d); this.sdShow(); this.visible=false; this.srchPos=-1; };
edtSrch.prototype.error=function(txt) { this.sd.innerHTML=''; var src=this.str_error; if (typeof txt!='undefined' && txt!='') src+=txt; var d=document.createElement('DIV'); d.innerHTML=src; d.className='info'; this.sd.appendChild(d); this.sdShow(); this.visible=false; this.srchPos=-1; };
edtSrch.prototype.abort=function() { window.clearTimeout(srchTimeout); this.sdHide(); };

edtSrch.prototype.newSrchItem=function(src,i,cls,nav,ap) { 
	var d=document.createElement('DIV'); d.innerHTML=src; d.srch=this; d.cls=cls; d.pos=i; d.cls=cls; if (typeof cls!='undefined' && cls!='') d.className=cls; if (typeof ap!='undefined') d.aPos=ap;  
	if (i!=-1) { d.onmousedown=edtSrch.prototype.sdMouseDown; d.onmouseover=edtSrch.prototype.sdMouseOver; d.onmouseout=edtSrch.prototype.sdMouseOut; }
	if (nav) {
		var c=document.createElement('DIV'); c.pos=-1; c.style.position='static'; var s=Number(this.resultStart); var t=Number(this.resultTop);
		var n=document.createElement('DIV'); n.innerHTML='&laquo;&laquo;'; n.id='SRCH_NAV_33'; n.pos='-33'; n.srch=this;
		if (s-t >= 0) { n.onmousedown=edtSrch.prototype.sdMouseDown; n.onmouseover=edtSrch.prototype.sdMouseOver; n.onmouseout=edtSrch.prototype.sdMouseOut; n.className='nav'; } else n.className='navDisabled'; n.cls=n.className;
		c.appendChild(n);
		var re=s+t; n=document.createElement('DIV'); n.innerHTML='&raquo;&raquo;'; n.id='SRCH_NAV_34'; n.pos='-34'; n.srch=this;
		if (s+t-1 < this.resultCount) { n.onmousedown=edtSrch.prototype.sdMouseDown; n.onmouseover=edtSrch.prototype.sdMouseOver; n.onmouseout=edtSrch.prototype.sdMouseOut; n.className='nav'; } else n.className='navDisabled'; n.cls=n.className;
		c.appendChild(n); d.className='nav info'; d.style.margin='2px';
		c.appendChild(d); this.sd.appendChild(c);
	} else this.sd.appendChild(d);
};

edtSrch.prototype.fldKeydown=function(ev) { 
	var k=(window.event) ? window.event.keyCode : ev.which; this.srch.lastKey=k; this.srch.enter=0;
	var c=String.fromCharCode(k);
	if (!ev) ev=window.event; if (ev.ctrlKey && ("cCvV-").indexOf(c)>-1) return false; 
	if (this.disabled || this.readOnly || this.srch.disabled || this.srch.readOnly) { stopEvent(ev); return false; }
	switch(k) {
		case 9: case 16: case 17: case 18: case 35: case 36: case 37: case 39: case 45: return false;
		case 8: this.srch.sdBck(); break; 
		case 13: this.srch.sdEnter(); stopEvent(ev); return false; break; 
		case 27: this.srch.sdEsc(); stopEvent(ev); return false; break; 
		case 33: this.srch.sdPgUp(); stopEvent(ev); return false; break; 
		case 34: this.srch.sdPgDn(); stopEvent(ev); return false; break; 
		case 38: this.srch.sdUp(); stopEvent(ev); return false; break;
		case 40: this.srch.sdDn(); stopEvent(ev); return false; break; 
		case 46: this.srch.sdDel(ev); break; 
	}
	if (!this.srch.suggest) this.srch.srchFldId.value=''; this.srch.resultStart=0; this.srch.abort();
	if (this.srch.srchType!='EXACT') srchTimeout=window.setTimeout("srchTimeoutCallback('"+this.srch.srchFld.id+"');", this.srch.timeout); 
	return true;
};

edtSrch.prototype.sdBck=function() { if (this.visible) this.abort(); this.lastVal=''; }
edtSrch.prototype.sdEnter=function() {
	this.enter=1; if (document.selection) document.selection.empty();
	if (this.visible && !this.searching) {
		var p=this.srchPos; 
		var ap=(this.method=='srchList' && p!=-1) ? this.sd.childNodes[p].aPos : -1;
		if (p>-1 && ((ap==-1 && p <= this.resultCount) || (ap!=-1 && ap <= this.srchQuery.length))) {
			if (this.sd.childNodes[p].className.indexOf('disabled') < 0) {
				var v=(ap!=-1) ? this.srchQuery[ap].value : this.srchQuery.getField(p, 'SRCHVALUE');
				var t=(ap!=-1) ? this.srchQuery[ap].text : this.srchQuery.getField(p, 'SRCHTEXT');
				if (this.srchFld.value.length >= this.valMinLength) { this.sdSetInput(v, t); if (this.multiple) this.sdHide(); else this.finish(); } else { this.srchFld.value=v.replace(reNbsp, ''); this.search(); }
			}
		} else this.finish();
	} else { 
		if (!this.srchFld.value.length || this.srchFld.value!=this.lastVal) this.search(); else this.finish();
	} 
};
edtSrch.prototype.sdEsc=function() { if (this.visible) this.abort(); else { if (!this.suggest && this.startId.length) { this.srchFldId.value=this.startId; this.srchFld.value=this.startVal; } if (!this.srchFldId.value.length) this.srchFld.value=''; formTabPrev(this.srchFld); } }
edtSrch.prototype.sdPgUp=function() { if (this.visible && !this.searching && this.resultCount>0) { var s=Number(this.resultStart) - Number(this.resultTop); if (s >= 0) { this.resultStart=s; this.search(); } } };
edtSrch.prototype.sdPgDn=function() { if (this.visible && !this.searching && this.resultCount>0) { var s=Number(this.resultStart) + Number(this.resultTop); if (s-1 < this.resultCount) { this.resultStart=s; this.search(); } } };
edtSrch.prototype.sdUp=function() { if (this.visible && !this.searching) { var i=0, c=(this.resultTop < this.resultCount) ? this.resultTop : this.resultCount; this.srchPos--; if (this.srchPos < 0) this.srchPos=c-1; if (this.srchPos < 0) return false; while (i < 2 && this.sd.childNodes[this.srchPos].pos < 0) { this.srchPos--; if (this.srchPos < 0) { i++; this.srchPos=c-1; } } } this.sdSelect(this.srchPos); };
edtSrch.prototype.sdDn=function() { if (this.visible && !this.searching) { var i=0, c=(this.resultTop < this.resultCount) ? this.resultTop : this.resultCount; this.srchPos++; if (this.srchPos < 0) this.srchPos=0;  if (this.srchPos < 0) return false; while (i < 2 && this.sd.childNodes[this.srchPos].pos < 0) { this.srchPos++; if (this.srchPos >= c) { i++; this.srchPos=0; } } this.sdSelect(this.srchPos); } else { this.lastVal=''; if (!this.searching) { this.search(); } } };
edtSrch.prototype.sdDel=function() { if (this.multiple && !this.visible && !this.srchFldId.value.length) { if (this.multiEls.length) { var l=this.multiEls.length-1; var id=this.multiEls[l]; this.multiEls.splice(l, 1); this.multiChk(true); var d=document.getElementById(id); if (d) d.parentNode.parentNode.removeChild(d.parentNode); } } };
edtSrch.prototype.fldPaste=function() { if (this.srch.srchType!='EXACT') srchTimeout=window.setTimeout("srchTimeoutCallback('"+this.srch.srchFld.id+"');", 80); };
edtSrch.prototype.fldClick=function() { if (this.disabled || this.readOnly || this.srch.srchType=='EXACT') return false; if (this.srch.srchFld.value=='') { this.srch.lastVal=''; if (this.srch.srchFld.value=='') this.srch.search(); } return true; }; 
edtSrch.prototype.fldFocus=function() { if (!this.srch.suggest) { this.srch.srchFld.className=this.srch.srchFld.className.replace('srchInvalidId', ''); this.srch.lastVal=this.srch.srchFld.value; this.srch.startId=this.srch.srchFldId.value; this.srch.startVal=this.srch.srchFld.value; this.srch.resultStart=0; } };
edtSrch.prototype.fldBlur=function() { if ((!this.srch.enter && !this.srch.mouseOver) || (!this.srch.suggest && this.srch.srchFldId.value == '')) this.srch.srchPos=-1;	this.srch.fireOnSelect(); }
edtSrch.prototype.sdMouseOver=function() { this.srch.mouseOver=1; this.srch.srchPos=this.pos; this.srch.sdSelect(this.pos); };
edtSrch.prototype.sdMouseOut=function() { this.srch.mouseOver=0; this.srch.srchPos=-1; this.srch.sdSelect(-2); };
edtSrch.prototype.sdMouseDown=function() { if (this.pos>-1) { this.srch.srchPos=this.pos; this.srch.sdEnter(); } if (this.pos==-33) this.srch.sdPgUp(); if (this.pos==-34) this.srch.sdPgDn(); };
edtSrch.prototype.sdSelect=function(s) { 
	this.hdHide(); 
	if (this.sd.childNodes && s!=-1) { 
		var n;
		n=document.getElementById('SRCH_NAV_33'); if (n) { if (s==-33) n.className+=' selected'; else n.className=n.cls; } 
		n=document.getElementById('SRCH_NAV_34'); if (n) { if (s==-34) n.className+=' selected'; else n.className=n.cls; } 
		for (var i=0; i < this.sd.children.length; i++) { this.sd.childNodes[i].className=this.sd.childNodes[i].cls; if (i==s || this.sd.childNodes[i].pos==s) { this.sd.childNodes[i].className+=' selected'; this.hdShow(); } }
	} 
};

edtSrch.prototype.finish=function() { this.srchFld.blur(); /* fireOnSelect */ formTabNext(this.srchFld); }
edtSrch.prototype.fireOnSelect=function() { 
	this.abort(); 
	if (!this.suggest) { if (this.srchFld.value=='') this.srchFldId.value=''; }
	if (!this.srchFldId.value.length || this.startId!=this.srchFldId.value) { if (this.onSelect) { if (!this.multiple || this.multiple && this.multiPos != -1) this.onSelect(); } }
	if (!this.srchFldId.value.length) { if (this.clearId) this.srchFld.value=''; else this.srchFld.className+=' srchInvalidId'; }
	if (this.startId!=this.srchFldId.value) { fireEvent(this.srchFldId, 'change'); fireEvent(this.srchFld, 'change'); } 
}; 

edtSrch.prototype.sdShow=function() { 
	if (!this.visible) {
		var t=getPageOffsetTop(this.srchFld) + this.srchFld.offsetHeight; var l=getPageOffsetLeft(this.srchFld)-2; //var w=parseInt(this.srchFld.offsetWidth);
		var s=this.sd.style; s.top=t+'px'; s.left=l+'px'; s.display='block'; s.background='white';
		if (ieV() < 7) { // IE hack, ker je butast in ne zna narisat diva cez select
			var i=document.getElementById("DIV_SRCH_IFRAME");
			if (i==null) { i=document.createElement('IFRAME'); i.id="DIV_SRCH_IFRAME"; i.setAttribute('src', ""); i.setAttribute('frameBorder', 'no'); i.setAttribute('scrolling', 'no'); document.body.appendChild(i); }
			s=i.style; s.display='block'; s.position='absolute'; s.top=t+2+'px'; s.left=l+2+'px'; s.width=this.sd.offsetWidth+'px'; s.height=this.sd.offsetHeight+'px'; s.zIndex=this.sd.style.zIndex - 1;
		}
	}
	this.visible=true;
};
edtSrch.prototype.sdHide=function() { var i=document.getElementById("DIV_SRCH_IFRAME"); if (i) { i.style.width="1px"; i.style.height="1px"; i.style.display='none'; } this.sd.style.display='none'; this.sd.innerHTML=''; this.visible=false; this.hdHide(); if (!this.suggest) this.lastVal=''; };
edtSrch.prototype.sdSetInput=function(v,t) { if (this.suggest) { this.lastVal=this.srchFld.value=v; } else { if (typeof v!='undefined' && (v!='' || v=='0')) { this.srchFldId.value=v; t+=''; t=t.replace(reNbsp, ''); this.lastVal=this.srchFld.value=t; if (this.multiple) this.newInput(v, this.srchFld.value, true); } else { this.srchPos=-1; this.srchFld.value=''; this.srchFldId.value=''; } } }; 
edtSrch.prototype.newHintItem=function(src,i) { var d=document.createElement('DIV'); d.innerHTML=src; d.hintIndex=i; d.srch=this; d.className='info'; d.style.display='none'; this.hd.appendChild(d); };
edtSrch.prototype.hdShow=function() {
	if (this.srchPos!=-1 && this.hd.childNodes.length && this.hd.childNodes[this.srchPos].innerHTML!='') {
		var d=this.sd.childNodes[this.srchPos]; var t=getPageOffsetTop(d) + d.offsetHeight + this.hoY; var l=getPageOffsetLeft(this.srchFld)-2 + this.hoX; var s=this.hd.style; s.top=t+'px'; s.left=l+'px'; s.display='block'; this.hd.childNodes[this.srchPos].style.display='block';
		if (this.hdif) { s=this.hdif.style; s.display='block'; s.position='absolute'; s.top=t+2+'px'; s.left=l+2+'px'; s.width=this.hd.offsetWidth+1+'px'; s.height=this.hd.offsetHeight+1+'px'; s.zIndex=this.hd.style.zIndex - 1; }
	}
};
edtSrch.prototype.hdHide=function() { if (this.hdif) this.hdif.style.display='none'; this.hd.style.display='none'; for (var i=0; i < this.hd.children.length; i++) { this.hd.childNodes[i].style.display='none'; } };

// --- -------------------------------------------------------------------- ---
function getPageOffsetLeft(el) { var l=el.offsetLeft; while ((el=el.offsetParent)!=null) { l+=el.offsetLeft; } return l; }
function getPageOffsetTop(el) { var t=el.offsetTop; while ((el=el.offsetParent)!=null) { t+=el.offsetTop; } return t; }

function toggleMultiSearch(id, enable, validate) { if (typeof enable == 'undefined') enable = false; if (typeof validate == 'undefined') validate = ''; var el=document.getElementById(id); if (el) { if (enable) { setObjValidation(el.id, '', 'readOnly search', true, '', ''); } else { setObjValidation(el.id, validate, 'search', false, '*', ''); } }}