function Ajax()
{
	this.req = null;
	this.url = null;
	this.method = 'GET';
	this.async = true;
	this.status = null;
	this.statusText = '';
	this.postData = null;
	this.readyState = null;
	this.responseText = null;
	this.responseXMP = null;
	this.handleResp = null;
	this.responseFormat = 'text';  // 'text, 'xml', or 'object'
	this.mimeType = null;

	this.doGet = function(url, hand, format)
		{
		this.url = url;
		this.handleResp = hand;
		this.responseFormat = format || 'text';
		this.doReq();
		}

	this.doReq = function()
		{
		if ( ! this.init())
			{
			alert('Could not create XMLHttpRequest object.');
			return;
			}
		this.req.open(this.method, this.url, this.async);
		var self = this; // fix loss-of-scope in inner function
		this.req.send(this.postData);
		if (self.req.readyState == 4)
			{
			switch (self.responseFormat)
				{
				case 'text':
					resp = self.req.responseText;
					break;
				case 'xml':
					resp = self.req.responseXML;
					break;
				case 'object':
					resp = self.req;
					break;
				}
			if (self.req.status >= 200 && self.req.status <= 299)
				self.handleResp(resp);
			else
				self.handleErr(resp);
			}
		};

	this.init = function()
		{
		if ( ! this.req)
			{
			try
				{
				// try to craete object for firefox, safari, ie7, etc
				this.req = new XMLHttpRequest();
				} catch (e)
						{
						try
							{
							// try to create object for later versions of ie
							this.req = new ActiveXObject('MSXML2.XMLHTTP');
							} catch (e)
									{
									try
										{
										// try to create object for early versions of ie
										this .req = new ActiveXObject('Microsoft.XMLHTTP');
										} catch (e)
												{
												// could not create an XMLHttpRequest object
												return false;
												}
									}
						}
			}
		return this.req;
		};

	this.handleErr = function()
		{
		var errorWin;
		try
			{
			errorWin = window.open('', 'errorWin');
			errorWin.document.body.innerHTML = this.responseText;
			} catch(e)
					{
					alert ('An error occurred, but the error message cannot be displayed. This is probably because of your browser\'s pop-up blocker.\n'
					     + 'Please allow pop-ups from this web site if you want to see the full error messages.\n\n'
							 + 'Status Code: ' + this.req.status + '\n'
							 + 'Status Description: ' + this.req.statusText);
					}
		};
}

var WeatherSummary = new function()
{
	this.targetUrl = null;
	this.responseFormat = null;
	this.showHand = null;
	this.ajax = new Ajax();
	this.ajax.async = false;

	this.init = function()
		{
		var self = WeatherSummary;
		self.targetUrl = 'current.xml';
		self.responseFormat = 'xml';
		self.doGet();
		}
	this.parseWeather = function(xml)
		{
		var self = WeatherSummary;
		var current = xml.getElementsByTagName('current')[0];
		if ( ! current) // may be missing if xml is being updated
			{  // may be missing if xml is being updated. Try again shortly
			self.showHand = setTimeout(self.doGet, 10000);
			return;
			}
		var weatherItem = new Object();
		for (i = 0; i < current.childNodes.length; i++)
			{
			el = current.childNodes[i];
			if (el.nodeType != 1)
				continue;
			if (el.childNodes[0] == null)
				continue;
			value = el.childNodes[0].nodeValue;
			weatherItem[el.nodeName] = value;
			}
		self.showWeather(weatherItem);
		}
	this.showWeather = function(weatherItem)
		{
		document.write( '<p><span>Weather station:</span> ' + weatherItem['stationName'] + '<br>' +
										'<span>Time:</span> ' + weatherItem['stationDate'] + ' ' + weatherItem['stationTime'] + 'm</p>' +
										'<p><span>Temperature:</span> ' + weatherItem['outsideTemp'] + ' ' + weatherItem['tempUnit'] + '</p>' +
										'<p><span>Wind:</span> ' + weatherItem['windDirection'] + ' ' + weatherItem['windSpeed'] + ' ' + weatherItem['windUnit'] + '<br>' +
										'<span>Wind chill:</span> ' + weatherItem['windChill'] + ' ' + weatherItem['tempUnit'] + '</p>' +
										'<p><span>Rain since midnight:</span> ' + weatherItem['dailyRain'] + ' ' + weatherItem['rainUnit'] + '</p>' +
										'<p><span>Humidity:</span> ' + weatherItem['outsideHumidity'] + ' ' + weatherItem['humUnit'] + '<br>' +
										'<span>Dew point:</span> ' + weatherItem['outsideDewPt'] + ' ' + weatherItem['tempUnit'] + '</p>' +
										'<p><span>Barometer:</span> ' + weatherItem['barometer'] + ' ' + weatherItem['barUnit'] + ' and ' + weatherItem['barTrend'] + '</p>' +
										'<p><span>Minimum temperature:</span> ' + weatherItem['lowOutsideTemp'] + ' ' + weatherItem['tempUnit'] + ' at ' + weatherItem['lowOutsideTempTime'] + 'm<br>' +
										   '<span>Maximum temperature:</span> ' + weatherItem['hiOutsideTemp'] + ' ' + weatherItem['tempUnit'] + ' at ' + weatherItem['hiOutsideTempTime'] + 'm</p>' +
										'<p><span>Moon:</span> ' + weatherItem['moonPhaseStr'] + '</p>' +
										'<p><span>Sunrise:</span> ' + weatherItem['sunriseTime'] + 'm<br>' +
										   '<span>Sunset:</span> ' + weatherItem['sunsetTime'] + 'm</p>');
		//								'<p>' + weatherItem['forecastStr'] + '</p>');
		}
	this.doGet = function()
		{
		var self = WeatherSummary;
		// send random parm to prevent caching
		var url = self.targetUrl + '?i=' + parseInt(Math.random()*99999999);
		self.ajax.doGet(url, self.parseWeather, self.responseFormat);
		}
}

