/***********************************************************************

  Copyright (C) 2008  PunBB

  Based on Easy BBCode extension by Rickard Andersson.

  PunBB 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.

  PunBB 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.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  MA  02111-1307  USA

***********************************************************************/

function insert_text(open, close)
{

  msgfield = (document.all) ? document.all.req_message : ((document.getElementById('afocus') != null) ? (document.getElementById('afocus').req_message) : (document.getElementsByName('req_message')[0]));

  // IE support
  if (document.selection && document.selection.createRange)
  {
    msgfield.focus();
    sel = document.selection.createRange();
    sel.text = open + sel.text + close;
    msgfield.focus();
  }

  // Moz support
  else if (msgfield.selectionStart || msgfield.selectionStart == '0')
  {
    var startPos = msgfield.selectionStart;
    var endPos = msgfield.selectionEnd;

    msgfield.value = msgfield.value.substring(0, startPos) + open + msgfield.value.substring(startPos, endPos) + close + msgfield.value.substring(endPos, msgfield.value.length);
    msgfield.selectionStart = msgfield.selectionEnd = endPos + open.length + close.length;
    msgfield.focus();
  }

  // Fallback support for other browsers
  else
  {
    msgfield.value += open + close;
    msgfield.focus();
  }

  return;
}

