/**
* library.js
*
* History:
* - 2009-02-27 ck: hinzufügen der Methoden hideWindow(), insertProposal(),
*					searchSuggestions() und searchSuggesions_callback()
*/

function popupNormal(sUrl, sWidth, sHeight)
{
	var popup;

	popup = window.open(sUrl, 'PressRelease','toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,screenX=10,screenY=10,resizable=yes,width=' + sWidth + ',height=' + sHeight, true);
	if(navigator.appName.indexOf('Netscape') != -1)
	popup.focus();
}

function popupPicture(sUrl, sWidth, sHeight)
{
	var popup;
	//var sWidth=sWidth+10;
	//var sHeight=sHeight+10;

	popup = window.open(sUrl, 'PictureViewer','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,screenX=10,screenY=10,resizable=yes,width=' + sWidth + ',height=' + sHeight, true);
	if(navigator.appName.indexOf('Netscape') != -1)
	popup.focus();
}

function printpage() {
	if (window.print)
	window.print();
	else
	alert("Leider unterstützt Ihr Browser diese Funktion nicht. Bitte aktivieren Sie die Druckfunktion Ihres Browsers manuell.");
}

/*
* Blendet das div mit der gegebenen ID aus
*
* string text			ID des auszublendenden Elements
*/
function hideWindow(element) {
	document.getElementById(element).style.display = "none";
}

/*
* fügt einen vorgeschlagenen Begriff in ein Textfeld ein
*
* string idInput 		ID des Textelds, in das der Begriff eingefügt werden soll
* string idResults		ID des Divs für die Suchergebnisse
* string proposal		Inhalt, der in das Textfeld eingefügt werden soll
* string submitForm	submit sendet das Formular nach dem Einfügen des Begriffs ab
*						nosubmit schreibt den Begriff nur in das Eingabefeld, sendet das Formular nicht ab
*/
function insertProposal(idInput, idResults, proposal, submitForm) {
	document.getElementById(idInput).value = proposal;
	hideWindow(idResults);
	document.getElementById(idInput).focus();
	if(submitForm == 'submit') {
		document.suche.submit();
	}
	return false;
}

//fügt einen vorgeschlagenen Begriff dem Textfeld hinzu
function appendProposal(id, proposal, nameForm)
{
	text 		= document.getElementById(id).value;
	words 		= text.split(",");
	last_word 	= words[words.length-1];
	text		= text.replace(last_word, proposal);
	document.getElementById(id).value = text;
	hideWindow("keyword_proposals");
	document.getElementById(id).focus();
	return false;
}

/*
* fügt einen vorgeschlagenen Begriff zu dem Inhalt eines Textfelds dazu
*
* string idInput 		ID des Textelds, in das der Begriff eingefügt werden soll
* string idSearch		ID des Suchfelds
* string idResults		ID des Divs für die Suchergebnisse
* string proposal		Inhalt, der in das Textfeld eingefügt werden soll
*/
function addProposal(idInput, idSearch, idResults, proposal) {
	if (document.getElementById(idInput).value == '') {
		document.getElementById(idInput).value = proposal;
	} else {
		document.getElementById(idInput).value = document.getElementById(idInput).value + ', ' + proposal;
	}
	hideWindow(idResults);

	document.getElementById(idSearch).value = '';
	document.getElementById(idSearch).focus();

	return false;
}

/*
* Funktion zur Suche der Suchvorschläge
*
* string text			Text, nach dem gesucht wird
* string idInput		ID des Texteingabefelds
* string idResults		ID des Divs für die Suchergebnisse
* string submitForm	submit sendet das Formular nach dem Anklicken des Begriffs ab
*						nosubmit schreibt den Begriff nur in das Eingabefeld, sendet das Formular nicht ab
*/
function searchSuggestions(text, idInput, idResults, submitForm) {
	// reset des Timers zum Ausblenden des Vorschlagsfensters
	if (typeof(timeout_ajaxnamesearch) != 'undefined')
	window.clearTimeout(timeout_ajaxnamesearch);

	// bereinigen des Strings
	text 		= text.replace(/^\s+|\s+$/g,"");

	// unter 3 Zeichen wird nicht gesucht
	if (text.length < 3) {
		hideWindow(idResults);
		return;
	}

	// Aufruf der PHP-Funktion
	x_searchSuggestions(text, idInput, idResults, submitForm, searchSuggestions_callback);

	// setzen des Timers zum Ausblenden des Vorschlagfensters
	timeout_ajaxnamesearch = window.setTimeout('hideWindow("' + idResults + '")', 10000);
}

/*
* Sajax Callback Function für searchSuggestions()
*
* erg					Ergebnis (JSON)
* string idResults		ID des Elements, in den die Ergebnisse angezeigt werden sollen
*/
function searchSuggestions_callback(erg)
{
	erg = eval("(" + erg + ")");

	searchSuggestionsDiv = document.getElementById(erg.idResults);

	// Abbruch, wenn das ErgebnisDiv nicht gefunden wird
	if (!searchSuggestionsDiv)
	return;

	// Ausblenden des ErgebnisDivs, wenn keine Ergebnisse vorliegen
	if (erg.content == "") {
		searchSuggestionsDiv.style.display = "none";
		// einblenden des ErgebnisDivs und reinschreiben der Suchergebnisse
	} else {
		searchSuggestionsDiv.style.display = "block";
		searchSuggestionsDiv.innerHTML = erg.content;
	}
}

/*
* Funktion zur Suche der Regionsvorschläge
*
* string text			Text, nach dem gesucht wird
* string idSearch		ID des Suchfelds
* string idInput		ID des Texteingabefelds
* string idResults		ID des Divs für die Suchergebnisse
*/
function regionSuggestionsByCode(text, idSearch, idInput, idResults) {
	// reset des Timers zum Ausblenden des Vorschlagsfensters
	if (typeof(timeout_ajaxnamesearch) != 'undefined')
	window.clearTimeout(timeout_ajaxnamesearch);

	// bereinigen des Strings
	text 		= text.replace(/^\s+|\s+$/g,"");

	// Aufruf der PHP-Funktion
	x_regionSuggestionsByCode(Base64.encode(text), idSearch, idInput, idResults, regionSuggestionsByCode_callback);

	// setzen des Timers zum Ausblenden des Vorschlagfensters
	timeout_ajaxnamesearch = window.setTimeout('hideWindow("' + idResults + '")', 30000);
}

/*
* Sajax Callback Function für searchSuggestions()
*
* erg					Ergebnis (JSON)
* string idResults		ID des Elements, in den die Ergebnisse angezeigt werden sollen
*/
function regionSuggestionsByCode_callback(erg)
{
	erg = Base64.decode(erg);
	erg = eval("(" + erg + ")");

	regionSuggestionsByCodeDiv = document.getElementById(erg.idResults);

	// Abbruch, wenn das ErgebnisDiv nicht gefunden wird
	if (!regionSuggestionsByCodeDiv)
	return;

	// Ausblenden des ErgebnisDivs, wenn keine Ergebnisse vorliegen
	if (erg.content == "") {
		regionSuggestionsByCodeDiv.style.display = "none";
		// einblenden des ErgebnisDivs und reinschreiben der Suchergebnisse
	} else {
		regionSuggestionsByCodeDiv.style.display = "block";
		regionSuggestionsByCodeDiv.innerHTML = erg.content;
	}
}

/*
* Funktion zur Suche der Regionsvorschläge
*
* string text			Text, nach dem gesucht wird
* string idSearch		ID des Suchfelds
* string idInput		ID des Texteingabefelds
* string idResults		ID des Divs für die Suchergebnisse
*/
function regionSuggestionsByName(text, idSearch, idInput, idResults) {
	// reset des Timers zum Ausblenden des Vorschlagsfensters
	if (typeof(timeout_ajaxnamesearch) != 'undefined')
	window.clearTimeout(timeout_ajaxnamesearch);

	// bereinigen des Strings
	text 		= text.replace(/^\s+|\s+$/g,"");

	// Aufruf der PHP-Funktion
	x_regionSuggestionsByName(Base64.encode(text), idSearch, idInput, idResults, regionSuggestionsByCode_callback);

	// setzen des Timers zum Ausblenden des Vorschlagfensters
	timeout_ajaxnamesearch = window.setTimeout('hideWindow("' + idResults + '")', 30000);
}

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}

// Funktion zur Tagsuche
// gesucht wird nach dem Suchbegriff, sofern das mehr als 3 Zeichen hat
function keywords_search(text, id, nameForm) {
	if(navigator.appName == 'Microsoft Internet Explorer' && navigator.appVersion.indexOf('MSIE 6.0') != -1) {
		return;
	}

	if (typeof(timeout_ajaxnamesearch) != 'undefined')
	window.clearTimeout(timeout_ajaxnamesearch);

	text 		= text.replace(/^\s+|\s+$/g,"");

	if (text.length < 3) {
		hideWindow("keyword_proposals");
		return;
	}

	x_keywords_search(text, id, nameForm, keywords_search_callback);
	timeout_ajaxnamesearch = window.setTimeout('hideWindow("keyword_proposals")', 10000);
}

//Sajax Callback Function für keywords_search()
function keywords_search_callback(erg)
{
	erg = eval("(" + erg + ")");

	keyword_proposals_div = document.getElementById('keyword_proposals');
	if (!keyword_proposals_div)
	return;
	if (erg.content == "") {
		keyword_proposals_div.style.display = "none";
	} else {
		keyword_proposals_div.style.display = "block";
		keyword_proposals_div.innerHTML = erg.content;
	}
}

function getProvincesByCountries(countrySelected, provinceDdId) {
	provinceSelected = document.getElementById(provinceDdId).value;
	x_getProvincesByCountries(countrySelected, provinceSelected, provinceDdId, getProvincesByCountries_cb);
}

function getProvincesByCountries_cb(erg) {
	erg = eval("(" + erg + ")");

	provincesDiv = document.getElementById(erg.provinceDdId);

	if (provincesDiv) {
		if (navigator.userAgent.indexOf("MSIE") != -1) {
			provincesDiv.outerHTML = "<select class=\"forminput\" id=\"select_province\" name=\"select_province\" style=\"width:200px;\">" + erg.content + "</select>";
		} else {
			provincesDiv.innerHTML = erg.content;
		}
	}

}