// JavaScript Document
function verificaCadastro(){
	var nome = document.getElementById('nome');
	var email = document.getElementById('email');
	var telefone = document.getElementById('telefone');
	var cpf = document.getElementById('cpf');
	
	if (!nome.value){
		alert('O campo nome não pode ser nulo!')
	    nome.focus();
		return false;
	} 	
	if (!email.value){
		alert('O campo email não pode ser nulo!')
	    email.focus();
		return false;
	}
	else{
		if (!valida_email(email.value)){
			alert('Email inválido!');
	    	email.focus();
			email.value = "";
			return false;	
		}	
	}
	if (!telefone.value){
		alert('O campo telefone não pode ser nulo!');
	    telefone.focus();
		return false;
	}
	
	if (!cpf.value){
		alert('O campo CPF não pode ser nulo!');
	    cpf.focus();
		return false;
	}
	else{
		if (!valida_cpf(cpf.value)){
		alert('CPF inválido!');
	    cpf.focus();
		cpf.value = "";
		return false;
		}
	}
	style.display='none';
	return true;
	
}

function verificaContato(){
	var nome = document.getElementById('nome');
	var email = document.getElementById('email');
	var cpf = document.getElementById('cpf');
	
	if (!nome.value){
		alert('O campo nome não pode ser nulo!')
	    nome.focus();
		return false;
	} 	
	if (!email.value){
		alert('O campo email não pode ser nulo!')
	    email.focus();
		return false;
	}
	else{
		if (!valida_email(email.value)){
			alert('Email inválido!');
	    	email.focus();
			email.value = "";
			return false;	
		}	
	}
		
	if (!cpf.value){
		alert('O campo CPF não pode ser nulo!');
	    cpf.focus();
		return false;
	}
	else{
		if (!valida_cpf(cpf.value)){
		alert('CPF inválido!');
	    cpf.focus();
		cpf.value = "";
		return false;
		}
	}
	style.display='none';
	return true;
	
}

function valida_email(mail){
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ 
			return true; 
		}
    }else if(typeof(mail) == "object"){
              if(er.test(mail.value)){ 
                  return true; 
              }
    }else{
         	return false;
         }
}

function valida_cpf(cpf){
	var numeros, digitos, soma, i, resultado, digitos_iguais;
    digitos_iguais = 1;
    if (cpf.length < 11)
    	return false;
    for (i = 0; i < cpf.length - 1; i++)
       	if (cpf.charAt(i) != cpf.charAt(i + 1)){
           	digitos_iguais = 0;
            break;
        }
    	if (!digitos_iguais){
           	numeros = cpf.substring(0,9);
          	digitos = cpf.substring(9);
           	soma = 0;
           	for (i = 10; i > 1; i--)
               	soma += numeros.charAt(10 - i) * i;
           	
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
           	if (resultado != digitos.charAt(0))
            	return false;
           	
			numeros = cpf.substring(0,10);
           	soma = 0;
           	
			for (i = 11; i > 1; i--)
               	soma += numeros.charAt(11 - i) * i;
   
   			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1))
            	return false;
            
		return true;
        }
      	else
			return false;
}

function numeros(){   
       
    if (document.all) // Internet Explorer   
            var tecla = event.keyCode;   
    else if(document.layers) // Nestcape   
            var tecla = e.which;   
  
    if ((tecla > 47 && tecla < 58)) // numeros de 0 a 9   
        return true;   
    else {   
        if (tecla != 8) // backspace   
            //event.keyCode = 0;   
            return false;   
        else   
            return true;   
    }   
}  

function validaLogin(){
	var usuario = document.getElementById('usuario');
	var senha = document.getElementById('senha');
	
	if (!usuario.value){
		alert('O campo login não pode ser nulo!')
	    usuario.focus();
		return false;
	}
	if (!senha.value){
		alert('O campo senha não pode ser nulo!')
	    senha.focus();
		return false;
	}
	
	style.display='none';
	return true;
	
}

function verificaData(){
	var dataIda   = document.getElementById('dataIda');
	var dataVolta = document.getElementById('dataVolta');

	if (!dataIda.value){
		alert('O campo data de ida não pode ser nulo!')
	    dataIda.focus();
		return false;
	}
	else{
		if(!formatoData(dataIda)){
			return false;
			}
		
	}
	style.display='none';
	return true;
}

function formatoData(campo) { 
	var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;    
	var msgErro = 'Formato inválido de data.';    
	if (!(campo.value.match(expReg)) && (campo.value!='')) {        
		alert(msgErro);
		campo.focus();
		return false;		
	} 
	
	return true;
}

function verificaMural(){
	var email = document.getElementById('email');
	var destino = document.getElementById('destino1');
	
	if (!email.value){
		alert('O campo email não pode ser nulo!')
	    email.focus();
		return false;
	}
	else{
		if (!valida_email(email.value)){
			alert('Email inválido!');
	    	email.focus();
			email.value = "";
			return false;	
		}	
	}
	if (!destino.value){
		alert('O campo destino não pode ser nulo!');
	    destino.focus();
		return false;
	}
	

	style.display='none';
	return true;
	
}

function fotomsn(){
	alert('AVISO.\nA imagem pode ter um tamanho máximo de 300Kb.\nO envio de uma imagem maior, ocasionará a perda dos dados digitados!');	
}

function limpa1(){
	var foto = document.getElementById('foto1');
	foto.value = '';
}

function limpa2(){
	var foto = document.getElementById('foto2');
	foto.value = '';
}

function verificaPromocoes(){
	var email = document.getElementById('email');
	var mensagem = document.getElementById('mensagem');
	if (!email.value){
		alert('O campo email não pode ser nulo!')
	    email.focus();
		return false;
	}
	else{
		if (!valida_email(email.value)){
			alert('Email inválido!');
	    	email.focus();
			email.value = "";
			return false;	
		}	
	}
	if (!mensagem.value){
		alert('O campo complemento não pode ser nulo!')
	    mensagem.focus();
		return false;
	}
	style.display='none';
	return true;	
}

function verificaOrcamento(){
	var email = document.getElementById('email');
	var cid_origem = document.getElementById('cid_origem');
	var cid_destino = document.getElementById('cid_destino');
	var data_saida  = document.getElementById('data_saida');	 	
	if (!email.value){
		alert('O campo email não pode ser nulo!')
	    email.focus();
		return false;
	}
	else{
		if (!valida_email(email.value)){
			alert('Email inválido!');
	    	email.focus();
			email.value = "";
			return false;	
		}	
	}	
	if (!cid_origem.value){
		alert('O campo cidade de origem não pode ser nulo!')
	    cid_origem.focus();
		return false;
	}
	if (!cid_destino.value){
		alert('O campo cidade de destino não pode ser nulo!');
	    cid_destino.focus();
		return false;
	}
    if (!data_saida.value){
		alert('O campo data de saída não pode ser nulo!');
	    data_saida.focus();
		return false;
	}
	else{
	 	  if(!verificaDataOrc(data_saida)){
		  		return false;		   
		  }
		  else{
		   		return true;
		  }
	 }
	style.display='none';
	return true;
	
}
function verificaDataOrc(){
	var data_saida   = document.getElementById('data_saida');

	if (!data_saida.value){
		alert('O campo data de saida não pode ser nulo!')
	    data_saida.focus();
		return false;
	}
	else{
		if(!formatoData(data_saida)){
			return false;
			}
		
	}
	style.display='none';
	return true;
}
