$(document).ready(function(){
    $('#fld-country').change(function() {
        country_change();
    });
    function country_change() {
    //  Empty city select box
        $('#fld-province').empty();
    //  Fill with relevant cities
		var fragment = '/ajax/provinces/?parent='+$('#fld-country').val();
		$('#fld-province').attr('disabled','disabled');
		if (arguments.length==2) {
            var province_id = arguments[0];
            var city_id = arguments[1];
    		$('#fld-province').load(fragment, function() {
                $('#fld-province').val(province_id);
                $('#fld-province').attr('disabled',false);
                province_change(city_id);
            });
		}
		else {
    		$('#fld-province').load(fragment, function(){
                $('#fld-province').attr('disabled',false);
    		});
		}
    }


    $('#fld-province').change(function() {
        province_change();
    });
    function province_change() {
    //  Empty city select box
        $('#fld-city').empty();
        if ($('#fld-province').val()=='ALL') {
            $('#fld-city').html('<option>All cities</option>').attr('disabled','disabled');
            $('#fld-within-all').show();
            $('#fld-within').val(0).attr('disabled','disabled');
        //  Set country id
            var country_id = $('#fld-province :selected').attr('id');
            country_id = parseInt(country_id.substr(12));
            $('#fld-country').val(country_id);
            return;
        }
        else {
            $('#fld-within-all').hide();
            $('#fld-within').attr('disabled',false).val(5);
            $('#fld-country').val(0);
        }
        
    //  Fill with relevant cities
        if ($('#fld-province').hasClass('onlylisted')) {
            var fragment = '/ajax/cities/?onlylisted=1&parent='+$('#fld-province').val();
        }
        else {
            var fragment = '/ajax/cities/?parent='+$('#fld-province').val();
        }
		
		$('#fld-city').attr('disabled','disabled');
		if (arguments.length==1) {
            var city_id = arguments[0];
            
    		$('#fld-city').load(fragment, function() {
                $('#fld-city').val(city_id);
                $('#fld-city').attr('disabled',false);
            });
		}
		else {
    		$('#fld-city').load(fragment, function() {
                $('#fld-city').attr('disabled',false);
    		});
		}
    }
    var postal_code = '';
    $('#fld-postal').focus(function() {
        postal_code = $('#fld-postal').val();
    });
    
    $('#fld-postal').blur(function() {
    //  Check if we have a postal code
        var postal = $(this).val();
        if (postal==postal_code) return;
        if (postal.length>=5 && postal.length<=8) {
        //  Magically load province and city
            $('#fld-country').attr('disabled','disabled');
            $.get('/ajax/postal/?postal='+postal, function(data) {
                if (data!='ERROR' && data.indexOf('|')) {
                //  Country|Province|City received
                    data = data.split('|');
                    $('#fld-country').val(data[0]);
                    country_change(data[1], data[2]);
                }
                $('#fld-country').attr('disabled',false);
            });
        }
    });
    
});




$(document).ready(function(){
    $('#fld-province-m').change(function(){province_change();});
    function province_change() {
    //  Empty city select box
        $('#fld-city-m').empty();
        if ($('#fld-province-m').val()=='ALL') {
            $('#fld-city-m').html('<option>All cities</option>').attr('disabled','disabled');
            $('#fld-within-all-m').show();
            $('#fld-within-m').val(0).attr('disabled','disabled');
        //  Set country id
            var country_id = $('#fld-province-m :selected').attr('id');
            country_id = parseInt(country_id.substr(14));
            $('#fld-country-m').val(country_id);
            return;
        }
        else {
            $('#fld-within-all-m').hide();
            $('#fld-within-m').attr('disabled',false).val(5);
            $('#fld-country-m').val(0);
        }
    //  Fill with relevant cities
        if ($('#fld-province-m').hasClass('onlylisted')) {
            var fragment = '/ajax/cities/?onlylisted=1&parent='+$('#fld-province-m').val();
        }
        else {
            var fragment = '/ajax/cities/?parent='+$('#fld-province-m').val();
        }
		$('#fld-city-m').attr('disabled','disabled');
		$('#fld-city-m').load(fragment, function() {
            $('#fld-city-m').attr('disabled',false);
		});
    }
});
