function somente_numero1(campo){
var digits="0123456789/";
var campo_temp;
for (var i=0;i<campo.value.length;i++){
  campo_temp=campo.value.substring(i,i+1); 
  if (digits.indexOf(campo_temp)==-1){
     campo.value = campo.value.substring(0,i);
     break;
   }
}

}
function somente_numero2(campo){
var digits="0123456789-()";
var campo_temp;
for (var i=0;i<campo.value.length;i++){
  campo_temp=campo.value.substring(i,i+1); 
  if (digits.indexOf(campo_temp)==-1){
     campo.value = campo.value.substring(0,i);
     break;
   }
}

}



function somente_numero(campo){
var digits="0123456789";
var campo_temp;
for (var i=0;i<campo.value.length;i++){
  campo_temp=campo.value.substring(i,i+1);
  if (digits.indexOf(campo_temp)==-1){
     campo.value = campo.value.substring(0,i);
     break;
   }
}

}
 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 mascaraData(data, e){
	somente_numero1(data);
    if(mascaraInteiro(data, e)==false){
        e.returnValue = false;
    }    
    return formataCampo(data, '00/00/0000', e);
}

function mascaraTelefone(telefone, e){
	somente_numero2(telefone);
    if(mascaraInteiro(telefone, e)==false){
        e.returnValue = false;
    }    
    return formataCampo(telefone, '(00)0000-0000', e);
}

//valida numero inteiro com mascara
function mascaraInteiro(data,e){
    if (e.keyCode < 48 || e.keyCode > 57){
        e.returnValue = false;
        return false;
    }
    return true;
}
//formata de forma generica os campos
function formataCampo(campo, Mascara, evento) {
    var boleanoMascara;
    
    var Digitato = evento.keyCode;
    exp = /\-|\.|\/|\(|\)| /g
    campoSoNumeros = campo.value.toString().replace( exp, "" );
  
    var posicaoCampo = 0;    
    var NovoValorCampo="";
    var TamanhoMascara = campoSoNumeros.length;;
    
    if (Digitato != 8) { // backspace
        for(i=0; i<= TamanhoMascara; i++) {
            boleanoMascara  = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".")
                                || (Mascara.charAt(i) == "/"))
            boleanoMascara  = boleanoMascara || ((Mascara.charAt(i) == "(")
                                || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " "))
            if (boleanoMascara) {
                NovoValorCampo += Mascara.charAt(i);
                  TamanhoMascara++;
            }else {
                NovoValorCampo += campoSoNumeros.charAt(posicaoCampo);
                posicaoCampo++;
              }           
          }    
        campo.value = NovoValorCampo;
          return true;
    }else {
        return true;
    }
}

function validaAdm(form){
	var formulario=document.forms["form"];
	if(formulario.nome.value=="" || formulario.nome.value==null){
		window.alert('Preencher o Nome');
		formulario.nome.focus();
		return false;
	}
	if(formulario.usuario.value=="" || formulario.usuario.value==null){
		window.alert('Preencher Usuário');
		formulario.usuario.focus();
		return false;
	}
	if(formulario.senha.value=="" || formulario.senha.value==null){
		window.alert('Preencher o Campo Senha');
		formulario.senha.focus();
		return false;
	}
	if(formulario.confirmasenha.value=="" || formulario.confirmasenha.value==null){
		window.alert('Preencher o Campo de Confirmação da Senha');
		formulario.confirmasenha.focus();
		return false;
	}
	if(!(formulario.senha.value==formulario.confirmasenha.value)){
		window.alert('Erro na Confimação!!! Tente Novamente');
		formulario.senha.focus();
		return false;
	}
	return true;	
}

function validaContato(form){
	if(document.forms["form"].nome.value==""){
		window.alert('Preencha o Nome');
		document.forms["form"].nome.focus();
		return false;
	}
	if(document.forms["form"].telefone.value==""){
		window.alert('Preencha o Telefone');
		document.forms["form"].telefone.focus();
		return false;
	}
	if(document.forms["form"].email.value==""){
		window.alert('Preencha o Email');
		document.forms["form"].email.focus();
		return false;
	}
	if(document.forms["form"].assunto.value==""){
		window.alert('Preencha o Assunto');
		document.forms["form"].assunto.focus();
		return false;
	}	
	if(document.forms["form"].departamento.value=="null"){
		window.alert('Escolha um Departamento');
		document.forms["form"].departamento.focus();
		return false;
	}
	if (document.forms["form"].mensagem.value==""){
		window.alert('Preencha a Mensagem');
		document.forms["form"].mensagem.focus();
		return false;
	}
	return true;
}

function excluir(){
	return window.confirm('Deseja Excluir este Registro ?');
}

function validaConcurso(form){
	if(document.forms["form"].nome.value==""){
		window.alert('Preencha o Nome');
		document.forms["form"].nome.focus();
		return false;
	}
	if (document.forms["form"].descricao.value==""){
		window.alert('Preencha a Descrição');
		document.forms["form"].descricao.focus();
		return false;
	}
	return true;
}

function libera_concurso(){
	if(document.getElementById("tipo").value == "on-line"){
		//libera os campos de agencia e banco
		document.getElementById("valor").disabled=false;
		document.getElementById("encerramento").disabled=false;
		document.getElementById("agencia").disabled=false;
		document.getElementById("conta").disabled=false;
		document.getElementById("contrato").disabled=false;
		document.getElementById("convenio").disabled=false;			
	}else{
		//bloqueia os campos de agencia e banco			
		document.getElementById("valor").disabled=true;
		document.getElementById("encerramento").disabled=true;
		document.getElementById("agencia").disabled=true;
		document.getElementById("conta").disabled=true;
		document.getElementById("contrato").disabled=true;
		document.getElementById("convenio").disabled=true;
	}
	return true;
}

function validaAlteraConcurso(form){
	if(document.forms["form"].cliente.value==""){
		window.alert('Um cliente deve ser selecionado!');
		document.forms["form"].cliente.focus();
		return false;
	}
	if(document.forms["form"].nome.value==""){
		window.alert('Preencha o Nome');
		document.forms["form"].nome.focus();
		return false;
	}
	if (document.forms["form"].descricao.value==""){
		window.alert('Preencha a Descrição');
		document.forms["form"].descricao.focus();
		return false;
	}
	if (document.forms["form"].situacao.value=="null"){
		window.alert('Escolha a Situação');
		document.forms["form"].situacao.focus();
		return false;
	}
	if(document.forms["form"].situacao.value=="realizados"){
		if(!(window.confirm('Ao finalizar o Concurso seus arquivos são deletados, deseja Finaliza-lo?'))){
			return false;
		}
	}
	return true;
}

function validaInscricao(form){

	if (document.getElementById("nome").value == ""){
		alert("O campo Nome deve ser preenchido!");
		document.getElementById("nome").focus();
		return false;
	}

	if (document.getElementById("sobrenome").value == ""){
		alert("O campo Sobrenome deve ser preenchido!");
		document.getElementById("sobrenome").focus();
		return false;
	}

	if (document.getElementById("telefone").value == ""){
		alert("O campo Telefone deve ser preenchido!");
		document.getElementById("telefone").focus();
		return false;
	}
	
	if (document.getElementById("data_nasc").value == ""){
		alert("O campo Data de Nascimento deve ser preenchido!");
		document.getElementById("data_nasc").focus();
		return false;
	}
	
	if (document.getElementById("e_mail").value == ""){
		alert("O campo E-mail deve ser preenchido!");
		document.getElementById("e_mail").focus();
		return false;
	}else{//validar email(verificao de endereco eletrônico)
			 parte1 = document.getElementById("e_mail").value.indexOf("@");
			 parte2 = document.getElementById("e_mail").value.indexOf(".");
			 parte3 = document.getElementById("e_mail").value.length;
			 if (!(parte1 >= 3 && parte2 >= 6 && parte3 >= 9)) {
					   alert ("O campo e-mail deve ser conter um endereco eletronico!");
					   document.getElementById("e_mail").focus();
					   return false;
			 }
		}
	
	if (document.getElementById("rua").value == ""){
		alert("O campo Rua deve ser preenchido!");
		document.getElementById("rua").focus();
		return false;
	}
	
	if (document.getElementById("cep").value == ""){
		alert("O campo Cep deve ser preenchido!");
		document.getElementById("cep").focus();
		return false;
	}
	
	if (document.getElementById("numero").value == ""){
		alert("O campo Número deve ser preenchido!");
		document.getElementById("numero").focus();
		return false;
	}
	
	if (document.getElementById("bairro").value == ""){
		alert("O campo Bairro deve ser preenchido!");
		document.getElementById("bairro").focus();
		return false;
	}
	
	if (document.getElementById("municipio").value == ""){
		alert("O campo Município deve ser preenchido!");
		document.getElementById("municipio").focus();
		return false;
	}
	
	if (document.getElementById("estado").value == ""){
		alert("O campo Estado deve ser preenchido!");
		document.getElementById("estado").focus();
		return false;
	}
	
	if (document.getElementById("n_identidade").value == ""){
		alert("O campo Número de Identidade deve ser preenchido!");
		document.getElementById("n_identidade").focus();
		return false;
	}
	
	if (document.getElementById("cpf").value == ""){
		alert("O campo Cpf deve ser preenchido!");
		document.getElementById("cpf").focus();
		return false;
	}else{//se nao estiver vazio
		   var cpf = document.getElementById("cpf").value;
		   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
		   cpf = remove(cpf, ".");
		   cpf = remove(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"){
			  window.alert("Preenchimento do CPF incorreto");
			  document.getElementById("cpf").focus();
			  return false;
		   }
		
		   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))){
			 window.alert("CPF inválido. Tente novamente.");
			 document.getElementById("cpf").focus();
			 return false;
		   }
		   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))){
			 window.alert("CPF inválido. Tente novamente.");
			 document.getElementById("cpf").focus();
			 return false;
		   }
		  
		}
	
	return true;
}

function validaArquivo(form){
	if(document.getElementById("cliente").value == ""){
		alert("Um cliente deve ser selecionado!");
		document.getElementById("cliente").focus();
		return false;
	}
	if (document.getElementById("nome").value == ""){
		alert("O campo Nome deve ser preenchido!");
		document.getElementById("nome").focus();
		return false;
	}
	if (document.getElementById("descricao").value == ""){
		alert("O campo Descrição deve ser preenchido!");
		document.getElementById("descricao").focus();
		return false;
	}
	if (document.getElementById("tipo").value == ""){
        alert("Selecione o tipo de inscrição!");
		document.getElementById("tipo").focus();
        return false;
	}
	//se for tipo on-line
	if(document.getElementById("tipo").value != 'manual'){
		if (document.getElementById("valor").value == ""){
        	alert("O campo Valor da inscrição deve ser preenchido!");
		document.getElementById("valor").focus();
        	return false;
		}
		if (document.getElementById("agencia").value == ""){
		alert("O campo Agência deve ser preenchido!");
		document.getElementById("agencia").focus();
		return false;
		}
		if (document.getElementById("conta").value == ""){
		alert("O campo Número da conta deve ser preenchido!");
		document.getElementById("conta").focus();
		return false;
		}
		if (document.getElementById("contrato").value == ""){
		alert("O campo Número do contrato deve ser preenchido!");
		document.getElementById("contrato").focus();
		return false;
		}
		if (document.getElementById("convenio").value == ""){
		alert("O campo Número do convênio deve ser preenchido!");
		document.getElementById("convenio").focus();
		return false;
		}
		if (document.getElementById("encerramento").value == ""){
        	alert("O campo Término das inscrições deve ser preenchido!");
		document.getElementById("encerramento").focus();
        	return false;
		}else{
			if (document.getElementById("encerramento").value.length < 10){
			alert("O campo Termino das incriçoes está incorreto!");
			document.getElementById("encerramento").focus();
			return false;
			}else{
				if (document.getElementById("encerramento").value.length > 10){
				alert("O campo Termino das incriçoes está incorreto!");
				document.getElementById("encerramento").focus();
				return false;
				}
			}
		}
	}
	//finaliza teste tipo
	return true;
}

function validaDownloads(form){
	if(document.forms["form"].nome.value==""){
		window.alert('Preencha o Nome');
		document.forms["form"].nome.focus();
		return false;
	}
	if (document.forms["form"].descricao.value==""){
		window.alert('Preencha a Descrição');
		document.forms["form"].descricao.focus();
		return false;
	}
	return true;
}

function validaCliente(form){
	if(document.forms["form"].nome.value==""){
		window.alert('Preencha o Nome');
		document.forms["form"].nome.focus();
		return false;
	}
	return true;
}

function validaNoticias(form){
	if(document.forms["form"].titulo.value==""){
		window.alert('Preencha o Título');
		document.forms["form"].titulo.focus();
		return false;
	}
	if (document.forms["form"].texto.value==""){
		window.alert('Preencha o Texto');
		document.forms["form"].texto.focus();
		return false;
	}
	return true;
}

function validaTrocarSenha(form){
	var formulario=document.forms["form"];
	if(formulario.senha.value=="" || formulario.senha.value==null){
		window.alert('Preencher o Campo Senha');
		formulario.senha.focus();
		return false;
	}
	if(formulario.confirmasenha.value=="" || formulario.confirmasenha.value==null){
		window.alert('Preencher o Campo de Confirmação da Senha');
		formulario.confirmasenha.focus();
		return false;
	}
	if(!(formulario.senha.value==formulario.confirmasenha.value)){
		window.alert('Erro na Confimação!!! Tente Novamente');
		formulario.senha.focus();
		return false;
	}
	return true;;

}

function validaTreinamento(form){
	var formulario=document.forms["form"];
	if(formulario.titulo.value==""){
		window.alert('Preencher o Título');
		formulario.titulo.focus();
		return false;
	}
	if(formulario.descricao.value==""){
		window.alert('Preencher a Descrição');
		formulario.descricao.focus();
		return false;
	}
	if(formulario.cargahoraria.value==""){
		window.alert('Preencher a Carga Horária');
		formulario.cargahoraria.focus();
		return false;
	}
	return true;
}
