//<![CDATA[
// öäüß

var flag = 0;
var popupWin = null;
var win = null;

/************************************************************************************
 * behandleSubmitButtonsFuerSerialisierung()										*
 *																					*
 * Damit auch die Namen der Submit-Buttons mittels AJAX und der jQuery-Funktion		*
 * "serialize()" - wie bei POST und GET üblich - mit einem angehängen _x bzw. _y	*
 * übermittelt werden können (allerdings ohne dabei die echten Koorodinaten des		*
 * Mauszeigerklicks anzugeben), und somit auch bei AJAX-formsn der jeweils		*
 * geklickte, grafische Submit-Button abgefragt werden kann, wird diese Funktion in	*
 * enableAjaxClassForms() automatisch aufgerufen.									*
 ************************************************************************************/
function behandleSubmitButtonsFuerSerialisierung() {
	$(".submitbutton").click(function() {
		var submit_button_name = $(this).attr("name");
		var submit_button_value = $(this).attr("value");
		$(this).parent().append('<input type="hidden" name="' + submit_button_name + '_x" value="1" />');
		$(this).parent().append('<input type="hidden" name="' + submit_button_name + '_y" value="1" />');
		$(this).parent().append('<input type="hidden" name="' + submit_button_name + '" value="' + submit_button_value + '" />');
	});
}

/****************************************************************************************************************************
 * behandleAjaxFormulare()																										*
 *																															*
 * Alle einfachen AJAX-forms bekommen die Klasse ".ajax" und werden in eigene PHP-Skripte ausgelagert,						*
 * die allesamt im Verzeichnis /inc/forms/ liegen. Diese Formular-Skripte geben jeweils die kompletten						*
 * Formular-Inhalte innerhalb der <form></form>-Tags aus.																	*
 * Dies schließt vorerst sämtliche forms aus, in denen Dateien hochgeladen werden oder in denen es							*
 * unterschiedliche Submit-Buttons gibt, die unterschiedliche Bedeutungen haben.											*
 * Bei SUBMIT werden alle Elemente der Klasse ".ajax-loader", die sich innerhalb des formss befinden,						*
 * sichtbar gemacht, und bei fehlgeschlagener AJAX-Übermittlung wieder unsichtbar.											*
 * Damit diese Automatisierung möglich ist, muss jedes betreffende Formular als ID den Dateinamen (ohne die Dateiendung)	*
 * des zugehörigen Skriptes erhalten, da hieraus der Skriptname generiert wird, an welchen die AJAX-Daten gesendet werden.	*
 * Aufgerufen wird diese Funktion am Ende aller Seiten über die Datei /inc/footer.php.										*
 ****************************************************************************************************************************/
function behandleAjaxFormulare() {
	behandleSubmitButtonsFuerSerialisierung();

	$('form.ajax').submit(function() {
		formId = $(this).attr('id');
		paramString = $(this).serialize();
		$('#' + formId + ' .ajax-loader').show();

		$.ajax({
			type: 'POST',
			url: '/inc/forms/' + formId + '.php',
			data: paramString,
			success: function(data, textStatus) {
				$("form#" + formId).html(data);
				behandleSubmitButtonsFuerSerialisierung();
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$("form#" + formId).append("<div class=\"fehler\">Es tut uns leid, aber der Versand der Formulardaten ist fehlgeschlagen.<br />Bitte versuchen Sie es zu einem späteren Zeitpunkt noch einmal.</div>");
				$("form#" + formId).append("<div class=\"fehler\">We're really sorry, but submitting this form data failed.<br />Please try again later.</div>");
				$("form#" + formId + " .ajax-loader").hide();
			}
		});

		return false;
	});
}


/*  #####################
	# PREPARE FORMULAR: #
	#####################
*/
function prepareMailtoFormular() {
	if (!document.getElementById)
		return;
	if (!document.getElementById("kontaktformular"))
		return;

	document.getElementById("kontaktformular").onsubmit = function() {
		var data = "";

		for (var i = 0; i < this.elements.length; i++) {
			data += this.elements[i].name;
			data += "=";
			data += escape(this.elements[i].value);
			data += "&";
		}

		return !sendEmail(data);
	}
}

function prepareMailtoFormularGb() {
	if (!document.getElementById)
		return;
	if (!document.getElementById("kontaktformular"))
		return;

	document.getElementById("kontaktformular").onsubmit = function() {
		var data = "";

		for (var i = 0; i < this.elements.length; i++) {
			data += this.elements[i].name;
			data += "=";
			data += escape(this.elements[i].value);
			data += "&";
		}

		return !sendEmailGb(data);
	}
}

function prepareToggleAboFormular() {
	if (!document.getElementById)
		return;
	if (!document.getElementById("aboformular"))
		return;

	document.getElementById("aboformular").onsubmit = function() {
		var data = "";

		for (var i = 0; i < this.elements.length; i++) {
			data += this.elements[i].name;
			data += "=";
			data += escape(this.elements[i].value);
			data += "&";
		}

		return !toggleAbo(data);
	}
}

function prepareToggleAboFormularGb() {
	if (!document.getElementById)
		return;
	if (!document.getElementById("aboformular"))
		return;

	document.getElementById("aboformular").onsubmit = function() {
		var data = "";

		for (var i = 0; i < this.elements.length; i++) {
			data += this.elements[i].name;
			data += "=";
			data += escape(this.elements[i].value);
			data += "&";
		}

		return !toggleAboGb(data);
	}
}

/*  ################
	# SHOW STATUS: #
	################
*/
function showEmailStatus() {
	var messageBox 					= document.getElementById("ergebnismeldung");
	var fehlerbox_name				= document.getElementById("fehler_kontakt_name");
	var fehlerbox_email				= document.getElementById("fehler_kontakt_email");
	var fehlerbox_betreff			= document.getElementById("fehler_kontakt_betreff");
	var fehlerbox_nachricht			= document.getElementById("fehler_kontakt_nachricht");
	var tropfanimation				= document.createElement("img");
	tropfanimation.setAttribute("id", "tropfanimation");
	tropfanimation.setAttribute("src", "img/tropfen.gif");
	tropfanimation.setAttribute("alt", "Bitte warten - Animation");
	tropfanimation.setAttribute("width", "60");
	tropfanimation.setAttribute("height", "59");

	clearText(messageBox);
	var newNode = document.createTextNode("Ihre Nachricht wurde gesendet, warte auf Antwort...");
	messageBox.appendChild(tropfanimation);
	messageBox.appendChild(newNode);

	messageBox.className			= 'hinweis';
	messageBox.style.display		= 'block';

	fehlerbox_name.style.display		= 'none';
	fehlerbox_email.style.display		= 'none';
	fehlerbox_betreff.style.display		= 'none';
	fehlerbox_nachricht.style.display	= 'none';

	if (request.readyState == 4) {

		if (request.status == 200 || request.status == 304) {
			var kontaktformular			= document.getElementById("formular");
			kontaktformular.innerHTML	= request.responseText;
			prepareMailtoFormular();
		} else {
			replaceText(messageBox, "Fehler: Leider ist ein Server-Fehler aufgetreten: HTTP-Status " + request.status);
			messageBox.className			= 'fehler';
			messageBox.style.display		= 'block';
		}
	}
}

function showEmailStatusGb() {
	var messageBox 					= document.getElementById("ergebnismeldung");
	var fehlerbox_name				= document.getElementById("fehler_kontakt_name");
	var fehlerbox_email				= document.getElementById("fehler_kontakt_email");
	var fehlerbox_betreff			= document.getElementById("fehler_kontakt_betreff");
	var fehlerbox_nachricht			= document.getElementById("fehler_kontakt_nachricht");
	var tropfanimation				= document.createElement("img");
	tropfanimation.setAttribute("id", "tropfanimation");
	tropfanimation.setAttribute("src", "img/tropfen.gif");
	tropfanimation.setAttribute("alt", "Please wait - animation");
	tropfanimation.setAttribute("width", "60");
	tropfanimation.setAttribute("height", "59");

	clearText(messageBox);
	var newNode = document.createTextNode("Your message has been transmitted, waiting for answer...");
	messageBox.appendChild(tropfanimation);
	messageBox.appendChild(newNode);

	messageBox.className			= 'hinweis';
	messageBox.style.display		= 'block';

	fehlerbox_name.style.display		= 'none';
	fehlerbox_email.style.display		= 'none';
	fehlerbox_betreff.style.display		= 'none';
	fehlerbox_nachricht.style.display	= 'none';

	if (request.readyState == 4) {

		if (request.status == 200 || request.status == 304) {
			var kontaktformular			= document.getElementById("formular");
			kontaktformular.innerHTML	= request.responseText;
			prepareMailtoFormular();
		} else {
			replaceText(messageBox, "Failure: Unfortunately an error has occurred: HTTP-status " + request.status);
			messageBox.className			= 'fehler';
			messageBox.style.display		= 'block';
		}
	}
}

function showToggleAboStatus() {
	var messageBox = document.getElementById("ergebnismeldung");
	var tropfanimation				= document.createElement("img");
	tropfanimation.setAttribute("id", "tropfanimation");
	tropfanimation.setAttribute("src", "img/tropfen.gif");
	tropfanimation.setAttribute("alt", "Bitte warten - Animation");
	tropfanimation.setAttribute("width", "60");
	tropfanimation.setAttribute("height", "59");

	clearText(messageBox);
	var newNode = document.createTextNode("Ihre Abo-Anfrage wurde gesendet, warte auf Antwort...");
	messageBox.appendChild(tropfanimation);
	messageBox.appendChild(newNode);

	messageBox.className		= 'hinweis';
	messageBox.style.display	= 'block';

	if (request.readyState == 4) {

		if (request.status == 200 || request.status == 304) {
			var aboformular			= document.getElementById("formular");
			aboformular.innerHTML	= request.responseText;
			prepareToggleAboFormular();
		} else {
			replaceText(messageBox, "Fehler: Leider ist ein Server-Fehler aufgetreten: HTTP-Status " + request.status);
			messageBox.className		= 'fehler';
			messageBox.style.display	= 'block';
		}
	}
}

function showToggleAboStatusGb() {
	var messageBox = document.getElementById("ergebnismeldung");
	var tropfanimation				= document.createElement("img");
	tropfanimation.setAttribute("id", "tropfanimation");
	tropfanimation.setAttribute("src", "img/tropfen.gif");
	tropfanimation.setAttribute("alt", "Please wait - animation");
	tropfanimation.setAttribute("width", "60");
	tropfanimation.setAttribute("height", "59");

	clearText(messageBox);
	var newNode = document.createTextNode("Your inquiry has been transferred, waiting for answer...");
	messageBox.appendChild(tropfanimation);
	messageBox.appendChild(newNode);

	messageBox.className		= 'hinweis';
	messageBox.style.display	= 'block';

	if (request.readyState == 4) {

		if (request.status == 200 || request.status == 304) {
			var aboformular			= document.getElementById("formular");
			aboformular.innerHTML	= request.responseText;
			prepareToggleAboFormularGb();
		} else {
			replaceText(messageBox, "Failure: Unfortunately an error has occurred: HTTP-status " + request.status);
			messageBox.className		= 'fehler';
			messageBox.style.display	= 'block';
		}
	}
}

/*  ##################
	# SEND FORMULAR: #
	##################
*/
function sendEmail(data) {
	var messageBox 		= document.getElementById("ergebnismeldung");
	var tropfanimation	= document.createElement("img");
	tropfanimation.setAttribute("id", "tropfanimation");
	tropfanimation.setAttribute("src", "img/tropfen.gif");
	tropfanimation.setAttribute("alt", "Bitte warten - Animation");
	tropfanimation.setAttribute("width", "60");
	tropfanimation.setAttribute("height", "59");

	clearText(messageBox);
	var newNode = document.createTextNode("Bitte warten, Ihre Kontaktanfrage wird übermittelt.");
	messageBox.appendChild(tropfanimation);
	messageBox.appendChild(newNode);
	messageBox.className		= 'hinweis';
	messageBox.style.display	= 'block';

	var url					= "php/mailto_formlogic.php";

	createRequest();

	if (request == null)
		return false;

	request.onreadystatechange = showEmailStatus;
	request.open("POST", url, true);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.send(data);

	return true;
}

function sendEmailGb(data) {
	var messageBox 		= document.getElementById("ergebnismeldung");
	var tropfanimation	= document.createElement("img");
	tropfanimation.setAttribute("id", "tropfanimation");
	tropfanimation.setAttribute("src", "img/tropfen.gif");
	tropfanimation.setAttribute("alt", "Please wait - animation");
	tropfanimation.setAttribute("width", "60");
	tropfanimation.setAttribute("height", "59");

	clearText(messageBox);
	var newNode = document.createTextNode("Please wait, your inquiry is being transmitted.");
	messageBox.appendChild(tropfanimation);
	messageBox.appendChild(newNode);
	messageBox.className		= 'hinweis';
	messageBox.style.display	= 'block';

	var url = "php/mailto_formlogic_gb.php";

	createRequest();

	if (request == null)
		return false;

	request.onreadystatechange = showEmailStatusGb;
	request.open("POST", url, true);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.send(data);

	return true;
}

function toggleAbo(data) {
	var messageBox = document.getElementById("ergebnismeldung");
	var tropfanimation				= document.createElement("img");
	tropfanimation.setAttribute("id", "tropfanimation");
	tropfanimation.setAttribute("src", "img/tropfen.gif");
	tropfanimation.setAttribute("alt", "Bitte warten - Animation");
	tropfanimation.setAttribute("width", "60");
	tropfanimation.setAttribute("height", "59");

	clearText(messageBox);
	var newNode = document.createTextNode("Bitte warten, Ihre Aboanfrage wird übermittelt.");
	messageBox.appendChild(tropfanimation);
	messageBox.appendChild(newNode);

	messageBox.className		= 'hinweis';
	messageBox.style.display	= 'block';

	var url			= "php/toggleabo_formlogic.php";

	createRequest();

	if (request == null)
		return false;

	request.open("POST", url, true);
	request.onreadystatechange = showToggleAboStatus;
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.send(data);
	return true;
}

function toggleAboGb(data) {
	var messageBox = document.getElementById("ergebnismeldung");
	var tropfanimation				= document.createElement("img");
	tropfanimation.setAttribute("id", "tropfanimation");
	tropfanimation.setAttribute("src", "img/tropfen.gif");
	tropfanimation.setAttribute("alt", "Please wait - animation");
	tropfanimation.setAttribute("width", "60");
	tropfanimation.setAttribute("height", "59");

	clearText(messageBox);
	var newNode = document.createTextNode("Please wait, your inquiry gets transmitted.");
	messageBox.appendChild(tropfanimation);
	messageBox.appendChild(newNode);

	messageBox.className = 'hinweis';
	messageBox.style.display = 'block';

	var url = "php/toggleabo_formlogic_gb.php";

	createRequest();

	if (request == null)
		return false;

	request.open("POST", url, true);
	request.onreadystatechange = showToggleAboStatusGb;
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.send(data);
	return true;
}

/*
	#################
	#	AJAX ENDE	#
	#################
*/

function openPopup(picture_name, width, height) {
	if (width == null)
		width = 500;
	if (height == null)
		height = 500;

	win = window.open(picture_name,'fensterchen','resizable,scrollbars,width=' + (width + 20) + ',height=' + (height + 20));
	win.document.write('<html>\n<head><title>Zoom</title></head>\n<body bgcolor=#000000>\n');
	win.document.write('<div align=\"center\">\n');
	win.document.write('<a href=\"javascript:window.close()\" title\"Schließen Sie dieses Fenster\"><img src=\"',picture_name,'\" border=0 alt=\"zoom\" title=\"Fenster schließen\"></a>\n');
	win.document.write('</div></body>\n</html>');
	win.document.close();
	win.focus();
}

function openBHB(volume, language) {
	url			= "/inc/abstracts.php?volume=" + volume + "&lan=" + language;
	popupWin	= window.open(url, 'remote',  'status,scrollbars,resizable,dependent,width=750,height=480,left=50,top=50')
	flag		= 1;
}

function openWindow(url, description) {
	popupWin = window.open('', 'remote',  'dependent,width=640,height=480,left=50,top=50');
	popupWin.document.write('<html>\n<head><title>Zoom</title></head>\n<body bgcolor="#000000" text="#FFFF99" link="#FFFF80" alink="#FFFF80" vlink="#FFFF80"><br /><br /><div align="center"><a href=\"javascript:window.close()\" title\"Schließen Sie dieses Fenster\"><img src="' + url + '" border="0" alt="' + description + '"></a></div><br /><div align="center"><font face="Verdana,Geneva,Arial,Helvetica,sans-serif" size="1">' + description + '<br /><br /><br />[<a href="JavaScript:self.close();" title="Close this window">close window</a>]</font></div>\n<br />\n</body>\n</html>');
	popupWin.focus();
	flag = 1;
}

function openWindowHigh(url, description) {
	popupWin = window.open('', 'remote',  'dependent,width=440,height=650,left=50,top=50');
	popupWin.document.write('<html>\n<head><title>Zoom</title></head>\n<body bgcolor="#000000" text="#FFFF99" link="#FFFF80" alink="#FFFF80" vlink="#FFFF80"><br /><br /><div align="center"><a href=\"javascript:window.close()\" title\"Schließen Sie dieses Fenster\"><img src="' + url + '" border="0" alt="' + description + '"></a></div><br /><div align="center"><font face="Verdana,Geneva,Arial,Helvetica,sans-serif" size="1">' + description + '<br /><br /><br />[<a href="JavaScript:self.close();" title="Close this window">close window</a>]</font></div>\n<br />\n</body>\n</html>');
	popupWin.focus();
	flag = 1;
}

function closePopup() {
	if (flag == 1 && ! popupWin.closed)
		popupWin.close();
}

function save() {
	var UrlString = "";
	for (i = 0; i < document.SCBNL.SCBNL_Auswahl.length; ++i) {
		if (document.SCBNL.SCBNL_Auswahl.options[i].selected == true)
			var FileName = document.SCBNL.SCBNL_Auswahl.options[i].value;
	}

	UrlString = "http://www.speleo-berlin.de/cgi-bin/getSCBNL.pl?" + escape(FileName);
	document.location.href = UrlString;
}

function showBHB(id_name) {
	h = eval('document.getElementById(\"Jahrgang_" + id_name + "\")');
	h.style.display = "block";
	h = eval('document.getElementById(\"volume_" + id_name + "\")');
	h.style.display = "none";
	h = eval('document.getElementById(\"plus_" + id_name + "\")');
	h.style.display = "none";
	h = eval('document.getElementById(\"minus_" + id_name + "\")');
	h.style.display = "inline";
}

function hideBHB(id_name) {
	h = eval('document.getElementById(\"Jahrgang_" + id_name + "\")');
	h.style.display = "none";
	h = eval('document.getElementById(\"plus_" + id_name + "\")');
	h.style.display = "inline";
	h = eval('document.getElementById(\"volume_" + id_name + "\")');
	h.style.display = "block";
	h = eval('document.getElementById(\"minus_" + id_name + "\")');
	h.style.display = "none";
}

/*
 * behandleExterneLinks()
 *
 * Sorgt dafür, dass alle externen Links in einem neuen Fenster geöffnet werden.
 */
function behandleExterneLinks() {
	if (document.links) {
		for (var i = 0; i < document.links.length; ++i) {
			var link = document.links[i].href;

			if ((link.indexOf('http://') >= 0) && (link.indexOf('speleo-berlin') == -1)) {
				document.links[i].onclick =
				function() {
					var link = this.href;
					var new_window = window.open(link, 'New_window');
					new_window.focus();
					return false;
				};
			}
		}
	}
}

$(document).ready(function(){
	behandleExterneLinks();
	$( ".datum" ).datepicker({ dateFormat: 'dd.mm.yy' });
});
//]]>
