var objDiv,objQ;
var ix=0;
var intTop;
var intLeft;
var formLeft;
var	formTop;
var move = false;
var timer = false;

function StartCheck(obj, strLocation, country) {
    timer = setTimeout("LiveSearch('" + obj + "', '" + strLocation + "', '" + country + "')", 500);
}

function StopCheck() {
    if (timer) {
        clearTimeout(timer);
    }
}

function LiveSearch(obj, strLocation, country) {
    searchhelp(document.getElementById(obj), strLocation, country);
}

function makeXMLobj(){
	var xmlHttp = false;
	if (typeof(XMLHttpRequest) != 'undefined') {
        xmlHttp = new XMLHttpRequest();
	}
	if (!xmlHttp) {
	    try {
	        xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch(e) {
	        try {
	            xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
	        } catch(e) {
	            xmlHttp  = false;
	        }
	    }
	}
	return xmlHttp;
}
function getXML(strURL){
	document.onkeyup = notMove;
	if(!move){
	    writeText("Daten werden geladen..");
	    var strResult;
	    xmlHttp = makeXMLobj();
	    if (xmlHttp) {
		    strURL = strURL.replace(/ü/g,"[u]")
		    strURL = strURL.replace(/ä/g,"[a]")
		    strURL = strURL.replace(/ö/g, "[o]")
		    strURL = strURL.replace(/ß/g, "[ss]")
	        xmlHttp.open('GET', strURL, true);
	        xmlHttp.setRequestHeader("Pragma", "no-cache");
	        xmlHttp.setRequestHeader("Content-Type", "text/html; charset=ISO-8859-1");
	        xmlHttp.onreadystatechange = function () {
	            if (xmlHttp.readyState == 4) {
	                strResult = xmlHttp.responseText;
	                if (strResult != "") {
	                    writeResult(strResult);
	                } else {
	                    if (window.location.search.indexOf("fb=1") > -1) {
	                        writeText("<div style='padding:5px; height:182px; border:4px solid red'>Leider ist das Pendlerportal für die von Dir eingegebene Region noch nicht verfügbar.<br /><br />Zur Zeit befinden wir uns in einer intensiven Aufbauphase und werden in Zusammenarbeit mit den Kommunen das Pendlerportal schrittweise erweitern. Wenn Du das Pendlerportal auch für Deine Region nutzen möchtest, kannst Du gerne Deinen Landkreis über diesen Dienst informieren und so helfen, den Aufbauprozess zu beschleunigen.<br /><br />Eine Übersicht über alle verfügbaren Landkreise kannst Du unter <a href='http://www.pendlerportal.de' target='_blank'>www.pendlerportal.de</a> aufrufen.</div>");
	                    } else {
	                        writeText("Es konnten keine zugehörigen Daten ermittelt werden.");
	                    }
	                }
	            }
	        };
	        xmlHttp.send(null);
	    }else{
		    writeText("Bitte aktivieren Sie ActiveX.");
	    }
	    return strResult;
	}
}
function searchhelp(tq,strlocation,country) {
	objQ = tq;
	var q = objQ.value;
	formLeft = getAbsPos(objQ,"Left");
	formTop = getAbsPos(objQ,"Top")+20;
	if(q!=""){
	    if (q.length > 2){
	        var strURL = "/xml/orte2.htm?region="+strlocation+"&land=" +country+"&suchstr=" +q;
	
	        var strResult = getXML(strURL);
	    }
	}
}
function makeDIV(){
	objDiv=document.createElement("DIV");
	objDiv.id="divErg" + objQ.id
	objDiv.style.zIndex="1";
	objDiv.style.padding="0";
	objDiv.style.position="absolute";
	objDiv.style.top=formTop+"px";
	objDiv.style.left=formLeft+"px";
	objDiv.style.height="200px";
	objDiv.style.minwidth = "420px";
	objDiv.style.overflow="auto";
	objDiv.style.backgroundColor="white";
	objDiv.style.border="1px solid black"; 
	objDiv.style.display="block";
	document.body.appendChild(objDiv);
	
}
function writeText(strtext){
	if (!objDiv) makeDIV();
	
    while(objDiv.childNodes.length>0)
	objDiv.removeChild(objDiv.childNodes[0]);
	/*
	objSpan=document.createElement("div");
	objSpan.style.display="block";
	objSpan.style.margin="0px";
	objSpan.style.width = "420px";
	objDiv.appendChild(objSpan)
	objSpan.innerHTML="";
	objSpan=document.createElement("div");
	objSpan.style.display="block";
	objSpan.style.margin="0px";
	objSpan.style.width = "420px";
	objDiv.appendChild(objSpan)
	objSpan.innerHTML="";
	*/
    objSpan=document.createElement("div");
	objSpan.style.display="block";
	objSpan.style.margin="0px";
	objSpan.style.width = "420px";
	objDiv.appendChild(objSpan)
	objSpan.innerHTML=strtext;
}
function writeResult(strResult){
	if (!objDiv) makeDIV();
	
    while(objDiv.childNodes.length>0)
	objDiv.removeChild(objDiv.childNodes[0]);
	
    var arrResult = strResult.split("<br />");
	
    if (arrResult.length > 1){
		objDiv.style.display="block";
	}
	
    ix = 0;
	objSpan=document.createElement("div");
	objSpan.style.display="block";
	objSpan.style.margin="0px";
	objSpan.style.width = "420px";
	objSpan.onmouseover = selectMouseRecord;
	objSpan.onmouseout = deselectMouseRecord;
	objSpan.onmousedown = returnMouseValue;
	//objSpan.onkeypress = alert(".");
	//	if (event.keyCode==13)alert("gna");
	//};
	objDiv.appendChild(objSpan)
	objSpan.innerHTML="keine Auswahl";

	for (i = 0; i < arrResult.length; i++) {
	    objSpan = document.createElement("div");
	    objSpan.style.display = "block";
	    objSpan.style.margin = "0px";
	    objSpan.style.width = "420px";
	    objSpan.onmouseover = selectMouseRecord;
	    objSpan.onmouseout = deselectMouseRecord;
	    objSpan.onmousedown = returnMouseValue;
	    objDiv.appendChild(objSpan)
	    objSpan.innerHTML = arrResult[i];
	}
}

function moveDown(){
	document.onkeydown = checkKeyCode;
}

function checkKeyCode(Ereignis) {
	if(navigator.appName == "Netscape"){
		strKey = Ereignis.which;
	}else{
		strKey = window.event.keyCode;
	}
	
	if(strKey==40){
		move = true;
		if (ix>=0)deselectRecord();
		ix++;
		selectRecord();
		
	}else if(strKey==38){
		move = true;
		deselectRecord();
		ix--;
		selectRecord();
	}else if(strKey==13 || strKey==9) {
	
		var strValue = objSpan.innerHTML;
		
		strValue = strValue.replace(/&nbsp;/g, " ");
		strValue = strValue.replace(/&nbsp;/g, " ");
		strValue = strValue.replace(/&szlig;/g, "ß");
		
		strValue = jQuery.trim(strValue);
		
		objQ.value = strValue;
		
		closeDD();
		
	}

}

function notMove(Ereignis){
	if(navigator.appName == "Netscape"){
		strKey = Ereignis.which;
	}else{
		strKey = window.event.keyCode;
	}
	if(strKey==40||strKey==38){
		move = true;
	}else{
		move = false;
	}
}

function selectRecord() {
	objSpan = objDiv.childNodes[ix]
	objSpan.style.backgroundColor="#ececec";
	objDiv.scrollTop = objSpan.offsetTop-30;
	//returnValue(objSpan.innerHTML);
	//objSpan.focus();

}

function selectMouseRecord() {
	objSpan = this;
	objSpan.style.backgroundColor="#ececec";
	//returnValue(objSpan.innerHTML);
}

function deselectRecord() {
	objSpan = objDiv.childNodes[ix]
	objSpan.style.backgroundColor="#ffffff";
	objSpan.focus();
}

function deselectMouseRecord() {
	objSpan = this;
	objSpan.style.backgroundColor="#ffffff";
	objSpan.focus();
}

function getAbsPos(obj,typ){
	count=0;
	while(obj!=null){
		count=count+obj["offset"+typ];
		obj=obj.offsetParent;}
		return count;
}

function returnValue(strValue) {
	strValue = strValue.replace(/&nbsp;/g, " ");
	strValue = strValue.replace(/&nbsp;/g, " ");
	strValue = strValue.replace(/&szlig;/g, "ß");
	//strValue = strValue.replace(/&uuml;/, "ü");
	//strValue = strValue.replace(/&auml;/, "ä");
	//strValue = strValue.replace(/&ouml;/, "ö");
	//strValue = strValue.replace(/&Uuml;/, "Ü");
	//strValue = strValue.replace(/&Auml;/, "Ä");
	//strValue = strValue.replace(/&Ouml;/, "Ö");
	strValue = jQuery.trim(strValue);
	objQ.value = strValue;
}

function returnMouseValue() {
	objSpan = this;
	var strValue = objSpan.innerHTML;
	strValue = strValue.replace(/&nbsp;/g, " ");
	strValue = strValue.replace(/&nbsp;/g, " ");
	strValue = strValue.replace(/&szlig;/g, "ß");
	//strValue = strValue.replace(/"&uuml;"/, "ü");
	//strValue = strValue.replace(/"&auml;"/, "ä");
	//strValue = strValue.replace(/"&ouml;"/, "ö");
	//strValue = strValue.replace(/"&Uuml;"/, "Ü");
	//strValue = strValue.replace(/"&Auml;"/, "Ä");
	//strValue = strValue.replace(/"&Ouml;"/, "Ö");
	strValue = jQuery.trim(strValue);	
	objQ.value = strValue;
	closeDD();
}

function closeDD() {
    if (objDiv) {
	document.body.removeChild(objDiv);
	objDiv = '';
	}
}

function closeDD2() {
    var Ergebnis = false;
    if (objQ) {
        var strValue = objQ.value;
        var Suche = /^(\d\d\d\d\d).+\B/;
        Ergebnis = Suche.test(strValue);
    }
    if (Ergebnis != false) {
        if (objDiv) {
            document.body.removeChild(objDiv);
            objDiv = '';
        }
	}else{
		if (objDiv) {
		    objSpan = objDiv.childNodes[1]
		
		    if(objSpan.innerHTML!=""){
			    returnValue(objSpan.innerHTML);
			    document.body.removeChild(objDiv);
			    objDiv = '';
		    }else{
			    if(strValue!='')writeText("<span class='err'>Es konnten keine zugehörigen Daten ermittelt werden oder Ihre Eingabe ist nicht eindeutig. <br <br>Bitte überprüfen Sie die Eingabe.</span>");
			    document.body.removeChild(objDiv);
			    objDiv = '';
		    }
		}
	}	
}
