FormManager = {
    form: null,
    rules: null,
    
    init: function(formNode, rules, requiredFields)
    {
        var self = this;
        this.form = formNode;
        this.rules = rules;
        this.requiredFields = requiredFields;
        
        Event.add(this.form, 'submit', function() {
            return self.validateForm.call(self);
        });
        
        for (var i = 0; i < this.form.length; ++i) {
            var checkUrlNode = document.getElementById(this.form[i].id + 'CheckUrl');
            
            if (this.form[i].name)
            {
                if (this.rules[this.form[i].name])
                {
                    if (checkUrlNode)
                    {
                        Event.add(checkUrlNode, 'click', function(id) {
                            return function() {
                                 self.checkUrl.call(self, id);
                            }
                        }(this.form[i].id));
                    }
                    
                    var validateFunc = function(field) {
                        var currentField = field;
                        return function(event) {
                            self.validateField.call(self, currentField);
                        }
                    }(this.form[i].name);
                            
                    Event.add(this.form[this.form[i].name], 'keydown', validateFunc);
                    Event.add(this.form[this.form[i].name], 'keyup', validateFunc);
                    Event.add(this.form[this.form[i].name], 'change', validateFunc);
                }
            }
        }
        
        Event.add(this.form.country, 'change', function() {
            if (this.selectedIndex) {
                FormManager.markFieldValid('country');
            } else {
                FormManager.hideOK(this.id + 'OK');
            }
        });
        
        Event.add(this.form.founded_year, 'change', function() {
            if (this.selectedIndex) {
                FormManager.showOK(this.id + 'OK');
            } else {
                FormManager.hideOK(this.id + 'OK');
            }
        });
    },
    
    validateForm: function() {
        var invalid = false;
        
        if (this.form.country.selectedIndex) {
            this.markFieldValid('country');
        } else {
            this.markFieldInvalid('country');
            var invalid = true;
        }
        
        for (var field in this.rules) {
            if (!this.validateField(field)) {
                invalid = true;
            }
        }
        
        return !invalid;
    },
    
    validateField: function(field, ignore)
    {
        var invalid = false;
        
        for (var ruleKey in this.rules[field]) {
            if (!RegExp(this.rules[field][ruleKey], "i").test(this.form[field].value)) {
                invalid = true;
                break;
            }
        }
        
        if (this.form[field].value == '') {
            if (!inArray(this.requiredFields, field)) {
                if (!ignore) {
                    this.markFieldEmpty(field);
                }
                invalid = false;
            } else {
                if (!ignore) {
                    this.markFieldInvalid(field);
                }
                invalid = true;
            }
        } else if (!invalid) {
            if (!ignore) {
                this.markFieldValid(field);
            }
        } else {
            if (!ignore) {
                this.markFieldInvalid(field);
            }
        }

        return !invalid;
    },


    checkUrl: function(id) {
        var node = document.getElementById(id);
        var checkUrlLink = document.getElementById(id + 'CheckUrl');
        
        if (this.validateField(node.name, true)) {
            httpRequest('/ajax/check_url/?url_to_check=' + encodeURIComponent(node.value), function(responce) {
                if (responce == 'found') {
                    checkUrlLink.className = 'green';
                } else {
                    checkUrlLink.className = 'red';
                }
            });
        } else {
            checkUrlLink.className = 'red';
        }
    },
    
    markFieldInvalid: function(fieldName) {
        this.form[fieldName].className = 'invalid';
        this.hideOK(this.form[fieldName].id + 'OK');
    },
    
    markFieldEmpty: function(fieldName) {
        this.form[fieldName].className = 'empty';
        this.hideOK(this.form[fieldName].id + 'OK');
    },
    
    reset: function() {
        for (var field in this.rules) {
            this.markFieldEmpty(field);
            var chekUrlNode = document.getElementById(this.form[field].id + 'CheckUrl');
            if (chekUrlNode) {
                chekUrlNode.className = "blue";
            }
        }
        
        this.hideOK('signupCountryOK');
        this.hideOK('signupFoundedOK');
    },
    
    markFieldValid: function(fieldName) {
        this.form[fieldName].className = 'valid';
        this.showOK(this.form[fieldName].id + 'OK');
    },
    
    showOK: function(id) {
        var okNode = document.getElementById(id);
        if (okNode) {
            okNode.style.visibility = 'visible';
        }
    },
    
    hideOK: function(id) {
        var okNode = document.getElementById(id);
        if (okNode) {
            okNode.style.visibility = 'hidden';
        }
    }
} 
