var counties = new Array("Budapest", "Baranya", "Békés", "Bács-Kiskun", "Borsod", "Csongrád", "Fejér", "Győr-Moson-Sopron", "Hajdu-Bihar", "Heves", "Jász-Nagykun-Szolnok", "Komárom", "Nógrád", "Pest", "Somogy", "Szabolcs-Szatmár-Bereg", "Tolna", "Veszprém", "Vas", "Zala");
						 
var links = new Array(20, 17, 15, 8, 12, 14, 18, 16, 7, 13, 6, 19, 11, 1, 10, 5, 4, 3, 2, 9);

var checkout = 0;

var ajaxEvalCommand = function(command)
{
	eval(command);
}

var insertOption = function (cityName, cityId, selectId, optNum)
{
	var newOption = document.createElement('option');
	
  	newOption.text = cityName;
  	newOption.value = cityId;
  	//newOption.addEventListener('click', initShowShops, false);
  	
  	try
  	{
    	selectId.add(newOption,null);
  	}
  	catch(ex)
  	{
  	  	selectId.add(newOption); // IE!!!
  	}
  	
  	var optionsDiv = document.getElementById("optionsDiv_"+selectId.id);
  	var optionHolder = document.createElement('p');
	var optionLink = document.createElement('a');
	var optionTxt = document.createTextNode(cityName);
    optionLink.href = "javascript:showOptions('"+selectId.id+"'); selectMe('"+selectId.id+"',"+optNum+",'"+selectId.id+"');";
	optionLink.appendChild(optionTxt);
	optionHolder.appendChild(optionLink);
	optionsDiv.appendChild(optionHolder);
}

var buildCitySelect = function(cities)
{
	cities = cities.split(";");
	var cityIds = cities[0].split(",");
	var cityNames = cities[1].split(",");
	selectId = document.getElementById("city_select");
	selectId.length = 0;
	
	var optionsDiv = document.getElementById("optionsDiv_"+selectId.id);
	var newO = optionsDiv.cloneNode(false);
	optionsDiv.parentNode.insertBefore(newO,optionsDiv);
	optionsDiv.parentNode.removeChild(optionsDiv);
	
	insertOption("Kérjük válasszon települést", 0, selectId, 0);
	for (i = 0; i < cityIds.length; i++) insertOption(cityNames[i], cityIds[i], selectId, i+1);
}

var showCities = function(countyId, isCheckout)
{
	var countySelect = document.getElementById("county_select");
	
	if (countySelect.options[countySelect.selectedIndex].value != countyId)
	{
		for (var i = 0; i < countySelect.length; i++ )
		{
			if (countySelect.options[i].value == countyId)
			{
				//countySelect.options[i].selected = true;
				selectMe(countySelect.id, i, countySelect.id, 1);
			}
		}
	}
	selectMe("city_select", 0, "city_select");
	
	var ax_args = "county=" + countyId;
	var ax_url = liveSite + "/administrator/components/com_virtuemart/classes/shop_selector.php";
	var ajaxRequest = new XHR({method: 'get', onSuccess: buildCitySelect}).send(ax_url, ax_args);
}

var showShops = function(cityId)
{
	var ax_args = "city=" + cityId+"&checkout="+checkout;
	var ax_url = liveSite + "/administrator/components/com_virtuemart/classes/shop_selector.php";
	var ajaxRequest = new XHR({method: 'get', onSuccess: ajaxEvalCommand}).send(ax_url, ax_args);
}
