City.Firms.FilterForm = Class.create({
  onPrepared: Prototype.emptyFunction,
  
  initialize: function( args ) {
    this.form       = $( args.form );
    this.submitBtn  = $( args.submitBtn );
    this.city       = args.city;
    this.cityID     = args.cityID;  
    this.district   = args.district;
    this.redirectTo = '';
  }, // initialize
  
  prepare: function() {
    this.loadData();
  }, // prepare
  
  submit: function() {
    if ( this.redirectTo != '' ) location.href = this.redirectTo;
    else alert( 'Виберіть, будь-ласка, галузь!' );
  }, // submit
  
  loadData: function() {
    var url  = '/firms/profiles_tree/';
    var self = this;
    
    var requestParams = {
      method: 'GET',
      parameters: {
        city : self.cityID 
      },
      onComplete: function() {
        try {
          if ( this.transport.status == 200 ) {
            var xmlDOC     = this.transport.responseXML;
            var treeSelect = new TreeSelect({
              xmlDoc      : xmlDOC,
              treeAlign   : 'hAlign',
              emptyOption : '- галузь -'
            });
            
            treeSelect.onchange = function() {
              self.redirectTo = '';
              if ( this.xvalue.length == 2 ){
                if ( this.xvalue[1] != 0 ) self.redirectTo = '/' + self.district + '/' + self.city + '/firms/profile/' + this.value + '/';
                else {
                  if ( this.xvalue[0] != 0 ) self.redirectTo = '/' + self.district + '/' + self.city + '/firms/industry/' + this.xvalue[0] + '/';
                } // if  
              } else {
                 if ( this.xvalue[0] != 0 ) self.redirectTo = '/' + self.district + '/' + self.city + '/firms/industry/' + this.xvalue[0] + '/';
              } // if    
            } // onchange
            
            self.select = treeSelect;
            self.onPrepared();
            
          } // if
        } catch( error ) {
          jsLog( error );
        } // try
      } // onComplete
    } // var requestParams
    
    new Ajax.Request( url, requestParams );
  }, // loadData
  
  initSubmitButton: function() {
  } // initSubmitButton
}); // City.Firms.FilterForm
