//<!--
function valida_preenchido(campo, aviso)
{
	if (eval("document." + nome_formulario + "." + campo + ".value==''"))
		avisar_erro_focar(campo, aviso)
	else	return(true)
}

function valida_selecionado(campo, aviso)
{
	if (eval("document." + nome_formulario + "." + campo + ".selectedIndex==0"))
		avisar_erro_focar(campo, aviso)
	else	return(true)
}

function avisar_erro_focar(campo, aviso)
{
	alert(aviso)
	eval("document." + nome_formulario + "." + campo + ".focus();")
	return(false)
}

function avisar_erro_focar_radio(campo, aviso)
{
	alert(aviso)
	eval("document." + nome_formulario + "." + campo + "[0].focus();")
	return(false)
}

function valida_tamanho_min(campo, tamanho_adequado, aviso)
{
	if (eval("document." + nome_formulario + "." + campo + ".value.length < " + tamanho_adequado))
		avisar_erro_focar(campo, aviso)
	else 	return(true)
}

function valida_tamanho_exato(campo, tamanho_adequado, aviso)
{
	if (eval("document." + nome_formulario + "." + campo + ".value.length != " + tamanho_adequado))
		avisar_erro_focar(campo, aviso)
	else 	return(true)
}

	
// abaixo encontram-se cada uma das funcoes responsaveis pela validacao dos formularios
var numero_limpo;
function limpa_carac_especiais(x)
{
	var transferencia;
	transferencia = '';
	for (i=0; i<=x.length; i++)
	{
		if (x.charAt(i)!='.' && x.charAt(i)!='/' && x.charAt(i)!='-')	transferencia = transferencia +''+ x.charAt(i)
	}
		numero_limpo='';
		numero_limpo = transferencia;
	}

function valida_cnpj(campo, aviso)
{
	temp = eval("document." + nome_formulario + "." + campo + ".value");
	
	limpa_carac_especiais(temp);
	cnpj = numero_limpo;
	
	if (isNumberString(cnpj) != 1)		avisar_erro_focar(campo, aviso)

	udigito = parseInt(cnpj.charAt(13))
	pdigito = parseInt(cnpj.charAt(12))
	mult = 5
	somat = 0
	i = 0
	for (i = 0; i <= 11; i++)
	{
		vnum = parseInt(cnpj.substring (i,i+1))
		vnum = vnum * mult
		somat = somat + vnum
		mult = mult - 1
		if (mult < 2)	mult = 9
	}
	resto = parseInt(somat / 11)
	resto = somat - (resto * 11)
	resto = 11 - resto
	if (resto == 10 || resto == 11)
	{
		resto = 0
	}
	if (resto == pdigito)
	{
		mult = 6
		somat = 0
		for (i = 0; i <= 12; i++)
		{
			vnum = parseInt(cnpj.substring (i,i+1))
			vnum = vnum * mult
			somat = somat + vnum
			mult = mult - 1
				if (mult < 2)
				{
					mult = 9
				}
		}
		resto = parseInt(somat / 11)
		resto = somat - (resto * 11)
		resto = 11 - resto
		if (resto == 10 || resto == 11)
		{
			resto = 0
		}
		if (resto != udigito)		avisar_erro_focar(campo, aviso)
		else 	return(true);
	}
	else		avisar_erro_focar(campo, aviso)
}

function valida_cpf (campo, aviso)
{
	var soma, resto, i;
	var temp, cpf

	temp = eval("document." + nome_formulario + "." + campo + ".value")

	limpa_carac_especiais(temp);
	cpf = numero_limpo

	if (isNumberString(cpf) != 1)
	{
		window.alert(aviso)
		eval("document." + nome_formulario + "." + campo + ".focus();")
		return(false)
	}

	//valida argumento
	if (cpf.length != 11) 
	{
		alert(aviso);
		eval("document." + nome_formulario + "." + campo + ".focus();")
		return(false);
	}

	soma = 0
	for (i = 0; i<=8; i++)
	{
		soma = soma + parseInt(cpf.charAt(i)) * (10 - i)
	}

	resto = 11 - (soma - (parseInt(soma / 11) * 11))
	if (resto == 10 || resto == 11 ) resto = 0;
	if (resto != parseInt(cpf.charAt(9)) )
	{
		alert(aviso);
		eval("document." + nome_formulario + "." + campo + ".focus();")
		return(false);
	}

	soma = 0
	for (i = 0; i<=9; i++)
	{
		soma = soma + parseInt(cpf.charAt(i)) * (11 - i)
	}
	resto = 11 - (soma - (parseInt(soma / 11) * 11))
	if (resto == 10 || resto == 11 ) resto = 0;
	if (resto != parseInt(cpf.charAt(10)) )
	{
		alert(aviso);
		eval("document." + nome_formulario + "." + campo + ".focus();")
		return(false);
	}
	
	var controle, contador;
	contador=1;
	controle = cpf.charAt(0)
	for (i=1; i<=10; i++)
	{
		if(controle==cpf.charAt(i)) contador++;
		if (contador==11)
		{
		alert(aviso);
		eval("document." + nome_formulario + "." + campo + ".focus();")
		return(false);
		}
	}
	return(true);
}

function valida_email(email)
{
	var checkStr = email;
	var ix = (checkStr.length - 4)
	var x = AtSignValid = DoublePeriod = PeriodValid = SpaceValid = ExtValid = 0;

	for (i = 0; i < checkStr.length; i++) 
	{ 
		if (checkStr.charAt(i) == '@')	AtSignValid++; 
		else 
			if (checkStr.charAt(i) == '.') 
			{
				if (x == (i-1) && i > 1) 
					DoublePeriod++; 
				else 
				{ 
					x = i;
					PeriodValid++; 
				} 
			} 
		else 
		if (checkStr.charAt(i) == ' ' || checkStr.charAt(i) == ';' || checkStr.charAt(i) == ':' || checkStr.charAt(i) == ',' || checkStr.charAt(i) == '/')
		SpaceValid ++; 
	}// fim do for

	//if (checkStr.indexOf(".com", ix) > -1) ExtValid++;
	//else if (checkStr.indexOf(".edu", ix) > -1) ExtValid++;
	//else if (checkStr.indexOf(".net", ix) > -1) ExtValid++;
	//else if (checkStr.indexOf(".org", ix) > -1) ExtValid++;
	//else if (checkStr.indexOf(".gov", ix) > -1) ExtValid++;
	//else if (checkStr.indexOf(".br", ix) > -1) ExtValid++;
	if (checkStr.indexOf(".", ix) > -1) ExtValid++;
	

	if (AtSignValid != 1) return(false);
	if (PeriodValid == 0) return(false);
	if (SpaceValid != 0) return(false);
	if (DoublePeriod != 0) return(false);
	if (ExtValid == 0) return(false);
	if (checkStr.length > 120) return(false);

	return(true);
	}

function valida_data(data)
{
	var dia = parseInt(data.substring(0,2),10);
	var mes = parseInt(data.substring(3,5),10);
	var ano = parseInt(data.substring(6,10),10);

	if (dia <= 31 && mes <=12 && ano >= 1000)
	{
		if (data.substring(0,1)=='0' && data.substring(1,2) != '0' ||	data.substring(0,1)!='0')
		{
			if (data.substring(2,3)=="/")
			{
				if (data.substring(3,4)=='0' && data.substring(4,5)!='0' ||data.substring(3,4)!='0')
				{
					if (data.substring(5,6)=="/")
					{
						if (data.substring(6,7)== '0' || data.substring(6,7)=='' && data.substring(7,8)!='0')
						{
							window.alert('O ano que você digitou não existe!');
							return false;
							if (mes == 2)
							{
								if ((dia > 0 ) && (dia <= 29))
								{
									if (dia == 29)
									{
										if ((ano % 4) == 0)	return true;
										else
										{
											window.alert('Este dia não existe, certifique - se de que digitou corretamente!');
											return false; 
										}
									}
								}
								else
								{
									window.alert('Este dia não existe, certifique - se de que digitou corretamente!');
									return false; 
								}
							}// fim de if mes == 2 
							if ((mes == 4)||(mes == 6)||(mes == 9)||(mes == 11))
							{
								if ((dia > 0 ) && (dia <= 30))	return true;
								else
								{
									window.alert('Este dia não existe, certifique - se de que digitou corretamente!');
									return false;
								}
							}

							if ((mes == 1)||(mes == 3)||(mes == 5)||(mes ==7)||(mes == 8)||(mes == 10)||(mes == 12))
							{
								if ((dia > 0) && (dia <= 31)) return true;
								else
								{
									window.alert('Este dia não existe, certifique -se de que digitou corretamente!');
									return false;
								}
							}
						}
					}
					else
					{
						window.alert('A data foi digitada fora do padrão (dd/mm/aaaa) !');
						return false;
					}
				}
				else
				{
					window.alert('Você digitou um mês que não existe!');
					return false;
				}
			}
			else
			{
				window.alert('A data foi digitada fora do padrão(dd/mm/aaaa) !');
				return false;
			}
		}
		else
		{
			window.alert('Você digitou um dia que não existe!');
			return false;
		}
	}
	else
	{
		window.alert('O dia e/ou o mês que você digitou não existe, ou Você digitou fora do padrão (dd/mm/aaaa) !');
		return false;
	}
	return true;
}



	//// funções auxiliares
	function isNumberString (InString) 
	{
		if(InString.length==0) return (false);
		var RefString="1234567890";
		for (Count=0; Count < InString.length; Count++)  
		{
			TempChar= InString.substring (Count, Count+1);
			if (RefString.indexOf (TempChar, 0)==-1)  
				return (false);
		}
			 return (true);
} //fim da function validar
//-->
