
var imageTag = false;
var theSelection = false;
var clientPC = navigator.userAgent.toLowerCase();
var clientVer = parseInt(navigator.appVersion);
var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
var is_moz = 0;
var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);

b_help = "{#Format_Tip_Bold#}";
i_help = "{#Format_Tip_Italic#}";
u_help = "{#Format_Tip_Underline#}";
highlight_help = "{#Format_Tip_High#}";
left_help = "{#Format_Tip_Left#}";
right_help = "{#Format_Tip_Right#}";
center_help = "{#Format_Tip_Center#}";
quote_help = "{#Format_Tip_Quote#}";
code_help = "{#Format_Tip_Code#}";
php_help = "{#Format_Tip_Php#}";
list_help = "{#Format_Tip_List#}";
image_help = "{#Format_Tip_Image#}";
web_help = "{#Format_Tip_Url#}";
email_help = "{#Format_Tip_Email#}";
close_help = "{#Format_Tip_CloseAll#}";
face_help = "{#Format_Tip_Face#}";
size_help = "{#Format_Tip_Size#}";
color_help = "{#Format_Tip_Color#}";

koobi__code = new Array();
koobi_tags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[highlight]','[/highlight]','[left]','[/left]','[center]','[/center]','[right]','[/right]','[url]','[/url]','[mail]','[/mail]','[img]','[/img]','[quote]','[/quote]','[code]','[/code]','[php]','[/php]','[list]','[/list]');
imageTag = false;

function smilie(code,field_id,field_name){ldelim}
	var field_name = 'text';
	var field_id = 'msgform';
	if (document.getElementById(field_id).createTextRange)
	{ldelim}
		document.getElementById(field_id).focus();
		document.selection.createRange().duplicate().text = code;
	{rdelim}
	else if (document.getElementById && !document.all) // Mozilla
	{ldelim}
    	var tarea = document.getElementById(field_id);
    	var selEnd = tarea.selectionEnd;
    	var txtLen = tarea.value.length;
    	var txtbefore = tarea.value.substring(0,selEnd);
    	var txtafter =  tarea.value.substring(selEnd, txtLen);
    	tarea.value = txtbefore + ' ' + code + ' ' + txtafter;
	{rdelim} else {ldelim}
		document.entryform.field_name.value += code;
   {rdelim}
{rdelim}

function InsertCode(code,field_id,field_name)
{ldelim}
	var field_name = 'text';
	var field_id = 'msgform';
	if (document.getElementById(field_id).createTextRange)
	{ldelim}
		document.getElementById(field_id).focus();
		document.selection.createRange().duplicate().text = code;
	{rdelim}
	else if (document.getElementById && !document.all) // Mozilla
	{ldelim}
    	var tarea = document.getElementById(field_id);
    	var selEnd = tarea.selectionEnd;
    	var txtLen = tarea.value.length;
    	var txtbefore = tarea.value.substring(0,selEnd);
    	var txtafter =  tarea.value.substring(selEnd, txtLen);
    	tarea.value = txtbefore + code + txtafter;
	{rdelim} else {ldelim}
		document.entryform.field_name.value += code;
   {rdelim}
{rdelim}

function koobi_style_help(help) {ldelim}
	document.getElementById('koobi_style_helper').innerHTML = eval(help + "_help");
{rdelim}

function getarraysize(thearray) {ldelim}
	for (i = 0; i < thearray.length; i++) {ldelim}
		if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		{rdelim}
	return thearray.length;
{rdelim}

function arraypush(thearray,value) {ldelim}
	thearray[ getarraysize(thearray) ] = value;
{rdelim}

function arraypop(thearray) {ldelim}
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
{rdelim}

function closeall(){ldelim}
	if (ktags[0]) {ldelim}
		while (ktags[0]) {ldelim}
			tagRemove = popstack(ktags)
			document.f.text.value += "[/" + tagRemove + "]";
			if ( (tagRemove != 'FONT') && (tagRemove != 'SIZE') && (tagRemove != 'COLOR') )
			{ldelim}
				eval("document.f." + tagRemove + ".value = ' " + tagRemove + " '");
				eval(tagRemove + "_open = 0");
			{rdelim}
		{rdelim}
{rdelim}

	ktags = new Array();
	document.f.text.focus();
}


function checkForm() {ldelim}
	formErrors = false;
	if (document.f.text.value.length < 2) {ldelim}
	formErrors = "";
	{rdelim}

	if (formErrors) {ldelim}
		alert(formErrors);
		return false;
	{rdelim} else {ldelim}
		koobi_style(-1);
		return true;
	{rdelim}
{rdelim}

function emoticon(text) {ldelim}
	var txtarea = document.f.text;
	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) {ldelim}
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	{rdelim} else {ldelim}
		txtarea.value  += text;
		txtarea.focus();
	{rdelim}
{rdelim}

function koobifontstyle(koobiopen, koobiclose) {ldelim}
	var txtarea = document.f.text;

	if ((clientVer >= 4) && is_ie && is_win) {ldelim}
		theSelection = document.selection.createRange().text;
		if (!theSelection) {ldelim}
			txtarea.value += koobiopen + koobiclose;
			txtarea.focus();
			return;
		{rdelim}
		document.selection.createRange().text = koobiopen + theSelection + koobiclose;
		txtarea.focus();
		return;
	{rdelim}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{ldelim}
		mozWrap(txtarea, koobiopen, koobiclose);
		return;
	{rdelim}
	else
	{ldelim}
		txtarea.value += koobiopen + koobiclose;
		txtarea.focus();
	{rdelim}
	storeCaret(txtarea);
{rdelim}


function koobi_style(koobi_codenumber) {ldelim}
	var txtarea = document.f.text;

	txtarea.focus();
	donotinsert = false;
	theSelection = false;
	koobilast = 0;

	if (koobi_codenumber == -1) {ldelim}
		while (koobi__code[0]) {ldelim}
			butnumber = arraypop(koobi__code) - 1;
			txtarea.value += koobi_tags[butnumber + 1];
			buttext = eval('document.f.newkoobicode' + butnumber + '.value');
			eval('document.f.newkoobicode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
		{rdelim}
		imageTag = false; 
		txtarea.focus();
		return;
	{rdelim}

	if ((clientVer >= 4) && is_ie && is_win)
	{ldelim}
		theSelection = document.selection.createRange().text;
		if (theSelection) {ldelim}
			document.selection.createRange().text = koobi_tags[koobi_codenumber] + theSelection + koobi_tags[koobi_codenumber+1];
			txtarea.focus();
			theSelection = '';
			return;
		{rdelim}
	{rdelim}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{ldelim}
		mozWrap(txtarea, koobi_tags[koobi_codenumber], koobi_tags[koobi_codenumber+1]);
		return;
	{rdelim}

	for (i = 0; i < koobi__code.length; i++) {ldelim}
		if (koobi__code[i] == koobi_codenumber+1) {ldelim}
			koobilast = i;
			donotinsert = true;
		{rdelim}
	{rdelim}

	if (donotinsert) {ldelim}		
		while (koobi__code[koobilast]) {ldelim}
				butnumber = arraypop(koobi__code) - 1;
				txtarea.value += koobi_tags[butnumber + 1];
				buttext = eval('document.f.newkoobicode' + butnumber + '.value');
				eval('document.f.newkoobicode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
				imageTag = false;
			{rdelim}
			txtarea.focus();
			return;
	{rdelim} else {ldelim} 

		if (imageTag && (koobi_codenumber != 18)) {ldelim}
			txtarea.value += koobi_tags[19];
			lastValue = arraypop(koobi__code) - 1;
			imageTag = false;
		{rdelim}
		
		list_extended = (koobi_codenumber == 26) ? '\n{#Format_List_Points#}' : '';
		
		if(koobi_codenumber == 26)
		{ldelim}
			tag_list(); // Listen-Eingabeaufforderung 
		{rdelim} else if(koobi_codenumber == 18) {ldelim}
			tag_image();  // Bild-Eingabeaufforderung
		{rdelim} else if(koobi_codenumber == 14) {ldelim}
			tag_url(); // URL-Eingabeaufforderung
		{rdelim} else if(koobi_codenumber == 16) {ldelim}
			tag_email(); // E-Mail-Eingabeaufforderung
		{rdelim} else {ldelim}
			txtarea.value += koobi_tags[koobi_codenumber] + list_extended;
			if ((koobi_codenumber == 14) && (imageTag == false)) imageTag = 1; 
			arraypush(koobi__code,koobi_codenumber+1);
			eval('document.f.newkoobicode'+koobi_codenumber+'.value += ""');
			if(koobi_codenumber == 26) koobi_style(-1);
		{rdelim}
		txtarea.focus();
		return;
	{rdelim}
	storeCaret(txtarea);
{rdelim}

// Eingabeaufforderung fuer E-Mail
function tag_email(){ldelim}
    var emailAddress = prompt("{#Format_text_enter_email#}", "");
	if (!emailAddress) {ldelim} 
		//alert("{#Format_error_no_email#}"); 
		return; 
	{rdelim}
	doInsert("[mail]"+emailAddress+"[/mail]", "", false);
{rdelim}

// Eingabeaufforderung fuer URL
function tag_url(){ldelim}
    var FoundErrors = '';
    var enterURL   = prompt("{#Format_enterUrl#}", "http://");
    var enterTITLE = prompt("{#Format_enterUrlName#}", "{#Format_enterUrlName2#}");
	if (!enterURL) FoundErrors += " " + "{#Format_error_no_url#}";
    if (!enterTITLE) FoundErrors += " " + "{#Format_error_no_title#}";
	if (FoundErrors){ldelim}
        //alert(""+FoundErrors);
        return;
   {rdelim}
	doInsert("[url="+enterURL+"]"+enterTITLE+"[/url]", "", false);
{rdelim}

// Eingabeaufforderung fuer Bild
function tag_image(){ldelim}
    var FoundErrors = '';
    var enterURL   = prompt("{#Format_text_enter_image#}", "http://");

    if (!enterURL) {ldelim}
        FoundErrors += " " + "{#Format_error_no_url#}";
    {rdelim}

    if (FoundErrors) {ldelim}
        alert(""+FoundErrors);
        return;
    {rdelim}

	doInsert("[img]"+enterURL+"[/img]", "", false);
{rdelim}

// Eingabeaufforderung fuer Liste
function tag_list(){ldelim}
	list_prompt = "{#Format_listprompt#}";
	list_prompt2 = "{#Format_listprompt2#}";
	var listtype = prompt(list_prompt, "");

	if ((listtype == "a") || (listtype == "1") || (listtype == "i"))
	{ldelim}
		thelist = "[list=" + listtype + "]\n";
	{rdelim}
	else
	{ldelim}
		thelist = "[list]\n";
	{rdelim}
	var listentry = "initial";
	while ((listentry != "") && (listentry != null))
	{ldelim}
		listentry = prompt(list_prompt2, "");
		if ((listentry != "") && (listentry != null))
		{ldelim}
			thelist = thelist + "[*]" + listentry + "\n";
		{rdelim}
	{rdelim}
	doInsert(thelist + "[/list]\n", "", false);
{rdelim}


function doInsert(ktag, kctag, once){ldelim}
	var isClose = false;
	var obj_ta = document.f.text;

	if ((clientVer >= 4) && is_ie && is_win) {ldelim}
		if(obj_ta.isTextEdit){ldelim}
			obj_ta.focus();
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;
			if((sel.type == "Text" || sel.type == "None") && rng != null){ldelim}
				if(kctag != "" && rng.text.length > 0) ktag += rng.text + kctag;
			else if(once) isClose = true;
			rng.text = ktag;
			{rdelim}
		{rdelim} else {ldelim}
			if(once) isClose = true;
			obj_ta.value += ktag;
		{rdelim}
	{rdelim} else {ldelim}
		if(once) {ldelim}
			isClose = true;
		{rdelim}
		
		var tarea = document.getElementById('msgform');
    	var selEnd = tarea.selectionEnd;
    	var txtLen = tarea.value.length;
    	var txtbefore = tarea.value.substring(0,selEnd);
    	var txtafter =  tarea.value.substring(selEnd, txtLen);
		tarea.value = txtbefore + ktag + txtafter;
		obj_ta.value = tarea.value;
	{rdelim}
	obj_ta.focus();
	return isClose;
{rdelim}	

// From http://www.massless.org/mozedit/
function mozWrap(txtarea, open, close)
{ldelim}
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2)
		selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	return;
{rdelim}

function storeCaret(textEl) {ldelim}
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
{rdelim}
