var	a = null, b = null;

function getWeather(place)
{
	if (trim(place) == "")
		return;

	innerText("weatherOutput", "Retrieving current weather");

	a = new ajaxRequest();

	if (place.indexOf(",") == -1)
		a.getData("/servlet/proxy?tgt=weather&attr=conditions&zip=" + place +
			"&fmt=text", showWeather);
	else
		a.getData("/servlet/proxy?tgt=weather&attr=conditions&city=" +
			place.substring(0,place.indexOf(",")) + "&state=" +
			place.substring(place.indexOf(",") + 1) + "&fmt=text", showWeather);

	return false;
}

function showWeather(text)
{
	var	failed = (text.indexOf("ERROR:") == 0);

	var report = getValue(text, "weather.conditions.report");

	var	html = "<table width=100% cellpadding=0 cellspacing=0 border=0>\n";

	if (! failed && report != null)
	{
		var weather = new CurrentConditions(report);

		html += "<tr>\n";
		html += "<td width=33%>\n";
		html += "	<table cellpadding=0 cellspacing=0 border=0>\n";
		html += "	<tr>\n";
		html += "	<td width=50% class=popupTitle>" + weather.temperature + "&deg; F</td>\n";
		html += "	<td width=50% align=center><img src=\"/weather/" + weather.iconName + ".gif\" width=30 height=30></td>\n";
		html += "	</tr>\n";
		html += "	<tr>\n"
		html += "	<td colspan=2 align=center class=popupData>" + weather.description + "</td>\n";
		html += "	</tr>\n";
		html += "	</table>\n";
		html += "</td>\n";
		html += "<td width=67%>\n";
		html += "	<table cellpadding=0 cellspacing=0 border=0>\n";
		html += "	<tr>\n";
		html += "	<td align=right class=popupLabel>Wind: </td><td class=popupData>&nbsp;" + weather.windDir + " at " + weather.windSpeed + " mph</td>\n";
		html += "	</tr>\n";
		html += "	<tr>\n";
		html += "	<td align=right class=popupLabel>Dew Point: </td><td class=popupData>&nbsp;" + weather.dewPoint + "&deg; F</td>\n";
		html += "	</tr>\n";
		html += "	<tr>\n";
		html += "	<td align=right class=popupLabel>Humidity: </td><td class=popupData>&nbsp;" + weather.humidity + " %</td>\n";
		html += "	</tr>\n";
		html += "	<tr>\n";
		html += "	<td align=right class=popupLabel>Pressure: </td><td class=popupData>&nbsp;" + weather.barPressure + " in. " + weather.barTendency + "</td>\n";
		html += "	</tr>\n";
		html += "	<tr>\n";
		html += "	<td align=right class=popupLabel>Visibility: </td><td class=popupData>&nbsp;" + weather.visibility + " mi.</td>\n";
		html += "	</tr>\n";
		html += "	</table>\n";
		html += "</td>\n";
		html += "</tr>\n";
		html += "<tr>\n";
		html += "<td align=center colspan=2 class=popupData>\n";
		html += "<br><br><span class=popupLabel>Reported At: </span>" + weather.reportedAt + ", " + weather.state + "\n";
		html += "</td>\n";
		html += "<tr>\n";
		html += "<td align=center colspan=2 class=popupData>\n";
		html += "<span class=popupLabel>Last Update: </span>" + weather.updateTime + "\n";
		html += "</td>\n";
		html += "</tr>\n";
	}
	else
	{
		html += "<tr>\n";
		html += "<td align=center class=text>Weather data unavailable.</td>\n";
		html += "</tr>\n";
	}

	html += "</table>\n";

	innerHTML("weatherOutput", html);
}

function getForecast(place)
{
	if (trim(place) == "")
		return;

	innerText("forecastOutput", "Retrieving weather forecast");

	b = new ajaxRequest();

	if (place.indexOf(",") == -1)
		b.getData("/servlet/proxy?tgt=weather&attr=forecast&zip=" + place +
			"&fmt=text", showForecast);
	else
		b.getData("/servlet/proxy?tgt=weather&attr=forecast&city=" +
			place.substring(0,place.indexOf(",")) + "&state=" +
			place.substring(place.indexOf(",") + 1) + "&fmt=text",
			showForecast);

	return false;
}

function showForecast(text)
{
	var	failed = (text.indexOf("ERROR:") == 0);

	var	report = getValue(text, "weather.forecast.report");

	var	html = "<table width=100% cellpadding=0 cellspacing=0 border=0>\n";

	if (! failed && report != null)
	{
		var forecast = new ExtendedForecast(report);

		html += "<tr>\n";
		html += "<td colspan=2 class=popupData><u>Day / Date</u></td><td class=popupData><u>Hi/Lo</u></td><td class=popupData><u>Precip.</u></td><td class=popupData><u>Wind</u></td><td class=popupData><u>Humidity</u></td>\n";
		html += "</tr>\n";

		for (i = 0; i < 7; i++)
		{
			var	day = forecast.days[i];

			html +=	"<tr>\n";
			html += "<td class=popupLabel>" + day.weekday + "</td>\n";
			html += "<td rowspan=2><img src=\"/weather/" + day.iconName + ".gif\" width=30 height=30></td>\n";
			html += "<td class=popupData>" + day.highTemp + "/" + day.lowTemp + "&deg;</td>\n";
			html += "<td align=center class=popupData>" + Math.round(day.precipitationPct / 10) * 10 + " %</td>\n";
			html += "<td class=popupData>" + day.windDir + " " + day.windSpeed + " mi</td>\n";
			html += "<td align=center class=popupData>" + day.humidity + " %</td>\n";
			html += "</tr>\n";
			html += "<tr>\n";
			html += "<td class=popupData>" + day.date + "</td>\n";
			html += "<td colspan=4 class=popupData>" + day.description + "</td>\n";
			html += "</tr>\n";
		}
	}
	else
	{
		html += "<tr>\n";
		html += "<td align=center colspan=6 class=text>Forecast data unavailable.</td>\n";
		html += "</tr>\n";
	}

	html += "</table>\n";

	innerHTML("forecastOutput", html);
}
