/* ************************************************************************** */
/* ** Functions for the YahooWeather AJAX application *********************** */
/* ************************************************************************** */
/* ** Retrieves information about current weather for a given location     ** */
/* ************************************************************************** */
/* ** Made by Johan Mares (www.johan-mares.be) 02/11/2006                  ** */
/* ************************************************************************** */
/* ** Last modified on 02/11/2006 | Current Version 0.10 | See changelog   ** */
/* ************************************************************************** */

function getXMLHTTPRequest() {
	var req = false;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else {
		if (window.ActiveXObject) {
			try {
				req = new ActiveXObject("Msml2.XMLHTTP");
			} catch (err1) {
				try {
					req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (err2) {
					req = false;
				}
			}
		}
	}
	return req;
}

/* ---------------------------------------------------------------------- */

var http = getXMLHTTPRequest();

/* ---------------------------------------------------------------------- */

function showWeather(domDoc) {
	var parent = document.getElementById('weather');
	var isTable = false;
	var mainNode = parent;
	for (i = 0; i < domDoc.childNodes.length; i++) {
		switch (domDoc.childNodes[i].getAttribute('tag')) {
			case 'tr':
				if (!isTable) {
					isTable = true;
					nodeTable = document.createElement('table');
					parent.appendChild(nodeTable);
					mainNode = document.createElement('tbody');
					nodeTable.appendChild(mainNode);					
				}
				nodeElm = document.createElement('tr');
				nodeTH = document.createElement('th');
				nodeTD = document.createElement('td');
				txtTH = domDoc.childNodes[i].getAttribute('title');
				txtTD = domDoc.childNodes[i].getAttribute('value');
				if (domDoc.childNodes[i].getAttribute('value')) {
					txtTD += " " + domDoc.childNodes[i].getAttribute('units');
				}
				nodeTH.appendChild(document.createTextNode(txtTH));
				nodeTD.appendChild(document.createTextNode(txtTD));
				nodeElm.appendChild(nodeTH);
				nodeElm.appendChild(nodeTD);
				break;
			case 'backgr':
				url = domDoc.childNodes[i].getAttribute('value');
				parent.style.backgroundImage = url;
				break;
			default:
				if (isTable) {
					isTable = false;
					mainNode = parent;
				}
				nodeElm = document.createElement(
					domDoc.childNodes[i].getAttribute('tag')
					);
				nodeTxt = document.createTextNode(
					domDoc.childNodes[i].getAttribute('value')
					);
				nodeElm.appendChild(nodeTxt);
		}
		mainNode.appendChild(nodeElm);
	}
}

/* ---------------------------------------------------------------------- */

function useHttpResponse() {
   if (http.readyState == 4) {
    if(http.status == 200) {
		var parent = document.getElementById('weather');
		parent.removeChild(parent.getElementsByTagName('h3').item(0));
		showWeather(http.responseXML.documentElement);
    }
  }
}

/* ---------------------------------------------------------------------- */

function getCurrentWeather() {
	// the parameters are declared in the html page
	var myurl = 'incl/srv/srv_YahooWeather.php?loc=' + parLoc + '&lang=' + parLang + '&degr=' + parDegr;
	//var parent = document.getElementById('weather');
	//parent.appendChild(nodeP = document.createElement('p'));
	//nodeP.appendChild(document.createTextNode(myurl));
	myDate = new Date().getTime();
	// add random number to URL to avoid cache problems
	var modurl = myurl+"&ts="+myDate;
	http.open("GET", modurl, true);
	// set up the callback function
	http.onreadystatechange = useHttpResponse;
	http.send(null);
}

/* ---------------------------------------------------------------------- */

