// JavaScript Document
//alert('oi ajax');
function getAJAX() {
	try{
		var ajax	=	new ActiveXObject("Msxml2.XMLHTTP");
		return ajax;
	}catch(e){
		try {
			var ajax=new ActiveXObject("Microsoft.XMLHTTP");
			return ajax;
		}catch(e){
			try {
				var ajax=new XMLHttpRequest();
				return ajax;
			}
			catch(e){
				alert("Erro: Este browser não suporta XMLHttpRequest!");	  
			return false;
			}
		}
	}
}  
function preencheCombo(sUrl, sParam, sTag, sCombo, sItemInicial){
		/*
		Objetivo: Carregar um combobox html(<select>) com informações
		Dados de entrada:
			- sUrl 		-> Endereço do arquivo que irá gerar o XML
			- sParam	-> Parametros a serem enviados para o arquivo de geração do XML
			- sTag		-> Tag referente ao registro no arquivo html
			- sCombo	-> Nome do combobox a ser preenchido
		Dados de Saída:
			-sSelecione -> Indica se apresenta um texto "Selecione" antes dos itens.
			- Combo Preenchido com o retono do arquivo XML ou uma string "Nenhum" quando não achado registros
		
		Funções auxiliares:
			- executaAjax();
		*/
		
		var bmAjax = getAJAX();
		var url = sUrl + "?" + sParam;	//alert(url);
		funcao =  function() { executeAjax(bmAjax, sCombo,sTag, sItemInicial) }
		bmAjax.onreadystatechange = eval(funcao);	
		bmAjax.open("GET",url,true);
		bmAjax.send(null);
}
function executeAjax(bmAjax, sCombo, sTag, sItemInicial){
	//if( bmAjax.status == 404 ) alert('Arquivo XML não encontrado');
	combox = document.getElementById(sCombo);
	if( bmAjax.readyState == 4 ) {
		if( bmAjax.status == 200 ){
			if ( bmAjax.overrideMimeType ) 
				bmAjax.overrideMimeType('text/xml');									
				var xmldoc = bmAjax.responseXML;
				//var doc = bmAjax.responseText;
				//alert(doc); return false;
				
				combox.innerHTML	= "---";
				
				if(sItemInicial != null){
					var novo			= document.createElement("option");
					novo.setAttribute("id", "opcoes");
					if(sItemInicial == "Todas") novo.value = "*";
					else novo.value = "";						
					novo.text  			= "--- "+sItemInicial+" ---";
					combox.options.add(novo);
				}
				
				var dataArray = xmldoc.getElementsByTagName(sTag);
				//alert(dataArray.length);
				if(dataArray.length > 0) {
					for(var i = 0 ; i < dataArray.length ; i++) {
						var item 	= dataArray[i];
						var codigo	=  item.getElementsByTagName("codigo")[0].firstChild.nodeValue;
						var nome	=  item.getElementsByTagName("nome")[0].firstChild.nodeValue;	
						var novo 	= document.createElement("option");
						novo.setAttribute("id", "opcoes");
						novo.value 	= codigo;
						novo.text  	= nome;
						//document.forms[0].codseq.options.add(novo);
						combox.options.add(novo);
					}
				}else{
					combox.innerHTML = "Nenhum";
					contGlobal = 5;
					var novo = document.createElement("option");
					novo.setAttribute("id", "opcoes");
					novo.value = "-----";
					novo.disable = true;
					novo.text  = "Nenhum";
					//document.forms[0].codseq.options.add(novo);	
					combox.options.add(novo);				
				}
		}
	}else{
			combox.innerHTML = "---";	
			var novo = document.createElement("option");
			novo.setAttribute("id", "opcoes");
			novo.value = codigo;
			novo.disable = 'disabled';
			novo.text  = "CARREGANDO";
			//document.forms[0].codseq.options.add(novo);	
			combox.options.add(novo);		
		}
}

function alertaMSG(msg){
	$bmGetById('msg').innerHTML = msg;
	window.setTimeout("$('msg').innerHTML=''", 3000);	
}
