  function get_el(id) {
    if      (document.layers)         return document.layers[id];         // NC 4
    else if (document.all)            return document.all[id];            // IE 4
    else if (document.getElementById) return document.getElementById(id); // DOM
    else                              return null;        
  }

  function ins2(txt1, txt2) {
    var txtarea;
    txtarea = document.forms['post'].msg_body; 
    txtarea.focus();
    
    if (document.selection) {
      theSelection = document.selection.createRange().text; // Get text selection
      document.selection.createRange().text = txt1 + theSelection + txt2;
      theSelection = '';
      return;
    }
    else
    if (txtarea.selectionEnd)
      mozWrap(txtarea, txt1, txt2);
    else
      txtarea.value = txtarea.value + txt1 + txt2;
  }
  
  function ins(txt){
    ins2(txt,' ');
  }

  function checktag(tag){
    tag=tag.substring(0,tag.length-1).toLowerCase(); //Extract tag
    if (tag=="br") return "\n";

    if (tag=="b") return "[b]";
    if (tag=="/b") return "[/b]";
    if (tag=="i") return "[i]";
    if (tag=="/i") return "[/i]";
    if (tag=="u") return "[u]";
    if (tag=="/u") return "[/u]";
    
    if (tag=="div class=\"d1\"") return "[q]";
    if (tag=="/div") return "[/q]";
    
    if (tag=="ul") return "[list]";
    if (tag=="/ul") return "[/list]";
    if (tag=="li") return "[*]";
    
    if (tag.indexOf("img") == 0) { 
      var idx = tag.indexOf("title=\"");
      if (idx >= 0) { 
        var smile = tag.substring(idx + 7, tag.length - 1);
	return smile.substring(0, smile.indexOf("\""));
      }
    }

    return '';
  }

  function quotmsg(id) {
    var m=get_el('quote_' + id).innerHTML;
    var a=get_el('author_' + id).innerHTML;
    var b="";
    var tag="";
    var c=true;
  
    for (var i=0; i < m.length; i++) {
      if (m.charAt(i)=='<') {
        c=false;
        tag="";
        continue;
      }
  
      if (c) 
        b += m.charAt(i); 
      else 
//      if (m.charAt(i) != '"') 
        tag += m.charAt(i);
  
      if (m.charAt(i) == '>') {
        c = true;
        b += checktag(tag); //Replace tag
      }
    }
    
    ins("[q][b]" + a + " писал(а):[/b]\n"+b+"\n[/q]\n");
  }

  function getSel() {
    if (window.getSelection)
       return window.getSelection();
    else if (document.selection)
       return document.selection.createRange().text;
    else if (document.getSelection)
       return document.getSelection();
    else return '';
  }

  function quot(id){
    var b = getSel();
    var a = get_el('author_' + id).innerHTML;
    if (b != '')
      ins("[q][b]" + a + " писал(а):[/b] \n"+b+"[/q]\n");
    else 
      quotmsg(id); 
  }
  
  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 trim(s){
    if (s=='') return '';
    while (s.substring(0,1) == ' ')
      s = s.substring(1, s.length);

    while (s.substring(s.length-1, s.length) == ' ')
      s = s.substring(0,s.length-1);

    return s;
  }
  
  function CheckSend(){
    if (trim(document.forms['newmess'].nick.value)=='') {
      alert('Вы забыли, как Вас зовут?');
      return false;
    }

    if (trim(document.forms['newmess'].mess.value)=='') {
      alert('Вы забыли, что хотели написать?');
      return false;
    }
    return true;
  }

  var m=1;
  function ShowMore(){
    if (m<5) {
      var x=get_el('att'+m);
      x.style.display='block';
      x.style.visibility='visible';
      m++;
      if (m>4) {
        x=get_el('lnk');
        x.style.display='none';
        x.style.visibility='hidden';
      }
    }
  }

  function CheckAll(frm, val) {
    var c = document.forms[frm].elements.length;
    for (i=0; i<c; i++) {
      if (document.forms[frm].elements[i].type=='checkbox')
        document.forms[frm].elements[i].checked=val;
    }
  }
