
var lsActiveLabel;
var lsActiveValue;

function livesearch(inputElementName, rpcCommand)
{
	this.width = "-";

	this.debug = function(a) {
		// console.log(a);
	}	
	
	this.show = function() {	
		this.divBackground.style.display = "block";
		this.divContainer.style.display = "block";
		lsActiveLabel = "";
	}
		
	this.hide = function() {
		this.divBackground.style.display = "none";
		this.divContainer.style.display = "none";	
		lsActiveLabel = "";
		//this.onchange(this.element.value);
	}
	
	this.attachToElement = function() 
	{
		var obj = document.getElementById(this.elementName);
		if (!obj) {
			console.log("livesearch can't find element " + this.elementName + " to attach to");
			return false;
		}
		
		this.element = obj;
		obj.setAttribute("autocomplete", "off");
		obj.obj = this;
		
		obj.onchange = function() {
			value = lsActiveLabel ? lsActiveLabel : this.value;
			this.obj.debug("obj.onchange [" + value + "]");
			this.obj.hide();
			this.obj.setValue(value, lsActiveValue);
		}
		
		//	The background div to automatically end the live search if the user clicks out of it
		
		var divBackground = document.createElement("div");
		divBackground.className		=	"livesearchBackground";
		divBackground.obj = this;
				
		divBackground.onclick = function() {
			//	Hide the live search when the user clicks on this background div
			this.obj.hide();
		}
			
	// ----	The container div
			
		var divContainer = document.createElement("div");
		divContainer.className = "livesearchResults";
		divContainer.style.top = obj.offsetHeight + "px";
		if (this.width == "-") var w = (obj.offsetWidth-2) + "px";
		else var w = this.width;
		w = "200px"; // ##FIXME FIX THIS
		divContainer.style.width = w;
		
	// ----	The results div
		
		var divResults = document.createElement("div");
		divResults.className = "LSRes";
				
	// ----	Add the divs to their parents
		
		document.body.appendChild(divBackground);
		obj.parentNode.appendChild(divContainer);
		divContainer.appendChild(divResults);
		
		obj.lsobj = this;
		
		this.divBackground 	= divBackground;
		this.divResults		= divResults;
		this.divContainer 	= divContainer;
		
		return true;
	}
	
	this.keypress = function(e) 
	{
		if (!this.onsearchstart()) return;
	
		if (window.XMLHttpRequest) {
			objRequest = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			objRequest = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (!objRequest) {
			this.debug("keypress: Ooops, no objRequest");
			return false;
		}
		
		var req = this.searchRoot + this.rpcCommand + "?base=" + this.element.value + this.searchParams;
		this.objRequest = objRequest;
		this.reqParams	= req;
		objRequest.open("GET", req, false);
		objRequest.send(null);
		data = objRequest.responseText;
		
		this.divResults.innerHTML = objRequest.responseText;
		this.show();
			
		if (this.divResults.childNodes[0] && this.divResults.childNodes[0].childNodes) 
		{
			var base = this.divResults.childNodes[0].childNodes;
			var lsObj = this;
			for (i=0; i<base.length; i++) 
			{
				base[i].onmouseover = function() {
					lsActiveLabel = this.innerHTML;
					lsActiveValue = this.getAttribute("code");
					//this.style.background = "#ccccc8";
					this.className = "LSRowOver";
				}
					
				base[i].onmouseout = function() {
					this.style.background = "";
					lsActiveLabel = "";
					this.className = "LSRow";
				}
					
				base[i].onclick = function(e) {
					// "this" is the OL in this chain: parentContainer -> input -> div -> div -> ul -> ol
					//	The only reference to the liveSearch object is held in the input element
					value 		= 	this.innerHTML;
					elInput		=	this.parentNode.parentNode.parentNode.parentNode.childNodes[0];
					//console.log("--[" + this.getAttribute("code") + "]");
					lsObj.setValue(value, this.getAttribute("code"));
					lsObj.hide();
				}
			}
		}								
	}
	
	this.setValue = function(value, code) {
		this.value = value;
		this.element.value = value;
		this.onchange(value, code);
	}
		
		
	// "constructor"
	
	this.elementName	=	inputElementName;
	this.rpcCommand		=	rpcCommand;
	this.searchParams	=	"";
	this.searchRoot		=	"";
	
	if (!this.attachToElement()) return;

	this.element.onkeyup = function(e) {
		this.lsobj.keypress(e);
	}
	
	this.onsearchstart = function() {
		return true;
	}
	this.onchange = function(value, object) {
	}
}

function debug(text) {
		var s = document.createElement("div");
		s.innerHTML = text;
		if (document.getElementById("debug(")) document.getElementById("debug(").appendChild(s);
	}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getrpc(cmd, args) {
	var k;
	if (k=getHTTPObject()) {
		var cmd = "/rpc/" + cmd + ".php?" + args;
 		m = k.open("GET", cmd, false);
		k.send(null);
		return k.responseText;
	}
}

function getHTTPObject()
{
	var xmlhttp;

	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	
	catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}

	if (!xmlhttp) { // && typeof XMLHttpRequest != 'undefined') {
    	try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			//console.log("Error: " + e);
			xmlhttp = false;
		}
	}
	return xmlhttp;
}
