//verificar vazio
function vazio( obj ){
	if( obj.length <= 0 ){
		return true
	}else{
		return false
	}
}

//verificar qde de caracteres
function tamanho( obj, tam ){
	if( obj.length < tam ){
		return true
	}else{
		return false
	}
}

//verificar numero
function numero( obj ){
	if( obj*1 != obj ){
		return true
	}else{
		return false
	}
}

//verificar e-mail
function vEmail( obj ){
	if(vazio(obj) == true ){
		return "Preencha o campo e-mail"
	}else if( tamanho(obj, 6) == true ){
		return "Preencha o e-mail corretamente"
	}else if( obj.indexOf("@") == -1 ){
		return "Todo email tem um '@' Não seja BURRO! Nem tente ser esperto..."
	}else if( obj.indexOf("@") != obj.lastIndexOf( "@" ) ){
		return "Emails só tem um @. Acha que não sabemos disso?"
	}else if( obj.lastIndexOf(".") < obj.lastIndexOf( "@" )+2 ){
		return "O ponto tá no lugar errado..."
	}else{
		return true
	}
}

//mascara de telefone
function mascaraTel(){
	caixa = document.getElementById("telefone")
	
	if( caixa.value.length == 4 ){
		caixa.value += "-"
	}
}


//mascara de CEP
function mascaraCep(){
	caixa = document.getElementById("cep")
	
	if( caixa.value.length == 5 ){
		caixa.value += "-"
	}
}

//mascara de CPF()
function mascaraCPF(){
	cpfx = document.getElementById("cx")
	if(cpfx.value.length == 3){
		cpfx.value += "."
	}
	if(cpfx.value.length == 7){
		cpfx.value += "."
	}
	if(cpfx.value.length == 11){
		cpfx.value += "-"
	}
}

//Validar o formulário
function validar(){
	//window.alert( document.getElementById("frmContato").nome.value )
	//return false
	with(document.getElementById("frmContato")){
		if(vazio(nome.value) == true){
			window.alert( "Preencha o campo nome" )
			nome.focus()
			return false// Ele retorna false pois a ação de enviar o formulário (no documento html) é cancelada ao receber o valor boleano "false". Confira no código html que o form está com a opção "return validar()", pois o ato de validar depende desta informação que o script enviará ao formulário.
		}
		if( numero(nome.value) == false ){
			window.alert( "Digite o nome corretamente" )
			nome.focus()
			return false
		}
		if(vEmail(email.value) != true ){
			window.alert( vEmail(email.value) )
			email.focus()
			return false
		}
		if( tamanho(ddd.value, 2) == true ){
			window.alert("Preencha o DDD corretamente")
			ddd.focus()
		return false
		}
		if( tamanho(telefone.value, 9) == true ){
			window.alert("Preencha o Telefone corretamente")
			telefone.focus()
		return false
		}
		if( numero(telefone.value.replace("-", "")) == true ){
			window.alert("O campo telefone só deve conter números.")
			telefone.focus()
		return false
		}
		
		//Data-------------------------------------------------------------------
		if(dia.value == 0){
			window.alert("Selecione o dia")
			dia.focus()
		return false
		}
		if(mes.value == 0){
			window.alert("Selecione o mês")
			mes.focus()
		return false
		}
		if(ano.value == 0){
			window.alert("Selecione o ano")
			ano.focus()
		return false
		}
		
		//Endereço------------------------------------------------------------------
		if(end.value == 0){
			window.alert("Informe o seu endereço")
			end.focus()
		return false
		}
		if( tamanho(cep.value, 8) == true ){
			window.alert("Preencha o CEP")
			cep.focus()
		return false
		}
		if( numero(cep.value.replace("-", "")) == true ){
			window.alert("No campo CEP você só deve inserir apenas números.")
			cep.focus()
		return false
		}
		
		//Sexo----------------------------------------------------------------------
		if(sexo[0].checked == false && sexo[1].checked == false){
			window.alert("Cê num tem sexo não é? Hermafrodita invertido...")
			return false
		}
	//Solução se tiver varios checks (economizar código)-----------
	//	cont = 0
	//	for( ab = 0; ab <= sexo.length-1; ab++ ){
	//		if( sexo[ab].checked == true ){
	//			cont++
	//		}
	//	}
	
	}
	
}
