/*
ajax.js - adaptiert durch Tim Reeves aus Internet-Pro 11/05,

Hauser:
Stand: 2009-10-02 (mit neuen BuchbarkeitsIcons)
Stand: 2009-11-07 (Hauser 2010)
Stand: 2009-11-25 (mit Schnellsuch-Felder Aktivität + Reisenummer)
Stand: 2010-10-09 (refitted into prototype, utf-8)
Stand: 2010-10-12 aktiv options now created in php, here only set current
Stand: 2010-11-15 escape() => encodeURIComponent()
Stand: 2010-12-17 Legacy $_SESSION['onload-referrer'] entfernt
Stand: 2011-04-14 title-Attribute in img-Tag has precedence as vis. eMail text
Stand: 2011-04-26 Get Firefox now http://www.mozilla-europe.org/de/
Stand: 2011-05-19 E-Mails: Use id-Attr of img-Tag if set, otherwise alt-Attr
Stand: 2011-05-28 E-Mails: IE6/7 return "" on a non-existent title attribute
Stand: 2011-05-29 Updated some varibale ids (no "f_" any more)
Stand: 2011-06-15 Added floating tip to Ampel-Img
Stand: 2011-06-20 Added BiR-Improvements on selected options
Stand: 2011-06-29 Added email icon for span class reisespezialist
Stand: 2011-07-04 Bugfix for span class reisespezialist
Stand: 2011-12-22 Compensate IE-Bug bei AnzTouren

THE BASIS OF ALL AJAX

window.g_loading strategy:
- Declared true at start of ajax.js (some functions exit when false)
- Declared true at start of colorflow.js (which may not be included)
- Set false at end of colorflow.js (now flow events can be accepted)
- Set true in call of popups.js::closePops() (i.e. page is unloading)
- Set true in call of startup.js::unLoadPage() (not used, AdMuncher-Event)
- So now standard body onunload event (navi.inc.php) IS "window.g_loading=true;"
- Suppresses actual tip-flowing in startup.js::tipflow() if false
- In navi.inc.php all mouseover-events are directly protected by the
  following test: if (typeof window.g_loading == 'boolean')
*/

window.g_loading = true;

window.g_has_xhr = false;
window.g_xmlhttp = null;

// This version from Sitepoint 5.10.2010
// Native XMLHttpRequest object available in IE8 does not work
// at all for local files. Solution = instantiate ActiveX first.
function getXMLHttpRequest()
{
	var request = null;

	if (typeof window.ActiveXObject != "undefined") {
		try
		{
			// Some people test for multiple versions of XMLHTTP, to try
			// to select the most recent. In my view, it's completely
			// unnecessary to instantiate advanced versions of MSXML
			// unless you specifically need their advanced features.
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(err) { request = null; }
	}

	if (request === null && typeof window.XMLHttpRequest != "undefined") {
		try
		{
			request = new XMLHttpRequest();
		}
		catch(err) { request = null; }
	}

	return request;
}

function getFirefox() {
	var getFF = "Am Besten holen Sie den modernen Gratis-Browser \"Firefox\" \nper DownLoad. Wollen Sie jetzt Firefox herunterladen?"
	var ans = confirm(getFF);
	if (ans == true) location.href = 'http://www.mozilla-europe.org/de/';
	return;
}

function checkBrowser() {
	var agent=navigator.userAgent;
	if (agent.indexOf('Mac') != -1 && agent.indexOf('MSIE 5') != -1){
		// MSIE on Mac is a catastrophy - not supported
		alert("Sie verwenden den MSIE 5 - Mac Browser, mit\ndem diese Website nicht korrekt dargestellt wird.");
		getFirefox();
	}
	if (!document.getElementById){
		alert("Sie verwenden ein sehr veralteter Browser, mit\ndem diese Website nicht korrekt dargestellt wird.");
		getFirefox();
	}
	return;
}

function ajaxProbe() {
	window.g_xmlhttp = getXMLHttpRequest();
	if (window.g_xmlhttp)
		window.g_has_xhr = true;
	else
		alert("AJAX-Technologie nicht vorhanden");
	return;
}

// Standard functions to delete a subtree in DOM model
function delKids(prnt)
{
	// alert('delKids: ' + prnt.nodeName + ' / ' + prnt.nodeValue);
	var c = prnt.lastChild, p;
	while (c) {
		// Note previous while last is still there!
		p = c.previousSibling;
		delTree(prnt, c);
		c = p;
	}
	return;
}

function delTree(prnt, chld)
{
	// alert('delTree:: Parent: ' + prnt.nodeName + ' / ' + prnt.nodeValue +
	// 	  ' Child: ' + chld.nodeName + ' / ' + chld.nodeValue);
	var c = chld.lastChild, p;
	while (c) {
		p = c.previousSibling;
		delTree(chld, c);
		c = p;
	}
	prnt.removeChild(chld);
	return;
}

function debugToBreadcrumbs(txt) {
	var jetzt = new Date();
	var debugTxt = jetzt.getTime() + ' ' + txt;
	var objBreadcrumbs = document.getElementById('breadcrumbs');
	delKids(objBreadcrumbs);
	var objText = document.createTextNode(debugTxt);
	objBreadcrumbs.appendChild(objText);
	return;
}

// THE FUNCTIONS FOR UPDATING THE LAND LIST ON CONTINENT CHANGE

// Add a single option element to a select list
function add_option(obj, optext, optval, selval)
{
	// alert("add_element: " + optext);
	var newOption = new Option(optext, optval);
	var idx = obj.options.length;
	obj.options[idx] = newOption;
	obj.options[idx].selected = (optval == selval);
	return;
}

// Insert the number of tours which match the field values
function set_anztouren(anz)
{
	// Compensate bugs in IE <= 8
	// debugToBreadcrumbs('"' + anz + '"');
	var anztext = (anz == ' ' || anz == '') ? ' 0 ' : ' ' + anz + ' ';
	var spanObj = document.getElementById('sanztouren');
	if (spanObj.firstChild)
		spanObj.firstChild.nodeValue = anztext;
	else {
		var textObj = document.createTextNode(anztext);
		spanObj.appendChild(textObj);
	}
}

// Rebuild a complete list of select box options
// Called when continent changes for new list of lands
function optionData()
{
	var xmlDocument = window.g_xmlhttp.responseXML;
	var optval='', optext='', base=null, i=0;

	// Remove all old options from the list
	// showProps(target_select_obj.options);
	while (target_select_obj.options.length > 0) {
		if (target_select_obj.remove)
			target_select_obj.remove(0);
		else
			target_select_obj.options.remove(0);
	}
	// Add new options to list
	for (i=0; i< xmlDocument.getElementsByTagName("llopt").length; i++)
	{
		base = xmlDocument.getElementsByTagName("llopt")[i];
		optval = base.getElementsByTagName("optval")[0].childNodes[0].nodeValue;
		optext = base.getElementsByTagName("optext")[0].childNodes[0].nodeValue;
		// No option will be selected, display defaults to the first
		add_option(target_select_obj, optext, optval, '');
	}
	// Enable the listbox again if it has more than one option
	if (i>1) target_select_obj.disabled = false;

	// ANZAHL TOUREN
	if (xmlDocument.getElementsByTagName("anztouren")[0]) {
		// IE <=8 sets no childNode when the value is " "
		if (xmlDocument.getElementsByTagName("anztouren")[0].childNodes[0])
			set_anztouren(xmlDocument.getElementsByTagName("anztouren")[0].childNodes[0].nodeValue);
		else
			set_anztouren(' ');
	}

	// Remove any old message
	if (document.getElementById('toursuchemsg')) {
		var chld = document.getElementById('toursuchemsg');
		var prnt = chld.parentNode;
		delTree(prnt,chld);
		// IE positioning bug - button slips down, although posn "bottom"
		document.getElementById('panztouren').className = 'panztouren';
		document.getElementById('danztouren').className = 'danztouren';
		}

	return;
}

function optionsHttpState()
{
	if (window.g_xmlhttp.readyState == 4)
	{
		if (window.g_xmlhttp.status == 200) {
			// Daten erfolgreich geladen: starte Verarbeitung
			optionData();
		} else {
			alert("Fehler beim Abrufen der Option-XML Daten - Status " + window.g_xmlhttp.status);
			alert("Statusmeldung vom Server: " + window.g_xmlhttp.statusText);
			// alert("Debug: Url=" + document.URL + " Prefix=" + window.g_pathPrefix);
		}
	}
	return;
}

function get_select_data(select_name, url, selected_id)
{
	// select_name = name of target dropdown to get new option list
	// url         = url to call which should deliver the new option data
	// selected_id = name of dropdown calling this function - delivers index
	// UND ZWAR: get_select_data('f_land','get_options.php?typ=laender','f_ziel');

	// var debugTxt = "get_select_data: " + select_name + " / " + url + " / " + selected_id;
	// debugToBreadcrumbs(debugTxt);

	// Do nothing if AJAX technology not available
	if (window.g_loading) return;

	if (select_name != "") {
		target_select_obj = eval("document.forms['toursuche']." + select_name);
		// Disable the target dropdown
		target_select_obj.disabled = true;
	}
	else
		target_select_obj = null;

	if (selected_id != ""){
		url += "&sel=" + eval("document.forms['toursuche']." + selected_id + ".value");
	}
	// Get the new option list via AJAX
	if (window.g_xmlhttp) {
		// window.g_xmlhttp.open("GET", window.g_pathPrefix + "cgi-bin/" + url, true );	// Fails at Strato
		// window.g_xmlhttp.open("GET", "asien.xml", true );				 		// Works at Strato
		// 3rd parameter is true for asynchronous, false for synchronous
		window.g_xmlhttp.open("GET", window.g_pathPrefix + 'scripts/' + url, true);
		// alert("XMLHttpObject geöffnet");
		window.g_xmlhttp.onreadystatechange = optionsHttpState;
		window.g_xmlhttp.send(null);
		}
	else {
		alert("XMLHttpObject fehlt!");
	}
	return;
}


// THE FUNCTIONS TO POPULATE THE FORM FIELDS AND ICONS ON PAGE LOADING

function onMouseOverTip(e) {
	if (typeof window.g_loading == 'boolean') showtip(this, 0, 0, 0, this.rel);
};
function onMouseNameTip(e) {
	if (typeof window.g_loading == 'boolean') showtip(this, 0, 0, 0, this.name);
};
function onMouseOutTip(e) {
	if (typeof window.g_loading == 'boolean') showtip(this, 0, 0, 0, '');
};

function setSelectedIndex(s, v) {
	for ( var i = 0; i < s.options.length; i++ ) {
		if ( s.options[i].value == v ) {
			// s.options[i].selected = true;
			s.selectedIndex = i;
			return;
		}
	}
}

function onloadData()
{
	var xmlDocument = window.g_xmlhttp.responseXML;

	// Empty text values mean there is no child node - catch them
	// alert(window.g_xmlhttp.responseText);

	var optval='', optext='', welches='', i=0, obj=null;

	// DIE VOLLTEXT-SUCHE
	if (xmlDocument.getElementsByTagName("vtsuche")[0] && document.forms['vtsuche'])
	{
		if (xmlDocument.getElementsByTagName("vtsuche")[0].childNodes[0])
			document.forms['vtsuche'].f_vtsuche.value =
				xmlDocument.getElementsByTagName("vtsuche")[0].childNodes[0].nodeValue;
		else
			document.forms['vtsuche'].f_vtsuche.value = ' ';
	}

	/**************************/
	/* DIE REISE-SCHNELLSUCHE */
	/**************************/

	// AKTUELLER KONTINENT
	obj = document.forms['toursuche'].f_ziel;
	welches = xmlDocument.getElementsByTagName("ziel")[0].childNodes[0].nodeValue;
	setSelectedIndex(obj, welches);

	// DIE LÄNDERLISTE
	var land_list_obj = document.forms['toursuche'].f_land;
	welches = xmlDocument.getElementsByTagName("land")[0].childNodes[0].nodeValue;
	// Remove all old options from the list
	// showProps(target_select_obj.options);
	while (land_list_obj.options.length > 0) {
		if (land_list_obj.remove)
			land_list_obj.remove(0);
		else
			land_list_obj.options.remove(0);
	}
	// Add new options to the list
	for (i=0; i< xmlDocument.getElementsByTagName("llopt").length; i++)
	{
		obj = xmlDocument.getElementsByTagName("llopt")[i];
		optval = obj.getElementsByTagName("optval")[0].childNodes[0].nodeValue;
		optext = obj.getElementsByTagName("optext")[0].childNodes[0].nodeValue;
		add_option(land_list_obj, optext, optval, welches);
	}
	// Enable the listbox if it has more than one option
	land_list_obj.disabled = (i>1) ? false : true;

	// MAX. PREIS
	obj = document.forms['toursuche'].f_maxpreis;
	welches = xmlDocument.getElementsByTagName('maxpreis')[0].childNodes[0].nodeValue;
	setSelectedIndex(obj, welches);

	// SCHWIERIGKEITSGRAD
	obj = document.forms['toursuche'].f_schwierig;
	welches = xmlDocument.getElementsByTagName("schwierig")[0].childNodes[0].nodeValue;
	setSelectedIndex(obj, welches);

	// AKTIVITÄT
	var aktiv_list_obj = document.forms['toursuche'].f_aktiv;
	welches = xmlDocument.getElementsByTagName("aktiv")[0].childNodes[0].nodeValue;
	setSelectedIndex(obj, welches);

	// REISENUMMER
	if (xmlDocument.getElementsByTagName("reisenr")[0])
		document.forms['toursuche'].f_reisenr.value =
			xmlDocument.getElementsByTagName("reisenr")[0].childNodes[0].nodeValue;

	// FRÜHESTER ABREISE
	if (xmlDocument.getElementsByTagName("abdatum")[0])
		document.forms['toursuche'].f_abdatum.value =
			xmlDocument.getElementsByTagName("abdatum")[0].childNodes[0].nodeValue;

	// SPÄTESTER RÜCKREISE
	if (xmlDocument.getElementsByTagName("rueckdatum")[0])
		document.forms['toursuche'].f_rueckdatum.value =
			xmlDocument.getElementsByTagName("rueckdatum")[0].childNodes[0].nodeValue;

	// ANZAHL TOUREN
	if (xmlDocument.getElementsByTagName("anztouren")[0]) {
		// IE <=8 sets no childNode when the value is " "
		if (xmlDocument.getElementsByTagName("anztouren")[0].childNodes[0])
			set_anztouren(xmlDocument.getElementsByTagName("anztouren")[0].childNodes[0].nodeValue);
		else
			set_anztouren(' ');
	}

	/**************************/
	/* END REISE-SCHNELLSUCHE */
	/**************************/

	// REISELEITER VOR- UND NACHNAME (falls Seite mit RL-Suche)
	if (document.forms['leitersuche']) {
		if (xmlDocument.getElementsByTagName("rlvorname")[0] &&
			xmlDocument.getElementsByTagName("rlvorname")[0].childNodes[0])
			document.forms['leitersuche'].f_rlvorname.value =
				xmlDocument.getElementsByTagName("rlvorname")[0].childNodes[0].nodeValue;
		if (xmlDocument.getElementsByTagName("rlnachname")[0] &&
			xmlDocument.getElementsByTagName("rlnachname")[0].childNodes[0])
			document.forms['leitersuche'].f_rlnachname.value =
				xmlDocument.getElementsByTagName("rlnachname")[0].childNodes[0].nodeValue;
	}

	// MITARBEITER VOR- UND NACHNAME (falls Seite mit Team-Suche)
	if (document.forms['teamsuche']) {
		if (xmlDocument.getElementsByTagName("htvorname")[0] &&
			xmlDocument.getElementsByTagName("htvorname")[0].childNodes[0])
			document.forms['teamsuche'].f_htvorname.value =
				xmlDocument.getElementsByTagName("htvorname")[0].childNodes[0].nodeValue;
		if (xmlDocument.getElementsByTagName("htnachname")[0] &&
			xmlDocument.getElementsByTagName("htnachname")[0].childNodes[0])
			document.forms['teamsuche'].f_htnachname.value =
				xmlDocument.getElementsByTagName("htnachname")[0].childNodes[0].nodeValue;
	}

	// ALLE BUCHBARKEITSICONS EINSETZEN
	var rsnr = '', bbar = '', icon = '', text = '', obja;
	var anz = xmlDocument.getElementsByTagName("reise").length;
	// alert(anz + ' Touren gefunden auf der Seite');
	for (i=0; i<anz; i++)
	{
		rsnr = xmlDocument.getElementsByTagName("reise")[i].childNodes[0].firstChild.data;
		bbar = xmlDocument.getElementsByTagName("reise")[i].childNodes[1].firstChild.data;
		icon = xmlDocument.getElementsByTagName("reise")[i].childNodes[2].firstChild.data;
		text = xmlDocument.getElementsByTagName("reise")[i].childNodes[3].firstChild.data;
		// alert(rsnr + ': ' + bbar + '/' + icon + '/' + text);
		obja = document.getElementById('ampel' + (i+1));
		obja.src = window.g_pathPrefix + 'images/' + icon;
		obja.setAttribute('name', text);
		obja.onmouseover = onMouseNameTip;
		obja.onmouseout = onMouseOutTip;
		// Nicht-buchbare Touren dürfen keinen "Buchen" Verweis haben
		// - entfernen falls da
		if (bbar == 'N' && obja.nextSibling) {
			var chld = obja.nextSibling;	// Probably a junk text node
			if (chld.nodeName != 'A') chld = chld.nextSibling;
			var prnt = obja.parentNode;
			delTree(prnt,chld);
		}
	}

	// ETWAIGE EMAIL-ADRESSEN EINSETZEN
	var nr = '', link='', title='', subject='', href='';
	var textObj=null, linkObj=null, spanObj=null;
	var anz = xmlDocument.getElementsByTagName("email").length;
	for (var i=0; i<anz; i++)
	{
		// Hier zählt 'i' lediglich durch die Antworten, doch
		// <nr> bezieht sich korrekterweise auf die Anfrage!!
		nr = xmlDocument.getElementsByTagName("email")[i].childNodes[0].firstChild.data;
		// childNodes[1] = ID => not needed (but good for debugging :)
		link = xmlDocument.getElementsByTagName("email")[i].childNodes[2].firstChild.data;
		show = xmlDocument.getElementsByTagName("email")[i].childNodes[3].firstChild.data;
		title = xmlDocument.getElementsByTagName("email")[i].childNodes[4].firstChild.data;
		subject = xmlDocument.getElementsByTagName("email")[i].childNodes[5].firstChild.data;
		href = subject == '' ? 'mailto:' + link
							 : 'mailto:' + link + '?subject=' + subject;
		// A title-Attribute in the img-Tag has precedence as visible link text
		spanObj = document.getElementById('email' + nr);
		if (spanObj.className == 'reisespezialist') {
			// Display an Icon rather than the "show" text
			// div.nochmehr enthält: <p>(E-Mail) {<p>(PDF)}
			// Here we must replace the 1st <p>, leaving any 2nd <p>
			var withPDF = false;
			var parObj = spanObj.parentNode;
			var divObj = parObj.parentNode;
			imgObj = document.createElement('img');
			imgObj.src = window.g_pathPrefix + 'images/umschlag.gif';
			imgObj.width = 32;
			imgObj.height = 32;
			imgObj.className = 'reisespezialist';
			imgObj.alt = '';
			linkObj = document.createElement('a');
			linkObj.setAttribute('href', href);
			title = 'E-Mail an den zuständigen Reisespezialist senden';
			linkObj.setAttribute('rel', title);
			linkObj.onmouseover = onMouseOverTip;
			linkObj.onmouseout = onMouseOutTip;
			// Nun alte Knoten aushängen und Link + Grafik einhängen
			divObj.replaceChild(linkObj, parObj);	// new, old
			// MSIE mag es nicht wenn der Textknoten vor dem Einhängen angehängt wird
			linkObj.appendChild(imgObj);
		}
		else {
			imgObj = spanObj.firstChild;
			imgTitle = imgObj.getAttribute('title');
			if (imgTitle != null && imgTitle != '') show = imgTitle;
			// alert(show + ' / ' + link);
			// Prepare the new link object
			textObj = document.createTextNode(show);
			linkObj = document.createElement('a');
			linkObj.setAttribute('href',href);
			if (title != '' && title != ' ' && title != '&nbsp;') {
				linkObj.setAttribute('rel', title);
				linkObj.onmouseover = onMouseOverTip;
				linkObj.onmouseout = onMouseOutTip;
			}
			// Nun Grafik aushängen und Link einhängen
			delKids(spanObj);
			spanObj.appendChild(linkObj);
			// MSIE mag es nicht wenn der Textknoten vor dem Einhängen angehängt wird
			spanObj.childNodes[0].appendChild(textObj);
		}
	}

	return;
}

function onloadHttpState()
{
	if (window.g_xmlhttp.readyState == 4)
	{
		if ( window.g_xmlhttp.status == 200) {
			// Daten erfolgreich geladen: starte Verarbeitung
			onloadData();
		} else {
			alert("Fehler beim Abrufen der Onload-XML Daten - Status " + window.g_xmlhttp.status);
			alert("Statusmeldung vom Server: " + window.g_xmlhttp.statusText);
		}
	}
	return;
}

// Hauser: This is called at the <body onload> event of EVERY page
function ajaxOnload() {
	checkBrowser();
	ajaxProbe();
	if (window.g_loading || !window.g_has_xhr) return;
	var curid='', touren='', emails='', sep='?', i, id, obj;
	// If page contains Reiseleitersuche then note initial values
	obj = document.getElementById('s_rlvorname');
	if (obj) { window.rlvorname = obj.value; }
	obj = document.getElementById('s_rlnachname');
	if (obj) { window.rlnachname = obj.value; }
	// Legacy - to be removed
	obj = document.getElementById('f_rlvorname');
	if (obj) { window.rlvorname = obj.value; }
	obj = document.getElementById('f_rlnachname');
	if (obj) { window.rlnachname = obj.value; }
	// Get a list of all the Tour Numbers on the page
	// They are the text content of cells with id's like "tournr<n>"
	for (i=1; true; i++) {
		curid = 'tournr' + i;
		obj = document.getElementById(curid);
		if (obj && document.getElementById('ampel' + i)) {
			touren += sep + curid + '=' + obj.firstChild.nodeValue;
			sep = '&';
		}
		else break;
	}
	// Get a list of all the Email Numbers on the page
	// They are the content of span-elements with id's like "email<n>"
	for (i=1; true; i++) {
		curid = 'email' + i;
		obj = document.getElementById(curid);
		if (obj) {
			// Use id-Attr of img-Tag if set (legacy convention), otherwise alt-Attr
			// alt-Attr is better because it allows the same value more than once
			id = obj.firstChild.id;
			if (!id || id == '') id = obj.firstChild.alt;
			emails += sep + curid + '=' + id;
			sep = '&';
		}
		else break;
	}
	// Call "onload.php" on the server to return the current values
	// and insert these values into the displayed page
	var url = window.g_pathPrefix + 'ajax/onload.php' + touren + emails;
	// alert(url);
	// debugToBreadcrumbs(url);
	window.g_xmlhttp.open("GET", url, true);
	window.g_xmlhttp.onreadystatechange = onloadHttpState;
	window.g_xmlhttp.send(null);
	return;
}

function onStartupHttpState()
{
	if (window.g_xmlhttp.readyState == 4)
	{
		if ( window.g_xmlhttp.status == 200) {
			// do nothing - the fact of JavaScript is noted in the session
			// alert("JavaScript OK");
		} else {
			alert("Fehler beim Abrufen der Startup-XML Daten - Status " + window.g_xmlhttp.status);
			alert("Statusmeldung vom Server: " + window.g_xmlhttp.statusText);
		}
	}
	return;
}


function dummyHttp() { return; }


// This is called by logingeneral.php.inc::writeShowFileLink(),
// it has already encoded the "target" parameter for direct use
function ajaxCount(target) {
	if (!window.g_has_xhr) ajaxProbe();
	if (!window.g_has_xhr || window.g_loading) return;
	var url = window.g_pathPrefix + 'ajax/webcount.php?tgt=' + target;
	// alert('ajaxCount: ' + url);
	window.g_xmlhttp.open("GET", url, true);
	window.g_xmlhttp.onreadystatechange = dummyHttp;
	window.g_xmlhttp.send(null);
	return;
}


window.g_winSizeTimerHandle = null;

// This is a sub-function to ajaxWindowSize()
function reportWinSizeViaXhr(strUrl, strValue) {
	window.g_xmlhttp.open("GET", strUrl, true);
	window.g_xmlhttp.onreadystatechange = dummyHttp;
	window.g_xmlhttp.send(null);
	window.g_win_size = strValue;
	window.g_winSizeTimerHandle = null;
	return;
}

// This is called by startup.js::neuAufbau() to report current sizes to server
function ajaxWindowSize(strParam, strValue) {
	if (!window.g_has_xhr) ajaxProbe();
	if (!window.g_has_xhr || window.g_loading) return;
	// Don't report if the current value matches the one at the server
	if (window.g_win_size == strValue) return;
	// Put the URL for XHR together
	var strUrl = window.g_pathPrefix + 'ajax/windowsize.php?' + strParam + '=' + strValue;
	// alert('ajaxWindowSize: ' + strUrl);
	if (window.g_reportWindowSize) {
		// Someones first call of the website
		reportWinSizeViaXhr(strUrl, strValue);
		window.g_reportWindowSize = false;
	}
	else {
		// Report via Timer to avoid multiple XHR during resizing
		var strCall = "reportWinSizeViaXhr('" + strUrl + "','" + strValue + "')";
		// Clear any superceded outstanding call
		if (window.g_winSizeTimerHandle != null)
			window.clearTimeout(window.g_winSizeTimerHandle);
		window.g_winSizeTimerHandle = window.setTimeout(strCall, 500);
	}
	return;
}

// THE FUNCTIONS TO UPDATE SERVER SESSION VARS ON SINGLE FIELD CHANGE

/*  Whenever the user changes a field value (in either form) then the
	new value is sent to the server and put in the session variables -
	which involves getting at least a small return document from the server.
	However, when the form is submitted this action is not required - in fact,
	a document would be returned to a page which no longer exists, so we want
	to suppress the onChange action when the event which triggers the onChange
	is the form submission. This in turn is caused by EITHER
	- the user is in a text input field and hits ENTER, or
	- he or she clicks on the submit button.
	In both cases the onChange happens BEFORE the onSubmit, so we have to
	add a small delay to the onChange action, and then not do it if we find
	we are at that time submitting the form.
	Note that onChange is triggered by onMouseDown on the submit button -
	it'll come up again and be a click soon enough! If we would wait for the
	onClick event we would have to wait too long - an indefinite period as the
	user may "sit" on the mouse button!
*/

var nowSubmitting = '';

function setNowSubmitting(typ) {
	// Note a form submission to suppress the onchange action - see neuWert()
	window.nowSubmitting = typ;
	// alert("setnowSubmitting: " + typ);
	return true;
}

// Decode the answer to a changed field value in the "Schnellsuche"
function tourData()
{
	// alert(window.g_xmlhttp.responseText);
	var xmlDocument = window.g_xmlhttp.responseXML;
	// Check for a modified value
	if (xmlDocument.getElementsByTagName("FLD")[0]) {
		// This can currently only be a normalised or rejected date
		var fld='', val='';
		fld = xmlDocument.getElementsByTagName("FLD")[0].childNodes[0].nodeValue;
		val = xmlDocument.getElementsByTagName("VAL")[0].childNodes[0].nodeValue;
		document.getElementById(fld).value = val;
		}
	// ANZAHL TOUREN - Insert the number of matching tours behind the button
	if (xmlDocument.getElementsByTagName("anztouren")[0])
	   set_anztouren(xmlDocument.getElementsByTagName("anztouren")[0].childNodes[0].nodeValue);
	// Display any message above the Schnellsuche
	if (xmlDocument.getElementsByTagName("MSG")[0]) {
		var msg='';
		msg = xmlDocument.getElementsByTagName("MSG")[0].childNodes[0].nodeValue;
		// Insert the message between div.schnellsuche and form-id 'toursuche'
		if (document.getElementById('toursuchemsg')) {
			var chld = document.getElementById('toursuchemsg');
			chld.firstChild.data = msg;
		}
		else {
			var myNode = document.createElement("p");
			// Give it the id attribute
			myNode.setAttribute('id', 'toursuchemsg');
			// Append the text node for the link
			var myText = document.createTextNode(msg);
			myNode.appendChild(myText);
			// Insert it into the DOM before the form
			var sblg = document.getElementById('toursuche');
			var prnt = sblg.parentNode;
			prnt.insertBefore(myNode,sblg);
			// IE positioning bug - button slips up, although posn "bottom"
			document.getElementById('panztouren').className = 'panztouren';
			document.getElementById('danztouren').className = 'danztouren';
		}
	}
	else {
		// Remove any old message
		if (document.getElementById('toursuchemsg')) {
			var chld = document.getElementById('toursuchemsg');
			var prnt = chld.parentNode;
			delTree(prnt,chld);
			// IE positioning bug - button slips down, although posn "bottom"
			document.getElementById('panztouren').className = 'panztouren';
			document.getElementById('danztouren').className = 'danztouren';
		}
	}
	return;
}	// tourData()

// Preview of the matching Reiseleiter / Mitarbeiter as user types name in
function persData()
{
	// alert(window.g_xmlhttp.responseText);
	var xmlDocument = window.g_xmlhttp.responseXML;
	// We only get search results if there is at least one input char
	if (xmlDocument.getElementsByTagName("zuwenigzeichen")[0]) {
		// Make the result panel invisible
		document.getElementById('persergebnisse').style.display="none";
		// And the standard panel visible
		document.getElementById('col2').style.display="block";
		return;
	}
	// Prepare the content in the result panel
	var persname='', persfile='', i=0, obj=null;
	var myNode=null, myText=null;
	var hitliste = document.getElementById('hitliste');
	var anzhits = xmlDocument.getElementsByTagName("pers").length;
	if (document.getElementById('f_htvorname')) {
		var vn = 'f_htvorname';
		var nn = 'f_htnachname';
	}
	else {
		if (document.getElementById('f_rlvorname')) {	// Legacy
			var vn = 'f_rlvorname';
			var nn = 'f_rlnachname';
		}
		else {
			var vn = 's_rlvorname';
			var nn = 's_rlnachname';
		}
	}
	if (anzhits == 0) {
		document.getElementById(vn).style.backgroundColor = '#FFE4E4';
		document.getElementById(nn).style.backgroundColor = '#FFE4E4';
	}
	else if (anzhits == 1) {
		document.getElementById(vn).style.backgroundColor = '#CDEFC7';
		document.getElementById(nn).style.backgroundColor = '#CDEFC7';
	}
	else {
		document.getElementById(vn).style.backgroundColor = '';
		document.getElementById(nn).style.backgroundColor = '';
	}
	// First delete the previous content
	delKids(hitliste);
	// Now add a link for each person found
	for (i=0; i<anzhits; i++)
	{
		obj = xmlDocument.getElementsByTagName("pers")[i];
		persname = obj.getElementsByTagName("name")[0].childNodes[0].nodeValue;
		persfile = obj.getElementsByTagName("file")[0].childNodes[0].nodeValue;
		if (i>0) {
			// Add a <br /> Element so each Person on a new line
			myNode = document.createElement("br");
			hitliste.appendChild(myNode);
		}
		myText = document.createTextNode(persname);
		if (persfile == '-')
			hitliste.appendChild(myText);
		else {
			// Append the link node
			myNode = document.createElement("a");
			// Give it the href attribute
			myNode.setAttribute('href', persfile);
			// The following is more puristic for attributes, but IE5.5 can't
			// myAttr = document.createAttribute("href");
			// myAttr.nodeValue = persfile + persFileExt;
			// myNode.setAttributeNode(myAttr);
			// Append the text node for the link
			myNode.appendChild(myText);
			// Append it to the results
			hitliste.appendChild(myNode);
		}
	}
	if (i == 0) {
		myText = document.createTextNode('Keine Treffer.');
		hitliste.appendChild(myText);
	}
	// Make the standard panel invisible
	document.getElementById('col2').style.display = "none";
	// and the result panel visible
	document.getElementById('persergebnisse').style.display = "block";
	return;
}

function ajaxFehler()
{
	alert("Fehler beim Notieren des neuen Feldwertes - Status " + window.g_xmlhttp.status);
	alert("Statusmeldung vom Server: " + window.g_xmlhttp.statusText);
	return;
}

function tourWertHttpState()
{
	if (window.g_xmlhttp.readyState == 4)
	{
		if ( window.g_xmlhttp.status == 200) {
			// daten erfolgreich geladen: starte Verarbeitung
			tourData();
		}
		else ajaxFehler();
	}
	return;
}

function vtsWertHttpState()
{
	if (window.g_xmlhttp.readyState == 4)
	{
		if ( window.g_xmlhttp.status == 200) {
			// Antwort erfolgreich - es gibt jedoch keine lokale Verarbeitung
			return;
		}
		else ajaxFehler();
	}
	return;
}

function persWertHttpState()
{
	if (window.g_xmlhttp.readyState == 4)
	{
		if ( window.g_xmlhttp.status == 200) {
			// daten erfolgreich geladen: starte Verarbeitung
			persData();
		}
		else ajaxFehler();
	}
	return;
}

function neuWert(typ,fld) {
	// Pass a new form field value to it's session variable via AJAX
	// But don't do it if the form is being submitted anyway
	// Since onchange() and onkeyup() are called BEFORE onsubmit(), we must wait...
	// Bekannte Typen: 'v' => Volltextsuche; 't' => Toursuche; 'r' => Reiseleiter
	//                 'm' => Mitarbeiter
	// var debugTxt = "neuWert: " + typ + " für " + fld.name + ", nowSubmitting=" + window.nowSubmitting;
	// alert(debugTxt);		// Triggers onBlur
	// debugToBreadcrumbs(debugTxt);
	if (window.g_loading) return;
	// When reacting on field input: Quietly do nothing if the value is unchanged
	// I mean onkeyup - we react to every change while in the field
	if (typ == 'r') {
		if (fld.id == 's_rlvorname' || fld.id == 'f_rlvorname') {
			if (window.rlvorname && window.rlvorname == fld.value) return;
			window.rlvorname = fld.value;
		}
		else {
			if (window.rlnachname && window.rlnachname == fld.value) return;
			window.rlnachname = fld.value;
		}
	}
	// We can't pass an object to setTimeout(), so pass the id
	var aufruf = "neuWertWaited('" + typ + "','" + fld.id + "')";
	window.setTimeout(aufruf,5);
	return;
}

function neuWertWaited(typ,fldid) {
	// alert("neuWertWaited: " + typ + " für " + fldid + ", nowSubmitting=" + window.nowSubmitting);
	if (typ == window.nowSubmitting || window.g_loading) return;
	window.g_xmlhttp = getXMLHttpRequest();
	// Call "set_formval.php" on the server to note new field value
	// Note that the response can be ignored for a VollTextSuche
	var fld = document.getElementById(fldid);
	var url = window.g_pathPrefix + 'scripts/set_formval.php?fld=' +
			  fldid + '&val=' + encodeURIComponent(fld.value);
	window.g_xmlhttp.open("GET", url, true);
	if (fldid == 's_rlvorname' || fldid == 's_rlnachname' ||
		fldid == 'f_rlvorname' || fldid == 'f_rlnachname' ||
		fldid == 'f_htvorname' || fldid == 'f_htnachname')
		// Send a request for Reiseleiter- or HauserTeam-Felder
		window.g_xmlhttp.onreadystatechange = persWertHttpState;
	else
		// A request for Volltext- oder Toursuche
		window.g_xmlhttp.onreadystatechange = fld.name == "f_vtsuche" ? vtsWertHttpState : tourWertHttpState;
	window.g_xmlhttp.send(null);
	return;
}

function reiseDaten() {

	var xmlDocument = window.g_xmlhttp.responseXML;
	// alert(window.g_xmlhttp.responseText);

	var rn = xmlDocument.getElementsByTagName('rn')[0].firstChild.data;
	var titel = xmlDocument.getElementsByTagName('titel')[0].firstChild.data;
	var spanObj = document.getElementById('reiseShow');
	var textObj = document.createTextNode(titel);
	var obj = document.getElementById('euro');
	var rno = document.getElementById('reisenummer');
	var land = '', von = '', bis = '', preis = '';
	delKids(spanObj);

	land = xmlDocument.getElementsByTagName('land')[0].firstChild.data;
	if (land == '-') {
		// We got no data
		land = '';
		spanObj.appendChild(textObj);
		obj.style.visibility = "hidden";
		rno.className = 'myField fehlerfarbe';
	}
	else {
		// Link einsetzen
		linkObj = document.createElement('a');
		linkObj.setAttribute('href',xmlDocument.getElementsByTagName('file')[0].firstChild.data);
		linkObj.setAttribute('title','Zur Reisebeschreibung');
		spanObj.appendChild(linkObj);
		// MSIE mag es nicht wenn der Textknoten vor dem Einhängen angehängt wird
		spanObj.childNodes[0].appendChild(textObj);
		obj.style.visibility = "visible";
		rno.className = 'myField weissefarbe';
		von = xmlDocument.getElementsByTagName('von')[0].firstChild.data;
		bis = xmlDocument.getElementsByTagName('bis')[0].firstChild.data;
		preis = xmlDocument.getElementsByTagName('preis')[0].firstChild.data + ',-';
	}
	obj = document.getElementById('reiseland');
	obj.value = land;
	obj = document.getElementById('reisevon');
	obj.value = von;
	obj = document.getElementById('reisebis');
	obj.value = bis;
	obj = document.getElementById('reisepreis');
	obj.value = preis;
	return;
}

function reiseDatenHttpState() {
	if (window.g_xmlhttp.readyState == 4) {
		if ( window.g_xmlhttp.status == 200) {
			// daten erfolgreich geladen: starte Verarbeitung
			reiseDaten();
		} else {
			alert("Fehler beim Abrufen der Reisedaten - Status " + window.g_xmlhttp.status);
			alert("Statusmeldung vom Server: " + window.g_xmlhttp.statusText);
		}
	}
	return;
}

// This is called at onBlur() of the reisenr-Field of the booking form
function neueReiseNr(val) {
	if (window.g_loading) return;
	window.g_xmlhttp = getXMLHttpRequest();
	// Call "reisedaten.php" on the server
	var url = '../scripts/reisedaten.php?rn=' + val;
	window.g_xmlhttp.open("GET", url, true);
	window.g_xmlhttp.onreadystatechange = reiseDatenHttpState;
	window.g_xmlhttp.send(null);
	return;
}

// This is a generic function to write an entry to the web log (zB onclick PDF-Anzeige)
function weblog(kuerzel,p1,p2) {
	if (window.g_loading) return;
	window.g_xmlhttp = getXMLHttpRequest();
	// Call "weblog.php" on the server
	var url = '../scripts/weblog.php?kl=' + kuerzel + '&p1=' + p1 + '&p2=' + p2;
	window.g_xmlhttp.open("GET", url, true);
	window.g_xmlhttp.onreadystatechange = dummyHttp;
	window.g_xmlhttp.send(null);
	return;
}


