var droppers=new Array();


function TDropFinder(tablename,fieldname,defaultvalue,filterfieldname,width){
	this.data=null;
	this.savedhandler=null;
	this.tablename=tablename;
	this.fieldname=fieldname;
	this.defaultvalue=defaultvalue;
	this.width=width||300;
  this.filterfieldname=filterfieldname;
  this.onchange=null;
  this.allowsubmit=false;
  
  this.timer=null;
	
	this.index=droppers.length;
	droppers[droppers.length]=this;
  this.name="dropper_"+this.index;
  this.layername="l_"+this.name;
  
	this.draw=function(){
	  var s=new String(this.defaultvalue);
    if(s.indexOf(',')>0)a=s.split(",");
    else a=[s,''];	
    
    document.write("<input type=hidden name="+this.fieldname+" value='"+a[0]+"'>");	
    document.write("<input type=text name="+this.name+" value='"+a[1]+"' size=1 style='width:"+this.width+"px;height:16px;border:1px solid black;margin:0 0 0 0px;font : normal normal 9px/13px  \"MS Sans Serif\",sans-serif;' "+
      "onkeydown=\"return droppers["+this.index+"].keydown()\"><img align=center src=pics/combo_select.gif width=15 height=16 border=0 name=combobutton style='cursor:hand;width:15px;height:16px;' "+
      "onClick=droppers["+this.index+"].showdropdown(true)>");
    document.write("<div class=dropdown id="+this.layername+" name="+this.layername+" style='position:absolute;overflow-y:auto;width:400px;margin:0 0 0 0px;height:200px;z-index:999;visibility:hidden;' "+
      "onMouseDown=droppers["+this.index+"].mousedown()></div>");
      
    if((s!='')&&(s!='0')&&(s.indexOf(',')<0)){
			this.getdata("GET","","droppers["+this.index+"].setpreset","droppers["+this.index+"].data",this.tablename+".ID="+s);      
    }
	}

	this.drawlayer=function (a){
		s="";
		for(var i=0;i<a.length;i++){
			s+="<tr><td NOWRAP class=dropdownitem "+
			  "onMouseOver=droppers["+this.index+"].mouseover(this) "+
			  "onMouseDown=droppers["+this.index+"].enter(this)>"+a[i][1]+"</td></tr>";
		}
		layerWrite(this.layername,"<table cellpadding=0 cellspacing=0>"+s+"</table>");		
	}

	this.selecteditem=function (){
		var l=layerGet(this.layername);
		while(l){
			if(l.children.length>0){
				p=l.firstChild.firstChild.firstChild;
				while(p){
					if(p.firstChild.className=="dropdownitemover")return p;
					p=p.nextSibling;
				}
				return null;
			}
			l=l.nextSibling;
		}
		return null;
	}

	this.selectedindex=function(){
		var l=layerGet(this.layername);
		var i=0;
		while(l){
			if(l.children.length>0){
				p=l.firstChild.firstChild.firstChild;
				while(p){
					if(p.firstChild.className=="dropdownitemover")return i;
					i++;
					p=p.nextSibling;
				}
				return -1;
			}
			l=l.nextSibling;
		}
		return -1;
	}

	this.setpreset=function(a){
		document.all[this.name].value=this.data[0][1];
		document.all[this.fieldname].value=this.data[0][0];		
		this.hidedropdown();
    this.performonchange();
		return true;

	}
	
	this.getdata=function(watte,id,func,varname,voorwaarde){
		if(window.ActiveXObject)var http = new ActiveXObject("Microsoft.XMLHTTP");
		if(window.XMLHttpRequest)var http = new XMLHttpRequest();

		var url="returndropdata.php?tablename="+this.tablename+"&fieldname="+this.fieldname+"&filterfieldname="+this.filterfieldname+"&watte="+watte+"&ID="+id;

		if((voorwaarde)&&(voorwaarde!="")) url=url+"&voorwaarde="+escape(voorwaarde);

			
		GetDataExtra="";
		http.onreadystatechange = function () {
			if(http.readyState==4){

			
  			if(varname!="")eval(varname+"="+http.responseText+";");
				if(func!="")eval(func+"("+http.responseText+");");
			}
		}
		http.open("GET",url,true);
		http.send(null);
	}

	this.hilite=function(inc,e){
		var l=layerGet(this.layername);
		while(l){
			if(l.children.length>0){
				p=l.firstChild.firstChild.firstChild;
				while(p){
					if(p.firstChild.className=="dropdownitemover"){
						var h=Math.floor(l.offsetHeight/p.firstChild.clientHeight)-1;
						if(inc>0){
							if(inc==100)inc=h;
							q=p;
							while((q.nextSibling)&&(inc>0)){q=q.nextSibling;inc--;}
						}
						else {
							if(inc==-100)inc=-h;
							q=p;
							while((q.previousSibling)&&(inc<0)){q=q.previousSibling;inc++;}

						}
						if(q){
							p.firstChild.className="dropdownitem";
							q.firstChild.className="dropdownitemover";
							this.adjust(q.firstChild);
						}
						return false;
					}
					p=p.nextSibling;
				}
				if(l.firstChild)
				if(l.firstChild.firstChild)
				if(l.firstChild.firstChild.firstChild){
	  			l.firstChild.firstChild.firstChild.firstChild.className="dropdownitemover";
  				this.adjust(l.firstChild.firstChild.firstChild.firstChild);
				}
				return false;
			}
			l=l.nextSibling;
		}
		return false;
	}

	this.adjust=function(obj){
		var l=layerGet(this.layername);
		while(obj.offsetTop<l.scrollTop)l.scrollTop-=(obj.clientHeight);
		while(obj.offsetTop+obj.clientHeight>(l.scrollTop+l.offsetHeight))l.scrollTop+=(obj.clientHeight);
	}

	this.showdropdown=function(laadlijst){
		if(!this.savedhandler){
			this.savedhandler=document.onmousedown;
			document.onmousedown=this.hidealldropdowns;
		}
	
		var l=layerGet(this.layername);
		layerMoveTo(this.layername,
		  findPosX(document.all[this.name]),
  		findPosY(document.all[this.name])+document.all[this.name].offsetHeight);
		l.onkeydown="return droppers["+this.index+"].keydown()";
		l.style.visibility="visible";
		if(laadlijst){
			var key=document.all[this.name].value||"%";
			this.getdata("GET",key,"droppers["+this.index+"].drawlayer","droppers["+this.index+"].data");
			document.all[this.name].focus();
		}			

//		HideControls(document.all[this.name]);
	}
	
	this.hidealldropdowns=function(){
	  for(var i=0;i<droppers.length;i++) droppers[i].hidedropdown();
	}	

	this.hidedropdown=function(){
		var l=layerGet(this.layername);
		l.style.visibility="hidden";
		l.onkeydown="";
		if(this.savedhandler){
			document.onmousedown=this.savedhandler;
			this.savedhandler=null;
		}
//		ShowControls(document.all[this.name]);		
	}

  this.mouseover=function(obj){
		var l=layerGet(this.layername);
		while(l){
			if(l.children.length>0){
				p=l.firstChild.firstChild.firstChild;
				while(p){
					if(p.firstChild.className=="dropdownitemover")p.firstChild.className="dropdownitem";
					p=p.nextSibling;
				}
				obj.className='dropdownitemover';
				this.adjust(obj);
				return false;
			}
			l=l.nextSibling;
		}
		return false;
	}

	this.enter=function(obj){
		if(!this.data)return false;
		i=this.selectedindex();
		if(i<0)return false;
		document.all[this.name].value=this.data[i][1];
		document.all[this.fieldname].value=this.data[i][0];		
//		document.all["ID"].value=data[i][0];		
		this.hidedropdown();
		
    this.performonchange(obj);
		return true;
	}

	this.performonchange=function(obj){
		if(this.onchange){
		  if(typeof(this.onchange)=="function") this.onchange(this);
      else eval(this.onchange);
      /*  
		  else if(this.onchange.indexOf("(")>0) eval(this.onchange);
      else this.onchange(this);
      */
		}  

//		document.all[this.fieldname].
		if(this.allowsubmit){
		
//		  if(obj)debug(obj);//.form.submit();
//		  else 
        document.all[this.name].form.submit();
//        document.all[this.fieldname].form.submit();
		}  
	}
	
	this.mousedown=function(e){
		if(!e) e = window.event;
		var i=e.keyCode ? e.keyCode : e.which;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		return false;
	}
	
	this.schedulegetdata=function(key){
	  clearTimeout(this.timer);
  	this.getdata("GET",key,"droppers["+this.index+"].drawlayer","droppers["+this.index+"].data");
	}

	this.keydown=function(e){
		if(!e) var e = window.event;
		var i=e.keyCode ? e.keyCode : e.which;
		var ch=unescape('%'+i.toString(16));
		var key=document.all[this.name].value;
		if(((ch>='A')&&(ch<='Z'))||((ch>='0')&&(ch<='9'))){
			key+=ch;
			if(this.timer)clearTimeout(this.timer);
			this.timer=setTimeout("droppers["+this.index+"].schedulegetdata('"+key+"')",1000);
//			this.getdata("GET",key,"droppers["+this.index+"].drawlayer","data");
		}
		else if(i==8){
			key=key.substring(0,key.length-1);
			if(this.timer)clearTimeout(this.timer);
			this.timer=setTimeout("droppers["+this.index+"].schedulegetdata('"+key+"')",1000);
//			this.getdata("GET",key,"droppers["+this.index+"].drawlayer","data");
		}
		else if(i==13){
		  if(this.data) this.enter();
		  else this.schedulegetdata(key);
		  return false;
		} //ENTER
		else if(i==27){this.hidedropdown();return false;} //CANCEL

		else if(i==33)this.hilite(-100,e); //PGUP
		else if(i==34)this.hilite(100,e); //PGDN

		else if(i==35)this.hilite(Number.POSITIVE_INFINITY,e); //END
		else if(i==36)this.hilite(Number.NEGATIVE_INFINITY,e); //HOME

		else if(i==37)this.hilite(-1,e); //LT
		else if(i==38)this.hilite(-1,e); //UP
		else if(i==39)this.hilite(1,e); //RT
		else if(i==40)this.hilite(1,e); //DN
		else return;
		document.all[this.name].value=key;
		this.showdropdown();
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		return false;
	}
	
	this.getvalue=function(){
  	return document.all[this.fieldname].value;
	}	
	
  this.draw();	

}


