System.Gadget.onSettingsClosing = SettingsClosing;

function init() {
		var defaultTown = System.Gadget.Settings.read("defaultTown");
		if (defaultTown != "")
		    tDefaultTown.value = defaultTown ;			
		buildCountryList(sDefaultCountry);
		ldefaultMode.innerHTML = prf_defaultMode;
		ldefaultTown.innerHTML = prf_defaultTown;
		ldefaultCountry.innerHTML = prf_defaultCountry;
		sMode.options[0] = new Option(m_Plan, "0");
		sMode.options[1] = new Option(m_iti_mode_iti, "1");
		var defaultMode = System.Gadget.Settings.read("defaultMode");
		if (defaultMode == "")
		    defaultMode = "0";
		sMode.selectedIndex = defaultMode;
}

function buildCountryList(control)
{
    defaultCountryCode = System.Gadget.Settings.read("defaultCountryCode");
    if (defaultCountryCode == "")
        defaultCountryCode = defaultdefaultCountryCode;
    control.options.length=0;
    for(i = 0;i < countries.length; i ++)
    {
        if (countries[i].f == 'iti')
        {
            var opt = document.createElement("option");
            opt.text = countries[i].l;
            opt.value = countries[i].c;
            if (defaultCountryCode == opt.value) 
                opt.selected=true;
            control.options.add(opt);
        }
    }
}

function SettingsClosing(event)
	{
		if (event.closeAction == event.Action.commit) {
			var town = tDefaultTown.value;
			System.Gadget.Settings.write("defaultTown", town);
			var countrycode = sDefaultCountry.options[sDefaultCountry.selectedIndex].value
			System.Gadget.Settings.write("defaultCountryCode", countrycode);			
			var mode = sMode.options[sMode.selectedIndex].value
			System.Gadget.Settings.write("defaultMode", mode);	
		}		
		event.cancel = false;
	}
