﻿//Função assincrona do populaCidade.
function retorno_populaCidade(ret)
{
    var dt = ret.value;
    var cidade = document.getElementById('cidade');
    cidade.options.length = 0;
	cidade.options[cidade.options.length] = new Option("Selecione uma Cidade","0");
	
    for(var i = 0; i < dt.length; i++)
    {
        cidade.options[cidade.options.length] = new Option(dt[i].Nome, dt[i].Codigo_Cidade);
    }
    
    document.getElementById('img_cidade').style.display = "none";
}

//Metodo responsável por popular as cidades de acordo com o estado selecionado.
function populaCidade(value)
{
    document.getElementById('img_cidade').style.display = "";
    indica_servicos.PopulaCidade(value,retorno_populaCidade);
}

function SetaCidade(dropDownCidade,enderecoSite)
{
    document.getElementById("cidade_hidden").value = dropDownCidade.value;
    
    var estado = document.getElementById("estado");
    var cidade = document.getElementById("cidade");
    
    //Viviane: URL Padrão 
    var estadoValor = estado.options[estado.selectedIndex].value;
    var cidadeValor = cidade.options[cidade.selectedIndex].value;
    
    
    //Buscar Estado
    estadoValor = indica_servicos.RetornaCidadeEstado(0,estadoValor).value;
    
    //Busca Cidade
    cidadeValor =indica_servicos.RetornaCidadeEstado(cidadeValor,0).value;
    
    var categoriaID = "";
    var subCategoriaID = "";
    var categoria = window.location.toString();
    //var categoria = 'http://bbel.uol.com.br/bbel_indica/lojas/sp/sao_paulo/alimentacao/lista/delicatessen/pg1';
    
    if (categoria.indexOf('/sos/') > -1)
    {
        categoria = categoria.split('.')[0];
        categoria = categoria.substring(categoria.indexOf('/') + 16);
    }
    else
    {
        categoria = categoria.split('.')[3];
        categoria = categoria.substring(categoria.indexOf('/') + 1);
    }
   
    var parametros = categoria.split('/');
 
    if (parametros.length == 6 )
    {    
        window.location = enderecoSite + "bbel_indica/servicos" + "/" + estadoValor + "/lista/" + cidadeValor + "/pg1.aspx";
    }
    
    else if (parametros.length == 5) 
    {
        categoriaID = parametros[4];
             
        window.location = enderecoSite + "bbel_indica/servicos" + "/" + estadoValor + "/" + cidadeValor + "/" + categoriaID + ".aspx";
    }
    else if (parametros.length > 5)
    {
        categoriaID = parametros[4];
        subCategoriaID = parametros[6];
        window.location = enderecoSite + "bbel_indica/servicos" + "/" + estadoValor + "/" + cidadeValor + "/" + categoriaID + "/" + parametros[5] + "/" + subCategoriaID + "/pg1.aspx";
    }
}

function RedirecionaParaSubcategoria(url,categoria)
{
    var estado = document.getElementById("estado");
    var cidade = document.getElementById("cidade");

    var estadoValor = estado.options[estado.selectedIndex].value;
    var cidadeValor = cidade.options[cidade.selectedIndex].value;

    var link;
    
    if (estadoValor != 0 && cidadeValor != 0)
    {  
    // alert(document.getElementById("cidade_hidden").value);
        document.getElementById("cidade_hidden").value = cidadeValor;
        link = element.href + "&estado=" + estadoValor + "&cidade=" + cidadeValor;
    }
    
    else
    {
        link = element.href;
    }
    window.location = link;
}