var Validator = Class.create();
Validator.prototype = {
    
    form_name: null,
    str_id: null,
    control: null,
    param: {},
    no_error: true,
    
    initialize: function(form_name, str_id, control, param) {
        this.form_name = form_name;
        this.str_id = str_id;
        this.control = control;
        this.param = param;
    },
    
    validate: function()
    {
        return true;
    },
    
    set_error: function(mess)
    {
        this.no_error = false;
        var error_block = 'error_' + this.form_name + '_' + this.str_id;
        $(error_block).update(mess);
        $(error_block).show();
    },
    
    set_no_error: function()
    {
        var error_block = 'error_' + this.form_name + '_' + this.str_id;
        $(error_block).update('');
        $(error_block).hide();
    }    
}

var LengthValidator = Class.create();

LengthValidator.prototype = {

    form_name: null,
    str_id: null,
    control: null,
    param: {},
    no_error: true,
    
    initialize: function(form_name, str_id, control, param) {
        this.form_name = form_name;
        this.str_id = str_id;
        this.control = control;
        this.param = param;
    },
    
    set_error: function(mess)
    {
        this.no_error = false;
        var error_block = 'error_' + this.form_name + '_' + this.str_id;
        $(error_block).update(mess);
        $(error_block).show();
    },
    
    set_no_error: function()
    {
        var error_block = 'error_' + this.form_name + '_' + this.str_id;
        $(error_block).update('');
        $(error_block).hide();
    },       
    
    validate: function()
    {
            
        var value = this.control.value;
        if (!this.param.required && !value)
            return true;

        if (this.param['max_length'])
        {
            if (value.length > this.param['max_length'])
            {
                this.set_error('Значение поля должно быть короче ' + this.param['max_length'] + ' символов');
            }
        }

        if (this.param['min_length'])
        {
            if (value.length < this.param['min_length'])
            {
                this.set_error('Значение поля должно быть длиннее ' + this.param['min_length'] + ' символов');
            }
        }

        if (this.no_error)
            this.set_no_error();
            
        return this.no_error;
    }

};

var RegexpValidator = Class.create();

RegexpValidator.prototype = {

    form_name: null,
    str_id: null,
    control: null,
    param: {},
    no_error: true,
    
    initialize: function(form_name, str_id, control, param) {
        this.form_name = form_name;
        this.str_id = str_id;
        this.control = control;
        this.param = param;
    },
    
    set_error: function(mess)
    {
        this.no_error = false;
        var error_block = 'error_' + this.form_name + '_' + this.str_id;
        $(error_block).update(mess);
        $(error_block).show();
    },
    
    set_no_error: function()
    {
        var error_block = 'error_' + this.form_name + '_' + this.str_id;
        $(error_block).update('');
        $(error_block).hide();
    },     
       
    validate: function()
    {
        var value = this.control.value;
        
        if (!this.param.required && !value)
            return true;
        var reg_text = this.param['regexp'].strip()
        var reg_split = reg_text.split('\/');
        var reg_param = reg_split.pop();
        reg_text = reg_split.join('/');
       
        reg_text = reg_text.replace(/^\//, '').replace(/\/$/, '');

        var req = new RegExp(reg_text, reg_param);
        if (this.param['regexp'] && !req.test(value))
            this.set_error(this.param.errmsg ? this.param.errmsg : 'Поле принимает недопустимое значение');

        if (this.no_error)
            this.set_no_error();
            
        return this.no_error;
    }
};

var DateValidator = Class.create();
DateValidator.prototype = {

    form_name: null,
    str_id: null,
    day_control: null,
    month_control: null,
    year_control: null,
    param: {},
    no_error: true,
    
    initialize: function(form_name, str_id, day_control, month_control, year_control, param) {
        this.form_name = form_name;
        this.str_id = str_id;
        this.day_control = day_control;
        this.month_control = month_control;
        this.year_control = year_control;
        this.param = param;
    },
    
    set_error: function(mess)
    {
        this.no_error = false;
        var error_block = 'error_' + this.form_name + '_' + this.str_id;
        $(error_block).update(mess);
        $(error_block).show();
    },
    
    set_no_error: function()
    {
        var error_block = 'error_' + this.form_name + '_' + this.str_id;
        $(error_block).update('');
        $(error_block).hide();
    },     
       
    validate: function()
    {
        var day = this.day_control.value;
        var month = this.month_control.value;
        var year = this.year_control.value; 
        if (!this.param.required && !day && !month && !year)
            return true;       
        if (day > -1 || month > -1 || year > -1)
        {
            if (((!/\d+/.test(year)) || year < this.param['min_year'] || year > this.param['max_year'] ) || 
                (day != -1 && ((!/\d+/.test(day)) || day < 1 || day > 31)) || (month != -1 && ((!/\d+/.test(month)) || month < 1 || month > 12)) ||
                ((day != -1 || month != -1) && year == -1) ||
                (day != -1 && month == -1 && year != -1)
                )
                    this.set_error('Недопустимая дата');
        }
                
        if (this.no_error)
            this.set_no_error();
            
        return this.no_error;
    }
};
