// insertcode is used for bold, italic, underline and quote and just
// wraps the tags around a selection or just adds tags
function insertcode(tag, desc, taid) {
	// our textfield
	var textarea = document.getElementById(taid);
	// our open tag  
	var open = tag;
	// our close tag
	var close = "";
	if(!textarea.setSelectionRange) {
		var selected = document.selection.createRange().text;
		if(selected.length <= 0)  {
			if (tag=='url') {
				// no text was selected ask for..
				textarea.value += open + prompt(desc, "http://") + close;
			} else {
				// no text was selected so just the tag
				textarea.value += open + close;
			}
		} else {
			// put the code around the selected text
			document.selection.createRange().text = open + selected + close;
		}
	} else {
		// the text before the selection
		var pretext = textarea.value.substring(0, textarea.selectionStart);
		if (tag=='url') {
			if (!textarea.value.substring(textarea.selectionStart, textarea.selectionEnd)) {
				var codetext = open + close;
			} else {
				var codetext = "[url=" + prompt(desc, "http://") + "]" + textarea.value.substring(textarea.selectionStart, textarea.selectionEnd) + close;
			}
		} else {
			// the selected text with tags before and after
			var codetext = open + textarea.value.substring(textarea.selectionStart, textarea.selectionEnd) + close;
			// check if there was a selection
			if(codetext == open + close)  {
				//just add tags
				codetext = open + close;
			}
		}
		// the text after the selection
		var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
		// update the text field
		textarea.value = pretext + codetext + posttext;
	}
	// set the focus on the text field
	textarea.focus();
}
