// JavaScript Document

// Abre uma janela
function abreJanela(url,nomeJanela,largura,altura) {
  window.open(url,nomeJanela, "toolbar=no,location=no,status=no,menubar=no,resizable=no,scrollbars=no,width= "+largura+" ,height= "+altura+" ");
}

function abreFotos(idAlbum) {
var pagina = 'fotos/index.php?id=';
  window.open(pagina+idAlbum,'Fotos', "toolbar=no,location=no,status=no,menubar=no,resizable=no,scrollbars=no,width=750,height=510");
}

// Jump Menu
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

// Abre uma janela
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// Valida o formulário de cadastro
function valida_formulario_cadastro() {

if (document.cadastro.nome.value == "") {
alert ("Erro: O campo nome deve ser preenchido!");
document.cadastro.nome.focus();
return false;
}

if (document.cadastro.email.value == "") {
alert ("Erro: O campo e-mail deve ser preenchido!");
document.cadastro.email.focus();
return false;
}

if (document.cadastro.email.value != document.cadastro.confirmacao_email.value) {
alert ("Erro: Os e-mails informados devem ser iguais!");
document.cadastro.email.focus();
return false;
}

if (document.cadastro.data_nascimento.value == "") {
alert ("Erro: O campo data de nascimento deve ser preenchido!");
document.cadastro.data_nascimento.focus();
return false;
}

if (document.cadastro.senha.value == "") {
alert ("Erro: O campo senha deve ser preenchido!");
document.cadastro.senha.focus();
return false;
}

if (document.cadastro.senha.value.length < 6 || document.cadastro.senha.value.length > 8) {
alert ("Erro: O campo senha deve ser preenchido com no mínimo 6 caracteres e no máximo 8 caracteres");
document.cadastro.senha.focus();
return false;
}

if (document.cadastro.senha.value != document.cadastro.confirmacao_senha.value) {
alert ("Erro: As senhas informadas devem ser iguais!");
document.cadastro.senha.focus();
return false;
}


if (document.cadastro.endereco.value == "") {
alert ("Erro: O campo endereço deve ser preenchido!");
document.cadastro.endereco.focus();
return false;
}

if (document.cadastro.bairro.value == "") {
alert ("Erro: O campo bairro deve ser preenchido!");
document.cadastro.bairro.focus();
return false;
}

if (document.cadastro.cidade.value == "") {
alert ("Erro: O campo cidade deve ser preenchido!");
document.cadastro.cidade.focus();
return false;
}

if (document.cadastro.cep.value == "") {
alert ("Erro: O campo CEP deve ser preenchido!");
document.cadastro.cep.focus();
return false;
}

return true;

} // Function

// Função para ocultar a camada

function ocultaCamada(camada,link) {

document.getElementById(camada).style.display = "none"; 
if (link != "") {
document.getElementById(link).style.display = ""; 
}

} // Function

function mostraCamada(camada,link) {

document.getElementById(camada).style.display = ""; 

if (link != "") {
document.getElementById(link).style.display = "none"; 
}

} // Function

// Autocomplete
function setaPalavra(alvo,fonte){
     var atual;
     palavra = $.trim(fonte.val()).toLowerCase();
     if(palavra!=''){
         for(var i=0; i<alvo[0].options.length; i++){
             atual = alvo[0].options[i].text;
             if(atual.substr(0,palavra.length).toLowerCase()==palavra){
                 alvo[0].selectedIndex = i;
                 alvo.css('visibility', 'visible');
                 return true; break;
             }
         }
     }
     //nao achou nenhum
     alvo.css('visibility','hidden');
     alvo[0].selectedIndex = -1;
     return false;
}
function ativaAutocomplete(elementoPaiAutocomplete){
     $(elementoPaiAutocomplete).each(function(){
         var $select = $(this).children('select');
         var $input = $(this).children('input');
        
         //do select
         $select.css('visibility','hidden');
         $select.css('position','absolute');
         $select.css('font-size','11px');
         $select.css('top', $input[0].offsetHeight + "px");
         $select.css('left', $input[0].offsetLeft + "px");
        
         $input.attr('autocomplete','off');
        
         //do pai autocomplete
         if($(this).css('position')!='relative'){
             $(this).css('position','relative');
         }
        
         //ao perder o foco, deve voltar pra vazio
         $input.add($select).blur(function(){
             $(this).parent().children('select').css('visibility','hidden');
         })
        
         //fazendo o autocomplete ao liberar tecla
         $input.keyup(function(event){
             //alert(event.witch);
             var $select = $(this).next('select');
             if(!event.ctrlKey && !event.altKey){
                 if(event.keyCode==13){  //enter, mando pra cima
                     event.preventDefault();
                     $select.click();
                     return false;
                 }else if(event.keyCode==38){ //seta pra cima
                     if($select[0].selectedIndex>=0){
                         //$select[0].focus();
                         $select[0].selectedIndex = $select[0].selectedIndex - 1;
                     }
                     $select.css('visibility','visible');
                     event.preventDefault();
                     return false;
                 }else if(event.keyCode==40){ //seta pra baixo
                     if($select[0].selectedIndex<$select[0].options.length){
                         //$select[0].focus();
                         $select[0].selectedIndex = $select[0].selectedIndex + 1;
                     }
                     $select.css('visibility','visible');
                     event.preventDefault();
                     return false;
                 }else{ //seleciono a palavra
                     setaPalavra($select,$(this));
                 }
             }
         })
         //pra tratar o tab e mandar o foco pro próximo, pulando o primeiro select
         //que é do autocomplete
         $input.keypress(function(event){
             if(event.keyCode==13){
                 if($(this).parent().children('select').css('visibility')=='visible'){
                     event.preventDefault();
                     event.stopPropagation();
                     return false;
                 }
             }else if(event.keyCode==9){ //se for o tab, faço um tab falso pro proximo campo
                 event.preventDefault();
                 event.stopPropagation();
                 if(! $.browser.opera){
                     $(this).parent().next().children('input,select,a,button')[0].focus();
                     var $input = $(this).parent().children('input');
                     $(this)[0].selectionStart = $(this)[0].selectionEnd;
                 }
                 return false;
             }else if(event.keyCode==38 || event.keyCode==40){
                 event.preventDefault();
                 return false;
             }
         })

         //fazendo ao clicar no select
         $select.click(function(event){
             if(this.selectedIndex>=1){
                 var texto_select = this.options[this.selectedIndex].text;
                 var $input = $(this).parent().children('input');
                 $input.val(texto_select);
                 $input.focus();
                 $input[0].selectionStart = 0;
                 $input[0].selectionEnd = $input.val().length;
             }
             $(this).css('visibility','hidden');
         })
         $select.focus(function(event){
             var $input = $(this).parent().children('input');
             $input.focus();
         });
         $select.blur(function(event){
             var $select = $(this).parent().children('select');
             $select.click();
         });
         $input.blur(function(event){
             $select.css('visibility','hidden');
         });
        
        
     })//fim do autocomplete.each()

}

/* ################################## Abas ########################### */

function stAba(menu,conteudo) {
	this.menu = menu;
	this.conteudo = conteudo;
}

var arAbas = new Array();
arAbas[0] = new stAba('td_noticias','div_noticias');
arAbas[1] = new stAba('td_materias','div_materias');

function AlternarAbas(menu,conteudo) {
	for (i=0;i<arAbas.length;i++) {
		m = document.getElementById(arAbas[i].menu);
		m.className = 'Abasmenu';
		c = document.getElementById(arAbas[i].conteudo)
		c.style.display = 'none';
	}
	m = document.getElementById(menu)
	m.className = 'Abasmenu-sel';
	c = document.getElementById(conteudo)
	c.style.display = '';
}
	
/* ########################## Fim ################################ */