function AutoSuggestControl(oTextbox, oProvider) {
	this.cur = -1;
	this.layer = null;
	this.provider = oProvider;
	this.textbox = oTextbox;
	this.timeoutId = null;
	this.userText = oTextbox.value;
	this.init();
}

function SuggestionProvider() {
	this.http = createXMLHttp();
}

function createXMLHttp() {
	if (typeof XMLHttpRequest != "undefined") {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		var aVersions = [ "MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp","Microsoft.XMLHttp"];
		for (var i = 0; i < aVersions.length; i++) {
			try {
				var oXmlHttp = new ActiveXObject(aVersions[i]);
				return oXmlHttp;
			} catch (oError) {

			}
		}
	}
	throw new Error("XMLHttp object could not be created.");
}

SuggestionProvider.prototype.requestSuggestions = function(oAutoSuggestControl, bTypeAhead) {
	var oHttp = this.http;
	if (oHttp.readyState != 0) {
		oHttp.abort();
	}
	var params = "adres="+encodeURIComponent(oAutoSuggestControl.userText);
	oHttp.open("get", "adressen.mpl?" + params, true);
	oHttp.onreadystatechange = function() {
		if (oHttp.readyState == 4) {
			var msg = oHttp.responseText;
			if (msg && msg.length>0) {
				var data = eval('(' + msg + ')');
				if (data.type == "adressen") {
					oAutoSuggestControl.autosuggest(data.adressen, bTypeAhead);
				}
			}
		}
	};
	oHttp.send(null);
}



AutoSuggestControl.prototype.goToSuggestion = function(iDiff) {
	var cSuggestionNodes = this.layer.childNodes;
	if (cSuggestionNodes.length > 0) {
		var oNode = null;
		if (iDiff > 0) {
			if (this.cur < cSuggestionNodes.length-1) {
				oNode = cSuggestionNodes[++this.cur];
			}
		} else {
			if (this.cur > 0) {
				oNode = cSuggestionNodes[--this.cur];
			}
		}
		if (oNode) {
			this.highlightSuggestion(oNode);
			this.textbox.value = oNode.firstChild.nodeValue;
		}
	}
}
AutoSuggestControl.prototype.hideSuggestions = function() {
	this.layer.style.visibility = "hidden";
}
AutoSuggestControl.prototype.highlightSuggestion = function(oSuggestionNode) {
	for (var i = 0; i < this.layer.childNodes.length; i++) {
		var oNode = this.layer.childNodes[i];
		oNode.className = (oNode == oSuggestionNode) ? "current" : "";
	}
}
AutoSuggestControl.prototype.createHint = function(m) {
	this.hint = document.createElement("div");
	this.hint.className = "hint";
	this.hint.innerHTML = m;
	this.hint.style.left = this.getLeft() + "px";
	this.hint.style.top = this.getTop() + "px";
	this.hint.onclick = function(oEvent) {
		oEvent = oEvent || window.event;
		oTarget = oEvent.target || oEvent.srcElement;
		oThis.hideHint();
		oThis.textbox.focus();
	}
	document.body.appendChild(this.hint);
	this.hint.style.display = (this.textbox.value.length == 0) ? '' : 'none';
}
AutoSuggestControl.prototype.showHint = function() {
	this.hint.style.display = '';
}
AutoSuggestControl.prototype.hideHint = function() {
	this.hint.style.display = 'none';
}
AutoSuggestControl.prototype.createDropDown = function() {
	this.layer = document.createElement("div");
	this.layer.className = "suggestions";
	this.layer.style.visibility = "hidden";
	this.layer.style.width = this.textbox.offsetWidth + "px";
	document.body.appendChild(this.layer);
	var oThis = this;
	this.layer.onmousedown = this.layer.onmouseup = this.layer.onmouseover = function(oEvent) {
		oEvent = oEvent || window.event;
		oTarget = oEvent.target || oEvent.srcElement;
		if (oEvent.type == "mousedown") {
			oThis.textbox.value = oTarget.firstChild.nodeValue;
			oThis.hideSuggestions();
		} else if (oEvent.type == "mouseover") {
			oThis.highlightSuggestion(oTarget);
		} else {
			oThis.textbox.focus();
		}
	}
}
AutoSuggestControl.prototype.getLeft = function() {
	var oNode = this.textbox;
	var iLeft = 0;
	while (oNode && oNode.tagName != "BODY") {
		iLeft += oNode.offsetLeft;
		oNode = oNode.offsetParent;
	}
	return iLeft;
}
AutoSuggestControl.prototype.getTop = function() {
	var oNode = this.textbox;
	var iTop = 0;
	while (oNode && oNode.tagName != "BODY") {
		iTop += oNode.offsetTop;
		oNode = oNode.offsetParent;
	}
	return iTop;
}
AutoSuggestControl.prototype.showSuggestions = function(aSuggestions) {
	var oDiv = null;
	this.layer.innerHTML = '';
	for (var i = 0; i < aSuggestions.length; i++) {
		oDiv = document.createElement("div");
		oDiv.appendChild(document.createTextNode(aSuggestions[i].straat));
		this.layer.appendChild(oDiv);
	}
	this.layer.style.left = this.getLeft() + "px";
	this.layer.style.top = (this.getTop() + this.textbox.offsetHeight) + "px";
	this.layer.style.visibility = "visible";
}
AutoSuggestControl.prototype.selectRange = function(iStart, iEnd) {
	if (this.textbox.createTextRange) {
		var oRange = this.textbox.createTextRange();
		oRange.moveStart("character", iStart);
		oRange.moveEnd("character", iEnd - this.textbox.value.length);
		oRange.select();
	} else if (this.textbox.setSelectionRange) {
		this.textbox.setSelectionRange(iStart, iEnd);
	}
	this.textbox.focus();
}
AutoSuggestControl.prototype.typeAhead = function(sSuggestion) {
	if (this.textbox.createTextRange || this.textbox.setSelectionRange) {
		var iLen = this.textbox.value.length;
		this.textbox.value = sSuggestion;
		this.selectRange(iLen, sSuggestion.length);
	}
}
AutoSuggestControl.prototype.autosuggest = function(aSuggestions, bTypeAhead) {
	this.cur = -1;
	if (aSuggestions && aSuggestions.length>0) {
		if (bTypeAhead) {
			var c = this.textbox.value;
			var s = -1;
			for (var i = 0; i < aSuggestions.length; i++) {
				if (aSuggestions[i].straat.indexOf(c) == 0) {
					s = i;
					break;
				}
			}
			if (s > -1) this.typeAhead(aSuggestions[s].straat);
		}
		this.showSuggestions(aSuggestions);
	} else {
		this.hideSuggestions();
	}
}
AutoSuggestControl.prototype.handleKeyUp = function(oEvent) {
	var iKeyCode = oEvent.keyCode;
	var oThis = this;
	this.userText = this.textbox.value;
	clearTimeout(this.timeoutId);
	if (iKeyCode == 8 || iKeyCode == 46) {
		this.timeoutId = setTimeout( function() {
			oThis.provider.requestSuggestions(oThis, false);
		}, 250);
		this.provider.requestSuggestions(this, false);
	} else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
	
	} else {
		this.timeoutId = setTimeout( function() {
			oThis.provider.requestSuggestions(oThis, false);
		}, 250);
		this.provider.requestSuggestions(this, true);
	}
}
AutoSuggestControl.prototype.handleKeyDown = function(oEvent) {
	switch(oEvent.keyCode) {
		case 38:
			this.goToSuggestion(-1);
			break;
		case 40:
			this.goToSuggestion(1);
			break;
		case 27:
			this.textbox.value = this.userText;
			this.selectRange(this.userText.length, 0);
		case 13:
			this.hideSuggestions();
			break;
	}
}
AutoSuggestControl.prototype.init = function() {
	oThis = this;
	this.textbox.onkeyup = function(oEvent) {
		oEvent = oEvent || window.event;
		oThis.handleKeyUp(oEvent);
	}
	this.textbox.onkeydown = function(oEvent) {
		oEvent = oEvent || window.event;
		oThis.handleKeyDown(oEvent);
	}
	this.textbox.onfocus = function() {
		oThis.hideHint();
	}
	this.textbox.onblur = function() {
		oThis.hideSuggestions();
		if (oThis.textbox.value.length == 0) oThis.showHint();
	}
	this.createDropDown();
	this.createHint("bijv. 1012js of wibautstraat");
}

