/*
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Bitflux GmbH                                      |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Bitflux GmbH <devel@bitflux.ch>                              |
// +----------------------------------------------------------------------+

*/


var liveSearchReq = false;
var t = null;
var liveSearchLast = [];
var resultListElId = "LSResult";

function liveSearchHideDelayed( ms ) {
	window.setTimeout("liveSearchHide()", (ms == undefined || ms == 0) ? 400 : undefined);
}

function liveSearchHide() {
	document.getElementById("LSResult").style.display = "none";
}

function liveSearchStart(inputElId, resElId, goal) {
	if (t) {
		window.clearTimeout(t);
	}
	t = window.setTimeout("liveSearchDoSearch('"+inputElId+"', '"+resElId+"', '"+goal+"')", 500);
}

function liveSearchDoSearch(inputElId, resElId, goal) {
	var inputEl = document.getElementById(inputElId);
	if (liveSearchLast[inputElId] != inputEl.value) {
		if (liveSearchReq && liveSearchReq.readyState < 4) {
			liveSearchReq.abort();
		}
		if ( inputEl.value.length < 3 ) {
			liveSearchHide();
			return false;
		}
		try {
			liveSearchReq=new ActiveXObject("Msxml2.XMLHTTP")
		} catch(e) {
			try {
				liveSearchReq=new ActiveXObject("Microsoft.XMLHTTP")
			} catch(oc) {
				liveSearchReq=null
			}
		}
		if (!liveSearchReq && typeof XMLHttpRequest != "undefined") {
			liveSearchReq=new XMLHttpRequest()
		}
		if(!liveSearchReq) { return }
		
		resultListElId = resElId;
		liveSearchLast[inputElId] = inputEl.value;
		
		var postdata= "searchString=" + inputEl.value + "&goal=" + goal;
		liveSearchReq.open("POST", "/dgap/action/AutoCompleteSearch/", true);
		liveSearchReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		liveSearchReq.setRequestHeader("Content-length", postdata.length);
		liveSearchReq.onreadystatechange = function() {
		   if(liveSearchReq.readyState == 4 && liveSearchReq.status == 200) {
		   		var res = document.getElementById(resultListElId);
				res.style.display = "block";
				res.innerHTML = liveSearchReq.responseText;
		   }
		}
		liveSearchReq.send(postdata);
	}
}
