// Autocomplete object
// Based on code provided by Joe Kepley, The Sling & Rock Design Group, Inc.

function AutoComp(elObj,divID,scriptName,scriptParams, callback)
{
//alert(scriptParams)
	var autoCompObj = this;
	this.el = elObj;	// input box used for autocomplete
	this.div = document.getElementById(divID);
	//this.choices = choices;		// choices for the autocomplete
	this.possible = new Array();	// possible matches
	this.userInput = null;
	this.scriptName = scriptName;
	this.scriptParams = scriptParams;
	
	
	var TAB = 9;
	var ESC = 27;
	var KEYUP = 38;
	var KEYDN = 40;
	var SHIFT = 16; 
	elObj.setAttribute("autocomplete","off");
	
	
	/*elObj.onkeydown = function(ev)
	{
		var key = autoCompObj.getKeyCode(ev);
		switch(key)
		{
			case ESC:
				autoCompObj.hideDiv();
				break;
		}
	}*/
	
	elObj.onkeyup = function(ev) 
	{
		var key = autoCompObj.getKeyCode(ev);
		switch(key)
		{
		//The control keys were already handled by onkeydown, so do nothing.
		case TAB:
		case KEYUP:
		case KEYDN:
		case SHIFT:
			break;
		case ESC:
			autoCompObj.hideDiv();
			break;
		default:

			if (this.value != autoCompObj.inputText && this.value.length > 0)
			{
				autoCompObj.userInput = this.value;
				//autoCompObj.getPossible();
				autoCompObj.div.innerHTML = ""; // clears previous results. used to improve look and feel;
				autoCompObj.createDiv();
				if(autoCompObj.div.style.display != "block")
					autoCompObj.showDiv();
			}
			else
			{
				autoCompObj.hideDiv();
			}
		}
	};
	this.getKeyCode = function(ev)
	{
		if(ev)			//Moz
		{
		//	alert(ev.keyCode)
			return ev.keyCode;
		}
		if(window.event)	//IE
		{
			return window.event.keyCode;
		}
	};
	/*this.getPossible = function
	{
		this.possible = new Array();
		for(i in this.choices)
		{
			var possible = this.choices[i];
			if(possible.toLowerCase().indexOf(this.userInput.toLowerCase()) == "0")
			{
				this.possible[this.possible.length] = possible;
			}
		}
	};*/
	this.createDiv = function()
	{
		/*if(this.div.firstChild)
			this.div.removeChild(this.div.firstChild);
		var ul = document.createElement("ul");
		for(i in this.possible)
		{
			var text = document.createTextNode(this.possible[i]);
			var a = document.createElement("a");
			var li = document.createElement("li");
			a.appendChild(text);
			a.setAttribute("href","#");
			a.setAttribute("title",text.nodeValue);
			a.onclick = function(ev)
			{
				autoCompObj.fillInput(this.firstChild.nodeValue);
				return false;
			};
			li.appendChild(a);
			ul.appendChild(li);
		}
		this.div.appendChild(ul);*/
		if(this.scriptName.indexOf("?w=") != -1)		// remove old query string if there is one
		{
			var index = this.scriptName.indexOf("?w=");
			this.scriptName = this.scriptName.substring(0,index);
		}
		this.scriptName = this.scriptName + "?w=" + this.userInput + '&' + this.scriptParams;	// attach query string
		xmlhttp.open("GET", this.scriptName,true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4)
			{
				if(xmlhttp.status == 200)
				{					
					autoCompObj.div.innerHTML = xmlhttp.responseText;
					autoCompObj.amendLinks();
				}
				else
				{
					alert("Data failed to load because the file, " + this.scriptName + ", wasn't found on the server.");	
				}
			}
		 }
		 xmlhttp.send("");
	};
	
	this.amendLinks = function()
	{
		//
		var a = this.div.getElementsByTagName("a");
		for(var i = 0; i < a.length; i++)
		{
			a[i].onclick = function()
			{
				autoCompObj.fillInput(this.firstChild.nodeValue);
				//alert(this.attributes.item[0]);				
				if (callback != 'undefined') {
					//eval(callback + '(' + this.attributes['id'].value + ')');	
					//alert(this.getAttribute('id'));
					//alert('test1 '+callback + '("' +  this.attributes['Id'].value + '")');
					eval(callback + '("' +  this.getAttribute('id') + '")');					
				}
			};
		}
	}
	
	this.fillInput = function(newValue)
	{
		this.el.value = newValue;
		this.hideDiv();
	};
	this.showDiv = function()
	{
		fadeIn(this.div.id,0,50);
		this.div.style.display = "block";
	};
	this.hideDiv = function()
	{
		this.div.style.display = "none";
	};
}
