/*
ajax.js - adaptiert durch Tim Reeves aus Internet-Pro 11/05,

Ursprunglich programmiert von Marco Zierl. Stand: 2010-10-09

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/products/firefox/';
	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 TO POPULATE THE EMAIL-ADRESSES ON PAGE LOADING

function onMouseOverTip(e) {
	if (typeof window.g_loading == 'boolean') showtip(this, 0, 0, 0, this.rel);
};
function onMouseOutTip(e) {
	if (typeof window.g_loading == 'boolean') showtip(this, 0, 0, 0, '');
};

function onloadData()
{
	var xmlDocument = window.g_xmlhttp.responseXML;

	// Empty text values mean there is no child node - catch them
	// alert(window.g_xmlhttp.responseText);

	// Parse the xml answer via DOM techniques
	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;
		title = xmlDocument.getElementsByTagName("email")[i].childNodes[3].firstChild.data;
		subject = xmlDocument.getElementsByTagName("email")[i].childNodes[4].firstChild.data;
		href = subject == '' ? 'mailto:' + link
							 : 'mailto:' + link + '?subject=' + subject;
		// alert(link);
		// Zunächst den Link vorbereiten
		textObj = document.createTextNode(link);
		linkObj = document.createElement('a');
		linkObj.setAttribute('href', href);
		if (title != '' && title != ' ' && title != '&nbsp;' && title != 'notip') {
			linkObj.setAttribute('rel', title);
			linkObj.onmouseover = onMouseOverTip;
			linkObj.onmouseout = onMouseOutTip;
		}
		// Nun Grafik aushängen und Link einhängen
		spanObj = document.getElementById('email' + nr);
		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;
}

// This is called at the <body onload> event of EVERY page
function ajaxOnload() {
	checkBrowser();
	ajaxProbe();
	if (window.g_loading || !window.g_has_xhr) return;
	// Get a list of all the Email Numbers on the page
	// They are the content of span-elements with id's like "email<n>"
	var emails = '', curid = '', i;
	for (i=1; true; i++) {
		curid = 'email' + i;
		if (document.getElementById(curid)) {
			emails += (i == 1) ? '?' : '&';
			emails += curid + '=' + document.getElementById(curid).firstChild.id;
			}
		else break;
	}
	// Call "onload.php" on the server to return the email
	// values and insert them into the displayed page
	window.g_xmlhttp.open("GET", window.g_pathPrefix + 'ajax/onload.php' + emails, 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;
}

// This is called by startup.js / startUp() ONLY at the start of each PHP-Session
function ajaxStartUp() {
	checkBrowser();
	ajaxProbe();
	if (window.g_loading || !window.g_has_xhr) return;
	window.g_xmlhttp.open("GET", window.g_pathPrefix + 'ajax/startup.php', true);
	window.g_xmlhttp.onreadystatechange = onStartupHttpState;
	window.g_xmlhttp.send(null);
	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;
}


