/**
 * Browser detection
 */
var textbar_clientPC  = navigator.userAgent.toLowerCase(); // Get client info
var textbar_is_gecko  = ((textbar_clientPC.indexOf('gecko')!=-1) && (textbar_clientPC.indexOf('spoofer')==-1)
                && (textbar_clientPC.indexOf('khtml') == -1) && (textbar_clientPC.indexOf('netscape/7.0')==-1));
var textbar_is_safari = ((textbar_clientPC.indexOf('AppleWebKit')!=-1) && (textbar_clientPC.indexOf('spoofer')==-1));
var textbar_is_khtml  = (navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled ));
if (textbar_clientPC.indexOf('opera')!=-1) {
    var textbar_is_opera = true;
    var textbar_is_opera_preseven = (window.opera && !document.childNodes);
    var textbar_is_opera_seven = (window.opera && document.childNodes);
}


/*
            bold:    [ '**', '**' ],
            italic:  [ '*', '*' ],
            code:    [ '`', '`' ],
            fonth1:  [ '# ', '' ],
            fonth2:  [ '## ', '' ],
            fonth3:  [ '### ', '' ],
            fonth4:  [ '#### ', '' ],
            fonth5:  [ '##### ', '' ],
            link:    [ '[', '](page-name "tooltip")' ],
            extlink: [ '[', '](http://www.example.com/ "tooltip")' ],
            list:    [ '1. ', '\n' ],
            list_ul: [ '* ', '\n' ],
            rule:    [ '----\n' ]
*/

/**
 * apply tagOpen/tagClose to selection in textarea, use sampleText instead
 * of selection if there is none.
 *
 * @author phpBB development team
 * @author MediaWiki development team
 * @author Andreas Gohr <andi@splitbrain.org>
 * @author Jim Raynor <jim_raynor@web.de>
 */
function textbar_insertTags( id, tagOpen, tagClose, sampleText )
{
    tagClose = tagClose || '';

    var txtarea = document.getElementById(id);

    if( document.selection  &&  ! textbar_is_gecko )  // IE
    {
        var theSelection = document.selection.createRange().text;
        var replaced = true;

        if( ! theSelection )
        {
            replaced = false;
            theSelection = sampleText;
        }

        txtarea.focus();

        // This has changed
        var text = theSelection;
        if( theSelection.charAt(theSelection.length-1) == " " )  // exclude ending space char, if any
        {
            theSelection = theSelection.substring( 0, theSelection.length - 1 );
            r = document.selection.createRange();
            r.text = tagOpen + theSelection + tagClose + " ";
        }
        else
        {
            r = document.selection.createRange();
            r.text = tagOpen + theSelection + tagClose;
        }

        if( ! replaced )
        {
            r.moveStart( 'character', -text.length - tagClose.length );
            r.moveEnd( 'character', -tagClose.length );
        }

        r.select();
    }
    else if( txtarea.selectionStart || txtarea.selectionStart == '0' )  // Mozilla
    {
        var startPos = txtarea.selectionStart;
        var endPos   = txtarea.selectionEnd;
        var replaced = ( endPos - startPos )

        var scrollTop = txtarea.scrollTop;
        var myText = txtarea.value.substring( startPos, endPos )  ||  sampleText;

        if( myText.charAt(myText.length-1) == " " )  // exclude ending space char, if any
            subst = tagOpen + myText.substring( 0, myText.length - 1 ) + tagClose + " ";
        else
            subst = tagOpen + myText + tagClose;

        txtarea.value = textbar_insertString( txtarea.value, startPos, endPos, subst );

        txtarea.focus();

        // set new selection
        if( replaced )
        {
            txtarea.selectionStart = txtarea.selectionEnd =
                startPos + tagOpen.length + myText.length + tagClose.length;
        }
        else
        {
            txtarea.selectionStart = startPos + tagOpen.length;
            txtarea.selectionEnd = txtarea.selectionStart + myText.length;
            txtarea.scrollTop = scrollTop;
        }
    }
    else  // All other browsers
    {
        var copy_alertText = alertText;
        var re1 = new RegExp( "\\$1", "g" );
        var re2 = new RegExp( "\\$2", "g" );
        copy_alertText = copy_alertText.replace( re1, sampleText );
        copy_alertText = copy_alertText.replace( re2, tagOpen + sampleText + tagClose );

        var text = ( sampleText ? prompt(copy_alertText) : '' ) || sampleText;
        text = tagOpen + text + tagClose;

        txtarea.value += "\n" + text;

        // in Safari this causes scrolling
        if( ! textbar_is_safari )
            txtarea.focus();
    }

    // reposition cursor if possible
    if( txtarea.createTextRange )
        txtarea.caretPos = document.selection.createRange().duplicate();
}

/*
 * Insert the given value at the current cursor position
 *
 * @see http://www.alexking.org/index.php?content=software/javascript/content.php
 */
function textbar_insertText( id, value )
{
    var field = document.getElementById(id);

    if( document.selection )  // IE
    {
        field.focus();
        document.selection.createRange().text = value;
        //( opener ? opener.document : document ).selection.createRange().text = value;
    }
    else if( field.selectionStart || field.selectionStart == '0' )  // MOZILLA/NETSCAPE
    {
        var startPos = field.selectionStart;
        var scrollTop = field.scrollTop;
        field.value = textbar_insertString( field.value, startPos, field.selectionEnd, value );

        field.focus();

        field.selectionStart = field.selectionEnd = startPos + value.length;
        field.scrollTop = scrollTop;
    }
    else
    {
        field.value += "\n" + value;
    }

    // reposition cursor if possible
    if( field.createTextRange )
        field.caretPos = document.selection.createRange().duplicate();
}

/**
 * Replace a specified range in a target string with a new string.
 */
function textbar_insertString( target, iStart, iEnd, insert )
{
    return target.substring( 0, iStart ) + insert + target.substring( iEnd, target.length );
}


