/*
Méthodes de validation dynamique du formulaire 
Copyright Quelle Energie, 2009
*/

// Définition des champs à surveiller, un tableau par étape
var champs_etapes = [
[{name:'maison_description[date_construction_id]', type:'select'}, 
{name:'maison_description[surface_habitable]', type:'text'}, 
{name:'maison_description[hauteur_sous_plafond]', type:'select'},
{name:'maison_description[mitoyennete_id]', type:'radio'},
{name:'maison_description[forme_id]', type:'radio'}] , 
[{name:'maison_description[type_toit_id]', type:'radio'}, 
{name:'maison_description[possede_surface_toit_ensoleillee]', type:'radio'}, 
{name:'maison_description[surface_toit_ensoleillee]', type:'text'},
{name:'maison_description[orientation_id]', type:'select'},
{name:'maison_description[pente_toit_id]', type:'select'},
{name:'maison_description[structure_toit_id]', type:'radio'},
{name:'maison_description[plancher_id]', type:'radio'},
{name:'maison_description[nb_niveaux_habitables]', type:'radio'},
{name:'maison_description[espace_exterieur_id]', type:'select'},
{name:'maison_description[surface_jardin]', type:'text'},
{name:'maison_description[travaux_isolation]', type:'radio'},
{name:'maison_description[travaux_isolation_murs]', type:'radio'},
{name:'maison_description[date_travaux_murs]', type:'select'},
{name:'maison_description[travaux_isolation_plancher_haut]', type:'radio'},
{name:'maison_description[date_travaux_plancher_haut]', type:'select'},
{name:'maison_description[travaux_isolation_plancher_bas]', type:'radio'},
{name:'maison_description[date_travaux_plancher_bas]', type:'select'},
{name:'maison_description[vitrage_id]', type:'select'},
{name:'maison_description[fenetre_materiau_id]', type:'radio'},
{name:'maison_description[possede_vitrage_sud_degage]', type:'radio'},
{name:'maison_description[chgt_fenetre_envisage]', type:'radio'},
{name:'maison_description[nb_fenetres_a_changer]', type:'text'},
{name:'maison_description[nb_fenetres]', type:'text'},
{name:'maison_description[largeur_fenetre]', type:'text'},
{name:'maison_description[hauteur_fenetre]', type:'text'}],
[{name:'maison_description[chauffage_mode_general]', type:'select'}, 
{name:'maison_description[type_chaudiere_gaz]', type:'select'},
{name:'maison_description[type_chaudiere_fuel]', type:'select'},
{name:'maison_description[type_pac]', type:'select'},
{name:'maison_description[type_poele]', type:'select'},
{name:'maison_description[possede_robinets_thermostatiques]', type:'radio'},
{name:'maison_description[ventilation_id]', type:'select'},
{name:'maison_description[possede_programmateur]', type:'radio'},
{name:'maison_description[ecs_mode]', type:'select'},
{name:'maison_description[possede_panneaux_solaires]', type:'radio'},
{name:'maison_description[type_panneaux_solaires]', type:'select'},
{name:'maison_description[puissance_PV_installee]', type:'text'},
{name:'maison_description[possede_climatisation]', type:'radio'},
{name:'maison_description[nb_pieces]', type:'text'},
{name:'maison_description[nb_pieces_climatisees]', type:'text'},
{name:'maison_description[nb_habitants]', type:'select'},
{name:'maison_description[mode_cuisson_id]', type:'radio'},
{name:'maison_description[est_raccordee_au_gaz]', type:'radio'},
{name:'maison_description[statut]', type:'select'},
{name:'maison_description[adresse]', type:'text'},
{name:'maison_description[code_postal]', type:'text'},
{name:'maison_description[commune]', type:'text'},
{name:'particulier[civilite]', type:'radio'},
{name:'particulier[nom]', type:'text'},
{name:'particulier[prenom]', type:'text'},
{name:'particulier[email]', type:'text'},
{name:'particulier[telephone_perso]', type:'text'},
//{name:'particulier[autorisation_contact_partenaires]', type:'radio'},
{name:'maison_description[accept_cgu]', type:'radio'}]
];

var champs_a_valider = new Array;

var Validation = Class.create();

Validation.prototype = {
	initialize : function(etape){
		this.etape=etape;
		champs_a_valider = champs_etapes[etape-1];
		for (var i=0; i<champs_a_valider.length; i++) {
			champ = Validation.getElement(i).each(function(elm) {
				Event.observe(elm, 'focus', function(ev) { Validation.validatePreviousFields(Validation.findField(Event.element(ev)));});
				Event.observe(elm, 'change', function(ev) { Validation.validateRequired(Validation.findField(Event.element(ev))); });
			})
			
		}
	}
}

Object.extend(Validation, {
	validatePreviousFields : function(i) {
		for (var j=0; j<i ; j++) { this.validateRequired(j);}
	},

	validateField : function(i) {
		this.validateRequired(i);
	},

	findField : function(elm) {
		var i=0;
		while(i<champs_a_valider.length) {
			if (champs_a_valider[i].name==elm.name) return i;
			i++;
		}
		return null;
	},

	validateRequired : function(i) {
		if (this.isVisible(i)) 
		{ 
			if (this.isEmpty(i)) {
				this.setError(i, "Ce champ est obligatoire");
				return false;
			}
			else	{
				this.removeError(i);
				return true;
			}
		} 
		else  return true; 
	},
	
	setError : function (i, message) {
		label = this.getLabel(i);
		error_div = this.getErrorDiv(i);
		elements = this.getElement(i);
		if (label) label.addClassName('with_error');
		if (error_div) error_div.update(message).show();
		if (elements) elements.each(function(elm) {elm.parentNode.addClassName('fieldWithErrors');});
	},
	
	removeError : function(i) {
		label = this.getLabel(i);
		error_div = this.getErrorDiv(i);
		elements = this.getElement(i);
		if (label) label.removeClassName('with_error');
		if (error_div) error_div.insert("").hide();
		if (elements) elements.each(function(elm) {elm.parentNode.removeClassName('fieldWithErrors');});
	},
	
	getLabel : function(i) {
		id = champs_a_valider[i].name.replace(/[\[\]]/, "_").replace(/(^\[)^|(\]$)/, "");
		label=$$('label[for="'+id+'"]');
		return label[0];
	},
	
	getErrorDiv : function(i) {
		id = champs_a_valider[i].name.replace(/[\[\]]/, "_").replace(/(^\[)^|(\]$)/, "");
		errorDiv=$$('div[class="erreur_champ"][for="'+id+'"]');
		return errorDiv[0];
	},

	getElement : function(i) {
		var champ = champs_a_valider[i];
		switch (champ.type) {
			case 'select':
			return $$('select[name="'+champ.name+'"]');
			break;
			case 'radio':
			return $$('input[name="'+champ.name+'"]');
			break;
			default: 
			return $$('input[name="'+champ.name+'"]');
		}
	},
	
	getValue : function(i) {
		var champ = champs_a_valider[i];
		switch (champ.type) {
			case 'select':
			var elm = this.getElement(i)[0];
			return elm.value;
			break;
			case 'radio':
			var elms = this.getElement(i);
			return elms.find(function(elm) {
				v = $F(elm)
				return !((v == null) || (v.length == 0));
			});
			break;
			default: 
			return this.getElement(i)[0].getValue();
		}
	},
	
	isEmpty : function(i) {
		v = this.getValue(i);
		return ((v == null) || (v.length == 0));
	},

	
	isVisible : function(i) {
		elm = this.getElement(i)[0];
		while(elm.tagName != 'BODY') {
			if(!$(elm).visible()) return false;
			elm = elm.parentNode;
		}
		return true;
	}
});

function loadValidation(etape) {
	new Validation(etape);
}
