try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}

function doBusca(tuf){

    //limpa o select
    var c=document.getElementById("cidade")
    while(c.options.length>0)c.options[0]=null
    c.options[0]=new Option(" -- Aguarde ... -- "," -- Aguarde ... -- ")

    //Monta a url com a uf
    xmlhttp.open("GET", "ajax_cidades.php?uf="+tuf,true);

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            //limpa o select
            var c=document.getElementById("cidade")
            while(c.options.length>0)c.options[0]=null
            //Transforma a lista de cidades JSON em Javascript
            var aCidades= xmlhttp.responseText.split("|");
            
			//popula o select com a lista de cidades obtida
			c.options[c.options.length]=new Option('escolha a cidade',0)
			
            for(var i=0;i<aCidades.length;i++){
                aCidades[i]=unescape(aCidades[i])
                c.options[c.options.length]=new Option(aCidades[i],aCidades[i])
            }
        }
    }
    xmlhttp.send(null);
}







function validaCNPJ(e) {
              CNPJ = e.cnpj.value;
                erro = new String;
                if (CNPJ.length < 18) 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 = "É necessario 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 = "É necessario preencher corretamente o número do CNPJ! \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 ="É necessario preencher corretamente o número do CNPJ! \n\n";
               }
               if (erro.length > 0){
                       alert(erro);
                      return false;
              }
			  else return true;
}

	function checkCheckBox(f){
		if (f.agree.checked == false ){
			alert('Você deve aceitar com os termos para continuar');
			return false;
		}
		else	
			return true;
		}
		
	function validaform(e) {		
			if(checkCheckBox(e) && validaCNPJ(e) ) return true;
			else return false;
		
	}
