//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Javascript CMS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


HTTP_ROOT = '';

var timeout;

google_maps_gestartet = 0;

// Inits
function init_cms(javascript_http_root) {
							
		// Lokal
		
		HTTP_ROOT = javascript_http_root;
	
	}

function toggleVisibility(id,showhide) {

	document.getElementById(id).style.visibility = '' + showhide + '';
      
   
}


function toggleDisplay(id,showhide) {

	document.getElementById(id).style.display = '' + showhide + '';
      
   
}


function display_admin() {

	document.getElementById('cms_loginarea').style.display = 'block';
	scrollTo(0,0);

}

function seite_vorschau(id) {

	window.open("/admin/seite/vorschau/?id=" + id,"_self");


}

function get_text_from_version(id,version,language) {
	
	
	html_code = '';	

	var req = new Request({
		
		method: 'get',
		url: HTTP_ROOT+'/ajax/get_version.ajax.php?id='+id+'&version='+version+'&language='+language,
		onComplete: function(response) { 
		
			var html_code = response;
			
			div_name = 'get_text_from_version_div_' + language;

			document.getElementById(div_name).innerHTML = html_code;

		}
		
	}).send();	
	
}




function login() {
	
	html_code = '';	
	
	benutzername = document.login_form.input_feld_benutzername.value;
	passwort = document.login_form.input_feld_passwort.value;	

	var req = new Request({
		
		method: 'get',
		url: HTTP_ROOT+'/ajax/login.ajax.php?benutzername='+benutzername+'&passwort='+passwort,
		onComplete: function(response) { 	
			
			var html_code = response;

			var login_ok = response;
			
			if (login_ok == 0) {
				alert('Sie konnten nicht angemeldet werden');
				
			}
		 
			if (login_ok == 1) {
			
				window.open('/admin','_self');
				
			}
	  
		}
		
	}).send();	
	
}

function logout() {
	
	html_code = '';	

	var req = new Request({
		
		method: 'get',
		url: HTTP_ROOT+'/ajax/logout.ajax.php',
		onComplete: function(response) { 	
			
			var html_code = response;
				
			window.open('index.php','_self');

		}
		
	}).send();	
	
}

function show_iframe() {
	
	document.getElementById('cms_body_content').style.display = 'none';
	document.getElementById('cms_iframe_content').style.display = 'block';	
	
}

function show_input_iframe() {
	document.getElementById('input_content').style.display = 'none';
	document.getElementById('input_iframe_content').style.display = 'block';		
}

function show_schriftgroesse_select_div(schriftart) {
	
	if (schriftart == 'Standard') {
		document.getElementById('cms_schriftgroesse_select_div').style.display = 'none';
		document.getElementById('cms_schrift_vorschau_div').style.display = 'none';
	}
	else {
		document.getElementById('cms_schriftgroesse_select_div').style.display = 'block';
		document.getElementById('cms_schrift_vorschau_div').style.display = 'block';
	}
	
}


function show_schrift_vorschau(schriftart,anzeigeart,schriftgroesse) {

	html_code = '';	

	var req = new Request({
		
		method: 'get',
		url: HTTP_ROOT+'/ajax/schrift_vorschau.ajax.php?schriftart='+schriftart+'&anzeigeart='+anzeigeart+'&schriftgroesse='+schriftgroesse,
		onComplete: function(response) { 	
			
			var html_code = response;

			document.getElementById('schrift_vorschau').innerHTML = html_code;
  
		}
		
	}).send();	
	
}


function ziel_seite_select_ajax(was,action,id) {
	
	html_code = '';	

	var req = new Request({
		
		method: 'get',
		url: HTTP_ROOT+'/ajax/get_ziel.ajax.php?was='+was+'&action='+action+'&id='+id,
		onComplete: function(response) { 
		
			var html_code = response;

			document.getElementById('ziel_seite_select_div').innerHTML = html_code;

		}
		
	}).send();	
		
}


function encode_utf8(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 show_iframe_frage() {
	
	document.getElementById('frage_body_content').style.display = 'none';
	document.getElementById('frage_iframe_content').style.display = 'block';	
}



