STMA.Forms = {
    selectAll: function(id) {
        tbl = $(id);
        all = $(id+'_selectAll');
        set = (all.checked ? true : false);
        all.checked = set;
        var inputs;
        var total = 0;
        inputs = tbl.getElementsByTagName('input');
        for(i=0;i<inputs.length;i++) {
            item = inputs[i];
            if (item.type == 'checkbox' && item.name.search(id+'_item') != '-1') {
                item.checked = set;
            }
        }
    },
    redir: function(t,a,fl) {
        f = this.getForm();
        if (fl) {
            var reg = /http:\/\/[A-Za-z]+?\.?[A-Za-z]+.[A-Za-z]+\//;
            res = reg.exec(f.action);
            act = res + t + '/';
        } else {
           act = f.action + (t ? t + '/' : '');  
        }
        f.setAttribute('action', act);
        
        this.deactAll();
        
        if (typeof(a) != "undefined") {
            args = STMA.splitArgs.call(a);
            
            if (args.length) {
                for(i=0;i<args.length;i++) {
                    item = args[i];
                    input = document.createElement('input');
                    input.setAttribute('type','hidden');
                    input.setAttribute('name',item[0]);
                    input.setAttribute('value',item[1]);
                    f.appendChild(input);
                }
            }            
        }
        
        f.submit();
    },
    getForm: function() {
        var forms;
        forms = document.getElementsByTagName('form');
        return forms[0];
    },
    deactAll: function() {
        f = this.getForm();
        
        var inputs = f.getElementsByTagName('input');
        for(i=0;i<inputs.length;i++) {
            inputs[i].disabled = true;
            inputs[i].name = '';
            inputs[i].value = '';
        }
        
        var inputs = f.getElementsByTagName('textarea');
        for(i=0;i<inputs.length;i++) {
            inputs[i].disabled = true;
            inputs[i].name = '';
            inputs[i].innerHTML = '';
        }        
    }
}
STMA.Forms.Tabs = {
    swap: function(o,id) {
        p = STMA.seekParent.call(o, 'tabSelector');
        if (typeof(p.index) == "undefined") {
            p.index = 0;
        }
        
        var res;
        res = p.getElementsByTagName('div');
        
        for(i=0;i<res.length;i++) {
            item = res[i];
            var items;
            items = item.getElementsByTagName('div');
            var children = new Array();
            for(a=0;a<items.length;a++) {
                node = items[a];
                if (node.parentNode == item) {
                    children.push(node);
                }
            }            
            if (item.parentNode == p) {
                if (item.className == 'navigation') {
                    children[p.index].className = 'tab';
                    children[id].className = 'tab focus';
                } else if (item.className == 'content') {
                    children[p.index].className = 'hidden';
                    children[id].className = '';
                }                
            }             

        }
        
        p.index = id;
    }     
}
STMA.Forms.ASCEdit = {
    node: null,
    skip: false,
    init: function(o,i,t) {
        if (this.skip) {
            this.skip = false;
            return false;
        }
        if (o == this.node) {
            return false;
        } else {
            if (this.node != null) {
                this.revert();    
            }
            this.id = i;
            this.type = t;
            this.node = o;
            source = o.firstChild;
            source.className = 'hidden';
            
            div = source.nextSibling;
            div.className = '';
            input = div.firstChild;
            if (typeof(input.value) != "undefined") {
                input.value = source.innerHTML;
            } else {
                input.innerHTML = source.innerHTML;
            }
            input.select();            
        }
    },
    revert: function(s,sk) {
        o = this.node;

        span = o.firstChild;
        input = span.nextSibling;

        if (s) {
            val = input.firstChild.value;
            span.innerHTML = val;
        }
        
        span.className = '';
        if (span.innerHTML == '' || span.innerHTML == '(prázdná hodnota)') {
            span.className = 'empty';
            span.innerHTML = '(prázdná hodnota)';
        }
        input.className = 'hidden';
        
        this.node = null;   
        if (sk) {
            this.skip = true;
        } 
    },
    update: function() {
        input = this.node.lastChild.firstChild;
        if (typeof(input.value) == "undefined") {
            input.value = input.innerHTML;
        }
        
        STMA.AJAX.init('ased',{onComplete: function(r){STMA.Forms.ASCEdit.revert(true)}},'objType='+this.type+';refId='+this.id+';code='+input.name+';value='+input.value,true);
    }
}

STMA.Forms.Table = {
    checkBoxes: function(id) {
        tbl = $(id);
        var items;
        items = tbl.getElementsByTagName('input');
        var nodes = new Array();
        
        for(i=0;i<items.length;i++) {
            item = items[i];
            if (item.type == 'checkbox' && item.name.search(id+'_item_') != '-1' && item.checked) {
                res = item.name.split('_');
                val = res.pop();
                nodes.push(val);
            }    
        }
        
        if (nodes.length) {
            STMA.AJAX.tempArgs += 'nodes='+nodes.join(',');    
        }
    },
    redraw: function(id) {
        obj = $(id);
        
        var rows;
        rows = obj.getElementsByTagName('tr');
        for(i=0;i<rows.length;i++) {
            if (i != 1 && i%2 == 1) {
                rows[i].className = 'dark';
            } else if (i == 2) {
                rows[i].className = 'first';
            }
            if (i>1) {
                if (rows[i].className) {
                    rows[i].className += ' node';
                } else {
                    rows[i].className = 'node';
                }
            }
        }
    }
}

STMA.Forms.Filebox = {
    add: function(o,id,c) {
        this.init(id);
        if (this.node) {
            n = document.createElement('div');
            n.className = 'popup';
            n.id = 'addFileInput';

            this.addNode = n;
            par = this.node.parentNode;
            opt = par.firstChild;
            par.insertBefore(n, opt.nextSibling);
            
            STMA.AJAX.init('file-input', {node: 'addFileInput'},'refId='+STMA.Core.refId, true);
            
            f = STMA.Forms.getForm();
            f.tempAction = f.action;
            f.action = '/shared/process/files-core.inc.php?action=upload&code='+c+'&objType='+STMA.Core.objType;
            f.setAttribute('enctype', 'multipart/form-data');
        } else {
            
            n = $(id).parentNode;
            n.removeChild(this.addNode);            
        }  
    },
    init: function(id) {
        obj = $(id);
        if (this.node == $(id)) {
            // revert
            f.action = f.tempAction;
            f.setAttribute('enctype', '');
            f.target = '';
            this.node = false;  
            this.id = null;  
        } else {
            // init
            this.node = $(id);
            this.id = id;    
        }
    },
    submit: function() {
        AIM.submit(STMA.Forms.getForm(),{onComplete: function(){
            div = $('aimInsertDiv');
            value = div.firstChild.contentWindow.document.body.innerHTML;
            STMA.Forms.Filebox.node.innerHTML += value;
            STMA.Forms.Filebox.update(STMA.Forms.Filebox.id,1);
            
            STMA.Forms.Filebox.init(STMA.Forms.Filebox.id);
            an = STMA.Forms.Filebox.addNode;
            an.parentNode.removeChild(an);
            
            div.parentNode.removeChild(div);
        }});
    },
    update: function(id, amount, node) {
        n = $(id);
        if (typeof(n.nodes) == 'undefined') {
            n.nodes = 0;
        }
        
        n.nodes = n.nodes + amount;
        
        if (n.nodes == 0) {
            n.style.display = 'none';
        } else {
            n.style.display = 'block';
        }
        
        if (node) {
            n.removeChild(node);
                    
        }                
    },
    load: function(id, code) {
        STMA.AJAX.init('files-core',{node: id, onComplete: function(r){
            var nodes;
            nodes = STMA.getElementsByClassname.call($(id), 'fileNode');
            STMA.Forms.Filebox.update(id, nodes.length);
        }},'action=load;code='+code+';refId='+STMA.Core.refId,true);
    },
    del: function(o) {
        n = STMA.seekParent.call(o, 'fileNode');
        id = n.id.split('_').pop();
        STMA.AJAX.init('files-core',{onComplete: function(r) {
            if (r == '') {
                STMA.Forms.Filebox.update(n.parentNode.id,-1,n);
            }
        }},'action=del;refId='+id,true);
    },
    node: false
}
