var request = null;
var xmlDoc = null;

try {
	request = new XMLHttpRequest();
} catch (trymicrosoft) {
	try {
		request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (othermicrosoft) {
		try {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (failed) {
			request = null;
		}
	}
}

if (request == null) {
	alert("Erro ao tentar criar um objeto Ajax");
}

function jb() {
	//url = "http://localhost:8080/jotabasso/jbasso.xml";
	url = "http://www.jotabasso.com.br:8080/jbasso.xml";
	request.open("get", url, true);
	request.onreadystatechange = atualizarPagina;
	request.send(null);
}

function atualizarPagina() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			montarPagina(request.responseXML);
		}
	}
}

function montarPagina(xml) {
	var node;

	node = xml.getElementsByTagName("soja").item(0);
	cotacoesNode(node, "soja");

	node = xml.getElementsByTagName("Milho").item(0);
	cotacoesNode(node, "milho");

	node = xml.getElementsByTagName("Dolar").item(0);
	cotacoesNode(node, "dolar");

	node = xml.getElementsByTagName("noticias").item(0);
	noticiasNode(node, "noticias");

	node = xml.getElementsByTagName("tempo").item(0);
	tempoNode(node, "tempo");
}

function cotacoesNode(node, nome) {
	var el = document.getElementById(nome);
	var prop = node.getElementsByTagName("propriedade");
	var vlr = node.getElementsByTagName("valor");

	for (i = 0; i < prop.length; i++) {
		var tr = document.createElement("TR");
		var td1 = document.createElement("TD");
		var td2 = document.createElement("TD");

		td1.appendChild(document.createTextNode(prop[i].firstChild.nodeValue));
		td2.appendChild(document.createTextNode(vlr[i].firstChild.nodeValue));

		tr.appendChild(td1);
		tr.appendChild(td2);

		el.appendChild(tr);
	}
}

function noticiasNode(node, nome) {
	var el = document.getElementById(nome);
	var prop = node.getElementsByTagName("propriedade");
	var vlr = node.getElementsByTagName("valor");
	
	for (i = 0; i < prop.length - 1; i++) {
		var span = document.createElement("SPAN");
		var a = document.createElement("A")

		a.appendChild(document.createTextNode(prop[i].firstChild.nodeValue));
		a.setAttribute("href",
				"http://www.agrolink.com.br/clientes/monsanto/detalhe_noticia.asp"
						+ vlr[i].firstChild.nodeValue);

		span.appendChild(a);
		el.appendChild(span);
	}
}

function tempoNode(node, nome){
	var el = document.getElementById(nome);
	
	var cid = node.getElementsByTagName("nome");
	var max = node.getElementsByTagName("maxima");
	var min = node.getElementsByTagName("minima");
	
	for (i = 0; i < cid.length - 5; i++) {
		var tr = document.createElement("TR");
		var td1 = document.createElement("TD");
		var td2 = document.createElement("TD");
		var td3 = document.createElement("TD");
		
		td1.appendChild(document.createTextNode(cid[i].firstChild.nodeValue));
		td2.appendChild(document.createTextNode(max[i].firstChild.nodeValue));
		td3.appendChild(document.createTextNode(min[i].firstChild.nodeValue));

		tr.appendChild(td1);
		tr.appendChild(td2);
		tr.appendChild(td3);

		el.appendChild(tr);
	}

}
