/* Funções genéricas */
	
	//retorna true se for numérico
	function isNum(t){
		var code;
		
		if(t.which)
			code = t.which;
		else
			code = t.keyCode;
		
		if(code > 47 && code < 58)
			return true;
		else
			return false;
	}
	
	//Ajusta campo para data 12/34/5678
	function SetDate(campo, evt){
		if(!isNum(evt)){
			return false;
		}
		
		if(campo.maxLength != 10)
			campo.maxLength = 10;
			
		var texto = campo.value;
					
		if(texto.length == 2)
			campo.value = texto + '/';
		
		if(texto.length == 5)
			campo.value = texto + '/';		
	}
	
	//ajusta hora
	function SetHour(campo, evt){
		if(!isNum(evt)){
			return false;
		}
		
		if(campo.maxLength != 5)
			campo.maxLength = 5;
			
		var texto = campo.value;
					
		if(texto.length == 2)
			campo.value = texto + ':';
	}
	
	//decrementa do total o numero de letras do campo e coloca em mostra
	function RemainDigits(campo, mostra, total){
		var atual  = campo.value.length;
		var remain = total - atual;
		
		campo.maxLength = total;
		
		if(remain > 0){
			document.getElementById(mostra).innerText = '+' + remain
		}
		else if(remain == 0){
			document.getElementById(mostra).innerText = remain;
		}
	}
	
	
	function setCep(campo, evt){
		if(!isNum(evt))
			return false;
		
		var texto = campo.value;
		
		if(campo.maxLength != 9)
			campo.maxLength = 9;
			
		if(texto.length == 5)
			campo.value = texto + '-';
	}
	
	function setCPF(campo){
		var x = campo.value;
					
		if(x.length == 3)
			campo.value = x+'.';
	
		if(x.length == 7)
			campo.value = x+'.';
		
		if(x.length == 11)
			campo.value = x+'-';			
	}
	
	
	
	/* NEVER TUCH THIS PART OF CODE */
	
	//Vamos para a minha implementaçao AJAX!!!!
	
	function loadCity(idest, destObj) {
      //verifica se o browser tem suporte a ajax
	  try {
         ajax = new ActiveXObject("Microsoft.XMLHTTP");
      } 
      catch(e) {
         try {
            ajax = new ActiveXObject("Msxml2.XMLHTTP");
         }
	     catch(ex) {
            try {
               ajax = new XMLHttpRequest();
            }
	        catch(exc) {
               alert("Esse browser não tem recursos para uso do Ajax");
               ajax = null;
            }
         }
      }
	  //se tiver suporte ajax
	  if(ajax) {
	     //deixa apenas o elemento 1 no option, os outros são excluídos
		 destObj.options.length = 1;
	     
		 ajax.open("POST", "http://www.anclivepabrasil.com.br/sincaa/reg/teste.php", true);
		 ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		 
		 ajax.onreadystatechange = function() {
            //enquanto estiver processando...emite a msg de carregando
			
			if(ajax.readyState == 1) {
			   destObj.disabled = true;   
	        }
			//após ser processado - chama função processXML que vai varrer os dados
            if(ajax.readyState == 4 ) {
			   if(ajax.responseXML) {
			      processXML(ajax.responseXML, destObj);
				  destObj.disabled = false;
			   }
			   else {
			       //caso não seja um arquivo XML emite a mensagem abaixo
				   //idOpcao.innerHTML = "--Primeiro selecione o estado--";
				   var a = 1;
			   }
            }
         }
		 //passa o código do estado escolhido
         ajax.send("idest="+idest);
      }
   }
   
   function processXML(obj, destSelect){
      //pega a tag cidade
      var dataArray   = obj.getElementsByTagName("cidade");
      
	  	  
	  //total de elementos contidos na tag cidade
      for(var i = 0 ; i < dataArray.length ; i++) {
         var val = dataArray[i];
		//contéudo dos campos no arquivo XML
		var codigo    =  val.getElementsByTagName("codigo")[0].firstChild.nodeValue;
		var descricao =  val.getElementsByTagName("descricao")[0].firstChild.nodeValue;
		
		//cria um novo option dinamicamente  
		var novo = document.createElement("option");
	    //atribui um ID a esse elemento
	    //novo.setAttribute("id", "opcoes");
		//atribui um valor
	    novo.value = codigo;
		//atribui um texto
	    novo.text  = descricao;
		//finalmente adiciona o novo elemento
		destSelect.options.add(novo);
	  }  
   }
	
	
	
	
