var SystechForm = new Class({
    initialize: function() {
        $$('form select[class^=conditional-]').each(function(el) {
            el.conditional = new Hash();
            var conditionals = el.getProperty('class').split(' ');
            for (var loop = 0; loop < conditionals.length; loop++) {
                var value = conditionals[loop].split('-');
                var prefix = value.shift();
                if (prefix == 'conditional' && value.length >= 2) {
                    var id      = el.getProperty('id');
                    var cnd     = $(id);
                    var val     = value.shift();
                    var name    = value.join('-');
                    var tgt     = $(name);
                    if (tgt) {
                        tgt.fx = new Fx.Slide(tgt, {
                            link: 'chain'
                        });
                        if (tgt.hasClass('closed')) {
                            tgt.removeClass('closed');
                            tgt.fx.hide();
                        }
                        tgt.toggle = this.toggle;
                        if (!cnd.conditional[val]) {
                            el.conditional[val] = new Array();
                        }
                        el.conditional[val][el.conditional[val].length] = tgt;
                    }
                }
            }
            el.toggle = this.toggle;
            el.addEvent('change', el.toggle);
            el.fireEvent('change');
        }.bind(this));
    },
    toggle: function() {
        var value = this.get('value');
        this.conditional.each(function(val, key) {
            for (var loop = 0; loop < val.length; loop++) {
                if (value == key) {
                    val[loop].fx.slideIn();
                } else {
                    val[loop].fx.slideOut();
                }
            }
        });
    }
});

var Systech_Validator = new Class({
    validateHook: null,
    validateEvent: null,
    form: null,
    initialize: function(frm) {
        this.form = $(frm);
        if (!this.form) {
            return false;
        }
        this.form.set('send', {
            async: false,
            url: this.form.getProperty('action') + '?format=json',
            onComplete: this.notify.bind(this),
            onError: this.error.bind(this)
        });
        this.validateHook = this.validate.bindWithEvent(this);
        this.form.addEvent('submit', this.validateHook);
    },
    validate: function(e) {
        this.validateEvent = new Event(e);
        $$('.zend-form-error').each(function(item) {
            item.destroy();
        });
        this.form.send();
    },
    notify: function(json, xml) {
        try {
            var obj = JSON.decode(json);
            if (obj.response) {
                if (obj.response == 'true') {
                    this.form.removeEvent('submit', this.validateHook);
                    this.form.fireEvent('submit');
                    return true;
                }
                var keys = JSON.decode(obj.response);
                for (var key in keys) {
                    var el = this.form.getElement('[name=' + key + ']');
                    if (el) {
                        var msgs = keys[key];
                        var div = new Element('div').addClass('zend-form-error').injectAfter(el);
                        for (var msg in msgs) {
                            var p = new Element('div').appendText(msgs[msg]).injectInside(div);
                        }
                        div.highlight('#ddf', '#fff');
                    }
                }
            }
        } catch (e) {
            this.error();
        }
        this.validateEvent.stop();
    },
    error: function() {
        alert('error');
    }
});

window.addEvent('domready', function() {
    new SystechForm();
});