/***************************************************************************
*   copyright            : (C) 2001,2002 Advanced Internet Designs Inc.
*   email                : forum@prohost.org
*
*   $Id: lib.js,v 1.1 2003/12/15 11:53:21 martijn Exp $
*
*       This program is free software; you can redistribute it and/or modify
*       it under the terms of the GNU General Public License as published by
*       the Free Software Foundation; either version 2 of the License, or
*       (at your option) any later version.
*
*   This code is borrowed from FUD Forum for TzZForum
*
***************************************************************************/

NS4 = (document.layers) ? 1 : 0;
IE4 = (document.all) ? 1 : 0;
OPERA = (navigator.userAgent.indexOf("Opera 5") > -1 || navigator.userAgent.indexOf("Opera/5") > -1 || navigator.userAgent.indexOf("Opera 6") > -1 || navigator.userAgent.indexOf("Opera/6") > -1) ? 1 : 0;

function insertTag(obj, stag, etag)
{

        if ( navigator.userAgent.indexOf("MSIE") > -1 && !OPERA ) {
                insertTagIE(obj, stag, etag);
        }
        else {
                insertTagNS(obj, stag, etag);
        }
}

function insertTagNS(obj, stag, etag)
{
        obj.value = obj.value+stag+etag;
}

function insertTagIE(obj, stag, etag)
{
        r=document.selection.createRange();
        if( document.selection.type == 'Text' && (obj.value.indexOf(r.text) != -1) ) {
                a = r.text;
                r.text = stag+r.text+etag;
                if ( obj.value.indexOf(document.selection.createRange().text) == -1 ) {
                        document.selection.createRange().text = a;
                }
        }
        else insertAtCaret(obj, stag+etag);
}

function insertAtCaret(textEl, text)
{
        if (textEl.createTextRange && textEl.caretPos)
        {
                var caretPos = textEl.caretPos;
                caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
        }
        else
                textEl.value  =  textEl.value + text;
}

function storeCaret(textEl)
{
         if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

