// JavaScript Document

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", 'includes/buscarendereco.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("end").value = arr.rua;
			document.getElementById("bairro").value = arr.bairro;
			document.getElementById("cidade").value = arr.cidade;
			document.getElementById("uf").value = arr.uf;
			document.form.numero.focus();
		}
	}
}

/*
Validar CPF CNPJ
*/
function validaCNPJ() {
                 erro = new String;
                 var CNPJ = document.getElementById('cnpj_cpf').value;
				 if (CNPJ.length != 14) erro += "É necessario preencher corretamente o número do CNPJ! \n\n";
                 /*if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
                 if (erro.length == 0) erro += "É necessário preencher corretamente o número do CNPJ! \n\n";
                 }
                 //substituir os caracteres que não são números
               if(document.layers && parseInt(navigator.appVersion) == 4){
                       x = CNPJ.substring(0,2);
                       x += CNPJ. substring (3,6);
                       x += CNPJ. substring (7,10);
                       x += CNPJ. substring (11,15);
                       x += CNPJ. substring (16,18);
                       CNPJ = x;
              } else {
                       CNPJ = CNPJ. replace (".","");
                       CNPJ = CNPJ. replace (".","");
                       CNPJ = CNPJ. replace ("-","");
                       CNPJ = CNPJ. replace ("/","");
               } */
               var nonNumbers = /\D/;
               if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! \n\n";
               var a = [];
               var b = new Number;
               var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
               for (i=0; i<12; i++){
                       a[i] = CNPJ.charAt(i);
                       b += a[i] * c[i+1];
 }
               if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
               b = 0;
               for (y=0; y<13; y++) {
                       b += (a[y] * c[y]);
               }
               if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
               if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
                       erro +="Digito verificador do CNPJ com problema!";
               }
               if (erro.length > 0){
                       alert(erro);
					   document.getElementById('id').value='';
					   return false;
               } else {
                       //alert("CNPJ valido!");
               }
               return true;
       }
	   
	   
	   
	   
         function validaCPF() {
                 erro = new String;
				 var cpf = document.getElementById('cnpj_cpf').value;
                 if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n";
                 var nonNumbers = /\D/;
                 if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n";
                 if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
                         erro += "Numero de CPF invalido!"
               }
               var a = [];
               var b = new Number;
               var c = 11;
               for (i=0; i<11; i++){
                       a[i] = cpf.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.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
                       erro +="Digito verificador do CPF com problema!";
					   document.getElementById('id').value='';
               }
               if (erro.length > 0){
                       alert(erro);
					   return false;
               }
			   	else {
					//alert("CPF valido!");
				}
               return true;
       }
	   
	   
	   function valida_cnpjcpf() {
		   var CNPJ = document.getElementById('cnpj_cpf').value;
		   if(CNPJ.length > 10 && CNPJ.length < 15) {
		   		if(CNPJ.length == 14) {
		   			validaCNPJ();
					if (erro.length > 0){
                       CNPJ="";
					   return false;
               		}
		   		}
				
				if(CNPJ.length == 11) {
					var cpf = CNPJ;
					document.getElementById('cnpj_cpf').value = CNPJ
					validaCPF();
					if (erro.length > 0){
					   CNPJ="";
					   return false;
               		}
		   		}
		   		
		   }else{
			   alert("Erro!!! Digitar o CNPJ ou o CPF (apenas os numeros).\n Ex.:\n CNPJ: 02557183000176\nou\n CPF: 12345678910");
			   document.getElementById('cnpj_cpf').value='';
               return false;
		   }
	   return true;
	   }
	   
	   function validacpf() {
			cpf = document.formcliente.cpf.value;
			if(cpf.length == 11) {
				validaCPF(); 
				if (erro.length > 0){
					   document.formcliente.cpf.value="";
					   return false;
					   document.formcliente.cpf.focus(true);
               		}
		   	}
			else {
			   alert("Erro!!! Digitar o CPF (apenas os numeros).\n Ex.:\n CPF: 28007361863");
               return false;
	   		}
	   return true;
	   }
	   

function muda_foto(id,ivar){
	
	if(ivar==0){
	document.getElementById('img_g').src='../produtos/'+id+'.jpg';
	}else{
	document.getElementById('img_g').src='../produtos/'+id+'_'+ivar+'.jpg';
	}

}

function exibir_produtos(){
document.getElementById('prod').style.display='block';	
}
