function form2suburbinit(ele) {
	this.timeout=false;
	this.input=ele;
	this.input.obj=this;
	this.input.show=false;
	this.input.popup=false;
	this.input.output=document.getElementById(this.input.id+'_out');
	this.input.oldtext=this.input.value;
	this.input.onkeypress=this.updateinput;
	this.input.onkeydown=this.scrollpopup;
	this.input.onkeyup=this.checkclear;
	this.input.onfocus=function() { this.focussed=true; }
	this.input.onblur=this.finalize;
	this.input.updatepopup = this.updatepopup;
	this.input.updatehighlight = this.updatehighlight;
	this.initpopup();
}
form2suburbinit.prototype={
finalize:function() {
	var input=this;
	this.obj.hidesearchpopup();
	this.focussed=false;
	if (!this.output.value) {
		$.getJSON("/form2suburb.nb?s="+this.value,function(data) {
			if(!data) {
				input.value='';
			} else {
				input.value=data[0][1];
				input.output.value=data[0][0];
			}
			input.obj.hidesearchpopup();
		});
	}
},
cancelbubble:function(e) {
	if (!e) e = window.event;
	if (e.stopPropagation) e.stopPropagation();
	e.cancelBubble = true;
	e.returnValue = false;
	return false;
},
initpopup:function() {
	this.input.popup = document.createElement('div');
	this.input.popup.style.width = (this.input.offsetWidth - 4) + 'px';
	this.input.popup.style.left = $(this.input).offset().left + 'px';
	this.input.popup.style.visibility = 'hidden';
	this.input.popup.className='suburbpopup';
	this.input.popup.input = this.input;
	this.input.popup.onmousemove = this.checkmouseupdate;
	document.body.appendChild(this.input.popup);
},
checkmouseupdate:function(e) {
	if (document.all) e = window.event;
	var y = e.pageY?e.pageY:e.clientY + document.body.scrollTop;
	var newtest = Math.floor((y - this.offsetTop - 2) / 18) + 1;
	if ((newtest > this.input.scrollcount) || (newtest < 1)) {
		return;
	}
	if (newtest != this.input.scrollindex) {
		this.input.scrollindex = newtest;
		this.input.updatehighlight();
	}
},
updateinput:function(e) {
	if (document.all) e = window.event;
	if (e.keyCode == 13) {
		this.popup.style.visibility='hidden';
		return this.obj.cancelbubble(e);
	}
	if (document.all || e.charCode) {
		this.obj.updatesoon();
	}
},
scrollpopup:function(e) {
	if (document.all) e = window.event;

	if (this.popup && (this.popup.style.visibility == 'visible')) {
		if (e.keyCode == 38) {
			this.scrollindex -= 1;
			if (this.scrollindex < 1) {
				this.scrollindex = this.scrollcount;
			}
			this.updatehighlight();
		} else if (e.keyCode == 40) {
			this.scrollindex += 1;
			if (this.scrollindex > (this.scrollcount)) {
				this.scrollindex = 1;
			}
			this.updatehighlight();
		} else if (e.keyCode == 9) {
			this.popup.style.visibility = 'hidden';
		} else if ((e.keyCode == 37) || (e.keyCode == 39)) {
			return;
		} else if (e.keyCode == 13) {
			this.popup.style.visibility='hidden';
			return this.obj.cancelbubble(e);
		} else {
			this.obj.updatesoon();
			return;
		}
	}
},
checkclear:function(e) {
	if (document.all) e = window.event;
	
	if (this.popup.style.visibility == 'visible') {
		if ((e.keyCode != 38) && (e.keyCode != 40)) {
			this.obj.updatesoon();
		}
	}
},
hidesearchpopup:function() {
	this.input.popup.style.visibility = 'hidden';
},
updatehighlight:function() {
	var divs = this.popup.getElementsByTagName('div');
	if (this.oldhighlight != null) {
		this.oldhighlight.style.background = 'buttonface';
		this.oldhighlight.style.color = 'buttontext';
	}
	divs[this.scrollindex - 1].style.background='highlight';
	divs[this.scrollindex - 1].style.color='highlighttext';
	this.oldhighlight = divs[this.scrollindex - 1];
	this.value = divs[this.scrollindex - 1].innerHTML;
	this.output.value=divs[this.scrollindex-1].getAttribute('sid');
},
updatesoon:function() {
	var obj=this;
	this.input.output.value='';
	if (this.timeout) {
		clearTimeout(this.timeout);
	}
	this.timeout=setTimeout(function(){
		obj.input.updatepopup();
	}, 100);
},
updatepopup:function() {
	if (this.value.length >= 2) {
		if (this.value == this.oldtext) {
			if (this.show) {
				this.popup.style.visibility='visible';
			}
		} else {
			var input = this;
			this.oldtext = this.value;

			$.ajax({
				url:"/form2suburb.nb?s="+this.value,
				dataType:'json',
				timeout:5000,
				success:function(data,stat) {
					if(input.focussed) {
						if(!data) {
							input.popup.style.visibility='hidden';
							input.show=false;
						} else {
							input.show = true;
							var x,div;
							input.scrollindex = 0;
							input.scrollcount = data.length;
							input.popup.innerHTML='';

							$.each(data, function(i,item) {
								$('<div>'+item[1]+'</div>').attr('title',item[1]).attr('sid',item[0]).appendTo(input.popup);
							});

							input.lasthighlight=null;
							input.popup.style.top=($(input).offset().top-input.popup.offsetHeight)+'px';
							input.popup.style.visibility='visible';
							input.oldhighlight=null;
						}
					}
				}
			});
		}
	} else {
		this.popup.style.visibility='hidden';
	}
},
run:function() {
}
}

