// ajax.js - adaptiert durch Tim Reeves aus Internet-Pro 11/05,
// ursprunglich programmiert von Marco Zierl. Stand: 2006-09-05

// THE BASIS OF ALL AJAX

var loading = true;	// doppelt gemoppelt da zur Sicherheit in navi.inc.php
var xmlhttp = null;
var target_select_obj = null;

function getXMLHttpRequest() {
	// mochiKit does it more or less so...
    var tryThese = [
        function () { return new XMLHttpRequest(); },
        function () { return new ActiveXObject('Msxml2.XMLHTTP'); },
        function () { return new ActiveXObject('Microsoft.XMLHTTP'); },
        function () { return new ActiveXObject('Msxml2.XMLHTTP.4.0'); },
    ];
    for (var i = 0; i < tryThese.length; i++) {
        var func = tryThese[i];
        try {
            return func();
        } catch (e) {
            // pass
        }
    }
    return null;
}

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() {
	xmlhttp = getXMLHttpRequest();
	if (xmlhttp)
		loading = false;
	else
		alert("AJAX-Technologie nicht verfügbar");
	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;
}
// THE FUNCTIONS TO POPULATE THE EMAIL-ADRESSES ON PAGE LOADING

function onloadData()
{
	var xmlDocument = xmlhttp.responseXML;

	// Empty text values mean there is no child node - catch them
	// alert(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);
		linkObj.setAttribute('title',title);
		// 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 (xmlhttp.readyState == 4)
	{
		if ( xmlhttp.status == 200) {
			// daten erfolgreich geladen: starte Verarbeitung
			onloadData();
		} else {
			alert("Fehler beim Abrufen der Onload-XML Daten - Status " + xmlhttp.status);
			alert("Statusmeldung vom Server: " + xmlhttp.statusText);
		}
	}
	return;
}

// This is called at the <body onload> event
function ajaxOnload(pathprefix) {
	checkBrowser();
	ajaxProbe();
	if (loading) 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
	xmlhttp.open("GET", pathprefix + 'ajax/onload.php' + emails, true);
	xmlhttp.onreadystatechange = onloadHttpState;
	xmlhttp.send(null);
	return;
}

function onStartupHttpState()
{
	if (xmlhttp.readyState == 4)
	{
		if ( 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 " + xmlhttp.status);
			alert("Statusmeldung vom Server: " + xmlhttp.statusText);
		}
	}
	return;
}

// This is called by startup.js / startUp() at the start of each PHP-Session
function ajaxStartUp(pathprefix) {
	checkBrowser();
	ajaxProbe();
	if (loading) return;
	xmlhttp.open("GET", pathprefix + 'ajax/startup.php', true);
	xmlhttp.onreadystatechange = onStartupHttpState;
	xmlhttp.send(null);
	return;
}

