var textarea;  
var len;  
var start;  
var end;  
var txt;

function converter()
{
  textarea = document.getElementById("texto");
  len = textarea.value.length;
  start = textarea.selectionStart;
  end = textarea.selectionEnd;
  txt = textarea.value.substring(start, end);

  /* - - - - REGRAS PARA CONVERSAO - - - - */

  /* (x)html livre por linha */
  txt = txt.replace(/([a-z0-9]+?)§([^\r\n]+)/gi, '<$1>$2</$1>');


  /* (x)html */

  //span
  txt = txt.replace(/[-]{2}(.+)?[-]{2}/gi, '<span>$1</span>');
  //strong
  txt = txt.replace(/[*]{2}(.+)?[*]{2}/gi, '<strong>$1</strong>');
  //link
  txt = txt.replace(/\[(a|link)[ ]+?([^ ]+?)[ ]+?\"?(.+?)\"?\]/gi, '<a href="$2" title="$3">$3</a>');
  //img
  txt = txt.replace(/\[img[ ]+?([^ ]+?)[ ]+?\"?(.+?)\"?\]/gi, '<img src="$1" alt="$2" />');
  //abbr
  txt = txt.replace(/\[(ab|abbr)[ ]+?\"(.+?)\"[ ]+?([^ ]+?)\]/gi, '<abbr title="$2">$3</abbr>');
  //acronym
  txt = txt.replace(/\[(ac|acronym)[ ]+?\"(.+?)\"[ ]+?([^ ]+?)\]/gi, '<acronym title="$2">$3</acronym>');
  //em
  txt = txt.replace(/[/]{2}(.+)?[/]{2}/gi, '<em>$1</em>');

    /* formulario */

    //input text
    txt = txt.replace(/\[(txt|text)[ ]+?([^ ]+?)[ ]+?\"?(.+?)\"?\]/gi, '<input type="text" name="$2" id="$2" value="$3" />');
    //input radio
    txt = txt.replace(/\[radio[ ]+?([^ ]+?)[ ]+?\"?(.+?)\"?\]/gi, '<input type="radio" name="$1" id="$1" value="$2" />');
    //input checkbox
    txt = txt.replace(/\[(check|checkbox)[ ]+?([^ ]+?)[ ]+?\"?(.+?)\"?\]/gi, '<input type="checkbox" name="$2" id="$2" value="$3" />');
    //button
    txt = txt.replace(/\[(botao|bt)[ ]+?\"?(.+?)\"?\]/gi, '<button type="submit">$2</button>');

  /* - - - - FIM REGRAS PARA CONVERSAO - - - - */

  // Converte a area selecionada
  textarea.value = textarea.value.substring(0,start) + txt + textarea.value.substring(end,len);  
}

function limparCampo()
{
  textarea = document.getElementById("texto");  
  textarea.value = "";
  textarea.focus();
}

function clear_html()
{
  textarea = document.getElementById("texto");
  len = textarea.value.length;
  start = textarea.selectionStart;
  end = textarea.selectionEnd;
  txt = textarea.value.substring(start, end);

  if (txt) {

    /* procura tags (x)html */
    txt = txt.replace(/<\/?(.+?)>/gi, '');

    textarea.value = textarea.value.substring(0,start) + txt + textarea.value.substring(end,len);

  } else {
    alert("Selecione algum texto.");
  }
}