// colocar no evento onKeyUp passando o objeto como parametro
function FormataData(val)
{
   	var pass = val.value;
	var expr = /[0123456789]/;
		
	for(i=0; i<pass.length; i++){
		// charAt -> retorna o caractere posicionado no índice especificado
		var lchar = val.value.charAt(i);
		var nchar = val.value.charAt(i+1);
	
		if(i==0){
		   // search -> retorna um valor inteiro, indicando a posição do inicio da primeira
		   // ocorrência de expReg dentro de instStr. Se nenhuma ocorrencia for encontrada o método retornara -1
		   // instStr.search(expReg);
		   if ((lchar.search(expr) != 0) || (lchar>3)){
			  val.value = "";
		   }
		   
		}else if(i==1){
			   
			   if(lchar.search(expr) != 0){
				  // substring(indice1,indice2)
				  // indice1, indice2 -> será usado para delimitar a string
				  var tst1 = val.value.substring(0,(i));
				  val.value = tst1;				
 				  continue;			
			   }
			   
			   if ((nchar != '/') && (nchar != '')){
				 	var tst1 = val.value.substring(0, (i)+1);
				
					if(nchar.search(expr) != 0) 
						var tst2 = val.value.substring(i+2, pass.length);
					else
						var tst2 = val.value.substring(i+1, pass.length);
	
					val.value = tst1 + '/' + tst2;
			   }

		 }else if(i==4){
			
				if(lchar.search(expr) != 0){
					var tst1 = val.value.substring(0, (i));
					val.value = tst1;
					continue;			
				}
		
				if	((nchar != '/') && (nchar != '')){
					var tst1 = val.value.substring(0, (i)+1);

					if(nchar.search(expr) != 0) 
						var tst2 = val.value.substring(i+2, pass.length);
					else
						var tst2 = val.value.substring(i+1, pass.length);
	
					val.value = tst1 + '/' + tst2;
				}
   		  }
		
		  if(i>=6){
			  if(lchar.search(expr) != 0) {
					var tst1 = val.value.substring(0, (i));
					val.value = tst1;			
			  }
		  }
	 }
	
     if(pass.length>10)
		val.value = val.value.substring(0, 10);
	 	return true;
}//fim da função de formatação de data

//DESABILITAR CAMPOS

function desabilita(atual,valor,campo){
var ref_select = document.getElementById(atual);
var ref_input = document.getElementById(campo);
	if (ref_select.value == valor){
	ref_input.disabled = true;
	ref_input.value='';
	}else{
		ref_input.disabled = false;
		
	}
}

function habilita_campo(){
    document.getElementById("nome_creche").disabled = true;//desabilitando
	document.getElementById("turno1").disabled = true;//desabilitando
	document.getElementById("turno2").disabled = true;//desabilitando
	document.getElementById("turno3").disabled = true;//desabilitando
    document.getElementById("jatentou1").disabled = false;//desabilitando
	document.getElementById("jatentou2").disabled = false;//desabilitando
	document.getElementById("lista1").disabled = false;//desabilitando
	document.getElementById("lista2").disabled = false;//desabilitando
	document.getElementById("lista3").disabled = false;//desabilitando
	
	
}

function habilita_campo2(){
    document.getElementById("jatentou1").disabled = true;//desabilitando
	document.getElementById("jatentou2").disabled = true;//desabilitando
	document.getElementById("lista1").disabled = true;//desabilitando
	document.getElementById("lista2").disabled = true;//desabilitando
	document.getElementById("lista3").disabled = true;//desabilitando
	document.getElementById("nome_creche").disabled = false;//desabilitando
	document.getElementById("turno1").disabled = false;//desabilitando
	document.getElementById("turno2").disabled = false;//desabilitando
	document.getElementById("turno3").disabled = false;//desabilitando
	
}


//FIM DESABILITAR CAMPOS


// valida CPF 

function Apenas_Numeros(caracter)
{
  var nTecla = 0;
  if (document.all) {
          nTecla = caracter.keyCode;
  } else {
          nTecla = caracter.which;
  }
  if ((nTecla> 47 && nTecla <58)
  || nTecla == 8 || nTecla == 127
  || nTecla == 0 || nTecla == 9  // 0 == Tab
  || nTecla == 13) { // 13 == Enter
          return true;
  } else {
          return false;
  }
}
function validaCPF(cpf) 
 {
   erro = new String;
 
        if (cpf.value.length == 11)
        {       
                        cpf.value = cpf.value.replace('.', '');
                        cpf.value = cpf.value.replace('.', '');
                        cpf.value = cpf.value.replace('-', '');
 
                        var nonNumbers = /\D/;
        
                        if (nonNumbers.test(cpf.value)) 
                        {
                                        erro = "A verificacao de CPF suporta apenas números!"; 
                        }
                        else
                        {
                                        if (cpf.value == "00000000000" || 
                                                        cpf.value == "11111111111" || 
                                                        cpf.value == "22222222222" || 
                                                        cpf.value == "33333333333" || 
                                                        cpf.value == "44444444444" || 
                                                        cpf.value == "55555555555" || 
                                                        cpf.value == "66666666666" || 
                                                        cpf.value == "77777777777" || 
                                                        cpf.value == "88888888888" || 
                                                        cpf.value == "99999999999") {
                                                        
                                                        erro = "Número de CPF inválido!"
                                        }
        
                                        var a = [];
                                        var b = new Number;
                                        var c = 11;
 
                                        for (i=0; i<11; i++){
                                                        a[i] = cpf.value.charAt(i);
                                                        if (i < 9) b += (a[i] * --c);
                                        }
        
                                        if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
                                        b = 0;
                                        c = 11;
        
                                        for (y=0; y<10; y++) b += (a[y] * c--); 
        
                                        if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
        
                                        if ((cpf.value.charAt(9) != a[9]) || (cpf.value.charAt(10) != a[10])) {
                                                erro = "Número de CPF inválido.";
                                                
                                        }
                        }
        }
        else
        {
                if(cpf.value.length == 0)
                        return false
                else
                        erro = "Número de CPF inválido.";
        }
        if (erro.length > 0) {
                        alert(erro);
                        cpf.focus();
                        return false;
        }       
        return true;    
 }
 
 //envento onkeyup
 function maskCPF(CPF) {
        var evt = window.event;
        kcode=evt.keyCode;
        if (kcode == 8) return;
        if (CPF.value.length == 3) { CPF.value = CPF.value + '.'; }
        if (CPF.value.length == 7) { CPF.value = CPF.value + '.'; }
        if (CPF.value.length == 11) { CPF.value = CPF.value + '-'; }
 }
 
 // evento onblur
 function formataCPF(CPF)
 {
        with (CPF)
        {
                value = value.substr(0, 3) + '.' + 
                                value.substr(3, 3) + '.' + 
                                value.substr(6, 3) + '-' +
                                value.substr(9, 2);
        }
 }
 function retiraFormatacao(CPF)
 {
        with (CPF)
        {
                value = value.replace (".","");
                value = value.replace (".","");
                value = value.replace ("-","");
                value = value.replace ("/","");
        }
 }
//-->

//------------------------------------------------------------------------
// Web Service CEP, desenvolvido por Evanil Rosano de Paula.
// Este Web Service está habilitado para funcionar em qualquer servidor, 
// no entanto terá melhor desempenho em sites hospedados pela Via Virtual.
// Visite nosso site e conheça nossos serviços.
// Via Virtual - Solucões WEB
// http://www.viavirtual.com.br
//-------------------------------------------------------------------------

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
        xmlhttp = false;
        }
      }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
      } catch (e) {
      xmlhttp = false;
      }
    }
  return xmlhttp;
  }
var http = getHTTPObject();

function funcaowebservicecep() 
{
	http.open("GET", 'buscarendereco2.php?cep='+document.getElementById("cep").value, true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);

	var arr; //array com os dados retornados
	function handleHttpResponse() 
	{
		if (http.readyState == 4) 
		{
			var response = http.responseText;
			eval("var arr = "+response); //cria objeto com o resultado
			document.getElementById("rua").value = arr.rua;
			document.getElementById("bairro").value = arr.bairro;
			document.getElementById("cidade").value = arr.cidade;
			document.getElementById("uf").value = arr.uf;
			document.getElementById("cep2").value = document.getElementById("cep").value
		}
	}
}

// função validar

function validar(){

 if (cadastro.nome.value==""){
  alert ("\nPor Favor digite Seu Nome."); 
  document.cadastro.nome.focus();
 return false; 
  }
  


  if (!confirm("\nTodos os dados foram preenchidos corretamente? ")){
    return false; 
  }
  	
}//fim da função validar