var novaJanela = function(){
	if(document.getElementsByTagName){
		var links = document.getElementsByTagName('a');
		for(var i=0; i<links.length; i++){
			if(links[i].className == 'blank'){
				links[i].target = "_blank";
				}
			}
		}
	}

/*
	Função que valida o cadastro
	de clientes do site
*/
function validaCadastro() {
	var DHTML = (document.getElementById || document.getElementsByTagName);
	if(!DHTML)
		return;
	var d = document.getElementById('formCadastro');
	var p = document.getElementById('msg');
	var a, erro, ok, validado, soma;
	if(!d || !p)
		return;
	d.onsubmit = function() {
		erro = '';
		ok = 0;
		p.innerHTML = '';
		for(var i = 0; (a = d.getElementsByTagName('input')[i]); i++) {		
			// campos simples
			if((a.name == 'nome' || a.name == 'profissao' || a.name == 'cep' || a.name == 'email' || a.name == 'endco' || a.name == 'cidade' || a.name == 'bairro' || a.name == 'uf' || a.name == 'n' || a.name == 'cpf') && (a.value == '' || a.value.length<2)) {
				erro += 'Preencha o campo '+a.name.toUpperCase()+' corretamente.<br />';
				a.style.border = '1px solid #f00';
			}
			
			// nascimento
			if(a.name == 'nascimento' && (a.value == '' || a.value.length < 8 || a.value.lastIndexOf('/') != 5)) {
				erro += 'Preencha o campo NASCIMENTO corretamente.<br />';
				a.style.border = '1px solid #f00';
			}
			
			// e-mail
			if(a.name == 'email' && a.value != '') {
				var parte1 = a.value.indexOf('@');
				var parte2 = a.value.lastIndexOf('.');
				var parte3 = a.length;
				if(parte1 < 3 || parte2 < 6 || parte3 < 9) {
					erro += 'Preencha o campo EMAIL corretamente.<br />';
					a.style.border = '1px solid #f00';
				}
			}
			
			// cpf
			if(a.name == 'cpf' && a.value != '') {
				validado = true;
				if (a.value.length != 14 || a.value == "00000000000" || a.value == "11111111111" || a.value == "22222222222" ||	a.value == "33333333333" || a.value == "44444444444" || a.value == "55555555555" || a.value == "66666666666" || a.value == "77777777777" || a.value == "88888888888" || a.value == "99999999999")
					validado = false;
				parte1 = a.value.indexOf('.');
				parte2 = a.value.lastIndexOf('.');
				parte3 = a.value.indexOf('-');
				if(parte1 != 3 || parte2 != 7 || parte3 != 11)
					validado = false;
				if(!validado) {
					erro += 'Preencha o campo CPF corretamente.<br />';
					a.style.border = '1px solid #f00';
				}
			}
		}
		if(erro != '') {
			p.innerHTML = erro;
			return false;
		} else {
			document.getElementById("btenviar").disabled = true;
			return true;
		}
	}
}

/*
	Função que altera os campos da busca
	entre o select e o input
*/
function alteraCampoBusca() {
	var DHTML = (document.getElementById || document.getElementsByTagName);
	if(!DHTML)
		return;
	
	var campo = document.getElementById('campo');
	var selectPaises = document.getElementById('pais');
	var input = document.getElementById('busca');
	if(!campo || !input || !selectPaises)
		return;
	campo.onchange = function() {
		if(this.value == 'pais') {
			selectPaises.style.display = 'block';
			input.style.display = 'none';
		} else {
			selectPaises.style.display = 'none';
			input.style.display = 'block';
		}
	}
}

/*
	Função que carrega o autocompletar
*/
function autoCompletar() {
	var DHTML = (document.getElementById || document.getElementsByTagName);
	if(!DHTML)
		return;
	var form, input, i, lista, campoBusca;
	form = document.getElementById('buscaForm');
	lista = document.getElementById('autoCompletar');
	if(!form || !lista)
		return;
	for(i = 0; (input = form.getElementsByTagName('input')[i]); i++) {
		if(input.getAttribute('title') && input.getAttribute('title').indexOf('Título') != -1) {
			input.onkeyup = function() {
				if(this.value != '') {
					campoBusca = document.getElementById('campo');
					if(campoBusca.value != 'titulo')
						return;
					lista.style.display = 'block';
					ajax('gerenciador/action/autocompletar.php?limit=1&titulo='+document.getElementById('busca').value, 'autoCompletar');
					selectAutoCompletar();
				} else {
					lista.style.display = 'none';
				}
			}
		}
	}
}

/*
	Função que seleciona o autocompletar
	e coloca o seu valor no input
*/
function selectAutoCompletar() {
	var DHTML = (document.getElementById || document.getElementsByTagName);
	if(!DHTML)
		return;
	var lista, itens, i, input;
	input = document.getElementById('busca');
	lista = document.getElementById('autoCompletar');
	if(!input || !lista)
		return;
	for(i = 0; (itens = lista.getElementsByTagName('li')[i]); i++) {
		itens.onmouseover = function() {
			this.style.backgroundColor = 'ddd';
		}
		itens.onmouseout = function() {
			this.style.backgroundColor = 'eee';
		}
	}
}

window.onload = function() {
	novaJanela();
	validaCadastro();
	alteraCampoBusca();
	autoCompletar();
}
