﻿function bindAno( formName, comboAno) {
j=0;
eval("document.getElementById(formName)."+comboAno+".options[0] = new Option('-', '')");
for (i=2011; i > 1941;i--){
j++;
eval("document.getElementById(formName)."+comboAno+".options[j] = new Option(i, i)");
}}
function carregaPreco(formName, cboPreco)
{
eval("document.getElementById(formName)."+cboPreco+".options[0] = new Option('R$', '0')");
for (i = 0; i<50; i++)
{
eval("document.getElementById(formName)."+cboPreco+".options[i+1] = new Option('"+(i*1000)+",00', '"+(i*1000)+"' )");
}}
function carregaPrecoFinal(formName, cboPreco, valor)
{
document.FormBusca.hidPreIni.value = valor;
limpaCombo(cboPreco);
eval("document.getElementById(formName)."+cboPreco+".options[0] = new Option('R$', '0')");
val = valor /1000 +1;
j = 0;
for (i = 0; i<75; i++)
{
eval("document.getElementById(formName)."+cboPreco+".options[j] = new Option('"+((i*2500)+(val*1000))+",00', '"+((i*2500)+(val*1000))+"' )");
j++;

}
for (i =75; i<125; i++)
{

eval("document.getElementById(formName)."+cboPreco+".options[j] = new Option('"+((i*5000)+((val-1)*1000)-184000)+",00', '"+((i*5000)+((val-1)*1000)-184000)+"' )");
j++;
}
}

//'/ajax/ajax.ashx?marcaid=' + _valorMarcaID + '&tipo=' + _tipo + '&uf=' + valorEstado
function PassaModelo(modeloid)
{
document.FormBusca.hidModeloID.value = modeloid;
}
function passPreco(preco)
{
document.FormBusca.hidPreFin.value =  preco;
}
function fillAnoM(comboAnoM, AnoFabri, formName)
{
limpaCombo('cboAnoM');
//alert(document.getElementById('cboAno').options[document.getElementById('cboAno').selectedIndex].value;
var AnoFab = AnoFabri;
j = 0;
if(AnoFabri != "")
{
eval("document.getElementById(formName)."+comboAnoM+".options[0] = new Option(AnoFabri, AnoFabri)");
for (i=AnoFab; i < 2011; i++)
{
j++;
AnoFab++;
//obj.options[j] = new Option(i, i);
eval("document.getElementById(formName)."+comboAnoM+".options[j] = new Option(AnoFab, AnoFab)");
}
document.FormBusca.hidAnoIni.value = AnoFabri - 1900;
}
else
{
bindAno('cboAnoM', 'FormBusca');
document.FormBusca.hidAnoIni.value = "0";
}
}
function limpaCombo(combo)
{
if (document.getElementById(combo) != null)
document.getElementById(combo).options.length = 0;
}
function PreencheModelo(valueMarca) {
    limpaCombo('cboModelo');
_iEstado = 0;
if (document.getElementById('cboEstado').value != 0) _iEstado = document.getElementById('cboEstado').value;
if(_iEstado == undefined || _iEstado == null || _iEstado == "")
_iEstado =0;
if (document.getElementById('rdoCarro').checked == true) {
CarregaXmlSelect('/ajax/ajax.ashx?metodo=pegamodelos&marcaid=' + valueMarca + '&tipo=0&uf=' + _iEstado, 'cboModelo');
}
else if (document.getElementById('rdoMoto').checked == true) {
CarregaXmlSelect('/ajax/ajax.ashx?metodo=pegamodelos&marcaid=' + valueMarca + '&tipo=1&uf=' + _iEstado, 'cboModelo');
}
document.FormBusca.hidMarcaID.value = valueMarca;
}
function PreencheMarca() {
    limpaCombo('cboMarca');
    limpaCombo('cboModelo');
    _iEstado = 0;
    if (document.getElementById('cboEstado').value != 0) _iEstado = document.getElementById('cboEstado').value;
    if (_iEstado == undefined || _iEstado == null || _iEstado == "")
        _iEstado = 0;
    if (document.getElementById('rdoCarro').checked == true) {
        CarregaXmlSelect('/ajax/ajax.ashx?metodo=pegamarcas&tipo=0&uf=' + _iEstado, 'cboMarca');
        document.getElementById('hidTipo').value = 0;
    }
    else if (document.getElementById('rdoMoto').checked == true) {
    CarregaXmlSelect('/ajax/ajax.ashx?metodo=pegamarcas&tipo=1&uf=' + _iEstado, 'cboMarca');
    document.getElementById('hidTipo').value = 1;
    }
}
function CarregaDestaques(_uf, _pg) {
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Seu navegador não suporta AJAX!");
        return;
    }


    xmlHttp.onreadystatechange = function() {
        switch (xmlHttp.readyState) {
            case 0: document.getElementById('dvDestaques').innerHTML ='Carregando.'; break;
            case 1: document.getElementById('dvDestaques').innerHTML ='Carregando..'; break;
            case 2: document.getElementById('dvDestaques').innerHTML ='Carregando...'; break;
            case 3: document.getElementById('dvDestaques').innerHTML ='Carregando....'; break;
        }
        if (xmlHttp.readyState == 4) {
            objSelect.options[0] = new Option('', 0);
            if (xmlHttp.responseText == undefined || xmlHttp.responseText == "" || xmlHttp.responseText == null)
                document.getElementById('dvDestaques').innerHTML = "";
            else   document.getElementById('dvDestaques').innerHTML =xmlHttp.responseText;
        }
    }
    xmlHttp.open("GET", '/ajax/ajax.ashx?metodo=destaques&pg='+_pg+'&uf=' + _uf, true);
    xmlHttp.send(null);
    return xmlHttp;


}
function PegaDestaques(cboDes) {
    CarregaDestaques(cboDes.value, 1);
 }
 function PaginaDestaques(cboPagina) {
     if (document.getElementById('cboDesEstado').value != 0) _iEstado = document.getElementById('cboDesEstado').value;
     CarregaDestaques(_iEstado, cboPagina.value); 
 }
function passanofin(anofin)
{
document.FormBusca.hidAnoFin.value = anofin - 1900;
}
function limpaComboMoto(combo)
{
if (document.getElementById(combo) != null)
document.getElementById(combo).options.length = 0;
}
function ir() {
var x = document.getElementById("cboMarca");
var xmodelo = document.getElementById("cboModelo");
var xst = document.getElementById("cboTipoCarro");
var textoBusca = "";
var _estado = "";
var rediIncom ="";
if (document.FormBusca.hidMarcaID.value != "0" && document.FormBusca.hidMarcaID.value != "") textoBusca += retirar_acento(x.options[x.selectedIndex].text);
if (document.FormBusca.hidModeloID.value != "0" && document.FormBusca.hidModeloID.value != "" && xmodelo.selectedIndex != "0" && xmodelo.selectedIndex != "-1" && xmodelo.options[xmodelo.selectedIndex].text != "Nenhum") textoBusca += "-" + retirar_acento(xmodelo.options[xmodelo.selectedIndex].text);
textoBusca = textoBusca.replace(" ","-");
if (x.selectedIndex != "0") {
if (document.FormBusca.hidMarcaID.value != "0" && document.FormBusca.hidMarcaID.value != "") rediIncom += "&maid=" + document.FormBusca.hidMarcaID.value;
if (document.FormBusca.hidModeloID.value != "0" && document.FormBusca.hidModeloID.value != "" && xmodelo.selectedIndex != "0" && xmodelo.selectedIndex != "-1") rediIncom += "&moid=" + document.FormBusca.hidModeloID.value;
if (document.FormBusca.hidAnoIni.value != "0" && document.FormBusca.hidAnoIni.value != "") rediIncom += "&anoini=" + document.FormBusca.hidAnoIni.value;
if (document.FormBusca.hidAnoFin.value != "0" && document.FormBusca.hidAnoFin.value != "") rediIncom += "&anofin=" + document.FormBusca.hidAnoFin.value;
if (document.FormBusca.hidPreIni.value != "0" && document.FormBusca.hidPreIni.value != "") rediIncom += "&preini=" + document.FormBusca.hidPreIni.value;
if (document.FormBusca.hidPreFin.value != "0" && document.FormBusca.hidPreFin.value != "") rediIncom += "&prefin=" + document.FormBusca.hidPreFin.value;
if (document.FormBusca.cboEstado.value != "0" && document.FormBusca.cboEstado.value != "") rediIncom += "&uf=" + document.FormBusca.cboEstado.value;
rediIncom += "&tp=" + document.FormBusca.hidTipo.value + "&st=" + xst.value;
rediIncom += "&pg=1";
window.location = "http://www.achacarro.com.br/busca.aspx?tag=" + textoBusca + rediIncom;
}
else {
 alert('Selecione uma marca.');
}
}
function imgNF(_obj) {
    if (_obj.src.match('sem-foto.png') == null) _obj.src = 'http://www.achacarro.com.br/fotos/sem-foto.png';
}
function abreVeiculo() {
    _o_Veiid = document.getElementById('txtVeiid');
   Veiid = document.getElementById('txtVeiid').value;
if (_o_Veiid.value == "")
{alert('Digite o Código do Veículo.');return; }
  xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Seu navegador não suporta AJAX!");
        return;
    }


    xmlHttp.onreadystatechange = function() {

        if (xmlHttp.readyState == 4) {
            if (xmlHttp.responseText == undefined || xmlHttp.responseText == "" || xmlHttp.responseText == null)
                alert("O veículo procurado parece não estar mais disponível \n ou o código digitado está errado.");
            else {
                window.location = xmlHttp.responseText;
            }
        } }
        xmlHttp.open("GET", '/ajax/ajax.ashx?metodo=cgveiculo&veiid=' + Veiid, true);
        xmlHttp.send(null);
        return xmlHttp;


   


}
function retirar_acento(palavra) {
    com_acento = 'áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ';
    sem_acento = 'aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC';
    nova = '';
    for (i = 0; i < palavra.length; i++) {
        if (com_acento.search(palavra.substr(i, 1)) >= 0) {
            nova += sem_acento.substr(com_acento.search(palavra.substr(i, 1)), 1);
        }
        else {
            nova += palavra.substr(i, 1);
        }
    }
    return nova;
}
