	
	INPUT_SEARCH_STATUS = new Array();
	INPUT_SEARCH_STATUS["active"] = false;
	
	function inputSearch(obj, height, script, param2) {
		
		if (INPUT_SEARCH_STATUS["active"]) {
			inputSearch_deactivate();
		}
		
		obj.select();
		
		INPUT_SEARCH_STATUS["active"] = true;
		INPUT_SEARCH_STATUS["obj"] = obj;
		INPUT_SEARCH_STATUS["input_id"] = obj.id;
		INPUT_SEARCH_STATUS["list_id"] = obj.id + "_input_search_list";
		INPUT_SEARCH_STATUS["script"] = script;
		INPUT_SEARCH_STATUS["find_state"] = false;
		INPUT_SEARCH_STATUS["find_text"] = "";
		INPUT_SEARCH_STATUS["first_click"] = false;
		INPUT_SEARCH_STATUS["param2"] = param2;

		var div_list = document.createElement('div');
  	div_list.setAttribute('id', INPUT_SEARCH_STATUS["list_id"]);
		div_list.style.position = "absolute";
		div_list.style.height = height + "px";
		div_list.style.width = (obj.clientWidth - 6) + "px";
		
		var posxy = get_elem_xy(obj);
		
		div_list.style.left = posxy[0] + "px";
		div_list.style.top = (posxy[1] + obj.clientHeight + 4) + "px";
		
		div_list.style.borderWidth = "1px";
		div_list.style.borderColor = "#333";
		div_list.style.borderStyle = "solid";
		div_list.style.overflow = "auto";
		div_list.style.backgroundColor = "white";
		div_list.style.padding = "5px";
		obj.offsetParent.insertBefore(div_list, obj.nextSibling);
		inputSearch_update();
	}

	function inputSearch_find() {
		var ajax = newXHR();
		var target_input = getElemById(INPUT_SEARCH_STATUS["input_id"]);
		var target_list =  getElemById(INPUT_SEARCH_STATUS["list_id"]);
		var find;
		
		ajax.open("post", INPUT_SEARCH_STATUS["script"], true);
		ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		ajax.setRequestHeader("connection", "close");
		ajax.onreadystatechange = function() { 
			/* ajax */
			if(ajax.readyState === readyState.COMPLETATO) {
				target_list.innerHTML = ajax.responseText;
				INPUT_SEARCH_STATUS["find_state"] = false;
			}
			/* end */
		}
		
		if (!INPUT_SEARCH_STATUS["first_click"]) { 
			find = ""; 
			INPUT_SEARCH_STATUS["first_click"] = true; 
		} else { 
			find = INPUT_SEARCH_STATUS["find_text"]; 
		}
		
		ajax.send("num="+INPUT_SEARCH_STATUS["input_id"]+"&find="+find+"&param2="+INPUT_SEARCH_STATUS["param2"]);
		target_list.innerHTML = "<img src=\"img/ajax_loader.gif\" />";
		INPUT_SEARCH_STATUS["find_state"] = true;
	}
	
	function inputSearch_update() {
		if (INPUT_SEARCH_STATUS["active"]) {
			if (INPUT_SEARCH_STATUS["swclose"]) {
				inputSearch_deactivate();
			} else {
				var target_input = getElemById(INPUT_SEARCH_STATUS["input_id"]);
				if (!INPUT_SEARCH_STATUS["find_state"]) {
					if (target_input.value != INPUT_SEARCH_STATUS["find_text"] || !INPUT_SEARCH_STATUS["first_click"]) {
						INPUT_SEARCH_STATUS["find_text"] = target_input.value;
						inputSearch_find();
					}
				}
				setTimeout("inputSearch_update()",100);
			}
		}
		
	}

	function inputSearch_deactivate() {
		if (INPUT_SEARCH_STATUS["active"]) {
			var parent = INPUT_SEARCH_STATUS["obj"].offsetParent;
			var elem = getElemById(INPUT_SEARCH_STATUS["list_id"]);
			parent.removeChild(elem);
		
			INPUT_SEARCH_STATUS["active"] = false;
			INPUT_SEARCH_STATUS["obj"] = false;
			INPUT_SEARCH_STATUS["input_id"] = false;
			INPUT_SEARCH_STATUS["list_id"] = false;
			INPUT_SEARCH_STATUS["script"] = false;
			INPUT_SEARCH_STATUS["find_state"] = false;
			INPUT_SEARCH_STATUS["find_text"] = false;
			INPUT_SEARCH_STATUS["first_click"] = false;
		}
	}
	
	function inputSearchSet(input_id, obj) {
		getElemById(input_id).value = obj.innerHTML;
		inputSearch_deactivate();
	}

	function clickedOutsideBox(e) {
		if (!e) { e = window.event; }
		var clickElem = getEventTarget(e);
		var pElem = clickElem;
		
		while(pElem != null) {
			if(pElem.id == INPUT_SEARCH_STATUS["list_id"]) {
				return false;
			}
			pElem = pElem.offsetParent;
		}
		
		if (clickElem.id == INPUT_SEARCH_STATUS["input_id"]) {
			return false;
		}
		
		return true;
	}
	
	function getEventTarget(e) {
		var targ = (e.target) ? e.target : e.srcElement;
		if(targ != null) {
			if(targ.nodeType == 3) { targ = targ.parentNode; }
		}
		return targ;
	}

	function get_elem_xy(obj) {	
		var xp, yp, op
		xp = obj.offsetLeft;
		yp = obj.offsetTop;
		while (obj.offsetParent) {
			op = obj.offsetParent;
			xp = xp + op.offsetLeft;
			yp = yp + op.offsetTop;
			obj = obj.offsetParent;
		}
		
		return new Array(xp,yp);
	}

	document.onclick = function(e) {
		if (INPUT_SEARCH_STATUS["active"]) {
			if (clickedOutsideBox(e)) {
				inputSearch_deactivate();
			}
		}
	}