function cad_veiculo (campo, valor) {
	
	if(campo == "tipo") {
		
		url = "adm/bd/chama_marca.php?id_tipo_veiculo="+valor;
		Dados(url, campo);
		
	} else if (campo == "marca") {
		
		var tipo_veiculo = document.getElementById("comprar-tipo").value;

		url = "adm/bd/chama_modelo.php?id_marca_veiculo="+valor+"&id_tipo="+tipo_veiculo;
		Dados(url, campo);
		
	} else if (campo == "modelo") {

		url = "adm/bd/chama_versao.php?id_modelo="+valor;
		Dados(url, campo);

	}

}

function Dados(valor, campo) {
	//verifica se o browser tem suporte a ajax
	try {
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(ex) {
			try {
				ajax = new XMLHttpRequest();
			}
			catch(exc) {
				alert("Esse browser não tem recursos para uso do Ajax");
				ajax = null;
			}
		}
	}

	//se tiver suporte ajax
	if(ajax) {

		idloading_ajax = document.getElementById("loading_ajax");

		ajax.open("GET", valor, true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

		ajax.onreadystatechange = function() {
			//após ser processado - chama função processXML que vai varrer os dados
		    if(ajax.readyState == 4 ) {
		    	if(ajax.responseXML) {
		    		processXML_ramo(ajax.responseXML,campo);
		    	}else {
		    		//caso não seja um arquivo XML emite a mensagem abaixo
		    		idloading_ajax.value = "Selecione o Produto";
		    	}
		    }
		}

		ajax.send(null);

	} // Fecha function Ajax

} // Fecha function Dados
   
function processXML_ramo(obj,campo){
	
	 // pega a tag resultado
	var dataArrays = obj.getElementsByTagName("resultado");
	
	if (campo == "tipo") {

//		alert(dataArrays.length);
		
		if(dataArrays.length > 0) {
				
			document.getElementById('comprar-marca').options.length = 1;
			document.getElementById('comprar-modelo').options.length = 1;
			document.getElementById('comprar-versao').options.length = 1;

			for(var i = 0 ; i < dataArrays.length ; i++) {

				var item = dataArrays[i];

				var marca = item.getElementsByTagName("marca")[0].firstChild.nodeValue;
				var id = item.getElementsByTagName("id")[0].firstChild.nodeValue;
				
				option = document.createElement("option");
				option.setAttribute("value",id);
				option.appendChild(document.createTextNode(marca));
				
				document.getElementById('comprar-marca').appendChild(option);
	
			}
		
		  }else{
		  	
	  			document.getElementById('comprar-marca').options.length = 1;
	  			document.getElementById('comprar-modelo').options.length = 1;
	  			document.getElementById("comprar-versao").options.length = 1;
		
		  }
		
	} else if (campo == "marca") {

//		alert(dataArrays.length);

			if(dataArrays.length > 0) {

			document.getElementById('comprar-modelo').options.length = 1;
			document.getElementById("comprar-versao").options.length = 1;

			for(var i = 0 ; i < dataArrays.length ; i++) {

				var item = dataArrays[i];

				var modelo = item.getElementsByTagName("modelo")[0].firstChild.nodeValue;
				var id = item.getElementsByTagName("id")[0].firstChild.nodeValue;

				option = document.createElement("option");
				option.setAttribute("value",id);
				option.appendChild(document.createTextNode(modelo));

				document.getElementById('comprar-modelo').appendChild(option);

			}
		
		  }else{
		  	
	  			document.getElementById('comprar-modelo').options.length = 1;
	  			document.getElementById("comprar-versao").options.length = 1;
		
		  }
		
	} else if (campo == "modelo") {
		
		if(dataArrays.length > 0) {
				
			document.getElementById("comprar-versao").options.length = 1;
	
			for(var i = 0 ; i < dataArrays.length ; i++) {

				var item = dataArrays[i];

				var versao = item.getElementsByTagName("versao")[0].firstChild.nodeValue;
				var id = item.getElementsByTagName("id")[0].firstChild.nodeValue;

				option = document.createElement("option");
				option.setAttribute("value",id);
				option.appendChild(document.createTextNode(versao));

				document.getElementById("comprar-versao").appendChild(option);

			}

		  }else{

	  			document.getElementById("comprar-versao").options.length = 1;

		  }

	}

}

function verifica_veiculos() {
	
	var tipo_veiculo = document.getElementById("comprar-tipo").value;
	var marca_veiculo = document.getElementById("comprar-marca").value;
	var modelo_veiculo = document.getElementById("comprar-marca").value;
	var versao_veiculo = document.getElementById("comprar-versao").value;
	
	var arrayDados = new Array();
	
	arrayDados[0] = tipo_veiculo;
	arrayDados[1] = marca_veiculo;
	arrayDados[2] = modelo_veiculo;
	arrayDados[3] = versao_veiculo;
	
	var erro = 0;
	for (i = 0 ; i < arrayDados.length ; i++){
		if(arrayDados[i] == ""){
			erro++;
		}
	}
	
	if (erro > 0){
		alert("Preencha ao menos a versão.");
	} else {
		document.getElementById("busca-veiculos").submit();
	}
	
}
