// AjaxWeather.js
// Javascript for index.php

// When the window is loaded, populate the page with data
Event.observe(window, 'load', 
	function() {
		// getCountries ();		// uncomment for AjaxWeather.php
	}
);


function getCountries() {
	var xmlDocument = getXMLDocument ();
	var _url = "AjaxWeather_backend.php";
	createXMLCdataNode (xmlDocument, 'ActionToPerform', "getCountries");
	_data = "&xmlData=" + getSerializedData (xmlDocument);
	var myAjax = new Ajax.Request (
		"AjaxWeather_backend.php",
		{
			onSuccess: function (transport) {
				$("divCountries").innerHTML = transport.responseText;
				getCountryLocations ("US");
				Event.observe("cc", "change",
					function() {
						getCountryLocations ($("cc").value);
					}
				);
			},
			onFailure: function () {
				alert ("System failure.  Please restart your browser and start again");
			},
			method: 'post',
			parameters: _data
		}
	);
}

function getWeatherForLocation ($icao) {
	var xmlDocument = getXMLDocument ();
	var _url = "AjaxWeather_backend.php";
	createXMLCdataNode (xmlDocument, 'ActionToPerform', "getWeatherForLocation");
	createXMLCdataNode (xmlDocument, 'icao', $icao);
	_data = "&xmlData=" + getSerializedData (xmlDocument);
	var myAjax = new Ajax.Request (
		"AjaxWeather_backend.php",
		{
			onSuccess: function (transport) {
				//$("divWeatherResults").toggle ();
				$("divWeatherResults").innerHTML = transport.responseText;
				//Effect.toggle("divWeatherResults", 'appear');
			},
			onFailure: function () {
				alert ("System failure.  Please restart your browser and start again");
			},
			method: 'post',
			parameters: _data
		}
	);
}

function getCountryLocations ($cc) {
		var xmlDocument = getXMLDocument ();
		var _url = "AjaxWeather_backend.php";
		createXMLCdataNode (xmlDocument, 'ActionToPerform', "getCountryLocations");
		createXMLCdataNode (xmlDocument, 'cc', $cc);
		_data = "&xmlData=" + getSerializedData (xmlDocument);
		var myAjax = new Ajax.Request (
			"AjaxWeather_backend.php",
			{
				onSuccess: function (transport) {
					$("spanCountryLocations").innerHTML = transport.responseText;
					getWeatherForLocation ($("icao").value);
					Event.observe("icao", "change",
						function() {
							getWeatherForLocation ($("icao").value);
						}
					);
//					alert (transport.responseText);
				},
				onFailure: function () {
					//alert ("System failure.  Please restart your browser and start again");
				},
				method: 'post',
				parameters: _data
			}
		);

}