//Gestion des tabs
if (typeof Effect == 'undefined')
	throw("tabs.js requires including script.aculo.us' effects.js library!");
var tabset = Class.create();
tabset.prototype = {
  tabs:           [], 
  panels:         [],
  showPanel:     null,
  currentPanel:  null,
  initialize: function(container, options) {
    this.options = Object.extend({
      classNames: {
        tab:        'tab',
        panel:      'panel',
        tabActive:  'selected'
      },
      ids: {
        tab:        'tab_',
        panel:      'panel_'
      },
      onEvent:      'click',
      effects:      false
    }, options || {} );
    container = $(container);
    this.tabs = container.select('.'+this.options.classNames.tab);
    this.tabs.each(function(tab) {
      Event.observe(tab, this.options.onEvent, this.activate.bind(this, tab), false);
    }.bind(this));
    this.panels = container.select('.'+this.options.classNames.panel);
    this.panels.invoke('hide');
  },
  activate: function(tab) {
    var tabName = tab.id.replace(this.options.ids.tab,'');
    this.currentPanel = this.options.ids.panel+tabName;
    if (this.showPanel == this.currentPanel) {
      return false;
    }
    if (this.showPanel) {
      if (this.options.effects) {
        new Effect.Fade(this.showPanel, {duration: 0.5, queue: 'front'});
      } else {
        $(this.showPanel).hide();//$(this.currentPanel).hide();
      }
    }
    if (this.options.effects) {
      new Effect.Appear(this.currentPanel, {duration: 0.5, queue: 'end'});
    } else {
      $(this.currentPanel).show();//$(this.showPanel).show();
    }
    this.tabs.invoke('removeClassName', this.options.classNames.tabActive);
    tab.addClassName(this.options.classNames.tabActive);
    this.showPanel = this.currentPanel;
  },
  getHash: function() {
    var hash = window.location.hash;
    return hash.substring(1); // remove #
  },
  autoActivate: function(tab) {
    var canDoAuto = false;
    var hash = this.getHash();
    if (hash) {
      var autoTab = $(this.options.ids.tab+hash);
      if (autoTab) {
        this.activate(autoTab);
        canDoAuto = true;
      }
    }
    if (!canDoAuto && tab) {
      this.activate(tab);
    }
  }
}

//Ajax recherche
var SearchForm = Class.create({
	initialize : function(){
	this.max = 4;
	$('btn_obt').observe('click', function(e){
		  	new Ajax.Updater('grp_check', 'search.php',{
  				asynchronous:false,
  				parameters:{langp:$F('langp'),town:$F('town'),region:$F('region'),sectort:$F('sector'),mels:$F('mels'),weak:$F('weak'),ehdaa:$F('ehdaa'),retard:$F('retard'),codes:$F('code'),btn_obt:$F('btn_obt')}
  				});
			this.observeCheck(this.max);
		}.bind(this));
	},
	observeCheck : function(max){
		var count = 0;
		$$('input.checkschool').invoke('observe','click',function(e){
			var item = e.element();
			if($F(item)){
				if(count == max)
				{
					alert('Pas plus de '+ max + ' écoles.')
					item.checked = false;
				}
				else
				{
					count++;
				}			
			}
			else{count--;}	
		});
	}	
})
	
//instanciation de  la classe
var formSearch = new SearchForm();
