// Determine browser type and stuff.
// Borrowed from http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var myAgent   = navigator.userAgent.toLowerCase();
var myVersion = parseInt(navigator.appVersion);

var is_op = ((myAgent.indexOf("opera") !=-1) || window.opera);
var is_ie = ((myAgent.indexOf("msie") !=-1) && (!is_op));
var is_nav = ((myAgent.indexOf('mozilla') != -1) && (!is_op)
 && (myAgent.indexOf('compatible') ==-1) && (myAgent.indexOf('spoofer') ==-1)
 && (myAgent.indexOf('webtv') ==-1) && (myAgent.indexOf('hotjava')==-1));

var is_win = ((myAgent.indexOf("win")!=-1) || (myAgent.indexOf("16bit")!=-1));
var is_mac = (myAgent.indexOf("mac")!=-1);



function doInsert(ibTag, ibClsTag, isSingle)
{
 var isClose = false;
 var obj_ta = document.REPLIER.Post;

 obj_ta.focus();

 if (document.REPLIER.Post.caretPos)
 {
  if (isSingle)
  {
   isClose = true;
   document.REPLIER.Post.caretPos.text += ibTag;
  }
  else
  {
   document.REPLIER.Post.caretPos.text += ibTag + ibClsTag;
  }
 }
 else if (document.REPLIER.Post.createTextRange)
 {
  // Supported browsers: Internet Explorer, Opera
  var st = document.REPLIER.Post.selectionStart;
  var ln = document.selection.createRange().text.length;
  var nT = ibTag + document.selection.createRange().text + ibClsTag;
  if (!ln && isSingle)
  {
   isClose = true;
   document.selection.createRange().text = ibTag;
  }
  else
  {
   document.selection.createRange().text = nT;
  }
  if (ln)
  {
   if (is_op)
   {
    document.REPLIER.Post.setSelectionRange(st, st + nT.length);
   }
   else
   {
    document.selection.createRange().moveStart('character', st);
    document.selection.createRange().moveEnd('character', st + nT.length);
    document.selection.createRange().select();
   }
  }
  else
  {
   if (is_op)
   {
    document.REPLIER.Post.setSelectionRange(st + ibTag.length, st + ibTag.length);
   }
   else
   {
    document.selection.createRange().moveStart('character', st + ibTag.length);
    document.selection.createRange().select();
   }
  }
 }
 else if (document.REPLIER.Post.setSelectionRange)
 {
  // Supported browsers: FireFox
  var st = document.REPLIER.Post.selectionStart;
  var ln = document.REPLIER.Post.selectionEnd - st;
  var nT = ibTag + document.REPLIER.Post.value.substr(st, ln) + ibClsTag;
  var mT = ibTag + document.REPLIER.Post.value.substr(st, ln);
  if (!ln && isSingle)
  {
   isClose = true;
   document.REPLIER.Post.value = document.REPLIER.Post.value.substr(0, st) + ibTag + document.REPLIER.Post.value.substr(st);
  }
  else
  {
   document.REPLIER.Post.value = document.REPLIER.Post.value.substr(0, st) + nT + document.REPLIER.Post.value.substr(st + ln);
  }
  if (ln)
  {
   document.REPLIER.Post.setSelectionRange(st, st + nT.length);
  }
  else
  {
   document.REPLIER.Post.setSelectionRange(st + ibTag.length, st + ibTag.length);
  }
 }
 else
 {
  if (isSingle)
  {
   isClose = true;
   document.REPLIER.Post.value += ibTag;
  }
  else
  {
   document.REPLIER.Post.value += ibTag + ibClsTag;
  }
 }

 return isClose;
}

function dualtag(thetag,closetag) {
 doInsert("["+thetag+"]", "[/"+closetag+"]", false);
}

function simpletag(thetag) {
 doInsert("["+thetag+"]", "[/"+thetag+"]", false);
}

function singletag(thetag) {
 doInsert("["+thetag+"]", "", false);
}

function hstat(msg)
{
 document.REPLIER.helpbox.value = msg;
}

//Вызывается для события onmouseover таблицы
//делает подсветку строк таблицы при прохождении над ними мышки
//проверено в ИЕ и ФФ
function tableFlashRows(_node, _color)
{
    _node.onmouseover=function()	{}	//удаляем обработчик таблицы
    //получаем список строк и на каждую вешаем 2 обработчика
    list = _node.rows;
    for (var i = 0; i < list.length; i++) {
	list[i].onmouseover=function()	{	this.style["backgroundColor"]= _color;	}
	list[i].onmouseout=function()	{	this.style["backgroundColor"]="";	}
    }
}