function getCursorPos(textElement) {
if (textElement.setSelectionRange && browser.isNS)
  return textElement.selectionEnd;

var textarea = textElement;
var selection_range = document.selection.createRange().duplicate();
if (selection_range.parentElement() == textarea)
{
    // Check that the selection is actually in our textarea
    // Create three ranges, one containing all the text before the selection,
    // one containing all the text in the selection (this already exists), and one containing all
    // the text after the selection.
    var before_range = document.body.createTextRange();
    before_range.moveToElementText(textarea);
    // Selects all the text
    before_range.setEndPoint("EndToStart", selection_range);
    // Moves the end where we need it
    var after_range = document.body.createTextRange();
    after_range.moveToElementText(textarea);
    // Selects all the text
    after_range.setEndPoint("StartToEnd", selection_range);
    // Moves the start where we need it
    var before_finished = false, selection_finished = false, after_finished = false;
    var before_text, untrimmed_before_text, selection_text, untrimmed_selection_text, after_text, untrimmed_after_text;
    // Load the text values we need to compare
    before_text = untrimmed_before_text = before_range.text;
    selection_text = untrimmed_selection_text = selection_range.text;
    after_text = untrimmed_after_text = after_range.text;
    // Check each range for trimmed newlines by shrinking the range by 1 character and seeing
    // if the text property has changed. If it has not changed then we know that IE has trimmed
    // a \r\n from the end.
    do {  if (!before_finished) {
        if (before_range.compareEndPoints("StartToEnd", before_range) == 0)
        {
            before_finished = true;
        } else {
            before_range.moveEnd("character", -1)
            if (before_range.text == before_text) {
                untrimmed_before_text += "\r\n";
            } else {
                before_finished = true;
            }
        }
    }
    if (!selection_finished) {
        if (selection_range.compareEndPoints("StartToEnd", selection_range) == 0) {
            selection_finished = true;
        } else {
            selection_range.moveEnd("character", -1)
            if (selection_range.text == selection_text) {
                untrimmed_selection_text += "\r\n";
            } else {
                selection_finished = true;
            }
        }
    }
    if (!after_finished) {
        if (after_range.compareEndPoints("StartToEnd", after_range) == 0) {
            after_finished = true;
        } else {
            after_range.moveEnd("character", -1)
            if (after_range.text == after_text) {
                untrimmed_after_text += "\r\n";
            } else {
                after_finished = true;
            }
        }
    }} while ((!before_finished || !selection_finished || !after_finished));
    // Untrimmed success test to make sure our results match what is actually in the textarea
    // This can be removed once you're confident it's working correctly
    var untrimmed_text = untrimmed_before_text + untrimmed_selection_text + untrimmed_after_text;
    var untrimmed_successful = false;
    if (textarea.value == untrimmed_text){
        untrimmed_successful = true;
    }
    // ** END Untrimmed success test
    var startPoint = untrimmed_before_text.length;
    var endPoint = startPoint + untrimmed_selection_text.length;
    var selected_text = untrimmed_selection_text;
    return startPoint;
    }
}

function getActiveElements(text)
{
    array = new Array();
    array["test"] = "test";
    array["test2"] = "test2";

    // first of all delete all completed tags
    var newstr = text;

        newstr = newstr.replace(/\[((font|size|color|bgcolor)=(.*))\]((.|\n|\r)*)\[\/\2\]/gm, "$4");

    // now get the values
    var test;
    var cur = 0;
    if (result = newstr.match(/\[font=(.*?)\]/g))
    {
        var cur = result.length - 1;
        test = result[cur];
        var font = test.replace(/\[font=(.*)\]/, "$1");
    }
    if (result = newstr.match(/\[size=(.*?)\]/g))
    {
        var cur = result.length - 1;
        test = result[cur];
        var size = test.replace(/\[size=(.*)\]/, "$1");
    }
    if (result = newstr.match(/\[color=(.*?)\]/g))
    {
        var cur = result.length - 1;
        test = result[cur];
        var color = test.replace(/\[color=(.*)\]/, "$1");
    }
    if (result = newstr.match(/\[bgcolor=(.*?)\]/g))
    {
        var cur = result.length - 1;
        test = result[cur];
        var bgcolor = test.replace(/\[bgcolor=(.*)\]/, "$1");
    }
    var newstr = text.replace(/\[((quote):(.*?))\]((.|\n|\r)*)\[\/\2\]/gm, "$4");
    if (newstr.match(/\[quote:(.*?)\]/g))
    {
        var quote = true;
    }
    // now get the tags without additional information
    var newstr = text;
    while (newstr.match(/\[(b|i|u|left|center|right|indent|code)\]((.|\n|\r)*)\[\/\1\]/gm))
    {
        newstr = newstr.replace(/\[(b|i|u|left|center|right|indent|code)\]((.|\n|\r)*?)\[\/\1\]/gm, "$2");
    }

    // now get the other values
    var test;
    var cur = 0;
    if (newstr.match(/\[b\]/))
    {
        var bold = true;
    }
    if (newstr.match(/\[i\]/))
    {
        var italic = true;
    }
    if (newstr.match(/\[u\]/))
    {
        var underline = true;
    }
    if (newstr.match(/\[left\]/))
    {
        var left = true;
    }
    if (newstr.match(/\[center\]/))
    {
        var center = true;
    }
    if (newstr.match(/\[right\]/))
    {
        var right = true;
    }
    if (newstr.match(/\[indent\]/))
    {
        var indent = true;
    }
    if (newstr.match(/\[code\]/))
    {
        var code = true;
    }
    // a = in a code tag is not a heading-tag
    if (!code)
    {
    // now get the heading-tags
    var newstr = text;
    newstr = newstr.replace(/(===) (.*)\1/g, "$2");
    // now get the heading values
    if (newstr.match(/( |\n)=== /))
    {
        var heading3 = true;
    }
    newstr = newstr.replace(/(==) (.*)\1/g, "$2");
    if (newstr.match(/( |\n)== /))
    {
        var heading2 = true;
    }
    newstr = newstr.replace(/(=) (.*)\1/g, "$2");
    if (newstr.match(/( |\n)= /))
    {
        var heading1 = true;
    }
    }
    
    values = new Object();
    values.font = font;
    values.size = size;
    values.color = color;
    values.bgcolor = bgcolor;
    values.quote = quote;
    values.bold = bold;
    values.italic = italic;
    values.underline = underline;
    values.left = left;
    values.center = center;
    values.right = right;
    values.indent = indent;
    values.code = code;
    values.heading1 = heading1;
    values.heading2 = heading2;
    values.heading3 = heading3;

    return values;
}

function manageValues(text)
{
    // first of all get all elements
    var color = document.getElementById("fontcolor");
    var bgcolor = document.getElementById("bgcolor");

    // now get the current position
    var pos = getCursorPos(text);

    // then get alle active elements
    values = getActiveElements(text.value.substring(0, pos));

    if (values.font == undefined)
    {
        values.font = "";
    }
    if (values.size == undefined)
    {
        values.size = "";
    }
    if (values.color == undefined)
    {
        values.color = "#000000";
    }
    if (values.bgcolor == undefined)
    {
        values.bgcolor = "#FFFFFF";
    }

    // finally set all values in editor
    document.getElementById("fselect").innerHTML = values.font;
    document.getElementById("fselect").style.fontFamily = values.font;
    document.getElementById("sselect").innerHTML = values.size;
    
    if (values.heading1) {
        document.getElementById("heading1").className = "editorbutton_active";
    } else {
        document.getElementById("heading1").className = "editorbutton";
    }
    if (values.heading2) {
        document.getElementById("heading2").className = "editorbutton_active";
    } else {
        document.getElementById("heading2").className = "editorbutton";
    }
    if (values.heading3) {
        document.getElementById("heading3").className = "editorbutton_active";
    } else {
        document.getElementById("heading3").className = "editorbutton";
    }
    if (values.quote) {
        document.getElementById("quotebtn").className = "editorbutton_active";
    } else {
        document.getElementById("quotebtn").className = "editorbutton";
    }
    if (values.bold) {
        document.getElementById("bold").className = "editorbutton_active";
    } else {
        document.getElementById("bold").className = "editorbutton";
    }
    if (values.italic) {
        document.getElementById("italic").className = "editorbutton_active";
    } else {
        document.getElementById("italic").className = "editorbutton";
    }
    if (values.underline) {
        document.getElementById("underline").className = "editorbutton_active";
    } else {
        document.getElementById("underline").className = "editorbutton";
    }
    if (values.left) {
        document.getElementById("left").className = "editorbutton_active";
    } else {
        document.getElementById("left").className = "editorbutton";
    }
    if (values.center) {
        document.getElementById("center").className = "editorbutton_active";
    } else {
        document.getElementById("center").className = "editorbutton";
    }
    if (values.right) {
        document.getElementById("right").className = "editorbutton_active";
    } else {
        document.getElementById("right").className = "editorbutton";
    }
    if (values.indent) {
        document.getElementById("indent").className = "editorbutton_active";
    } else {
        document.getElementById("indent").className = "editorbutton";
    }
    if (values.code) {
        document.getElementById("codebtn").className = "editorbutton_active";
    } else {
        document.getElementById("codebtn").className = "editorbutton";
    }
}

