function autoComplete(functionname, targetElement) {
	var val = $F(targetElement);
	var target = targetElement.identify();
	
	if (val.length > 0) {
		dwr.engine.setErrorHandler(errorHandlerFn);
		functionname(val, function(data) {
			if (data != null && typeof data == 'object') {
				onAutoComplete(data, target);
			}
		});
	}
}

function errorHandlerFn(message, exception) {
	// alert(dwr.util.toDescriptiveString(exception, 3));
	// alert("Access is denied"); // simple error, we don't seem to get much more back
}

function onAutoComplete(data, target)  {
	// alert(dwr.util.toDescriptiveString(data, 3));
	
	var suggestBoxHTML = "<ul>";
	for (var i = 0; i < data.length; i++){
		suggestBoxHTML += "<li><a href=\"#\" onClick=\"return onAutoCompleteChosen(this.innerHTML, '" + target + "');\">" + data[i].name + "</a></li>";
	}
	suggestBoxHTML += "</ul>";
	
	$("autoComplete_" + target).show();
    Element.update("autoComplete_" + target, suggestBoxHTML);
}

function onAutoCompleteChosen(val, target) {
	$(target).value = val;
	$("autoComplete_" + target).hide();
	
	return false;
}