function DoConfirm(mensagem, url) {
	if(confirm(mensagem)) location.href = url;
}
function smilie(thesmilie) {
    AddTxt=thesmilie;
    AddText(AddTxt);
}

function AddText(text) {
    if (document.noticia.texto.createTextRange && document.noticia.texto.caretPos) {
	  var caretPos = document.noticia.texto.caretPos;
	  caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?	text + ' ' : text;
    } else {
        document.noticia.texto.value += text;
	  document.noticia.texto.focus(caretPos)
    }
}

var clientPC = navigator.userAgent.toLowerCase();
var clientVer = parseInt(navigator.appVersion);
var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav  = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1) && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1) && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
var is_win   = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac    = (clientPC.indexOf("mac")!=-1);

b_help = "Texto sobrecarregado: [b]texto[/b]  (alt+b)";
i_help = "Texto itálico: [i]texto[/i]  (alt+i)";
u_help = "Texto sublinhado: [u]texto[/u]  (alt+u)";
q_help = "Texto citado: [quote]texto[/quote]  (alt+q)";
c_help = "Marca de código: [code]código[/code]  (alt+c)";
l_help = "Lista: [list]texto[/list] (alt+l)";
o_help = "Lista ordenada: [list=]texto[/list]  (alt+o)";
p_help = "Inserir imagem: [img]http://url_da_imagem[/img]  (alt+p)";
w_help = "Inserir URL: [url]http://url[/url] ou [url=http://url]URL texto[/url]  (alt+w)";
a_help = "Fechar todas as marcas de bbCode";
s_help = "Cor: [color=red]texto[/color]  Tip: pode também usar color=#FF0000";
f_help = "Fonte: [size=x-small]texto pequeno[/size]";

bbcode = new Array();
bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[quote]','[/quote]','[code]','[/code]','[list]','[/list]','[list=]','[/list]','[img]','[/img]','[url]','[/url]');
imageTag = false;

function helpline(help) {
     document.noticia.helpbox.value = eval(help + "_help");
}

function getarraysize(thearray) {
    for(i = 0; i < thearray.length; i++) {
	  if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null)) {
	      return i;
        }
    }
    return thearray.length;
}

function arraypush(thearray,value) {
    thearray[ getarraysize(thearray) ] = value;
}

function arraypop(thearray) {
    thearraysize = getarraysize(thearray);
    retval = thearray[thearraysize - 1];
    delete thearray[thearraysize - 1];
    return retval;
}
function checkForm() {
    formErrors = false;
    if (document.noticia.texto.value.length < 2) {
	  formErrors = "Deve ser escrita a mensagem";
    }
    if (formErrors) {
	  alert(formErrors);
	  return false;
    } else {
	  bbstyle(-1);
	  return true;
    }
}

function emoticon(text) {
    text = ' ' + text + ' ';
    if (document.noticia.texto.createTextRange && document.noticia.texto.caretPos) {
	  var caretPos = document.noticia.texto.caretPos;
	  caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
	  document.noticia.texto.focus();
    } else {
	  document.noticia.texto.value  += text;
	  document.noticia.texto.focus();
    }
}

function bbfontstyle(bbopen, bbclose) {
    if ((clientVer >= 4) && is_ie && is_win) {
	   theSelection = document.selection.createRange().text;
	   if (!theSelection) {
	       document.noticia.texto.value += bbopen + bbclose;
	       document.noticia.texto.focus();
	       return;
	   }
	   document.selection.createRange().text = bbopen + theSelection + bbclose;
	   document.noticia.texto.focus();
	   return;
     } else {
	   document.noticia.texto.value += bbopen + bbclose;
	   document.noticia.texto.focus();
	   return;
     }
     storeCaret(document.noticia.texto);
}

function bbstyle(bbnumber) {
    donotinsert = false;
    theSelection = false;
    bblast = 0;
    if (bbnumber == -1) {
	  while(bbcode[0]) {
	      butnumber = arraypop(bbcode) - 1;
	      document.noticia.texto.value += bbtags[butnumber + 1];
	      buttext = eval('document.noticia.addbbcode' + butnumber + '.value');
	      eval('document.noticia.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
	  }
	  imageTag = false;
	  document.noticia.texto.focus();
	  return;
    }
    if ((clientVer >= 4) && is_ie && is_win) {
	  theSelection = document.selection.createRange().text;
    }
    if (theSelection) {
	  document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
	  document.noticia.texto.focus();
	  theSelection = '';
	  return;
    }
    for(i = 0; i < bbcode.length; i++) {
	  if (bbcode[i] == bbnumber+1) {
	      bblast = i;
	      donotinsert = true;
	  }

    }
    if (donotinsert) {
	  while(bbcode[bblast]) {
	      butnumber = arraypop(bbcode) - 1;
	      document.noticia.texto.value += bbtags[butnumber + 1];
	      buttext = eval('document.noticia.addbbcode' + butnumber + '.value');
	      eval('document.noticia.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
	      imageTag = false;
	   }
	   document.noticia.texto.focus();
	   return;
     } else {
	   if (imageTag && (bbnumber != 14)) {
	       document.noticia.texto.value += bbtags[15];
	       lastValue = arraypop(bbcode) - 1;
	       document.noticia.addbbcode14.value = "Img";
	       imageTag = false;
	   }
	   document.noticia.texto.value += bbtags[bbnumber];
	   if ((bbnumber == 14) && (imageTag == false)) imageTag = 1;
	   arraypush(bbcode,bbnumber+1);
	   eval('document.noticia.addbbcode'+bbnumber+'.value += "*"');
	   document.noticia.texto.focus();
	   return;
     }
     storeCaret(document.noticia.texto);
}

function storeCaret(textEl) {
    if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}