// Our global state
var geocoder;


// Set up the local searcher.
function OnShowItems()
{
    var CountryOption = document.getElementById(CountryOptionID);
    var CountryList = document.getElementById(CountryListID);
    var DistanceOption = document.getElementById(DistanceOptionID);
    var Postcode = document.getElementById(PostcodeID);
    
    if (CountryOption.checked && DistanceOption.checked && Postcode.value.length > 0 && Postcode.value != 'Postcode/City')
    {
        geocoder = new GClientGeocoder();
        
        var location = Postcode.value + ', ' + CountryList.options[CountryList.selectedIndex].text;

        geocoder.getLatLng(location, OnGeocode);
    }
    else
    {
        if (Postcode.value.length <= 0 || Postcode.value == 'Postcode/City')
        {
            DistanceOption.checked = false;
        }
        DoThePostBack();
    }
}


function OnGeocode(point)
{
    if (!point)
    {
        var DistanceOption = document.getElementById(DistanceOptionID);
        var Postcode = document.getElementById(PostcodeID);
        
        DistanceOption.checked = false;
        Postcode.value = 'Postcode/City';
    }
    else
    {
        var HiddenLat = document.getElementById(HiddenLatID);
        var HiddenLng = document.getElementById(HiddenLngID);

        HiddenLat.value = point.lat();
        HiddenLng.value = point.lng();        
    }
    
    DoThePostBack();
}


function OnCountryChange()
{
    var CountryOption = document.getElementById(CountryOptionID);
    var CountryList = document.getElementById(CountryListID);
    var DistanceOption = document.getElementById(DistanceOptionID);
    var DistanceList = document.getElementById(DistanceListID);
    var Postcode = document.getElementById(PostcodeID);
    var DistanceSearch = document.getElementById('distancesearch');
    
    if (CountryOption.checked && CountryList.selectedIndex > 0)
    {
        DistanceOption.disabled = false;
        DistanceList.disabled = false;
        Postcode.disabled = false;
        DistanceSearch.style.color = '#000000';
    }
    else
    {
        DistanceOption.disabled = true;
        DistanceList.disabled = true;
        Postcode.disabled = true;
        DistanceSearch.style.color = '#999999';
    }
}