function toggle_topic_notify(id, active)
{
	// if success, show modal panel
	var success = function(result) 
	{
		// check if result is ok
		if (result.result != 1) 
			return;
		
		$('#topicNotify').html(result.content);
		$('#topicNotify2').html(result.content);
	};

	_rpcPost('Forum', 'toggleTopicNotification', {"id": id, "active": active}, success, function(result) {alert(result);});
}

function posts_show(id, removeinfo)
{
	if (id == 0)
	{
		// show all
		$('.post_hide').removeClass();
		$('.post_show').hide();
	}
	else
	{
		// show {id}..1
		for (i=1; i <= id; i++)
		{
			$('#p'+i).removeClass('post_hide');
			$('#p'+i+' div:last-child').hide();
		}

	}
	if (removeinfo)
		$('#p0').hide();
}

function smiley(text)
{
	insert_tag(document.getElementById('__content'), text, '');
}

function storeCaret(textEl) 
{
	//	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

function textbox_resize(change)
{
	var box = document.getElementById('__content');
	var height_cur = parseInt( box.style.height ) ? parseInt( box.style.height ) : 300;
	var height_new = height_cur + change;

	if (height_new > 50)
	{
		box.style.height = height_new + "px";
	}
	return false;
}

function insert_code(aTag, eTag) 
{
	return insert_tag(document.getElementById('__content'), aTag, eTag);
}

// inserts a tag into the textarea. if text is selected, the tag will enclose
// the selected text.
function insert_tag(ta, pre, post)
{
    var text;
    var pretext;
    var posttext;
    var range;
    if (ta == null) return;

    // Store the current scroll offset, so we can restore it after
    // adding the tags to its contents.
    var offset = ta.scrollTop;

    if (ta.setSelectionRange)
    {
        // Get the currently selected text.
        pretext = ta.value.substring(0, ta.selectionStart);
        text = ta.value.substring(ta.selectionStart, ta.selectionEnd);
        posttext = ta.value.substring(ta.selectionEnd, ta.value.length);
        
        // switch for single tags
        if (text != '' && post == '')
        {
        	post = pre;
        	pre = '';
        }

        // Strip whitespace from text selection and move it to the
        // pre- and post.
        var res = editor_tools_strip_whitespace(text, true);
        text = res[0];
        pre = res[1] + pre;
        post = post + res[2];

        ta.value = pretext + pre + text + post + posttext;

        // Reselect the selected text.
        var cursorpos1 = pretext.length + pre.length;
        var cursorpos2 = cursorpos1 + text.length;
        ta.setSelectionRange(cursorpos1, cursorpos2);
        ta.focus();
    }
    else if (document.selection) /* MSIE support */
    {
        // Get the currently selected text.
        ta.focus();
        range = document.selection.createRange();

        // Fumbling to work around newline selections at the end of
        // the text selection. MSIE does not include them in the
        // range.text, but it does replace them when setting range.text
        // to a new value :-/
        var virtlen = range.text.length;
        if (virtlen > 0) {
            while (range.text.length == virtlen) {
                range.moveEnd('character', -1);
            }
            range.moveEnd('character', +1);
        }

        text = range.text;

        // switch for single tags
        if (text != '' && post == '')
        {
        	post = pre;
        	pre = '';
        }
        

        // Strip whitespace from text selection and move it to the
        // pre- and post.
        var res = editor_tools_strip_whitespace(text, true);
        text = res[0];
        pre = res[1] + pre;
        post = post + res[2];

        // Add pre and post to the text.
        range.text = pre + text + post;

        // Reselect the selected text. Another MSIE anomaly has to be
        // taken care of here. MSIE will include carriage returns
        // in the text.length, but it does not take them into account
        // when using selection range moving methods :-/
        // By setting the range.text before, the cursor is now after
        // the replaced code, so we will move the start and the end
        // back in the text.
        var mvstart = post.length + text.length -
                      ((text + post).split('\r').length - 1);
        var mvend   = post.length +
                      (post.split('\r').length - 1);
        range.moveStart('character', -mvstart);
        range.moveEnd('character', -mvend);
        range.select();
    }
    else /* Support for really limited browsers, e.g. MSIE5 on MacOS */
    {
        ta.value = ta.value + pre + post;
    }

    ta.scrollTop = offset;
}

//Strip whitespace from the start and end of a string.
function editor_tools_strip_whitespace(str, return_stripped)
{
    var strip_pre = '';
    var strip_post = '';

    // Strip whitespace from end of string.
    for (;;) {
        var lastchar = str.substring(str.length-1, str.length);
        if (lastchar == ' '  || lastchar == '\r' ||
            lastchar == '\n' || lastchar == '\t') {
            strip_post = lastchar + strip_post;

            str = str.substring(0, str.length-1);
        } else {
            break;
        }
    }

    // Strip whitespace from start of string.
    for (;;) {
        var firstchar = str.substring(0,1);
        if (firstchar == ' '  || firstchar == '\r' ||
            firstchar == '\n' || firstchar == '\t') {
            strip_pre += firstchar;
            str = str.substring(1);
        } else {
            break;
        }
    }

    if (return_stripped) {
        return new Array(str, strip_pre, strip_post);
    } else {
        return str;
    }
} 
