function ajaxPesquisa(obj,idPesquisa){
	obj.html('<option value="0" selected>Carregando...</option>').multiselect('refresh');
	var valores = '';
	var id = obj.attr('id').split("_");
	
	var paramentros = $('#'+idPesquisa).serialize();
	paramentros = encodeURI(paramentros);
        paramentros = paramentros.replace('pesquisaOrigem=ok&','').replace(/%255B%255D/g,'[]');
 
	$.ajax({
	  url: 'ajax'+id[0]+'.php',
	  dataType: 'json',
	  data:  paramentros,
	  async: false,
	  success: function(dados) {
				
			var selectValor = selectValores[obj.attr('name').replace("[]","")];
			if (selectValor != null){
				selectValor = selectValor.split("#144#");
			}else{
				selectValor = new Array(0);
			}
						
			$.each(dados,function(i, dado){
				valores += '<option value="'+dado.value+'"';
				if ($.inArray(dado.value,selectValor) != -1 )
					valores += " selected ";
				valores += ' >'+dado.label+'</option>';
			});
							
			obj.html(valores).multiselect('refresh');
			tabindex = parseInt(obj.attr("tabindex"))+1;
			if($('#'+idPesquisa+' select[tabindex ="'+tabindex+'"]')[0]){
				ajaxPesquisa($('#'+idPesquisa+' select[tabindex ="'+tabindex+'"]'),idPesquisa);
			}
                        
                        if(id[0] == "Bairro"){
                            ajaxBairroRelacionados("#"+idPesquisa);
			}
							
	  },error: function() {
			obj.html('<option value="0">Erro ao carregar</option>').multiselect('refresh');
	}
	});


}


function pesquisaVianet(idPesquisa,campo){
	$('#'+idPesquisa+' select').multiselect({
	  		selectedList: 1,
			open: function (){ alterar = false;},
			click: function (){ alterar = true;},
			checkAll: function(){alterar = true;},
   			uncheckAll: function(){alterar = true;},
			close: function(e){
			
				if($(this).attr('multiple') && alterar && $(this).val() != null  )
				{
				   selectValores[$(this).attr('name').replace("[]","")] = $(this).val().join("#144#");
				}else{
					selectValores[$(this).attr('name')] = $(this).val();
				}
				if($(this).attr('tabindex') && alterar )
				{ 
					 tabindex = parseInt($(this).attr("tabindex"))+1;
					 if($('#'+idPesquisa+' select[tabindex ="'+tabindex+'"]')[0])
						 ajaxPesquisa($('#'+idPesquisa+' select[tabindex ="'+tabindex+'"]'),idPesquisa);	
				}
		//	 }
		 }
	})

	ajaxPesquisa($('#'+idPesquisa+ ' ' +campo),idPesquisa);
}
