/**
copyright 2007 by flobbymedia.de
**/
var fmGCTimeoutId;
var fmGCEntry = new Array();
function livefmGeoCoder(mode)
{
if(fmGCTimeoutId!=0) {
clearTimeout(fmGCTimeoutId);
}
fmGCTimeoutId = setTimeout("fmGeoCoder( '"+ mode + "')",700);
}

function fmGeoCoder(mode)
{

 try {		
  fmGC = new XMLHttpRequest();
  } catch(w3c) {
   try {		
    fmGC = new ActiveXObject("Msxml2.XMLHTTP");
   } catch(msie) {
    try {	
     fmGC = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(msie_alt) {
     alert("Error 000001");
     return false;	
    }
   }
  }
  var printdiv = document.getElementById("fmGeoCoderResult");
  var newContent ="<img src=\"" + CONFIGScriptPath + "/data/images/icon/loading_ajax_m.gif\" alt=\"lade\" /> Bitte warten, die Datenbank wird durchsucht.";
  
  var Adress = escape(document.getElementById("fmGeoCoderInput").value);

  printdiv.innerHTML = newContent;

	fmGC.open("GET", CONFIGScriptPath + "/data/request/gc.fm?a='" + Adress + "'", true);
	
	fmGC.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
	fmGC.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	fmGC.send(null);

  fmGC.onreadystatechange = function() {

   if (fmGC.readyState == 4) {

    if (fmGC.status != 200 && fmGC.status != 0) {
     var printerror = "<strong>AJAXERROR</strong> in module fmGeoCoder. Errorcode: 000002";
     printdiv.innerHTML = printerror;
     return false;
    }

var pl = fmGC.responseXML.getElementsByTagName('Placemark');
var newContent = "<table style=\"width:100%;\">\n\t";
newContent += "<tr style=\"font-weight:bold;\"><td>Adresse</td><td>Ort</td></tr>\n\t";
   for(var i = 0; i < pl.length; i++) {
	fmGCEntry[i] = new Array();
	fmGCEntry[i]['pid'] 	= i;
	var aa	= pl[i].getElementsByTagName('AdministrativeAreaName');
		if(aa.length == 1){	fmGCEntry[i]['aa'] 	= aa[0].firstChild.nodeValue;		} else { fmGCEntry[i]['aa'] = '';	}
	var city 	= pl[i].getElementsByTagName('LocalityName');
		if(city.length == 1){	fmGCEntry[i]['city'] 	= city[0].firstChild.nodeValue;	} else { fmGCEntry[i]['city']  = '';	}
	var sa 	= pl[i].getElementsByTagName('SubAdministrativeAreaName');
		if(sa.length > 0){	fmGCEntry[i]['sa'] 	= sa[0].firstChild.nodeValue;		} else { fmGCEntry[i]['sa'] = '';	}
	var district = pl[i].getElementsByTagName('DependentLocalityName');
		if(district.length > 0){	fmGCEntry[i]['district'] 	= district[0].firstChild.nodeValue;	} else { fmGCEntry[i]['district'] = '';	}
	var zip 	= pl[i].getElementsByTagName('PostalCodeNumber');
		if(zip.length > 0){	fmGCEntry[i]['zip'] 	= zip[0].firstChild.nodeValue;		} else { fmGCEntry[i]['zip']  = '';	}
	var street 	= pl[i].getElementsByTagName('ThoroughfareName');
		if(street.length > 0){	fmGCEntry[i]['street'] 	= street[0].firstChild.nodeValue;	} else { fmGCEntry[i]['street']  = '';	}
	var point 	= pl[i].getElementsByTagName('coordinates');
		if(point.length > 0){	fmGCEntry[i]['point'] 	= point[0].firstChild.nodeValue;	} else { fmGCEntry[i]['point']  = '';	}

      newContent += "<tr onclick=\"javascript:fmGeoCoderSave('" + fmGCEntry[i]['pid'] + "', '" + mode + "');\" onmouseover=\"this.style.background='#efefef'; this.style.cursor='pointer';\" onmouseout=\"this.style.background='none'; this.style.cursor='auto';\"><td>" + fmGCEntry[i]['street'] + "</td><td><strong>" + fmGCEntry[i]['zip'] + " " + fmGCEntry[i]['city'] + "</strong> (" + fmGCEntry[i]['aa'] + ")</td></tr>\n\t";
}
if (pl.length == 1){
/*	fmGeoCoderSave('0', '" + mode + "'); */
}
if (pl.length == 0){
	newContent += "<tr><td colspan=\"2\"><strong>Die angegebene Adresse konnte nicht ermittelt werden!</strong><br />Bitte kontrolliere deine Eingabe auf Rechtschreibfehler oder Kontaktiere unseren <a href=\"" + CONFIGScriptPath + "/support\">Support</a></td></tr>\n\t";
} 

newContent += "</table>";
      printdiv.innerHTML = newContent;
      return true;

    var printerror = "<strong>AJAXERROR</strong> in module fmGeoCoder. Errorcode: 000003";
    printdiv.innerHTML = printerror;
    return false;
   }
  }
}

function fmGeoCoderSave(id, mode)
{
	Slide('fmGeoCoderInputDiv','0','60','20');
	if(typeof(hint) === undefined) {  Slide('fmGeoCoderSaveDiv','0','75','20'); } else {  Slide('fmGeoCoderSaveDiv','0','100','20'); }
	document.getElementById("fmGeoCoderInput").disabled 		= true;
	document.getElementById("fmGeoCoderResult").style.height 	= '0px';
	document.getElementById("fmGeoCoderStreet").value 		= fmGCEntry[id]['street'];
	document.getElementById("fmGeoCoderCity").value 			= fmGCEntry[id]['city'];
	document.getElementById("fmGeoCoderZip").value 			= fmGCEntry[id]['zip'];
	document.getElementById("fmGeoCoderAA").value 			= fmGCEntry[id]['aa'];
	document.getElementById("fmGeoCoderSA").value 			= fmGCEntry[id]['sa'];
	document.getElementById("fmGeoCoderDistrict").value 		= fmGCEntry[id]['district'];
	document.getElementById("fmGeoCoderPoint").value 			= fmGCEntry[id]['point'];
	/* If the most important fields are filled, pass icon*/
	if (fmGCEntry[id]['street'].length == 0 || fmGCEntry[id]['city'].length == 0 || fmGCEntry[id]['zip'].length == 0) {
		document.getElementById("checkadress").innerHTML = "<img src=\"" + CONFIGScriptPath + "/data/images/icon/fail.gif\" alt=\"fail\" />";
		if(typeof(hint) === undefined) {  showHint("adressewie"); }
	} else {
		document.getElementById("checkadress").innerHTML = "<img src=\"" + CONFIGScriptPath + "/data/images/icon/check.gif\" alt=\"pass\" />";
		if(typeof(hint) === undefined) {  showHint("adresse"); }
	}
}

function fmGeoCoderReset(hint)
{

	Slide('fmGeoCoderInputDiv','0','60','20');
	if(typeof(hint) === undefined) {  Slide('fmGeoCoderSaveDiv','0','75','20'); } else {  Slide('fmGeoCoderSaveDiv','0','100','20'); }
	document.getElementById("fmGeoCoderInput").disabled 		= false;
	document.getElementById("checkadress").innerHTML 			= '';
	document.getElementById("fmGeoCoderResult").style.height 	= 'auto';
	document.getElementById("fmGeoCoderStreet").value 		= '';
	document.getElementById("fmGeoCoderCity").value 			= '';
	document.getElementById("fmGeoCoderZip").value 			= '';
	document.getElementById("fmGeoCoderAA").value 			= '';
	document.getElementById("fmGeoCoderSA").value 			= '';
	document.getElementById("fmGeoCoderDistrict").value 		= '';
	document.getElementById("fmGeoCoderPoint").value 			= '';
	if(typeof(hint) === undefined) { showHint("adresse"); }
}

function fmGeoCoderSet(street, city, zip, aa, sa, district, point, hint)
{
	Slide('fmGeoCoderInputDiv','0','60','20');
	if(typeof(hint) === undefined) {  Slide('fmGeoCoderSaveDiv','0','75','20'); } else {  Slide('fmGeoCoderSaveDiv','0','100','20'); }
	document.getElementById("fmGeoCoderInput").disabled 		= true;
	document.getElementById("fmGeoCoderInput").value			= street + ", " + zip + " " + city;
	document.getElementById("fmGeoCoderResult").style.height 	= '0px';
	document.getElementById("fmGeoCoderStreet").value 		= street;
	document.getElementById("fmGeoCoderCity").value 			= city;
	document.getElementById("fmGeoCoderZip").value 			= zip;
	document.getElementById("fmGeoCoderAA").value 			= aa;
	document.getElementById("fmGeoCoderSA").value 			= sa;
	document.getElementById("fmGeoCoderDistrict").value 		= district;
	document.getElementById("fmGeoCoderPoint").value 			= point;
	/* If the most important fields are filled, pass icon*/
	if (street.length == 0 || city.length == 0 || zip.length == 0) {
		document.getElementById("checkadress").innerHTML = "<img src=\"" + CONFIGScriptPath + "/data/images/icon/fail.gif\" alt=\"fail\" />";
		if(typeof(hint) === undefined) {  showHint("adressewie"); }
	} else {
		document.getElementById("checkadress").innerHTML = "<img src=\"" + CONFIGScriptPath + "/data/images/icon/check.gif\" alt=\"pass\" />";
		if(typeof(hint) === undefined) {  showHint("adresse"); }
	}
}