STMA.Forms = {
    selectAll: function(id) {
        tbl = $g(id);
        all = $g(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;
            }
        }
    },
    getV: function(v) {
        inp = $g('formInput_'+v);
        if (inp != null) {
            return inp.value;
        }  else {
            return '';    
        }
    },    
    getVal: function(o) {
        return o.value;    
    },
    redir: function(t,a,fl) {
        f = this.getForm();
        action = f.action;
        if (action.indexOf('?') != '-1') {
            temp = action.split('?');
            action = temp.shift();
        }
        if (fl) {
            var reg = /http:\/\/[A-Za-z\-]+?\.?[A-Za-z]+.[A-Za-z]+\//;
            res = reg.exec(action);
            act = res + t + '/';
        } else {
            temp = action;
            if (temp.substr(temp.length-1, 1) == '/') {
                temp = temp.substr(0, temp.length-1);
            }
            temp = temp.split('/');
            if (temp[temp.length-1] != t) {
                act = action + (t ? t + '/' : '');    
            } else {
                act = action;
            }
        }
        
        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();
    },
    setAct: function(a) {
        f = this.getForm();
        
        this.addAttr('action', a);
        
        f.submit();
    },
    addAttr: function(n,v) {
        f = this.getForm();
        
        inp = document.createElement('input');
        inp.type = 'text';
        inp.name = n;
        inp.value = v;
        inp.className = 'hidden';
        f.appendChild(inp);
        
        return inp;
    },
    getForm: function() {
        var forms;
        forms = document.getElementsByTagName('form');
        for(i=0;i<forms.length;i++) {
            item = forms[i];
            return item;
        }
        return null;
    },
    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 = '';
        }        
    },
    def: function(i,v) {
        if (typeof(i) == 'string') {
            /* not defined yet */
            o = $g(i);
            if (!o.value) {
                o.value = v;
            }
            o.defValue = v;
            o.onblur = function(){
                STMA.Forms.def(this)
            };
            o.onfocus = function(){
                if (this.value == this.defValue) {
                    this.value = '';    
                }
            };            
        } else {
            /* was already set - onchange */
            o = i;
            if (!o.value) {
                o.value = o.defValue;
            }
        }

    }
}
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,
    spec: null,
    init: function(o,i,t,sp) {
        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;
            this.spec = sp;
            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;
        this.spec = 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+(this.spec ? ';objSpec='+this.spec : ''),true);
    }
}

STMA.Forms.Table = {
    checkBoxes: function(id) {
        tbl = $g(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) {
        return false;
        obj = $g(id);
        
        var rows = new Array();
        rowsTemp = obj.getElementsByTagName('tr');
        for(a=0;a<rowsTemp.length;a++) {
            rows.push(rowsTemp[a]);
        }
        if (typeof(rows[0]) != 'undefined') {
            if (rows[0].firstChild.className == 'options') {
                rows.shift();
            }
            for(i=0;i<rows.length;i++) {
                if (rows[i].className != 'paging') {
                    if (i != 0 && i%2 == 0) {
                        rows[i].className = 'dark';
                    } else if (i == 1) {
                        rows[i].className = 'first';
                    }
                    if (i>0) {
                        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 = $g(id).parentNode;
            n.removeChild(this.addNode);            
        }  
    },
    init: function(id) {
        obj = $g(id);
        if (this.node == $g(id)) {
            // revert
            f.action = f.tempAction;
            f.setAttribute('enctype', '');
            f.target = '';
            this.node = false;  
            this.id = null;  
        } else {
            // init
            this.node = $g(id);
            this.id = id;    
        }
    },
    submit: function() {
        AIM.submit(STMA.Forms.getForm(),{onComplete: function(){
            div = $g('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 = $g(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($g(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
}
STMA.Forms.Validation = {
    list: new Array(),
    ins: function(o,t) {
      if (!t) {
        t = 'plain';
      }  
      this.list.push(o);
      o.dataType = t;
      o.isValid = false;
      
      o.setAttribute('onchange', 'STMA.Forms.Validation.update(this)');
    },
    exec: function() {
        if (this.list.length) {
            process = true;
            for(i=0;i<this.list.length;i++) {
                var item;
                item = this.list[i];
                if (!this.check(item)) {
                    this.mark(item);
                    process = false;
                }
            }    
            return process;
        } else {
            return true;
        }
    },
    set: function(o,v) {
        o.isValid = v > 0 ? true : false;
        if (!o.busy) {
            this.mark(o);
        }
    },
    update: function(o) {
        p = this.check(o);
        /*
        if (!p) {
            o.focus();
        }
        */
        if (!o.busy) {
            this.set(o,p); 
        }
    }
}
