﻿function setCidades(obj, bandeira, ddlId) {
    var progress = document.getElementById("ctl00_UpProgress")    
    progress.style.display = "block";
    
    alert(bandeira);
    
    var cidsList = document.getElementById(ddlId);
    cidsList.options.length = 0; 
    cidsList.options[cidsList.options.length] = new Option("Selecione", "");
    
    cidsList.disabled = true;
    
    try{WebHome_UserControl_LojaMaisProxima.setCidades(obj.value, bandeira, setCidades_callback);}
    catch(e){}
    function setCidades_callback(response){              
        if (response.error != null)
        {
            progress.style.display = "none";
            cidsList.disabled = false;
            return;
        }

        var cids = response.value;       
        if (cids == null || typeof(cids) != "object") return;    
         
        for (var i = 0; i < cids.length; ++i)
            cidsList.options[cidsList.options.length] = new Option(cids[i].Vc_nom_cidade, cids[i].In_cod_cidade);
            
        progress.style.display = "none";
        cidsList.disabled = false;
    }
}

function setModelo(obj, ddlId){
    var cidsList = document.getElementById(ddlId);
    cidsList.options.length = 0; 
    cidsList.options[cidsList.options.length] = new Option("-- Carregando aguarde --", "");
    
    var modelos = new Array();
    var x = 0;
    for(var i=0; i<obj.options.length; i++){
        if(obj.options[i].selected){
            modelos[x] = obj.options[i].value;
            x++
        }
    }    
    
    cidsList.disabled = true;
    try{WebHome_UserControl_ProcureSeuVeiculo.setModelo(modelos, calbeak);}
    catch(e){
        try{WebHome_UserControl_ProcureSeuVeiculoHotSites.setModelo(modelos, calbeak);}
        catch(e){
            try{WebHome_PesquisaMotos.setModelo(modelos, calbeak);}
            catch(e){WebHome_PesquisaMotosHotSites.setModelo(modelos, calbeak);}
        }
    }
    function calbeak(response){        
        cidsList.options.length = 0; 
        if (response.error != null)
        {
            cidsList.options[cidsList.options.length] = new Option("-- selecione uma marca --", "");
            cidsList.disabled = false;
            return;
        }

        var cids = response.value;       
        if (cids == null || typeof(cids) != "object") return;    
        
        for (var i = 0; i < cids.length; ++i)
                cidsList.options[cidsList.options.length] = new Option(cids[i].Vc_nom_modelo, cids[i].In_cod_modelo);

        if( cidsList.options.length < 1 )
            cidsList.options[cidsList.options.length] = new Option("-- não existem itens com esta opção --", "");            
        cidsList.disabled = false;
    }
}

function lojaLocation(objUf, objCid){
    var objUf = document.getElementById(objUf)
    var objCid = document.getElementById(objCid)
    window.location = "LojaMaisProxima.aspx?uf=" + objUf.value + "&cid=" + objCid.value;    
}

function lojaLocationHotSites(objUf, objCid, objMarca){

var objUf = document.getElementById(objUf)
var objCid = document.getElementById(objCid)

window.location = "LojaMaisProximaHotSites.aspx?uf="+ objUf.value + "&cid=" + objCid.value + "&marca=" + objMarca;

}

function getVeiculos(marcaId, modeloId, fpDId, fpAteId, blindadoId, novos){
    var marca = document.getElementById(marcaId)
    var modelo = document.getElementById(modeloId)
    var blindado = document.getElementById(blindadoId)
    var fpD = document.getElementById(fpDId)
    var fpAte = document.getElementById(fpAteId)
        
    var marcas = "";    
    
    
    var cbMarca = marca.getElementsByTagName("input");
        var valores = new Array();
        for(var i=0; i<cbMarca.length; i++) {
            if(cbMarca[i].type.toLowerCase() == "checkbox") {
                if(cbMarca[i].checked) 
                {
                    //alert(inputs[i].value + 'a');
                    //valores.push(inputs[i].value);
                    marcas += cbMarca[i].value + ",";
                }
            }
        }        
    /*for(var i=0; i<marca.length; i++){
        if(marca[i].checked)
        {
        alert(marca[i]);
            if( marca[i].value != null )
            {
                marcas += marca[i].value + ",";
            }
        }
    }*/
    
//    if (blindadoId != "true" && blindadoId != "false")
//    {
//        blindado.value = "";
//    }
    
    if( marcas.length > 0 )
       marcas = marcas.substring(0, marcas.length - 1)
    
    var modelos = "";    
    
    
    var cbModelo = modelo.getElementsByTagName("input");
        var valores = new Array();
        for(var i=0; i<cbModelo.length; i++) {
            if(cbModelo[i].type.toLowerCase() == "checkbox") {
                if(cbModelo[i].checked) 
                {
                    //alert(inputs[i].value + 'a');
                    //valores.push(inputs[i].value);
                    modelos += cbModelo[i].value + ",";
                }
            }
        }       
        
        
    /*for(var i=0; i<modelo.options.length; i++){
        if(modelo.options[i].selected){
            if( modelo.options[i].value != "null" )
                modelos += modelo.options[i].value + ",";
        }
    }*/
    
    if( modelos.length > 0 )
    {
        modelos = modelos.substring(0, modelos.length - 1)
        window.location = (novos == 1 ? "ResultadosBusca.aspx?tpv=cn" : "ResultadosBusca.aspx?tpv=cs") + "&m=" + marcas +
        "&mo=" + modelos + "&fpi=" + fpD.value + "&fpf=" + fpAte.value + "&bld=" + blindado.value;
    }else{
        window.location = (novos == 1 ? "ResultadosBuscaModelo.aspx?tpv=cn" : "ResultadosBuscaG.aspx?tpv=cs") + "&m=" + marcas +
        "&mo=" + modelos + "&fpi=" + fpD.value + "&fpf=" + fpAte.value + "&bld=" + blindado.value;
    }
}

function getVeiculosSemi(marcaId, modeloId, fpDId, fpAteId, blindadoId, anoIni, anoFim, semi){
    var marca = document.getElementById(marcaId)
    var modelo = document.getElementById(modeloId)
    var blindado = document.getElementById(blindadoId)
    var fpD = document.getElementById(fpDId)
    var fpAte = document.getElementById(fpAteId)
    var AnoIni = document.getElementById(anoIni)
    var Anofim = document.getElementById(anoFim)

    var marcas = "";    
    /*for(var i=0; i<marca.options.length; i++){
        if(marca.options[i].selected){
            if( marca.options[i].value != null )
                marcas += marca.options[i].value + ",";
        }
    }   */
    
    var inputs = marca.getElementsByTagName("input");
        var valores = new Array();
        for(var i=0; i<inputs.length; i++) {
            if(inputs[i].type.toLowerCase() == "checkbox") {
                if(inputs[i].checked) 
                {
                    //alert(inputs[i].value + 'a');
                    //valores.push(inputs[i].value);
                    marcas += inputs[i].value + ",";
                }
            }
        }     
         
    
//    if (blindadoId != "true" && blindadoId != "false")
//    {
//        blindado.value = "";
//    }
    
    if( marcas.length > 0 )
       marcas = marcas.substring(0, marcas.length - 1)
    
    var modelos = "";    
    
    var cbModelo = modelo.getElementsByTagName("input");
        var valores = new Array();
        for(var i=0; i<cbModelo.length; i++) {
            if(cbModelo[i].type.toLowerCase() == "checkbox") {
                if(cbModelo[i].checked) 
                {
                    //alert(inputs[i].value + 'a');
                    //valores.push(inputs[i].value);
                    modelos += cbModelo[i].value + ",";
                }
            }
        }      
        
    /*for(var i=0; i<modelo.options.length; i++){
        if(modelo.options[i].selected){
            if( modelo.options[i].value != "null" )
                modelos += modelo.options[i].value + ",";
        }
    }*/
    
    //if( modelos.length > 0 )
    //{
        modelos = modelos.substring(0, modelos.length - 1)
        window.location = (semi == 1 ? "ResultadosBusca.aspx?tpv=cn" : "ResultadosBusca.aspx?tpv=cs") + "&m=" + marcas +
        "&mo=" + modelos + "&fpi=" + fpD.value +
        "&fpf=" + fpAte.value + "&anoIni=" + AnoIni.value + "&anoFim=" + Anofim.value + "&tp=1" + "&bld=" + blindado.value;
     /*}else{
        window.location = (semi == 1 ? "ResultadosBuscaG.aspx?tpv=cn" : "ResultadosBuscaG.aspx?tpv=cs") + "&m=" + marcas +
        "&mo=" + modelos + "&fpi=" + fpD.value +
        "&fpf=" + fpAte.value + "&anoIni=" + AnoIni.value + "&anoFim=" + Anofim.value + "&tp=1";
     }*/
}

function getVeiculosMotos(marcaId, modeloId, fpDId, fpAteId, novos){
    var marca = document.getElementById(marcaId)
    var modelo = document.getElementById(modeloId)
    var fpD = document.getElementById(fpDId)
    var fpAte = document.getElementById(fpAteId)
    
    var marcas = "";    
    /*for(var i=0; i<marca.options.length; i++){
        if(marca.options[i].selected){
            if( marca.options[i].value != null )
                marcas += marca.options[i].value + ",";
        }
    }     */
    
    
    var inputs = marca.getElementsByTagName("input");
        var valores = new Array();
        for(var i=0; i<inputs.length; i++) {
            if(inputs[i].type.toLowerCase() == "checkbox") {
                if(inputs[i].checked) 
                {
                    //alert(inputs[i].value + 'a');
                    //valores.push(inputs[i].value);
                    marcas += inputs[i].value + ",";
                }
            }
        }     
    
    if( marcas.length > 0 )
       marcas = marcas.substring(0, marcas.length - 1)
    
    var modelos = "";    
    /*for(var i=0; i<modelo.options.length; i++){
        if(modelo.options[i].selected){
            if( modelo.options[i].value != "null" )
                modelos += modelo.options[i].value + ",";
        }
    }*/
    
    var cbModelo = modelo.getElementsByTagName("input");
        var valores = new Array();
        for(var i=0; i<cbModelo.length; i++) {
            if(cbModelo[i].type.toLowerCase() == "checkbox") {
                if(cbModelo[i].checked) 
                {
                    //alert(inputs[i].value + 'a');
                    //valores.push(inputs[i].value);
                    modelos += cbModelo[i].value + ",";
                }
            }
        }      
    
    if( modelos.length > 0 )
        modelos = modelos.substring(0, modelos.length - 1)
    
    window.location = (novos == 1 ? "ResultadosBusca.aspx?tpv=mn" : "ResultadosBusca.aspx?tpv=ms") + "&m=" + marcas +
            "&mo=" + modelos + "&fpi=" + fpD.value + "&fpf=" + fpAte.value;
}

function getVeiculosMotosSemi(marcaId, modeloId, fpDId, fpAteId, anoIni, anoFim, semi){
    var marca = document.getElementById(marcaId)
    var modelo = document.getElementById(modeloId)
    var fpD = document.getElementById(fpDId)
    var fpAte = document.getElementById(fpAteId)
    var AnoIni = document.getElementById(anoIni)
    var Anofim = document.getElementById(anoFim)
    
    var marcas = "";    
    /*for(var i=0; i<marca.options.length; i++){
        if(marca.options[i].selected){
            if( marca.options[i].value != null )
                marcas += marca.options[i].value + ",";
        }
    } */
    
    var inputs = marca.getElementsByTagName("input");
        var valores = new Array();
        for(var i=0; i<inputs.length; i++) {
            if(inputs[i].type.toLowerCase() == "checkbox") {
                if(inputs[i].checked) 
                {
                    //alert(inputs[i].value + 'a');
                    //valores.push(inputs[i].value);
                    marcas += inputs[i].value + ",";
                }
            }
        }        
    
    if( marcas.length > 0 )
       marcas = marcas.substring(0, marcas.length - 1)
    
    var modelos = "";    
    /*for(var i=0; i<modelo.options.length; i++){
        if(modelo.options[i].selected){
            if( modelo.options[i].value != "null" )
                modelos += modelo.options[i].value + ",";
        }
    }*/
    
    var cbModelo = modelo.getElementsByTagName("input");
        var valores = new Array();
        for(var i=0; i<cbModelo.length; i++) {
            if(cbModelo[i].type.toLowerCase() == "checkbox") {
                if(cbModelo[i].checked) 
                {
                    //alert(inputs[i].value + 'a');
                    //valores.push(inputs[i].value);
                    modelos += cbModelo[i].value + ",";
                }
            }
        }      
    
    if( modelos.length > 0 )
        modelos = modelos.substring(0, modelos.length - 1)
    
    window.location = (semi == 1 ? "ResultadosBusca.aspx?tpv=mn" : "ResultadosBusca.aspx?tpv=ms") + "&m=" + marcas +
            "&mo=" + modelos + "&fpi=" + fpD.value +
            "&fpf=" + fpAte.value + "&anoIni=" + AnoIni.value + "&anoFim=" + Anofim.value + "&tp=2";
}

function removeBusca(id, marca){
    if( event.srcElement ){
        var td = event.srcElement.parentNode
        if(confirm("Confirma a exclusão do item na busca?")){
            td.parentNode.removeChild(td)
            var hMarcas = document.getElementById("ctl00_Meio_hMarcas") 
            var hModelo = document.getElementById("ctl00_Meio_hModelos")
            var hfpi = document.getElementById("ctl00_Meio_hfpis")
            var hfpf = document.getElementById("ctl00_Meio_hfpfs")
            var hr = document.getElementById("ctl00_Meio_hrs")
                            
            if( marca == 1 ){
                hMarcas.value = hMarcas.value.replace(id, "");
                hMarcas.value = hMarcas.value.replace(",,", ",");
                if(hMarcas.value.substring(hMarcas.value.length-1, hMarcas.value.length) == ",")
                    hMarcas.value = hMarcas.value.substring(0, hMarcas.value.length - 1)
                if(hMarcas.value.substring(0, 1) == ",")
                    hMarcas.value = hMarcas.value.substring(1, hMarcas.value.length)
            }
            else{   
                hModelo.value = hModelo.value.replace(id, "");
                hModelo.value = hModelo.value.replace(",,", ",");
                if(hModelo.value.substring(hModelo.value.length-1, hModelo.value.length) == ",")
                    hModelo.value = hModelo.value.substring(0, hModelo.value.length - 1)
                if(hModelo.value.substring(0, 1) == ",")
                    hModelo.value = hModelo.value.substring(1, hModelo.value.length)
            }
           
            window.location = "ResultadosBusca.aspx?tpv=cn&m=" + hMarcas.value +
                "&mo=" + hModelo.value + "&fpi=" + hfpi.value +
                "&fpf=" + hfpf.value + "&r=" + hr.value;
        }
    }
}

function removeBuscaSemi(id, marca){
    if( event.srcElement ){
        var td = event.srcElement.parentNode
        if(confirm("Confirma a exclusão do item na busca?")){
            td.parentNode.removeChild(td)
            var hMarcas = document.getElementById("ctl00_Meio_hMarcas") 
            var hModelo = document.getElementById("ctl00_Meio_hModelos")
            var hfpi = document.getElementById("ctl00_Meio_hfpis")
            var hfpf = document.getElementById("ctl00_Meio_hfpfs")
            var hanoini = document.getElementById("ctl00_Meio_ProcureSeuVeiculo1_tbcSearch_tbpSemi_ddlAnoInicialSemi")
            var hanofim = document.getElementById("ctl00_Meio_ProcureSeuVeiculo1_tbcSearch_tbpSemi_ddlAnoFinalSemi")
            var hr = document.getElementById("ctl00_Meio_hrs")
                            
            if( marca == 1 ){
                hMarcas.value = hMarcas.value.replace(id, "");
                hMarcas.value = hMarcas.value.replace(",,", ",");
                if(hMarcas.value.substring(hMarcas.value.length-1, hMarcas.value.length) == ",")
                    hMarcas.value = hMarcas.value.substring(0, hMarcas.value.length - 1)
                if(hMarcas.value.substring(0, 1) == ",")
                    hMarcas.value = hMarcas.value.substring(1, hMarcas.value.length)
            }
            else{   
                hModelo.value = hModelo.value.replace(id, "");
                hModelo.value = hModelo.value.replace(",,", ",");
                if(hModelo.value.substring(hModelo.value.length-1, hModelo.value.length) == ",")
                    hModelo.value = hModelo.value.substring(0, hModelo.value.length - 1)
                if(hModelo.value.substring(0, 1) == ",")
                    hModelo.value = hModelo.value.substring(1, hModelo.value.length)
            }
            
            window.location = "ResultadosBusca.aspx?tpv=cs&m=" + hMarcas.value +
                "&mo=" + hModelo.value + "&fpi=" + hfpi.value +
                "&fpf=" + hfpf.value + "&anoIni=" + hanoini.value + "&anoFim=" + hanofim.value + "&r=" + hr.value;
        }
    }
}


function removeBuscaMotos(id, marca){
    if( event.srcElement ){
        var td = event.srcElement.parentNode
        if(confirm("Confirma a exclusão do item na busca?")){
            td.parentNode.removeChild(td)
            var hMarcas = document.getElementById("ctl00_Meio_hMarcas") 
            var hModelo = document.getElementById("ctl00_Meio_hModelos")
            var hfpi = document.getElementById("ctl00_Meio_hfpis")
            var hfpf = document.getElementById("ctl00_Meio_hfpfs")            
            var hr = document.getElementById("ctl00_Meio_hrs")
                
            if( marca == 1 ){
                hMarcas.value = hMarcas.value.replace(id, "");
                hMarcas.value = hMarcas.value.replace(",,", ",");
                if(hMarcas.value.substring(hMarcas.value.length-1, hMarcas.value.length) == ",")
                    hMarcas.value = hMarcas.value.substring(0, hMarcas.value.length - 1)
                if(hMarcas.value.substring(0, 1) == ",")
                    hMarcas.value = hMarcas.value.substring(1, hMarcas.value.length)
            }
            else{   
                hModelo.value = hModelo.value.replace(id, "");
                hModelo.value = hModelo.value.replace(",,", ",");
                if(hModelo.value.substring(hModelo.value.length-1, hModelo.value.length) == ",")
                    hModelo.value = hModelo.value.substring(0, hModelo.value.length - 1)
                if(hModelo.value.substring(0, 1) == ",")
                    hModelo.value = hModelo.value.substring(1, hModelo.value.length)
            }
                       
            window.location = "BuscaMotos.aspx?m=" + hMarcas.value +
                "&mo=" + hModelo.value + "&fpi=" + hfpi.value +
                "&fpf=" + hfpf.value + "&r=" + hr.value;
        }
    }
}

function removeBuscaMotosSemi(id, marca){
    if( event.srcElement ){
        var td = event.srcElement.parentNode
        if(confirm("Confirma a exclusão do item na busca?")){
            td.parentNode.removeChild(td)
            var hMarcas = document.getElementById("ctl00_Meio_hMarcas") 
            var hModelo = document.getElementById("ctl00_Meio_hModelos")
            var hfpi = document.getElementById("ctl00_Meio_hfpis")
            var hfpf = document.getElementById("ctl00_Meio_hfpfs")
            var hanoini = document.getElementById("ctl00_Meio_PesquisaMotos_tbcSearch_tbpSemi_ddlMotoAnoInicial")
            var hanofim = document.getElementById("ctl00_Meio_PesquisaMotos_tbcSearch_tbpSemi_ddlMotoAnoFinal")
            var hr = document.getElementById("ctl00_Meio_hrs")
                            
            if( marca == 1 ){
                hMarcas.value = hMarcas.value.replace(id, "");
                hMarcas.value = hMarcas.value.replace(",,", ",");
                if(hMarcas.value.substring(hMarcas.value.length-1, hMarcas.value.length) == ",")
                    hMarcas.value = hMarcas.value.substring(0, hMarcas.value.length - 1)
                if(hMarcas.value.substring(0, 1) == ",")
                    hMarcas.value = hMarcas.value.substring(1, hMarcas.value.length)
            }
            else{   
                hModelo.value = hModelo.value.replace(id, "");
                hModelo.value = hModelo.value.replace(",,", ",");
                if(hModelo.value.substring(hModelo.value.length-1, hModelo.value.length) == ",")
                    hModelo.value = hModelo.value.substring(0, hModelo.value.length - 1)
                if(hModelo.value.substring(0, 1) == ",")
                    hModelo.value = hModelo.value.substring(1, hModelo.value.length)
            }
            
            window.location = "ResultadosBusca.aspx?tpv=mn&m=" + hMarcas.value +
                "&mo=" + hModelo.value + "&fpi=" + hfpi.value +
                "&fpf=" + hfpf.value + "&anoIni=" + hanoini.value + "&anoFim=" + hanofim.value + "&r=" + hr.value;
        }
    }
}

function getRefinaBusca(blindagem,cambio,portas,cor,combustivel,marca,modelo)
{
var hBlindagem = document.getElementById("ctl00_cphColunaEsquerda_RefineBusca1_rblBlindadoNovos")
var hBlindagemSim = document.getElementById("ctl00_cphColunaEsquerda_RefineBusca1_rblBlindadoNovos_0")
var hBlindagemNao = document.getElementById("ctl00_cphColunaEsquerda_RefineBusca1_rblBlindadoNovos_1")
var hCambio = document.getElementById("ctl00_cphColunaEsquerda_RefineBusca1_Quilometragem1_ddlCambio")
var hPortas = document.getElementById("ctl00_cphColunaEsquerda_RefineBusca1_Quilometragem1_ddlPortas")
var hCor = document.getElementById("ctl00_cphColunaEsquerda_RefineBusca1_Quilometragem1_ddlCor")
var hCombustivel = document.getElementById("ctl00_cphColunaEsquerda_RefineBusca1_Quilometragem1_ddlCombustivel")
var opcionais = document.getElementById("ctl00_cphColunaEsquerda_RefineBusca1_Opcionais1_ListBox1")
var hMarcas = document.getElementById("ctl00_cphColunaEsquerda_RefineBusca1_ListMarcasNovos")
var hModelos = document.getElementById("ctl00_cphColunaEsquerda_RefineBusca1_listModeloNovos")

  
        
        ///////////////////////////////////////////
        var marcas = "";    
        for(var i=0; i<hMarcas.options.length; i++){
            if(hMarcas.options[i].selected){
                if( hMarcas.options[i].value != null )
                    marcas += hMarcas.options[i].value + ",";
            }
        }     
        
        if( marcas.length > 0 )
           marcas = marcas.substring(0, marcas.length - 1)
        /////////////////////////////////////////////
        var modelos = "";    
        for(var i=0; i<hModelos.options.length; i++){
            if(hModelos.options[i].selected){
                if( hModelos.options[i].value != "null" )
                    modelos += hModelos.options[i].value + ",";
            }
        }
        
        if( modelos.length > 0 )
            modelos = modelos.substring(0, modelos.length - 1)
        /////////////////////////////////////////////    
        var opcList = "";    
        for(var i=0; i<opcionais.options.length; i++){
            if(opcionais.options[i].selected){
                if( opcionais.options[i].value != "null" )
                    opcList += opcionais.options[i].value + ",";
            }
        }
        
        if( opcList.length > 0 )
            opcList = opcList.substring(0, opcList.length - 1)        
        //////////////////////////////////////////
        
        var flgBindado = "";
        if (hBlindagemSim.checked == true)
        {
        flgBlindado = "0";
        }
        else
        {
        flgBlindado = "1"
        }        
        
        //Consistência de parametros    
        //alert('cambio:'+hCambio.value)
        //alert('portas:'+hPortas.value)
        //alert('cor:'+hCor.value)
        //alert('combustivel:'+hCombustivel.value)
        //alert('marcas:'+marcas)
        //alert('modelos:'+modelos)
        //alert('opcionais:'+opcList)
        
        window.location = "busca_refinada_novos.aspx?flgBlindado=" + flgBlindado +
                "&marcas=" + marcas + "&modelos=" + modelos +
                "&tipo_cambio=" + hCambio.value + "&num_portas=" + hPortas.value + "&id_cor=" + hCor.value + "&id_combustivel=" + hCombustivel.value + "&id_opcionais="+opcList; 
        
}


