//##############################################################################################################################
//http://www.hospedia.com.br/artigos/1/ajax/1/alimentando_uma_combobox_dinamicamente_utilizando_tecnicas_ajax.html
//http://www.hospedia.com.br/exemplos/artigos/ajax/01_cidades.php?uf=AC
//http://www.hospedia.com.br/exemplos/artigos/ajax/01.js
//http://www.mredkj.com/tutorials/tutorial006.html
var HttpReq = null;
var dest_combo = null;
function ajaxComboBox(comboOrigem, comboDestino, url){
    dest_combo = comboDestino;
    var indice = document.getElementById(comboOrigem).selectedIndex;
    var codigo = document.getElementById(comboOrigem).options[indice].getAttribute('value');
    url = url + codigo;
    if (document.getElementById) { //Verifica se o Browser suporta DHTML.
        if (window.XMLHttpRequest) {
            HttpReq = new XMLHttpRequest();
            HttpReq.onreadystatechange = XMLHttpRequestChange;
			document.getElementById('div_carregando').innerHTML = "&nbsp;Carregando...";
            HttpReq.open("GET", url, true);
            HttpReq.send(null);
        } else if (window.ActiveXObject) {
            HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
            if (HttpReq) {
                HttpReq.onreadystatechange = XMLHttpRequestChange;
				document.getElementById('div_carregando').innerHTML = "&nbsp;Carregando...";
                HttpReq.open("GET", url, true);
                HttpReq.send();
            }
        }
    }
	document.getElementById('div_carregando').innerHTML = "&nbsp;Carregando...";
}

function XMLHttpRequestChange() {
    if (HttpReq.readyState == 4 && HttpReq.status == 200){  //Verifica se o arquivo foi carregado com sucesso.
        var result = HttpReq.responseXML;
        var resultados = result.getElementsByTagName("nome");
        document.getElementById(dest_combo).innerHTML = "";
		for (var i = 0; i < resultados.length; i++) {
            new_opcao = create_opcao(resultados[i]);
            document.getElementById(dest_combo).appendChild(new_opcao);
        }
		document.getElementById('div_carregando').innerHTML = "";
    }
}

function create_opcao(resultado) { //Cria um novo elemento OPTION.
    //return opcao.cloneNode(true);
	document.getElementById('div_carregando').innerHTML = "&nbsp;Carregando.";
    var new_opcao = document.createElement("option"); //Cria um OPTION.
    var texto = document.createTextNode(resultado.childNodes[0].data); //Cria um texto.
    new_opcao.setAttribute("value",resultado.getAttribute("id")); //Adiciona o atributo de valor a nova opção.
    new_opcao.appendChild(texto); //Adiciona o texto a OPTION.
    return new_opcao; // Retorna a nova OPTION.
}
//##############################################################################################################################
//##############################################################################################################################


//##############################################################################################################################
function ativararea(codigo){

	switch(codigo){
		case "1":
			resetcampos();
			display_tipo('div_agro','block');
			break;
		  
		case "2":
			resetcampos();
			display_tipo('div_maquinas','block');
			break;
		  
		case "4":
			resetcampos();
			display_tipo('div_caminhoes','block');
			break;
		  
		case "6":
			resetcampos();
			display_tipo('div_diversos','block');
			break;
		  
		default:
			resetcampos();
	}

	ajaxComboBox('tx_grupoacodigo','tx_grupobcodigo','produto_xml.asp?grupoacodigo=');

	// zerando o combo 2
	var elSel = document.getElementById('tx_grupoccodigo');
	var i;
	for (i = elSel.length - 1; i>=0; i--) {
		elSel.remove(i);
	}
}


function ativarareapesquisa(codigo){

	ajaxComboBox('tx_grupoacodigo','tx_grupobcodigo','produto_xml.asp?grupoacodigo=');

	// zerando o combo 2
	var elSel = document.getElementById('tx_grupoccodigo');
	var i;
	for (i = elSel.length - 1; i>=0; i--) {
		elSel.remove(i);
	}
}


function ativaopcao2(){
	ajaxComboBox('tx_grupobcodigo','tx_grupoccodigo','produto_xml.asp?grupobcodigo=');
}

function resetcampos(){
	display_tipo('div_agro','none');
	display_tipo('div_maquinas','none');
	display_tipo('div_diversos','none');
	display_tipo('div_caminhoes','none');
	document.getElementById('tx_agrounidade').selectedIndex = 0;
	document.getElementById('tx_agroquantidade').value = "A COMBINAR";
	document.getElementById('tx_caminhaocarroceria').selectedIndex = 0;
	document.getElementById('tx_caminhaoeixo').selectedIndex = 0;
	document.getElementById('tx_caminhaocondicoes').selectedIndex = 0;
	document.getElementById('tx_caminhaomodelo').value = "";
	document.getElementById('tx_caminhaoano').value = "";
	document.getElementById('tx_caminhaokilometragem').value = "";
	document.getElementById('tx_diversosdescricao').value = "";
	document.getElementById('tx_maqfabricante').value = "";
	document.getElementById('tx_maqmodelo').value = "";
	document.getElementById('tx_maqano').value = "";
	document.getElementById('tx_maqcondicoes').selectedIndex = 0;
}

//##############################################################################################################################
//##############################################################################################################################


//##############################################################################################################################
function display_tipo(id,idacao) {
	document.getElementById(id).style.display = idacao;
}
//##############################################################################################################################
//##############################################################################################################################

