var ns6=document.getElementById && !document.all;
var ie = document.all
function close_tag(tag) {
	if(ns6) {
		var pos = document.getElementById('topic_message_body').selectionStart;
		var txtlength = document.getElementById('topic_message_body').textLength;
		var s1 = document.getElementById('topic_message_body').value.substring(0,pos);
		var s2 = document.getElementById('topic_message_body').value.substring(pos, txtlength);
	} else if(ie) {
		var pos = document.selection.createRange().text;
		document.select.createRange().text += tag;
	}

	document.getElementById('topic_message_body').value = s1 + tag + s2;
	document.getElementById('topic_message_body').setSelectionRange(pos,pos);
}

function wrapMoz(txtarea, lft, rgt) {
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + lft + s2 + rgt + s3;
	if(selStart - selEnd == 0) {
		var text = document.getElementById('topic_message_body').value;
		var offset = selStart+lft.length;
		document.getElementById('topic_message_body').focus();
		document.getElementById('topic_message_body').setSelectionRange(offset, offset);
	} else {
		document.getElementById('topic_message_body').focus();
	}
	return false;
}

function WrapIE(lft, rgt) {
	if(!document.getElementById('topic_message_body').focus()) {
		strSelection = document.selection.createRange().text;
		var curPos = document.selection.createRange().duplicate();
		document.selection.createRange().text = lft + strSelection + rgt;
	}

	if(strSelection == "") {
		curPos.moveStart('character', lft.length);
		curPos.collapse();
		curPos.select();
	}
	return false;
}

function insertImage(txtarea, lft, rgt) {
	var my_link = prompt("URL:","http://");
	if (my_link != null) {
		text= lft + my_link + rgt;
		document.getElementById('topic_message_body').value = document.getElementById('topic_message_body').value + ' ' + text;
	}
	document.getElementById('topic_message_body').focus();
	return false;
}

function insertURL() {
	var url = prompt("URL:","http://");
	
	while(url == '' || url == null) {
		url = prompt("URL:","http://");
	}

	var text = prompt("TEXT FOR LINK:","");
	
	while(text == '' || text == null) {
		text = prompt("TEXT FOR LINK:","");
	}

	if(ie) {
		WrapIE('[url link=' + url + ']' + text,'[/url]');
	} else {
		wrapMoz(document.getElementById('topic_message_body'),'[url link=' + url + ']' + text,'[/url]');
	}

	return false;
}

function insertItem() {
	var url = prompt("Item ID (found on wowhead):", "");

	while(url == '' || url == null) {
		url = prompt("Item ID (found on wowhead):", "");
	}

	var text = prompt("TEXT FOR LINK:","");

        while(text == '' || text == null) {
                text = prompt("TEXT FOR LINK:","");
        }

	if(ie) {
                WrapIE('[item]' + url,'[/item]');
        } else {
                wrapMoz(document.getElementById('topic_message_body'),'[item=' + url + ']' + text,'[/item]');
        }

        return false;
}

function insertSpell() {
	var url = prompt("Spell ID (found on wowhead):", "");

        while(url == '' || url == null) {
                url = prompt("Spell ID (found on wowhead):", "");
        }

        var text = prompt("TEXT FOR LINK:","");

        while(text == '' || text == null) {
                text = prompt("TEXT FOR LINK:","");
        }

        if(ie) {
                WrapIE('[spell]' + url,'[/spell]');
        } else {
                wrapMoz(document.getElementById('topic_message_body'),'[spell=' + url + ']' + text,'[/spell]');
        }

        return false;
}

