var ajax = (window.ActiveXObject) ?  new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
var plzboxclose = new CPlzBoxClose();
function change(id,val){
	document.getElementById(id).value=val;
	}
function CPlzDropdown (targetboxid) {

	var selectedbackgroundcolor = "#e4f4fd";
	var land = "DE";
	var germany;
	var austria;
	var switzerland;
	var oldtextbox = "";
	var markerObjects = new Array();
	var markerContents = new Array();
	var markerPosition = 0;
	plzboxclose.Add(targetboxid);
	
	
	this.setDBTable = function(landVar){
		land = landVar;
	}
	
	this.plzClick = function(textbox) {
		if(oldtextbox != textbox.value) {
			oldtextbox = textbox.value;
			markerContents[0] = textbox.value;
			markerPosition = 0;
		}
	germany=document.getElementById("DE").checked;
	austria=document.getElementById("AT").checked;
	switzerland=document.getElementById("CH").checked;
	if(germany) {
	this.setDBTable("DE");
	}
	else{
	if(austria) {
	this.setDBTable("AT");
	}
	else{
	if(switzerland){
	this.setDBTable("CH");
	}
	}
	}
	}
	this.plzKey = function(event,textbox) {
	    targetbox = document.getElementById(targetboxid);
		if(event) {
			keyNum = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	
			if(keyNum == 40 || keyNum == 38 || keyNum == 39 || keyNum == 37 || keyNum == 13) {
				if(keyNum == 40) { // unten
					markerPosition++;
					if(markerPosition > markerObjects.length-1) markerPosition = 0;
				} else if(keyNum == 38) { // oben
					markerPosition--;
					if(markerPosition < 0) markerPosition = markerObjects.length-1
				}
				if(markerObjects.length > 0) {
					for(i=1;i<markerObjects.length;i++)
						document.getElementById(markerObjects[i]).style.background="none";
					if(markerPosition > 0)
						document.getElementById(markerObjects[markerPosition]).style.background=selectedbackgroundcolor;
					textbox.value = markerContents[markerPosition];
					document.getElementById(targetboxid).style.visibility="visible";
				}
				if(keyNum == 13)
					document.getElementById(targetboxid).style.visibility="hidden";
			} else
			if(oldtextbox != textbox.value) {
				oldtextbox = textbox.value;
				if(textbox.value.length > 1) {
					encodedURL = encodeURI("typo3conf/ext/itnoaib/pi1/server.php?feld="+textbox.value+"&land="+land);
	    			ajax.open('GET', encodedURL, false);
	    			ajax.send(null);
	    			json_data = ajax.responseText;
	    			json_array = eval("("+json_data+")");
	    			targetbox.innerHTML = "";
	    			markerPosition = 0;
					markerObjects = new Array();
					markerContents = new Array();
					markerObjects[0] = "";
					markerContents[0] = textbox.value;
					l=json_array.length;
					if(navigator.appName.search(/Explorer/)!=-1) l=l-1;
					for(i=0; i < l; i++) {
	    				markerObjects[i+1] = targetboxid+"_entry_"+i;
	    				markerContents[i+1] = json_array[i][1];
	    				targetbox.innerHTML+="<div class='tx_itnoaib_pi1_dropdown_element' id='"+markerObjects[i+1]+"'"+
	    				" onclick=\"javascript:change('"+textbox.id+"','"+json_array[i][1]+"');\"><a href=\"javascript:change('"+textbox.id+"','"+json_array[i][1]+"');\">"+json_array[i][0]+"</a></div>";
	    			}
					document.getElementById(targetboxid).style.visibility="visible";
				}
				else
				if(textbox.value.length < 2) {
					targetbox.innerHTML = "";
				}
			}
		}
	} // ende function plzKey()

	this.closeBox = function() {
		if(document.getElementById(targetboxid) != null)
			document.getElementById(targetboxid).style.visibility="hidden";
	}
	
} // ende: CPlzDropdown

function CPlzBoxClose() {
	var PlzBoxArray = new Array();
	this.Add = function(id) {
		PlzBoxArray.push(id);
	}
	function closeBox() {
		for(i=0;i<PlzBoxArray.length;i++)
		document.getElementById(PlzBoxArray[i]).style.visibility="hidden";
	}
	document.onmouseup=closeBox;
	window.onmouseup=closeBox;
}

