
var OrderForm = new Class({

  initialize: function (element) {

    this.element = $(element);

    // do nothing if something is missing
    if (!this.element) { return; }

    // Element names are case sensitive in Webkit
    this.activeService = null;

    this.s  = $('submit');
    this.t  = this.s.parentNode;
    this.s1 = $('s1');
    this.s2 = $('s2');
    this.s3 = $('s3');
    this.s4 = $('s4');
    this.ss = $$('select.service_selector').getLast();
    this.ps = $$('select.package').getLast();
    this.gs = $$('select.group').getLast();

    // install event handlers
    this.ss.addEvent('change', this.onChangeServiceSelector.bind(this));
    this.ps.addEvent('change', this.updateService1.bind(this));
    this.gs.addEvent('change', this.updateService1.bind(this));

    // open agb in new window
    var links = $$('#form_order a');
    if (links && links.getLast() ) {
      links.getLast().setProperty('target','_blank');
    }

    // disable all rows of service 1
    this.disableService1Rows();

    // disable all by default
    this.disableAllServices();

    // get visible service from page alias in url
    var strPageAlias = document.URL.match(/\/([^\/]*)\.[^\.]*$/)[1];
    switch (strPageAlias) {

      case 'auftragsformular-rundum-service':
        this.disableServiceSelector();
        this.enableService(this.s1);
        break;

      case 'auftragsformular-einzel-services':
        this.disableServiceSelector();
        this.enableService(this.s2);
        break;

      case 'anmeldeformular-bewerbertraining':
        this.disableServiceSelector();
        this.enableService(this.s3);
        break;
      case 'auftragsformular-arbeitgeber-service':
        this.disableServiceSelector();
        this.enableService(this.s4);
        break;
    }
  },

  disableServiceSelector: function() {
    var ss = $$('tr.service_selector').getLast();
    if (ss && ss.parentNode) {
      ss.parentNode.removeChild(ss);
    }
  },


  disableService: function(el) {
    if (el && this.t) {
      this.t.removeChild(el);
    }
  },

  enableService: function(el) {
    if (el && this.s && this.t) {
      this.disableActiveService();
      this.t.insertBefore(el, this.s);
      this.setActiveService(el);
    }
  },

  disableAllServices: function() {
    this.setActiveService(null);
    this.disableService(this.s1);
    this.disableService(this.s2);
    this.disableService(this.s3);
    this.disableService(this.s4);
  },

  disableActiveService: function(el) {
    if (this.activeService) {
      this.disableService(this.activeService);
      this.activeService = null;
    }
  },

  setActiveService: function(el) {
    this.activeService = el;
  },

  onChangeServiceSelector: function() {
    switch (this.ss.value) {
      case '1':
        this.enableService(this.s1);
        break;
      case '2':
        this.enableService(this.s2);
        break;
      case '3':
        this.enableService(this.s3);
        break;
      case '4':
        this.enableService(this.s4);
        break;
      default:
        this.disableActiveService();
        break;
    }
  },

  disableService1Rows: function() {
    this.S1Rows = new Array();
    this.S1Rows_visible = new Array();
    $$('tr.s1').each( function(item, index){
      this.S1Rows.push(this.s1.removeChild(item));
    }.bind(this));
  },

  updateService1: function() {
    var e = new Array();
    var p = this.ps.value;
    var g = this.gs.value;
    
    if (g) {
      e = this.S1Rows.filter(function(item, index){ return (item.hasClass(g)); });
    }

    if (p) {
      e = e.filter(function(item, index){ return (item.hasClass(p)); });
    }

    while (this.S1Rows_visible.length > 0) {
      this.s1.removeChild(this.S1Rows_visible.pop());
    }
    
    e.each(function(item, index){
      this.S1Rows_visible.push(item);
      this.s1.appendChild(item);
    }.bind(this));

  }

});

window.addEvent('domready',function() { new OrderForm('form_order'); }); 
