//	Javascript class developped by Michaël Villar
//	http://www.nemstudio.com/
//	Require script.aculo.us library
//	Cette création est mise à disposition selon le Contrat Paternité-Partage des Conditions Initiales 
//	à l'Identique 2.0 Belgique disponible en ligne http://creativecommons.org/licenses/by-sa/2.0/be/ 
//	ou par courrier postal à Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

AJAXForm = Class.create();
AJAXForm.prototype = {
	valider : Object,
	objects : Array,
	inputNames : Array,
	values : Array,
	valueNames : Array,
	url : "",
	options : '',
	
	initialize: function(valider,url) {
		this.options = Object.extend({
			method:"post"}, arguments[2] || {});
		
		this.url = url;
		this.valider = valider;
		
		this.valider.ref = this;
		this.valider.onclick = function() {
			this.ref.validForm();
			return false;
		}
		
		this.objects = new Array();
		this.inputNames = new Array();
		this.values = new Array();
		this.valueNames = new Array();
	},
	
	validForm : function() {
		var postbody = "";
		for(i=0;i<this.objects.length;i++) {
			if (postbody != "")
				postbody += "&";
		
			val = this.objects[i].value;
			if (this.objects[i].type == "select-one") {
				val = this.objects[i].options[this.objects[i].selectedIndex].value;
			}
			if (this.objects[i].type == "radio") {
				if (this.objects[i].checked == "1")
					val = this.objects[i].value;
				else
					val = "";
			}
			postbody += this.inputNames[i] + "=" + encodeURIComponent(val);
		}
		for(i=0;i<this.values.length;i++) {
			if (postbody != "")
				postbody += "&";
				
			postbody += this.valueNames[i] + "=" + encodeURIComponent(this.values[i]);
		}
		this.options.postBody = postbody;
		new Ajax.Request(this.url, this.options);
	},
	
	addInput : function(name,obj) {
		this.inputNames.push(name);
		this.objects.push(obj);
	},
	
	addValue : function(name,value) {
		this.valueNames.push(name);
		this.values.push(value);
	}
}