
// DOM for all browsers
//
function obj(name) {
	if(document.all) 	return eval("document.all."+name);
	else 				return document.getElementById(name);
}


function show_only_div ( id ) {
	obj('editlayer_menu').style.visibility='hidden';		obj('editlayer_menu').style.display='none';
	obj('editlayer_node').style.visibility='hidden';		obj('editlayer_node').style.display='none';
	obj('editlayer_node_out').style.visibility='hidden';	obj('editlayer_node_out').style.display='none';
	obj('editlayer_code').style.visibility='hidden';		obj('editlayer_code').style.display='none';
	obj('editlayer_func').style.visibility='hidden';		obj('editlayer_func').style.display='none';
	obj('editlayer_attrib').style.visibility='hidden';		obj('editlayer_attrib').style.display='none';
	obj('editlayer_attrib_node').style.visibility='hidden';	obj('editlayer_attrib_node').style.display='none';
	obj('editlayer_version').style.visibility='hidden';		obj('editlayer_attrib').style.display='none';
	obj('editlayer_customtool').style.visibility='hidden';	obj('editlayer_customtool').style.display='none';

	obj(id).style.visibility='visible';
	obj(id).style.display='block';
}


function edit_prepare ( area , table , field , id , nodename , article , allow_edit ) {
	var data;

	show_adminlayer();
	if( edit_data[id] ) {
		data	= unescape( edit_data[id].replace( /%/g , "%25")  );
	} else {
		data	= "";
	}

	if( data.match( /^PRE[\n\r]+/ ) ) {
		data	= data.replace( /^PRE[\n\r]+/ , '' );
		data	= data.replace( /\n\r/g , "\n" );
		data	= data.replace( /\r\n/g , "\n" );
		data	= data.replace( /\r/g , "\n" );
		data	= data.replace( /\n/g , "<br>" );
	}

	obj('edit_table_data').value		= table;
	obj('edit_field_data').value		= field;
	obj('edit_id_data'   ).value		= id;

	if( obj('edit_nodename_data') )		obj('edit_nodename_data').value		= nodename;
	if (obj('edit_article_data'	) ) 	obj('edit_article_data'	).value		= article;
	
	obj('output_table').innerHTML	= table;
	obj('output_name').innerHTML	= nodename;
	obj('output_article').innerHTML	= field;

	obj('edit_query_version' ).value		= 1;


	show_only_div( 'editlayer_node' );


	// force plain Editor?

	if( data.match(/\[\[/)  ) {
		editormode( 'plain' , 1 );
	} else if( typeof nodes_used_as_list!='undefined' && nodes_used_as_list[nodename] == 1 ) {
		editormode( 'plain' , 1 );
	} else {
		editormode( 'rich' , 0 );
	}

	set_editor_content ( data );
	
	window.location.hash	='top';

	// ---attribute
	if( typeof( edit_data_attribute[id] ) == 'undefined' )			edit_data_attribute[id]='';
	obj('attribute_node').value		= unescape( edit_data_attribute[id].replace( /%/g , "%25")  );
	obj('attribute_node_id').value	= id;

}

function editormode( want_mode , lock ){
	var editor = FCKeditorAPI.GetInstance('FCKeditor_node');

	if( lock ) {
		editor.ToolbarSet.Collapse() ;
	} else {
		editor.ToolbarSet.Expand() ;
	}

	if (	( editor.EditMode != FCK_EDITMODE_WYSIWYG  && want_mode=='rich' )	||
			( editor.EditMode == FCK_EDITMODE_WYSIWYG  && want_mode=='plain' )
	) {
		editor.SwitchEditMode();
	}
}


function set_editor_content ( content ) {

		var editor;
		editor = FCKeditorAPI.GetInstance('FCKeditor_node');
		editor.SetHTML( content );
		editor.Focus();
}

function get_editor_content ( which ) {
	var editor;
	editor = FCKeditorAPI.GetInstance('FCKeditor_node');
	return editor.GetXHTML();
}

function FCKeditor_OnComplete( editorInstance ) {
	//  switch_editor( 'rich' );
}

function body_onkeypress(e) {
	var kC  = (window.event) ?		event.keyCode : e.keyCode;
	var Esc = (window.event) ?		27 : e.DOM_VK_ESCAPE;
	if(kC==Esc  &&  obj('adminlayer') )	{
		close_adminlayer();
	}
}

function popup (adr) {
	win = window.open(adr, "", "width=300,height=400,left=100,top=200");
	win.focus();
}

function goto_url (adr) {
	document.location.href	= adr;
}



// Thank you to http://aktuell.de.selfhtml.org/artikel/javascript/utf8b64/utf8.htm
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;
}


// Thank you to http://aktuell.de.selfhtml.org/artikel/javascript/utf8b64/utf8.htm
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 show_adminlayer() {
	all_flash_visibility('hide');
	obj('adminlayer_trigger').style.visibility	= 'hidden';
	obj('adminlayer').style.visibility			= 'visible';
	obj('adminlayer').style.display				= 'block';
	show_only_div( 'editlayer_code' );
}

function close_adminlayer() {
	all_flash_visibility('show');
	obj('adminlayer_trigger').style.visibility	= 'visible';
	obj('adminlayer').style.visibility			= 'hidden';
	obj('adminlayer').style.display				= 'none';
	
	show_only_div( 'editlayer_code' );
	obj('editlayer_code').style.visibility='hidden';
	obj('editlayer_code').style.display='none';
}

function delete_adminlayer() {
	all_flash_visibility('show');
	obj('adminlayer_trigger').style.display		= 'none';
	obj('adminlayer').style.display				= 'none';
}

// Aus- und Einblenden aller Flash-Movies fuer Admin
function all_flash_visibility ( visi ) {
	f	= document.getElementsByTagName("object");
	for( var i=0; i<f.length ; i++ ) {
		if( visi=='show' ) {
			f[i].parentNode.style.visibility	= "visible";
		} else {
			f[i].parentNode.style.visibility	= "hidden";
		}
	}
}

function createXMLHttpRequest() {
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
	try { return new XMLHttpRequest(); } catch(e) {}
	alert("XMLHttpRequest not supported");
	return null;
}



function mysql_select( table , field , id ) {
	var req = createXMLHttpRequest();
	q	= '../start/index.php?command=XMLHTTP_MYSQL_SELECT&edit_table=' + table+'&edit_field='+field+'&id='+id ;
	req.open('GET', encodeURI(q) , false); 
	req.send(null);
	return req.responseText;
}

function mysql_send_command( command ) {
	var req = createXMLHttpRequest();
	q	= '../start/index.php?command=XMLHTTP_MYSQL_SEND_COMMAND&xmlhttp_mysql_send_command=' + command ;
	req.open('GET', encodeURI(q) , false); 
	req.send(null);
	return req.responseText;
}

function delete_node2( table, id) {
	query	= "DELETE FROM " + table + " WHERE id='" + id + "' LIMIT 1";
	mysql_send_command( query );
	alert( "Bitte Seite neu laden.");
}

function edit_prepare2 ( table , id , edit_type , field , extras ) {
	var data;

	show_adminlayer();
	
	data	= mysql_select( table , field , id );


	obj('edit_table_data').value		= table;
	obj('edit_field_data').value		= field;
	obj('edit_id_data'   ).value		= id;

	obj('edit_query_version' ).value	= 2;

	show_only_div( 'editlayer_node' );

	if( ","+extras+",".match(/,PLAINTEXT,/)  ) {
		editormode( 'plain' , 1 );
	} else {
		editormode( 'rich' , 0 );
	}


	set_editor_content ( data );

	window.location.hash	='top';
}

function child_nodes_recursive( o ) {
	// if( typeof( o ) == 'undefined' ) 		return Array();

	var nodes	= as_array( o.childNodes );
	var children;
	for( var i=0 ; i<o.childNodes.length ; i++ ) {
		children	= as_array( child_nodes_recursive( o.childNodes[i] ) );
		nodes	= nodes.concat( children );
	}
	return nodes;
}


// convert a nodelist to array
function as_array( l ) {
	tmp	= new Array();
	for( var i=0 ; i<l.length ; i++ ) {
		tmp[ tmp.length ]	= l[i];
	}
	return tmp;
}


// body_onload funktion registrieren, die dann spaeter ausgefuehrt werden soll
onload_function	= new Array();
function register_onload( f ) {
	onload_function[onload_function.length]	= f;
}

// Registrierte body_onload Funktionen als Event ausfuehren
function body_onload() {

	// New
	for( var i=0 ; i<onload_function.length ; i++ ) {
		eval( onload_function[i] );
	}
	
	// Old
	if (typeof body_onload_custom == "function") {
		body_onload_custom();
	}
}






// Hilfsfunktion zu highlight (Suchresultate hervorheben): Geht Objektbaum durch
function searchme( o , such ) {
	if( o && such ) {
		var forbidden=0;
		if( o.className  ) {
			forbidden= (o.className.search(/\bnofindhighlight\b/)!=-1 );
		}
	
		if(! forbidden) {
			for( var i=0 ; i<o.childNodes.length ; i++ ) {
				if( o.childNodes[i].nodeType == 3 	&&		o.nodeName!="SCRIPT"	&&		o.childNodes[i].nodeName=="#text"	&&	o.childNodes[i].nodeValue.search(such)!=-1	 ) {	// Textnodes
					found[ found.length]	= o; //.childNodes[i];
					foundc[ foundc.length]	= i;
				}
	
				
				if( o.childNodes[i].hasChildNodes() ) {
					searchme( o.childNodes[i] , such );
				}
			}
		}
	}
}


//	basiselement nach RegExp durchsuchen und highlighten
function highlight( b , such ) {
//	Parameter:
//	var b = document.getElementById("ausgabe");
//	var such = new RegExp("(" + decodeURI("<  ?php echo $hi ?  >") + ")","gi");

	searchme(b , such );

	for( var i=0 ; i<found.length ; i++ ) {
		o	= found[i];
		c	= foundc[i];
//document.write( "-"+o.childNodes[c].nodeValue+"-<br>");

		var newspan = document.createElement("span");
	
		neutext	= o.childNodes[c].nodeValue;
		neutext	= neutext.replace(/^ / , '&nbsp;' );	// workaround IE6: Inserting textnode beginning with spaces is truncated. Kill B!ll...
		neutext	= neutext.replace(such , '<span class="find_highlight">$1<\/span>' );
	
		newspan.innerHTML	= neutext;
		o.replaceChild(newspan,o.childNodes[c]);

	}
}