var AjaxDispatcher = Class.create({
  
  initialize:function(options) {
    this.options = {
      ajaxUrl: '/php/ajax.php'
    };
    Object.extend(this.options, options || {});
  },
  
  setFrontentProcessor:function(objProcessor) {
    this.frontendProcessor = objProcessor;
  },
  
  get:function(identifier, data) {
    this.identifier = identifier;
    this.postdata = {
      identifier:this.identifier,
      action:'get'
    };
    switch (this.identifier) {
      case 'bundeslaender':
      case 'unterregionen':
      
        this.postdata = Object.extend(this.postdata, data || {});
        new Ajax.Request(this.options.ajaxUrl, {
          method: 'post',
          evalScripts: false,
          parameters: this.postdata,
          onFailure:function(response){
            alert('Failure: '+response.status);
            return null;
          },
          onComplete: function(response){
            return this.postprocess(response);
          }.bind(this)
        });

      break;
      default:
        return null;
      break;
    }
    
  },
  
  set:function(identifier, data) {
    this.identifier = identifier;
    this.postdata = {
      identifier:this.identifier,
      action:'set'
    };
    
    switch (this.identifier) {
      case 'objektstatus':
        
        this.postdata = Object.extend(this.postdata, data || {});
        new Ajax.Request(this.options.ajaxUrl, {
          method: 'post',
          evalScripts: false,
          parameters: this.postdata,
          onFailure:function(response){
            alert('Failure: '+response.status);
            return null;
          },
          onComplete: function(response){
            return this.postprocess(response);
          }.bind(this)
        });

      break;
      default:
        return null;
      break;
    }
    
  },
  
  postprocess:function(response) {
    if (this.frontendProcessor) {
      this.frontendProcessor.setData(response.headerJSON);
      this.frontendProcessor.process();    
    }
  }
});


var AjaxFrontend = Class.create({
  
  initialize:function(options) {
    this.options = {};
    Object.extend(this.options, options || {});
  },
  
  setData:function(json) {
    this.dataJSON = json;
  },
  
  process:function() {
    switch (this.options.outputType) {
      case 'selectoptions':
        if (this.options.updateElement != '') {
          $(this.options.updateElement).update(this.makeSelectOptions());
        }
      break;
      case 'updateelement':
        if (this.options.newContent != '' && this.options.updateElement != '' && this.dataJSON.done == '1') {
          $(this.options.updateElement).update(this.options.newContent);
        }
      break;
      default:
      break;
    }
  },
  
  makeSelectOptions:function() {
    this.output = '';
    if (this.options.noValueText) {
      this.output = '<option value="">'+this.options.noValueText+'</option>';
    }
    if (this.dataJSON) {
      for (i=0;i<this.dataJSON.length;i++) {
        this.output += '<option value="'+eval("this.dataJSON[i]."+this.options.valueFieldName)+'">'+eval("this.dataJSON[i]."+this.options.optionFieldName)+'</option>';
      }
    }
    
    return this.output;
  }
});
