﻿function provinceCity(province, city) {
    var cities = [];
    
    $.get('/city.xml',
        function(xml) {
            createProvince(xml);
        }
    );

    $('#province').change(function() {
        provinceChange(this.selectedIndex);
    });

    function createProvince(xml)
    {
        $(xml).find('name').each(function(i) {
            var name = $(this).text();
            $('<option value="' + name + '">' + name + '</option>').appendTo('#province');
        });
        $(xml).find('city').each(function(i) {
            cities[i] = $(this).text();
        });
        provinceCitySelecte(province, city);
    }

    function provinceChange(index)
    {
        $('#city').empty();
        var city = cities[index].split(" ");
        $(city).each(function(i) {
            $('<option value="' + city[i] + '">' + city[i] + '</option>').appendTo('#city');
        });
    }

    function provinceCitySelecte(province, city)
    {
        //setTimeout just for IE6.0 "Could not set the selected property. Unspecified error."
        setTimeout(function() {
            $('#province > option[value=' + province + ']').attr('selected', 'true');
            $('#province').change();
            setTimeout(function() {
                $('#city > option[value=' + city + ']').attr('selected', 'true');
            }, 1);
        }, 1);
    }
}
