function saveEditorData(elementId){
	// pega sempre a ultima instancia
	for (n in tinyMCE.instances) {
		inst = tinyMCE.instances[n];
		if (tinyMCE.isInstance(inst)){
			continue;
		}
	}
	elem = document.getElementById(elementId); 
	elem.value = inst.getHTML();
}


String.prototype.trim = function(){
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function checkForm(formIds){
	result = false;
	formId = document.getElementById(formIds);

	elementsLength = formId.elements.length;
	for(i=0; i < elementsLength; i++){
		if(
			formId.elements[i].type == "text" || 
			formId.elements[i].type == "password" || 
			formId.elements[i].type == "file" || 
			formId.elements[i].type == "textarea" 
		){
			valor = formId.elements[i].value.trim();
			if(valor.length == 0){
				title = formId.elements[i].title;
				if(title.substring(0,1) == "*"){
					title = title.substring(1,title.length);
					alert('Preencha corretamente o campo '+ title);
					return false;
				}
			}
		}
		else if(formId.elements[i].type == "select-one"){
			valor = formId.elements[i].value.trim();
			if(formId.elements[i].options[formId.elements[i].selectedIndex].value == ""){
				title = formId.elements[i].title;
				if(title.substring(0,1) == "*"){
					title = title.substring(1,title.length);
					alert('Preencha corretamente o campo '+ title);
					return false;
				}
			}
		}
	}
}

chatPodeEnviar = true;
function sendChatMsg(){
	result = false;
	sizeMsg = document.forms['chatForm'].elements['CHMMensagem'].value.length;
	if(sizeMsg > 0 && chatPodeEnviar){
		result = true;
	}
	else{
		if(!chatPodeEnviar) alert('Aguarde alguns instantes!');
		if(sizeMsg == 0) alert('Por favor, digite uma mensagem antes de enviar!');
		result = false;
	}
	chatPodeEnviar = false;
	setTimeout('chatPodeEnviar = true;', 3000);
	return result;
}

function closeToolBox(){
	theDiv = document.getElementById('caixa_ferramentas');
	theDiv.style.display = 'none';
}
function relatarErros(){
	callAjaxData('caixa_ferramentas', '/index.php?snippet=atualiza_bit&bit=relatar_erro', true, '')
	theDiv = document.getElementById('caixa_ferramentas');
	theDiv.style.display = 'inline';
}


function openCommentBox(NOTCodigo, VIDCodigo, ARTCodigo){
	callAjaxData('caixa_comentarios', '/index.php?snippet=atualiza_bit&bit=emitir_comentario&NOTCodigo='+NOTCodigo+'&VIDCodigo='+VIDCodigo+'&ARTCodigo='+ARTCodigo, true, '')
	theDiv = document.getElementById('caixa_comentarios');
	theDiv.style.display = 'inline';
}

function closeCommentBox(){
	theDiv = document.getElementById('caixa_comentarios');
	theDiv.style.display = 'none';
}

function abreCaixaForumResposta(id, citando, nome, data){
	displayType = (navigator.appName == "Netscape") ? 'table-row' : 'inline';
	theDiv = document.getElementById('caixa_resposta');
	theDivTxt = document.getElementById('caixa_resposta_txt');
	theDiv.style.display = displayType;
	if(citando == 'S'){
		theDivTxt.innerHTML = "Você está postando sua opinião em resposta à mensagem de <b>" +nome+ "</b> postada em <b>" + data + "</b>. Sua mensagem será posicionada no topo da lista.";
		document.forms["form_resposta"].citando_nome.value = nome;
		document.forms["form_resposta"].citando_data.value = data;
	}
	else{
		theDivTxt.innerHTML = 'Você está postando uma nova mensagem.<br />Esta mensagem será posicionada no topo da lista.';
		document.forms["form_resposta"].citando_nome.value = '';
		document.forms["form_resposta"].citando_data.value = '';
	}
}

function ExcluirSelecionadas(CLICodigo, snippet, form){
	if(confirm('Deseja realmente excluir os ítens selecionados?')){
		a_excluir = "";
		for(i=0; i<document.forms[form].elements.length; i++){
			if(document.forms[form].elements[i].checked)
			a_excluir = a_excluir + document.forms[form].elements[i].value+",";
		}
		document.location.href='index.php?snippet='+snippet+'&CLICodigo='+CLICodigo+'&acao=excluir&a_excluir='+a_excluir;
	}
}

function AbrirAdmin(url){
	janAdmin = window.open(url, 'janAdmin', 'width=787, height=500, left=1, top=1, scrollbars=yes, resizable=1');
}

function AbreJanela(URL, width, height, top, left, scroll){
	//	window.showModalDialog(URL,ArgValues,'dialogHeight: '+height+'px; dialogWidth: '+width+'px; dialogTop: '+top+'px; dialogLeft: '+left+'px; edge: Raised; scroll: '+scroll+'; unadorned: Yes; center: No; help: No; resizable: No; status: No;');
	janGraph = window.open(URL, 'janela_popup', 'width='+width+', height='+height+', left='+left+', top='+top+', scrollbars='+scroll+', resizable=1');
}

///////////// Seleciona todos os checkbox
var checkflag = "false";
function check(field) {
	if (checkflag == "false") {
		for (i = 0; i < field.length; i++) {
			field[i].checked = true;
		}
		checkflag = "true";
		return true;
	}
	else {
		for (i = 0; i < field.length; i++) {
			field[i].checked = false;
		}
		checkflag = "false";
		return true;
	}
}

function mascaraCampo(objForm, strField, sMask, evtKeyPress) {
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	if(document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode; }
	else if(document.layers) { // Nestcape
		nTecla = evtKeyPress.which;
	}
	sValue = objForm[strField].value;
	// Limpa todos os caracteres de formatação que
	// já estiverem no campo.
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ",", "" );
	sValue = sValue.toString().replace( ",", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
	fldLen = sValue.length;
	mskLen = sMask.length;
	i = 0;
	nCount = 0;
	sCod = "";
	mskLen = fldLen;
	while (i <= mskLen) {
		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ","))
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
		if (bolMask) {
			sCod += sMask.charAt(i);
			mskLen++; }
		else {
			sCod += sValue.charAt(nCount);
			nCount++;
		}
		i++;
	}
	objForm[strField].value = sCod;
	if (nTecla != 8) { // backspace
		if (sMask.charAt(i-1) == "9") { // apenas números...
			return ((nTecla > 47) && (nTecla < 58)); 
		} // números de 0 a 9
		else { // qualquer caracter...
			return true;
		}
	}
	else {
		return true;
	}
}

