﻿function QueryString(variavel)
{
    var variaveis = location.search.replace(/\x3F/,"").replace(/\x2B/g," ").split("&")
    var nvar   
    if(variaveis != "")
    {
        var qs = []
        for(var i = 0; i < variaveis.length; i++)
        {
            nvar = variaveis[i].split("=")
            qs[nvar[0]] = unescape(nvar[1])
        }
        return qs[variavel]
    }
    return null
}

//Função assincrona do populaMarca.
function retorno_populaMarca(ret){
    var dt = ret.value;
    var marca = document.getElementById('marca');
    marca.options.length = 0;
	marca.options[marca.options.length] = new Option("--","0");            
    for(var i = 0; i < dt.length; i++)
    {                
        marca.options[marca.options.length] = new Option(dt[i].Marca, dt[i].Codigo_VeiculoMarca);
    }
    document.getElementById('img_marca').style.display = "none";
}

//Função assincrona do populaModelo.
function retorno_populaModelo(ret){
    var dt = ret.value;
    var modelo = document.getElementById('modelo');
    modelo.options.length = 0;
	modelo.options[modelo.options.length] = new Option("Selecione o modelo","0");            
    for(var i = 0; i < dt.length; i++)
    {                
        modelo.options[modelo.options.length] = new Option(dt[i].Modelo, dt[i].Codigo_VeiculoModelo);
    }
    document.getElementById('img_modelo').style.display = "none";
    
}

//Metodo responsável por popular os modelos de um veiculo de acordo com a marca selecionada.
function populaModelo(value)
{
    document.getElementById('img_modelo').style.display = "";
    FunctionAjax.BuscaModeloComAnunciosLiberados(value,retorno_populaModelo);   
}


//Função assincrona do populaModelo.
function retorno_populaVersao(ret){
    var dt = ret.value;
    var versao = document.getElementById('versao');
    versao.options.length = 0;
	versao.options[versao.options.length] = new Option("Selecione a versão","0");            
    for(var i = 0; i < dt.length; i++)
    {                
        versao.options[versao.options.length] = new Option(dt[i].Versao, dt[i].Codigo_VeiculoVersao);
    }
    document.getElementById('img_versao').style.display = "none";
    
}

//Metodo responsável por popular os modelos de um veiculo de acordo com a marca selecionada.
function populaVersao(value)
{
    document.getElementById('img_versao').style.display = "";
    FunctionAjax.BuscaVersaoComAnunciosLiberados(value,retorno_populaVersao);   
}

//Metodo responsavel por buscar os opcionais checados de um veiculo
function BuscaOpcionaisMarcados()
{
    var ids = '';
       
    if(document.getElementById('ul_opcionais') != null)
    {
        var element = document.getElementById('ul_opcionais').getElementsByTagName('INPUT');
        for(var i = 0; i < element.length; i++)
        {
            if(element[i].type == 'checkbox')
            {
                if(element[i].checked)
                {
                    ids += (ids=='')?(element[i].value):(";"+element[i].value)
                }
            }
        }
    }
    return ids;
}

function RealizaBuscaAvancada(tipoBusca)
{
    var sucesso = false;
    var _opcionais = BuscaOpcionaisMarcados();
    
    //var tipoBusca ="";
    var garagem ="";
    var modelo="";
    var tipo="";
    var cidade="";
    var marca="";
    var versao="";
    var ano_de="";
    var ano_ate="";
    var valor_de="";
    var valor_ate="";
    var cidade_busca_avancada="";
    var combustivel="";
    var cor="";
    var n_portas="";
    var unico_dono="";
    var alienado="";
    var garantia="";
    var ipva_pago="";
    var usado="";
    var licenciado="";
    var refrigeracao="";
    var estilo="";
    var opcionais="";



    var url = 'resultado_busca.aspx?tipo='+tipoBusca;
    if(document.getElementById('marca').value != "0")
    {
        marca = document.getElementById('marca').value; 
        url += "&marca="+document.getElementById('marca').value; 
        sucesso = true;       
    }
    if(document.getElementById('garagem').value != "0")
    {
        garagem = document.getElementById('garagem').value; 
        url += "&garagem="+document.getElementById('garagem').value; 
        sucesso = true;       
        
    }
    
    if(document.getElementById('modelo').value != "0")
    {
        modelo = document.getElementById('modelo').value;
        url += "&modelo="+document.getElementById('modelo').value;
        sucesso = true;
    }
   
    if(document.getElementById('versao').value != "0")
    {
        versao = document.getElementById('versao').value;
        url += "&versao="+document.getElementById('versao').value;
        sucesso = true;
    }
    
    if(document.getElementById('veiculo_ano_de').value != "0")
    {
       ano_de = document.getElementById('veiculo_ano_de').value;
        url += "&ano_de="+document.getElementById('veiculo_ano_de').value;
        sucesso = true;
    }
    
    if(document.getElementById('veiculo_ano_ate').value != "0")
    {
        ano_ate = document.getElementById('veiculo_ano_ate').value;
        url += "&ano_ate="+document.getElementById('veiculo_ano_ate').value;
        sucesso = true;
    }
    if(document.getElementById('valor_de').value != "0")
    {
        valor_de = document.getElementById('valor_de').value;
        url += "&valor_de="+document.getElementById('valor_de').value;
        sucesso = true;
    }
    
    if(document.getElementById('valor_ate').value != "0")
    {
        valor_ate = document.getElementById('valor_ate').value;
        url += "&valor_ate="+document.getElementById('valor_ate').value;
        sucesso = true;
    }
    
    if(document.getElementById('cidade_busca_avancada').value != "0")
    {
        cidade = document.getElementById('cidade_busca_avancada').value;
        url += "&cidade="+document.getElementById('cidade_busca_avancada').value;
        sucesso = true;
    }
    
    if(document.getElementById('combustivel').value != "0")
    {
        combustivel = document.getElementById('combustivel').value;
        url += "&combustivel="+document.getElementById('combustivel').value;
        sucesso = true;
    }
    
    if(document.getElementById('cor').value != "0")
    {
        cor = document.getElementById('cor').value;
        url += "&cor="+document.getElementById('cor').value;
        sucesso = true;
    }
    
    if(tipoBusca == '1')
    {
        if(document.getElementById('n_portas').value != "0")
        {
            n_portas = document.getElementById('n_portas').value;
            url += "&n_portas="+document.getElementById('n_portas').value;
            sucesso = true;
        }
    }
    else if(tipoBusca == '2')
    {
        if(document.getElementById('estilo').value != "0")
        {
            estilo = document.getElementById('estilo').value;
            url += "&estilo="+document.getElementById('estilo').value;
            sucesso = true;
        }
        
        if(document.getElementById('tipo_refrigeracao').value != "0")
        {
            refrigeracao = document.getElementById('tipo_refrigeracao').value;
            url += "&refrigeracao="+document.getElementById('tipo_refrigeracao').value;
            sucesso = true;
        }
    }
    
    if(_opcionais != '')
    {
            opcionais = _opcionais;
           url += "&opcionais="+_opcionais;
           sucesso = true;
    }
    
    if(document.getElementById('unico_dono').checked)
    {
        unico_dono = "1";
        url += "&unico_dono=1";
        sucesso = true;
    }
    
    if(document.getElementById('alienado').checked)
    {
        alienado = "1";
        url += "&alienado=1";
        sucesso = true;
    }
    
    if(document.getElementById('garantia').checked)
    {
        garantia = "1";
        url += "&garantia=1";
        sucesso = true;
    }
    
    if(document.getElementById('ipva_pago').checked)
    {
        ipva_pago = "1";
        url += "&ipva_pago=1";
        sucesso = true;
    }
    
    if(document.getElementById('veiculo_usadoSim').checked)
    {
        usado = "1";
        url += "&usado=1";
        sucesso = true;
    }
    
    if(document.getElementById('veiculo_usadoNao').checked)
    {
        usado = "0";
        url += "&usado=0";
        sucesso = true;
    }
    if(document.getElementById('licenciado').checked)
    {
        licenciado = "1";
        url += "&licenciado=1";
        sucesso = true;
    }
    
    if(!sucesso)
    {
        alert('Por favor selecione pelo menos um campo para realizar a busca.');
        
        return false;
    }

    window.location = url;
}



function validaOpcionais()
{
  var variavel = QueryString('opcionais');
  var elementosInput = document.getElementsByName('opcionais');
  if( variavel != null)
    {
        variavel = variavel.split(';');
        for(var x = 0; x < variavel.length; x++)
        {

            for (var i = 0; i < elementosInput.length; i++)
            {
                if('opcionais_'+variavel[x] == elementosInput[i].id)
                {
                     elementosInput[i].checked = true;
                }
            }
        }
    }
}
function buscaVazia()
{
    alert('Sua busca não retornou nenhum resultado, por favor tente novamente.');
}
