
function populateSelect(elt, arr)
{
    elt.options.length = 0;
    elt.options[0] = new Option('', '');
    for (i = 0; i < arr.length; i++) {
        elt.options[i+1] = new Option(arr[i], arr[i]);
    }
    elt.selectedIndex = 0;
}

function clearSelect(elt)
{
    elt.options.length = 0;
    elt.selectedIndex = 0;
}

function enableCountry(co, r, c)
{
    co.options[0] = new Option(co.fullDefault, '');
    co.selectedIndex = 0;
    co.disabled = 0;

    r.options[0] = new Option(r.emptyDefault, '');
    r.selectedIndex = 0;
    r.disabled = 1;

    c.options.length = 0;
    c.options[0] = new Option(c.emptyDefault, '');
    c.disabled = 1;
}

function enableRegion(r, c)
{
    r.options[0] = new Option(r.fullDefault, '');
    r.selectedIndex = 0;
    r.disabled = 0;

    c.options.length = 0;
    c.options[0] = new Option(c.emptyDefaultRegion, '');
    c.disabled = 1;
}

function enableCity(c)
{
    c.options[0] = new Option(c.fullDefault, '');
    c.selectedIndex = 0;
    c.disabled = 0;
}

function loadingCountry(co, r, c)
{
    co.options.length = 0;
    co.options[0] = new Option(co.loadingText);
    co.disabled = 1;

    r.options.length = 0;
    r.disabled = 1;

    c.options.length = 0;
    c.disabled = 1;

}

function loadingRegion(r, c)
{
    r.options.length = 0;
    r.options[0] = new Option(r.loadingText);
    r.disabled = 1;

    c.options.length = 0;
    c.disabled = 1;

}

function loadingCity(c)
{
    c.options.length = 0;
    c.options[0] = new Option(c.loadingText);
    c.disabled = 1;
}

__country.onload = function()
                            {
                                this.emptyDefault = '';
                                this.fullDefault = 'Please select a country';
                                this.loadingText = 'Loading ...';

                                loadingCountry(this, __region, __city);

                                try {
                                    __r1 = ajaxac_createXMLHttp();

__r1.open('get', '/examples/CountryRegionCityJax/index.php/getcountries');

__r1_xmlhttpsuccess = function()
                            {
                                _data = ajaxac_receivejsarray(__r1.responseText);
                                populateSelect(__country, _data);
                                enableCountry(__country, __region, __city);
                            }

__r1_onreadystatechange = function()
                            {
                                if (__r1.readyState == 4 && __r1.status == 200 && __r1_xmlhttpsuccess) {
                                    __r1_xmlhttpsuccess();
                                }
                            }

__r1.onreadystatechange = __r1_onreadystatechange;

__r1.send(null);
                                }
                                catch (e) { }


                                return false;
                            }

__country.onload();

__country.onchange = function()
                            {
                                try {
                                    if (this.selectedIndex == 0)
                                        enableCountry(this, __region, __city);
                                    else {
                                        loadingRegion(__region, __city);
                                        __r2 = ajaxac_createXMLHttp();

__r2.open('get', '/examples/CountryRegionCityJax/index.php/getregions' + '?' + 'c=' + encodeURIComponent(__country.value));

__r2_xmlhttpsuccess = function()
                            {
                                _data = ajaxac_receivejsarray(__r2.responseText);
                                populateSelect(__region, _data);
                                enableRegion(__region, __city);
                            }

__r2_onreadystatechange = function()
                            {
                                if (__r2.readyState == 4 && __r2.status == 200 && __r2_xmlhttpsuccess) {
                                    __r2_xmlhttpsuccess();
                                }
                            }

__r2.onreadystatechange = __r2_onreadystatechange;

__r2.send(null);
                                    }

                                }
                                catch (e) { }


                                return false;
                            }
__region.onload = function()
                            {
                                this.emptyDefault = 'Please select a country';
                                this.fullDefault = 'Please select a region/state';
                                this.loadingText = 'Loading ...';
                            }

__region.onload();

__region.onchange = function()
                            {
                                try {
                                    if (this.selectedIndex == 0)
                                        enableRegion(__region, __city);
                                    else {
                                        loadingCity(__city);
                                        __r3 = ajaxac_createXMLHttp();

__r3.open('get', '/examples/CountryRegionCityJax/index.php/getcities' + '?' + 'c=' + encodeURIComponent(__country.value) + '&' + 'r=' + encodeURIComponent(__region.value));

__r3_xmlhttpsuccess = function()
                            {
                                _data = ajaxac_receivejsarray(__r3.responseText);
                                populateSelect(__city, _data);
                                enableCity(__city);
                            }

__r3_onreadystatechange = function()
                            {
                                if (__r3.readyState == 4 && __r3.status == 200 && __r3_xmlhttpsuccess) {
                                    __r3_xmlhttpsuccess();
                                }
                            }

__r3.onreadystatechange = __r3_onreadystatechange;

__r3.send(null);
                                    }
                                }
                                catch (e) { }

                                return false;
                            }
__city.onload = function()
                            {
                                this.emptyDefault = 'Please select a country';
                                this.emptyDefaultRegion = 'Please select a region/state';
                                this.fullDefault = 'Please select a city';
                                this.loadingText = 'Loading ...';
                            }

__city.onload();