
function limpa_select(){var f=document.getElementById('formulario');f.cidade.selectedIndex=0;f.bairro.selectedIndex=0;f.modelo.selectedIndex=0;f.marca.selectedIndex=0;f.estado.selectedIndex=0;habilita(false,'limpa');habilita(false,'lista');}
function desabilita_select(booleano){$('#marca,#modelo,#estado,#cidade,#bairro,#tipo_veiculo').attr('disabled',booleano);}
function troca_tipo_veiculo(){desabilita_select(true);$('body').fadeTo('fast',0.3);var tv=$('#tipo_veiculo').attr('value');if(tv=='carro'){location.href='/';return;}
location.href='/?tv='+tv;}
function carrega_select(id){$('#form_table').fadeTo('fast',0.4);$('#img_processando').css('z-index','999');desabilita_select(true);habilita(false,'lista');habilita(false,'limpa');var f=document.getElementById('formulario');var cidade=f.cidade;var bairro=f.bairro;var marca=f.marca;var modelo=f.modelo;var estado=f.estado;var tipo_veiculo=f.tipo_veiculo;var ano_inicial=f.ano_inicial;var ano_final=f.ano_final;if(id=='ano_inicial'||id=='ano_final'){ano_inicial=ano_inicial.options[ano_inicial.selectedIndex].value;ano_final=ano_final.options[ano_final.selectedIndex].value;}
else
ano_inicial=ano_final='';if(id=="estado"){cidade.selectedIndex=0;bairro.selectedIndex=0;}
else if(id=="marca"){modelo.selectedIndex=0;}
else if(id=="cidade"){bairro.selectedIndex=0;}
var d={'tv':tipo_veiculo.options[tipo_veiculo.selectedIndex].value,'marca':marca.options[marca.selectedIndex].value,'modelo':modelo.options[modelo.selectedIndex].value,'estado':estado.options[estado.selectedIndex].value,'cidade':cidade.options[cidade.selectedIndex].value,'bairro':bairro.options[bairro.selectedIndex].value,'ano_inicial':ano_inicial,'ano_final':ano_final}
var json;$.getJSON('/_pesquisa',d,function(json){carregou_select(json);});}
function carregou_select(json){$('#img_processando').css('z-index','-999');povoa_select($('#marca'),json.rs_marca,json.sel_marca);povoa_select($('#modelo'),json.rs_modelo,json.sel_modelo);povoa_select($('#estado'),json.rs_estado,json.sel_estado);povoa_select($('#cidade'),json.rs_cidade,json.sel_cidade);povoa_select($('#bairro'),json.rs_bairro,json.sel_bairro);povoa_select($('#ano_inicial'),json.rs_ano.slice(json.sel_ano_final),json.sel_ano_inicial-json.sel_ano_final);povoa_select($('#ano_final'),json.rs_ano.slice(0,json.sel_ano_inicial+1),json.sel_ano_final);$('#query_string').attr('value',json.qs);var f=document.getElementById('formulario');var ano_inicial=f.ano_inicial;var ano_final=f.ano_final;if(false&&f.estado.selectedIndex==0&&f.marca.selectedIndex==0)
{habilita(false,'limpa');habilita(false,'lista');}
else{habilita(true,'limpa');habilita(true,'lista');}
$('#form_table').fadeTo('fast',1);habilita_dropdown();}
function habilita_dropdown(){$('#marca,#estado,#tipo_veiculo').attr('disabled',false);if($('#estado').attr('selectedIndex')==0){$('#cidade').attr('disabled',true);$('#bairro').attr('disabled',true);}
else{$('#cidade').attr('disabled',false);if($('#cidade').attr('selectedIndex')==0)
$('#bairro').attr('disabled',true)
else
$('#bairro').attr('disabled',false);}
if($('#marca').attr('selectedIndex')==0)
$('#modelo').attr('disabled',true);else
$('#modelo').attr('disabled',false);}
function pede_lista(){$('#div_anuncios').fadeTo('fast',0.3);habilita(false,'lista');habilita(false,'limpa');desabilita_select(true);location.href='/?'+$('#query_string').attr('value');return;}
function onready_muda_habilitacao(){var f=document.getElementById('formulario');habilita(false,'lista')
if(f.estado.selectedIndex==0&&f.marca.selectedIndex==0){habilita(false,'limpa');}
else{habilita(true,'limpa');}
habilita_dropdown();}
function listou_muda_habilitacao(){habilita(false,'lista');habilita(true,'limpa');}
function habilita(boleano,ele){document.getElementById(ele).disabled=!boleano;ele='#'+ele;if(ele=='#lista'||ele=='#limpa'){if(boleano){$(ele).addClass('habilitado');}
else{$(ele).removeClass('habilitado');}}}
noo=[function(l,i){return l[i];},function(l,i){return l;}];function povoa_select(select,rs,selectedIndex){select.empty();var rs_length=rs.length;if(typeof(rs[0])=="object")var n=0;else var n=1;for(var i=0;i<rs_length;i++){var linha=rs[i];var option=jQuery('<option value="'+noo[n](linha,1)+'">'+noo[n](linha,0)+'</option>');option.appendTo(select);}
select.attr('selectedIndex',selectedIndex);}
