// JavaScript Document
function mudaitembusca(cod){
	var posicaoX = new Array("","0px","0px","0px","0px","144px","144px","144px","144px");
	var posicaoY = new Array("","0px","204px","136px","68px","0px","204px","136px","68px");
	var titArray = new Array("","LANCHAS","VELEIROS","JET SKI","MOTORES","INFLÁVEIS","CASCOS","ACESSÓRIOS","OUTROS");
	var codItem = new Array("",1,8,3,101,7,37,100,102);
	document.getElementById('codsub').value = codItem[cod];
	for (ii=1;ii<=8;ii++){
		if (cod == ii){
			document.getElementById('btn'+ii).innerHTML = "<div style='width:144;height:68;background-image:url(imagens_buscador/btn_busca_press_maior.jpg);background-position:"+posicaoX[ii]+" "+posicaoY[ii]+";'></div>";
		} else {
			document.getElementById('btn'+ii).innerHTML = "<a href='javascript:mudaitembusca("+ii+")' class='botoesClassi' style='background-position:"+posicaoX[ii]+" "+posicaoY[ii]+";'></a>";
		}
	}
	procuraTipos();
	if (cod == 7){
		document.getElementById('marca').style.display = "none";
		document.getElementById('categoria').style.display = "block";
		document.getElementById('buscaItem').innerHTML = "Categorias";
	} else if(cod == 8){
		document.getElementById('marca').style.display = "none";
		document.getElementById('categoria').style.display = "block";
		document.getElementById('buscaItem').innerHTML = "Categorias";
	} else {
		document.getElementById('marca').style.display = "block";
		document.getElementById('categoria').style.display = "none";
		document.getElementById('buscaItem').innerHTML = "Marcas";
	}
	document.getElementById('titBusca').innerHTML = titArray[cod];
}
function procuraTipos(){
	var HttpReq = createXMLHTTP();
	HttpReq.open("GET", "buscador/veiculo_tipos.php", true);
	HttpReq.onreadystatechange=function(){
		procuraEstados('');
		procuraCidades('');
		procuraMarcasCategorias('');
		if (HttpReq.readyState == 4 && HttpReq.status == 200){
        			var result = HttpReq.responseXML;
        			var subcategorias = result.getElementsByTagName("nome");
					document.getElementById('tipo').disabled = false;
        			document.getElementById('tipo').innerHTML = "";
        			for (var i = 0; i < subcategorias.length; i++) {
            			new_opcao = create_opcao(subcategorias[i]);
            			document.getElementById('tipo').appendChild(new_opcao);
        			}
					
    	} else {
			document.getElementById('tipo').disabled = true;
			document.getElementById('tipo').innerHTML = "<option value='nada'>Cargando..</option>";
		}
	}
	HttpReq.send(null);
}
function create_carregando() { 
    var new_opcao = document.createElement("option"); 
    new_opcao.setAttribute("value",""); 
    new_opcao.appendChild("Carregando"); //Adiciona o texto a OPTION.
   return new_opcao; // Retorna a nova OPTION.
}
function procuraEstados(tipo){
	var codsub = document.getElementById('codsub').value;
	var HttpReq = createXMLHTTP();
	HttpReq.open("GET", "buscador/veiculo_estado.php?tipo=" + tipo + "&subcategoria=" + codsub, true);
	HttpReq.onreadystatechange=function(){
		if (HttpReq.readyState == 4 && HttpReq.status == 200){
        			var result = HttpReq.responseXML;
        			var subcategorias = result.getElementsByTagName("nome");
					document.getElementById('estado').disabled = false;
        			document.getElementById('estado').innerHTML = "";
        			for (var i = 0; i < subcategorias.length; i++) {
            			new_opcao = create_opcao(subcategorias[i]);
            			document.getElementById('estado').appendChild(new_opcao);
        			}
					var valor = subcategorias[0].getAttribute("cod");
					procuraCidades(valor);
    	} else {
			document.getElementById('estado').disabled = true;
			document.getElementById('estado').innerHTML = "<option value='nada'>Cargando..</option>";
		}
	}
	HttpReq.send(null);
}
function procuraCidades(estado){
	var codsub = document.getElementById('codsub').value;
	var tipo = document.getElementById('tipo').value;
	var HttpReq = createXMLHTTP();
	HttpReq.open("GET", "buscador/veiculo_cidade.php?tipo=" + tipo + "&codsub=" + codsub + "&estado=" + estado, true);
	HttpReq.onreadystatechange=function(){
		if (HttpReq.readyState == 4 && HttpReq.status == 200){
        			var result = HttpReq.responseXML;
        			var subcategorias = result.getElementsByTagName("nome");
					document.getElementById('cidade').disabled = false;
        			document.getElementById('cidade').innerHTML = "";
        			for (var i = 0; i < subcategorias.length; i++) {
            			new_opcao = create_opcao(subcategorias[i]);
            			document.getElementById('cidade').appendChild(new_opcao);
        			}
					var valor = subcategorias[0].getAttribute("cod");
					procuraMarcasCategorias(valor);
    	} else {
			document.getElementById('cidade').disabled = true;
			document.getElementById('cidade').innerHTML = "<option value='nada'>Cargando..</option>";
		}
	}
	HttpReq.send(null);
}
function procuraMarcasCategorias(cidade){
	var codsub = document.getElementById('codsub').value;
	var tipo = document.getElementById('tipo').value;
	var HttpReq = createXMLHTTP();
	if (codsub == 100){
		var codcat = 2;
		HttpReq.open("GET", "buscador/geral_categoria.php?tipo=" + tipo + "&cidade=" + cidade + "&codcat=" + codcat, true);
		HttpReq.onreadystatechange=function(){
			if (HttpReq.readyState == 4 && HttpReq.status == 200){
        			var result = HttpReq.responseXML;
        			var subcategorias = result.getElementsByTagName("nome");
					document.getElementById('categoria').disabled = false;
        			document.getElementById('categoria').innerHTML = "";
        			for (var i = 0; i < subcategorias.length; i++) {
            			new_opcao = create_opcao(subcategorias[i]);
            			document.getElementById('categoria').appendChild(new_opcao);
        			}
    		} else {
			document.getElementById('categoria').disabled = true;
			document.getElementById('categoria').innerHTML = "<option value='nada'>Cargando..</option>";
			}
		}
	} else if (codsub == 102){
		var codcat = 6;
		HttpReq.open("GET", "buscador/geral_categoria.php?tipo=" + tipo + "&cidade=" + cidade + "&codcat=" + codcat, true);
		HttpReq.onreadystatechange=function(){
			if (HttpReq.readyState == 4 && HttpReq.status == 200){
        			var result = HttpReq.responseXML;
        			var subcategorias = result.getElementsByTagName("nome");
					document.getElementById('categoria').disabled = false;
        			document.getElementById('categoria').innerHTML = "";
        			for (var i = 0; i < subcategorias.length; i++) {
            			new_opcao = create_opcao(subcategorias[i]);
            			document.getElementById('categoria').appendChild(new_opcao);
        			}
    		} else {
			document.getElementById('categoria').disabled = true;
			document.getElementById('categoria').innerHTML = "<option value='nada'>Cargando..</option>";
			}
		}
	} else if (codsub == 101){
		HttpReq.open("GET", "buscador/motor_marca.php?tipo=" + tipo + "&cidade=" + cidade, true);
		HttpReq.onreadystatechange=function(){
			if (HttpReq.readyState == 4 && HttpReq.status == 200){
        			var result = HttpReq.responseXML;
        			var subcategorias = result.getElementsByTagName("nome");
					document.getElementById('marca').disabled = false;
        			document.getElementById('marca').innerHTML = "";
        			for (var i = 0; i < subcategorias.length; i++) {
            			new_opcao = create_opcao(subcategorias[i]);
            			document.getElementById('marca').appendChild(new_opcao);
        			}
    		} else {
			document.getElementById('marca').disabled = true;
			document.getElementById('marca').innerHTML = "<option value='nada'>Cargando..</option>";
			}
		}
	} else {
		HttpReq.open("GET", "buscador/veiculo_marca.php?tipo=" + tipo + "&subcategoria=" + codsub + "&cidade=" + cidade, true);
		HttpReq.onreadystatechange=function(){
			if (HttpReq.readyState == 4 && HttpReq.status == 200){
        			var result = HttpReq.responseXML;
        			var subcategorias = result.getElementsByTagName("nome");
					document.getElementById('marca').disabled = false;
        			document.getElementById('marca').innerHTML = "";
        			for (var i = 0; i < subcategorias.length; i++) {
            			new_opcao = create_opcao(subcategorias[i]);
            			document.getElementById('marca').appendChild(new_opcao);
        			}
    		} else {
			document.getElementById('marca').disabled = true;
			document.getElementById('marca').innerHTML = "<option value='nada'>Cargando..</option>";
			}
		}
	}
	
	HttpReq.send(null);
}
function validar(Busca2){
	var d = document.Busca2;
	if (d.tipo.value == '0'){alert("Por favor, selecionar tipo de produto.");d.tipo.focus();return (false);}
	if (d.estado.value == ''){alert("Esta opção não tem resultados.");d.estado.focus();return (false);}
	return (true);
}
