function Mascara(tipo, campo, teclaPress) {
	if (window.event)
	{
		var tecla = teclaPress.keyCode;
	} else {
		tecla = teclaPress.which;
	}
 
	var s = new String(campo.value);
	// Remove todos os caracteres à seguir: ( ) / - . e espaço, para tratar a string denovo.
	s = s.replace(/(\.|\(|\)|\/|\-| )+/g,'');
 
	tam = s.length + 1;
 
	if ( tecla != 9 && tecla != 8 ) {
		switch (tipo)
		{
		case 'CPF' :
			if (tam > 3 && tam < 7)
				campo.value = s.substr(0,3) + '.' + s.substr(3, tam);
			if (tam >= 7 && tam < 10)
				campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,tam-6);
			if (tam >= 10 && tam < 12)
				campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,3) + '-' + s.substr(9,tam-9);
		break;
 		
		


		case 'CNPJ' :
 
			if (tam > 2 && tam < 6)
				campo.value = s.substr(0,2) + '.' + s.substr(2, tam);
			if (tam >= 6 && tam < 9)
				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,tam-5);
			if (tam >= 9 && tam < 13)
				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,tam-8);
			if (tam >= 13 && tam < 15)
				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,4)+ '-' + s.substr(12,tam-12);
		break;
 
		case 'TEL' :
			if (tam > 2 && tam < 4)
				campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,tam);
			if (tam >= 7 && tam < 11)
				campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,4) + '-' + s.substr(6,tam-6);
		break;
 
		case 'DATA' :
			if (tam > 2 && tam < 4)
				campo.value = s.substr(0,2) + '/' + s.substr(2, tam);
			if (tam > 4 && tam < 11)
				campo.value = s.substr(0,2) + '/' + s.substr(2,2) + '/' + s.substr(4,tam-4);
		break;
		case 'CEP' :                       
		if (tam > 5 && tam < 7)                               
		campo.value = s.substr(0,5) + '-' + s.substr(5, tam);               
		break;
		}
	}
}
function chkCpf (campo,valor) {

 

 strcpf = valor;

 str_aux = "";

 

 for (i = 0; i <= strcpf.length - 1; i++)

   if ((strcpf.charAt(i)).match(/\d/)) 

     str_aux += strcpf.charAt(i);

   else if (!(strcpf.charAt(i)).match(/[\.\-]/)) {

     alert ("O campo CPF apresenta caracteres inválidos !!!");

     campo.focus();

     return false;

   }

 

 if (str_aux.length != 11) {

   alert ("O campo CPF deve conter 11 dígitos !!!");

   campo.focus();

   return false;

 }

 

 soma1 = soma2 = 0;

 for (i = 0; i <= 8; i++) {

   soma1 += str_aux.charAt(i) * (10-i);

   soma2 += str_aux.charAt(i) * (11-i);

 }

 d1 = ((soma1 * 10) % 11) % 10;

 d2 = (((soma2 + (d1 * 2)) * 10) % 11) % 10;

 if ((d1 != str_aux.charAt(9)) || (d2 != str_aux.charAt(10))) {

   alert ("O CPF digitado é inválido !!!");

   campo.focus();

   return false;

 }



 return true;

}



// Verifica se o CNPJ é válido

function chkCnpj(s)

{

var i;

 //s = limpa_string(numero);

 var c = s.substr(0,12);

 var dv = s.substr(12,2);

 var d1 = 0;



 for (i = 0; i < 12; i++)

 {

  d1 += c.charAt(11-i)*(2+(i % 8));

 }

 

if (d1 == 0) return false;



       d1 = 11 - (d1 % 11);



if (d1 > 9) d1 = 0;



if (dv.charAt(0) != d1)

{

 return false;

}



 d1 *= 2;



 for (i = 0; i < 12; i++)

{

 d1 += c.charAt(11-i)*(2+((i+1) % 8));

}



d1 = 11 - (d1 % 11);



if (d1 > 9) d1 = 0;



if (dv.charAt(1) != d1)

{

 return false;

}

       return true;

}
function Ajax(){
this.assincr = true;
this.method = "GET";
this.val = "";
this.xmlhttp = null;

try{
this.xmlhttp =  new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
try{
this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(ex){
try{
this.xmlhttp = new XMLHttpRequest();
}catch(exc){
alert("Esse browser não tem recursos para uso do Ajax");
this.xmlhttp = null;
}
}
}

//carrega o conteudo de uma ajax em uma var
this.loadResult = function(url){
if(this.xmlhttp) {
this.xmlhttp.open(this.method, url , this.assincr);
//
if(this.method == 'GET'){
this.xmlhttp.send(null);
}else if(this.method == 'POST'){
this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
try{
this.xmlhttp.send(url.split("?")[1]);
}catch(e){}
}
//
if(this.assincr){
this.xmlhttp.onreadystatechange = function(){
if(ajax.xmlhttp.readyState == 4){
if(ajax.xmlhttp.status == 200){
ajax.val = ajax.xmlhttp.responseText;
}else{
alert(ajax.xmlhttp.statusText);
}
}
}
}else{
ajax.val = ajax.xmlhttp.responseText;
}
}
return this.val;
}

}

// AJAX Script
function openAjax() { 
var Ajax; 
try {Ajax = new XMLHttpRequest(); 
}catch(ee) { 
try {Ajax = new ActiveXObject("Msxml2.XMLHTTP"); 
}catch(e) { 
try {Ajax = new ActiveXObject("Microsoft.XMLHTTP"); 
}catch(e) {Ajax = false; 
} 
} 
} 
return Ajax; 
} 

function carregaAjax(id,url) { 
if(document.getElementById) { 
var exibeResultado = document.getElementById(id); 
var Ajax = openAjax(); 
Ajax.open("GET", url, true); 
Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
Ajax.onreadystatechange = function() 
{ 
if(Ajax.readyState == 1) { 
exibeResultado.innerHTML = "<div align=center><strong><font color=white size=1 face=arial> Carregando...</font></strong></div>"; 
} 
if(Ajax.readyState == 4) { 
if(Ajax.status == 200) { 
var resultado = Ajax.responseText; 
resultado = resultado.replace(/\+/g," "); 
resultado = unescape(resultado); 
exibeResultado.innerHTML = resultado; 
} else { 
exibeResultado.innerHTML = "<div align=center><font color=white size=1>Erro: .</font></div>"; 
} 
} 
} 
Ajax.send(null); 
} 
} 

//----------------------------------------------
/*Busca CEP */
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(){
	
	document.getElementById("loading").style.display='';
	http.open("GET", 'cep.asp?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("uf").value = arr.uf;
			document.getElementById("cidade").value = arr.cidade;
			document.getElementById("bairro").value = arr.bairro;
			document.getElementById("endereco").value =arr.logra+' '+arr.rua;
			document.getElementById("loading").style.display='none';
			document.getElementById("numero").focus();

		}
	}
	
}

function somenteNumero(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;
       }
    }
}