function AjaxTip (instancename)
{
	this.instancename = instancename;
	this.textboxcontrol = null; // 'street'
	this.secondarytextboxcontrol = null; // Ebbe fog beíródni a találat (de csak ha meg van adva)!
	this.tipcontainer = null; // cregaddresstipcontainer
	this.tipframe = null; // cregaddrestipframe
	this.tipbox = null; // cregaddresstip
	this.tiplistarray = null;
	this.maxtips = null;
	this.nooftips = null;
	this.disprownum = null;
	this.selectedtipindex = null;
	this.oldselectedtipindex = null;
	this.on_textinput = null;
	this.on_tipselect = null;
	this.on_originatedchange = null;
	this.minchars = null;
	this.width = null;
	this.height = null;
	this.originatedfromlist = null;
	this.selectedid = null;
	this.tipmargin = null;
	this.tipfontsize = null;
	this.tipfontfamily = null;
	this.mouseovertip = null;
}

AjaxTip.prototype.InitControls = function ()
{
	var parentobject = this;

	this.tipcontainer = document.getElementById("tipcontainer_" + this.instancename);
	if (this.tipcontainer != null) {
		this.tipcontainer.parentNode.removeChild(this.tipcontainer);
	}
	this.tipcontainer = document.createElement("div");
	this.tipcontainer.id = "tipcontainer_" + this.instancename;
	this.tipcontainer.style.visibility = "hidden";
	this.tipcontainer.style.position = "absolute";
	this.tipcontainer.style.zIndex = "2";
	this.tipcontainer.style.fontSize = this.tipfontsize + "px";
	this.tipcontainer.style.fontFamily = this.tipfontfamily;

	this.tipframe = document.createElement("frame");
	this.tipframe.id = "tipframe_" + this.instancename;
	this.tipframe.className = "ajaxtipframe";
	this.tipframe.style.position = "absolute";
	this.tipframe.style.zIndex = "3";

	this.tipcontainer.style.width = this.width + "px"; /*this.textboxcontrol.clientWidth + "px";*/
	this.tipcontainer.style.height = this.textboxcontrol.clientHeight + "px";
	this.tipcontainer.style.left = this.width + "px"; /*this.textboxcontrol.clientWidth + "px";*/
	this.tipcontainer.style.top = this.textboxcontrol.clientHeight + "px";
	this.tipframe.style.width = this.width + "px"; /* this.textboxcontrol.clientWidth + "px"; */
	this.tipframe.style.height = (this.textboxcontrol.clientHeight * this.nooftips) + "px";
	this.tipframe.style.left = "0px";
	this.tipframe.style.top = "0px";

	this.tipbox = document.createElement("div");
	this.tipbox.id = "tipbox_" + this.instancename;
	this.tipbox.className = "ajaxtipbox";
	this.tipbox.style.width = this.width + "px"; /* this.textboxcontrol.clientWidth + "px"; */
	this.tipbox.style.height = (this.textboxcontrol.clientHeight * this.nooftips) + "px";
	this.tipbox.style.position = "absolute";
	this.tipbox.style.top = "0px";
	this.tipbox.style.left = "0px";
	this.tipbox.style.zIndex = "4";
	
	document.body.appendChild(this.tipcontainer);
	this.tipcontainer.appendChild(this.tipframe);
	this.tipcontainer.appendChild(this.tipbox);
	
	this.textboxcontrol.onkeydown = function (e)
	{
		AjaxTip.prototype.CheckInput(parentobject, e);
	}
	this.textboxcontrol.onkeyup = function (e)
	{
		AjaxTip.prototype.UpdateTips(parentobject, e);
	}
	this.textboxcontrol.autocomplete = "off";
	this.textboxcontrol.onblur = function ()
	{
		if (parentobject.mouseovertip != true)
		{
			AjaxTip.prototype.Hide(parentobject);
		}
	}
	
	mypos1 = this.findPos(this.tipcontainer);
	mypos2 = this.findPos(this.textboxcontrol);
	diffleft = mypos2[0] - mypos1[0] + 5;
	difftop = mypos2[1] - mypos1[1] + 1;
	//alert(mypos1[0] + "/" + mypos2[0] + ", " + mypos1[1] + "/" + mypos2[1] + " -> " + diffleft + "  ~  " + difftop);
	this.tipcontainer.style.top = this.tipcontainer.clientHeight + difftop + this.textboxcontrol.clientHeight + "px";
	this.tipcontainer.style.left = this.tipcontainer.clientWidth + diffleft + "px";
}

AjaxTip.prototype.Hide = function (sender)
{
	//if (sender.selectedtipindex == -1)
	//{
		sender.tipcontainer.style.visibility = "hidden";
	//}
}

AjaxTip.prototype.findPos = function (myobject)
{
	var curleft = curtop = 0;
	if (myobject.offsetParent) 
	{
		do
		{
			curleft += myobject.offsetLeft;
			curtop += myobject.offsetTop;
		} while (myobject = myobject.offsetParent);
	}
	return [curleft,curtop];
}

AjaxTip.prototype.GenerateTipList = function(responseText)
{
	var i;
	var myrows = null;
	var mycolumns = null;
	var myrownum = 0;
	var mycolnum = 0;
	var newdiv = null;
	var parentobject = this;
	
	this.tiplistarray = [];
	if (responseText == "error") {
		htmllist = "Hiba történt.";
	} else 	if (responseText != "empty") {
		var myrows = responseText.split("\r\n");
		var myrownum = myrows.length;
		
		this.tipbox.innerHTML = "";
		for (i = 0; i < myrownum; i++)
		{
			var mycolumns = myrows[i].split(";");
			var mycolnum = mycolumns.length;
			this.tiplistarray [i] = new Array();
			for (j = 0; j < mycolnum; j++)
			{
				this.tiplistarray [i][j] = mycolumns[j];
			}
			newdiv = document.createElement("div");
			//newdiv.style.backgroundColor = "#FF00FF";
			newdiv.id = "tipitem_" + this.instancename + "~" + i;
			newdiv.className = "ajaxtipitem_inactive";
			newdiv.style.overflow = "hidden";
			newdiv.onmouseover = function (e)
			{
				AjaxTip.prototype.TipMouseOver (parentobject, this, e);
			}
			newdiv.onmouseout = function (e)
			{
				AjaxTip.prototype.TipMouseOut (parentobject, this, e);
			}
			newdiv.onclick = function () {
				AjaxTip.prototype.TipClick (parentobject, this);
			}
			newdiv.style.height = this.textboxcontrol.clientHeight + "px";
			newdiv.innerHTML = this.EscapeHTML(this.tiplistarray [i][1]);
			this.tipbox.appendChild(newdiv);
		}
		this.nooftips = myrows.length;
		this.disprownum = this.nooftips; 
	} else {
		this.tipbox.innerHTML = "";
		newdiv = document.createElement("div");
		newdiv.id = "tipitem_" + this.instancename + "~" + "0";
		newdiv.className = "ajaxtipitem_inactive";
		newdiv.style.overflow = "hidden";
		newdiv.style.height = this.textboxcontrol.clientHeight + "px";
		newdiv.innerHTML = "Nincs találat.";
		this.tipbox.appendChild(newdiv);
		this.nooftips = 0;
		this.disprownum = 1;
	}
	if (responseText != "error") {
		//if (responseText != "empty") {
		if (this.tipmargin == null)
		{
			this.tipmargin = this.getStyle(newdiv, "margin", "margin-top");
			this.tipmargin = parseInt(this.tipmargin.substr(0, this.tipmargin.length - 2));
		}
		this.tipcontainer.style.height = (Math.abs(this.disprownum) * (newdiv.offsetHeight + this.tipmargin) + this.tipmargin) + "px";
		this.tipbox.style.height = (Math.abs(this.disprownum) * (newdiv.offsetHeight + this.tipmargin) + this.tipmargin) + "px";
		this.tipframe.style.height = (Math.abs(this.disprownum) * (newdiv.offsetHeight + this.tipmargin) + this.tipmargin) + "px";
		this.tipcontainer.style.visibility = "visible";
		//alert(this.nooftips + "* " + "(" + newdiv.offsetHeight + " + " + this.tipmargin + ")" +  " + " + this.tipmargin + " = " + this.tipcontainer.clientHeight);
	} else {
		this.tipcontainer.style.visibility = "hidden";
	}
	this.selectedtipindex = -1;
}

AjaxTip.prototype.UpdateTips = function (sender, myevent)
{
	var keycode = sender.MyKeyCode(myevent);

	if ((keycode >= 48) || (keycode == 8) || (keycode == 46))
	{
		sender.originatedfromlist = false;
		sender.OnOriginatedChange(sender);
		sender.selectedid = null;
		//alert(keycode);
		if (sender.textboxcontrol.value.length >= sender.minchars)
		{
			//streetandhouse = SplitStreetAndHouseNo(addresstextbox.value);
			//document.getElementById("name").value = streetandhouse;
			//GetRemoteContentCReg("", "src/getaddress.php", "searchmode=tipwithcoords&mapid=" + mapid + "&zip=0&street=" + streetandhouse[0] + "&houseno=" + streetandhouse[1] + "&maxresults=16&t=" + new Date().getTime(), "GenerateAddressTipList(xmlHttp.responseText);");
			eval(sender.on_textinput);
		} else {
			sender.tipcontainer.style.visibility = "hidden";
		}
	}
}

AjaxTip.prototype.CheckInput = function (sender, myevent)
{
	var keycode = sender.MyKeyCode(myevent);
	var tmptext = sender.textboxcontrol.value;
	
	//alert(keycode);
	switch (keycode)
	{
		case 27:
			sender.tipcontainer.style.visibility = "hidden";
			sender.textboxcontrol.value = tmptext;
			sender.selectedtipindex = -1;
			break;
		case 40:
			if ((sender.nooftips > 0) && (sender.tipcontainer.style.visibility == "visible")) {
				sender.oldselectedtipindex = sender.selectedtipindex;
				sender.selectedtipindex++;
				if ((sender.selectedtipindex >= sender.nooftips) || (sender.selectedtipindex < 0))
				{
					sender.selectedtipindex = 0;
				}
				sender.UpdateTipSelection(sender);
			}
			break;
		case 38:
			if ((sender.nooftips > 0) && (sender.tipcontainer.style.visibility == "visible")) {
				sender.oldselectedtipindex = sender.selectedtipindex;
				sender.selectedtipindex--;
				if ((sender.selectedtipindex >= sender.nooftips) || (sender.selectedtipindex < 0))
				{
					sender.selectedtipindex = sender.nooftips - 1;
				}
				sender.UpdateTipSelection(sender);
			}
			break;
		case 13:
			var lt = new RegExp("&lt;", "g");
			var gt = new RegExp("&gt;", "g");
			var amp = new RegExp("&amp;", "g");

			if (sender.selectedtipindex > -1)
			{
				if (sender.secondarytextboxcontrol == null)
				{
					//sender.textboxcontrol.value = document.getElementById("tipitem_" + sender.instancename + "~" + sender.selectedtipindex).innerHTML.replace(lt, "<").replace(gt, ">").replace(amp, "&").trim();
					sender.textboxcontrol.value = this.EscapeHTML(document.getElementById("tipitem_" + sender.instancename + "~" + sender.selectedtipindex).innerHTML);
				}
				else
				{
					//sender.secondarytextboxcontrol.value = document.getElementById("tipitem_" + sender.instancename + "~" + sender.selectedtipindex).innerHTML.replace(lt, "<").replace(gt, ">").replace(amp, "&").trim();
					sender.secondarytextboxcontrol.value = this.EscapeHTML(sender.secondarytextboxcontrol.value = document.getElementById("tipitem_" + sender.instancename + "~" + sender.selectedtipindex).innerHTML);
				}
				sender.textboxcontrol.focus();
				sender.originatedfromlist = true;
				sender.selectedid = sender.tiplistarray[sender.selectedtipindex][0];
				sender.OnOriginatedChange(sender);
				setTimeout(function()
				{
					sender.textboxcontrol.focus();
				}
				, 1);
				sender.setCaretToEnd(sender.textboxcontrol);
				sender.tipcontainer.style.visibility = "hidden";
				eval(sender.on_tipselect);
			}
			break;
	}
}

AjaxTip.prototype.UpdateTipSelection = function (sender)
{
	if (sender.oldselectedtipindex > -1)
	{
			document.getElementById("tipitem_" + sender.instancename + "~" + sender.oldselectedtipindex).className = "ajaxtipitem_inactive";
	}
	document.getElementById("tipitem_" + sender.instancename + "~" + sender.selectedtipindex).className = "ajaxtipitem_selected";
}

AjaxTip.prototype.MyKeyCode = function (myevent)
{
	var keynum;
	var keychar;
	var numcheck;
	
	if(window.event) // IE
	{
		keynum = event.keyCode;
	}
	else if(myevent.which) // Netscape/Firefox/Opera
	{
		keynum = myevent.which;
	}
	return keynum;
}

AjaxTip.prototype.setCaretToEnd = function (el)
{
  if (el.createTextRange) {
    var v = el.value;
    var r = el.createTextRange();
    r.moveStart('character', v.length);
    r.select();
  }
}

AjaxTip.prototype.TipMouseOver = function (sender, tipdiv, myevent)
{
	if(sender.checkMouseEnter(tipdiv, myevent)) {
		//alert(sender.selectedtipindex);
		var selectedtip = document.getElementById("tipitem_" + sender.instancename + "~" + sender.selectedtipindex)
		if (selectedtip != null)
		{
			selectedtip.className = "ajaxtipitem_inactive";
		}
		tipdiv.className="ajaxtipitem_selected";
		sender.selectedtipindex = parseInt(tipdiv.id.substring(tipdiv.id.indexOf("~") + 1));
		sender.mouseovertip = true;
	}
}

AjaxTip.prototype.TipMouseOut = function (sender, tipdiv, myevent)
{
	if(sender.checkMouseLeave(tipdiv, myevent)) {
		var mousetipindex = parseInt(tipdiv.id.substring(tipdiv.id.indexOf("~") + 1));
		tipdiv.className="ajaxtipitem_inactive";
		
		if (sender.selectedtipindex == mousetipindex)
		{
			sender.selectedtipindex = -1;
		}
		sender.mouseovertip = false;
	}
}

AjaxTip.prototype.TipClick = function (sender, tipdiv)
{
	var lt = new RegExp("&lt;", "g");
	var gt = new RegExp("&gt;", "g");
	var amp = new RegExp("&amp;", "g");

	//alert("hello1");
	if (sender.secondarytextboxcontrol == null)
	{
		//sender.textboxcontrol.value = tipdiv.innerHTML.replace(lt, "<").replace(gt, ">").replace(amp, "&").trim();
		this.EscapeHTML(sender.textboxcontrol.value = tipdiv.innerHTML);
	}
	else
	{
		sender.secondarytextboxcontrol.value = tipdiv.innerHTML.replace(lt, "<").replace(gt, ">").replace(amp, "&").trim();
	}
	sender.tipcontainer.style.visibility = "hidden";
	sender.textboxcontrol.focus();
	sender.originatedfromlist = true;
	sender.selectedid = sender.tiplistarray[sender.selectedtipindex][0];
	sender.OnOriginatedChange(sender);
	sender.setCaretToEnd(sender.textboxcontrol);
	if (sender.on_tipselect != null)
	{
		eval(sender.on_tipselect);
	}
	//alert("hello2");
}

AjaxTip.prototype.containsDOM = function (container, containee)
{
	var isParent = false;
	do {
		if ((isParent = container == containee)) {
			break;
		}
		try {
			containee = containee.parentNode;
		}
		catch (err) {
			containee = null;
		}
	}
	while (containee != null);
	return isParent;
}

AjaxTip.prototype.checkMouseEnter = function (element, evt)
{
	if(window.event) // IE
	{
		evt = event;
	}
	if (element.contains && evt.fromElement) {
		return !element.contains(evt.fromElement);
	}
	else if (evt.relatedTarget) {
		return !AjaxTip.prototype.containsDOM(element, evt.relatedTarget);
	}
}

AjaxTip.prototype.checkMouseLeave = function (element, evt)
{
	if(window.event) // IE
	{
		evt = event;
	}
	if (element.contains && evt.toElement) {
		return !element.contains(evt.toElement);
	}
	else if (evt.relatedTarget) {
		return !AjaxTip.prototype.containsDOM(element, evt.relatedTarget);
	}
}

AjaxTip.prototype.getStyle = function (el, stylePropIE, styleProFF)
{
	//var x = document.getElementById(el);
	var x = el;
	if (x.currentStyle)
	{
		var y = x.currentStyle[stylePropIE];
	}
	else if (window.getComputedStyle)
	{
		var y = document.defaultView.getComputedStyle(x,'').getPropertyValue(styleProFF);
	}
	return y;
}

AjaxTip.prototype.OnOriginatedChange = function (sender)
{
	eval(sender.on_originatedchange);
}

AjaxTip.prototype.RemoveDOMElements = function ()
{
	if (this.tipcontainer != null)
	{
		this.tipcontainer.parentNode.removeChild(this.tipcontainer);
	}
}

AjaxTip.prototype.EscapeHTML = function(htmlstring) {
  var mydiv = document.createElement('div');
  var mytext = document.createTextNode(htmlstring);
  mydiv.appendChild(mytext);
  return mydiv.innerHTML;
}
