Suggest.prototype.suggest_index ;
Suggest.prototype.control ;
Suggest.prototype.targetId ;
Suggest.prototype.editDiv ;
Suggest.prototype.script ;
Suggest.prototype.objName ;
Suggest.prototype.ruler ;
Suggest.prototype.target_style ;
Suggest.prototype.close_image ;
Suggest.prototype.suggest_bg_normal;
Suggest.prototype.suggest_bg_highlight;

function Suggest (_script,_editDiv,_control,_targetId,_objName,_ruler,_target_style,_close_image,_suggest_bg_normal,_suggest_bg_highlight)
{
	this.suggest_index = -1;
	this.control = _control;
	this.targetId = _targetId;
	this.editDiv = _editDiv;
	this.script = _script;
	this.objName = _objName;
	this.ruler = _ruler;
	this.target_style = _target_style;
	this.close_image = _close_image;
	this.suggest_bg_normal = _suggest_bg_normal;
	this.suggest_bg_highlight = _suggest_bg_highlight;
};
Suggest.prototype.findPosX = function ()
{
	myControl = this.control;
	var curleft = 0;
	if(myControl.offsetParent)
	while(1)
	{
		curleft += myControl.offsetLeft;
		if(!myControl.offsetParent)
		break;
		myControl = myControl.offsetParent;
	}
	else if(myControl.x)
	curleft += myControl.x;
	return curleft;
};

Suggest.prototype.findPosY = function ()
{
	myControl = this.control;
	var curtop = 0;
	if(myControl.offsetParent)
	while(1)
	{
		curtop += myControl.offsetTop;
		if(!myControl.offsetParent)
		break;
		myControl = myControl.offsetParent;
	}
	else if(myControl.y)
	curtop += myControl.y;
	return curtop;
};

Suggest.prototype.suggest = function (e,qs)
{
	obj = this;
	
	var key=e.keyCode || e.which;
	if (key==38 || key==40 || key==27) return;
	var ts = Math.round(new Date().getTime() / 1000);
	oXmlHttp = zXmlHttp.createRequest ();
	if ( qs == "" ) oXmlHttp.open("get",this.script,true);
	else oXmlHttp.open("get",this.script+"?"+qs+"&rand="+ts,true);
	oXmlHttp.onreadystatechange =function()
	{
		if ( oXmlHttp.readyState == 4 )
		{
			if (oXmlHttp.responseText != "")
			{
				
				if ( oXmlHttp.responseText == "No Results" )
				{
					document.getElementById(obj.targetId).style.visibility='hidden';
					document.getElementById(obj.targetId).innerHTML= "";
					obj.suggest_index = -1;
					document.getElementById(obj.ruler).innerHTML = obj.control.value;
					w = document.getElementById(obj.ruler).offsetWidth+10;
					w = w + 20;
					obj.control.style.width = w+'px';
				}
				else
				{
					document.getElementById(obj.targetId).innerHTML= oXmlHttp.responseText;
					document.getElementById(obj.targetId).style.visibility='visible';
					document.getElementById(obj.targetId).style.top=(obj.findPosY()+obj.control.offsetHeight)+'px';
					document.getElementById(obj.targetId).style.left=obj.findPosX()+'px';
					obj.suggest_index = -1;
					document.getElementById(obj.ruler).innerHTML = obj.control.value;
					w = document.getElementById(obj.ruler).offsetWidth+10;
					w = w + 20;
					obj.control.style.width = w+'px';
				}
			}
		}
	};
	oXmlHttp.send(null);
 	document.getElementById(this.ruler).innerHTML = this.control.value;
 	w = document.getElementById(this.ruler).offsetWidth+10;
 	this.control.style.width = w+'px';

};

Suggest.prototype.addElement = function (content)
{
	var ni = document.getElementById(this.editDiv);

	var newdiv = document.createElement('div');
	newdiv.style.display='inline';
	num = ni.childNodes.length+1;
//	var divIdName = content;
	var divIdName = Math.round(new Date().getTime() / 1000);
	newdiv.setAttribute('id',divIdName);
	newdiv.innerHTML = "<div style='display:inline;"+this.target_style+"'>"+content+"<a onClick='javascript:"+this.objName+".removeElement(\""+divIdName+"\")'> "+this.close_image+" </a></div>";

	ni.insertBefore( newdiv, this.control);
};

Suggest.prototype.removeElement = function (divNum)
{
	var d = document.getElementById(this.editDiv);
	var olddiv = document.getElementById(divNum);
	d.removeChild(olddiv);
};

Suggest.prototype.down = function ()
{

	if ( document.getElementById(this.targetId).style.visibility == "visible" )
	{
		if ( this.suggest_index != -1)
		{
			tr_name = "suggest_selection_"+this.suggest_index;
			tr_ob = document.getElementById(tr_name);
			tr_ob.style.backgroundColor = this.suggest_bg_normal;
		}
		this.suggest_index ++;
		if (this.suggest_index >= document.getElementById("suggest_table").rows.length ) this.suggest_index=0;
		tr_name = "suggest_selection_"+this.suggest_index;
		tr_ob = document.getElementById(tr_name);
		tr_ob.style.backgroundColor = this.suggest_bg_highlight;
	}
};

Suggest.prototype.up = function ()
{
	if ( document.getElementById(this.targetId).style.visibility == "visible" )
	{
		if ( this.suggest_index != -1)
		{
			tr_name = "suggest_selection_"+this.suggest_index;
			tr_ob = document.getElementById(tr_name);
			tr_ob.style.backgroundColor = this.suggest_bg_normal;
		}
		this.suggest_index --;
		if (this.suggest_index < 0 ) this.suggest_index= document.getElementById("suggest_table").rows.length-1;
		tr_name = "suggest_selection_"+ this.suggest_index;
		tr_ob = document.getElementById(tr_name);
		tr_ob.style.backgroundColor = this.suggest_bg_highlight;
	}
};

Suggest.prototype.mouseSelect = function (mouse_suggest_index)
{
	td_name = "td_selection_"+mouse_suggest_index;
	this.addElement(document.getElementById(td_name).innerHTML);
	document.getElementById(this.targetId).style.visibility='hidden';
	document.getElementById(this.targetId).innerHTML= "";
	this.suggest_index = -1;
	this.control.value="";
	this.control.focus();
	if (navigator.appName != 'Microsoft Internet Explorer')
	{
		e.stopPropagation();
		e.preventDefault();
	}
};

Suggest.prototype.handleKeyPress = function (e)
{
	var key=e.keyCode || e.which;
		if (key==13)
		{
			if ( document.getElementById(this.targetId).style.visibility == "visible" && this.suggest_index != -1)
			{
				td_name = "td_selection_"+this.suggest_index;
				this.addElement(document.getElementById(td_name).innerHTML);
				document.getElementById(this.targetId).style.visibility='hidden';
				document.getElementById(this.targetId).innerHTML= "";
				this.suggest_index = -1;
			}
			else this.addElement(this.control.value);
			this.control.value="";
			this.control.focus();
			if (navigator.appName != 'Microsoft Internet Explorer')
			{
				e.stopPropagation();
				e.preventDefault();
			}
		}
		else if ( key == 27)
		{
			document.getElementById(this.targetId).style.visibility='hidden';
			document.getElementById(this.targetId).innerHTML= "";
			this.suggest_index= -1;
		}
		else if ( key == 8 && this.control.value == "")
		{
			var ni = document.getElementById(this.editDiv);
			if (ni.childNodes.length > 2)
				this.removeElement(ni.childNodes[ni.childNodes.length-2].id);
		}
		else if ( key == 38 )
		{
			this.up();
		}
		else if ( key == 40 )
		{
			this.down();
		}

		document.getElementById(this.ruler).innerHTML = this.control.value;
		w = document.getElementById(this.ruler).offsetWidth+10;
		w = w + 20;
		this.control.style.width = w+'px';
		this.control.value = this.control.value;
};

Suggest.prototype.removeLastElement = function ()
{
	var ni = document.getElementById(this.editDiv);
	if (ni.childNodes.length > 2)
	{
		this.removeElement(ni.childNodes[ni.childNodes.length-2].id);
		return false;
	}
	else return false;
};
