
//function to open a pop up
//URL: is the relative or absolute http URL
//Name: is the name of the new window (pop up)
//xWidth: the width of the pop up (int > 0)
//xHeight: the height of the pop up (int > 0).
function openPopUp(URL,Name,xWidth,xHeight,xScrollBars){
	xLeft=(screen.availWidth-xWidth)/2;
	xTop=(screen.availHeight-xHeight)/2;
	xWin = window.open(URL,Name,'scrollbars='+xScrollBars+',width='+xWidth+',height='+xHeight+',left='+xLeft+',top='+xTop+',dependent=yes,resizable=yes');
	xWin.focus();
}


//function to open an external URL in a new window
//the dimentions of this window are the dimentions of the screen
function openWindow(URL){
	xWidth = screen.availWidth;
	xHeight = screen.availHeight;
	xWin = window.open(URL,'','toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,dependent=yes,resizable=yes,width='+ xWidth +',height='+ xHeight +',top=0,left=0');
	xWin.focus();
}



//function to open an image popup
function showImage(holderURL, imageURL, wi, hi, marge, resize){
	posX = 30; //(screen.availWidth - wi)/2;
	posY = 30; //(screen.availHeight- hi)/2;
	imageURL = encodeURI(imageURL);
	imageWin = window.open(holderURL +'?showImage='+ imageURL +'&margin='+ marge +'&resize='+ resize, '', 'width='+ wi +',height='+ hi +',left='+ posX +',top='+ posY +',dependent=yes,resizable=no');
	imageWin.focus();
}



//function to swap page language without changing the url path
function swapLang(lang){
	var URL = document.URL;
	var host = window.location.host;
	var leftPart = URL.substring(0, URL.indexOf(host) + host.length);
	var rightPart = URL.substring(URL.indexOf(host) + host.length, URL.length);
	if (rightPart.charAt(0) == "/") rightPart = rightPart.substring(1, rightPart.length);
	var newURL = "";
	if (rightPart.indexOf("/") == 3){
		var origLang = rightPart.substring(0, rightPart.indexOf("/"));
		newURL = leftPart + "/" + lang + rightPart.substring(rightPart.indexOf("/"), rightPart.length);
	}else{
		if (URL.indexOf("lang=") < 0){
			newURL = (URL.indexOf("?") < 0) ? URL + "?lang=" + lang : URL + "&lang=" + lang;
		}else{
			var langVar = "lang=";
			leftPart = URL.substring(0, URL.indexOf(langVar) + langVar.length);
			rightPart = URL.substring(URL.indexOf(langVar) + langVar.length, URL.length);
			if (rightPart.indexOf("&") > -1) rightPart = rightPart.substring(rightPart.indexOf("&"), rightPart.length);
			newURL = leftPart + lang + rightPart;
		}
	}
	document.location = newURL;
}


//function to open a route planer (maps.goole.de)
function routePlaner(fromStreet, fromZIP, fromCity, fromCountry, toStreet, toZIP, toCity, toCountry){
	planerURL = "http://maps.google.de/maps?f=d&hl=de&geocode=";
	planerURL += "&saddr=" + fromStreet + "+" + fromZIP + "+" + fromCity + "+" + fromCountry;
	planerURL += "&daddr=" + toStreet + "+" + toZIP + "+" + toCity + "+" + toCountry;
	planer = window.open(planerURL, 'routePlaner', screen.availWidth-60, screen.availHeight-60 ,'yes');
	planer.focus();
}


//Function to get a defined number of decimals behind the comma.
//First parameter: is type of double
//Second parameter: is an integer (number of decimals to show)
function formatNumber(expr, decplaces) {
	var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));
	while (str.length <= decplaces) {
		str = "0" + str;
	}
	var decpoint = str.length - decplaces;
	return str.substring(0,decpoint) + "," + str.substring(decpoint, str.length);
	//return str.substring(0,decpoint) + (decplaces) ? "." + str.substring(decpoint,str.length) : "";
}


//Function to set inner text of a label
/*
function setInnerText(txt, txtHolder){
	if (typeof txtHolder.textContent != "undefined"){
		txtHolder.textContent = txt;
	}
	else if (typeof txtHolder.innerText != "undefined"){
		txtHolder.innerText = txt;
	}
	else if (typeof txtHolder.removeChild != "undefined"){
		while (txtHolder.hasChildNodes()){
			txtHolder.removeChild(txtHolder.lastChild);
		}
		txtHolder.appendChild(document.createTextNode(txt));
	}
}
*/

function setInnerText(inStr, txtHolder){
	/* OLD
	if (typeof txtHolder.textContent != "undefined"){
		txtHolder.textContent = txt;
	}else if (typeof txtHolder.innerText != "undefined")
		txtHolder.innerText = txt;
	}else if (typeof txtHolder.removeChild != "undefined"){
		while (txtHolder.hasChildNodes()){ txtHolder.removeChild(txtHolder.lastChild)}
		txtHolder.appendChild(document.createTextNode(txt));
	}
	*/	
	// remove existing nodes in this text holder
	while (txtHolder.hasChildNodes()){
		txtHolder.removeChild(txtHolder.lastChild);
	}
	// split input of line breaks in it.
	var splitStr = inStr.split("<br>");
	for (var i=0; i<splitStr.length; i++){
		txtHolder.appendChild(document.createTextNode(splitStr[i]));
		if (i<splitStr.length-1) txtHolder.appendChild(document.createElement("BR"));
	}
}


//Function to check email format validity
function isValidEmail(eMail) {
	if (window.RegExp) {
		reg = /^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$/;
		return reg.test(eMail)
	} else {
		if (eMail.length<7)						return false;
		if (eMail.indexOf("@")<1)				return false;
		if (eMail.indexOf(".")==0)				return false;
		if (eMail.indexOf(".")==eMail.length-1)	return false;
		if (eMail.indexOf("-")==0)				return false;
		if (eMail.indexOf("-")==eMail.length-1)	return false;
		return true
	}
	return false
}


//Function2 crypt and decrypt as antispam protection.
function CryptMail(s,shift) {	//
	var n=0;
	var r="";
	for(var i=0;i<s.length;i++) { 
		n=s.charCodeAt(i); 
		if (n>=8364) {n = 128;}
		r += String.fromCharCode(n-(shift)); 
	}
	return r;
}
  
// JS function for uncrypting spam-protected emails:

function SendMailTo(s,shift)	{	//
	location.href=CryptMail(s, shift);
}



/**
*
*  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) {
            if ((c > 32) && (c < 255)){
				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) {
	    if ((c > 32) && (c < 255)){
                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;
    }

}