/**
* inicializacao do AJAX */ 
function ajaxInit() {
  var xmlhttp;
  try {
     xmlhttp = new XMLHttpRequest();
  } catch(ee) {
     try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch(e) {
        try {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(E) {
           xmlhttp = false;
        }
     }
  }
  return xmlhttp;
}

/**
* retorna a lista de subCats
*/
function buscaSubCatImovel(idc,idSubCat,campoSelSubCat){
	xmlhttpCat = ajaxInit();
    // limpa o select

    var c = document.getElementById(campoSelSubCat);
	
    while(c.options.length>0)c.options[0]=null;
    c.options[0] = new Option("Aguarde...","Aguarde...");

	url = "subCatImovel.php?idc="+idc;
	
    xmlhttpCat.open("GET", url, true);

    xmlhttpCat.onreadystatechange=function() {
        if (xmlhttpCat.readyState==4){
            // limpa o select
            var c = document.getElementById(campoSelSubCat);
            while(c.options.length > 0) c.options[0] = null;
			var subCatStr = xmlhttpCat.responseText;
			var subCatArr = subCatStr.split(':');
			var posSel = 0;
            // popula o select com a lista de subCats obtida
			c.options[c.options.length] = new Option("Selecione",0);
            for(var i = 0; i < subCatArr.length; i++){
				if ((i % 2) == 0) {
                	//c.options[c.options.length] = new Option(subCatArr[i+1],subCatArr[i]);
					if (subCatArr[i] == idSubCat){
						posSel = i;
						c.options[c.options.length] = new Option(subCatArr[i+1],subCatArr[i],true,true);
					} else c.options[c.options.length] = new Option(subCatArr[i+1],subCatArr[i]);
				}			
            }
        }
    }
    xmlhttpCat.send(null)
}


/////////////////////

function buscaCidade(tuf,idCidade,campoSelCidade){
	xmlhttp = ajaxInit();
    // limpa o select
    var c = document.getElementById(campoSelCidade);
	
    while(c.options.length>0)c.options[0]=null;
    c.options[0] = new Option("Aguarde...","Aguarde...");

	url = "cidadeLista.php?uf="+tuf;
    xmlhttp.open("GET", url, true);

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            // limpa o select
            var c = document.getElementById(campoSelCidade);
            while(c.options.length > 0) c.options[0] = null;
			var cidadeStr = xmlhttp.responseText;
			var cidadeArr = cidadeStr.split(':');
			var posSel = 0;
            // popula o select com a lista de cidades obtida
		  if(cidadeArr.length > 2){
			c.options[c.options.length] = new Option("Selecione",0);
		  }
            for(var i = 0; i < cidadeArr.length; i++){
				if ((i % 2) == 0) {
                	//c.options[c.options.length] = new Option(cidadeArr[i+1],cidadeArr[i]);
					if (cidadeArr[i] == idCidade){
						posSel = i;
						c.options[c.options.length] = new Option(cidadeArr[i+1],cidadeArr[i],true,true);
					} else c.options[c.options.length] = new Option(cidadeArr[i+1],cidadeArr[i]);
				}			
            }
        }
    }
    xmlhttp.send(null)
}

