System.Gadget.onSettingsClosing = settingsClosing;
var g_stationsSearchObject;
var prevZipCode;
var prevUnits;
var g_preferencesObject;
var degreesHaveChanged;

function getGadgetSettings()
{

}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function setGadgetSettings()
{
	if(cityMenu.disabled != true && stationMenu.disabled != true)
	{
		System.Gadget.Settings.write("zipCode", cityMenu.value);
		System.Gadget.Settings.write("stationId",stationMenu.value);
		System.Gadget.Settings.write("countryName", g_stationsSearchObject.cityList[cityMenu.selectedIndex].replace(" (International)","") );
		System.Gadget.Settings.write("type", g_stationsSearchObject.cityTypeList[cityMenu.selectedIndex] );
        System.Gadget.Settings.write("prevCityName",cityMenu.innerText);
        System.Gadget.Settings.write("prevStationName", stationMenu.innerText);
	}
	
	if(deg[0].checked)
		System.Gadget.Settings.write("units", "0");
	else if(deg[1].checked)
		System.Gadget.Settings.write("units", "1");
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function load()
{
    degreesHaveChanged = false
    if(g_preferencesObject == null)
        g_preferencesObject = new WBPreferences();
        
		if(g_stationsSearchObject != null) { delete g_stationsSearchObject; }
		var locationUrl = System.Gadget.Settings.read("locationUrl");
		var stationUrl = System.Gadget.Settings.read("stationUrl");
		
		g_preferencesObject.SetCurrentPartnerId(System.Gadget.Settings.read("pid"));
		g_stationsSearchObject = new WBStations(locationUrl,stationUrl,cityDataLoaded,dataLoadingError,stationDataLoaded,dataLoadingError,"",g_preferencesObject);

		if( System.Gadget.Settings.read("type") == "0" )
		{
			searchField.value = getZipCode();
		}
		else
			searchField.value = System.Gadget.Settings.read("countryName");
		
		//document.body.style.height = "145px";
		cityMenu.disabled = true;
		cityMenu.options.length = 0;
		stationMenu.disabled = true;
		stationMenu.options.length = 0;
		citySelectButton.disabled = true;
		stationSelectButton.disabled = true;

        if( System.Gadget.Settings.read("units") == "0" )
        {
        	deg[0].checked = true;
        	deg[1].checked = false;
        }
        else if( System.Gadget.Settings.read("units") == "1" )
    	{    
			deg[0].checked = false;
        	deg[1].checked = true;
		}
		
		prevZipCode = System.Gadget.Settings.read("zipCode");
		prevUnits = System.Gadget.Settings.read("units");
		
		var prevCityName = System.Gadget.Settings.read("prevCityName");
		if(prevCityName != "")
		    cityMenu.options[0] = new Option(prevCityName);
		
		var prevStationName = System.Gadget.Settings.read("prevStationName");
		if(prevStationName != "")
		    stationMenu.options[0] = new Option(prevStationName);
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function startSearch()
{
	//document.body.style.height = "211px";
	cityMenu.disabled = true;
	cityMenu.options.length = 0;
	stationMenu.disabled = true;
	stationMenu.options.length = 0;
	cityGoButton.disabled = true;
	cityMenu.options[0] = new Option("Searching for cities...");
	searchField.value = searchField.value.replace("<","").replace(">","");
	g_preferencesObject.SetCurrentSearchString(escape(searchField.value));
	g_stationsSearchObject.loadCities();
	citySelectButton.disabled = true;
	stationSelectButton.disabled = true;
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function cityDataLoaded()
{
    if(g_stationsSearchObject.cityList.length > 0)
    {
	    cityMenu.options.length = 0;
	    var i;
	    for (i=0; i < g_stationsSearchObject.cityList.length; i++)
	    {
		    cityMenu.options[i] = new Option( g_stationsSearchObject.cityList[i],g_stationsSearchObject.cityZipCodeList[i]);
	    }
	    cityMenu.disabled = false;
	    citySelectButton.disabled = false;
	    stationSelectButton.disabled = true;
	    stationMenu.disabled = true;
	    stationMenu.options.length = 0;
	    if( g_stationsSearchObject.cityList.length == 1 )
		    searchForStation();
    }
    else
    {
        cityMenu.disabled = true;
        cityMenu.options[0] = new Option("No Cities Found...");
        citySelectButton.disabled = true;
        stationMenu.disabled = true;
        stationMenu.innerText = "";
        stationSelectButton.disabled = true;
    }
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function searchForStation()
{
	//document.body.style.height = "275px";
	stationMenu.disabled = true;
	citySelectButton.disabled = false;
	stationSelectButton.disabled = true;
	stationMenu.options.length = 0;
	stationMenu.options[0] = new Option("Searching for stations...");
	var str;
	
	if(g_stationsSearchObject.cityTypeList[cityMenu.selectedIndex] == true)
		str = '&CityCode=' + g_stationsSearchObject.cityZipCodeList[cityMenu.selectedIndex];
	else
		str = '&ZipCode=' + g_stationsSearchObject.cityZipCodeList[cityMenu.selectedIndex];
	g_stationsSearchObject.loadStations( str );
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function stationDataLoaded()
{
	stationMenu.options.length = 0;
	var i;
	for (i=0; i < g_stationsSearchObject.stationsList.length; i++)
	{
		stationMenu.options[i] = new Option( g_stationsSearchObject.stationsList[i],g_stationsSearchObject.stationCodes[i+1]);
	}
	stationMenu.disabled = false;
	citySelectButton.disabled = false;
	stationSelectButton.disabled = false;
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function dataLoadingError()
{

}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function settingsClosing(event)
{
    if(event.closeAction == event.Action.commit && checkForAllowedExit() == true)
    {
        setGadgetSettings();
    	event.cancel = false;
    }
    else
    {
   		System.Gadget.Settings.write("zipCode",prevZipCode);
		System.Gadget.Settings.write("units",prevUnits);
		event.cancel = true;
    }
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function checkForAllowedExit()
{
    var value;
    
    if(cityMenu.disabled != true && stationMenu.disabled != true) // city and station have a selected value
        value = true;
    else if(degreesHaveChanged == true && cityMenu.innerText != "" && stationMenu.innerText != "")
        value = true;
    else
        value = false;

    return value;
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function degreeChanged()
{
    degreesHaveChanged = true;
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function checkForEnterKey(event)
{
    cityMenu.innerText = "";
	stationMenu.innerText = "";
	stationMenu.disabled = true;
	cityMenu.disabled = true;
	citySelectButton.disabled = true;
	stationSelectButton.disabled = true;
	    
	if(event.keyCode == 13 || event.keyCode == 3) // Enter or return key
		startSearch();
}
////////////////////////////////////////////////////////////////////////////////
//
//
////////////////////////////////////////////////////////////////////////////////
function getZipCode()
{
	var zpcd = System.Gadget.Settings.read("zipCode");
	if(zpcd != "")
	{
		zpcd = zpcd + "";
		while(zpcd.length < 5)
			zpcd = "0" + zpcd;
		return zpcd;
	}
	else
	{
		return zpcd;
	}
}
