function compileAreas(){
result='';
	if (document.form.chkareas[0].checked) {
		result=result+document.form.chkareas[0].value+';';
		}
	if (document.form.chkareas[1].checked) {
		result=result+document.form.chkareas[1].value+';';
		}
	if (document.form.chkareas[2].checked) {
		result=result+document.form.chkareas[2].value+';';
		}
		
	if (document.form.chkareas[3].checked) {
		result=result+document.form.chkareas[3].value+';';
		}	
	if (document.form.chkareas[4].checked) {
		result=result+document.form.chkareas[4].value+';';
		}	
	if (document.form.chkareas[5].checked) {
		result=result+document.form.chkareas[5].value+';';
		}	
	if (document.form.chkareas[6].checked) {
		result=result+document.form.chkareas[6].value+';';
		}
	document.form.areas.value=result;
}

function valida_usuario(mode){
  if (document.form.nome.value==""){
  	alert("Preencha o seu nome.");
	document.form.nome.focus();
	return false;
	}
	
  if (document.form.login.value==""){
  	alert("Preencha o login.");
	document.form.login.focus();
	return false;
	}
	
  if ((document.form.email.value.indexOf("@")==-1) || (document.form.email.value.indexOf(".")==-1) || (document.form.email.value=="")){
  	alert("E-mail inválido.");
	document.form.email.focus();
	return false;
	}
	
  if (mode=="insert"){
  	if (document.form.senha.value==""){
  		alert("Digite uma senha");
		document.form.senha.focus();
		return false;
		}
	}
	
  if (document.form.senha.value!=document.form.senha2.value){
		document.form.senha.value="";
		document.form.senha2.value="";
		alert("A senha informada não confere com a confirmada, redigite.");
		document.form.senha.focus();
		return false;
		}
return true;
}

function confirmaExclusaoUsuario(id){
	if (confirm("Tem certeza que quer excluir este usuário?")){
		location.href("exclui_user.php?user_id="+id);
	}
}

function confirmaExclusaoTipo(id){
	if (confirm("Tem certeza que quer excluir este tipo?")){
		location.href("exclui_tipo.php?tipo_id="+id);
	}
}

function confirmaExclusaoTema(id){
	if (confirm("Tem certeza que quer excluir este tema?")){
		location.href("exclui_tema.php?tema_id="+id);
	}
}

function confirmaExclusaoFonte(id){
	if (confirm("Tem certeza que quer excluir esta fonte?")){
		location.href("exclui_fonte.php?fonte_id="+id);
	}
}

function confirmaExclusaoMoeda(id){
	if (confirm("Tem certeza que quer excluir esta moeda?")){
		location.href("exclui_moeda.php?moeda_id="+id);
	}
}

function confirmaExclusaoConteudo(id){
	if (confirm("Tem certeza que quer excluir este tipo?")){
		location.href("exclui_conteudo.php?conteudo_id="+id);
	}
}

function confirmaExclusaoPublicacao(id){
	if (confirm("Tem certeza que quer excluir esta publicação?")){
		location.href("exclui_publicacao.php?pub_id="+id);
	}
}

function confirmaExclusaoImagem(id){
	if (confirm("Tem certeza que quer excluir esta imagem?")){
		location.href("exclui_imagem.php?file_id="+id);
	}
}

function confirmaExclusaoDocumento(id){
	if (confirm("Tem certeza que quer excluir este documento?")){
		location.href("exclui_documento.php?file_id="+id);
	}
}

function confirmaPublicacaoConteudo(id, status,obj){
	if (confirm("Tem certeza que quer alterar o estado de publicação deste conteúdo?")){
		location.href("publicar_texto.php?conteudo_id="+id+"&status="+status);
	} else return false;
}

function confirmaPublicacaoPublicacao(id, status,obj){
	if (confirm("Tem certeza que quer alterar o estado de publicação desta publicação?")){
		location.href("publicar_publicacao.php?pub_id="+id+"&status="+status);
	} else return false;
}

function confirmaEsgotamentoPublicacao(id, status,obj){
	if (confirm("Tem certeza que quer modificar o estado do estoque desta publicação?")){
		location.href("esgotar_publicacao.php?pub_id="+id+"&estoque="+status);
	} else return false;
}

function confirmaPublicacaoOnline(id, status,obj){
	if (confirm("Tem certeza que quer modificar esta publicação?")){
		location.href("online_publicacao.php?pub_id="+id+"&online="+status);
	} else return false;
}

function confirmaDestaquePublicacao(id, status,obj){
	if (confirm("Tem certeza que quer modificar o destaque desta publicação?")){
		location.href("destaque_publicacao.php?pub_id="+id+"&destaque="+status);
	} else return false;
}

function confirmaNovidadePublicacao(id, status,obj){
	if (confirm("Tem certeza que quer modificar a novidade desta publicação?")){
	    location.href("novidade_publicacao.php?pub_id="+id+"&novidade="+status);
	} else return false;
}

function validaMoeda(){
	if (document.form.simbolo.value==""){
		alert("Preencha o símbolo.");
		document.form.simbolo.focus();
		return false;
	}
	
	if (document.form.moeda.value==""){
		alert("Preencha o nome da moeda.");
		document.form.moeda.focus();
		return false;
	}
return true;
}

function validaTipo(){
	if (document.form.tipo.value==""){
		alert("Preencha o tipo.");
		document.form.tipo.focus();
		return false;
	}
return true;
}

function validaTema(){
	if (document.form.tema.value==""){
		alert("Preencha o tema.");
		document.form.tema.focus();
		return false;
	}
return true;
}

function validaFonte(){
	if (document.form.fonte.value==""){
		alert("Preencha a fonte.");
		document.form.fonte.focus();
		return false;
	}
return true;
}

function validaConteudo(){
	if (document.form.titulo.value==""){
		alert("Preencha o título.");
		document.form.título.focus();
		return false;
	}
	
	if (document.form.resumo.value==""){
		alert("Preencha o sutiã.");
		document.form.resumo.focus();
		return false;
	}

	if (document.form.data.value==""){
		alert("Preencha a data.");
		document.form.resumo.focus();
		return false;
	}
	
	if (document.form.periodo_inicio.value!="" & document.form.periodo_fim.value!="") {
		i=document.form.periodo_inicio.value;
		f=document.form.periodo_fim.value
		if (Date.UTC(i.substr(6,4),i.substr(3,2),i.substr(0,2))>=Date.UTC(f.substr(6,4),f.substr(3,2),f.substr(0,2))){
			alert("Período inválido.");
			document.form.periodo_fim.value="";
			document.form.periodo_fim.focus();
			return false;
		}
	}
return true;
}

function validaPublicacao(){
	if (document.form.titulo.value==""){
		alert("Preencha o título.");
		document.form.título.focus();
		return false;
	}
	
	if (document.form.imagem.value==""){
		alert("Selecione uma imagem.");
		document.form.imagem.focus();
		return false;
	}
return true;
}

function validaCliente(){
	if (document.form.nome.value==""){
		alert("Preencha o seu nome.");
		document.form.nome.focus();
		return false;
	}
	if (document.form.endereco.value==""){
		alert("Preencha seu endereço.");
		document.form.endereco.focus();
		return false;
	}

	if (document.form.CEP.value==""){
		alert("Preencha o CEP.");
		document.form.CEP.focus();
		return false;
	}
	
	if (document.form.cidade.value==""){
		alert("Preencha a sua cidade.");
		document.form.cidade.focus();
		return false;
	}
		
	if (document.form.estado.value==""){
		alert("Selecione um estado.");
		document.form.estado.focus();
		return false;
	}
	
	if (document.form.email.value==""){
		alert("Preencha o seu endereço de correio eletrônico.");
		document.form.email.focus();
		return false;
	}
	
	if ((document.form.email.value.indexOf("@")==-1) || (document.form.email.value.indexOf(".")==-1)){
  		alert("E-mail inválido.");
		document.form.email.focus();
		return false;
	}

return true;
}

function validaContato() {
	if (document.form.nome.value=="") {
	alert("Preencha o seu nome.");
	document.form.nome.focus();
	return false;
	}
	
	if (document.form.email.value=="") {
	alert("Preencha o seu email.");
	document.form.email.focus();
	return false;
	}
	
	if (document.form.msg.value=="") {
	alert("Preencha a sua mensagem.");
	document.form.msg.focus();
	return false;
	}
	
	return true;
}

function validaImagem(){
	if (document.frmImagePick.ImgUrl.value==""){
		alert("Selecione uma foto.");
		document.frmImagePick.imgFile.focus();
		return false;
	}
	if (document.frmImagePick.AltText.value==""){
		alert("Preencha o texto alternativo.");
		document.frmImagePick.AltText.focus();
		return false;
	}
return true;
}

function visualizarImagemPub(){
	if (document.form.imgId.value=="" || document.form.imgId.value==null) {
		alert ("Não há imagens carregadas.");
		return;
		}
	id=document.form.imgId.value;
	window2=window.open('imgwindow.php?file_id=' + id.toString(),'window2','scrollbars=no, dependent=yes,alwaysRaised=yes');
	window2.document.title='Visualização de imagem';
	}
	
function visualizarImagem(id){
	window2=window.open('imgwindow.php?file_id=' + id.toString(),'window2','scrollbars=no, dependent=yes,alwaysRaised=yes');
	window2.document.title='Visualização de imagem';
	}
	
function selecionarImagem(id){
	if (confirm("Selecionar esta imagem?")) {
		if (opener!=null){
			window.opener.document.form.imgId.value=id;
			imageChanged(window.opener);
			window.close();
			}
		}
	}
	
function selecionarImagemEditor(id, alt){
	if (confirm("Selecionar esta imagem?")) {
		if (opener!=null){
			window.opener.document.getElementById('txtUrl').value="/_proc/downloadimage.php?fileId="+id;
			window.opener.document.getElementById('txtAlt').value=alt;
			window.opener.UpdatePreview();
			window.close();
			}
		}
	}
	
function refreshMala(){
	document.form.selMalaDireta.value="";
	for (i=0;i<document.form.malaDireta.length;i++){
		if (document.form.malaDireta[i].checked) 
		document.form.selMalaDireta.value=document.form.selMalaDireta.value+document.form.malaDireta[i].value+";";
		}
	document.form.selMalaDireta.value=document.form.selMalaDireta.value.substring(0,document.form.selMalaDireta.value.length-1);
	}

function refreshTema(){
	document.form.temas.value="";
	for (i=0;i<document.form.slctTema.length;i++){
		if (document.form.slctTema[i].checked) 
		document.form.temas.value=document.form.temas.value+document.form.slctTema[i].value+";";
		}
	document.form.temas.value=document.form.temas.value.substring(0,document.form.temas.value.length-1);
	}

function visualizarDocumento(id){
	window2=window.open('downloadfile.php?fileId=' + id.toString(),'window2','scrollbars=no, dependent=yes,alwaysRaised=yes');
	window2.document.title='Visualização de documento';
	}

function visualizarDocumentoPub(id){
	window2=window.open('downloadfile.php?fileId=' + id.toString(),'window2','scrollbars=no, dependent=yes,alwaysRaised=yes');
	window2.document.title='Visualização de documento';
	}


function selecionarDocumento(id){
	if (confirm("Selecionar este documento?")) {
		if (opener!=null){
			window.opener.document.form.fileId.value=id;
			addDocument(window.opener);
			window.close();
			} 
		}
	}
	
function addDocument(obj) {
  obj.document.form.action='adicionarDocumento.php';
  obj.document.form.submit();
}

function selecionarDocumentoEditor(id){
	if (confirm("Selecionar este documento?")) {
		if (opener!=null){
			window.opener.document.getElementById('txtUrl').value="/_proc/downloadfile.php?fileId="+id;
			window.close();
			}
		}
	}
	
function imageChanged(obj){
	if (obj.document.form.imgId.value!="") {
		obj.document.form.btnCarregarImagem.value="Trocar imagem...";
		} else {
		obj.document.form.btnCarregarImagem.value="Carregar imagem...";
		}
	}
	
function documentChanged(pub_id,file_id){
   alert('change');
}

function editaMateria(){
	MM_openBrWindow('/editor/default.php','cntEdit','width=650,height=550');
	}
	
function mostraMateria(obj){
	if (obj.value!=""){
	  window2=window.open('','window2','width=650,height=550,scrollbars=yes,status=yes');
	  x=obj.value;
	  window2.document.write(x);
	  window2.document.title='Visualização de documento';
	  } else alert("Grave o conteúdo para poder fazer a previsão do texto.");
	}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function validaData(obj){
  if (obj.value==""){
    return;
	}
  else {
    var dia="";
	var mes="";
	var ano="";
	var flag=true;
	
	dia=obj.value.slice(0,2);
	mes=obj.value.slice(3,5);
	ano=obj.value.slice(6,10);
	
	if (obj.length<10){
	  flag=false;
	  }
	if (parseInt(dia,10)>31 || parseInt(dia,10)<1) {
      flag=false;
      }
	if (parseInt(mes,10)<1 ||parseInt(mes,10)>12) {
	  flag=false;
	  } 
	if (ano.length<4) {
	  flag=false;
	  } 
	}
  if (!flag) {
    alert("Data inválida");
    obj.focus();
    }
  }

function fmtData(obj) {
  var l=obj.value.length;
  if (isNaN(String.fromCharCode(event.keyCode))) {
	  event.keyCode=0;
    }
  if (obj.value.length>9){
	  event.keyCode=0;
    }	
  if (l==2) {
    obj.value=obj.value+"/";
	}
  if (l==5) {
    obj.value=obj.value+"/";
    }
  }
  
function soNumero(obj){
	  if (isNaN(String.fromCharCode(event.keyCode))) {
	  event.keyCode=0;
      }
	}
	
function fmtCEP(obj) {
  var l=obj.value.length;
  if (isNaN(String.fromCharCode(event.keyCode))) {
	  event.keyCode=0;
    }
  if (obj.value.length>10){
	  event.keyCode=0;
    }	
  if (l==2) {
    obj.value=obj.value+".";
	}
  if (l==6) {
    obj.value=obj.value+"-";
    }
  }
  
  
function fmtMoney(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	len = fld.value.length;
	for(i = 0; i < len; i++)
		if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++)
		if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
		aux2 = '';
		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		fld.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
			fld.value += aux2.charAt(i);
		fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}
  
function validaCEP(obj){
	if (obj.value.length<10 & obj.value!="") {
		alert ("CEP inválido");
		obj.focus();
	    }
    }

function filtraPubs(x) {
	document.form.letra.value=x;
	document.form.submit();
}

function addToCart(x) {
	switch (x) {
		case 'adicionar':
		document.form.action='/_proc/adicionarPedido.php';
		break;
	
		case 'recalcular':
		document.form.action='/_proc/recalcularPedido.php';
		break;
	
		case 'fechar':
		document.form.action='/_proc/fecharPedido.php';
		break;
	}
	document.form.submit();
}

function editaPeriodoCambio(){
	if (document.form.periodo.value!="") {
		x='form_periodo_cambio.php?id='+document.form.periodo.value;	
		MM_openBrWindow(x,'editaperiodo','width=540,height=160,dependent=yes,alwaysRaised=yes')
	}
}

function excluirPeriodoCambio() {
	if (document.form.periodo.value!="") {
		if (confirm("Tem certeza que quer excluir este período?")){
			location.href("exclui_periodo_cambio.php?id="+document.form.periodo.value);
		}
	}
}

function carregaPeriodoCambio() {
		if (document.form.periodo.value!="") {
			location.href("gerencia_cambio.php?id="+document.form.periodo.value);
		}
}

function validaPeriodo() {
	//Verifica se os campos estão preenchidos
	if (document.form.periodo_inicio.value==""){
		alert("Digite a data inicial do período.");
		document.form.periodo_inicio.focus();
		return false;
	}
	if (document.form.periodo_fim.value==""){
		alert("Digite a data final do período.");
		document.form.periodo_fim.focus();
		return false;
	}	
	i=document.form.periodo_inicio.value;
	f=document.form.periodo_fim.value
	if (Date.UTC(i.substr(6,4),i.substr(3,2),i.substr(0,2))>=Date.UTC(f.substr(6,4),f.substr(3,2),f.substr(0,2))){
		alert("Período inválido.");
		document.form.periodo_fim.value="";
		document.form.periodo_fim.focus();
		return false;
	}
    return true;
}

function resetPesquisa() {
	document.form.data.value="";
	document.form.keyword.value="";
	document.form.tema.value="";
	document.form.submit();
}

function resetPesquisaClip() {
	document.form.data.value="";
	document.form.keyword.value="";
	document.form.tema.value="";
	document.form.fonte.value="";
	document.form.submit();
}

function openLink(s,id){
	opener.location="/news/"+s+"?id="+id;
}

function gotoPage(pagina) {
	document.form.page.value=pagina;
	document.form.submit();
}

function avancaPagina() {
	if (isNaN(document.form.page.value)) {
	    document.form.page.value=2;
	} else {
		if (parseInt(document.form.page.value)<parseInt(document.form.pagecount.value)){
			document.form.page.value=parseInt(document.form.page.value)+1;
		}
	}
  document.form.submit();
}

function voltaPagina() {
	if (isNaN(document.form.page.value)) {
	    document.form.page.value=1;
	} else {
		if (parseInt(document.form.page.value)>1){
			document.form.page.value=parseInt(document.form.page.value)-1;
		}
	}
	document.form.submit();
}