//Função que cria o objeto
function criaObjeto(){
	var xmlhttp;
	//Criando o Objeto
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	  try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (e) {
		xmlhttp = false;
	  }
	}
	@else
	  xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
		  xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

function consultar(n){
	manipulaResposta(n);
}

function manipulaResposta(id) {
	function xmlMicoxLoader(url){
	  //by Micox: micoxjcg@yahoo.com.br.
		if(window.XMLHttpRequest){
			var Loader = new XMLHttpRequest();
			Loader.open("GET", url ,false);
			Loader.send(null);
			return Loader.responseXML;
		}else if(window.ActiveXObject){
			Loader = new ActiveXObject("Msxml2.DOMDocument.3.0");
			Loader.async = false;
			Loader.load(url);
			return Loader;
		}
	}
	function xmlMicoxArvore(xmlNode,identacao){
	  //by Micox: micoxjcg@yahoo.com.br
		var arvoreTxt="";
		for(var i=0;i<xmlNode.childNodes.length;i++){//percorrendo os filhos do nó
			arvoreTxt = arvoreTxt + identacao + xmlNode.childNodes[i].nodeName + ": "
			if(xmlNode.childNodes[i].childNodes.length==0){
			//se não tiver filhos eu já pego o nodevalue
				arvoreTxt = arvoreTxt + xmlNode.childNodes[i].nodeValue + "\n";
			}else if(xmlNode.childNodes[i].childNodes.length>0){
			//se tiver filhos eu tenho que pegar o valor pegando o valor do primeiro filho
				arvoreTxt = arvoreTxt + xmlNode.childNodes[i].firstChild.nodeValue + "\n";
				arvoreTxt = arvoreTxt + xmlMicoxArvore(xmlNode.childNodes[i],identacao + "> > ");
			}
		}
		return arvoreTxt;
	}
	
	xml = xmlMicoxLoader('xml/linhas.xml'); //carrega o xml

	var linhas = xml.getElementsByTagName('linha');
	for(i=0; i<linhas.length; i++) {
		//separação dos elementos do xml
		var xml_id = linhas[i].getElementsByTagName('id')[0].firstChild.nodeValue;
		var xml_nome = linhas[i].getElementsByTagName('nome')[0].firstChild.nodeValue;
		var xml_texto = linhas[i].getElementsByTagName('texto')[0].firstChild.nodeValue;
		var xml_imagem = linhas[i].getElementsByTagName('imagem')[0].firstChild.nodeValue;
		if(xml_id == id) {
			var coluna = document.getElementById('contProdutos');
			var div = document.createElement('div');
			div.setAttribute('id', 'descricoes');
			var h2 = document.createElement('h2');
			var p = document.createElement('p');
			document.getElementById('produtos').style.background = xml_imagem;
			h2.innerHTML = xml_nome;
			p.innerHTML = xml_texto;
			div.appendChild(h2);
			div.appendChild(p);
			coluna.innerHTML = '';
			coluna.appendChild(div);
			
			//Feedback
			document.getElementById('topoInterno').getElementsByTagName('p')[0].innerHTML = 'Produtos | ' +xml_nome;
		}
	}
}

//Função que gera uma linha radomincamente quando carrega a página produtos
function chamaMetodologia() {
	if(!document.getElementById('contProdutos')) {
		return;
	}
	n = Math.floor(Math.random()*7);
	consultar(n);
}

window.onload = function() {
	chamaMetodologia();	
}