// JavaScript Document

// SLIDESHOW - INICIO

var tempo;
var intervalo = 6000; //Intervalo entra as mudanças de slides. Definido em milisegundos.

function $(campo) {
	return document.getElementById(campo);
}

var $A = Array.from = function(iterable) {
	if (!iterable) return [];
	if (iterable.toArray) {
		return iterable.toArray();
	} else {
		var results = [];
		for (var i = 0; i < iterable.length; i++)
			results.push(iterable[i]);
		return results;
	}
};

Function.prototype.bind = function() {
	var __method = this, args = $A(arguments), object = args.shift();
	return function() {
		return __method.apply(object, args.concat($A(arguments)));
	};
};

function addEvent(obj, evType, fn) { 
	if (typeof obj == "string") {
		if (null == (obj = document.getElementById(obj))) {
			throw new Error("Elemento HTML não encontrado. Não foi possível adicionar o evento.");
		}
	}
	if (obj.attachEvent) {
		return obj.attachEvent(("on" + evType), fn);
	} else if (obj.addEventListener) {
		return obj.addEventListener(evType, fn, true);
	} else {
		throw new Error("Seu browser não suporta adição de eventos.");
	}
}

function nextSlide() {
	var divs = $('slides').getElementsByTagName('div');
	var i;
	for (i = 0; i < divs.length; i++) {
		if (divs[i].className == 'visible') {
			divs[i].className = 'hidden';
			$('link' + (i + 1)).className = 'inativo'
			i = (i == divs.length - 1) ? 0 : i + 1;
			divs[i].className = 'visible';
			$('link' + (i + 1)).className = 'ativo' 
			break;
		}
	}
}

function init() {
	var as = $('links').getElementsByTagName('a');
	var i;
	for (i = 0; i < as.length; i++) {
		var f = function(v) {
			setSlide(as[v]);
		}
		addEvent(as[i], 'click', f.bind(this, i));
	}
	tempo = setInterval('nextSlide()', intervalo);
}

function setSlide(link) {
	var divs = $('slides').getElementsByTagName('div');
	var i;
	for (i = 0; i < divs.length; i++) {
		divs[i].className = 'hidden';
	}
	var lis = $('links').getElementsByTagName('div'); // alterei aqui de li para div
	for (i = 0; i < lis.length; i++) {
		lis[i].className = 'inativo';
	}
	link.parentNode.className = 'ativo';
	i = parseInt(link.firstChild.data);
	$('slide' + i).className = 'visible';
	clearInterval(tempo);
}

addEvent(window, 'load', init);

// SLIDESHOW - FIM

// script para abrir popup
function OpenWindow(FileNameToOpen,janela,largura,altura)
{
newWindow = window.open(FileNameToOpen,janela, 'width='+largura+', height='+altura+', toolbar=no, scrollbars=yes, location=no, left=100, top=100')
if (newWindow.open)
{
newWindow.focus()
}
}

// Início do código de Aumentar/ Diminuir a letra
 
// Para usar coloque o comando: "javascript:mudaTamanho('tag_ou_id_alvo', -1);" para diminuir
// e o comando "javascript:mudaTamanho('tag_ou_id_alvo', +1);" para aumentar
 
var tagAlvo = new Array('p'); //pega todas as tags p//
 
// Especificando os possíveis tamanhos de fontes, poderia ser: x-small, small...
var tamanhos = new Array( '9px','10px','11px','12px','13px','14px','15px' );
var tamanhoInicial = 2;
 
function mudaTamanho( idAlvo,acao ){
  if (!document.getElementById) return
  var selecionados = null,tamanho = tamanhoInicial,i,j,tagsAlvo;
  tamanho += acao;
  if ( tamanho < 0 ) tamanho = 0;
  if ( tamanho > 6 ) tamanho = 6;
  tamanhoInicial = tamanho;
  if ( !( selecionados = document.getElementById( idAlvo ) ) ) selecionados = document.getElementsByTagName( idAlvo )[ 0 ];
  
  selecionados.style.fontSize = tamanhos[ tamanho ];
  
  for ( i = 0; i < tagAlvo.length; i++ ){
    tagsAlvo = selecionados.getElementsByTagName( tagAlvo[ i ] );
    for ( j = 0; j < tagsAlvo.length; j++ ) tagsAlvo[ j ].style.fontSize = tamanhos[ tamanho ];
  }
}

// FORMULARIO PARA CADASTRO
function Formatar_Telefone(objeto,tammax,teclapres){

    var tecla = teclapres.keyCode;
    vr = objeto.value;
    vr = vr.replace( "(", "" );
    vr = vr.replace( ")", "" );
    vr = vr.replace( " ", "" );
    vr = vr.replace( "-", "" );
    tam = vr.length;

    if (tam < tammax && tecla != 8) {
            tam = vr.length + 1 ;
    }

    if (tecla == 8 ) {
            tam = tam - 1 ;
    }

    if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
            if ( tam <= 4 ) {
                     objeto.value = vr ;
            }
             if ( (tam > 4) && (tam <= 8) ) {
                     objeto.value = vr.substr(0,tam-4) + '-' + vr.substr( tam - 4, tam ) ;
            }
             if ( (tam >= 9) && (tam <= 10) ) {
                    objeto.value = '(' + vr.substr(0,2) + ') ' + vr.substr(2,tam-6) + '-' + vr.substr(tam-4,tam) ;
            }
    }
}

function Validar_Email(email){

    if(email.length < 6) {
            return false;
    }

    var x = 0;
    for (var c=0;c<email.length;c++) {
            if (email.substring(c,c+1) == '@') {
                    x = c;
            }
    }
    var y = 0;
    if (x > 0) {
            for (c=x;c<email.length;c++) {
                    if (email.substring(c,c+1)=='.') {
                            y = c;
                            var valida = 1;
                    }
            }
            if (y > 0) {
                    var dominio = '';
                    for (c=x;c<y;c++) {
                            dominio = dominio + email.substring(1,c);
                    }
            }
    }
    else {
            return false;
    }
    if (y <= x+2){
            return false;
    }
    if (valida == 1){
            return true;
    }
}

function Validar_EditCadastro(){
    
	if (document.formCad.nome.value=="") {
            document.formCad.nome.focus();
            alert("Digite seu nome!");
            return false;
    }	
	
    if (document.formCad.email.value=="") {
            document.formCad.email.focus();
            alert("Informe o seu e-mail!");
            return false;
    } else {
            if (!Validar_Email(document.formCad.email.value)) {
                    document.formCad.email.focus();
                    alert("Digite um e-mail válido!");
                    return false;
            }
    }	        
	
	if (document.formCad.endereco.value=="") {
            document.formCad.endereco.focus();
            alert("Digite o nome da sua rua!");
            return false;
    }	
	
	if (document.formCad.n.value=="") {
            document.formCad.n.focus();
            alert("Digite o número da sua casa!");
            return false;
    }	
	
	if (document.formCad.telefone.value=="") {
            document.formCad.telefone.focus();
            alert("Digite o número de telefone!");
            return false;
    }	
            
	if (document.formCad.bairro.value=="") {
            document.formCad.bairro.focus();
            alert("Digite o nome do seu bairro!");
            return false;
    }	
			
	if (document.formCad.cidade.value=="") {
            document.formCad.cidade.focus();
            alert("Digite o nome da sua cidade!");
            return false;
    }	        		          
        
    if (document.formCad.senha01.value!="" || document.formCad.senha02.value!="") {
        
		var senha01 = document.formCad.senha01.value;    
		var senha02 = document.formCad.senha02.value;
		
		if(senha01 != senha02) {
			document.formCad.senha01.focus();
            alert("As senhas não são iguais!");
            return false;
		}
    }                 
            
}	

function Validar_Cadastro(){
    
	if (document.formCad.nome.value=="") {
            document.formCad.nome.focus();
            alert("Digite seu nome!");
            return false;
    }	
	
    if (document.formCad.email.value=="") {
            document.formCad.email.focus();
            alert("Informe o seu e-mail!");
            return false;
    } else {
            if (!Validar_Email(document.formCad.email.value)) {
                    document.formCad.email.focus();
                    alert("Digite um e-mail válido!");
                    return false;
            }
    }	        
	
	if (document.formCad.endereco.value=="") {
            document.formCad.endereco.focus();
            alert("Digite o nome da sua rua!");
            return false;
    }	
	
	if (document.formCad.n.value=="") {
            document.formCad.n.focus();
            alert("Digite o número da sua casa!");
            return false;
    }	
	
	if (document.formCad.telefone.value=="") {
            document.formCad.telefone.focus();
            alert("Digite o número de telefone!");
            return false;
    }		
			
	if (document.formCad.cidade.value=="") {
            document.formCad.cidade.focus();
            alert("Digite o nome da sua cidade!");
            return false;
    }		        		    
    
	if (document.formCad.senha01.value=="") {
            document.formCad.senha01.focus();
            alert("Digite a sua senha!");
            return false;
    }        
        
    
	if (document.formCad.senha02.value=="") {
            document.formCad.senha02.focus();
            alert("Confirme a sua senha!");
            return false;
    }  

	if (document.formCad.senha01.value!="" && document.formCad.senha02.value!="") {
        
		var senha01 = document.formCad.senha01.value;    
		var senha02 = document.formCad.senha02.value;
		
		if(senha01 != senha02) {
			document.formCad.senha01.focus();
            alert("As senhas não são iguais!");
            return false;
		}
    }                 
            
}

//script que rediciona as caixas de informativos
function navegaBoxGeral(url){

	if(url != "") {
		
		OpenWindow("componentes/newsletter/displayer_newsletter.php?id=" + url + "", "visualizar", 600, 500);
		
	}
	
}