var agent_location = 
{
	construct: function()
	{
        var self = this;

		this.$city = $('field_8');
        this.$state = $('field_21');
        this.$zip = $('field_20');

        if ( this.$city==null || this.$state==null || this.$zip==null )
        {
            return false;
        }

        this.$location_info = new Element('div', {id: 'location_info'});
        this.$location_info.inject(this.$city, 'after');

        this.set_info();

        this.$city.setStyle('width', '150px');
        this.$state.getParents('tr')[0].setStyle('display', 'none');

        new bsn.AutoSuggest('field_8', {
            script: 'ajax_requests.php?task=suggest_location&',
            varname: 'city',
            json:true,
            timeout:4000,
			multisuggest: false,
            callback: function(data) {
                var state = data.info.substr(data.info.indexOf(',')+1);

                self.$zip.set('value', data.id);
                self.$state.set('value', state.trim());

                self.set_info(state);
            }
        });
	},

    set_info: function(text)
    {
        if ( text == undefined )
        {
            var state = this.$state.get('value');
            var zip = this.$zip.get('value');

            if ( state.length && zip.length ) {
                text = state; 
            }
            else {
                return false;
            }
        }

        var location = '<b>' + text + '</b>';
        this.$location_info.set('html', location);   
    }
};

