STMA.Forms.Validation.check = function(o) {
    o.busy = false;
    p = false;
    if (o.value == o.cacheVal) {
        //alert('cached > '+o.id+ "\r\n"+o.value+"\r\n"+o.cacheVal+"\r\n"+o.isValid);
        p = o.isValid;
    } else {
        switch(o.dataType) {
            case 'plain':
            if (o.value != '') {
                p = true;
            }
            break;
            case 'username':
            if (o.value != '') {
                o.busy = true;
                STMA.AJAX.init('check-username', {onComplete: function(r){
                    o.busy = false;
                    STMA.Forms.Validation.set(o,r);
                    if (r == 0) {
                        alert('Toto uživatelské jméno je již obsazené!');    
                    }
                }}, 'username='+o.value, true);         
            }
            break;
            case 'password':
            v = o.value;
            if (v.length >= 3) {
                if (v.length <= 16) {
                    p = true;    
                } else {
                    alert('Zadané heslo je příliš dlouhé!');    
                }
            } else {
                alert('Zadané heslo je příliš krátké!');
            }
            break;
            case 'password-check':
            ref = $g('formInput_Password').value;
            if (o.value == ref) {
                p = true;
            } else {
                if (ref) {
                    alert('Zadaná hesla se neshodují!');
                }
            }
            break;  
            case 'e-mail':
            v = o.value;
            if (v.indexOf('@') != -1 && v.indexOf('.') != -1) {
                p = true;
            } else {
                alert('Zadejte prosím platný e-mail!');
            }
            break;
        }
        //alert('new > '+o.id+"\r\n"+o.value + "\r\n"+o.id+ "\r\n"+o+"\r\n"+p);
    }
    o.cacheVal = STMA.Forms.getVal(o);
    o.isValid = p;
    return p;
}

STMA.Forms.Validation.mark = function(o) {
    if (!o.busy) {
        o.className = o.isValid ? '' : 'invalid';    
    }
    
}

STMA.Forms.AddItems = {
    maxW: 400,
    active: false,
    objType: null,
    cell: null,
    bounder: null,
    bg: null,
    hPad: 71,
    catW: 150,
    cache: null,
    lastNode: null,
    update: function(o,i) {
        if (this.lastNode != null) {
            this.lastNodeReplace.parentNode.replaceChild(this.lastNode, this.lastNodeReplace);
        }
        this.lastNode = o;
        val = o.innerHTML;
        
        txt = document.createTextNode(val);
        this.lastNodeReplace = txt;
        
        o.parentNode.replaceChild(txt, o);
        
        cache = this.cache.join(',');
        STMA.AJAX.init('add-items.items',{node: this.itemsCont},'catId='+i+';cache='+cache+';refId='+STMA.Core.refId+';noCache=true;objType='+this.objType,true); 
    },
    check: function(o) {
        val = o.value;
        if (o.checked) {
            this.cache.push(val);    
        } else {
            var id = this.cache.indexOf(val);
            if (id != '-1') {
                this.cache.splice(id, 1);
            }
        }
    },
    init: function(o,f,n,t,a) {
        if (!this.active) {
            this.objType = t;
            this.cache = new Array();
            this.ajaxFile = f;
            this.ajaxNode = n;
            link = o.parentNode;
            td = link.parentNode;
            w = td.offsetWidth;
            
            bg = document.createElement('div');
            bg.className = 'addItemIconBg';
            td.replaceChild(bg, link);
            bg.appendChild(link);
            this.bg = bg; 
            
            this.cell = td;
            
            this.width = (w > this.maxW ? this.maxW : w) - 8;
            
            bounder = document.createElement('div');
            bounder.id = 'addItems';
            bounder.style.width = w+'px';
            
            td.appendChild(bounder); 
            this.bounder = bounder; 
            
            canvas = document.createElement('div'); 
            canvas.className = 'canvas';
            canvas.style.width = this.width+'px'; 
            bounder.appendChild(canvas);
            
            hd = document.createElement('h3');
            hd.innerHTML = 'Přidání položek do výběru';
            canvas.appendChild(hd);
            
            h = canvas.offsetHeight - this.hPad;
            
            cats = document.createElement('div');
            cats.className = 'catsContainer';
            cats.style.width = this.catW+'px';
            cats.style.height = h+'px';
            canvas.appendChild(cats);
            
            items = document.createElement('div');
            items.className = 'itemsContainer';
            items.style.width = (this.width - this.catW - 4)+'px';
            items.style.height = h+'px';
            this.itemsCont = items;
            canvas.appendChild(items);
            
            btn = document.createElement('input');
            btn.type = 'submit';
            btn.value = 'Přidat';
            btn.setAttribute('onclick', 'STMA.Forms.AddItems.exec();return false');
            canvas.appendChild(btn);
            
            STMA.AJAX.init('add-items.cats', {node: cats}, a, true);
            STMA.AJAX.init('add-items.items', {node: items}, a, true);
        } else {
            this.cell.replaceChild(this.bg.firstChild, this.bg);
            this.bg = null;
            this.cell.removeChild(this.bounder);
            this.cell = null;
            this.cache = null;
            this.lastNode = null;
        }
        
        this.active = (this.active ? false : true);
    },
    exec: function() {
        STMA.AJAX.init(this.ajaxFile, {node: this.ajaxNode}, 'action=addItems;refId='+STMA.Core.refId+';nodes='+this.cache.join(','));
        this.init();
    }
}

STMA.Forms.ASCEdit.boolUpdate = function(o,c,i,t) {
    if (typeof(o.value) == 'undefined') {
        if (o.src.search('checkT') != -1) {
            o.value = true;
        } else {
            o.value = false;
        }
    }
    o.value = (o.value ? false : true);
    
    STMA.AJAX.init('ased',{onComplete: function(){
        if (o.value) {
            o.src = o.src.replace('checkF', 'checkT');
        } else {
            o.src = o.src.replace('checkT', 'checkF');
        }
    }},'refId='+i+';objType='+t+';code='+c+';value='+(o.value ? 1 : 0),true);
}
