// insertcode is used for bold, italic, underline and quote and just
// wraps the tags around a selection or prompts the user for some
// text to apply the tag to

// ID på textarea: 
var idArea = "tekst";

function insertcode(tag, desc)
{

    // our textfield
    var textarea = document.getElementById(idArea);

    // our open tag
    var open = "[" + tag + "]";

    // our close tag
    var close = "[/" + tag + "]";

	var tempText = new SplitSelection(idArea);

	// Indsæt koden om det markerede eller bed om en værdi med prompt hvis der ikke er markeret.
	
	var content = (tempText.match == null || tempText.match.length == 0) ? prompt("Indtast den tekst du gerne vil have  " + desc, "") : tempText.match;

	if (content == null) { content = "" };

	// Opdater textboksen	
	textarea.value = tempText.before + open + content + close + tempText.after;

    // set the focus on the text field
    textarea.focus();
}


function insertCodeAndAttribute(tag, att, desc)
{

    // our textfield
    var textarea = document.getElementById(idArea);

    // our open tag
    var open = "[" + tag + " " + att + "]";

    // our close tag
    var close = "[/" + tag + "]";

	var tempText = new SplitSelection(idArea);

	// Indsæt koden om det markerede eller bed om en værdi med prompt hvis der ikke er markeret.
	
	var content = (tempText.match == null || tempText.match.length == 0) ? prompt("Indtast den tekst du gerne vil have  " + desc, "") : tempText.match;

	if (content == null) { content = "" };

	// Opdater textboksen	
	textarea.value = tempText.before + open + content + close + tempText.after;

    // set the focus on the text field
    textarea.focus();
}


// inserts a link by prompting the user for a url
function insertlink()
{

    // our textfield
    var textarea = document.getElementById(idArea);
	
	var tempText = new SplitSelection(idArea);
	
    // our link
    var url = prompt("Indtast webadressen (med http)", "http://");

	var vistTekst = (tempText.match > "") ? tempText.match : url.replace("http://", "");
	var vistadr = prompt("Indtast den tekst der skal vises som link", vistTekst);
    var link = "[L=" + url + "]" + vistadr + "[/L]";

	textarea.value = tempText.before + link + tempText.after;

    // set the focus on the text field
    textarea.focus();
}

function testSelection(){
	var s = new SplitSelection("foo");
//	var s = selection.create();
//	alert("start:" + s.start + ", end:" + s.end);
	alert("start: " + s.before + "\nmatch: " + s.match + "\nafter: " + s.after);
}

