
function xmlForm(form_id, requestPrefix, cssClassEnabled, cssClassDisabled) {
    
    this.url_base           = requestPrefix;
    this.refs               = new Object();
    this.submit_buttons     = new Array();
    
    // Инициялизация на формата
    // Взима всички полета от формата
    // Всички полета с атрибут lang=='require' им се слага статус false
    this.getRequest = function(el) {
        var opt = el.lang ? el.lang.split('@') : null;
        xmlRequest(this.url_base + '&type=' + opt[0] + '&field_name='+ el.id +'&field_value='+el.value.theEscape() + (opt[2] ? '&compare_value='+eval('this.refs.'+opt[2]+'.value') : ''), 'xmlForm_'+this.form_id+'.checkField(\''+el.name+'\')', 'xmlForm_'+this.form_id+'.uncheckField(\''+el.name+'\')');
    }
    
    this.reloadForm = function(form) {
        this.fields = Object();
        this.submit_buttons = null;
        this.submit_buttons = new Array();
        this.have_require = false;
        
        for(i=0; i<form.elements.length; i++) {
            
            if (typeof(form.elements[i].name) != 'undefined' && form.elements[i].name != '') {
                var name_converted = form.elements[i].name.replace(/\[|\]/g,"_");
            } else {
                continue;
            }
            
            eval('this.fields.' + name_converted + "= false");
            eval('this.refs.' + name_converted + "=form.elements[i]");
            
            var check = false;
            
            if (form.elements[i].lang) {
                var options = form.elements[i].lang ? form.elements[i].lang.split('@') : null;
                check = form.elements[i] && typeof(options[1]) != 'undefined' && options[1] == 'required';
            } else eval('this.fields.'+name_converted+"= true");
            
            this.have_require = (check === true) ? true : this.have_require;
            
            var oxmlForm = this;
            
            switch (form.elements[i].type) {
                default :
                    if (check === true) {
                        //if (form.elements[i].value != '') {
                        //Слагаме автоматичния чек
                            this.getRequest(form.elements[i]);
                        //} 
                        
                        form.elements[i].onkeyup = function () {
                            var oThis = this;
                            
                            clearTimeout(this.timeotId);
                            this.timeotId = setTimeout(function () {
                                    oxmlForm.getRequest(oThis);
                                } , 1000);
                        }
                    }
                break;
            
                case 'submit':
                    if (this.submit_buttons) this.submit_buttons[this.submit_buttons.length] = form.elements[i];
                        else this.submit_buttons = [form.elements[i]];
                break;
                
                case 'hidden':
                    if (check === true) {
                        
                        if (form.elements[i].watch) {
                            form.elements[i].watch('value', function (property, old_value, new_value) {
                                var oThis = this;
                                
                                clearTimeout(this.timeotId);
                                this.timeotId = setTimeout(function () {
                                        oxmlForm.getRequest(oThis);
                                    } , 1000);
                                return new_value;
                            });
                        } else if (typeof(form.elements[i].onpropertychange) != 'undefined') {
                            
                            form.elements[i].onpropertychange = function () {
                                
                                if (window.event.propertyName != 'value') return false;
                                
                                var oThis = this;
                                
                                if (oThis.timeotId) clearTimeout(oThis.timeotId);
                                oThis.timeotId = setTimeout(function () {
                                        oxmlForm.getRequest(oThis);
                                    } , 1000);
                            }
                        }
                    }
                break;
                
                case 'checkbox':
                    if (check === true) {
                        form.elements[i].onchange = function () {
                            if (this.checked && this.checked === true)
                                if (this.name) oxmlForm.checkField(this.name);
                            else
                                if (this.name) oxmlForm.uncheckField(this.name);
                        }
                    }
                break;
            }
        }
    }
    
    this.resetForm = function (form) {
        if (form) {
            form.reset();
            this.reloadForm(form);
            this.checkForm();
        }
        
        if (this.have_require === true) {
            disableButton(this.submit_buttons[0]);
            changeCssClass(this.submit_buttons[0], this.cssClassDisabled);
        } else changeCssClass(this.submit_buttons[0], this.cssClassEnabled);
    }
    
    this.init = function (form_id, cssClassEnabled, cssClassDisabled) {
        this.form_id = form_id;
        
        var form = getReference(this.form_id);
        if (form) {
            this.reloadForm(getReference(form_id));
        }
        this.cssClassEnabled = cssClassEnabled;
        this.cssClassDisabled = cssClassDisabled;
        
        if (this.have_require === true) {
            disableButton(this.submit_buttons[0]);
            changeCssClass(this.submit_buttons[0], this.cssClassDisabled);
        } else changeCssClass(this.submit_buttons[0], this.cssClassEnabled);
    }

    this.checkForm = function () {
        for(i in this.fields) {
            if (this.fields[i] !== true) {
                this.error = 'require_field';
                this.error_field = this.fields[i];
                
                this.submit_buttons[0].disabled = true;
                this.submit_buttons[0].className = this.cssClassDisabled;
                
                return false;
            }
        }
        this.submit_buttons[0].disabled = false;
        this.submit_buttons[0].className = this.cssClassEnabled;
        return true;
    }
    
    this.checkField = function(field_name) {
        eval('this.fields.' + field_name.replace(/\[|\]/g,"_") + '= true');
        this.checkForm();
    }
    
    this.uncheckField = function(field_name) {
        eval('this.fields.' + field_name.replace(/\[|\]/g,"_") + '= false');
        this.checkForm();
    }
    
    this.init(form_id, cssClassEnabled, cssClassDisabled);
}