TextEditor = document.getElementsByTagName("textarea");
function setEditor() {
	FocusedElement = TextEditor.length-1;
	if(TextEditor[0]){
		for(i=0;i<TextEditor.length;i++){
			if(TextEditor[i].className.match("no_editor") != "no_editor"){
				setCursor(TextEditor[i], i)
				var EditorDiv = document.createElement('div');
				BrowserStyle = "margin:1px 0px -1px 0px;";
				TextEditor[i].style.height = TextEditor[i].style.height.substr(0, TextEditor[i].style.height.length-2) - 26 + "px";

				if(TextEditor[i].className.match("FullWidthElm") == "FullWidthElm"){
					EditorWidth="100%";
					EditorPaddingRight="padding-right:4px;"
				} else {
					EditorPaddingRight="";
					EditorWidth = (TextEditor[i].offsetWidth-2-4) + "px";
				}
				
				EditorHTML = "<div style=\"border:1px solid #787878;padding:2px;"+EditorPaddingRight+"border-bottom:0px;background-color:#EEEEEE;width:"+ EditorWidth +";"+ BrowserStyle +"\"\">";
				EditorHTML += "<img onclick=\"addFormat('"+ i +"', 'b');\" style=\"border:1px solid #EEEEEE;\" onmouseover=\"butActive(this);\" onmouseout=\"butUnActive(this);\" id=\"b\" src=\"" + MainPath +"images/icons/bold.gif\" title=\"Fed tekst\" />";
				EditorHTML += "<img onclick=\"addFormat('"+ i +"', 'i');\" style=\"border:1px solid #EEEEEE;\" onmouseover=\"butActive(this);\" onmouseout=\"butUnActive(this);\" id=\"i\" src=\"" + MainPath +"images/icons/italic.gif\" title=\"Kursiv tekst\" />";
				EditorHTML += "<img onclick=\"addFormat('"+ i +"', 'u');\" style=\"border:1px solid #EEEEEE;margin-right:5px;\" onmouseover=\"butActive(this);\" onmouseout=\"butUnActive(this);\" id=\"u\" src=\"" + MainPath +"images/icons/underline.gif\" title=\"Understreget tekst\" />";
				EditorHTML += "<img onclick=\"addLink('"+ i +"');\" style=\"border:1px solid #EEEEEE;\" onmouseover=\"butActive(this);\" onmouseout=\"butUnActive(this);\" id=\"url\" src=\"" + MainPath +"images/icons/link.gif\" title=\"Indsæt link\" />";
				EditorHTML += "<img onclick=\"addImage('"+ i +"');\" style=\"border:1px solid #EEEEEE;margin-right:5px;\" onmouseover=\"butActive(this);\" onmouseout=\"butUnActive(this);\" id=\"img\" src=\"" + MainPath +"images/icons/image.gif\" title=\"Indsæt billede\" />";
				EditorHTML += "<img onclick=\"addFormat('"+ i +"', 'h1');\" style=\"border:1px solid #EEEEEE;\" onmouseover=\"butActive(this);\" onmouseout=\"butUnActive(this);\" id=\"img\" src=\"" + MainPath +"images/icons/h1.gif\" title=\"Overskrift 1\" />";
				EditorHTML += "<img onclick=\"addFormat('"+ i +"', 'h2');\" style=\"border:1px solid #EEEEEE;\" onmouseover=\"butActive(this);\" onmouseout=\"butUnActive(this);\" id=\"img\" src=\"" + MainPath +"images/icons/h2.gif\" title=\"Overskrift 2\" />";
				EditorHTML += "<img onclick=\"addFormat('"+ i +"', 'h3');\" style=\"border:1px solid #EEEEEE;margin-right:5px;\" onmouseover=\"butActive(this);\" onmouseout=\"butUnActive(this);\" id=\"img\" src=\"" + MainPath +"images/icons/h3.gif\" title=\"Overskrift 3\" />";
				EditorHTML += "<img onclick=\"addFormat('"+ i +"', 'quote');\" style=\"border:1px solid #EEEEEE;\" onmouseover=\"butActive(this);\" onmouseout=\"butUnActive(this);\" id=\"img\" src=\"" + MainPath +"images/icons/quote.gif\" title=\"Indsæt citation\" />";
				EditorHTML += "<img onclick=\"addFormat('"+ i +"', 'code');\" style=\"border:1px solid #EEEEEE;\" onmouseover=\"butActive(this);\" onmouseout=\"butUnActive(this);\" id=\"img\" src=\"" + MainPath +"images/icons/code.gif\" title=\"Indsæt kodeboks\" />";
				EditorHTML += "<img onclick=\"addFormat('"+ i +"', 'tag');\" style=\"border:1px solid #EEEEEE;\" onmouseover=\"butActive(this);\" onmouseout=\"butUnActive(this);\" id=\"img\" src=\"" + MainPath +"images/icons/tag.gif\" title=\"Indsæt tag\" />";
				EditorHTML += "</div>";
				EditorDiv.innerHTML = EditorHTML;
				TextEditor[i].parentNode.insertBefore(EditorDiv, TextEditor[i]);
			}
		}
	}
}

function butActive(obj){
	obj.style.backgroundColor = "#CFE1F9";
	obj.style.border = "1px solid #30559C";
}

function butUnActive(obj){
	obj.style.backgroundColor = "";
	obj.style.border = "1px solid #EEEEEE";
}

function addFormat(pArea, pFormat){
	TextEditor[pArea].focus();
	startTxt = "["+pFormat+"]";
	endTxt = "[/"+pFormat+"]";
	if(document.all){
		Range = document.selection.createRange();
		if(pFormat == "quote" || pFormat == "code")
			Range.text = startTxt + "\n" + Range.text + "\n" + endTxt;
		else
			Range.text = startTxt + Range.text + endTxt;
			Range.select();
	} else {
		cursorpos = TextEditor[pArea].selectionEnd+startTxt.length+endTxt.length;
		selectionTxt = TextEditor[pArea].value.substr(TextEditor[pArea].selectionStart, TextEditor[pArea].selectionEnd-TextEditor[pArea].selectionStart);
		if(pFormat == "quote" || pFormat == "code"){	
			selectionTxt = startTxt + "\n" + selectionTxt + "\n" + endTxt;
			cursorpos = cursorpos + 2;
		} else {
			selectionTxt = startTxt + selectionTxt + endTxt;
		}
		TextEditor[pArea].value = TextEditor[pArea].value.substr(0, TextEditor[pArea].selectionStart) + selectionTxt + TextEditor[pArea].value.substr(TextEditor[pArea].selectionEnd);
		TextEditor[pArea].setSelectionRange(cursorpos, cursorpos);
	}
	TextEditor[pArea].focus();
}

function addAttachmentFormat(pFormat, pFilename){
	TextEditor[FocusedElement].focus();
	if(pFormat == "image"){
		startTxt = "[img att="+ pFilename +"]";
		endTxt = "";
	} else if(pFormat == "link"){
		startTxt = "[url att="+ pFilename +"]";
		endTxt = "[/url]";	
	}
	if(document.all){
		Range = document.selection.createRange();
		if(pFormat == "image"){
			Range.text = startTxt + endTxt;
		} else {
			if(Range.text != ""){
				centerTxt = Range.text
			} else {
				centerTxt = prompt("Indtast navnet på linket", "");
				if(centerTxt == null)
				return false;
			}
			Range.text = startTxt + centerTxt + endTxt;
			Range.select();
		}
	} else {
		selectionTxt = TextEditor[FocusedElement].value.substr(TextEditor[FocusedElement].selectionStart, TextEditor[FocusedElement].selectionEnd-TextEditor[FocusedElement].selectionStart);
		if(pFormat == "image"){	
			cursorpos = TextEditor[FocusedElement].selectionStart+startTxt.length+endTxt.length;
			selectionTxt = startTxt + endTxt;
		} else {
			if(selectionTxt != ""){
				centerTxt = selectionTxt;
			} else {
				centerTxt = prompt("Indtast navnet på linket", "");
				if(centerTxt == null)
				return false;
			}
			cursorpos = TextEditor[FocusedElement].selectionStart+centerTxt.length+startTxt.length+endTxt.length;
			selectionTxt = startTxt + centerTxt + endTxt;
		}
		TextEditor[FocusedElement].value = TextEditor[FocusedElement].value.substr(0, TextEditor[FocusedElement].selectionStart) + selectionTxt + TextEditor[FocusedElement].value.substr(TextEditor[FocusedElement].selectionEnd);
		TextEditor[FocusedElement].setSelectionRange(cursorpos, cursorpos);
	}
	TextEditor[FocusedElement].focus();
}

function setCursor(pObj, pI){
	pObj.onfocus = function() {
		FocusedElement = pI;
	}
}

function addLink(pArea) {
	TextEditor[pArea].focus();
	linkAddress = prompt('Indtast adressen på linket:', 'http://');				
	if ((linkAddress != null) && (linkAddress != "") && (linkAddress != "http://")) {
		startTxt = "[url="+ linkAddress +"]";
		endTxt = "[/url]";
		if(document.all){
			Range = document.selection.createRange();
				if(Range.text == ""){
					linkName = prompt('Indtast navnet på linket', '');
					centerTxt = linkName;
				} else {
					centerTxt = Range.text;
				}
			Range.text = startTxt + centerTxt + endTxt;
			Range.select();
		} else {
			selectionTxt = TextEditor[pArea].value.substr(TextEditor[pArea].selectionStart, TextEditor[pArea].selectionEnd-TextEditor[pArea].selectionStart);		
				if(selectionTxt == ""){
					linkName = prompt('Indtast navnet på linket', '');
					centerTxt = linkName;
				} else {
					centerTxt = selectionTxt;
				}
			cursorpos = TextEditor[pArea].selectionStart+centerTxt.length+linkAddress.length+startTxt.length+endTxt.length;
			selectionTxt = startTxt + centerTxt + endTxt;
			TextEditor[pArea].value = TextEditor[pArea].value.substr(0, TextEditor[pArea].selectionStart) + selectionTxt + TextEditor[pArea].value.substr(TextEditor[pArea].selectionEnd);
			TextEditor[pArea].setSelectionRange(cursorpos, cursorpos);
		}
	}
	TextEditor[pArea].focus();
}

function addImage(pArea) {
	TextEditor[pArea].focus();
	imageAddress = prompt('Indtast adressen på billedet:', 'http://');				
	if ((imageAddress != null) && (imageAddress != "") && (imageAddress != "http://")) {
		startTxt = "[img]";
		endTxt = "[/img]";
		if(document.all){
			Range = document.selection.createRange();
			Range.text = startTxt + imageAddress + endTxt;
			Range.select();
		} else {
			cursorpos = TextEditor[pArea].selectionEnd+imageAddress.length+startTxt.length+endTxt.length;
			selectionTxt = TextEditor[pArea].value.substr(TextEditor[pArea].selectionStart, TextEditor[pArea].selectionEnd-TextEditor[pArea].selectionStart);		
			selectionTxt = startTxt + imageAddress + endTxt;
			TextEditor[pArea].value = TextEditor[pArea].value.substr(0, TextEditor[pArea].selectionStart) + selectionTxt + TextEditor[pArea].value.substr(TextEditor[pArea].selectionEnd);
			TextEditor[pArea].setSelectionRange(cursorpos, cursorpos);
		}
	}
	TextEditor[pArea].focus();
}