var citytipbox = null;
var counties = Array (
"",
"Győr-Moson-Sopron megye",
"Vas megye",
"Zala megye",
"Fejér megye",
"Komárom-Esztergom megye",
"Veszprém megye",
"Baranya megye",
"Somogy megye",
"Tolna megye",
"Pest megye",
"Borsod-Abaúj-Zemplén megye",
"Heves megye",
"Nógrád megye",
"Hajdú-Bihar megye",
"Jász-Nagykun-Szolnok megye",
"Szabolcs-Szatmár-Bereg megye",
"Bács-Kiskun megye",
"Békés megye",
"Csongrád megye"
)

function CallRemotePage(sender, myurl, params, action)
{
	var xmlHttp;
	var AJAXResult;
	
	if (params == null)
	{
		params = "";
	}
	
	if (action == null)
	{
		action = "";
	}
		
	try
	{
		// Firefox, Opera 8.0+, Safari
		if (!xmlHttp)
			xmlHttp=new XMLHttpRequest();
		else if (xmlHttp.readyState != 0)
			xmlHttp.abort(); 
	}
	catch (e)
	{    
		// Internet Explorer
		try
		{
			if (!xmlHttp)
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			else if (xmlHttp.readyState != 0)
				xmlHttp.abort(); 
		}
		catch (e)
		{
			try
			{
				if (!xmlHttp)
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				else if (xmlHttp.readyState != 0)
					xmlHttp.abort(); 
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function()	{
		if(xmlHttp.readyState==4)	{
			if (action.length > 0)
			{
				// visszakapott szoveg: xmlHttp.responseText
				AJAXResult = xmlHttp.responseText;
				eval(action);
			}
		}
	}
	if (params.length > 0) {
		params += "&";
	}
	params += "t=" + ((new Date()).valueOf());
		
	xmlHttp.open("POST", myurl, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);  
}

function InitCityTipBox ()
{
	if (citytipbox != null)
	{
		citytipbox.RemoveDOMElements();
		delete citytipbox;
	}
	citytipbox = new AjaxTip("citytipbox");
	citytipbox.textboxcontrol = document.getElementById('txtCity');
	citytipbox.minchars = 1;
	citytipbox.maxtips = 5;
	citytipbox.width = 200;
	citytipbox.tipfontsize = 14;
	citytipbox.tipfontfamily = "Tahoma, Arial, Helvetica, sans-serif";
	// Megye comboval
	//citytipbox.on_textinput = "document.getElementById('txtZip').value = ''; CallRemotePage(null, 'citysearch.php', 'citysearchtext=' + encodeURIComponent(citytipbox.textboxcontrol.value) + '&county=' + document.getElementById('cmbCounty').value + '&count=' + citytipbox.maxtips, 'citytipbox.GenerateTipList(AJAXResult);');";
	
	// Megye combo nélkül
	citytipbox.on_textinput = "document.getElementById('txtZip').value = ''; CallRemotePage(null, 'citysearch.php', 'citysearchtext=' + encodeURIComponent(citytipbox.textboxcontrol.value) + '&county=0' + '&count=' + citytipbox.maxtips, 'citytipbox.GenerateTipList(AJAXResult);');";	
	citytipbox.on_tipselect = "on_CityTipChange (citytipbox.tiplistarray[citytipbox.selectedtipindex])";
	citytipbox.InitControls();
}

function on_CountyChange ()
{
	document.getElementById("txtCity").value = "";
	document.getElementById("txtZip").value = "";
}

function on_CityChange ()
{
	document.getElementById("txtZip").value = "";
}

function on_CityTipChange (newvalue)
{
	var tempvalue = newvalue[3];
	if (newvalue[4] != "")
	{
		tempvalue += " ► " + newvalue[4];
	}
	document.getElementById("txtZip").value = tempvalue;
	// Megye comboval
	//SetComboValue (document.getElementById("cmbCounty"), newvalue[2]);
	
	//Megye combo nélkül
	if (newvalue[1] == "Budapest")
	{
		document.getElementById("txtCounty").value = "Budapest";
	}
	else
	{
		document.getElementById("txtCounty").value = counties[newvalue[2]];
	}

	if ($('motor') != null)
	{
		//gotoTelepules(newvalue[5]/1.191+120, newvalue[6]/1.7+80);
		gotoTelepules(newvalue[5], newvalue[6]);
	}
}

function on_ZipChange (newvalue)
{
	if ((Number(newvalue) != "NaN") && (newvalue >= 1011) && (newvalue <= 9985))
	{
		// Megye comboval
		//CallRemotePage(null, 'citysearch.php', 'county=' + document.getElementById('cmbCounty').value + '&zip=' + document.getElementById("txtZip").value, 'RefreshSearchFormByZip(AJAXResult);');
		
		// Megye combo nélkül
		CallRemotePage(null, 'citysearch.php', 'county=0' + '&zip=' + document.getElementById("txtZip").value, 'RefreshSearchFormByZip(AJAXResult);');
	}
	document.getElementById("txtCity").value = "";
	// Megye comboval
	//document.getElementById("cmbCounty").options[0].selected = true;
}

function RefreshSearchFormByZip(resultdata)
{
	if (resultdata != "empty")
	{
		var resultarr = resultdata.split(";");
		document.getElementById("txtCity").value = resultarr[1];
		
		//Megye comboval
		//SetComboValue (document.getElementById("cmbCounty"), resultarr[2]);
		
		// Megye combo nélkül
		if (resultarr[1] == "Budapest")
		{
			document.getElementById("txtCounty").value = "Budapest";
		}
		else
		{
			document.getElementById("txtCounty").value = counties[resultarr[2]];
		}
		
		if ($('motor') != null)
		{
			//gotoTelepules(newvalue[5]/1.191+120, newvalue[6]/1.7+80);
			gotoTelepules(resultarr[5], resultarr[6]);
		}
	}
}

function SetComboValue (mycombo, myvalue) 
{
	for (var i = 0; i < mycombo.options.length; i++ )
	{
		if ( mycombo.options[i].value == myvalue )
		{
			mycombo.options[i].selected = true;
			return;
		}
	}
}

function ResetForm ()
{
	document.getElementById("txtCity").value = "";
	document.getElementById("txtZip").value = "";	

	//Megye comboval
	//document.getElementById("cmbCounty").options[0].selected = true;

	//Megye combo nélkül
	document.getElementById("txtCounty").value = "";
}
