function cad_veiculo (campo, valor) {
	
	if(campo == "tipo_veiculo") {
		
		url = "adm/bd/chama_marca.php?id_tipo_veiculo="+valor;
		Dados(url, campo);
		
	} else if (campo == "marca") {
		
		var tipo_veiculo = document.getElementById("vender-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);

	} else if (campo == "versao") {

		var id_modelo = document.getElementById("comprar-modelo").value;
		var id_versao = document.getElementById("comprar-versao").value;
		url = "adm/bd/chama_ano_versao.php?id_modelo="+id_modelo+"&id_versao="+id_versao;
//		document.write(url);
		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_veiculo") {

//		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;
			document.getElementById('comprar-ano-versoes').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-ano-versoes").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-ano-versoes").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-ano-versoes").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;

		  }

	} else if (campo == "versao") {

		if(dataArrays.length > 0) {
				
			document.getElementById("comprar-ano-versoes").options.length = 1;
	
			for(var i = 0 ; i < dataArrays.length ; i++) {
				
				var item = dataArrays[i];
	
				var id = item.getElementsByTagName("id")[0].firstChild.nodeValue;
				var anos = item.getElementsByTagName("anos_versao")[0].firstChild.nodeValue;
				
				anos_array = anos.split(";");
				
				for (vkk = 0; vkk < anos_array.length ; vkk++){
					option = document.createElement("option");
					option.setAttribute("value",anos_array[vkk]);
					option.appendChild(document.createTextNode(anos_array[vkk]));
					document.getElementById("comprar-ano-versoes").appendChild(option);
				}
				
	
			}
		
		  } else {
		  	
	  			document.getElementById("comprar-ano-versoes").options.length = 1;
			
		  }
		
	}

}


/*#####################################################################################
#######################################################################################
############################### FUNÇÕES DE VALIDAÇÃO ##################################
#######################################################################################
#####################################################################################*/

function FormataValor(campo,tammax,teclapres) {

	var tecla = teclapres.keyCode;

    caracteres = '01234567890';

	if ((caracteres.search(String.fromCharCode(tecla))!=-1) || tecla == 8 || tecla == 9 ||(tecla >= 96 && tecla <= 105)){
        var vr = campo.value;
    	vr = vr.replace( "/", "" );
    	vr = vr.replace( "/", "" );
    	vr = vr.replace( ",", "" );
    	vr = vr.replace( ".", "" );
    	vr = vr.replace( ".", "" );
    	vr = vr.replace( ".", "" );
    	vr = vr.replace( ".", "" );
    	tam = vr.length;
  
    
    	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
    
    	if (tecla == 8 ){	tam = tam - 1 ; }
    		
    	if ( tecla == 8 || (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105) ){
    		if ( tam <= 2 ){ 
    	 		campo.value = vr ; }
    	 	if ( (tam > 2) && (tam <= 5) ){
    	 		campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
    	 	if ( (tam >= 6) && (tam <= 8) ){
    	 		campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
    	 	if ( (tam >= 9) && (tam <= 11) ){
    	 		campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
    	 	if ( (tam >= 12) && (tam <= 14) ){	
    	 		campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
    	 	if ( (tam >= 15) && (tam <= 17) ){
    	 		campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}
    	}
     }else{
     	
       event.returnValue = false;   
           
     }   
}

function verifica_selecao() {
	
	var tipo_veiculo = document.getElementById("vender-tipo").value;
	var marca_veiculo = document.getElementById("comprar-marca").value;
	var modelo_veiculo = document.getElementById("comprar-modelo").value;
	var versao_veiculo = document.getElementById("comprar-versao").value;
	var ano_versao = document.getElementById("comprar-ano-versoes").value;
	
	var dadosArray = new Array();
	
	dadosArray[0] = tipo_veiculo;
	dadosArray[1] = marca_veiculo;
	dadosArray[3] = modelo_veiculo;
	dadosArray[4] = versao_veiculo;
	dadosArray[5] = ano_versao;
	
	var erro = 0;
	for (vkk = 0; vkk < dadosArray.length ; vkk++){
		if (dadosArray[vkk] == ""){
			erro++;
		}
	}
	
	if (erro > 0){
		alert("Por favor, preencha todos os dados do veículo.");
	} else {
		document.getElementById("form-vender").submit();
	}
 	
}

function usados_sel (id_pai,funct,id_prox){
	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;
			}
		}
	}
	if(ajax) {
		ajax.open("GET", "inc/ajax_site/avalie_home.php?funcao="+funct+"&id_pai="+id_pai, true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.onreadystatechange = function() {
		    if(ajax.readyState == 4 ) {
		    	if(ajax.responseXML) {
					var obj = ajax.responseXML;
					var dataArrays = obj.getElementsByTagName("resultado");
					if(dataArrays.length > 0) {
						
						document.getElementById(id_prox).options.length = 1;
						
						for(var i = 0 ; i < dataArrays.length ; i++) {
							var item = dataArrays[i];
							
							var id = item.getElementsByTagName("id")[0].firstChild.nodeValue;
							var valor = item.getElementsByTagName("valor")[0].firstChild.nodeValue;
							
							if(id != 0 && valor != 0){
								option = document.createElement("option");
								option.setAttribute("value",id);
								option.appendChild(document.createTextNode(valor));
								
								document.getElementById(id_prox).appendChild(option);
							}
						}
					}
		    	}
		    }
		}
		ajax.send(null);
	}
}
