function bindComboVeiculo(iCodMontadora)
{
    var newOption=null;
    var ddl=null;
    var arrInterna=null;

	oCabAno		= window.document.getElementById(strIdDDDLAno);
	oCabMod		= window.document.getElementById(strIdDDDLModelo);
	oCabVeic	= window.document.getElementById(strIdDDDLVeiculo);
    
    ddl = document.getElementById(strIdDDDLVeiculo)
    if( oCabVeic != null)
    {
        limpaSelect(oCabVeic);
        limpaSelect(oCabAno);
        limpaSelect(oCabMod);
        if(V.length>0)
        {
            oCabVeic.options.add( new Option( "--Selecione--", 0 ) );
            for(i=0;i<V.length;i++)
            {
                arrInterna = V[i].split("|");
                if(arrInterna[4] == iCodMontadora)
                {
                    oCabVeic.options.add( new Option( arrInterna[0], arrInterna[1] + "-" + arrInterna[2] + "-" + arrInterna[3]) );
                }
            }
            oCabVeic.options.add( new Option( "---Não sei---", 0 ) );
        }
        oCabVeic.disabled = false;
        oCabAno.disabled = true;
        oCabMod.disabled = true;
    }
    
}

function bindComboAno()
{
	oCabAno		= window.document.getElementById(strIdDDDLAno);
	oCabMod		= window.document.getElementById(strIdDDDLModelo);
	oCabVeic	= window.document.getElementById(strIdDDDLVeiculo);
	
	oCabAno.length=0;
	oCabMod.length=0;
	oCabMod.options.add( new Option( "--Selecione--",0 ) )
	oCabMod.disabled = true;

	strValueVeic = oCabVeic[oCabVeic.selectedIndex].value
	
	if( strValueVeic.length > 0 && 
		strValueVeic.indexOf("-") != strValueVeic.lastIndexOf("-") )
	{
		oCabAno.disabled = false
		try
		{
			iAnoIni = 0;
			iAnoFim = 0;
			iAnoIni = parseInt( strValueVeic.substr( strValueVeic.indexOf("-")+1, 04 ) );
			iAnoFim = parseInt( strValueVeic.substr( strValueVeic.lastIndexOf("-")+1, 04 ) );
			if( iAnoFim == 9999 )
			{
				iAnoFim = parseInt( (new Date()).getFullYear() );
				oCabAno.options.add( new Option( "----", 0 ) );
			}
			else
			{
				oCabAno.options.add( new Option( "----", 0 ) );
			}
			if( iAnoIni > 0 )
			{
				for(i=iAnoFim; i>=iAnoIni; i--)
				{
					oCabAno.options.add( new Option( i.toString(), i ) );
				}
			}
			
		}
		catch( Err )
		{
			oCabAno.options.add( new Option( "----", 0 ) );
		}
	}
	else
	{
		oCabAno.add( new Option( "----", 0 ) );
		oCabAno.disabled = true
	}

}

function bindComboModelo(arg, context)
{
    arrModelo = arg.split("||");
    oCabMod		= window.document.getElementById(strIdDDDLModelo);
    
    if(oCabMod != null)
    {
        limpaSelect(oCabMod);
        if(arrModelo.length>0)
        {
            oCabMod.options.add( new Option( "--Selecione--", 0 ) );
            for(i=0;i<arrModelo.length;i++)
            {
                if(arrModelo[i].split("|")[0] != "")
                {
                    oCabMod.options.add( new Option( arrModelo[i].split("|")[0], arrModelo[i].split("|")[1] ) );
                }
            }
            oCabMod.options.add( new Option( "--Não sei--", 0 ) );
            oCabMod.disabled = false;
        }
        else
        {
            oCabMod.disabled = true;
        }
    }
}
function getParametroAJAXModelo()
{
	oCabAno		= window.document.getElementById(strIdDDDLAno);
	oCabVeic	= window.document.getElementById(strIdDDDLVeiculo);
	oCabMod		= window.document.getElementById(strIdDDDLModelo);
	
    strReturn = "";
	strReturn += oCabVeic[oCabVeic.selectedIndex].value;
	strReturn += "|";
	strReturn += oCabAno[oCabAno.selectedIndex].value;
	strReturn += "|";
	strReturn += oCabMod[oCabMod.selectedIndex].value;
    
    oCabMod.options.add( new Option( "Carregando...", "0" ) );
    oCabMod.disabled = true;
    return strReturn;
    
}
function TrataErro(arg, context)
{
    alert('Erro' + arg);
}

function Enter(event){

		iKey = KeyStroke( event );
		if (iKey == 13){
			executaBusca();
			return false;
		}
//		else if(iKey == 9)
//		{
//			window.document.getElementById(strIdBtnBuscar).focus();	
//			return true;
//		}
}

function LimpaCampos(strCampos)
{
        arrCampos = strCampos.split(",");
        for(i=0;i < arrCampos.length;i++)
        {
            objto = window.document.getElementById(arrCampos[i]);
            if( objto != null )
            {
                if (objto.tagName == 'INPUT') objto.value='';
                if (objto.tagName == 'SELECT')
                {
                    if (arrCampos[i] != 'cmbMontadora')
                    {
                        objto.length=0;
                        objto.options[objto.length] = new Option("--Selecione--",0);
                        objto.disabled= true;
                    }
                    objto.selectedIndex=0;
                }
            }
        }
}
function executaBusca()
{
	if (VdCabBusca())
	{
	    //TODO: mudar isso depois
		if( false )
		{
			strLnk =  "?F=1"
			strLnk +=  carregaFiltros(1);
			window.location.href = unescape(window.location.pathname) + strLnk;
			window.location.replace( unescape(window.location.pathname) + strLnk );
			window.location.reload();
		}
		else
		{
		    carregaFiltros(2);
		    window.document.forms[0].action = "index.aspx";
		    objFrm	= window.document.forms[0];
		    objFrm.submit();	
		}
	}
}
function addHidden(strNome,strValor)
{
    var input = null;
    if(document.getElementById(strNome) != null)
    {
        input = document.getElementById(strNome);
        input.value = strValor;
    }
    else
    {
        input = document.createElement("input");
        input.setAttribute("type", "hidden");
        input.setAttribute("id", strNome);
        input.setAttribute("name", strNome);
        input.setAttribute("value", strValor);
        document.forms[0].appendChild(input);
    }
}
function carregaFiltros(iOperacao)
{
		objMontadora	= window.document.getElementById(strIdDDDLMontadora);
		objVeiculo		= window.document.getElementById(strIdDDDLVeiculo);
		objAno			= window.document.getElementById(strIdDDDLAno);
		objModelo		= window.document.getElementById(strIdDDDLModelo);
		objDescricao	= window.document.getElementById(strIdTXTDescricao);

		strLnk = "";
		
		if( objMontadora != null && objMontadora.value != "0")
		{
		    if(iOperacao==1)
		    {
			    strLnk += "&CM=" + objMontadora.value
			}
			else
			{
			    addHidden("CM",objMontadora.value);
			}			
		}

		if( objVeiculo != null && objVeiculo.value != "0")
		{
		    if(iOperacao==1)
		    {
			    strLnk += "&CV=" + objVeiculo.value
			}
			else
			{
			    addHidden("CV",objVeiculo.value);
			}
			    
		}

		if( objAno != null && objAno.value != "0")
		{
		    if(iOperacao==1)
		    {
			    strLnk += "&ANO=" + objAno.value
			}
			else
			{
			    addHidden("ANO",objAno.value);
			}
		}

		if( objModelo != null && objModelo.value != "0")
		{
		    if(iOperacao==1)
		    {
			    strLnk += "&CMD=" + objModelo.value
			}
			else
			{
			    addHidden("CMD",objModelo.value);
			}
		}

		if( objDescricao != null && objDescricao.value != "0")
		{
			
			if(iOperacao==1)
		    {
			    strLnk += "&NSN=" + objDescricao.value
			}
			else
			{
			    addHidden("NSN",objDescricao.value);
			}
		}
		
		addHidden("BUSCA","1");

		return strLnk
}
function VdCabBusca()
{

    return true;
    
//	objmontadora	= window.document.getelementbyid(striddddlmontadora);
//	objveiculo		= window.document.getelementbyid(striddddlveiculo);
//	objano			= window.document.getelementbyid(striddddlano);
//		
//    if (objmontadora.value == 0)
//    {
//		alert('por favor, para a busca poder ser feita, preencha a montadora, o veículo e o ano.');
//		return false;
//    }else{
//		if (objveiculo.value == 0)
//		{
//			alert('por favor, para a busca poder ser feita, preencha a montadora, o veículo e o ano.');
//			return false;
//		}
//		else
//		{
//			if (objano.value == 0 && objano.length > 1)
//			{
//				alert('por favor, para a busca poder ser feita, preencha a montadora, o veículo e o ano.');
//				return false;
//			}
//			else
//			{
//				return true;
//			}
//		}
//    }
}
