﻿function AcertarBuscaAno(buscaEspecifica, comboAnodeEsp, comboAnoateEsp, comboAnodeCarac, comboAnoateCarac, nomePainel, nomeDivTitulo, textoDivDescricaoExibe, textoDivDescricaoEsconde, liAnoCarac)
{
    if (buscaEspecifica)
    {
        if (document.getElementById(comboAnodeCarac).value != '')
            selecionarCombo(document.getElementById(comboAnodeEsp), document.getElementById(comboAnodeCarac).value);
        if (document.getElementById(comboAnoateCarac).value != '')
            selecionarCombo(document.getElementById(comboAnoateEsp), document.getElementById(comboAnoateCarac).value);
        document.getElementById(comboAnodeCarac).options[0].selected = true;
        document.getElementById(comboAnoateCarac).options[0].selected = true;
        document.getElementById(liAnoCarac).style.display = 'none';
    }
    else
    {
        var inserido = false;
        if (document.getElementById(comboAnodeEsp).value != '')
        {
            selecionarCombo(document.getElementById(comboAnodeCarac), document.getElementById(comboAnodeEsp).value);
            inserido = true;
        }
        if (document.getElementById(comboAnoateEsp).value != '')
        {
            selecionarCombo(document.getElementById(comboAnoateCarac), document.getElementById(comboAnoateEsp).value);
            inserido = true;
        }
        if (inserido)
        {
            document.getElementById(nomePainel).style.display = '';
            defineTextoDiv(nomeDivTitulo, textoDivDescricaoExibe);
        }
        document.getElementById(comboAnodeEsp).options[0].selected = true;
        document.getElementById(comboAnoateEsp).options[0].selected = true;
        document.getElementById(liAnoCarac).style.display = '';
    }
}

function ValidaQuilometragem(cboQuilometragem, cboQuilometragemAte)
{
    // inverte as quilometragens, se necessário
    var Inicial = document.getElementById(cboQuilometragem);
    var Final = document.getElementById(cboQuilometragemAte);

    if (parseInt(Inicial.value) > parseInt(Final.value))
    {
        var x = Inicial.value;
        var y = Final.value;

        Inicial.value = ''; 
        Final.value = '';

        Inicial.value = y;
        Final.value = x;
    }
}

function ValidarAnoBuscaEspecifica(cboAno,cboAnoAte, cboAnoCarc, cboAnoCarcAte)
{

   var Inicial = document.getElementById(cboAno);
   var Final = document.getElementById(cboAnoAte);
   var valorInicial;
   var valorFinal;
   var CboAno = document.getElementById(cboAnoCarc);
   var cboAnoCarc = document.getElementById(cboAnoCarcAte);
	 	 
    for(var i = 0; i < Inicial.options.length; i++)
    {
        if (Inicial.options[i].selected == true) 
        {
            valorInicial = Inicial.options[i].text;
        }
    } 
    for(var i = 0; i < Final.options.length; i++)
    {
        if (Final.options[i].selected == true) 
        {
            valorFinal = Final.options[i].text;
        }
    }

    if (parseInt(valorInicial) > parseInt(valorFinal))
    {
        var x = Inicial.value;
        var y = Final.value;

        Inicial.value = ''; 
        Final.value = '';

        Inicial.value = y;
        Final.value = x;
    }
}

function ValidarAno(cboAno,cboAnoAte)
{
    var Inicial = document.getElementById(cboAno);
    var Final = document.getElementById(cboAnoAte);
    var valorInicial;
    var valorFinal;

    for(var i = 0; i < Inicial.options.length; i++)
    {
        if (Inicial.options[i].selected == true) 
        {
            valorInicial = Inicial.options[i].text;
        }
    } 
    for(var i = 0; i < Final.options.length; i++)
    {
        if (Final.options[i].selected == true) 
        {
            valorFinal = Final.options[i].text;
        }
    }

    if (parseInt(valorInicial) > parseInt(valorFinal))
    {
        var x = Inicial.value;
        var y = Final.value;

        Inicial.value = ''; 
        Final.value = '';

        Inicial.value = y;
        Final.value = x;
    }
}

function ValidarPreco(txtPreco,txtPrecoFinal)
{
	// inverte os faixa de preco, se necessário
    var Inicial = document.getElementById(txtPreco);
    var Final = document.getElementById(txtPrecoFinal);

    if (parseFloat(Inicial.value) > parseFloat(Final.value))
    {
        var x = Inicial.value;
        var y = Final.value;

        Inicial.value = ''; 
        Final.value = '';

        Inicial.value = y;
        Final.value = x;
    }
}

function ChecarDropDownAnos(comboAno,comboAnoAte,comboCarcAno,comboAnoCarcAnoAte)
{
	var comboAno = document.getElementById(comboAno);
	var comboAnoAte = document.getElementById(comboAnoAte); 
	var comboCarcAno = document.getElementById(comboCarcAno);
	var comboAnoCarcAnoAte = document.getElementById(comboAnoCarcAnoAte);
	
	if(comboAno.options[0].selected == false && comboAnoAte.options[0].selected == false)
	{
		comboCarcAno.disabled  = true;
		comboAnoCarcAnoAte.disabled  = true; 
		comboCarcAno.options[0].selected = true;
		comboAnoCarcAnoAte.options[0].selected  = true;
	}
	else
	{
		comboCarcAno.disabled  = false;
		comboAnoCarcAnoAte.disabled  = false; 
		comboCarcAno.options[0].selected = true;
		comboAnoCarcAnoAte.options[0].selected  = true;
	} 
}

function lstDblClicked(lst,txt,hdn)
{
	var lista = document.getElementById(lst);
	var texto = document.getElementById(txt);
	var hidden = document.getElementById(hdn);
	
	for (var i = 0; i < lista.options.length; i++)
	{
		if (lista.options[i].selected == true) 
		{
			texto.value = lista.options[i].text;
			hidden.value = lista.options[i].value;
			texto.focus();
			tb_remove();
		}
	}
}

function ApagarOverVersao(txt)
{
	var txtbox = document.getElementById(txt);
	if(txtbox.value == 'Digite aqui')
	{
		txtbox.value = ''; 
	}
	else 
	{
		return true;
	}
}


function exibirMsgErro()
{
	tb_showThickbox('Excluir', '#TB_inline?width=243&height=135&inlineId=modalPreencherPreco', false, 'frmUI');
}

function copiaValorCombo(origem,destino)
{
	var lista = document.getElementById(origem);
	var texto = document.getElementById(destino);
	
	for (var i = 0; i < lista.options.length; i++)
		if (lista.options[i].selected == true) 
			texto.value = lista.options[i].text;
}

function AtualizarBuscaVersao(controleAutoComplete, valorModelo, valorAnoDe, valorAnoAte)
{
	var valor = "codModelo:" + valorModelo + "|" + "AnoIni:" + valorAnoDe + "|" + "AnoFim:" + valorAnoAte
	$find(controleAutoComplete).set_contextKey(valor);
}

function exibePaineisTipoBusca(painelBuscaEspecifica, exibeBuscaEspecifica, 
                               painelBuscaCarroceria, exibeBuscaCarroceria, 
                               painelBuscaTipoNecessidade, exibeBuscaTipoNecessidade,
                               rd1, rd2, rd3) {
    exibePaineis(painelBuscaEspecifica, exibeBuscaEspecifica);
    exibePaineis(painelBuscaCarroceria, exibeBuscaCarroceria);
    exibePaineis(painelBuscaTipoNecessidade, exibeBuscaTipoNecessidade);
    document.getElementById(rd1).checked = true;
    document.getElementById(rd2).checked = false;
    document.getElementById(rd3).checked = false;
}

function exibePainelVersao(mostrar) {
    if(mostrar)
        document.getElementById('itemVersao').style.display = 'block';
    else
        document.getElementById('itemVersao').style.display = 'none';
        
}

function limpaPaineisTipoBusca(hid1, hid2) {
    
    var obj = document.getElementById(hid1);
    var idArray = obj.value.split(',');

    obj = document.getElementById(hid2);
    idArray = idArray.concat(idArray, obj.value.split(','));
    
    for (var i = 0; i < idArray.length; i++) 
    {
        if (idArray[i] != '') 
        {
            var control = document.getElementById(idArray[i]);

            switch (control.type) 
            {
                case "text":
                    control.value = "";
                    break;
                case "checkbox":
                    control.checked = false;
                    break;
                case "select-one":
                    control.selectedIndex = 0;
                    break;
            }
        }
    }
    if (document.getElementById('itemVersao').value == "")
        document.getElementById('itemVersao').value = "Digite aqui";
}
function limpaTudo(hid1) {

    var obj = document.getElementById(hid1);
    var idArray = obj.value.split(',');

    for (var i = 0; i < idArray.length; i++) {
        if (idArray[i] != '') {
            var control = document.getElementById(idArray[i]);

            switch (control.type) {
                case "text":
                    control.value = "";
                    break;
                case "checkbox":
                    control.checked = false;
                    break;
                case "select-one":
                    control.selectedIndex = 0;
                    break;
            }
        }
    }
    if (document.getElementById('itemVersao').value == "")
        document.getElementById('itemVersao').value = "Digite aqui";
}

function limpaHidden(hid1) {
    document.getElementById(hid1).value = "";
}

function limpaNavegacaoAbas(hid1, hid2) {

    var obj = document.getElementById(hid1);
    var idArray = obj.value.split(',');

    obj = document.getElementById(hid2);
    idArray = idArray.concat(idArray, obj.value.split(','));

    for (var i = 0; i < idArray.length; i++) {
        if (idArray[i] != '') {
            var control = document.getElementById(idArray[i]);

            switch (control.type) {
                case "text":
                    control.value = "";
                    break;
                case "checkbox":
                    control.checked = false;
                    break;
                case "select-one":
                    control.selectedIndex = 0;
                    break;
            }
        }
    }

   
}

function exibeEscondePainelOpcional(nomePainel, nomeDivTitulo, textoDivDescricaoExibe, textoDivDescricaoEsconde) {
    var exibe = (document.getElementById(nomePainel).style.display == 'none');
    
    exibePaineis(nomePainel, exibe);
    if (exibe)
    {
        defineTextoDiv(nomeDivTitulo, textoDivDescricaoExibe);
        document.getElementById(nomeDivTitulo).className = 'on';
    }
    else
    {
        defineTextoDiv(nomeDivTitulo, textoDivDescricaoEsconde);
        document.getElementById(nomeDivTitulo).className = 'off';
    }
}

function exibePaineis(nomePainel, exibe) {
    if (exibe)
        document.getElementById(nomePainel).style.display = '';
    else
        document.getElementById(nomePainel).style.display = 'none';
}

function defineTextoDiv(nomeDiv, descricao) {
	if(document.all)
		document.getElementById(nomeDiv).innerHTML = descricao;
	else
		document.getElementById(nomeDiv).textContent = descricao;
}

function controleDisabled(nomeControle, disabled) {
    if (document.getElementById(nomeControle) != null)
        document.getElementById(nomeControle).disabled = disabled;
}

function soNumero(objeto){
	var keypress=event.keyCode;
	var campo=eval(objeto);
	var caracteres='01234567890';
	if((caracteres.indexOf(String.fromCharCode(keypress))!=-1))
		event.returnValue=true;
	else 
		event.returnValue=false;
}

function selecionarCombo(combo, valor)
{
    for (i=0; i<combo.options.length; i++)
    {
        if (combo.options[i].value == valor)
        {
            combo.options[i].selected = true;
            break;
        }
    }
}

function selecionarCheckBox(chks, checked)
{
	var itens = document.getElementById(chks).value.split(',');
	for (i=0; i<itens.lenght; i++)
	{
		if (document.getElementById(itens[i]) != null)
			document.getElementById(itens[i]).checked = checked;
	}
}