function rememberSelection(region, country, city)
{
    ////alert("rememberSelection: " + region + "," + country + "," + city);////
    if (region == null) { deleteCookie("lastRegion", "/", null); }
    else { setCookie("lastRegion", region, null, "/", null, null); }
    if (country == null) { deleteCookie("lastCountry", "/", null); }
    else { setCookie("lastCountry", country, null, "/", null, null); }
    if (city == null) { deleteCookie("lastCity", "/", null); }
    else { setCookie("lastCity", city, null, "/", null, null); }
}

function recallRegion(region)
{
    var recallRegion = getCookie("lastRegion");
    if (recallRegion != null && recallRegion != "") { return recallRegion; }
    return region;
}

function recallCountry(country)
{
    var recallCountry = getCookie("lastCountry");
    if (recallCountry != null && recallCountry != "") { return recallCountry; }
    return country;
}

function recallCity(city)
{
    var recallCity = getCookie("lastCity");
    if (recallCity != null && recallCity != "") { return recallCity; }
    return city;
}

function generateOption(option, selected)
{
    var selectedIndicator = "";
    if (option == selected) selectedIndicator = "selected"
    return '<option value="' + option + '" ' + selectedIndicator + '>' + option + '</option>';
}

function generateRegionSelector(selected)
{
    var result = '<select id="regionTitle" name="regionTitle" onchange="changeRegion()">';
    var i;
    for (i = 0; i < r_region_title.length; i++)
    {
        var s = r_region_title[i];
        result = result + generateOption(s, selected);
    }
    result = result + '</select>';
    return result;
}

function generateCountrySelector(selected, region)
{
    var result = '<select id="countryTitle" name="countryTitle" onchange="changeCountry()" style="width: 150px">';
    result = result + generateOption(cn_country_title[0], selected);
    var section;
    for (section = 1; section <= 2; section++)
    {
        if (region != null && section == 1) result = result + generateOption("----Countries in " + region + "----", selected);
        else if (region != null && section == 2) result = result + generateOption("----Countries outside " + region + "----", selected);
        var i;
        for (i = 1; i < cn_country_title.length; i++)
        {
            var s = cn_country_title[i];
            var s2 = cn_region_title[i];
            if (section == 1 && region == s2)
                result = result + generateOption(s, selected);
            else if (section == 2 && region != s2)
                result = result + generateOption(s, selected);
        }
    }
    result = result + '</select>';
    return result;
}

function generateCitySelector(selected, country)
{
    var result = '<select id="cityTitle" name="cityTitle" onchange="changeCity()" style="width: 100px">';
    result = result + generateOption(ct_city_title[0], selected);
    var section;
    for (section = 1; section <= 2; section++)
    {
        if (country != null && section == 1) result = result + generateOption("----Cities in " + country + "----", selected);
        else if (country != null && section == 2) result = result + generateOption("----Cities outside " + country + "----", selected);
        var i;
        for (i = 1; i < ct_city_title.length; i++)
        {
            var s = ct_city_title[i];
            var s2 = ct_country_title[i];
            if (section == 1 && country == s2)
                result = result + generateOption(s, selected);
            else if (section == 2 && country != s2)
                result = result + generateOption(s, selected);
        }
    }
    result = result + '</select>';
    return result;
}

function changeRegion()
{
    var selector = document.getElementById("regionTitle");
    var selection = selector.options[selector.selectedIndex].value;
    if (selection.indexOf("Select a") == 0 || selection == "Global") selection = null;
    var subselector = document.getElementById("countrySelector");
    subselector.innerHTML = generateCountrySelector(null, selection);
    subselector = document.getElementById("citySelector");
    subselector.innerHTML = generateCitySelector(null, null);
    rememberSelection(selection, null, null);
}

function changeCountry()
{
    var selector = document.getElementById("countryTitle");
    var selection = selector.options[selector.selectedIndex].value;
    if (selection.indexOf("Select a") == 0 || selection == "Global") selection = null;
    var country = selection;

    if (document.getElementById("homepage"))
    {
	if (country == "Singapore")
		window.open("http://www.eventshub.com.sg", "_blank", "toolbar=yes,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes", "y");
	else
	        document.location.href = "/calendar/calendar.php?type=month&day=17&month=07&year=2006&category=0&source=calendarSelector&prevRegion=&prevCountry=&prevCity=&regionTitle=&cityTitle=Select+a+city&countryTitle=" + country;
        return;
    }

    var region = getRegionByCountry(country);
    var city = null;

    var subselector = document.getElementById("regionSelector");
    if (subselector) { subselector.innerHTML = generateRegionSelector(region); }
    var subselector = document.getElementById("countrySelector");
    if (subselector) { subselector.innerHTML = generateCountrySelector(country, region); }
    var subselector = document.getElementById("citySelector");
    if (subselector) { subselector.innerHTML = generateCitySelector(city, country); }
    rememberSelection(region, country, null);
}

function changeCity()
{
    var selector = document.getElementById("cityTitle");
    var selection = selector.options[selector.selectedIndex].value;
    if (selection.indexOf("Select a") == 0 || selection == "Global") selection = null;
    var city = selection;
    var country = getCountryByCity(selection);
    var region = getRegionByCountry(country);

    var subselector = document.getElementById("regionSelector");
    subselector.innerHTML = generateRegionSelector(region);
    var subselector = document.getElementById("countrySelector");
    subselector.innerHTML = generateCountrySelector(country, region);
    var subselector = document.getElementById("citySelector");
    subselector.innerHTML = generateCitySelector(city, country);
    rememberSelection(region, country, city);
}

function getRegionByCountry(country)
{
    if (country == null) return null;
    for (i = 1; i < cn_country_title.length; i++)
    {
        var s = cn_country_title[i];
        var s2 = cn_region_title[i];
        if (s == country) return s2;
    }
    return null;
}

function getCountryByCity(city)
{
    if (city == null) return null;
    for (i = 1; i < cn_country_title.length; i++)
    {
        var s = ct_city_title[i];
        var s2 = ct_country_title[i];
        if (s == city) return s2;
    }
    return null;
}
