
AutoSuggest=function(obj,url,frm){
	this.values=new Array;
	
	this.DOMObject=document.getElementById(obj);
	this.DOMObject.autoComplete="off";
	this.DOMObject.setAttribute("autocomplete","off");

	this.menuActive=false;

	this.xml=new XMLReader();
	this.xml.setHandler(4,this.method(this.initialise));

	this.xml.load(url,true,false);
}

AutoSuggest.prototype.initialise=function(){

	// cut up values
	if(!this.xml.xmlhttp.responseXML) return;
	this.loaded=1;

	var u=this.xml.xmlhttp.responseXML.getElementsByTagName("value");
	for(i=0;i<u.length;i++) {
		var val=u[i].childNodes[0].nodeValue;
		var firstChar=val.charAt(0).toLowerCase();
		if(!this.values[firstChar]){
			this.values[firstChar]=new Array();
		}
		this.values[firstChar].push(val);
	}		


	// prep text field
	var self=this;
	this.DOMObject.autocomplete=false;
	this.DOMObject.onfocus=
	this.DOMObject.onkeyup=function(e){
		self.checkMatch(e);
	}
	this.DOMObject.onblur=function(e){
		self.onBlur(e);	
	}

	// prep menu
	this.menu=document.createElement("select");
	this.menu.size=4;
	this.menu.multiple=false;
	this.menu.className="menu";
	this.menu.style.width=this.DOMObject.offsetWidth+"px";
	this.menu.onfocus=
	this.menu.onchange=function(e){
		self.menuActive=true;
		self.passChoice(this.value);
	}
	this.menu.onblur=
	this.menu.onblur=function(e){
		self.setFieldFocus();
		self.closeMenu(false);
	}
	this.menu.onmousedown=function(e){
		self.menuActive=true;
	}
	this.menu.onclick=function(e){
		if(getSourceElement(e).nodeName!="OPTION" && !window.event) return;
		self.setFieldFocus();
		self.closeMenu(true);
	}
	this.menu.onkeyup=function(e){
		//TAB=9, KEYUP=38, KEYDN=40, ENTER=13, ESC=27, KEYPRESS=32?
		key=e ? e.keyCode : (window.event ? window.event.keyCode : null);
		if(key==32 || key==13 || key==27){
			self.setFieldFocus();
			self.closeMenu(true);
			return false;
		} 
	}
	
	document.getElementById("container_balk").appendChild(this.menu);
	if(document.all){
		this.frame=document.createElement("iframe");
		this.frame.className="frame";
		this.frame.width=this.frame.height="1";
		this.frame.style.width=this.menu.offsetWidth+"px";
		this.frame.style.height=this.menu.offsetHeight+"px";
//		this.frame.style.left=calculateLeft(this.DOMObject)+"px";
		this.frame.style.left="400px";
		this.frame.style.top=calculateTop(this.DOMObject)+"px";
		document.getElementById("container_balk").appendChild(this.frame);
	}
}

AutoSuggest.prototype.destroy=function(){
	if(this.loaded) {
		this.DOMObject.autocomplete=
		this.DOMObject.onfocus=
		this.DOMObject.onkeyup=
		this.DOMObject.onblur=
		this.menu.onfocus=
		this.menu.onblur=
		this.menu.onmousedown=
		this.menu.onmouseup=null;
	}
}

AutoSuggest.prototype.checkMatch=function(e){
	//TAB=9, KEYUP=38, KEYDN=40, ESC=27
	key=e ? e.keyCode : (window.event ? window.event.keyCode : null);
	if(key==0 || key==9) return;
	if((key==38 || key==40) && this.menu.className.match("show")) {
		this.menuActive=true;
		this.menu.focus();
		return false;
	}
	if(key==27){
		this.setFieldFocus();
		this.closeMenu(true);
		return false;
	}
	this.value=this.DOMObject.value.toLowerCase();
	
	// create matches array
	var inp=this.value.toLowerCase();
	var firstChar=inp.charAt(0);
	var charValues=this.values[firstChar]; 
	this.matches=new Array;
	for(var i=0;charValues&&i<charValues.length;i++){
		var txt=charValues[i].toLowerCase();
		if(txt.indexOf(inp)==0) this.matches[this.matches.length]=charValues[i];
	}
	this.renderMatches();
}

AutoSuggest.prototype.onBlur=function(e){
	if(!this.menuActive)
	this.closeMenu(false);

	return false;
}

AutoSuggest.prototype.renderMatches=function(){
	var len=this.matches.length;
	
	if (len == 1) {
		this.menu.style.height="30px";
	} else if (len > 0 && len < 8) {
		this.menu.style.height=(16*len)+"px";		
	} else {
		this.menu.style.height="111px";
	} 

	if(len==0 || this.value.length<1) {
		removeClass(this.menu,"show");
		if(this.frame) removeClass(this.frame,"show");
		return;
	}

//	this.menu.style.left=parseInt(calculateLeft(this.DOMObject))-setLeft+"px";
//	this.menu.style.top=parseInt(calculateTop(this.DOMObject))-setTop+"px";
	this.menu.style.left=parseInt(calculateLeft(this.DOMObject))+"px";
	this.menu.style.top=parseInt(calculateTop(this.DOMObject))+"px";
	this.menu.style.width=this.DOMObject.offsetWidth+"px";
	addClass(this.menu,"show");
	if(this.frame) addClass(this.frame,"show");
	this.menu.innerHTML="";
	// fill menu with matches

	for(var i=0;i<len;i++){
		var opt=document.createElement("option");
		opt.value=opt.innerHTML=this.matches[i];
		if(i==0) opt.selected=true;
		this.menu.appendChild(opt);
	}
}

AutoSuggest.prototype.passChoice=function(val){
	this.DOMObject.value=val;	
}

AutoSuggest.prototype.closeMenu=function(status){

	this.menuActive=false;
	removeClass(this.menu,"show");
	if(this.frame) removeClass(this.frame,"show");

	if (status)
	submitForm(this.DOMObject);

}

AutoSuggest.prototype.setFieldFocus=function(){
	this.menu.blur();
	this.DOMObject.focus();	
	this.DOMObject.select();
}

addClass=function(obj,cName) { 
	removeClass(obj,cName); 
	return obj.className+=(obj.className.length>0?' ':'')+cName; 
}

removeClass=function(obj,cName) {
	return obj.className=obj.className.replace(new RegExp("^"+cName+"\\b\\s*|\\s*\\b"+cName+"\\b",'g'),''); 
}

getSourceElement=function(e){
	if(e && e.target) return e.target;
	if(window.event && window.event.srcElement) return window.event.srcElement;
}

calculateLeft=function(object) {
	if (object) return object.offsetLeft + calculateLeft(object.offsetParent); 
	else return 0;
}

calculateTop=function(object) {
	if (object) return object.offsetTop + calculateTop(object.offsetParent); 
	else return 0;
}

Object.prototype.method=function(method) {
	var context=this;
	return function(){
		method.apply(context,arguments);
	}
}

submitForm = function(obj) {

	var fl = document.forms.length;
	
	for (var n = 0; n < fl; n++) {
		var len = document.forms[n].elements.length;
		
		for (var i = 0; i < len; i++) {
			if (document.forms[n].elements[i].id == obj.id) {
			 var frm = n;
			 var elm = i-1;
			}
		}
	}
	
	var frmVal = document.forms[frm].elements[elm].value;
	
	if(frmVal!='' && obj.id =='location') {
		if (document.forms[frm].houdwaar) {
			document.forms[frm].houdwaar.value = 'false';
		}
		document.forms[frm].submit();
	} 
}			

/********* XML reader **********/
var XML_LOADING       = 1;
var XML_LOADED        = 2;
var XML_INTERACTIVE   = 3;
var XML_COMPLETE      = 4;
function XMLReader() {
	this.xmlhttp = null;

	if (window.XMLHttpRequest) {
		this.xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
	}

	if (this.xmlhttp == null) {
		return null;
	}
	this.handlers = new Array(5);
}
XMLReader.prototype.load = function (URL, async, asText) {

	this.asText = (asText) ? true : false;
	this.xmlhttp.open("GET", URL, async);
	var self = this;
	if (async) {
		this.xmlhttp.onreadystatechange = function() {
			self.callHandler(self);
		};
		this.xmlhttp.send(null);
	} else {
		this.xmlhttp.send(null);
		if (asText) {
			return this.xmlhttp.responseText;
		} else {
			return this.xmlhttp.responseXML;
		}
	}
}
XMLReader.prototype.callHandler = function (self) {
	if (self.handlers[self.xmlhttp.readyState]) {
		if (self.xmlhttp.readyState == XML_COMPLETE) {
			self.handlers[self.xmlhttp.readyState](self.xmlhttp.responseXML);
		} else {
			self.handlers[self.xmlhttp.readyState]();
		}
	}
}
XMLReader.prototype.setHandler = function (state, pointer) {
	this.handlers[state] = pointer;
}
XMLReader.prototype.clearHandlers = function () {
	for (var i=0; i < this.handlers.length; i++) this.handlers[i] = null;
}
