// JavaScript Document
//alert('oi ajax');
function buscaNoticias(){
document.getElementById("noticias").style.display= "none";
ajax = getAJAX();
ajax.onreadystatechange = function(){
trataBuscaNoticias(ajax);
};
ajax.open("POST","controller/noticias.php",true);
ajax.send("action=getNoticiasList");
}

function trataBuscaNoticias(ajax){
if( ajax.readyState == 4 ) {
		if( ajax.status == 200 ){
			document.getElementById("noticias").innerHTML = "";
			document.getElementById("noticias").innerHTML = ajax.responseText;
			document.getElementById("noticias").style.display = "";
		}
	}
}

function testeAjax(){
alert("oi teste beta-gama-omega");
}

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(browserNaoSuportaXMLHttpRequest);	  
			return false;
			}
		}
	}
}  
function preencheCombo(sUrl, sParam, sTag, sCombo, bTodos){
		/*
		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:
			- Combo Preenchido com o retono do arquivo XML ou uma string "Nenhum" quando n�o achado registros
		
		Fun��es auxiliares:
			- executaAjax();
		*/
		var ajax = getAJAX();
		var url = sUrl + "?" + sParam;	//alert(url);
		funcao =  function() { executeAjax(ajax,sCombo,sTag, bTodos) }
		ajax.onreadystatechange = eval(funcao);	
		ajax.open("GET",url,true);
		ajax.send(null);
}



function executeAjax(ajax, sCombo, sTag, bTodos){

	combox = document.getElementById(sCombo);
	if( ajax.readyState == 4 ) {
		if( ajax.status == 200 ){
							
				var xmldoc = ajax.responseXML;
				//var doc = ajax.responseText;alert(doc); return false;			
				
				combox.innerHTML	= "---";
				var novo			= document.createElement("option");
				novo.value 			= "";
				novo.text  			= "--- Selecione ---";
				combox.options.add(novo);
				
				if(bTodos){
					var novo			= document.createElement("option");
					novo.value 			= "*";
					novo.text  			= "Todos";
					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;
						if(sTag == 'cidade' && sCombo == 'cidade') novo.value 	= nome;
						novo.text  	= nome;
						//document.forms[0].codseq.options.add(novo);
						combox.options.add(novo);
					}
				}else{
					if(idioma.indexOf("pt") >=0)
						combox.innerHTML = "Nenhum";	
					else
						combox.innerHTML = "None";	
						
					var novo = document.createElement("option");
					novo.setAttribute("id", "opcoes");
					novo.value = "-----";
					novo.disable = true;
					if(idioma.indexOf("pt") >=0)
						novo.text  = "Nenhum";
					else
						novo.text = "None";	
					
					//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';
			if(idioma.indexOf("pt") >=0)
				novo.text  = "CARREGANDO";
			else
				novo.text  = "LOADING";
				
			//document.forms[0].codseq.options.add(novo);	
			combox.options.add(novo);		
		}
}