function valida_form(form,campos,nomecampos,tipos,status) {
	/*
	form = posição do formulario (0,1,...)
	campos = campos a verificar (0,1,2,...)
	tipos = tipo de cada campo:
		1-inteiro
		2-decimal
		3-data
		4-email
		5-cpf
		6-cnpj
		7-cep
		8-string
		9-login/senha
		10-confirmacao de senha, verificar se a string é valida e saber se o valor é identico ao informado no campo senha
	status=0-nao obrigatorio 1-obrigatorio
	*/

	var mensagem ="Os seguintes campos estão incorretos\n\n";
	var erro=false;

	for(var i=0;i<nomecampos.length;i++) {
		resultado=true;

		valor = document.forms[form].elements[campos[i]].value

		switch(tipos[i]) {
		case 1:
			resultado=valida_numero(valor,true);
			break;
		case 2:
			resultado=valida_numero(valor,false);
			break;
		case 3:
			resultado=valida_data(valor);
			break;
		case 4:
			resultado=valida_email(valor);
			break;
		case 5:
			resultado=valida_cpf(valor);
			break;
		case 6:
			resultado=valida_cnpj(valor);
			break;
		case 7:
			resultado=valida_cep(valor);
			break;
		case 8:
			resultado=(valor.length==0) ? false : true;
			break;
		case 9:
			resultado=valida_string(valor);
			break;

		}


		if(!resultado) {
			mensagem += "- " + nomecampos[i] + "\n";
			erro=true
		}
	}


	if(erro) 
		alert (mensagem);

	return !erro;
}

// valida numero inteiro e decimal
function valida_numero(numero,inteiro){

	numero = new String(numero);

	if(numero.length==0)
		return false;

	var exp = /^\$|\./g ;
	// retira $ e .
	numero = numero.replace(exp,"");

	exp = /,/g ;
	// troca , por .
	numero = numero.replace(exp,".");

	numero = parseFloat(numero);

	if(isNaN(numero))
		return false;

	if(inteiro && numero!=Math.round(numero))
		return false;

	return true;
}

// verifica se um ano é bissexto
function ano_bi(ano) {
	if (ano % 100 == 0){
		if (ano % 400 == 0)
			return true;
	}
	else
		if ((ano % 4) == 0)
			return true;

	return false;
}

// valida uma data
function valida_data(data){

	var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})\2(\d{4})$/ ;
	var datadiv = data.match(datePat);

	if (datadiv==null)
		return false;

	var dia = datadiv[1];
	var mes = datadiv[3];
	var ano = datadiv[4];

	if(dia<1 || dia>31 || mes<1 || mes>12 || ano < 2001)
		return false;

	if((mes==4 || mes==6 || mes==9 || mes==11) && dia>30)
		return false;

	if(mes==2)
		if(dia>29)
			return false;
		else
			if(dia==29 && !ano_bi(ano))
				return false;
	
	return true;
}

// funcao simples de validacao de e-mail
function valida_email(email) {
	var reEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;

	if(!reEmail.test(email))
	{
		return false;
	}
	else
	{
		return true;
	}
}


function valida_email_antigo(email) {
	var chars = "@#$&[]()/\\\{}!^:`\"";
	var pat = /^(.+)@(.+)$/;

	var emaildiv = email.match(pat);

	if(emaildiv==null)
		return false;

	var login = emaildiv[1];
	var dominio = emaildiv[2];

	for(var i=0;i<chars.length;i++) {
		if(login.indexOf(chars.substr(i,1))!=-1)
			return false;
	}

	for(var i=0;i<chars.length;i++) {
		if(dominio.indexOf(chars.substr(i,1))!=-1)
			return false;
	}

	return true;
}

// valida cpf
function valida_cpf(cpf) {
	var cpf = new String(cpf);
	var aux_cpf = "";

	if(cpf.length!=11)
		return false;

	for(j=0;j<cpf.length;j++)
		if(cpf.substr(j,1)>="0" && cpf.substr(j,1)<="9")
			aux_cpf += cpf.substr(j,1);

	if(aux_cpf.length!=11)
		return false;
	else
	{
		var cpf1 = String(aux_cpf);
		var cpf2 = cpf.substr(cpf.length-2,2);
		var controle = "";
		var start = 2;
		var end = 10;

		for(var i=1;i<=2;i++) {
			var soma = 0;
			for(j=start;j<=end;j++)
				soma += cpf1.substr((j-i-1),1)*(end+1+i-j);
			if(i==2)
				soma += digito * 2;
				digito = (soma * 10) % 11;

				if(digito==10)
					digito=0;
				controle += digito;
				start = 3;
				end = 11;
		}

		if(controle!=cpf2)
			return false;
	}

	return true;
	
}

// valida cnpj
function valida_cnpj(cnpj) {
	var erro = true;
	var aux_cnpj = "";
	var cnpj1=0,cnpj2=0;

	for(j=0;j<cnpj.length;j++)
		if(cnpj.substr(j,1)>="0" && cnpj.substr(j,1)<="9")
			aux_cnpj += cnpj.substr(j,1);

	if(aux_cnpj.length!=14)
		erro = false;
	else {
		cnpj1 = aux_cnpj.substr(0,12);
		cnpj2 = aux_cnpj.substr(aux_cnpj.length-2,2);
		fator = "543298765432";
		controle = "";
		for(j=0;j<2;j++) {
			soma = 0;
			for(i=0;i<12;i++)
				soma += cnpj1.substr(i,1) * fator.substr(i,1);
			if(j==1) soma += digito * 2;
			digito = (soma * 10) % 11;
			if(digito==10) digito = 0;
			controle += digito;
			fator = "654329876543";
		}
		if(controle!=cnpj2)
			erro = false;
	}

	return erro;

}

// valida cep
function valida_cep(cep) {
	if(cep.length>9 || (cep.indexOf("-")==-1 && cep.length>8))
		return false;
	var pat = /((\d{5})(-)(\d{3}))|(\d{8})/;
	var cepdiv = cep.match(pat);

	if(cepdiv==null)
		return false;

	return true;

}

// valida login e senha
function valida_string(string){
	str = new String(string);

	if(str.indexOf(" ")!=-1)
		return false;

	var chars = "@#$&[]()/\\\{}!^:'\"";

	for(var i=0;i<chars.length;i++) {
		if(str.indexOf(chars.substr(i,1))!=-1)
			return false;
	}

	return true;
}


function valida_enquete(nform) {
	//validacao de radio buttons sem saber quantos sao
	marcado = -1
	for (i=0; i<nform.resposta.length; i++) {
		if (nform.resposta[i].checked) {
			marcado = i
			vresposta = nform.resposta[i].value
		}
	}
	
	if (marcado == -1) {
		alert("Selecione uma resposta.");
		nform.resposta[0].focus();
		return false;
	} else {

		nform.button_votar.disabled=true;
		window.open('pop_enquete.php?id_resposta=' + vresposta,'RESULTADO','status=no,location=no,menubar=no,width=320,height=200')
		return true; 
	} 
}