// JavaScript Document
function NewAjax(){
	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;
				}
			 }
	}
	return ajax;
}
var req;

function loadXMLDoc(url,valor)
{
    req = null;
    // Procura por um objeto nativo (Mozilla/Safari)
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url+'?tipo='+valor, true);
        req.send(null);
    // Procura por uma versao ActiveX (IE)
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url+'?tipo='+valor, true);
            req.send();
        }
    }
} 

function processReqChange()
{
    // apenas quando o estado for "completado"
    if (req.readyState == 4) {
        // apenas se o servidor retornar "OK"
        if (req.status == 200) {
            // procura pela div id="atualiza" e insere o conteudo
            // retornado nela, como texto HTML
            document.getElementById('marca').innerHTML = req.responseText;
        } else {
            alert("Houve um problema ao obter os dados:\n" + req.statusText);
        }
    }
}
 
function AtualizaMarca(valor)
{
    loadXMLDoc("marca.php",valor);
} 

function loadXMLDoc2(url,valor)
{
    req = null;
    // Procura por um objeto nativo (Mozilla/Safari)
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange2;
        req.open("GET", url+'?marca='+valor, true);
        req.send(null);
    // Procura por uma versao ActiveX (IE)
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange2;
            req.open("GET", url+'?marca='+valor, true);
            req.send();
        }
    }
} 

function processReqChange2()
{
    // apenas quando o estado for "completado"
    if (req.readyState == 4) {
        // apenas se o servidor retornar "OK"
        if (req.status == 200) {
            // procura pela div id="atualiza" e insere o conteudo
            // retornado nela, como texto HTML
            document.getElementById('modelo').innerHTML = req.responseText;
        } else {
            alert("Houve um problema ao obter os dados:\n" + req.statusText);
        }
    }
}
 
function AtualizaModelo(valor)
{
    loadXMLDoc2("modelo.php",valor);
} 

var HttpReq = null;
 var dest_combo = null;
 function CidadeUF(url, comboBox, cid){
  	dest_combo = comboBox;
    var indice = document.getElementById('uf').selectedIndex;
    var sigla = document.getElementById('uf').options[indice].getAttribute('value');
    url = url + '?mar=' + sigla;
    

    if (document.getElementById) { //Verifica se o Browser suporta DHTML.
        if (window.XMLHttpRequest) {
            HttpReq = new XMLHttpRequest();
            HttpReq.onreadystatechange = XMLCidade;
            HttpReq.open("GET", url, true);
            HttpReq.send(null);
        } else if (window.ActiveXObject) {
            HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
            if (HttpReq) {
                HttpReq.onreadystatechange = XMLCidade;
                HttpReq.open("GET", url, true);
                HttpReq.send();
            }
        }
    }
 }

 function XMLCidade() {
	if (HttpReq.readyState == 4 && HttpReq.status == 200){
		var result = HttpReq.responseXML;
        var cidades = result.getElementsByTagName("nome");
        document.getElementById('municipio').innerHTML = "";
        for (var i = 0; i < cidades.length; i++) {
            new_opcao = create_cidade(cidades[i]);
            document.getElementById('municipio').appendChild(new_opcao);
        }
    }
 } 

 function create_cidade(cidade) {
    var new_opcao = document.createElement("option");
    var texto = document.createTextNode(cidade.childNodes[0].data);
    new_opcao.setAttribute("value",cidade.getAttribute("id"));
    //new_opcao.setAttribute("innerHTML",cidade.getAttribute("id"));
    new_opcao.setAttribute("id",cidade.getAttribute("id"));
    if(document.getElementById('mun').value == cidade.getAttribute("id")){
		new_opcao.setAttribute("selected","selected");
	}

   
    new_opcao.appendChild(texto); //Adiciona o texto a OPTION.
    return new_opcao; // Retorna a nova OPTION.
 } 
 
 
