// *******************************************************
// Sprache ***********************************************
// *******************************************************
tag_prompt = "Gebe einen Text ein:";
youtube_prompt = "Geben Sie hier die YouTubeID zu Ihrem Video ein";
myvideo_prompt = "Geben Sie hier die MyVideoID zu Ihrem Video ein";
img_prompt = "Bitte gebe die Adresse zum Bild ein:";
font_formatter_prompt = "Gebe einen Text ein - ";
link_text_prompt = "Gebe einen Linknamen ein (optional)";
link_url_prompt = "Gebe die volle Adresse des Links ein";
link_email_prompt = "Gebe eine Email Adesse ein";
list_type_prompt = "Was für eine Liste möchtest du? Gebe '1' ein für eine nummerierte Liste, 'a' für ein alphabetische, oder gar nichts für eine einfache Punktliste.";
list_item_prompt = "Gebe eine Listenpunkt ein.\nGebe nichts ein oder drücke 'Cancel' um die Liste fertigzustellen.";
// *******************************************************

tags = new Array();

// browser detection
var myAgent   = navigator.userAgent.toLowerCase();
var myVersion = parseInt(navigator.appVersion);
var is_ie   = ((myAgent.indexOf("msie") != -1)  && (myAgent.indexOf("opera") == -1));
var is_win   =  ((myAgent.indexOf("win")!=-1) || (myAgent.indexOf("16bit")!=-1));


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

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

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

// *******************************************************

function setmode(modevalue) {
document.cookie = "bbcodemode="+modevalue+"; path=/; expires=Wed, 1 Jan 2020 00:00:00 GMT;";
}

function normalmode(theform) {
if (theform.mode[0].checked) return true;
else return false;
}

function stat(thevalue) {
document.bbform.status.value = eval(thevalue+"_text");
}

function setfocus(theform) {
theform.message.focus();
}

function closetag(theform) {
if (!normalmode(theform)) {
if (tags[0]) theform.message.value += "[/"+ arraypop(tags) +"]";
}
setfocus(theform);
}

function closeall(theform) {
if (!normalmode(theform)) {
if (tags[0]) {
while (tags[0]) {
theform.message.value += "[/"+ arraypop(tags) +"]";
}
theform.message.value += " ";
}
}
setfocus(theform);
}

// *******************************************************
var selectedText = "";
AddTxt = "";

function getActiveText(msg) { 
selectedText = (document.all) ? document.selection.createRange().text : document.getSelection();
if (msg.createTextRange) msg.caretPos = document.selection.createRange().duplicate();
return true;
}

function AddText(NewCode,theform) {
if (theform.message.createTextRange && theform.message.caretPos) {
var caretPos = theform.message.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? NewCode + ' ' : NewCode;
} else theform.message.value+=NewCode
AddTxt = "";
setfocus(theform);
}

function normalMode(theForm) {
if (theForm.mode[0].checked) {
return true;
}
else {
return false;
}
}

function addText(theTag, theClsTag, isSingle, theForm)
{
var isClose = false;
var message = theForm.message;
var set=false;
var old=false;
var selected="";

if(message.textLength>=0 ) { // mozilla, firebird, netscape
if(theClsTag!="" && message.selectionStart!=message.selectionEnd) {
selected=message.value.substring(message.selectionStart,message.selectionEnd);
str=theTag + selected+ theClsTag;
old=true;
isClose = true;
}
else {
str=theTag;
}

message.focus();
start=message.selectionStart;
end=message.textLength;
endtext=message.value.substring(message.selectionEnd,end);
starttext=message.value.substring(0,start);
message.value=starttext + str + endtext;
message.selectionStart=start;
message.selectionEnd=start;

message.selectionStart = message.selectionStart + str.length;

if(old) { return false; }

set=true;

if(isSingle) {
isClose = false;
}
}
if ( (myVersion >= 4) && is_ie && is_win) {  // Internet Explorer
if(message.isTextEdit) {
message.focus();
var sel = document.selection;
var rng = sel.createRange();
rng.colapse;
if((sel.type == "Text" || sel.type == "None") && rng != null){
if(theClsTag != "" && rng.text.length > 0)
theTag += rng.text + theClsTag;
else if(isSingle)
isClose = true;

rng.text = theTag;
}
}
else{
if(isSingle) isClose = true;

if(!set) {
message.value += theTag;
}
}
}
else
{
if(isSingle) isClose = true;

if(!set) {
message.value += theTag;
}
}

message.focus();

return isClose;
}	

function getSelectedText(theForm) {
var message = theForm.message;
var selected = '';

if(navigator.appName=="Netscape" &&  message.textLength>=0 && message.selectionStart!=message.selectionEnd ) 
selected=message.value.substring(message.selectionStart,message.selectionEnd);	

else if( (myVersion >= 4) && is_ie && is_win ) {
if(message.isTextEdit){ 
message.focus();
var sel = document.selection;
var rng = sel.createRange();
rng.colapse;

if((sel.type == "Text" || sel.type == "None") && rng != null){
if(rng.text.length > 0) selected = rng.text;
}
}	
}

return selected;
}

function bbcode(theForm, theTag, promptText) {
	if ( normalMode(theForm) || (theTag=="IMG")) {
		var selectedText = getSelectedText(theForm);
		if (promptText == '' || selectedText != '') promptText = selectedText;
		
		switch (theTag) {
		   default: inserttext = prompt(((theTag == "IMG") ? (img_prompt) : (tag_prompt)) + "\n[" + theTag + "]xxx[/" + theTag + "]", promptText); break;
		   case 'youtube': inserttext = prompt(youtube_prompt+"\n["+theTag+"]Ihre YouTube ID[/"+theTag+"]",promptText); break; 
		   case 'myvideo': inserttext = prompt(myvideo_prompt+"\n["+theTag+"]Ihre MyVideo ID[/"+theTag+"]",promptText); break;
		}

		if ( (inserttext != null) && (inserttext != "") ) {
		addText("[" + theTag + "]" + inserttext + "[/" + theTag + "]", "", false, theForm);
		}
	}
	else {
		var donotinsert = false;
		for (i = 0; i < tags.length; i++) {
		if (tags[i] == theTag) donotinsert = true;
		}

		if (!donotinsert) {
			if(addText("[" + theTag + "]", "[/" + theTag + "]", true, theForm)){
			arraypush(tags, theTag);
			}
		}
		else {
			var lastindex = 0;

			for (i = 0 ; i < tags.length; i++ ) {
				if ( tags[i] == theTag ) {
				lastindex = i;
				}
			}

			while (tags[lastindex]) {
			tagRemove = arraypop(tags);
			addText("[/" + tagRemove + "]", "", false, theForm);
			}
		}
	}
}


// *******************************************************

function fontformat(theForm,theValue,theType) {
setFocus(theForm);

if (normalMode(theForm)) {
if (theValue != 0) {

var selectedText = getSelectedText(theForm);
var insertText = prompt(font_formatter_prompt+" "+theType, selectedText);
if ((insertText != null) && (insertText != "")) {
addText("["+theType+"="+theValue+"]"+insertText+"[/"+theType+"]", "", false, theForm);
}
}
}
else {
if(addText("["+theType+"="+theValue+"]", "[/"+theType+"]", true, theForm)) {
arraypush(tags, theType);	
}
}

theForm.sizeselect.selectedIndex = 0;
theForm.fontselect.selectedIndex = 0;
theForm.colorselect.selectedIndex = 0;

setFocus(theForm);
}

function setFocus(theForm) {
theForm.message.focus();
}

// *******************************************************

function namedlink(theForm,theType) {
var selected = getSelectedText(theForm);

var linkText = prompt(link_text_prompt,selected);
var prompttext;

if (theType == "URL") {
prompt_text = link_url_prompt;
prompt_contents = "http://";
}
else {
prompt_text = link_email_prompt;
prompt_contents = "";
}

linkURL = prompt(prompt_text,prompt_contents);


if ((linkURL != null) && (linkURL != "")) {
var theText = '';

if ((linkText != null) && (linkText != "")) {
theText = "["+theType+"="+linkURL+"]"+linkText+"[/"+theType+"]";
}
else {
theText = "["+theType+"]"+linkURL+"[/"+theType+"]";
}

addText(theText, "", false, theForm);
}
}

// *******************************************************

function dolist(theForm) {
listType = prompt(list_type_prompt, "");
if ((listType == "a") || (listType == "1")) {
theList = "[list="+listType+"]\n";
listEend = "[/list="+listType+"] ";
}
else {
theList = "[list]\n";
listEend = "[/list] ";
}

listEntry = "initial";
while ((listEntry != "") && (listEntry != null)) {
listEntry = prompt(list_item_prompt, "");
if ((listEntry != "") && (listEntry != null)) theList = theList+"[*]"+listEntry+"\n";
}

addText(theList + listEend, "", false, theForm);
}

// *******************************************************

function smilie(theSmilie) {
addText(" " + theSmilie, "", false, document.bbform);
}

function opensmiliewindow(x,y,sid) {
window.open("misc.php?action=moresmilies&sid="+sid, "smilies", "toolbar=no,scrollbars=yes,resizable=yes,width="+x+",height="+y);
}


function mk_popup(url,width,height,scrollbar) 
{ 
 var breite=width; 
 var hoehe=height; 
 var positionX=0; 
 var positionY=0;  
 pop=window.open(url,'Originalgröße','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars='+scrollbar+',resizable=0,fullscreen=0,width='+breite+',height='+hoehe+',top=10000,left=10000'); 
 pop.resizeTo(breite,hoehe); 
 pop.moveTo(positionX,positionY); 
}

function schild(theform,thetype) {  
    if (thetype == "schild") { 
     prompt_text = "Text im Schild";   
    }  
    if (selectedText) var dtext=selectedText;
    else var dtext="";
    insertschild = prompt(prompt_text,dtext); 
    if ((insertschild != null) && (insertschild != "")) { 
     AddTxt = "["+thetype+"]"+insertschild+"[/"+thetype+"] "; 
     AddText(AddTxt,theform); 
    } 
} 

function denkblase(theform,thetype) { 
    if (thetype == "denk") { 
     prompt_text = "Text in der Denkblase"; 
    } 
    if (selectedText) var dtext=selectedText;
    else var dtext="";
    insertdenk = prompt(prompt_text,dtext); 
    if ((insertdenk != null) && (insertdenk != "")) { 
     AddTxt = "["+thetype+"]"+insertdenk+"[/"+thetype+"] "; 
     AddText(AddTxt,theform); 
    } 
} 

