//início validação de formulário

function existe(nome_do_campo,f) {  
	if(f=="") { f = document.formulario; }
	for (i=0; i<f.elements.length; i++) {    //faz um loop por todos os campos do formulário
		if (f.elements.item(i).name == nome_do_campo) {  //verifica se o nome do campo é o solicitado
			return true;
		} 
	}
	return false;
}

function tipoarquivo (campo,f,ext) {
	//campo é o nome do campo. f é o nome do formulário e extensão é a(s) extensões exigidas, separadas por vírgula
	if(f==null) { f = eval(document.formulario); }
	if(existe(campo,f)) {
		obj = eval("document."+f.name+"."+campo);
		valor = obj.value;
		
		if(obj.type == "file") {
			if(obj.title != "") { nome = obj.title; } else { nome = campo; }
			
			//verifica se o nome do arquivo tem menos de 3 caracteres ou é vazio
			if (valor==""||valor.length<3) {
				alert("Você deve escolher um arquivo para enviar no campo "+nome+".");
				obj.style.backgroundColor = "#F9F7E3";
				obj.focus();
				return false;
			}
			//verifica se a extensão do arquivo é uma das que são requisito
			pedacos = valor.split(".");
			extensao_do_arquivo = pedacos[pedacos.length-1]
			extensao_requisito = ext.split(",");
			extensao = false;
			for(i=0;i<extensao_requisito.length;i++) {
				if(extensao_requisito[i] == extensao_do_arquivo) {
					extensao = true;
				}
			}
			if(extensao == false) {
				alert("Não é permitido enviar este tipo de arquivo no campo "+nome+".");
				obj.style.backgroundColor = "#F9F7E3";
				obj.focus();
				return false;
			} else {
				return true;
			}
		}
	}
}

function preenchido(campo,f) {
	if(f==null) { f = eval(document.formulario); }
	if(existe(campo,f)) {
		obj = eval("document."+f.name+"."+campo);
		valor = obj.value;
		if(obj.type == "text" || obj.type == "textarea" || obj.type == "password") {
			if (valor==""||valor=="Escreva sua mensagem"||valor=="Seu nome"||valor=="Seu email"||valor=="Pesquisar") {
				if(obj.title != "") { nome = obj.title; } else { nome = campo; }
				alert("O campo "+nome+" deve ser preenchido.");
				obj.style.backgroundColor = "#F9F7E3";
				obj.focus();
				return false;
			} 
		}
		if(obj.type == "file") {
			if(obj.title != "") { nome = obj.title; } else { nome = campo; }
			if (valor==""||valor.length<3) {
				alert("Você deve escolher um arquivo para enviar no campo "+nome+".");
				obj.style.backgroundColor = "#F9F7E3";
				obj.focus();
				return false;
			}
			alert(obj.value);
		}
		if(obj.length > 0) {
			if(obj[0].type == "radio" || obj[0].type == "checkbox") {
				//faz um loop por todos os filhos do elemento e verifica se está preenchido
				for (i=0; i<obj.length; i++) { if (obj.item(i).checked) { return true; } } 
			}
			if(obj.tagName == "SELECT") {				
				if(obj.options[obj.options.selectedIndex].value!="") { return true; }
			}
			if(obj[0].title != "") { nome = obj[0].title; } else { nome = campo; }
			alert("O campo "+nome+" deve ser preenchido.");
			for (i=0; i<obj.length; i++) { obj[i].style.backgroundColor = "#F9F7E3"; }  			
			obj[0].focus();
			return false;
		} else {
			if(obj.type == "radio" || obj.type == "checkbox") {
				if(obj.title != "") { nome = obj.title; } else { nome = campo; }
				if (obj.checked) { return true; } 
				alert("O campo "+nome+" deve ser preenchido.");
				obj.style.backgroundColor = "#F9F7E3";
				obj.focus();
				return false;
			}
		}
		return true;
	}
}

function validanumero(campo,f) {
	if(f==null) { f = document.formulario; }
	if(existe(campo,f)) {
		obj = eval("document."+f.name+"."+campo);
		valor = obj.value;
		str = "0123456789";	
		result = true;
		for (var i=0;i<valor.length;i++)
			if (str.indexOf(valor.substr(i,1))<0) {
				result = false;
				break;
			}
		if (result==false) {
			alert("Este campo só aceita números!");
			obj.value = "";
			obj.style.backgroundColor = "#F9F7E3";
			obj.focus();
			return false;
		} else {
			return true;
		}
	}
}

function validaemail(campo,f) {
	if(f==null) { f = document.formulario; }
	if(existe(campo,f)) {
		obj = eval("document."+f.name+"."+campo);
		valor = obj.value;
		if ( (valor.length<=5) || (valor.substr(valor.indexOf('@')).indexOf('.') < 3) ) {
			alert("Escreva corretamente seu email.");
			obj.style.backgroundColor = "#F9F7E3";
			obj.focus();
			return false;
		} else {
			return true;
		}
	}
}

// validações específicas 

function validar_busca(){
	if(preenchido('buscar',document.fm_busca)) {
		endereco = document.fm_busca.action+"&buscar="+document.fm_busca.buscar.value;
		document.location.href=endereco;
	}
}

function validar_indique(){
	if((preenchido('nome')) &&
	  (validaemail('email')) &&
	  (preenchido('nomeamigo')) &&
	  (validaemail('emailamigo'))) {
		document.formulario.submit();
	}
}

function pega_obj(nome) {
	if (document.layers) { nome = document.layers[nome]; } //nn
	else { if (document.all) { nome = document.all[nome]; } //ie6
	else { if (document.getElementById) { nome = document.getElementById(nome) } } } //ff
	return nome;
}

function validarCPF(campo,f){

	if(f==null) { f = document.formulario; }
	if(existe(campo,f)) {
		obj = eval("document."+f.name+"."+campo);
		var cpf = obj.value;
		
		msg = "CPF inválido.";
		
	
		var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
		if(!filtro.test(cpf)){
			return msgerro(obj,msg);
		}
		
		cpf = remove(cpf, ".");
		cpf = remove(cpf, "-");
		
		if(isNaN(cpf)) {
		}
		
		
		if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
			cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
			cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
			cpf == "88888888888" || cpf == "99999999999"){
		}
		
		soma = 0;
		for(i = 0; i < 9; i++)
			soma += parseInt(cpf.charAt(i)) * (10 - i);
		resto = 11 - (soma % 11);
		if(resto == 10 || resto == 11)
			resto = 0;
		if(resto != parseInt(cpf.charAt(9))){
		}
		soma = 0;
		for(i = 0; i < 10; i ++)
			soma += parseInt(cpf.charAt(i)) * (11 - i);
		resto = 11 - (soma % 11);
		if(resto == 10 || resto == 11)
			resto = 0;
		if(resto != parseInt(cpf.charAt(10))){
			return msgerro(obj,msg);
		}
		return true;
	}
}
	
function remove(str, sub) {
	i = str.indexOf(sub);
	r = "";
	if (i == -1) return str;
	r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
	return r;
}

function msgerro(obj,msg) {
	if(obj.title != "") { nome = obj.title; } else { nome = obj.name; }
	if(msg!="") { msg = msg+"\n"; }
	alert(msg+"O campo "+nome+" deve ser preenchido corretamente.");
	obj.style.backgroundColor = "#F9F7E3";
	obj.focus();
	return false;
}

function minimocaracteres(campo,minimo,f) {
	if(f==null) { f = eval(document.formulario); }
	if(existe(campo,f)) {
		obj = eval("document."+f.name+"."+campo);
		valor = obj.value;
		if(obj.type == "text" || obj.type == "textarea" || obj.type == "password") {
			if (valor.length < minimo) {
				if(obj.title != "") { nome = obj.title; } else { nome = campo; }
				alert("O campo "+nome+" deve ter no mínimo "+minimo+" caracteres.");
				if(obj.type == "password") {
					obj.value = "";
				}
				obj.style.backgroundColor = "#F9F7E3";
				obj.focus();
				return false;
			} 
		}
	}		
	return true;
}
