function loadXML(url) {
	var oXmlHttp;
	var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
	var ie = (typeof window.ActiveXObject != 'undefined'); 

	if ( window.XMLHttpRequest )		// Gecko
		oXmlHttp = new XMLHttpRequest();
	else if ( window.ActiveXObject )	// IE
		oXmlHttp = new ActiveXObject("MsXml2.XmlHttp");

	oXmlHttp.open("GET", url, false ) ;
	oXmlHttp.send( null ) ;

	if (moz) {
		xmlDoc = document.implementation.createDocument("", "doc", null);
		this.DOMDocument = oXmlHttp.responseXML;
		xmlDoc = this.DOMDocument;
	} else if (ie) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = false;
		while(xmlDoc.readyState != 4) {};
		xmlDoc.load(oXmlHttp.responseXML);
	}

	return xmlDoc;
}

function getXmlTagName(xmlDoc,tagName, noh) {	
	var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
	var ie = (typeof window.ActiveXObject != 'undefined'); 		

	if (moz) {
		return xmlDoc.getElementsByTagName(tagName)[noh].childNodes[0].nodeValue;
	} else {
		return xmlDoc.getElementsByTagName(tagName).item(noh).text;
	}
}

function countXmlTagName(xmlDoc,tagName) {	
	return xmlDoc.getElementsByTagName(tagName).length;
}

function escondeCombo(comboDestino, intTotal) {
	if (intTotal > 0) {
		document.getElementById(comboDestino.name).style.display = "block";
	} else {
		document.getElementById(comboDestino.name).style.display = "none";
	}
}

function limpaCombo(combo) {
	for (i=combo.length; i>=0; i--) { combo.options[i] = null; }
}

function fechaMsgAguarde() {
	setTimeout("document.getElementById('msgAguarde').style.display = 'none'", 500);
}

var formCombo, funcaoCombo;
function mostraMsgAguarde(form, funcao) {
	//### seta variáveis para que o setTimeout os encontre
	formCombo	= form;
	funcaoCombo = funcao;

	document.getElementById('msgAguarde').style.display = 'block';

	//### chama a função 50 milisegundos depois de mostrar a mensagem
	setTimeout('eval(funcaoCombo)', 50)

	//### posiciona a mensagem
	var posX, posY;
	posY = (document.all)?document.body.scrollTop+30:window.pageYOffset+30;
	posX = (((document.body.clientWidth-750)/2)+750)-180;

	//### posiciona no centro da tela
	//posX = (document.body.clientWidth - 180) /2;
	//posY = ((screen.availHeight - 50) / 2) + (posY/1.5);

	document.getElementById('msgAguarde').style.left = posX;
	document.getElementById('msgAguarde').style.top	 = posY;
}

function loadCombo(url, comboDestino, inicial, limparCmb, insertBranco, nenhumaOpcao, blnTodas) {
	txtInicial	= "";
	objXML		= loadXML(url);

	if (limparCmb) { limpaCombo(comboDestino); }
	if (insertBranco) { addOption(comboDestino, "", "", true); }
	if (blnTodas) { addOption(comboDestino, "Todas", "{all}", false); }

	for (var i=0; countXmlTagName(objXML, "reg") != i; i++) {
		addOption(comboDestino, getXmlTagName(objXML, "conteudo", i), getXmlTagName(objXML, "id", i), false);
		if (getXmlTagName(objXML, "id", i) == inicial) {
			txtInicial = getXmlTagName(objXML, "conteudo", i);
		}
	}
	if (txtInicial.length > 0) { selectMatchingOptions(comboDestino, txtInicial); }
	if (nenhumaOpcao) {
		addOption(comboDestino, "----------------------------------", "", false);
		addOption(comboDestino, "Nenhuma das opções", "0", false);
	}
	fechaMsgAguarde();
	return countXmlTagName(objXML, "reg");
}

function comboEstado(id, comboDestino, inicial, cmbLetraMunicipio, cmbMunicipio) {
	if (cmbLetraMunicipio != null){ limpaCombo(cmbLetraMunicipio); }
	if (cmbMunicipio != null){ limpaCombo(cmbMunicipio); }
	intItens = loadCombo('../../xml/estado.asp?id='+ id, comboDestino, inicial, true, true, true, false);
}

function comboLetrasMunicipio(id, comboDestino, cmbMunicipio) {
	if (comboDestino != null){limpaCombo(comboDestino); }
	if (cmbMunicipio != null){ limpaCombo(cmbMunicipio); }
	intItens = loadCombo('../../xml/letrasMunicipio.asp?id='+ id, comboDestino, null, true, true, false, true);
}

function comboMunicipio(estadoId, strLetra, comboDestino, inicial) {
	if (comboDestino != null){limpaCombo(comboDestino); }
	if (strLetra != "") {
		if (strLetra == "{all}") { strLetra = "" }
		intItens = loadCombo('../../xml/municipio.asp?id='+ estadoId +'&letra='+ strLetra, comboDestino, inicial, true, true, true, false);
	} else {
		fechaMsgAguarde();
	}
}

