function BrowserType() {
  var MSIE=navigator.userAgent.indexOf('MSIE');
  var NETS=navigator.userAgent.indexOf('Netscape');
  var OPER=navigator.userAgent.indexOf('Opera');
  var MZ=navigator.userAgent.indexOf('Mozilla');

  if (MZ != -1 && MSIE == -1 && NETS == -1 && OPER == -1) {
        return 'MZ';
  }
  else if (NETS != -1) {
      return 'NETS';
  }
  else if (MSIE != -1 && OPER == -1) {
    return 'MSIE';
  }
  else {
        return 'OPER';
  }
}
function TagInsert(tag, elm) {
    window.document.getElementById(elm).focus();
    if(tag == '[a]') {
      var userInput = prompt("Please enter the site you'd like to link", "http://");
      tag = '<a href=\'' + userInput + '\'></a>';
    }
    var rng = document.selection.createRange();
    rng.text = tag;
}
function TagInsert_2(tag1, tag2, elm) {
    window.document.getElementById(elm).focus();
    var rng = document.selection.createRange();
    
    if(tag1 == '[a]') {
      var userInput = prompt("Введите ссылку", "http://");
      var userInput_2 = '';
 //     tag1 = '[a href="' + userInput + '" text="' + rng.text + '"]';
      if (userInput != 'http://' && userInput != null) { 
        if (!rng.text) {
           userInput_2 = prompt("Введите название ссылки", "");
        }
        rng.text = '[a href="' + userInput + '" text="' + (rng.text ? rng.text : userInput_2) + '"]';    
      } else {
        rng.text = '';
      }
    } else {  
      rng.text = tag1 + rng.text + tag2;
    }
}

function CreatePanel(elm) {
  if(BrowserType() == 'MSIE') {
    document.write('<input style="background: url(/img/button/but1.jpg) no-repeat; border: none; width:31px; height: 21px; margin-bottom: 8px;"  type="button"  OnClick="TagInsert_2(\'[b]\', \'[/b]\', \'' + elm + '\')" title="Жирный. Выделите нужный текст и нажмите кнопку">');
    document.write('&nbsp;');
    document.write('<input style="background: url(/img/button/but2.jpg) no-repeat; border: none; width:31px; height: 21px; margin-bottom: 8px;" type="button" value="<i>" OnClick="TagInsert_2(\'[i]\', \'[/i]\', \'' + elm + '\')" title="Курсив. Выделите нужный текст и нажмите кнопку">');
//    document.write('&nbsp;');
//    document.write('<input type="button" value="<br>" OnClick="TagInsert(\'[br]\', \'' + elm + '\')">');
    document.write('&nbsp;');
    document.write('<input style="background: url(/img/button/but3.jpg) no-repeat; border: none; width:31px; height: 21px; margin-bottom: 8px;" type="button" value="<u>" OnClick="TagInsert_2(\'[u]\', \'[/u]\', \'' + elm + '\')" title="Подчеркивание. Выделите нужный текст и нажмите кнопку">');
    document.write('&nbsp;');
    document.write('<input style="background: url(/img/button/but4.jpg) no-repeat; border: none; width:31px; height: 21px; margin-bottom: 8px;" class=reg_input3 type="button" value="url" OnClick="TagInsert_2(\'[a]\', \'\', \'' + elm + '\')" title="Ссылка. Выделите нужный текст и нажмите кнопку">');
    document.write('&nbsp;');
  }
  else {
    document.write('<input  style="background: url(/img/button/but1.jpg) no-repeat;  border: none; width:31px; height: 21px; margin-bottom: 8px; margin-right: 7px;" type="button"  onclick="return pnhEditTextarea(\'' + elm + '\', \'bold\')"  title="Жирный. Выделите нужный текст и нажмите кнопку">');
    document.write('<input style="background: url(/img/button/but2.jpg) no-repeat; border: none; width:31px; height: 21px; margin-bottom: 8px; margin-right: 7px;"  type="button"  onclick="return pnhEditTextarea(\'' + elm + '\', \'cursive\')" title="Курсив. Выделите нужный текст и нажмите кнопку">');
       document.write('<input style="background: url(/img/button/but3.jpg) no-repeat; border: none; width:31px; height: 21px; margin-bottom: 8px; margin-right: 7px;" type="button"  onclick="return pnhEditTextarea(\'' + elm + '\', \'u\')" title="Подчеркивание. Выделите нужный текст и нажмите кнопку">');
    document.write('<input style="background: url(/img/button/but4.jpg) no-repeat; border: none; width:31px; height: 21px; margin-bottom: 8px; margin-right: 7px;"  type="button"  onclick="return pnhEditTextarea(\'' + elm + '\', \'a_href\')" title="Ссылка. Выделите нужный текст и нажмите кнопку">');

  } 
}


// for Mozilla
function pnhGetSelectionStart(element) {
    var startpos = 0;
    startpos = element.selectionStart; // moz
    return startpos;
}

function pnhGetSelectionEnd(element) {
    var endpos = 0;
    endpos = element.selectionEnd; // moz
    return endpos;

}

function pnhSetSelectionStart(element,newposition) {
    element.selectionStart = newposition; // moz
}


function pnhSetSelectionEnd(element,newposition) {
    element.selectionEnd = newposition; // moz
}

function pnhSetSelection(element,newstart,newend) {
    pnhSetSelectionStart(element,newstart);
    pnhSetSelectionEnd(element,newend);
}


// Inserts a string at a given position
function pnhStringInsert(DOMEle,newtext,newpos) {
    DOMEle.value=DOMEle.value.slice(0,newpos)+newtext+DOMEle.value.slice(newpos);
}


function pnhEditTextarea(textarea_id, action) {
    // grab the textarea element off the dom tree
    var ta = document.getElementById(textarea_id);

    // decide what you're addding
    var startTag = "";
    var endTag = "";

    switch (action) {
        case "bold":
            startTag = "[b]";
            endTag   = "[/b]";
            break;
        case "cursive":
            startTag = "[i]";
            endTag   = "[/i]";
            break;
        case "br":
            startTag = "";
            endTag   = "[br]";
            break;
        case "u":
            startTag = "[u]";
            endTag   = "[/u]";
            break;
        case "strong":
            startTag = "[strong]";
            endTag   = "[/strong]";
            break;
        case "emphasis":
            startTag = "[em]";
            endTag   = "[/em]";
            break;
        case "a_href":
            var userInput = prompt("Введите ссылку", "http://");
//            startTag = "[a href=\""+userInput+"\"]";
            if (userInput != 'http://' && userInput != null) {   
                startTag = '[a href="' + userInput + '" text="';           
                endTag = '"]';
            } else {
                startTag = '';           
                endTag = '';
            }
            break;


    }


    // decide where to add it and then add it

//    alert("selectionEnd,length: "+ta.selectionEnd+","+ta.value.length);

    var firstPos = pnhGetSelectionStart(ta);
    var secondPos = pnhGetSelectionEnd(ta)+startTag.length; // we're inserting one at a time
    
    if (action == "a_href" && pnhGetSelectionStart(ta) == pnhGetSelectionEnd(ta)) {
           userInput_2 = prompt("Введите название ссылки", "");
           endTag = userInput_2 + endTag;
    }
    pnhStringInsert(ta,startTag,firstPos);
    pnhStringInsert(ta,endTag,secondPos);


    // reset focus... after the first tag and before the second
    pnhSetSelectionStart(ta,firstPos+startTag.length);
    pnhSetSelectionEnd(ta,secondPos);
    ta.focus();

    return false;
}

// !for Mozilla
