// fR33$tyler´s bbcode - java functions

var bbtags   = 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));

// für beides
function setmode(modeValue) {
 	document.cookie = "bbcodemode="+modeValue+"; path=/; expires=Wed, 1 Jan 2025 00:00:00 GMT;";
}

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

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 pushArray(theArray, value) {
 	theArraySize = getArraySize(theArray);
 	theArray[theArraySize] = value;
}

function popArray(theArray) {
	theArraySize = getArraySize(theArray);
 	retVal = theArray[theArraySize - 1];
 	delete theArray[theArraySize - 1];
 	return retVal;
}

// für newstext
function s_smiley (code) {
		addText(" " + code, "", false, document.bbform);
		}


// für erweiterten newstext
function l_smiley (code) {
		ext_addText(" " + code, "", false, document.bbform);
         }

// für kommentare
function kom_smiley (code) {
		kom_addText(" " + code, "", false, document.bbform);
         }

// für newstext
function closetag(theForm) {
 	if (!normalMode(theForm)) {
  		if (bbtags[0]) addText("[/"+ popArray(bbtags) +"]", "", false, theForm);
  	}
 	
 	setFocus(theForm);
}

// für erweiterten newstext
function ext_closetag(theForm) {
 	if (!normalMode(theForm)) {
  		if (bbtags[0]) ext_addText("[/"+ popArray(bbtags) +"]", "", false, theForm);
  	}
 	
 	ext_setFocus(theForm);
}

// für kommentare
function kom_closetag(theForm) {
 	if (!normalMode(theForm)) {
  		if (bbtags[0]) kom_addText("[/"+ popArray(bbtags) +"]", "", false, theForm);
  	}
 	
 	kom_setFocus(theForm);
}

// für newstext
function closeall(theForm) {
 	if (!normalMode(theForm)) {
  		if (bbtags[0]) {
   			while (bbtags[0]) {
    				addText("[/"+ popArray(bbtags) +"]", "", false, theForm);
   			}
   		}
 	}
 	
 	setFocus(theForm);
}

// für erweiterten newstext
function ext_closeall(theForm) {
 	if (!normalMode(theForm)) {
  		if (bbtags[0]) {
   			while (bbtags[0]) {
    				ext_addText("[/"+ popArray(bbtags) +"]", "", false, theForm);
   			}
   		}
 	}
 	
 	ext_setFocus(theForm);
}

// für kommentare
function kom_closeall(theForm) {
 	if (!normalMode(theForm)) {
  		if (bbtags[0]) {
   			while (bbtags[0]) {
    				kom_addText("[/"+ popArray(bbtags) +"]", "", false, theForm);
   			}
   		}
 	}
 	
 	kom_setFocus(theForm);
}


// für newstext
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)) {
			pushArray(bbtags, theType);	
		}
	}
 
 	theForm.sizeselect.selectedIndex = 0;
 	theForm.fontselect.selectedIndex = 0;
 	theForm.colorselect.selectedIndex = 0;
 	
 	setFocus(theForm);
}

// für erweiterten newstext
function ext_fontformat(theForm,theValue,theType) {
 	ext_setFocus(theForm);
 
 	if (normalMode(theForm)) {
  		if (theValue != 0) {
   
   			var selectedText = ext_getSelectedText(theForm);
   			var insertText = prompt(font_formatter_prompt+" "+theType, selectedText);
   			if ((insertText != null) && (insertText != "")) {
    				ext_addText("["+theType+"="+theValue+"]"+insertText+"[/"+theType+"]", "", false, theForm);
    			}
  		}
 	}
 	else {
		if(ext_addText("["+theType+"="+theValue+"]", "[/"+theType+"]", true, theForm)) {
			pushArray(bbtags, theType);	
		}
	}
 
 	theForm.sizeselect.selectedIndex = 0;
 	theForm.fontselect.selectedIndex = 0;
 	theForm.colorselect.selectedIndex = 0;
 	
 	ext_setFocus(theForm);
}

// für kommentare
function kom_fontformat(theForm,theValue,theType) {
 	kom_setFocus(theForm);
 
 	if (normalMode(theForm)) {
  		if (theValue != 0) {
   
   			var selectedText = kom_getSelectedText(theForm);
   			var insertText = prompt(font_formatter_prompt+" "+theType, selectedText);
   			if ((insertText != null) && (insertText != "")) {
    				kom_addText("["+theType+"="+theValue+"]"+insertText+"[/"+theType+"]", "", false, theForm);
    			}
  		}
 	}
 	else {
		if(kom_addText("["+theType+"="+theValue+"]", "[/"+theType+"]", true, theForm)) {
			pushArray(bbtags, theType);	
		}
	}
 
 	theForm.sizeselect.selectedIndex = 0;
 	theForm.fontselect.selectedIndex = 0;
 	theForm.colorselect.selectedIndex = 0;
 	
 	kom_setFocus(theForm);
}

// für newstext
function bbcode(theForm, theTag, promptText) {
	if ( normalMode(theForm) || (bbcode=="IMG")) {
		var selectedText = getSelectedText(theForm);
		if (promptText == '' || selectedText != '') promptText = selectedText;
		
		inserttext = prompt(tag_prompt + "\n[" + theTag + "]xxx[/" + theTag + "]", promptText);
		if ( (inserttext != null) && (inserttext != "") ) {
			addText("[" + theTag + "]" + inserttext + "[/" + theTag + "]", "", false, theForm);
		}
	}
	else {
		var donotinsert = false;
  		for (i = 0; i < bbtags.length; i++) {
   			if (bbtags[i] == theTag) donotinsert = true;
  		}
  		
  		if (!donotinsert) {
   			if(addText("[" + theTag + "]", "[/" + theTag + "]", true, theForm)){
				pushArray(bbtags, theTag);
			}
  		}
		else {
			var lastindex = 0;
			
			for (i = 0 ; i < bbtags.length; i++ ) {
				if ( bbtags[i] == theTag ) {
					lastindex = i;
				}
			}
			
			while (bbtags[lastindex]) {
				tagRemove = popArray(bbtags);
				addText("[/" + tagRemove + "]", "", false, theForm);
			}
		}
	}
}

// funktion für erweiterten newstext
function ex_bbcode(theForm, theTag, promptText) {
	if ( normalMode(theForm) || (ex_bbcode=="IMG")) {
		var selectedText = ext_getSelectedText(theForm);
		if (promptText == '' || selectedText != '') promptText = selectedText;
		
		inserttext = prompt(tag_prompt + "\n[" + theTag + "]xxx[/" + theTag + "]", promptText);
		if ( (inserttext != null) && (inserttext != "") ) {
			ext_addText("[" + theTag + "]" + inserttext + "[/" + theTag + "]", "", false, theForm);
		}
	}
	else {
		var donotinsert = false;
  		for (i = 0; i < bbtags.length; i++) {
   			if (bbtags[i] == theTag) donotinsert = true;
  		}
  		
  		if (!donotinsert) {
   			if(ext_addText("[" + theTag + "]", "[/" + theTag + "]", true, theForm)){
				pushArray(bbtags, theTag);
			}
  		}
		else {
			var lastindex = 0;
			
			for (i = 0 ; i < bbtags.length; i++ ) {
				if ( bbtags[i] == theTag ) {
					lastindex = i;
				}
			}
			
			while (bbtags[lastindex]) {
				tagRemove = popArray(bbtags);
				ext_addText("[/" + tagRemove + "]", "", false, theForm);
			}
		}
	}
}

// funktion für kommentare
function kom_bbcode(theForm, theTag, promptText) {
	if ( normalMode(theForm) || (kom_bbcode=="IMG")) {
		var selectedText = kom_getSelectedText(theForm);
		if (promptText == '' || selectedText != '') promptText = selectedText;
		
		inserttext = prompt(tag_prompt + "\n[" + theTag + "]xxx[/" + theTag + "]", promptText);
		if ( (inserttext != null) && (inserttext != "") ) {
			kom_addText("[" + theTag + "]" + inserttext + "[/" + theTag + "]", "", false, theForm);
		}
	}
	else {
		var donotinsert = false;
  		for (i = 0; i < bbtags.length; i++) {
   			if (bbtags[i] == theTag) donotinsert = true;
  		}
  		
  		if (!donotinsert) {
   			if(kom_addText("[" + theTag + "]", "[/" + theTag + "]", true, theForm)){
				pushArray(bbtags, theTag);
			}
  		}
		else {
			var lastindex = 0;
			
			for (i = 0 ; i < bbtags.length; i++ ) {
				if ( bbtags[i] == theTag ) {
					lastindex = i;
				}
			}
			
			while (bbtags[lastindex]) {
				tagRemove = popArray(bbtags);
				kom_addText("[/" + tagRemove + "]", "", false, theForm);
			}
		}
	}
}


// für newstext
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://www.";
	}
		else if (theType == "EXURL") {
 		prompt_text = link_url_prompt;
 		prompt_contents = "http://www.dietle.de/relatedlink.php?link=http://www.";
	}
	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);
 	}
}

// für erweiterten newstext
function ext_namedlink(theForm,theType) {
	var selected = ext_getSelectedText(theForm);
 
	var linkText = prompt(link_text_prompt,selected);
	var prompttext;
 
	if (theType == "URL") {
 		prompt_text = link_url_prompt;
 		prompt_contents = "http://www.";
	}
		else if (theType == "EXURL") {
 		prompt_text = link_url_prompt;
 		prompt_contents = "http://www.dietle.de/relatedlink.php?link=http://www.";
	}
	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+"]";
		}
  		
  		ext_addText(theText, "", false, theForm);
 	}
}

// für kommentare
function kom_namedlink(theForm,theType) {
	var selected = kom_getSelectedText(theForm);
 
	var linkText = prompt(link_text_prompt,selected);
	var prompttext;
 
	if (theType == "URL") {
 		prompt_text = link_url_prompt;
 		prompt_contents = "http://www.";
	}
		else if (theType == "EXURL") {
 		prompt_text = link_url_prompt;
 		prompt_contents = "http://www.dietle.de/relatedlink.php?link=http://www.";
	}
	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+"]";
		}
  		
  		kom_addText(theText, "", false, theForm);
 	}
}

// für newstext
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);
}

// für erweiterten newstext
function ext_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";
 	}
 	
 	ext_addText(theList + listEend, "", false, theForm);
}


// für kommentare
function kom_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";
 	}
 	
 	kom_addText(theList + listEend, "", false, theForm);
}

// für newstext
function addText(theTag, theClsTag, isSingle, theForm)
{
	var isClose = false;
	var news_short = theForm.news_short;
	var set=false;
  	var old=false;
  	var selected="";
  	
  	if(navigator.appName=="Netscape" &&  news_short.textLength>=0 ) { // mozilla, firebird, netscape
  		if(theClsTag!="" && news_short.selectionStart!=news_short.selectionEnd) {
  			selected=news_short.value.substring(news_short.selectionStart,news_short.selectionEnd);
  			str=theTag + selected+ theClsTag;
  			old=true;
  			isClose = true;
  		}
		else {
			str=theTag;
		}
		
		news_short.focus();
		start=news_short.selectionStart;
		end=news_short.textLength;
		endtext=news_short.value.substring(news_short.selectionEnd,end);
		starttext=news_short.value.substring(0,start);
		news_short.value=starttext + str + endtext;
		news_short.selectionStart=start;
		news_short.selectionEnd=start;
		
		news_short.selectionStart = news_short.selectionStart + str.length;
		
		if(old) { return false; }
		
		set=true;
		
		if(isSingle) {
			isClose = false;
		}
	}
	if ( (myVersion >= 4) && is_ie && is_win) {  // Internet Explorer
		if(news_short.isTextEdit) {
			news_short.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) {
      				news_short.value += theTag;
      			}
		}
	}
	else
	{
		if(isSingle) isClose = true;

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

	news_short.focus();
	
	return isClose;
}	


// für erweiterten newstext
function ext_addText(theTag, theClsTag, isSingle, theForm)
{
	var isClose = false;
	var news_long = theForm.news_long;
	var ext_news_long = theForm.ext_news_long;
	var set=false;
  	var old=false;
  	var selected="";
  	
  	if(navigator.appName=="Netscape" &&  news_long.textLength>=0 ) { // mozilla, firebird, netscape
  		if(theClsTag!="" && news_long.selectionStart!=news_long.selectionEnd) {
  			selected=news_long.value.substring(news_long.selectionStart,news_long.selectionEnd);
  			str=theTag + selected+ theClsTag;
  			old=true;
  			isClose = true;
  		}
		else {
			str=theTag;
		}
		
		news_long.focus();
		start=news_long.selectionStart;
		end=news_long.textLength;
		endtext=news_long.value.substring(news_long.selectionEnd,end);
		starttext=news_long.value.substring(0,start);
		news_long.value=starttext + str + endtext;
		news_long.selectionStart=start;
		news_long.selectionEnd=start;
		
		news_long.selectionStart = news_long.selectionStart + str.length;
		
		if(old) { return false; }
		
		set=true;
		
		if(isSingle) {
			isClose = false;
		}
	}
	if ( (myVersion >= 4) && is_ie && is_win) {  // Internet Explorer
		if(news_long.isTextEdit) {
			news_long.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) {
      				news_long.value += theTag;
      			}
		}
	}
	else
	{
		if(isSingle) isClose = true;

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

	news_long.focus();
	
	return isClose;
}	

// für kommentare
function kom_addText(theTag, theClsTag, isSingle, theForm)
{
	var isClose = false;
	var kommentar = theForm.kommentar;
	var kom_kommentar = theForm.kom_kommentar;
	var set=false;
  	var old=false;
  	var selected="";
  	
  	if(navigator.appName=="Netscape" &&  kommentar.textLength>=0 ) { // mozilla, firebird, netscape
  		if(theClsTag!="" && kommentar.selectionStart!=kommentar.selectionEnd) {
  			selected=kommentar.value.substring(kommentar.selectionStart,kommentar.selectionEnd);
  			str=theTag + selected+ theClsTag;
  			old=true;
  			isClose = true;
  		}
		else {
			str=theTag;
		}
		
		kommentar.focus();
		start=kommentar.selectionStart;
		end=kommentar.textLength;
		endtext=kommentar.value.substring(kommentar.selectionEnd,end);
		starttext=kommentar.value.substring(0,start);
		kommentar.value=starttext + str + endtext;
		kommentar.selectionStart=start;
		kommentar.selectionEnd=start;
		
		kommentar.selectionStart = kommentar.selectionStart + str.length;
		
		if(old) { return false; }
		
		set=true;
		
		if(isSingle) {
			isClose = false;
		}
	}
	if ( (myVersion >= 4) && is_ie && is_win) {  // Internet Explorer
		if(kommentar.isTextEdit) {
			kommentar.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) {
      				kommentar.value += theTag;
      			}
		}
	}
	else
	{
		if(isSingle) isClose = true;

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

	kommentar.focus();
	
	return isClose;
}	

// für newstext
function getSelectedText(theForm) {
	var news_short = theForm.news_short;
	var selected = '';
	
	if(navigator.appName=="Netscape" &&  news_short.textLength>=0 && news_short.selectionStart!=news_short.selectionEnd ) 
  		selected=news_short.value.substring(news_short.selectionStart,news_short.selectionEnd);	
  	
	else if( (myVersion >= 4) && is_ie && is_win ) {
		if(news_short.isTextEdit){ 
			news_short.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;
}

// für erweiterten newstext
function ext_getSelectedText(theForm) {
	var news_long = theForm.news_long;
	var selected = '';
	
	if(navigator.appName=="Netscape" &&  news_long.textLength>=0 && news_long.selectionStart!=news_long.selectionEnd ) 
  		selected=news_long.value.substring(news_long.selectionStart,news_long.selectionEnd);	
  	
	else if( (myVersion >= 4) && is_ie && is_win ) {
		if(news_long.isTextEdit){ 
			news_long.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;
}


// für kommentare
function kom_getSelectedText(theForm) {
	var kommentar = theForm.kommentar;
	var selected = '';
	
	if(navigator.appName=="Netscape" &&  kommentar.textLength>=0 && kommentar.selectionStart!=kommentar.selectionEnd ) 
  		selected=kommentar.value.substring(kommentar.selectionStart,kommentar.selectionEnd);	
  	
	else if( (myVersion >= 4) && is_ie && is_win ) {
		if(kommentar.isTextEdit){ 
			kommentar.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;
}

// für newstext
function setFocus(theForm) {
 	theForm.news_short.focus();
}

// für erweiterten newstext
function ext_setFocus(theForm) {
 	theForm.news_long.focus();
}

// für kommentare
function kom_setFocus(theForm) {
 	theForm.kommentar.focus();
}

// bbinfo
function hrc () {

         win2=window.open("news/faq.php#6","neu","width=700,height=500,resizable=yes,screenX=1,screenY=1,scrollbars=yes,menubar=no");

         }