var rmc = {};
rmc.RMC = new Class({
	Implements : Options,
	
	options : {
		datasXml : null, //objet contenant les datas venant des xml
		root : 'RMC', //the form here
		resultElement : 'RMCresults'
	},
	
	fieldsById : {},
	allFields : [],

	
	initialize : function(options) {
		this.setOptions(options);
		this.root = $(this.options.root);
		this.resultElement = $(this.options.resultElement);
		if(!this.root) return;
		this._initFields();
		this._initListeners();
	},
	
	_initListeners : function() {
		var self = this;
		this.root.addEvent('submit', this._formSubmitHandler.bindWithEvent(this));
		
		var inputDate = this.fieldsById.trouverDisponibilite_datedepartInp;
		var inputDateValue = inputDate.value.split('/');
		var selectJour = this.fieldsById.selectJour_dateDepart;
		var selectMois = this.fieldsById.selectMois_dateDepart;
		
		selectJour.addEvent('change',function(e){
			new Event(e).stop();
			var selectedJour =  selectJour.options[selectJour.selectedIndex];
			inputDateValue[0] = selectedJour.value;
			inputDate.value=[selectedJour.value,inputDateValue[1],inputDateValue[2]].join('/');
			self.root.fireEvent('submit');
		});
		
		selectMois.addEvent('change',function(e){
			new Event(e).stop();
			var selectedMois =  selectMois.options[selectMois.selectedIndex];
			inputDateValue[1] = selectedMois.value.split('/')[0];
			inputDateValue[2] = selectedMois.value.split('/')[1];
			inputDate.value=[inputDateValue[0],selectedMois.value].join('/');
			self.root.fireEvent('submit');
		});
		
		inputDate.addEvent('update', function(){
			var inputDateValue = inputDate.value.split('/');
			var moisAnnee = [inputDateValue[1],inputDateValue[2]].join('/');
			
			selectJour.getElements('option').each(function(option){
				if (option.value == inputDateValue[0])
					option.selected = true;
			});
			
			selectMois.getElements('option').each(function(option){
				if (option.value == moisAnnee)
					option.selected = true;
			});
			
			self.root.fireEvent('submit');
		});
		
		this.fieldsById.trouverDisponibilite_datedepartInp.addEvent('click', function(e){new Event(e).stop()});
		this.fieldsById.trouverDisponibilite_datedepartInp.getParent().addEvent('click', this._dateDepartClickHandler.bindWithEvent(this));
	},
	
	_initFields : function() {
		/* get all fields */
		var self = this;
		var fields = this.root.getElements('input,select');
		this.allFields = fields.map(function(field){
			if (field.id) {
				this.fieldsById[field.id] = $(field);
				
				$(field).addEvent('change', function (e){
					if (self.root)
						self.root.fireEvent('submit', e);
				});
				
				return field;
			}
			return null;
		}.bind(this)).filter(function(field){
			return field!=null;
		});
		this.checkBoxesAndRadios = this.allFields.filter(function(field){
			return field.type=="checkbox" || field.type=='radio';
		});
		
		var toDay = new Date();
		//this.fieldsById.trouverDisponibilite_datedepartInp.value = toDay.getDate()+'/'+(toDay.getMonth()+1)+'/'+toDay.getFullYear();
		
		this.resultElement.addEvent('click', function (e){
			if (this.root)
				this.root.fireEvent('submit', e);
		})
		
		// remplissage des combos box
		
		//select nombre d'adultes
		DomUtils.generateSelectOptions(this.fieldsById.trouverDisponibilite_nbadultes, this.options.datasXml.reservationDirecte.data.nombresadultes.adulte.map(function(adulte, index){
			return {text:adulte, value:parseInt(adulte)};
		}));
		//select nombre d'enfants
		DomUtils.generateSelectOptions(this.fieldsById.trouverDisponibilite_nbenfants, this.options.datasXml.reservationDirecte.data.nombresenfants.enfant.map(function(enfant, index){
			return {text:enfant, value:parseInt(enfant)};
		}));
		
		new CMToggle({root:$('activites')})
	},
	
	//listeners 
	_formSubmitHandler : function(e) {
		if (e)
			new Event(e).stop();
		var url = this._getUrlByScenarios();
		var xhr = new AGAjax({
			method:'post',
			url : url,
			async : true,
			noCache : true,
			onSuccess : function(xhr) { // on xhr is complete and is OK
				this._htmlPageLoadedHandler(xhr);
			}.bind(this)
		})
		xhr.send();
	},
	
	_dateDepartClickHandler : function(e) {
		new DatePicker().show(this.fieldsById.trouverDisponibilite_datedepartInp.parentNode, {destField:this.fieldsById.trouverDisponibilite_datedepartInp});
	},
	
	
	_htmlPageLoadedHandler : function(xhr) {
		var html = xhr.responseText;
		html = html.substring(html.indexOf('<!-- Start body -->'), html.indexOf('<!-- End body -->'));
		this.resultElement.innerHTML = html;
	},
	
	
	_getUrlByScenarios : function() {
			//this method must go to hell !
	},
	
	//utils
	_oneFieldChecked : function() { //teste si au moins un champ est checké
		return this._getNumberOfCheckedFields()>0;
	},
	
	_getNumberOfCheckedFields : function() {
		return this.allFields.filter(function(field) {
			return field.checked==true;
		}).length;
	},
	
	_oneOrMoreIsChecked : function(fields) {
		return fields.filter(function(field){
			return field.checked;
		}).length>0;
	},
	
	// on prend le tableau de tous les inputs, on retire ceux qui servent au scénario, puis on regarde dans ce qui restent ceux qui sont cochés.
	// on suite on retourne true ou false
	_noOtherFieldsAreChecked : function(fields) { 
		return $A(this.checkBoxesAndRadios).filter(function(field){
			return !fields.contains(field);
		}).filter(function(field){
			return field.checked;
		}).length == 0;
	}
	
	
})
