function getXMLHTTPRequest() {
	try {
		req = new XMLHttpRequest();
	} catch(err1) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (err2) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (err3) {
				req = false;
			}
		}
	}
	return req;
}

var http = getXMLHTTPRequest();

function getWeather() {
	var myurl = '/scripts/weather.php';
	myRand = parseInt(Math.random()*999999999999999);
	// add random number to URL to avoid cache problems
	var modurl = myurl+"?rand="+myRand;
	http.open("POST", myurl, true);
	// set up the callback function
	http.onreadystatechange = useWeatherData;
	http.send('');
}

function useWeatherData() {
	if (http.readyState == 4) {
		if(http.status == 200) {
			var items = http.responseXML.getElementsByTagName("item");
			var weatherStr = "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n"; 
			// get the first item
			var item = items[0];
			// now we have the item object, time to get the contents
			// get the town name
			var town = item.getElementsByTagName("town")[0].firstChild.nodeValue;
			// get the minimum temperature
			var minValue = item.getElementsByTagName("min")[0].firstChild.nodeValue;
			// get the maximum temperature
			var maxValue = item.getElementsByTagName("max")[0].firstChild.nodeValue;
			// get the icon
			var icon = item.getElementsByTagName("icon")[0].firstChild.nodeValue;
			// get the icon alt tag
			var iconAlt = item.getElementsByTagName("iconAlt")[0].firstChild.nodeValue;
			weatherStr += "<tr>\n" +
						 "<td width=\"100\">"+town+"</td>\n" +
						 "<td>"+minValue+" - "+maxValue+"</td>\n" +
						 "<td width=\"25\"><img src=\"/images/weather-icons/"+icon+"\" width=\"20\" height=\"19\" alt=\""+iconAlt+"\"/></td>\n" +
						 "</tr>";
			weatherStr += "</table>";
			document.getElementById('weatherBoxWeather').innerHTML = weatherStr;
			getServerTime(false);
		}
	} else {
		document.getElementById('weatherBoxWeather').innerHTML = '';
	}
}
