// Class validate
var ValidateFormV2 = new Class({
		
		Implements: [Options, Events],
		options: {
			formSelector: '.formValidatorV2',
			btnForm: '.btnText',
			msgContainer: null,
			showErrorMsg: true,
			autoSendForm: true
		},
		
		initialize: function(options){
			this.setOptions(options);
			this.disposedMsgBox=null;
			this.errorArray = [];
			this.errorMsgList = new Hash({
				1	 : 'Attention!',
				1000 : 'Tous les champs marqués d\'un astérique (<span class="orange">*</span>) sont obligatoires.',
				1001 : 'Seulement les chiffres sont permis.',
				1002 : 'Le format de l\'adresse courriel n\'est pas valide.',
				1003 : 'Les mots de passe ne sont pas indentiques.',
				1004 : 'Le nombre de caractères est insuffisant.',
				1005 : 'Veuiller selectionner parmis les choix.'
			});
			
			$$(this.options.formSelector).each(function(form,index) {
				form.addEvent('submit', function(e) {
					e.preventDefault();
					this.errorArray.empty();
					this.currentForm = form;
					this.validateForm();
				}.bind(this));
				form.getElements(this.options.btnForm).addEvents({
					'click': function(e) {
						e.preventDefault();
						this.errorArray.empty();
						this.currentForm = form;
						this.validateForm();
					}.bind(this)
				},this);
			},this);
			
		},
		
		validateForm:function() {
			focusSet = false;
			this.currentForm.getElements('.req').each(function(formEl,index) {
				formEl.removeClass('erreur');
				tagType = formEl.get('tag');
				valPassed = true;
				switch(tagType) {
					case 'input': case 'select': case 'textarea':
						valPassed = this.validateElement(formEl);
					break;
					default:
						valPassed = this.validateElement(formEl);
					break;
				}
				if(valPassed==false && focusSet==false) {
					formEl.focus();
					focusSet = true;
				}
			}, this);
			
			this.chkToSendForm();
		},
		
		validateElement: function(formEl) {
			if(formEl.getProperty('value')=='') {
				return (this.validateDefault(formEl)==true) ? true : this.stackErrors(formEl,1000);
			}
			if(formEl.hasClass('num')) {
				return (this.validateNum(formEl)==true) ? true : this.stackErrors(formEl,1001);
			}
			if(formEl.hasClass('email')) {
				return (this.validateEmail(formEl)==true) ? true : this.stackErrors(formEl,1002);
			}
			if(formEl.hasClass('pwdCheck') || formEl.hasClass('pwdCheckNext')) {
				return (this.validatePwd(formEl)==true) ? true : this.stackErrors(formEl,1003);
			}
			if(formEl.getProperty('class').test("char")) {
				return (this.validateCharNum(formEl)==true) ? true : this.stackErrors(formEl,1004);
			}
			if(formEl.getProperty('tag')=='select') {
				return (this.validateDefault(formEl)==true) ? true : this.stackErrors(formEl,1005);
			}
			
		},
		
		validateDefault: function(formEl) {
			return (formEl.getProperty('value')!='') ? true : false;
		},
		
		validateNum: function(formEl) {
			return (formEl.getProperty('value').test(/[0-9]/)) ? true : false;
		},
		
		validateEmail: function(formEl) {
			//return (formEl.getProperty('value').test("[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})"));
			return (formEl.getProperty('value').test(/^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*[.]([a-z]{2,4})$/));
		},
		
		validatePwd: function(formEl) {
			return (formEl.getProperty('value')!='' && this.currentForm.getElement('.pwdCheckNext').getProperty('value')==this.currentForm.getElement('.pwdCheck').getProperty('value')) ? true : false;
		},
		
		validateCharNum: function(formEl) {
			theClass = formEl.getProperty('class');
			charNum = theClass.match(/char(\d+)/)[1];
			return (charNum==formEl.getProperty('value').length) ? true : false;
		},
		
		stackErrors: function(formEl,errorCode) {
			formEl.addClass('erreur');
			this.errorArray.push(errorCode);
			return false;
		},
		
		outPutErrors: function(errorKey) {
			if($chk(this.disposedMsgBox)) {
				this.disposedMsgBox.set('html', '<h3>'+this.errorMsgList[1]+'</h3> '+this.errorMsgList[errorKey]).injectTop($(this.options.msgContainer) || this.currentForm);
			} else {
				this.msgErrorBox = new Element('div',{
					'id': 'msgV2',
					'class': 'msgV2 attention',
					'html': '<h3>'+this.errorMsgList[1]+'</h3> '+this.errorMsgList[errorKey]
				}).injectTop($(this.options.msgContainer) || this.currentForm);
			}
		},
		
		chkToSendForm: function() {
			($chk(this.msgErrorBox)) ? this.disposedMsgBox = this.msgErrorBox.dispose() : null;
			if(this.errorArray.length>0) {
				if(this.options.showErrorMsg==true) {
					this.outPutErrors(this.errorArray[0]);
				}
			} else {
				if(this.options.autoSendForm==true) {
					this.sendForm();
				} else {
					this.fireEvent('sendForm');
				}
			}
			
		},
		
		sendForm: function() {
			if(this.options.autoSendForm==true) {
				this.currentForm.submit();
			} else {
				return true;
			}
		}
		
});



