// TABLEAU VALEUR
// 0(Mixed) : La valeur du champs à tester
// 1(Mixed) : La valeur du champs à tester
// 2(Mixed) : Valeur comparative ex: pass et sa confirmation
// 3(Varchar) : Le nom du champs qui apparaît dans le formulaire (pour l'affichage de l'erreur)
// 4(Bool) : Le champs est-il obligatoire ?
// 5(Varchar) : Le type de champs
// 6(Int) : Facultatif. Correspond au maxlength.
//

var ValidForm = new Class({
	initialize: function(options){
		this.options = options;
		this.error = '';
		this.maxlength = 200;
	},
	
	//On implémente les méthodes de test
	setValidation: function(tableau_a_tester)
	{
		if(isArray(tableau_a_tester)){
			//On test si on a l'id du champs pour placer une ancre dans le message d'erreur
			tableau_a_tester.each(function(valeur,index){
				if(valeur!=undefined){
					if(!isEmpty(valeur[1]) || valeur[1]!=undefined)
						valeur[3] = '<a href="#'+ valeur[1] +'">'+ valeur[3] +'</a>';
					
					//Recquis
					if(valeur[4]==true){
						if(isEmpty(valeur[0]))
							this.error +=  valeur[3] +' est obligatoire<br/>';
					}
					
					if(isEmpty(this.error) && !isEmpty(valeur[0])){
						switch(valeur[5]){
							case 'numeric' :
								if(valeur[6]!=undefined){
									if(valeur[6]<valeur[0].length)
										this.error +=  valeur[3] +' ne doit comporter que '+ valeur[6] +' chiffres<br/>';
								}else if(!isNumeric(valeur[0])){
									this.error +=  valeur[3] +' doit être un numérique<br/>';
								}
								break;
		
							case 'date' :
								if(!isFRDate(valeur[0]))
									this.error +=  valeur[3] +' doit être une date<br/>';
								break;
		
							case 'password' :
								if(valeur[0].length>this.maxlength)
									this.error +=  'Le contenu de '+ valeur[3] +'est trop long<br/>';
								else if(valeur[0]!=valeur[2])
									this.error +=  'Votre '+ valeur[3] +' et sa confirmation ne correspondent pas<br/>';
								break;
		
							case 'varchar' :
								if(valeur[0].length>this.maxlength)
									this.error +=  'Le contenu de '+ valeur[3] +'est trop long<br/>';
								break;
		
							case 'email' :
								if(!isEmail(valeur[0]))
									this.error +=  'Votre '+ valeur[3] +' n\'est pas valide<br/>';
								else if(valeur[0]!=valeur[2])
									this.error +=  'Votre '+ valeur[3] +' et sa confirmation ne correspondent pas<br/>';
								break;
		
							case 'phone' :
								if(!isFRPhoneNumber(valeur[0]))
									this.error +=  'Votre '+ valeur[3] +' n\'est pas valide<br/>';
								break;
						}
					}
				}
			},this);
		}
	}
})

var Formulaire = new Class({
	initialize: function(options){
		this.options = options;
	},
	// Implémentation des Setteurs
	setName: function(vName) {
		this.options.name = vName;
	},
	setId: function(vId) {
		this.options.id = vId;
	},
	setType: function(vType) {
		this.options.type = vType;
	},
	setTitle: function(vTitle) {
		this.options.title = vTitle;
	},
	setOpt: function(vOpt) {
		this.options.opt = vOpt;
	}
});

var Input = new Class({
	Extends: Formulaire,
	initialize: function(options) {
		// Déclaration des variables membres
		this.options = options;
	},
	
	// Implémentation des Setteurs
	setValue: function(vValue) {
		this.options.value = vValue;
	},
	setSize: function(iSize) {
		this.options.size = iSize;
	},
	setMaxlength: function(iMaxlength) {
		this.options.maxlength = iMaxlength;
	},
	
	// Implémentation de la méthode d'affichage
	setAffiche: function()
	{
		var value;
		var size;
		var maxlength;
		var title;
		
		if(this.options.opt!=undefined)
			opt = this.options.opt;
		else
			opt = "";
		
		if(this.options.value!=undefined)
			value = "value='"+ this.options.value +"'" ;
		else
			value = "";
		
		if(this.options.size!=undefined)
			size = "size='"+ this.options.size +"'";
		else
			size = "";
		
		if(this.options.maxlength!=undefined)
			maxlength = "maxlength='"+ this.options.maxlength +"'" ;
		else
			maxlength = "";
		
		if(this.options.title!=undefined)
			title = "title='"+ this.options.title +"'" ;
		else
			title = "";
		
		switch (this.type) {
			case 'checkbox':
				value='';
				size='';
				maxlength='';
				title='';
				if(this.options.value=='on')
					opt += ' checked';
				break ;
			case 'button':
			case 'submit':
				$size='';
				$maxlength='';
				break ;
			case 'hidden':
				size='';
				maxlength='';
				title='';
				break ;
			case 'text':
				title='';
				break ;
		}
		this.affiche = "<input name='"+ this.options.name +"' id='"+ this.options.id +"' type='"+ this.options.type +"' "+ value +" "+ size +" "+ maxlength +" "+ title +" "+ opt +"/>";
	}	
});

var Textarea = new Class({
	Extends: Formulaire,
	initialize: function(options) {
		// Déclaration des variables membres
		this.options = options;
	},
	
	// Implémentation des Setteurs
	setValue: function(vValue) {
		this.options.value = vValue;
	},
	setRows: function(iRows) {
		this.options.rows = iRows;
	},
	setCols: function(iCols) {
		this.options.cols = iCols;
	},
	
	// Implémentation de la méthode d'affichage
	setAffiche: function()
	{
		if(this.options.opt!=undefined)
			opt = this.options.opt;
		else
			opt = "";
		
		this.affiche =  "<textarea name='"+ this.options.name +"' id='"+ this.options.id +"' cols='"+ this.options.cols +"' rows='"+ this.options.rows +"' "+ opt +">";
		this.affiche +=  this.options.value;
		this.affiche +=  "</textarea>\n";
	}
});

var Select = new Class({
	Extends: Formulaire,
	initialize: function(options) {
		// Déclaration des variables membres
		this.options = options;
	},
	
	// Implémentation des Setteurs
	setMultiple: function(vMultiple) {
		this.options.multiple = vMultiple;
	},
	setSize: function(iSize) {
		this.options.size = iSize;
	},
	setValues: function(vValues) {
		this.options.values = vValues;
	},
	setSelectedValue: function(vSelectedValues) {
		this.options.selectedvalues = vSelectedValues;
	},
		
	// Implémentation de la méthode d'affichage
	setAffiche: function()
	{
		var size;
		if(this.options.size!=undefined)
				size =  'size="'+ this.options.size +'"';
		else
				size = '';
		
		if(this.options.opt!=undefined)
			opt = this.options.opt;
		else
			opt = "";
		
		this.affiche = '<select name="'+ this.options.name +'" id="'+ this.options.id +'" '+ opt +'>';
		if(this.options.firstblank!=undefined)
			this.affiche += '<option value=""></option>';
		
		if(this.options.optgroups!=undefined){
			this.options.values.each(function(val,key){
				if(this.options.values[key]!=undefined){
					this.affiche += '<optgroup label="'+ val +'">';
							this.options.values2[key].each(function(val_opts,key_opts){
								if(val_opts!=undefined){
									if(this.options.selectedvalues==key +'.'+ key_opts)
										this.affiche += '<option selected="selected" value="'+ key +'.'+ key_opts +'">'+ val_opts +'</option>';
									else
										this.affiche += '<option value="'+ key +'.'+ key_opts +'">'+ val_opts +'</option>';
								}
							}, this)
					this.affiche += '</optgroup>';
				}
			}, this)
		}else{
			this.options.values.each(function(val,key){
				list_options = '<option value="'+ key +'">'+ val +'</option>';
	
				if(isArray(val)){
					if(this.options.selectedvalues!=undefined){
						if(this.options.selectedvalues==val[0])
							list_options = '<option selected="selected" value="'+ val[0] +'">'+ val[1] +'</option>';
						else
							list_options = '<option value="'+ val[0] +'">'+ val[1] +'</option>';
					}else
						list_options = '<option value="'+ val[0] +'">'+ val[1] +'</option>';
				}else{
					if(this.options.selectedvalues!=undefined){
						if(this.options.selectedvalues==key)
							list_options = '<option selected="selected" value="'+ key +'">'+ val +'</option>';
						else
							list_options = '<option value="'+ key +'">'+ val +'</option>';
					}else
						list_options = '<option value="'+ key +'">'+ val +'</option>';
				}
	
				this.affiche += list_options;
			}, this)
		}
		this.affiche += '</select>';
	}
});