var http_request = false;
var cityFieldId = '';
var streetFieldId = '';
var typeFieldId = '';
var names=new Array();
var values=new Array();


function macheRequest(url,efaproxy) {
    http_request = false;
//window.location.href = url;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        
        if(macheRequest.arguments.length == 1){
	        if (http_request.overrideMimeType) {
	            http_request.overrideMimeType('text/xml');
	            // zu dieser Zeile siehe weiter unten
	        }
    	}
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Ende :( Kann keine XMLHTTP-Instanz erzeugen');
        return false;
    }
    http_request.open('GET', url, true);

    if(macheRequest.arguments.length == 2){
		http_request.onreadystatechange = processEfaRequest;
	}
	else{
		http_request.onreadystatechange = alertInhalt;
	}
       
    http_request.send(null);
    
}

/**
 * Utility function to add a stop to an internal array
 * @param name the name of the stop
 * @param a string containing "omc:id"
 */
function addToStopsArray(name, value)
{
	names[names.length]=name;
	values[values.length]=value;
}


/**
 * Sends back the result as a list of stops.
 * @param names an array of String, containing the stop names
 * @param values an array of String, containing "omc:id"
 */

function sendResultStops()
{	
	// if we have a parent window
	if (window.opener)
	{
		var result = window.opener.odvMapResult;

		// if we found the result object to write to
		if (result)
		{
			// go through the list of stops and add them
			for (i=0; i<names.length; i++)
			{
				result.stopNameList[i]=names[i];
				result.stopList[i]=values[i];	
			}
			result.type="stop";
			result.placeName="";
			
			// Clear fields
			try {window.opener.document.getElementById(window.opener.cityFieldId).value = '';} catch(e) {}
			try {window.opener.document.getElementById(window.opener.streetFieldId).value = '';} catch(e) {}
					
			
			// Fill stop
			try {
				window.opener.document.getElementById(window.opener.typeFieldId).options.selectedIndex = 1;
				window.opener.document.getElementById(window.opener.streetFieldId).value = result.stopNameList[0];
			} catch(e) {
				// alert(window.opener.odvMapResult.coordValue);
				alert('Zum gegebenen Standort konnten leider keine Daten ermittelt werden.');
			}
		
			window.opener.focus(); // moved into macheRequest
			window.close();
		}
	}

}

function processEfaRequest(){
	if (http_request.readyState == 4) {
		parameter = http_request.responseText;
		window.open(WEB_BASEPATH + "/navtec/?"+parameter,"odvMap", "menubar=no,status=no, resizable=yes, toolbar=no, scrollbars=yes, top=0, left=0, width=850, height=680");
	}
	
}

function alertInhalt() {
	if (http_request.readyState == 4) {
		var xmldoc = http_request.responseXML;
		var root_node = xmldoc.getElementsByTagName('city').item(0);
		var allowPoint = true; // Field city must be available to add address
		
		// Clear fields
		try {window.opener.document.getElementById(cityFieldId).value = '';} catch(e) {}
		try {window.opener.document.getElementById(streetFieldId).value = '';} catch(e) {}

		// Fill city
		try {
			window.opener.document.getElementById(cityFieldId).value = root_node.firstChild.data;
		} catch(e) {
			// alert(window.opener.odvMapResult.coordValue);
			alert('Zum gegebenen Standort konnten leider keine Daten ermittelt werden.');
			allowPoint = false;
		}

		if(allowPoint)
		{
			
			// Fill address
			root_node = xmldoc.getElementsByTagName('address').item(0);
			try {window.opener.document.getElementById(streetFieldId).value = root_node.firstChild.data;} catch(e) {}
			
			// Alternative information for points without a street
			if(window.opener.document.getElementById(streetFieldId).value == '')
			{
				root_node = xmldoc.getElementsByTagName('cityId').item(0);
				try {window.opener.document.getElementById(streetFieldId).value = root_node.firstChild.data;} catch(e) {}
			}
		}

		window.opener.focus(); // moved into macheRequest
		window.close();
	}
}

// function to open the popup-window with the map
function CreateMapRequest(givenCityFieldId, givenStreetFieldId, givenadresstype)
{
	
	typeFieldId =	givenadresstype;
	
	if(CreateMapRequest.arguments.length == 2){
		givenadresstype = "address";
	}
	else{
		givenadresstype	= document.getElementById(givenadresstype).options[document.getElementById(givenadresstype).options.selectedIndex].value;	
		
	}
	cityFieldId 	= givenCityFieldId;
	streetFieldId 	= givenStreetFieldId;
	typ				= givenadresstype;
	stadt 			= document.getElementById(cityFieldId).value;
	strasse  		= document.getElementById(streetFieldId).value;
	var req;
	
	
	
	req = WEB_BASEPATH+"/efaproxy/?city="+stadt+"&address="+strasse+"&addressType="+typ;

	macheRequest(req,true);
	


	/* end edited by kernpunkt*/	
	
	//window.open(WEB_BASEPATH + "/navtec/?&amp;xCenterReal=3435000&amp;yCenterReal=464300&amp;zoomLevel=0","odvMap", "menubar=no,status=no, resizable=yes, toolbar=no, scrollbars=yes, top=0, left=0, width=850, height=680");     
}

// object to describe a mapResult
function mapResult()
{
	this.type			= "address";

	this.centerX		= 0;
	this.centerY		= 0;
	this.mapName		= "";
	this.coordValue		= "";
	this.desc			= "";

	this.placeName		= "";
	this.omc			= 0;
	this.placeID		= 0;

	this.streetName		= "";
	this.streetID		= 0;

	this.poiID			= 0;

	this.stopName = ".";

	this.stopList		= new Array();
	this.stopNameList	= new Array();
}

// object to describe a mapRequest
function mapRequest()
{
	this.odvMap		= null;
	this.mapUsage	= "unknown";
	this.mapType	= "unknown";
	this.objectList = new Array();
}

/* create instances of the needed objects */
var odvMapResult  = new mapResult();
var odvMapRequest = new mapRequest();

/*
 * function to save the given coords
 *
 * this function will be called from sendResultMapCenter to handle the given coords.
 * This is the right place to add the data into your own application.
 *
 */
function setMapInput()
{
	alert('Status:' + odvMapRequest.mapUsage
	+ ' City:' + odvMapResult.placeName
	+ ' Street:' + odvMapResult.streetName
	+ ' Koordinaten:' + odvMapResult.coordValue
	+ ' Type' + odvMapResult.type);
}

/*
 * callback from java application to select a point
 *
 * this function will be called from the java application after the user has choosen
 * a location. This function will close the popup and write back the coords.
 *
 */
function sendResultMapCenter(desc, coord)
{
	// if we have a parent window
	if (window.opener)
	{
		var result = window.opener.odvMapResult;

		cityFieldId = window.opener.cityFieldId;
		streetFieldId = window.opener.streetFieldId;

		// if we found the result object to write to
		if (result)
		{
			result.desc = desc;
			result.coordValue = coord;
			//window.opener.setMapInput(); // no longer required
			
			macheRequest(WEB_BASEPATH + '/pointrequest/?xmloutput=true&address=' + window.opener.odvMapResult.coordValue);

			//window.opener.focus(); // moved into macheRequest
			//window.close();
		}
	}
}

window.onload = function()
{
	if(document.getElementById('popupclose'))
	{
		try
		{
			document.getElementById('popupclose').onclick = function(){window.close()}
		}
		catch (e) {}
	}
}