// VARIAVEIS DE INICIALIZACAO
var imageTag = false;
var theSelection = false;

// CHECA O BROWSER
var clientPC = navigator.userAgent.toLowerCase(); // INFO CLIENT
var clientVer = parseInt(navigator.appVersion); // VERSAO BROWSER

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);

// DEFINE AS TAGS
bbcode = new Array();
bbtags = new Array('<b>','</b>','<i>','</i>','<u>','</u>','&lt;','&gt;','<font color=#C52727>','</font>','<font color=#00FF00>','</font>','<font color=#0000FF>','</font>','<span class="texto_normal">','</span>','<span class="verdana11">','</span>');
imageTag = false;

// REPLACEMENT FOR ARRAYNAME.LENGTH PROPERTY
function getarraysize(thearray) {
    for (i = 0; i < thearray.length; i++) {
        if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
            return i;
    }
    return thearray.length;
}

// REPLACEMENT FOR ARRAYNAME.PUSH(VALUE) NOT IMPLEMENTED IN IE UNTIL VERSION 5.5
// APPENDS ELEMENT TO THE ARRAY
function arraypush(thearray,value) {
    thearray[ getarraysize(thearray) ] = value;
}

// REPLACEMENT FOR ARRAYNAME.POP() NOT IMPLEMENTED IN IE UNTIL VERSION 5.5
// REMOVES AND RETURNS THE LAST ELEMENT OF AN ARRAY
function arraypop(thearray) {
    thearraysize = getarraysize(thearray);
    retval = thearray[thearraysize - 1];
    delete thearray[thearraysize - 1];
    return retval;
}

function bbfontstyle(bbopen, bbclose) {
    if ((clientVer >= 4) && is_ie && is_win) {
        theSelection = document.selection.createRange().text;
        if (!theSelection) {
            eval("document."+theform+"."+campo_ativo+".value += bbopen + bbclose");
            eval("document."+theform+"."+campo_ativo+".focus()");
            return;
        }
        document.selection.createRange().text = bbopen + theSelection + bbclose;
        eval("document."+theform+"."+campo_ativo+".focus()");
        return;
    } else {
        eval("document."+theform+"."+campo_ativo+".value += bbopen + bbclose");
        eval("document."+theform+"."+campo_ativo+".focus()");
        return;
    }
    storeCaret(eval("document."+theform+"."+campo_ativo));
}

function bbstyle(bbnumber) {
    if(!is_ie) {
        alert('Erro. Navegador Incompatível.');
        return false;
    }
    donotinsert = false;
    theSelection = false;
    bblast = 0;
    if (bbnumber == -1) { // FECHA TODAS AS TAGS ABERTAS
        while (bbcode[0]) {
            butnumber = arraypop(bbcode) - 1;
            eval("document."+theform+"."+campo_ativo+".value += bbtags[butnumber + 1]");
            buttext = eval('document.'+theform+'.addbbcode' + butnumber + '.value');
            eval('document.'+theform+'.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
        }
        imageTag = false; // TODAS AS TAGS SAO FECHADAS INCLUSIVE A DE IMAGEM
        eval("document."+theform+"."+campo_ativo+".focus()");
        return;
    }

    if ((clientVer >= 4) && is_ie && is_win)
        theSelection = document.selection.createRange().text; // Get text selection

    if (theSelection) {
        // ADICIONA AS TAGS NA SELECAO
        document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
        eval("document."+theform+"."+campo_ativo+".focus()");
        theSelection = '';
        return;
    }

    // FIND LAST OCCURANCE OF AN OPEN TAG THE SAME AS THE ONE JUST CLICKED
    for (i = 0; i < bbcode.length; i++) {
        if (bbcode[i] == bbnumber+1) {
            bblast = i;
            donotinsert = true;
        }
    }

    if (donotinsert) {		// CLOSE ALL OPEN TAGS UP TO THE ONE JUST CLICKED & DEFAULT BUTTON NAMES
        while (bbcode[bblast]) {
            butnumber = arraypop(bbcode) - 1;
            eval("document."+theform+"."+campo_ativo+".value += bbtags[butnumber + 1]");
            buttext = eval('document.'+theform+'.addbbcode' + butnumber + '.value');
            eval('document.'+theform+'.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
            imageTag = false;
        }
        eval("document."+theform+"."+campo_ativo+".focus()");
        return;
    } else { // Open tags
        if (imageTag && (bbnumber != 14)) {		// FACHA A TAG IMAGEM ANTES DE ADICIONAR OUTRA
            eval("document."+theform+"."+campo_ativo+".value += bbtags[15]");
            lastValue = arraypop(bbcode) - 1;	// REMOVE THE CLOSE IMAGE TAG FROM THE LIST
            eval("document."+theform+".addbbcode14.value = 'Aa';");	// RETURN BUTTON BACK TO NORMAL STATE
            imageTag = false;
        }

        // ABRE A TAG
        eval("document."+theform+"."+campo_ativo+".value += bbtags[bbnumber]");
        if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // CHECK TO STOP ADDITIONAL TAGS AFTER AN UNCLOSED IMAGE TAG
        arraypush(bbcode,bbnumber+1);
        eval('document.'+theform+'.addbbcode'+bbnumber+'.value += "*"');
        eval("document."+theform+"."+campo_ativo+".focus()");
        return;
    }
    storeCaret(eval("document."+theform+"."+campo_ativo));
}

// INSERT AT CLARET POSITION. CODE FROM
function storeCaret(textEl) {
    if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

ns4 = (document.layers) ? true:false
ie4 = (document.all) ? true:false
visible = false;

function hide(id) {
	if (ns4) document.layers[id].visibility = "hide"
	else	 document.getElementById(id).style.display="none";
}
function show(id) {
	if (ns4) document.layers[id].visibility = "show"
	else	 document.getElementById(id).style.display="";
}

function escreve(id, txt) {
	if (ns4) {
		var lyr = document.layers[id].document
		lyr.write(txt)
		lyr.close()
	}
	else document.getElementById(id).innerHTML = txt
}

function assistente(txt, nid, campo) {
	html = '<div style="font-family: Arial, Helvetica, sans-serif; font-size: 11px; border: solid 1px; border-color: #FFAA00; padding: 3; background-color: #EFF3F7; width: 369">'+txt+'</div>';
	escreve(nid,html);
    if(visible == false) {
        show(nid);
        visible = true;
        eval("document.all['"+campo+"'].value = '¿'");
    }
    else {
        hide(nid);
        visible = false;
        eval("document.all['"+campo+"'].value = '?'");
    }
}

