var Validation = Class.create({
  
    initialize: function() {
        this._error_msg = '';               // error message
        this._css_class = 'red';            // error css class
        this._timeout = 2000;
        
        this.email_ereg = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
        this.username_ereg = /^[a-zA-Z0-9_.@-]{4,40}$/;
        this.cp_ereg = /^[0-9]{5}$/;
        this.phone_ereg = /^[0]{1}[0-9]{9}$/;
        this.phone_mobile_ereg1 = /^[0]{1}[6]{1}[0-9]{8}$/;
        this.phone_mobile_ereg2 = /^[0]{1}[7]{1}[0-9]{8}$/;
        //this.no_chassis_ereg = /^(WAUZZZ|TRUZZZ|WUAZZZ)[a-zA-Z0-9]{5}[0-9]{6}$/;
        this.no_chassis1_ereg = /^(WAUZZZ)[0-9]{1}[a-zA-Z]{1}[a-zA-Z0-9]{3}[0-9]{6}$/
		this.no_chassis2_ereg = /^(WUAZZZ)[0-9]{2}[a-zA-Z0-9]{3}[0-9]{6}$/
		this.no_chassis3_ereg = /^(WUAZZZ)[0-9]{1}[a-zA-Z]{1}[a-zA-Z0-9]{3}[0-9]{6}$/
		this.no_chassis4_ereg = /^(TRUZZZ)[0-9]{1}[a-zA-Z]{1}[a-zA-Z0-9]{3}[0-9]{6}$/
        
        this.immatr_ereg = /^[0-9]{1,4}(\s|)[a-zA-Z]{2,3}(\s|)[0-9]{2}$/;
        this.km_ereg = /^[0-9]{0,}$/;
    },

    check: function(name, obj, obj_label1, obj_label2, obj_label3, obj_label4) {
        if (!obj.checked) {
            this._error_msg += '- ' + name + '\n';
            
            
            if (obj_label1) {
                obj_label1.className = 'red';
                setTimeout('$(\'' + obj_label1.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label2) {
                obj_label2.className = 'red';
                setTimeout('$(\'' + obj_label2.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label3) {
                obj_label3.className = 'red';
                setTimeout('$(\'' + obj_label3.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label4) {
                obj_label4.className = 'red';
                setTimeout('$(\'' + obj_label4.id + '\').className = \'\';', this._timeout);
            }
        }
    },

    checkNotEmpty: function(name, obj, obj_label1, obj_label2, obj_label3, obj_label4) {
        if (obj.value == '' || obj.value == '0') {
            this._error_msg += '- ' + name + '\n';
            
            
            if (obj_label1) {
                obj_label1.className = 'red';
                setTimeout('$(\'' + obj_label1.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label2) {
                obj_label2.className = 'red';
                setTimeout('$(\'' + obj_label2.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label3) {
                obj_label3.className = 'red';
                setTimeout('$(\'' + obj_label3.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label4) {
                obj_label4.className = 'red';
                setTimeout('$(\'' + obj_label4.id + '\').className = \'\';', this._timeout);
            }
        }
    },
    
    checkNumber: function(name, obj, obj_label1, obj_label2, obj_label3, obj_label4) {
        if (!Number(obj.value)) {
            this._error_msg += '- ' + name + '\n';
            
            if (obj_label1) {
                obj_label1.className = 'red';
                setTimeout('$(\'' + obj_label1.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label2) {
                obj_label2.className = 'red';
                setTimeout('$(\'' + obj_label2.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label3) {
                obj_label3.className = 'red';
                setTimeout('$(\'' + obj_label3.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label4) {
                obj_label4.className = 'red';
                setTimeout('$(\'' + obj_label4.id + '\').className = \'\';', this._timeout);
            }
        }
    },
    
    checkPassword: function(id_obj, password, confirm_password, obj_label1, obj_label2, obj_label3, obj_label4) {
        if (id_obj!=null && id_obj.value == ''){
            if ((password.value == '') || ((password.value!=''|| confirm_password.value!='') && password.value != confirm_password.value))
                this._error_msg += '- mot de passe\n';
            }
            
        else{
            if ((password.value!='' || confirm_password.value!='') && password.value != confirm_password.value){
                this._error_msg += '- mot de passe\n';
            }
        }
    
        if (this._error_msg != '') {
            if (obj_label1) {
                obj_label1.className = 'red';
                setTimeout('$(\'' + obj_label1.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label2) {
                obj_label2.className = 'red';
                setTimeout('$(\'' + obj_label2.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label3) {
                obj_label3.className = 'red';
                setTimeout('$(\'' + obj_label3.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label4) {
                obj_label4.className = 'red';
                setTimeout('$(\'' + obj_label4.id + '\').className = \'\';', this._timeout);
            }
        }
    },
    
    checkPhone: function(name, obj, obj_label1, obj_label2, obj_label3, obj_label4) {
        if (!this.phone_ereg.test(obj.value)) {
            this._error_msg += '- ' + name + '\n';
            
            if (obj_label1) {
                obj_label1.className = 'red';
                setTimeout('$(\'' + obj_label1.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label2) {
                obj_label2.className = 'red';
                setTimeout('$(\'' + obj_label2.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label3) {
                obj_label3.className = 'red';
                setTimeout('$(\'' + obj_label3.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label4) {
                obj_label4.className = 'red';
                setTimeout('$(\'' + obj_label4.id + '\').className = \'\';', this._timeout);
            }
        }
    },
     
    checkMobilePhone: function(name, obj, obj_label1, obj_label2, obj_label3, obj_label4) {
        if (!this.phone_mobile_ereg1.test(obj.value) && !this.phone_mobile_ereg2.test(obj.value)) {
            this._error_msg += '- ' + name + '\n';
            
            if (obj_label1) {
                obj_label1.className = 'red';
                setTimeout('$(\'' + obj_label1.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label2) {
                obj_label2.className = 'red';
                setTimeout('$(\'' + obj_label2.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label3) {
                obj_label3.className = 'red';
                setTimeout('$(\'' + obj_label3.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label4) {
                obj_label4.className = 'red';
                setTimeout('$(\'' + obj_label4.id + '\').className = \'\';', this._timeout);
            }
        }
    },
    
    checkCP: function(name, obj, obj_label1, obj_label2, obj_label3, obj_label4) {
        if (!this.cp_ereg.test(obj.value)) {
            this._error_msg += '- ' + name + '\n';
            
            if (obj_label1) {
                obj_label1.className = 'red';
                setTimeout('$(\'' + obj_label1.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label2) {
                obj_label2.className = 'red';
                setTimeout('$(\'' + obj_label2.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label3) {
                obj_label3.className = 'red';
                setTimeout('$(\'' + obj_label3.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label4) {
                obj_label4.className = 'red';
                setTimeout('$(\'' + obj_label4.id + '\').className = \'\';', this._timeout);
            }
        }
    },
    
    checkUsername: function(name, obj, obj_label1, obj_label2, obj_label3, obj_label4) {
        if (!this.username_ereg.test(obj.value)) {
            this._error_msg += '- ' + name + '\n';
            
            if (obj_label1) {
                obj_label1.className = 'red';
                setTimeout('$(\'' + obj_label1.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label2) {
                obj_label2.className = 'red';
                setTimeout('$(\'' + obj_label2.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label3) {
                obj_label3.className = 'red';
                setTimeout('$(\'' + obj_label3.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label4) {
                obj_label4.className = 'red';
                setTimeout('$(\'' + obj_label4.id + '\').className = \'\';', this._timeout);
            }
        }
    },
    
    checkEmail: function(name, obj, obj_label1, obj_label2, obj_label3, obj_label4) {
        if (!this.email_ereg.test(obj.value)) {
            this._error_msg += '- ' + name + '\n';
            
            if (obj_label1) {
                obj_label1.className = 'red';
                setTimeout('$(\'' + obj_label1.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label2) {
                obj_label2.className = 'red';
                setTimeout('$(\'' + obj_label2.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label3) {
                obj_label3.className = 'red';
                setTimeout('$(\'' + obj_label3.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label4) {
                obj_label4.className = 'red';
                setTimeout('$(\'' + obj_label4.id + '\').className = \'\';', this._timeout);
            }
        }
    },
    
    checkNoChassis: function(name, obj, obj_label1, obj_label2, obj_label3, obj_label4) {
        if (!this.no_chassis1_ereg.test(obj.value) &&
        	!this.no_chassis2_ereg.test(obj.value) &&
        	!this.no_chassis3_ereg.test(obj.value) &&
        	!this.no_chassis4_ereg.test(obj.value)) {
        	
            this._error_msg += '- ' + name + '\n';
            
            if (obj_label1) {
                obj_label1.className = 'red';
                setTimeout('$(\'' + obj_label1.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label2) {
                obj_label2.className = 'red';
                setTimeout('$(\'' + obj_label2.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label3) {
                obj_label3.className = 'red';
                setTimeout('$(\'' + obj_label3.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label4) {
                obj_label4.className = 'red';
                setTimeout('$(\'' + obj_label4.id + '\').className = \'\';', this._timeout);
            }
        }
    },
    
    checkImmatr: function(name, obj, obj_label1, obj_label2, obj_label3, obj_label4) {
        if (!this.immatr_ereg.test(obj.value)) {
            this._error_msg += '- ' + name + '\n';
            
            if (obj_label1) {
                obj_label1.className = 'red';
                setTimeout('$(\'' + obj_label1.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label2) {
                obj_label2.className = 'red';
                setTimeout('$(\'' + obj_label2.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label3) {
                obj_label3.className = 'red';
                setTimeout('$(\'' + obj_label3.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label4) {
                obj_label4.className = 'red';
                setTimeout('$(\'' + obj_label4.id + '\').className = \'\';', this._timeout);
            }
        }
    },
    
    checkKm: function(name, obj, obj_label1, obj_label2, obj_label3, obj_label4) {
        if (!this.km_ereg.test(obj.value)) {
            this._error_msg += '- ' + name + '\n';
            
            if (obj_label1) {
                obj_label1.className = 'red';
                setTimeout('$(\'' + obj_label1.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label2) {
                obj_label2.className = 'red';
                setTimeout('$(\'' + obj_label2.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label3) {
                obj_label3.className = 'red';
                setTimeout('$(\'' + obj_label3.id + '\').className = \'\';', this._timeout);
            }
            
            if (obj_label4) {
                obj_label4.className = 'red';
                setTimeout('$(\'' + obj_label4.id + '\').className = \'\';', this._timeout);
            }
        }
    },
    
    validateUsername: function(name, obj, obj_label1) {
        new Ajax.Request(CONFIG_URI, {
            method: 'GET',
            encoding: 'utf-8',
            parameters: 'call=validateUsername' + 
                        '&username='+ obj.value + 
                        '&name='+ name +
                        '&label='+ obj_label1.id,
            onSuccess: function(transport) {
                response = transport.responseText.evalJSON();                
                
                if (response.error){
                    validation._error_msg += '- ' + response.name + '\n';
                                                            
                    if ($(response.label)) {
                        $(response.label).className = 'red';
                        setTimeout('$(\'' + response.label + '\').className = \'\';', validation._timeout);
                    }                    
                }
            },
            onFailure: function(){ alert('Server busy'); }
        });
    },
    
    validateEmail: function(name, obj, obj_label1) {
        new Ajax.Request(CONFIG_URI, {
            method: 'GET',
            encoding: 'utf-8',
            parameters: 'call=validateEmail' + 
                        '&email='+ obj.value + 
                        '&name='+ name +
                        '&label='+ obj_label1.id,
            onSuccess: function(transport) {
                response = transport.responseText.evalJSON(); 
                               
                if (response.validates==0){
                    validation._error_msg += '- ' + response.name + '\n';
                                                            
                    if ($(response.label)) {
                        $(response.label).className = 'red';
                        setTimeout('$(\'' + response.label + '\').className = \'\';', validation._timeout);
                    }
                }
            },
            onFailure: function(){ alert('Server busy'); }
        });
    }
});

