var 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 normalMode(theForm) {
	if (theForm.mode[0].checked) {
		return true;
	}
	else {
		return false;
	}
}


function textcode(theForm, theTag, promptText) {
	if ( normalMode(theForm) || (textcode=="IMG")) {
		var selectedText = getSelectedText(theForm);
		if (promptText == '' || selectedText != '') promptText = selectedText;
		
		inserttext = prompt(w_text_prompt + "\n[" + theTag + "]xxx[/" + theTag + "]", promptText);
		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)){
				pushArray(tags, theTag);
			}
  		}
		else {
			var lastindex = 0;
			
			for (i = 0 ; i < tags.length; i++ ) {
				if ( tags[i] == theTag ) {
					lastindex = i;
				}
			}
			
			while (tags[lastindex]) {
				tagRemove = popArray(tags);
				addText("[/" + tagRemove + "]", "", false, theForm);
			}
		}
	}
}


function bildcode(theForm, theTag, promptText) {
	if ( normalMode(theForm) || (bildcode=="IMG")) {
		var selectedText = getSelectedText(theForm);
		if (promptText == '' || selectedText != '') promptText = selectedText;
		
		inserttext = prompt(w_link_picture_prompt + "\n", promptText);
		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)){
				pushArray(tags, theTag);
			}
  		}
		else {
			var lastindex = 0;
			
			for (i = 0 ; i < tags.length; i++ ) {
				if ( tags[i] == theTag ) {
					lastindex = i;
				}
			}
			
			while (tags[lastindex]) {
				tagRemove = popArray(tags);
				addText("[/" + tagRemove + "]", "", false, theForm);
			}
		}
	}
}

function urlcode(theForm,theType) {
	var selected = getSelectedText(theForm);
 
	var linkText = prompt(w_link_text_prompt,selected);
	var prompttext;
 
	if (theType == "URL") {
 		prompt_text = w_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 addText(theTag, theClsTag, isSingle, theForm)
{
	var isClose = false;
	var text = theForm.text;
	var set=false;
  	var old=false;
  	var selected="";
  	
  	if(navigator.appName=="Netscape" &&  text.textLength>=0 ) { // mozilla, firebird, netscape
  		if(theClsTag!="" && text.selectionStart!=text.selectionEnd) {
  			selected=text.value.substring(text.selectionStart,text.selectionEnd);
  			str=theTag + selected+ theClsTag;
  			old=true;
  			isClose = true;
  		}
		else {
			str=theTag;
		}
		
		text.focus();
		start=text.selectionStart;
		end=text.textLength;
		endtext=text.value.substring(text.selectionEnd,end);
		starttext=text.value.substring(0,start);
		text.value=starttext + str + endtext;
		text.selectionStart=start;
		text.selectionEnd=start;
		
		text.selectionStart = text.selectionStart + str.length;
				
		if(old) { return false; }
		
		set=true;
		
		if(isSingle) {
			isClose = false;
		}
	}
	if ( (myVersion >= 4) && is_ie && is_win) {  // Internet Explorer
		if(text.isTextEdit) {
			text.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) {
      				text.value += theTag;
      			}
		}
	}
	else
	{
		if(isSingle) isClose = true;

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

	text.focus();
	
	return isClose;
}	


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


