/* autofill for city name
author : REMOTION bv. (www.remotion.nl)

*/

listen(window, 'load', initAutofill);

	var tbox , citylist;
	var citycache= new Array()
	var currentXML = null;
	var OfficieelCity;

	function fillddl(e, strAbr)
	{
		var evt = e || window.event;
		var kc = new Number(evt.keyCode);
		//$_("k").innerHTML = kc;

		switch(kc.valueOf())
		{
			case 40: //arrow down
			if (citylist.style.display=='block')citylist.focus();
			return 0;
			break;
			case 27: citylist.style.display='none';return void(0); break;
			default:
		}
		getCitylist(strAbr)
	}
	function getCitylist(strAbr)
	{
		//determine if we should get new request or just get it from cache;
		if (strAbr.length ==0) return;
		var idx = strAbr.substr(0,1).toLowerCase();

		if (citycache[idx])
		{

			currentXML = citycache[idx];
			displayCityList(strAbr);
		} else
		{

				citycache[idx] = null;
				getContent("/templates/xml/city.asp?abr="+strAbr,
				getCitylistCallback,null);
		}

	}
	function getOfficieelCity(strCityName)
	{
		getContent("/templates/xml/officieelcity.asp?cnaam="+strCityName,getOfficieelCityCallback,null);

		return OfficieelCity;
	}

	function getOfficieelCityCallback(objXML)
	{
		currentXML= objXML;
		if (currentXML)
		{
			var noderoot = currentXML.firstChild;
			while (noderoot.nodeType != 1)
			{
				noderoot = noderoot.nextSibling;
				if (!noderoot) break;
			}
			if (!noderoot) return;

			if (!noderoot.getElementsByTagName("city")[0])
			{
				return;
			}
			else
			{
				OfficieelCity = noderoot.getElementsByTagName("city")[0].firstChild.nodeValue;
				//alert(OfficieelCity.length);
			}

			//alert(arrcities.length);
			//alert(OfficieelCity);
		}
	}

	function getOfficieelCityCallbackAll(objXML)
	{
		currentXML= objXML;
		if (currentXML)
		{
			var noderoot = currentXML.firstChild;
			while (noderoot.nodeType != 1)
			{
				noderoot = noderoot.nextSibling;
				if (!noderoot) break;
			}
			if (!noderoot) return;

			if (!noderoot.getElementsByTagName("city")[0])
			{
				return;
			}
			else
			{
				for(i=0;i< noderoot.getElementsByTagName("city").length;i++)
				{
					OfficieelCity = noderoot.getElementsByTagName("city")[i].firstChild.nodeValue;
					//alert(OfficieelCity);
				}
			}

			//alert(arrcities.length);
			//alert(OfficieelCity);
		}
	}

	function displayCityList(strAbr)
	{
		if (currentXML)
		{
			var noderoot = currentXML.firstChild;
			while (noderoot.nodeType != 1)
			{
				noderoot = noderoot.nextSibling;
				if (!noderoot) break;
			}
			if (!noderoot) return;

			var arrcities = noderoot.getElementsByTagName("city");
			//alert(arrcities.length);

			//clear the ddl
			var obj = citylist.firstChild;
			while(obj)
			{
				var temp = obj.nextSibling;
				citylist.removeChild(obj);
				obj = temp;
			}

			var listSize = 0;
			for (i =0; i < arrcities.length;i++)
			{
				if (i == 0)
				{
					var cityname= new String(arrcities[i].firstChild.nodeValue);
					if (cityname.substr(0,strAbr.length).toLowerCase()==strAbr.toLowerCase())
					{
						var  opt = document.createElement("option");
						opt.value = cityname ;
						opt.selected = true;
						opt.innerHTML = cityname;
						citylist.appendChild(opt);
						listSize +=1;
					}
				}
				else
				{
					var cityname= new String(arrcities[i].firstChild.nodeValue);
					if (cityname.substr(0,strAbr.length).toLowerCase()==strAbr.toLowerCase())
					{
						var  opt = document.createElement("option");
						opt.value = cityname ;
						opt.innerHTML = cityname;
						citylist.appendChild(opt);
						listSize +=1;
					}
				}
			}

			if(arrcities.length >0)
			{

				//var lcity = (arrcities.length <10)?(arrcities.length):10;
				var lcity = listSize>10?10:listSize;
				if (lcity == 1) lcity = 2;
				//window.status=lcity + "  " + listSize;
				//citylist.size = lcity;
				if (lcity == 0 )
				{
					citylist.style.display="none";
				}
				else
				{
					citylist.style.display="block";
				}
				//alert("0");
			}
			else
			{
				citylist.style.display="none";
			}
		}
		else
		{
			citylist.style.display="none";
		}

	}

	function getCitylistCallback(objXML)
	{
		currentXML= objXML;
		citycache[tbox.value.substr(0,1).toLowerCase()] = currentXML;
		displayCityList(tbox.value);
	}

	function displayCity(obj)
	{
		tbox.value= obj.value;
	}
	function initAutofill()
	{
		citylist= $_("ddlcity");
		if (!citylist) return;
		tbox = $_("cityname");
		tbox.onkeyup = function(e)
		{
			fillddl(e,this.value);
		}
		tbox.onfocus = function()
		{
			citylist.style.display='none';
		}
		citylist.onchange= function()
		{
			displayCity( this);
		}
		citylist.onkeypress=function(e)
		{
			var evt = e || window.event;
			if ((evt.keyCode==13) || (evt.keyCode==27))
			{
				if (evt.keyCode==13) displayCity( this);
				$_("zoekcity").focus();
				this.style.display="none";
			}
		}
		citylist.ondblclick=function()
		{
				displayCity( this)
				$_("zoekcity").focus();
				this.style.display="none";
		}
		citylist.onclick=function()
		{
				displayCity( this)
				$_("zoekcity").focus();
				this.style.display="none";
		}
	}

	/*for debug only.
	function displayCache(){
		var s =""
		for (e in citycache)
		{
			s +=e+":-->"
			var els = citycache[e];
			var ele = els.getElementsByTagName("title");
			s+="["
			for (i =0; i < ele.length;i++)
			{
				s += ele[i].firstChild.nodeValue + (i== ele.length-1 ?"": ",")
			}
			s+="]\n";
		}
		$_("cache").innerHTML= "<pre>"+ s+"</pre>";
	}
	*/