/*

textCode v2.00 Javascript (c) 2003 - 2004 by Alexander v. Weiss | www.vonweiss.de

* Bitte meinen Copyright Hinweis drin lassen, wenn Sie
* den Code verwenden wollen. Das Skript hier hat mich 
* viel Zeit gekostet. Dafür habe ich auch extra nicht den
* Quellcode auf Unleserlichkeit verstümmelt.
* Verbesserungsvorschläge nehme ich gern entgegen. 
*/

function help(form,str,bbbNr) {
    eval("document.forms[form].helpbox"+bbbNr+".value = str;");
}

var IEsel = 0; 
var IEselWasALT = 0;

function IEsaveSelection(event) { /* IE Fix (Textarea verliert Focus bei Benutzung eines Shortcuts. Wert muss sofort gespeichert werden) */
	if (document.selection)
		if (event.keyCode == 18) {
			IEselWasALT = 2;
			IEsel = document.selection.createRange();
		} else if (IEselWasALT == 2)
			IEselWasALT = 1;
		else {
			IEselWasALT = 0;
			IEsel = 0;
		}
}

function handleTextarea(todo,textarea,str) {
	/* todo: READ,WRITE */
	if (document.selection) { // InternetExplorer
		if (todo=="READ") {
			if (IEselWasALT)
				return IEsel.text;
			else
				return document.selection.createRange().text;
		}
		if (todo=="WRITE") {
			textarea.focus();
			if (IEselWasALT)
				var sel = IEsel.text = str;
			else
				var sel = document.selection.createRange().text = str;
			return;
		}
	} else if (document.getElementById && !document.all) { // Mozilla, Netscape
		var selLength = textarea.textLength;
		var selStart = textarea.selectionStart;
		var selEnd = textarea.selectionEnd;
		if (todo=="READ")
			return (textarea.value).substring(selStart, selEnd);
		if (todo=="WRITE") {
			var s1 = (textarea.value).substring(0,selStart);
			var s2 = (textarea.value).substring(selEnd, selLength);
			textarea.value = s1 + str + s2;
			textarea.focus();
			return;
		}
	} else {
		if (todo=="READ") {
		}
		if (todo=="WRITE") {
			if (textarea.createTextRange) {
				textarea.focus();
				document.selection.createRange().duplicate().text = str;
			} else if (document.getElementById && !document.all) { // Mozilla
				var selEnd = textarea.selectionEnd;
				var txtLen = textarea.value.length;
				var s1 = textarea.value.substring(0,selEnd);
				var s2 =  textarea.value.substring(selEnd, txtLen);
				textarea.value = s1 + str + s2;
			} else {
				textarea.value += str;
			}
		}
	}
}

function stacksize(thearray) {
    for (i = 0 ; i < thearray.length; i++ ) {
        if ( (thearray[i] == "") || (thearray[i] == null) || (thearray == 'undefined') ) {
            return i;
        }
    }
    return thearray.length;
}

function pushstack(thearray, newval) {
    arraysize = stacksize(thearray);
    thearray[arraysize] = newval;
}

function popstack(thearray) {
    arraysize = stacksize(thearray);
    theval = thearray[arraysize - 1];
    delete thearray[arraysize - 1];
    return theval;
}

function valHex(wert) {
	if (wert.length != 6)
		return "0";
	i=0; j=1;
	val=0;
	textVal="#" + wert;
	while (j<textVal.length) {
		i++; j++;
		if ((textVal.length==7) && ((textVal.substring(i,j) == 0) || (textVal.substring(i,j) == 1) || (textVal.substring(i,j) == 2) || (textVal.substring(i,j) == 3) || (textVal.substring(i,j) == 4) || (textVal.substring(i,j) == 5) || (textVal.substring(i,j) == 6) || (textVal.substring(i,j) == 7) || (textVal.substring(i,j) == 8) || (textVal.substring(i,j) == 9)) || ((textVal.substring(i,j) == "A") || (textVal.substring(i,j) == "a") || (textVal.substring(i,j) == "B") || (textVal.substring(i,j) == "b") || (textVal.substring(i,j) == "C") || (textVal.substring(i,j) == "c") || (textVal.substring(i,j) == "D") || (textVal.substring(i,j) == "d") || (textVal.substring(i,j) == "E") || (textVal.substring(i,j) == "e") || (textVal.substring(i,j) == "F") || (textVal.substring(i,j) == "f"))) {
			val="1";
		} else {
			val="0";
			j=textVal.length
		}
	}
	return val;
}

function isUndefined(a) {
	return typeof(a) == 'undefined';
}

function textCode(form,text,button,buttonvalue,selected,typ) {
	if (eval("typeof textCodeTags"+text+" == \"undefined\""))
		eval("textCodeTags"+text+" = new Array();");
	eval("var textCodeTags = textCodeTags"+text); 
	var textarea = document.forms[form].elements[text];
	var code = buttonvalue.toLowerCase();
	var result = handleTextarea("READ",textarea,"");
	var var_open = "_"+ text +"_open";
	
	if (typ==0) {
	/* Typ 0: Alle Tags schließen */
		lastindex = 0;
		while (textCodeTags[lastindex]) {
			tagRemove = popstack(textCodeTags);
			handleTextarea("WRITE",textarea,"[/"+tagRemove.toLowerCase()+"]");
			if (tagRemove != "Color")
				eval("document.forms[form].elements['textCode_"+text+"_"+tagRemove+"'].value = '"+tagRemove+"'");
			eval(tagRemove + var_open + " = 0");
		}
	}

	if (typ==1 || typ==2 || typ==6) {
	/* Typ 1: B, I, U, Flag, Toggle, Table, Img */
	/* Typ 2: ColorSelect */
	/* Typ 6: ToolTip (Mit Titel Eingabeaufforderung) */
		var test = buttonvalue+var_open;
		if (eval("typeof "+test+" == \"undefined\""))
			eval(buttonvalue + var_open + " = 0");
		var tagOpen = eval(buttonvalue + var_open);
		if (result=="") {
			if (tagOpen == 0) {
				eval(buttonvalue + var_open + " = 1");
				pushstack(textCodeTags, buttonvalue);
				if (typ==1) {
					handleTextarea("WRITE",textarea,"["+code+"]");
					eval("button.value = '\"" + buttonvalue + "\"'");
				} else if (typ==2) {
					handleTextarea("WRITE",textarea,"["+code+"="+selected+"]");
				} else if (typ==6) {
					dertitel = prompt("Titel - Der der Text, über den man mit der Maus fährt, damit der Tooltip angezeigt wird.","");
					if ((dertitel != null) && (dertitel != "")) {
						handleTextarea("WRITE",textarea,"["+code+"="+dertitel+"]");
						eval("button.value = '\"" + buttonvalue + "\"'");
					} else {
						eval(buttonvalue + var_open + " = 0");
					}
				}
			} else {
				lastindex = 0;
				for (i = 0 ; i < textCodeTags.length; i++ ) {
					if ( textCodeTags[i] == buttonvalue ) {
						lastindex = i;
					}
				}
				while (textCodeTags[lastindex]) {
					tagRemove = popstack(textCodeTags);
					handleTextarea("WRITE",textarea,"[/"+tagRemove.toLowerCase()+"]");
					if (tagRemove != "Color")
						eval("document.forms[form].elements['textCode_"+text+"_"+tagRemove+"'].value = '"+tagRemove+"'");
					eval(tagRemove + var_open + " = 0");
				}
			}
		} else {
			if (typ==1) {
				handleTextarea("WRITE",textarea,"["+code+"]"+result+"[/"+code+"]");
			} else if (typ==2) {
				if (valHex(result) == "1")
					handleTextarea("WRITE",textarea,selected);
				else
					handleTextarea("WRITE",textarea,"["+code+"="+selected+"]"+result+"[/"+code+"]");
			} else if (typ==6) {
				dertitel = prompt("Titel - Der der Text, über den man mit der Maus fährt, damit der Tooltip angezeigt wird.","");
				if ((dertitel != null) && (dertitel != ""))
					handleTextarea("WRITE",textarea,"["+code+"="+dertitel+"]"+result+"[/"+code+"]");
				else
					handleTextarea("WRITE",textarea,selected);
			}
		}
	}
	
	if (typ==3) {
	/* Typ 3: URL */
		linkurl = prompt("URL (z.B. www.vonweiss.de)","");
		if ((linkurl != null) && (linkurl != "")) {
			linktext = prompt("Die Beschriftung (optional)","");
			if ((linktext != null) && (linktext != ""))
				handleTextarea("WRITE",textarea,"[url="+linkurl+"]"+linktext+"[/url]");
			else
				handleTextarea("WRITE",textarea,"[url]"+linkurl+"[/url]");
		}
	}

	if (typ==4) {
	/* Typ 4: FlagSelect */
		if (selected == 0)
			return;
		handleTextarea("WRITE",textarea,"[flag]" + selected + "[/flag]");
	}

	if (typ==5) {
	/* Typ 5: Smilie,Other */
		handleTextarea("WRITE",textarea,selected);
	}

	if (typ==2 || typ==4) 
		eval("document.forms[form].elements['textCode_"+text+"_"+buttonvalue+"'].selectedIndex = 0");

	eval("textCodeTags"+text+" = textCodeTags;"); 
}
