var form = Class.create();
form.prototype = {
	initialize:function(form, langue){
		this.error = false;
		this.form = form;
		this.langue = langue;
		this.fields = new Array();
		this.form.getElements().each(function(f) {this.fields.push(new field(f, this.form, this.langue));},this);
		this.submitBtt = this.form.down('button.sbtt');
		//Event.observe(this.form, 'submit', this.send.bindAsEventListener(this),false);
		Event.observe(this.submitBtt,'click',this.send.bindAsEventListener(this),false);
	},
	send:function() {
		var obj = this;
		this.error = false;
		this.fields.each(function(f) {if(f.test()) {this.error = true;}},this);
		if (!this.error) {
			this.submitBtt.disabled = true;
			this.form.request({ 
				onComplete: function(rtn){
					var data = rtn.responseText.evalJSON(true);
					if(data['reload']) {window.location = data['reload'];}
					else if(data['replace']) {
						obj.form.innerHTML = data['replace'];}
					else if(data['alert']) {
						alert(data['alert']);
						obj.fields.each(function(f) {f.test();});
					}
					obj.submitBtt.disabled = false;
				}
			})
		} else {
			if (this.langue=='fr') {alert('Veuillez remplir tout les champs correctement avant de soumettre le formulaire');}
			if (this.langue=='en') {alert('Please fill all field correctly before submiting the form');}
		}
	}
}
	
var field = Class.create();
field.prototype = {
	initialize:function(field, form, langue){
		this.lg = langue;
		this.message = '';
		this.field = field;
		this.form = form;
		this.container = this.field.up();
		this.error = false;
		this.value = this.field.value;
		if (this.field.value != '') {this.init = this.field.value;} 
		else {this.init = null;}
		if (this.field.hasClassName('compa')) {Event.observe(this.field,'focus',this.compa.bindAsEventListener(this),false);}
		Event.observe(this.field,'keyup',this.test.bindAsEventListener(this),false);
		Event.observe(this.field,'change',this.test.bindAsEventListener(this),false);
		this.field.insert ({'after':'<div class="state"></div><div class="warning">test</div>'});
		this.warning = this.field.next('div.warning');
		this.warning.hide();

	},
	validate:function() {
		if (!this.error) {
			if (this.container.hasClassName('error')){this.container.removeClassName('error')};
			this.container.addClassName('ok');
			this.warning.hide();
			this.warning.innerHTML = '';
		} else {
			if (this.container.hasClassName('ok')){this.container.removeClassName('ok')};
			this.container.addClassName('error');
			this.warning.show();
			this.warning.innerHTML = this.message;
		}
	},
	maj:function() {
		
		var filter = /^([0-9]{4})[-][01][0-9][-][0123][0-9]$/;
		if(filter.test(this.field.value)) {												 
			if (this.field.value != '') {
				var td = new Date();
				var bd = new Date();
				td.setFullYear(td.getFullYear()-18,td.getMonth(),td.getDate());
				var b =this.field.value.split('-');
				bd.setFullYear(b[0],b[1],b[2]);
				if (bd >= td) {
					if (this.lg == 'fr') {this.message = 'minimum de 18 ans requis';}
					if (this.lg == 'en') {this.message = 'The minimum age is 18';}
					this.error = true;
				}
			}
		}else{
			if (this.lg == 'fr') {this.message = 'Format invalide';}
			if (this.lg == 'en') {this.message = 'Invalid format';}
			this.error = true;
		}
	},
	req:function(){
		if (this.field.value == '') {
			if (this.lg == 'fr') {this.message = 'Ce champ est obligatoire';}
			if (this.lg == 'en') {this.message = 'This field is mandatory';}
			this.error = true;
		}
	},
	mail:function() {	
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(!filter.test(this.field.value)) {
			if (this.lg == 'fr') {this.message = 'Adresse courriel invalide';}
			if (this.lg == 'en') {this.message = 'Invalid e-mail address';}
			this.error = true;
		}
	},
	cp:function() {
		var filter = /^[a-zA-Z][0-9][a-zA-Z]( {0,1})[0-9][a-zA-Z][0-9]$/;
		if(!filter.test(this.field.value)) {
			if (this.lg == 'fr') {this.message = 'Code postal invalide';}
			if (this.lg == 'en') {this.message = 'Invalid postal code';}
			this.error = true;
		}
	},
	tel:function() {
		var filter = /^([0-9]{3})[ .-]([0-9]{3})[ .-]([0-9]{4})$/;
		if(!filter.test(this.field.value)) {
			if (this.lg == 'fr') {this.message = 'Numéro de téléphone invalide';}
			if (this.lg == 'en') {this.message = 'Invalid phone number';}
			this.error = true;
		}
	},
	compa:function() {
		this.form.getInputs(this.field.type, this.field.name).each(function(t) {
			if(t.hasClassName('compb')) {t.value = '';}
		});
	},
	compb:function() {
		compval = '';
		this.form.getInputs(this.field.type, this.field.name).each(function(t) {
			if(t.hasClassName('compa')) {compval = t.value;}
		});
		if(compval != this.field.value) {
			if (this.lg == 'fr') {this.message = 'Erreur de validation';}
			if (this.lg == 'en') {this.message = 'Validation Error';}
			this.error = true;
		}
	},
	minl:function() {
		if (this.field.value.length < 6) {
			if (this.lg == 'fr') {this.message = 'Minimum de 6 caractères';}
			if (this.lg == 'en') {this.message = '6 characters minimum';}
			this.error = true;
		}
	},
	unique:function() {
		if (this.field.value != this.init) {
			var obj = this;
			var options = {
				parameters: {f: this.field.id, v: this.field.value},
				onSuccess: function(rtn){
					if(rtn.responseText == "false") {
						if (obj.lg == 'fr') {obj.message = 'Cette valeur est déjà utilisée';}
						if (obj.lg == 'en') {obj.message = 'This value is already used';}
						obj.error = true;
						obj.validate();
					}
    			}
			}
			//new Ajax.Request('ajax/account.testunique.php', options);
		}
	},
	test:function() {
		this.error = false;
		if(this.field.hasClassName('req') && !this.error) {this.req();}
		if(this.field.hasClassName('minl') && !this.error) {this.minl();}
		if(this.field.hasClassName('mail') && !this.error) {this.mail();}
		if(this.field.hasClassName('cp') && !this.error) {this.cp();}
		if(this.field.hasClassName('tel') && !this.error) {this.tel();}
		if(this.field.hasClassName('compb') && !this.error) {this.compb();}
		if(this.field.hasClassName('maj') && !this.error) {this.maj();}
		if(this.field.hasClassName('unique') && !this.error) {this.unique();}
		this.validate();
		return(this.error);
	}
}
$$('form.vlf').each(function(f) {new form(f, 'fr');});