// -----------------------------------------------------------------------
//
// $Id: javascript.js,v 1.3 2007/09/29 19:59:33 raoul Exp $
//
// Copyright (C) 2003-2007 Raoul Proença
// License: GNU GPL version 3 (see copying.txt file)
// Website: http://genu.org/
//
// -----------------------------------------------------------------------
//
// 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 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// -----------------------------------------------------------------------

var textarea = document.getElementsByTagName('textarea');

function insert(text1, text2)
{
    var area = textarea[0];
    area.focus();
    /* Internet Explorer */
    if (typeof document.selection != 'undefined')
    {
        var sel = document.selection.createRange();
        var str = sel.text;
        sel.text = text1 + str + text2;
        sel = document.selection.createRange();
        if (str.length == 0)
        {
            sel.move('character', -text2.length);
        }
        else
        {
            sel.moveStart('character', text1.length + str.length + text2.length);
        }
        sel.select();
    }
    /* Gecko */
    else if (typeof area.selectionStart != 'undefined')
    {
        var start = area.selectionStart;
        var end = area.selectionEnd;
        var str = area.value.substring(start, end);
        area.value = area.value.substr(0, start) + text1 + str + text2 + area.value.substr(end);
        var pos;
        if (str.length == 0)
        {
            pos = start + text1.length;
        }
        else
        {
            pos = start + text1.length + str.length + text2.length;
        }
        area.selectionStart = pos;
        area.selectionEnd = pos;
    }
    /* Others */
    else
    {
        var pos;
        var re = new RegExp('^[0-9]{0,3}$');
        while (!re.test(pos))
        {
            pos = prompt('Insert string at position (0...' + area.value.length + '):', area.value.length);
        }
        if (pos > area.value.length)
        {
            pos = area.value.length;
        }
        var str = prompt('Please enter string:');
        area.value = area.value.substr(0, pos) + text1 + str + text2 + area.value.substr(pos);
    }
}

function vIE(){return (navigator.appName=='Microsoft Internet Explorer')?parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]):-1;}



