var xmlHttp;
var f_c;
var kph_mph;
var weather = new Array;
var weather_set_intervel = 2;
var got_data = 0;
var weather_icon = {
	"Sunny":"00",
	"Mainly Sunny":"01",
	"Partly Cloudy":"02",
	"Scattered Clouds":"02",
	"Mostly Cloudy":"03",
	"Increasing Cloud":"04",
	"Decreasing Cloud":"05",
	"Light Rainshower":"06",
	"Rain Showers":"06",
	"Light Rain Showers":"06",
	"Heavy Rain Showers":"06",
	"Mixed Rain and Snow Shower":"07",
	"Light flurries":"08",
	"Snow Showers":"08",
	"Light Snow Showers":"08",
	"Heavy Snow Showers":"08",
	"Thunderstorm without Precipitation":"45",
	"Recent Thunderstorm":"09",
	"Recent Thunderstorm with Rain":"09",
	"Thunderstorm":"09",
	"Light Thunderstorm":"09",
	"Heavy Thunderstorm":"09",
	"Thunderstorms and Rain":"09",
	"LightThunderstorms and Rain":"09",
	"Heavy Thunderstorms and Rain":"09",
	"Thunderstorms and Snow":"09",
	"LightThunderstorms and Snow":"09",
	"Heavy Thunderstorms and Snow":"09",
	"Thunderstorms and Ice Pellets":"09",
	"LightThunderstorms and Ice Pellets":"09",
	"Heavy Thunderstorms and Ice Pellets":"09",
	"Thunderstorms and Hail":"09",
	"LightThunderstorms and Hail":"09",
	"Heavy Thunderstorms and Hail":"09",
	"Thunderstorms and Small Hail":"09",
	"LightThunderstorms and Small Hail":"09",
	"Heavy Thunderstorms and Small Hail":"09",
	"Cloudy":"10",
	"Overcast":"10",
	"Distant Precipitation":"11",
	"Squalls":"11",
	"Recent Rainshower":"05",
	"Recent Rain":"05",
	"Mixed Rain and Drizzle":"28",
	"Light Rain":"11",
	"Heavy Mixed Rain and Drizzle":"11",
	"Moderate Rain":"12",
	"Rain":"12",
	"Heavy Rain":"13",
	"Heavy Rainshower":"13",
	"Light Freezing Fog":"14",
	"Moderate Freezing Fog":"14",
	"Freezing Fog":"14",
	"Strong Freezing Fog":"14",
	"Light Freezing Drizzle":"14",
	"Moderate Freezing Drizzle":"14",
	"Freezing Drizzle":"14",
	"Strong Freezing Drizzle":"14",
	"Light Freezing Rain":"14",
	"Moderate Freezing Rain":"14",
	"Freezing Rain":"14",
	"Strong Freezing Rain":"14",
	"Recent Freezing Precipitation":"05",
	"Ice Pellets":"27",
	"Ice Pellet Showers":"27",
	"Light Ice Pellet Showers":"27",
	"Heavy Ice Pellet Showers":"27",
	"Rain and Snow":"15",
	"Heavy Rain and Snow":"15",
	"Heavy Mixed Rain and Snow Shower":"15",
	"Light Snow":"16",
	"Moderate Snow":"17",
	"Snow":"17",
	"Heavy flurries":"18",
	"Heavy Snow":"18",
	"Tornado":"42",
	"Thunderstorm with Rain":"19",
	"Thunderstorm with Hail":"19",
	"Heavy Thunderstorm with Rain":"19",
	"Thunderstorm with Sand or Dust Storm":"19",
	"Heavy Thunderstorm with Hail":"19",
	"Smoke":"44",
	"Light Smoke":"44",
	"Heavy Smoke":"44",
	"Haze":"20",
	"Light Haze":"20",
	"Heavy Haze":"20",
	"Dust":"21",
	"Widespread Dust":"21",
	"Light Widespread Dust":"21",
	"Heavy Widespread Dust":"21",
	"Blowing Widespread Dust":"21",
	"Light Blowing Widespread Dust":"21",
	"Heavy Blowing Widespread Dust":"21",
	"Dust Haze":"23",
	"Dust Devils":"41",
	"Dust Whirls":"41",
	"Light Dust Whirls":"41",
	"Heavy Dust Whirls":"41",
	"Sand or Dust Storm":"23",
	"Sandstorm":"23",
	"Light Sandstorm":"23",
	"Heavy Sandstorm":"23",
	"Sand":"23",
	"Light Sand":"23",
	"Heavy Sand":"23",
	"Blowing Sand":"23",
	"Light Blowing Sand":"23",
	"Heavy Blowing Sand":"23",
	"Severe Sand or Dust Storm":"23",
	"Mist":"20",
	"Light Mist":"20",
	"Heavy Mist":"20",
	"Shallow Fog":"20",
	"Fog Patches":"02",
	"Patches of Fog":"02",
	"Fog":"20",
	"Light Fog":"20",
	"Heavy Fog":"20",
	"Ice fog":"20",
	"Fog Depositing Ice":"20",
	"Drifting Snow":"25",
	"Low Drifting Snow":"25",
	"Light Low Drifting Snow":"25",
	"Heavy Low Drifting Snow":"25",
	"Heavy Drifting Snow":"40",
	"Blowing Snow":"40",
	"Light Blowing Snow":"40",
	"Heavy Blowing Snow":"40",
	"Ice Crystals":"26",
	"Light Ice Crystals":"26",
	"Heavy Ice Crystals":"26",
	"Light Snow Pellets":"27",
	"Heavy Snow Pellets":"27",
	"Light Hail":"27",
	"Hail":"27",
	"Heavy Hail":"27",
	"Hail Showers":"27",
	"Light Hail Showers":"27",
	"Heavy Hail Showers":"27",
	"Small Hail Showers":"27",
	"Light Small Hail Showers":"27",
	"Heavy Small Hail Showers":"27",
	"Light Drizzle":"28",
	"Moderate Drizzle":"28",
	"Drizzle":"28",
	"Heavy Drizzle":"28",
	"Snow Grains":"26",
	"Light Snow Grains":"26",
	"Heavy Snow Grains":"26",
	"Clear":"00",
	"Mainly Clear":"01"
//	"Partly Cloudy":"32",
//	"Mostly Cloudy":"33",
//	"Increasing Cloud":"34",
//	"Decreasing Cloud":"35",
//	"Light Rainshower":"36",
//	"Mixed Rain and Snow Shower":"37",
//	"Light flurries":"38",
//	"Recent Thunderstorm with Rain":"39"
};
var weather_night_icons = {
	"00":"30",
	"01":"31",
	"02":"32",
	"03":"33",
	"04":"34",
	"05":"35",
	"06":"36",
	"07":"37",
	"08":"38",
	"09":"39"
};

var places = new Array(
new Place(0, "../../weather/whitehorse.xml",
	'Whitehorse', 'Whitehorse',
	100, 60.715750, 135.045472, "state_Change_ec",
	'http://www.weatheroffice.gc.ca/city/pages/yt-16_metric_e.html'),
new Place(1, "../../weather/faro.xml",
	'Faro', 'Carmacks',
	225, 62.225, 133.333333, "state_Change_ec",
	'http://www.weatheroffice.gc.ca/city/pages/yt-12_metric_e.html'),
new Place(2, "../../weather/carmacks.xml",
	'Carmacks', 'Dawson',
	350, 62.111944, 136.266944, "state_Change_ec",
	'http://www.weatheroffice.gc.ca/city/pages/yt-17_metric_e.html'),
new Place(3, "../../weather/dawson.xml",
	'Dawson', 'Eagle',
	475, 64.063, 139.435917, "state_Change_ec",
	'http://www.weatheroffice.gc.ca/city/pages/yt-6_metric_e.html'),
new Place(4, "../../weather/london.xml",
	'London', 'Circle',
	600, 51.500152, 0.126236, "state_Change_wu",
	'http://www.wunderground.com/global/stations/03772.html'),
new Place(5, "../../weather/alert.xml",
	'Alert', 'Fort_Yukon',
	725, 82.483333, 62.25, "state_Change_ec",
	'http://www.weatheroffice.gc.ca/city/pages/nu-22_metric_e.html'),
new Place(6, "../../weather/vostok.xml",
	'Vostok', 'Stevens_Village',
	850, -78.464422, -106.837328, "state_Change_wu",
	'http://www.wunderground.com/global/stations/89606.html'));

function Place(ord, url, place, id, posn, lat, lng, parser, url2)
{
	this.ord = ord;
	this.url = url;
	this.place = place;
	this.id = id;
	this.posn = posn;
	this.lat = lat;
	this.lng = lng;
	this.parser = parser;
	this.url2 = url2;

	this.setweather = setweather;
	this.statechange = 'if(xmlHttp.readyState!=4) return;'
			+ 'if(xmlHttp.status!=200){'
			+ '	var d = Date();'
			+ '	document.getElementById("date").innerHTML = d.toLocaleString();'
			+ '	report_state(xmlHttp.status);'
			+ '	return;'
			+ '}'
			+ parser+'(places['+ord+']);'
			+ 'showResults(places['+(ord+1)+'])';
}

function setweather(temp, cond, wind, time_stamp, forecasts, day)
{
	this.temp = temp;
	this.cond = cond;
	this.wind = wind;
	this.time_stamp = time_stamp;
	this.forecasts = forecasts;
	this.day = day;
}


function showResults(place)
{ 
	if (typeof(place) == "undefined") {
		var d = Date();
		document.getElementById("date").innerHTML = d.toLocaleString();

		got_data = 1;
		write_weather();
		return;
	}
	xmlHttp=null;
	var txt;
	if (window.XMLHttpRequest)
	{// code for IE7, Firefox, Mozilla, etc.
		xmlHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{// code for IE5, IE6
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlHttp!=null)
	{
		xmlHttp.onreadystatechange= new Function(place.statechange);
		try
		{
			xmlHttp.open("GET",place.url,true);
		}
		catch (er)
		{
			txt = document.getElementById("date").innerHTML;
			if (txt.substr(0,1) != '<') txt = '<span style="color:red">'+txt+'</span>';
			document.getElementById("date").innerHTML = txt;
		}
		xmlHttp.send(null);
	}
	else
	{
		alert("Your browser does not support XMLHTTP.");
	}
}

function report_state(status)
{
	var txt = document.getElementById("date").innerHTML;
	if (txt.substr(0,1) != '<') txt = '<span style="color:red">'+txt+' ['+status+']</span>';
	else {
		idx = txt.indexOf('[');
		if (idx > 0) {
			txt = txt.substr(0, idx+1)+status+']</span>';
		}
	}
	document.getElementById("date").innerHTML = txt;
	return;
}

function state_Change_ec(place) 
{ 
	var xmlDoc;
	var txt;
	var x;
	var xx;
	var xxx;
	var i;
	var j;
	var forecasts = new Array();
	try
	{
		xmlDoc = xmlHttp.responseXML.documentElement;
	}
	catch (er)
	{
		alert("Problem parsing response as XML");
	}
	//result = new Array;
	var description = "";
	var forecast = "";
	var category;
	var title;
	var forecast_issued = / Forecast issued/;
	
	x=xmlHttp.responseXML.documentElement.getElementsByTagName("channel");
	for (i=0;i<x.length;i++)
	{
		xx=x[i].getElementsByTagName("item");
		{
			for (j=0;j<xx.length;j++)
			{
				xxx=xx[j].getElementsByTagName("category");
				try
				{
					category = xxx[0].firstChild.nodeValue;
				}
				catch (er)
				{
					category = "";
				}
				if (category == 'Current Conditions') {
					xxx=xx[j].getElementsByTagName("title");
					try
					{
						title = xxx[0].firstChild.nodeValue;
					}
					catch (er)
					{
						title = "";
					}
					xxx=xx[j].getElementsByTagName("description");
					try
					{
						description = xxx[0].firstChild.nodeValue;
					}
					catch (er)
					{
						alert(er);
						description = "";
					}
				}
				if (category == 'Weather Forecasts') {
					xxx=xx[j].getElementsByTagName("title");
					try
					{
						title = xxx[0].firstChild.nodeValue;
					}
					catch (er)
					{
						title = "";
					}
					xxx=xx[j].getElementsByTagName("description");
					try
					{
						forecast = xxx[0].firstChild.nodeValue;
					}
					catch (er)
					{
						alert(er);
						forecast = "";
					}
					var fc = forecast.split(forecast_issued);					if (typeof(fc[1]) != 'undefined')
						forecast = fc[0];
					var idx = title.indexOf(':');
					if (idx > 0)
						forecast = title.substr(0, idx) + ': ' + forecast;
					forecasts.push(forecast);
				}
			}
		}
	}
	write_weather_ec(place, description, forecasts, day_night(place.lat, place.lng));
}

function state_Change_wu(place) 
{ 
	var xmlDoc;
	var txt;
	var x;
	var xx;
	var xxx;
	var i;
	var j;
	var description = "";
	var forecast = "";
	var forecasts = new Array();
	var timestamp = '';
	try
	{
		xmlDoc = xmlHttp.responseXML.documentElement;
	}
	catch (er)
	{
		alert("Problem parsing response as XML");
	}
	//result = new Array;
	var description;
	//var category;
	var title;
	var cur_con = /Current Conditions/;
	var time_sep = / - /;
	var as_of = / as of/;
	
	x=xmlHttp.responseXML.documentElement.getElementsByTagName("channel");
	for (i=0;i<x.length;i++)
	{
		xx=x[i].getElementsByTagName("item");
		{
			for (j=0;j<xx.length;j++)
			{
				xxx=xx[j].getElementsByTagName("title");
				try
				{
					title = xxx[0].firstChild.nodeValue;
				}
				catch (er)
				{
					title = "";
				}
				if (title.match(cur_con) != null) {
					var spl = title.split(time_sep);
					if (typeof(spl[1]) != 'undefined')
						timestamp = spl[1];
					xxx=xx[j].getElementsByTagName("description");
					try
					{
						description = xxx[0].firstChild.nodeValue;
					}
					catch (er)
					{
						alert(er);
						description = "";
					}
				}
				else if (title.match(as_of) != null) {
					var spl = title.split(as_of);
					xxx=xx[j].getElementsByTagName("description");
					try
					{
						forecast = xxx[0].firstChild.nodeValue;
					}
					catch (er)
					{
						alert(er);
						forecast = "";
					}
					if (!forecast.match(/ - /))
						forecast = spl[0] + ' - ' + forecast;
					forecasts.push(forecast);
				}
			}
		}
	}
	write_weather_wu(place, description, timestamp, forecasts, day_night(place.lat, place.lng));
}

Date.prototype.julianDate=function(){
var j=parseInt((this.getTime()-new Date('Dec 30,'+(this.getFullYear()-1)+' 23:00:00').getTime())/86400000).toString(),
i=3-j.length;
while(i-->0)j=0+j;
return j
};

function day_night(lat, lng)
{
	var date = new Date();
//	var g = (date.getMonth() * 30 + date.getDay() + 7) % 360;
	var g = date.julianDate();
	g = g*2*Math.PI/360;
	var D = 0.396372-22.91327*Math.cos(g)+4.02543*Math.sin(g)-0.387205*Math.cos(2*g)
   +0.051967*Math.sin(2*g)-0.154527*Math.cos(3*g) + 0.084798*Math.sin(3*g) 
	var TC = 0.004297+0.107029*Math.cos(g)-1.837877*Math.sin(g)
		-0.837378*Math.cos(2*g)-2.340475*Math.sin(2*g);
	var hour = date.getUTCHours() + date.getUTCMinutes()/60;
	var SHA = (hour-12)*15 - lng + TC;
	var off = Math.sin(lat*2*Math.PI/360)*Math.sin(D*2*Math.PI/360);
	var amp = Math.cos(lat*2*Math.PI/360)*Math.cos(D*2*Math.PI/360)
	var cosSZA = off + amp*Math.cos(SHA*2*Math.PI/360)
	var cosMax;
	var cosMin;
	if (amp > 0) {
		cosMax = off + amp;
		cosMin = off - amp;
	} else {
		cosMax = off - amp;
		cosMin = off + amp;
	}
	var day = new Array(	Math.acos(cosSZA)*180/Math.PI,
				Math.acos(cosMax)*180/Math.PI,	
				Math.acos(cosMin)*180/Math.PI);
	return day;
}

function write_weather_ec(place, description, forecasts, day)
{
	var ec_data;
	if (description == "") {
		place.setweather("unknown", undefined, "-1", "", forecasts, day);
	} else {
		ec_data = parse_ec(description);
		place.setweather(ec_data['Temperature'], ec_data['Condition'], ec_data['Wind'], ec_data['bserved at'], forecasts, day);
	}
}

function write_weather_wu(place, description, time_stamp, forecasts, day)
{
	var wu_data;
	if (description == "") {
		place.setweather("unknown", undefined, "-1", time_stamp, forecasts, day);
	} else {
		wu_data = parse_wu(description);
		var wind = wu_data['Wind Direction']+' '+wu_data['Wind Speed'];
		place.setweather(wu_data['Temperature'], wu_data['Conditions'], wind, time_stamp, forecasts, day);
	}
}

function write_weather()
{
	for (var xx in places) 
		write_one_weather(places[xx]);
}

function write_one_weather(place)
{
	var tlen = place.temp;
	var temp = place.temp;
	var cond = place.cond;
	var wind = place.wind;
	tlen = tlen.substr(0, tlen.length-3);
	var tempx = parseFloat(tlen);
	var dir;
	tlen = 100 - (tempx+41)*0.84;
	tlen = tlen.toFixed(0);
	if (temp != "unknown" && f_c == 'F') {
		tempx = 32 + tempx*1.8;
		temp = tempx.toFixed(0)+'&deg;F';
	}
	var posn = place.posn+20;
	var idx = wind.indexOf(' ');
	if (idx > 0) {
		dir = wind.substr(0, idx);
		wind = wind.substr(idx+1);
		switch (dir)
		{
		case 'North': dir = 'N';
			break;
		case 'South': dir = 'S';
			break;
		case 'East': dir = 'E';
			break;
		case 'West': dir = 'W';
			break;
		default: break;
		}
	} else {
		dir = 'calm';
	}
	var speed =	parseFloat(wind);
	var gust = wind.split('gust ');
	if (speed == 0) {
		dir = 'calm';
		speed = 'calm';
	}
	else if (speed < 0 || speed > 200) {
		dir = 'unknown';
		speed = 'unknown';
	}
	else {
		if (kph_mph == 'mph') {
			speed = speed * 0.621371192;
			speed = speed.toFixed(0);
		}
		speed = speed + kph_mph;
		if (gust[1] != undefined) {
			gust[1] = parseFloat(gust[1]);
			if (kph_mph == 'mph') {
				gust[1] = gust[1] * 0.621371192;
				gust[1] = gust[1].toFixed(0);
			}
			speed = speed + ' gust ' + gust[1] + kph_mph;
		}	
	}
	var cond_ico = "na";
	if (cond != undefined) {
		cond = cond.replace(/\s+$/g,"");
		cond_ico	= weather_icon[cond];
		if (cond_ico == undefined) cond_ico = "na";
	}
	var sun_txt
	var day_posn;
	var i = Math.abs(parseInt(90-place.day[0]));
	if (i != 0) i += '&deg; '+ Math.abs(parseInt((90-place.day[0])*60)%60)+"'";
	else
		i = Math.abs(parseInt((90-place.day[0])*60)%60)+"'";
	if (place.day[0] <= 90) {
		sun_txt = 'Sun is '+ i +' above the horizon';
	} else {
		if (cond != undefined) {
			var night_icon = weather_night_icons[cond_ico];
			if (night_icon != undefined) cond_ico = night_icon;
		}
		sun_txt = 'Sun is '+ i +' below the horizon';
	}
	i = parseInt(90-place.day[2]);
	if (i != 0) 
		i += '&deg; '+Math.abs(parseInt((90-place.day[2])*60)%60)+"'";
	else
		i = parseInt((90-place.day[2])*60)%60+"'";
	var j = parseInt(90-place.day[1]);
	if (j != 0) 
		j += '&deg; '+Math.abs(parseInt((90-place.day[1])*60)%60)+"'";
	else
		j = parseInt((90-place.day[1]*60))%60+"'";
	sun_txt += ". Today's range: "+i+' to '+j;
	
	var txt=    '<div style="position:absolute; top:'+posn+'px;left:113px;padding:0px 0px 0px 0px">';
	txt=txt+'<img src="../../images/thermo.png"></div>';
	txt=txt+'<div style="position:absolute; top:'+posn+'px;left:113px;z-index:1;padding:0px 0px 0px 0px">'
	txt=txt+'<img src="../../images/blue.png" height="'+tlen+'" width="40"></div>';
	txt=txt+'<div style="position:absolute; top:'+posn+'px;left:113px;z-index:2;padding:0px 0px 0px 0px">';
	txt=txt+'<a href="'+place.url2+'" target="_blank" alt="Weather forecast for '+place.place+'">';
	txt=txt+'<img src="../../images/thermometer.gif" height="120" width="40" title="'+temp+'"></a></div>';
	txt+=    '<div style="position:absolute; top:'+(posn+60)+'px;left:93px;padding:0px 0px 0px 0px;z-index:1">'
		+'<img src="../../weather/icons/horizon.gif" title=""></div>';
	day_posn = posn+parseInt(place.day[1]*120/180);
	txt+=    '<div style="position:absolute; top:'+day_posn+'px;left:100px;padding:0px 0px 0px 0px;z-index:2">'
		+'<img src="../../weather/icons/sun_max.gif" title=""></div>';
	day_posn = posn+parseInt(place.day[2]*120/180);
	txt+=    '<div style="position:absolute; top:'+day_posn+'px;left:100px;padding:0px 0px 0px 0px;z-index:2">'
		+'<img src="../../weather/icons/sun_min.gif" title="'+sun_txt+'"></div>';
	day_posn = posn+parseInt(place.day[0]*120/180);
	txt+=    '<div style="position:absolute; top:'+day_posn+'px;left:100px;padding:0px 0px 0px 0px;z-index:3">'
		+'<img src="../../weather/icons/sun.gif" title=""></div>';
	txt=txt+'<div style="position:absolute; top:'+posn+'px;left:5px;padding:0px 0px 0px 0px">';
	txt=txt+'<p class="s"><span class=h2>&nbsp;<br>'+place.place+'</span><br></div><br><br><br>';
	if (cond_ico != "na") {
		txt += '<div style="float:left;padding-right:5px"><img src="../../weather/icons/'
			+ cond_ico +'.gif" title="'+cond+'"></div>';
	} else {
		if (cond != undefined) txt += '<br><p class="s">'+cond+'</p>';
	}
//	txt=txt+temp+'<br><table><COL WIDTH=40><COL WIDTH=60><tr><td><img src="../../images/'+dir+'.png" title="'+dir;
//	if (speed !='calm') txt += ' '+speed;
//	txt += '"></td>';
//	txt=txt+'<td class="s">'+speed+'</td></tr></table></p></div>';
	txt=txt+'<img src="../../images/'+dir+'.png" title="'+dir;
	if (speed !='calm' && speed != 'unknown') txt += ' '+speed;
	txt += '">';
//	txt=txt+'</p></div>';
	txt +=    '<div style="position:absolute; top:'+posn+'px;left:0px;padding:0px;margin:0px;z-index:3">';
	txt +=	'<a href="JavaScript://Displaying_weather_report_and_forecasts." onmouseover="weatherpopup('+place.ord+')" onmouseout="weatherpopoff('+place.ord+')" onclick="return false">';
	txt +=	'<img src="../../transparent.gif" height="120" width="113" style="border:0px"></a></div>';
	txt +=	'<div style="display:none;z-index:5;background-image:url(\'../transparent.gif\');position:absolute; top:'+posn+'px;left:158px;width:400px" id="weather_pop'+place.ord+'">';
	txt +=	'<div style="background:lightyellow;width:400px;z-index:5;position:relative;padding:5px;border:2px;border-style:solid;border-color:saddlebrown">';
	txt += '<h2>'+place.place+' ('+weather_fmt(place.lat, place.lng)+')</h2>';
	txt += '<h3>'+place.time_stamp+'</h3>';
	txt += '<p>'+cond+'.&nbsp '+temp+'.&nbsp Wind: '+dir;
	if (speed !='calm' && speed != 'unknown') txt += ' '+speed;
	txt += '<p>'+sun_txt;
	for (var fc in place.forecasts)
		txt += '<p>'+place.forecasts[fc];
	txt += '<p class="si">Note that units in forecasts for places in Canada are &deg;C and kph, and units on places outside Canada are &deg;F and mph'
	txt += '</div>';
	txt +=	'<div style="position:absolute;top:0px;left:-38px;z-index:6;background-image:url(\'../../images/triangle.gif\');width:40px;height:60px;padding:0px;margin:0px">&nbsp;</div>';
	txt += '</div>';
	document.getElementById(place.id).innerHTML = txt;
}

function weather_fmt(lat, lng)
{
	var north = 'N';
	var west = 'W';
	if (lat < 0) {
		lat = -lat; north = 'S';
	}
	if (lng < 0) {
		lng = -lng; west = 'E';
	}
	return weather_dms(lat)+north+' '+weather_dms(lng)+west;
}
function weather_dms(a)
{
	return parseInt(a)+'&deg;'+parseInt(a*60)%60+"'"+parseInt(a*3600)%60+'"';
}

function weatherpopup(ord)
{
	document.getElementById('weather_pop'+ord).style.display="inline";
}

function weatherpopoff(ord)
{
	document.getElementById('weather_pop'+ord).style.display="none";
}

function parse_ec(description)
{
	var conditions = description.split('<br/>');
	var cond;
	var bits;
	var results = new Array;
	while (conditions.length > 1) {
		cond = conditions.shift();
		cond = cond.substr(4);
		bits = cond.split('</b> ');
		bits[0] = bits[0].substr(0, bits[0].length-1);
		results[bits[0]] = bits[1];
	}
	return results;
}

function parse_wu(description)
{
	var conditions = description.split(' | ');
	var cond;
	var bits;
	var results = new Array;
	var pat = / \/ /;
	while (conditions.length > 0) {
		cond = conditions.shift();
		bits = cond.split(': ');
		var idx = bits[1].indexOf('<');
		if (idx != -1) bits[1] = bits[1].substr(0, idx);
		var res = bits[1].split(pat);
		if (typeof(res[1]) != "undefined") bits[1] = res[1]+' ';
		results[bits[0]] = bits[1];
	}
	return results;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function temp_in(deg)
{
	createCookie('FC', deg, 300);
	f_c = deg;
	if (got_data == 1)
		write_weather();
}

function wind_in(units)
{
	createCookie('kph_mph', units, 300);
	kph_mph = units;
	if (got_data == 1)
		write_weather();
}

function init_weather(){
	f_c = readCookie('FC');
	if (f_c == 'F') document.getElementById('F').checked = true;
	else document.getElementById('C').checked = true;
	kph_mph = readCookie('kph_mph');
	if (kph_mph != 'mph') {
		kph_mph = 'kph';
		document.getElementById('kph').checked = true;
	}
	else document.getElementById('mph').checked = true;
	showResults(places[0]);
	setInterval("do_weather()", 500);
	

}

function do_weather()
{
	var d = new Date();
	var sec= d.getSeconds();
	var min	= d.getMinutes();
	if (sec == 0) {
		if (min == 20)
			showResults(places[0]);
		else if (min % 5 == 0) {
			for (var xx in places) {
				places[xx].day = day_night(places[xx].lat, places[xx].lng);
			}
			write_weather();
		}
	}
}
