
 var watchId;
 var lastAcc;
 function doLocate(position) {
    var acc = position.coords.accuracy;
    if (acc > 1000 || acc < lastAcc * 0.9) {
	lastAcc = acc;
    } else {
       navigator.geolocation.clearWatch(watchId);
       successCallback(position);
    }
 }
	function successCallback(position) {
		xaddWGS84_GEOX = position.coords.longitude; //11.459655;
		xaddWGS84_GEOY = position.coords.latitude; //49.281230;
		sy=4;
		rho = 180 / 3.1415926535897932384626433832795;
		e2 = 0.0067192188;
		c = 6398786.849;
		bf = xaddWGS84_GEOY / rho;
		g = 111120.61962 * xaddWGS84_GEOY -15988.63853 * Math.sin(2*bf) +16.72995 * Math.sin(4*bf) -0.02178 * Math.sin(6*bf) +0.00003 * Math.sin(8*bf);
		co = Math.cos(bf);
		g2 = e2 * (co * co);
		g1 = c / Math.sqrt(1+g2);
		t = Math.sin(bf) / Math.cos(bf); 
		dl = xaddWGS84_GEOX - sy * 3;
		fa = co * dl / rho;
		y = g + fa * fa * t * g1 / 2 + fa * fa * fa * fa * t * g1 * (5 - t * t + 9 * g2) / 24; 
		rm = fa * g1 + fa * fa * fa * g1 * (1 - t * t + g2) / 6 + fa * fa * fa * fa * fa * g1 * (5 - 18 * t * t * t * t * t * t) / 120;
		x = rm + sy * 1000000 + 500000;
		y = y + 120; // empirische korrektur
		x = x + 80; // empirische korrektur
		y = 6160000 - y; // GK > NAV4
		document.forms[0][lastGPS + "l"].value = "c:" + x + ":" + y + ":aktuelle Position";
		document.forms[0][lastGPS].value = "aktuelle Position";
	}
	function errorCallback(err) {
	}
	function mes(message) {
		document.getElementById("debug").innerText = message;
	}
	var lastGPS;
	var watchId;
 function fnLoc(pmType) {
	lastGPS = pmType;
    if (!navigator.geolocation) {
	alert("GPS nicht vorhanden.");
    } else {
	lastAcc = 999999;
      watchId = navigator.geolocation.watchPosition(doLocate);
    }
 }
 function fnLocBn(pmType) {
    if (navigator.geolocation) {
	if(document.getElementById("gps_" + pmType))
		document.getElementById("gps_" + pmType).innerHTML = '<div style=\"float:right\" onclick="fnLoc(\'' + pmType + '\')">hier (GPS)</div>';
    }
 }
	
//
// TIMER
//
var rTBTimer = new rTBTimerClass();
function rTBTimerClass() {
	this.timers = new Array();
	this.id = 0;
}
rTBTimerClass.prototype.fnAdd = function(pmObject, pmFunction, pmTimeout) {
	// var id = this.id++;
	if(this.timers[1])
		this.fnCancel(1);
	var id = 1;
	this.timers[id] = new Array();
	this.timers[id]["class"] = pmObject;
	this.timers[id]["function"] = pmFunction;
	this.timers[id]["timeoutId"] = window.setTimeout("fnTBTimer('" + id + "')", pmTimeout);
	return id;
}
rTBTimerClass.prototype.fnCancel = function(pmId) {
	window.clearTimeout(this.timers[pmId]["timeoutId"]);
}
rTBTimerClass.prototype.fnExec = function(pmId) {
	var obj = this.timers[pmId]["class"];
	eval("obj." + this.timers[pmId]["function"] + "()");
}

function fnTBTimer(pmId) {
	if(typeof(rTBTimer) == 'undefined') {
		return;
	}
	rTBTimer.fnExec(pmId);
}


//
// INIT
//
function fnEFA_CreateAJAXInput(pmInput, pmDiv, pmUsage) {
	var loc = new rEFA_LocationClass(pmUsage);
	loc.div = pmDiv;
	loc.fnAdd(pmInput);
	loc.fnVerify();
}
function fnAJAX_Verify(pmEvent) {
	pmEvent = pmEvent?pmEvent:window.event;
	this.ajaxControl.fnNotifyChange();
}


//
// LOCATION
//
function rEFA_LocationClass(pmUsage) {
	this.usage = pmUsage;
	this.state = "undefined";
	this.control = null;
	this.val = null;
}
rEFA_LocationClass.prototype.fnInit = function() {
	this.val = this.fnReadValues();
}
rEFA_LocationClass.prototype.fnAdd = function(pmControl) {
	this.control = pmControl;
	pmControl.ajaxControl = this;
	pmControl.onchange = fnAJAX_Verify;
	pmControl.onkeyup = fnAJAX_Verify;
	pmControl.onclick = fnAJAX_Verify;
}
rEFA_LocationClass.prototype.fnReadValues = function() {
	var res = this.control.value;
	return res;
}
rEFA_LocationClass.prototype.fnNotifyChange = function() {
	rTBTimer.fnAdd(this, "fnVerify", 400);
}
rEFA_LocationClass.prototype.fnVerify = function() {
	var newVal = this.fnReadValues();
	if(newVal == this.val)
		return;
	this.val = newVal;
	var params = {
		rRequestType: "location",
		rUsage: this.usage,
		Type: "any",
		Name: this.val,
		noCache: Date()
		};
	params = $H(params).toQueryString();

	var _ajax = new Ajax.Request('/ib/site/tools/Interface_EFA_Fare2.php/loc', {
			method: 'get',
			asynchronous: true,
			parameters: params,
			onComplete: fnAJAXCallback_EFAInput
		}
	);
}


function fnAJAXCallback_EFAInput(pmRequest) {
	if (pmRequest && pmRequest.responseXML) {
		
		var itdLPs = pmRequest.responseXML.getElementsByTagName("itdLayoutParam");
		for (var i = 0; i < itdLPs.length; i++) {
			if (itdLPs[i].getAttribute("name") == "Usage") {
				var usage = itdLPs[i].getAttribute("value");
			}
		}
		var odv = pmRequest.responseXML.getElementsByTagName("itdOdv")[0];
		var odvName = pmRequest.responseXML.getElementsByTagName("itdOdvName")[0];
		var odvPlace = pmRequest.responseXML.getElementsByTagName("itdOdvPlace")[0];
		if(usage) {
			var message = "";
			if(odvName.getAttribute("state") == "identified") {
				//message += odvPlace.getElementsByTagName("odvPlaceElem")[0].firstChild.nodeValue + " / " + odvName.getElementsByTagName("odvNameElem")[0].firstChild.nodeValue;
				message = odvName.getElementsByTagName("odvNameElem")[0].firstChild.nodeValue;
				document.getElementById("ajo_" + usage).style.display = "";
			}
			else if(odvName.getAttribute("state") == "list") {
				var list = odvName.getElementsByTagName("odvNameElem");
				if(list.length > 1) {
					message += "(" + list.length + " Ergebnisse)";
				}
				else {
					for(var i=0; i<list.length; i++) {
						message += list[i].firstChild.nodeValue;
					}
				}
				document.getElementById("ajo_" + usage).style.display = "";
			}
			else if(odvPlace.getAttribute("state") == "list") {
				message = "&nbsp;";
			}
			else {
				message = "&nbsp;";
			}
			document.getElementById("ajo_" + usage).innerHTML = message;
		}
	}
}

