//------------------------------------------------------------------------
// Сохранение позиции курсора в области редактирования
//------------------------------------------------------------------------
function storeCaret(text) {
	if (text.createTextRange) {
		text.caretPos = document.selection.createRange().duplicate();
	}
}
//------------------------------------------------------------------------
function CheckVersion(){
    var ver, flagIE;
    ver = window.navigator.appVersion;
    flagIE = (ver.indexOf("MSIE") != -1);
    // Если истина, то браузер IE.
    return flagIE;
}
//------------------------------------------------------------------------
//Вот функция mozWrap: (С.О.)
function mozWrap(txtarea, open, close)
{
         var selLength = txtarea.textLength;
         var selStart = txtarea.selectionStart;
         var selEnd = txtarea.selectionEnd;
         if (selEnd == 1 || selEnd == 2)
                 selEnd = selLength;

         var s1 = (txtarea.value).substring(0,selStart);
         var s2 = (txtarea.value).substring(selStart, selEnd)
         var s3 = (txtarea.value).substring(selEnd, selLength);
         txtarea.value = s1 + open + s2 + close + s3;
         return;
}
//------------------------------------------------------------------------
//  Помещение в область редактирования текста в позицию курсора
//------------------------------------------------------------------------
function SetTextToCaret(text) {
	if (document.mess.body.createTextRange && document.mess.body.caretPos) {
		var caretPos = document.mess.body.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
		text + ' ' : text;
	}
	else document.mess.body.value += text;
}
//------------------------------------------------------------------------
function SetText(format_tag)
{
    var oTxt, oSel, txt;

    // Выделяем из входящего параметра тэги. Разделитель тегов ПРОВЕЛ.
    tag_begin = (format_tag.split(" "))[0];
    tag_end   = (format_tag.split(" "))[1];

    // Ссылка на выделенный текст в объекте TEXTAREA (body), формы FORM (mess)
    oSel = document.selection; //document.mess.body.document.selection;

	//,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

	// Если объект выделения не определен, то есть броузер не ИЕ
    //if (typeof(oSel) == 'undefined')
    //    {
	//	  SetTextToCaret(format_tag);
	//	  return true;
	//	}

        // Если объект выделения не определен, то есть броузер не ИЕ
     if (typeof(oSel) == 'undefined')
         {
           txtarea = document.mess.body; //document.forms[0].body;
           txtarea.focus();
           if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
           {
                 mozWrap(txtarea, tag_begin, tag_end );
                 return true;
           }
           else  {
                  txtarea.value =  txtarea.value + format_tag;
                  return true;
                 }
         }


	//,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    // Создаем текстовый объект
    oTxt = oSel.createRange();

    // Проверяем, есть ли выделенный текст.
    if ((oSel.type != "Text") | (oSel == undefined) )
        {
		  SetTextToCaret(format_tag);
		  return true;
		}

    // В переменной txt выделенный фрагмент.
    txt = oTxt.text;

    // Выделяем из входящего параметра тэги. Разделитель тегов ПРОВЕЛ.
    tag_begin = (format_tag.split(" "))[0];
    tag_end   = (format_tag.split(" "))[1];

    // Накладываем форматирование тегами.
    oTxt.text = tag_begin + txt + tag_end;

    // Снять выделение со строки.
    oSel.empty();

    return true;
}

//------------------------------------------------------------------------
function SetMessage()
{
  Quote = '%msgXXX';
  SetText(Quote);
}
//------------------------------------------------------------------------
function SetUrl()
{
  Quote = '[url=http://www.xxx] [/url]';
  SetText(Quote);
}
//------------------------------------------------------------------------
function SetQW()
{
  Quote = '%qwXXX';
  SetText(Quote);
}
//------------------------------------------------------------------------
function SetMess(){

  Quote = '#XXX';
  SetText(Quote);
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
function SetTalkMess(){

  Quote = '%talkXXX:YYY';
  SetText(Quote);
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
function SetQuintana(){

  Quote = '%quinXXX:YYY';
  SetText(Quote);
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
function SetTalk(){

  Quote = '%talkXXX';
  SetText(Quote);
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
// Добавление знака цитирования
//------------------------------------------------------------------------
function SetQuote(){

  Quote = '>>>';
  SetText(Quote);
}
//------------------------------------------------------------------------
function SetQuoteBlock(){

  Quote = '[Quote] [/Quote]';
  SetText(Quote);
}
//------------------------------------------------------------------------
// Добавление выделeния жирным шрифтом
//------------------------------------------------------------------------
function SetBold(){

  Quote = '[B] [/B]';
  SetText(Quote);
}
//------------------------------------------------------------------------
// Добавление тегов кода
//------------------------------------------------------------------------
function SetCode(){

  Quote = '[Code] [/Code]';
  SetText(Quote);
}
//------------------------------------------------------------------------
function SetSmall(){

  Quote = '[Small] [/Small]';
  SetText(Quote);
}
//------------------------------------------------------------------------
function SetBig(){

  Quote = '[Big] [/Big]';
  SetText(Quote);
}
//------------------------------------------------------------------------
// Добавление тегов Italic
//------------------------------------------------------------------------
function SetItalic(){

  Quote = '[EM] [/EM]';
  SetText(Quote);
}
//------------------------------------------------------------------------
// Добавление тегов UnderLine
//------------------------------------------------------------------------
function SetUnderLine(){

  Quote = '[U] [/U]';
  SetText(Quote);
}
//------------------------------------------------------------------------
function SetList(){

  Quote = "\n"+'[UL]'+"\n"+'[LI] '+"\n"+'[/UL]';
  SetText(Quote);
}
//------------------------------------------------------------------------
function SetCenter(){

  Quote = '[CENTER] [/CENTER]';
  SetText(Quote);
}
//------------------------------------------------------------------------
function SetHeader(){

  Quote = '[HEADER] [/HEADER]';
  SetText(Quote);
}
//------------------------------------------------------------------------
function SetHR(){

  Quote = '[HR]';
  SetText(Quote);
}
//------------------------------------------------------------------------
//  Отметить все checkbox'ы
//------------------------------------------------------------------------
function CheckAll(state , ID ) {

  for(i=0; i<document.search.elements.length; i++) {
    var elem=document.search.elements[i];

    if (elem.name == 'section3' ) elem.checked=state;
	//elem.checked=state;
  }
}
//------------------------------------------------------------------------
