/**
 *  Javascript trim, ltrim, rtrim
 *  http://www.webtoolkit.info/
 */


function urlReplace(string){
	
	//info("string = " + string);
	var ret = string.replace(/ /g, "_"); 
	ret = ret.replace(/,/g, "");
	ret = ret.replace(/ä/g, "ae");
	ret = ret.replace(/ü/g, "ue");
	ret = ret.replace(/ö/g, "oe");
	ret = ret.replace(/\./g, "-");
	
	//info("return string = " + ret);
	return ret;
	
}

function createEarthfaveURL(obj){

	
	var urlCName = obj.catName0;
	var urlCity  = obj.city; 
	var urlName  = obj.name;
	
	
	if( obj.catName0 ) { urlCName = urlReplace( obj.catName0 ); } 
	if( obj.name ) 		{ urlName =  urlReplace( obj.name ); } 
	if( obj.city ) 		{ urlCity =  urlReplace( obj.city ); } 
	
	var url = global.URL + urlCName + '_' + urlCity + '/' +  obj.id + '/' + urlName ;
	return url;
	
}
														
														
														

function trim(str, chars) 
{
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) 
{
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) 
{
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}  


function addslashes(str) {
	str=str.replace(/\'/g,'\'');
	str=str.replace(/\"/g,'\"');
	//str=str.replace(/\\/g,'\\\\');
	//str=str.replace(/\0/g,'\\0');
	return str;
}


// Zum Kodieren von Ajax-Parametern vor dem Absenden: zuerst nach UFF-8, dann URL-Kodieren. 
//	Das UFT8-Kodieren muß leider explizit gemacht werden, da es nach dem URL-Kodieren nicht mehr geht
function encodeForRequest(text)
{
	return escape(encode_utf8(text));	
}


// Danke an selfhtml, siehe http://aktuell.de.selfhtml.org/artikel/javascript/utf8b64/utf8.htm
function encode_utf8(rohtext) 
{
	if (! rohtext) {
		return rohtext;
	}
	// Dient der Normalisierung des Zeilenumbruchs
	rohtext = rohtext.replace(/\r\n/g,"\n");
	var utftext = "";
	for(var n=0; n<rohtext.length; n++)
	{
		// ermitteln des Unicodes des  aktuellen Zeichens
		var c=rohtext.charCodeAt(n);
		// alle Zeichen von 0-127 => 1byte
		if (c<128)
		utftext += String.fromCharCode(c);
		// alle Zeichen von 127 bis 2047 => 2byte
		else if((c>127) && (c<2048)) 
		{
			utftext += String.fromCharCode((c>>6)|192);
			utftext += String.fromCharCode((c&63)|128);}
			// alle Zeichen von 2048 bis 66536 => 3byte
			else 
			{
				utftext += String.fromCharCode((c>>12)|224);
				utftext += String.fromCharCode(((c>>6)&63)|128);
				utftext += String.fromCharCode((c&63)|128);
			}
	}
	return utftext;
}


function decode_utf8(utftext) {
             var plaintext = ""; var i=0; var c=c1=c2=0;
             // while-Schleife, weil einige Zeichen uebersprungen werden
             while(i<utftext.length)
                 {
                 c = utftext.charCodeAt(i);
                 if (c<128) {
                     plaintext += String.fromCharCode(c);
                     i++;}
                 else if((c>191) && (c<224)) {
                     c2 = utftext.charCodeAt(i+1);
                     plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
                     i+=2;}
                 else {
                     c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
                     plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
                     i+=3;}
                 }
             return plaintext;
         }


function info(text, level, module)
{
//	if (typeof(console) !== "undefined") console.log(text);

/*
	if (typeof(module) == "undefined") {
		module = "all";
	}
	else {
		if (typeof(global) != "undefined") {
			if (global.LOG_CURRENT_MODULE != module) {
				console.groupEnd();
				global.LOG_CURRENT_MODULE = module;
				console.groupCollapsed(module);
			}
		}
	}
	if (typeof(level) == "undefined") {
		level = 1; // error
	}
	
	if (typeof(global) != "undefined") {
		if (global.LOG_LEVEL >= level && (global.LOG_MODULE == "all" || global.LOG_MODULE == module)) {
			if (typeof(console) !== "undefined") {
				console.log("   ", text);
			}
		}
	}
*/
}


/**
 *  Returns the inner height of the browser window.
 *  Note: That can differ from the height of subordinated elements of the window (in case of scrollable elements, for example)
 */
function getWindowHeight() 
{
  if (typeof(window.innerHeight) == 'number') 
  {
    //Non-IE
    return window.innerHeight;
  } 
  else if (document.documentElement && document.documentElement.clientHeight) 
  {
    //IE 6+ in 'standards compliant mode'
    return document.documentElement.clientHeight;
  } 
  else if (document.body && document.body.clientHeight) 
  {
    //IE 4 compatible
    return document.body.clientHeight;
  }
}


/**
 *  Returns the inner width of the browser window.
 *  Note: That can differ from the width of subordinated elements of the window (in case of scrollable elements, for example)
 */
function getWindowWidth() 
{
  if (typeof(window.innerWidth) == 'number') 
  {
    //Non-IE
    return window.innerWidth;
  } 
  else if (document.documentElement && document.documentElement.clientWidth) 
  {
    //IE 6+ in 'standards compliant mode'
    return document.documentElement.clientWidth;
  } 
  else if( document.body && document.body.clientWidth ) 
  {
    //IE 4 compatible
    return document.body.clientWidth;
  }
}

/**
* gibt mir die id eines efs in seinem array zurueck
*/
function getArrayIdByEfIdOfActiveMap(activeMap,efid){
		var i =0;
		for(i; i < activeMap.earthfaves.length ; i++){
				if(activeMap.earthfaves[i].id == efid){ return i; }
		}
		return false;			
}