
var Page = {};

/**
 * Création d'un objet pour le traitement des listes
 */
Page.List = Class.create();
Page.List.prototype = {

	/**
	 * constructeur de la classe
	 */
	initialize: function(options) {
		
		// Définition des parametres
		this.url			= 'ajax/list.php?t=1';
		this.action 		= 'action.php';
		this.id_container	= 'adm-list';
		this.id_form_search	= 'form_search';
		this.id_form_list	= 'form_list';
		this.img_loader		= '<img src="img/loader.gif" height="16" width="16" />';
		this.strGET			= '';
		this.strSearch		= '';
		this.strList		= '';
		this.page			= 0;
		this.sort			= 1;
		this.limit			= 10;
		this.order			= 'ASC';
		this.options		= {};
	
		Object.extend(this.options, options || {});
		
		// on affecte les propriétés des options
		for (var o1 in this) {
			for (var o2 in this.options) {
				if (o1==o2) {
					this[o1] = this.options[o2];
				}
			}
		}
		
		
		if($(this.id_container)){
			this.container = $(this.id_container);
		}else{
			alert("Attention, le conteneur n'est pas valide.");
		}
	}, // fin du constructeur

	/**
	 * affiche le tableau de liste
	 */
	display: function (options)
	{
		this.options		= {};
	
		Object.extend(this.options, options || {});

		if(this.options.onFinish){
			onfinish = this.options.onFinish;
		}
		
		if ($(this.id_form_search)) { this.strSearch = "&" + $(this.id_form_search).serialize(); }
		if ($(this.id_form_list)) {	this.strList = "&" + $(this.id_form_list).serialize(); }
		
		this.strGET = this.strSearch + this.strList;
	
		var container = this.container;
	
		container.update(this.img_loader);
		
		new Ajax.Request(this.url + this.strGET, {
			method: 'get',
			onSuccess: function(transport) {
				
				container.update(transport.responseText);
				
				if(onfinish)
				onfinish();
				
			}
		});
	},
	
	/**
	 * change la page courante de la liste
	 */
	changePage: function (page)
	{
		$(this.id_form_list).elements['p'].value = page;
		this.page = page;
		this.display();
	},

	/**
	 * Sort et Order de la liste
	 */
	sortList: function (s,o)
	{
		if($(this.id_form_list)){
			$(this.id_form_list).elements['sort'].value = s;
			$(this.id_form_list).elements['order'].value = o;
			this.sort = s;
			this.order = o;
			this.display();
		}else{
			alert("Le formulaire de la liste n'est pas valide pour le tri.")	
		}
	}
	
}; // fin de List





/**
 * Création d'un formulaire
 */
Page.Form = Class.create();
Page.Form.prototype = {

	/**
	 * constructeur de la classe
	 */
	initialize: function(form, options) {
		
		// Définition des parametres
		this.form		= form;
		this.options	= {};
		
		Object.extend(this.options, options || {});
		
		// on affecte les propriétés des options
		for (var o1 in this) {
			for (var o2 in this.options) {
				if (o1==o2) {
					this[o1] = this.options[o2];
				}
			}
		}

		if(!$(this.form)){
			alert("Attention le nom du formulaire est vide ou inexistant");
		}	

	} // fin du constructeur
	,
	/**
	 * verification du formulaire
	 */
	verif: function(data, options){ // veririfcation du formulaire
		
		this.bgColorAlerte = "#DDDDDD";
		this.bgColorBase = "#ffffff";
		this.ftColorAlerte = "#000000";
		this.ftColorBase = "#000000";
		this.valid = true;
		this.message = "<strong>Attention erreur de saisie !</strong>";
		
		Object.extend(this.options, options || {});
		
		// on affecte les propriétés des options
		for (var o1 in this) {
			for (var o2 in this.options) {
				if (o1==o2) {
					this[o1] = this.options[o2];
				}
			}
		}
		
		cptDisplayError = 0;
		
		for (var element in data) {
		
			validLigne = true;
			
			var elt = $(this.form).elements[element]; 			
			elt = $(elt);
		
			if(elt){
				
				elt.setStyle({backgroundColor:this.bgColorBase,color:this.ftColorBase});
				
				switch(data[element].type){
			
					case "email":
						if( ((data[element].need)||(elt.getValue()!="")) && isNotEmailAdress(elt.getValue()) ){
							elt.setStyle({backgroundColor:this.bgColorAlerte,color:this.ftColorAlerte});
							this.valid = false;
							validLigne = false;
						}
					break;
					
					case "cp":
						if( ((data[element].need)||(elt.getValue()!="")) && isNotCodePostal(elt.getValue()) ){
							elt.setStyle({backgroundColor:this.bgColorAlerte,color:this.ftColorAlerte});
							this.valid = false;
							validLigne = false;
						}
					break;
					
					case "int":
						if( ((data[element].need)||(elt.getValue()!="")) && isNotInt(elt.getValue()) ){
							elt.setStyle({backgroundColor:this.bgColorAlerte,color:this.ftColorAlerte});
							this.valid = false;
							validLigne = false;
						}
					break;
					
					case "float":
						if( ((data[element].need)||(elt.getValue()!="")) && isNotFloat(elt.getValue()) ){
							elt.setStyle({backgroundColor:this.bgColorAlerte,color:this.ftColorAlerte});
							this.valid = false;
							validLigne = false;
						}
					break;
					
					case "tel":
						if( ((data[element].need)||(elt.getValue()!="")) && isNotTelephone($elt.getValue(),data[element].typetel) ){
							elt.setStyle({backgroundColor:this.bgColorAlerte,color:this.ftColorAlerte});
							this.valid = false;
							validLigne = false;
						}
					break;
					
					case "password":
						if($(data[element].pass2)){
							$(data[element].pass2).setStyle({backgroundColor:this.bgColorBase,color:this.ftColorBase});
							
							if(data[element].need){
								if( ( elt.getValue()=="" ) || (elt.getValue()!=$(data[element].pass2).getValue() ) ){
									elt.setStyle({backgroundColor:this.bgColorAlerte,color:this.ftColorAlerte});
									$(data[element].pass2).setStyle({backgroundColor:this.bgColorAlerte,color:this.ftColorAlerte});
									this.valid = false;
									validLigne = false;
								}
							}else{
								if( ( elt.getValue()=="" ) || (elt.getValue()!=$(data[element].pass2).getValue() ) ){
									elt.setStyle({backgroundColor:this.bgColorAlerte,color:this.ftColorAlerte});
									$(data[element].pass2).setStyle({backgroundColor:this.bgColorAlerte,color:this.ftColorAlerte});
									this.valid = false;
									validLigne = false;
								}
							}
						}else{
							alert("La confirmation du mot de passe est obligatoire");
						}
					break;
					
					default:
						if((elt.getValue()=="")&&(data[element].need)){
							elt.setStyle({backgroundColor:this.bgColorAlerte,color:this.ftColorAlerte});
							this.valid = false;
							validLigne = false;
						}
					break;
				}

				if( (data[element].mincar)&&(elt.getValue().length<data[element].mincar)) {
					elt.setStyle({backgroundColor:this.bgColorAlerte,color:this.ftColorAlerte});
					this.valid = false;
					validLigne = false;
				}
				
				if( (!validLigne) &&(data[element].message)){
					if(cptDisplayError==0){
						this.message += '<ul>';
					}
					this.message += "<li>"+data[element].message+"</li>";
					cptDisplayError ++;
				}
				
			}else{
				alert("Attention, le champs '"+element+"' n'existe pas !!")	
			}
		}
		
		if( (!this.valid)&&(this.message!="")){
			
			if(cptDisplayError>0){
			this.message += "</ul>";
			}
			
			new Display.Message(this.message, {cssClass: "adm-alerte",speed:0.3});
		}
		
		return this.valid;
	}


	
}; // fin de Form.Verif



