Act = {
  bindWith : function(obj,f,fn) {
    if (fn) {
        var inputs;
        inputs = document.getElementsByName('decoy_'+fn);
        for(i=0;i<inputs.length;i++) {
            item = inputs[i];
            id = item.id.split('_');
            id.shift();
            id = id.join('_');
            $('input_'+id).value = ($('chbox_'+id).checked ? (f?$('chbox_'+id).value : 1) : 0);
            
        }
    } else {
        $('input_'+obj).value = ($('chbox_'+obj).checked ? (f?$('chbox_'+obj).value : 1) : 0);
    }
    
    
  },
  sendForm: function(a) {
      var form = document.forms[0];
      form.action = a;
      form.submit();
  },
  inactRedir : function(a) {
    location.href = '/admin/logout/'+(a?'?returnUrl='+a:'');
  },
  swapNode: function(node, tgt) {
    newTemp = document.createElement('div');
    oldTemp = document.createElement('div');

    
    main = node.parentNode;
    
    // set temp nodes
    main.replaceChild(newTemp, tgt);
    main.replaceChild(oldTemp, node);
    // return original nodes
    main.replaceChild(node, newTemp);
    main.replaceChild(tgt, oldTemp);
  
    newTemp = null;
    oldTemp = null;  
  },
  redir : function(t,a,r,m,e) {
    if (!m) m = 'act';
    switch (m) {
        case 'act':
        var u = t; 
    }
    $('input_act').value = a;
    $('input_refId').value = r;
    var f;
    f = document.getElementsByTagName('form');
    if (typeof(e) != "undefined") {
        e = e.split(';');
        for(i=0;i<e.length;i++) {
            vals = e[i].split('=');
            key = vals[0];
            val = vals[1];
            if ($('extra_'+key) != null) {
                inp = $('extra_'+key);
                inp.parentNode.removeChild(inp);
            }
            inp = document.createElement('input');
            inp.type = "hidden";
            inp.id = inp.name = 'extra_'+key;
            inp.value = val;
            f[0].appendChild(inp);
        }   
    }
    fCtrl.flush();
    
    if (t.substr(0,1) == '/') {
        add = '';
    } else {
        add = '/'
    }
    f[0].action += '/admin'+add + t + '/';
    
    f[0].submit();
    
    return false;
  },
  setAct : function(a,s) {
    if (typeof($('input_act')) != "undefined") {
        $('input_act').value = a;
    } else {
        var f = document.getElementsByTagName('form');
        var form = f[0];

        var inp = document.createElement('input');
        inp.setAttribute('type', 'hidden');
        inp.setAttribute('name', 'act');
        inp.setAttribute('id', 'input_act');
        inp.value = a;
        form.appendChild(inp);
    }
    if (s) {
        Act.skipCheck = true;
    }
  },
  swLoc : function(r,c,e) {
    p = r.parentNode;
    if (!e) e = 'loc';
    l = e.length + 4;
    var divs;
    divs = p.getElementsByTagName('div');
    for(i=0;i<divs.length;i++) {
        if (divs[i].id == e+"_tab_"+c) {
            divs[i].className = "tab active";
        } else if (divs[i].id.substr(0,l) == e+"_tab") {
            divs[i].className = "tab";
        }
        if (divs[i].id == e+"_cont_"+c) {
            divs[i].className = "tabContent";
        } else if (divs[i].id.substr(0,l+1) == e+"_cont") {
            divs[i].className = "tabContent hidden";
        }        
    }
  },
  limitVal : function(i,m) {
    if (i.value.length > m) {
        i.value = i.value.substr(0,m);
    }
    if (typeof(i.value.length) != "undefined") {
        if (i.value.length) {
            l = i.value.length;
        } else {
            l = 0;
        }
    } else {
        l = 0;
    }
    var sp;
    sp = i.parentNode.getElementsByTagName('span');
    for(i=0;i<sp.length;i++) {
        if(sp[i].className == "limiter") {      
            sp[i].innerHTML = m-l;        
        }
    }    
    
  },
    cmExpand: function(o,i,f) {
        if ((o != null && o.parentNode != null) || (o != null && typeof(o) == "string")) {
            if (typeof(o) == "string") {
                o = $(o);
                if (typeof(o.exp) != "undefined" && o.exp) {
                    return false;
                }
            }
            var d;
            d = o.parentNode.getElementsByTagName('div');
            d = d[0];
            if (d.hasChildNodes() && typeof(o.exp) == "undefined") {
                o.exp = true;
            }
            o.src = (o.exp ? o.src.replace('min','plus') : o.src.replace('plus','min'));
             
            if (typeof(o.exp) == "undefined") {
                AJAX.init('sub/cat.menu',{onComplete: function(r) {
                    o.exp = true;
                    d.innerHTML = r;
                    if (typeof(f) == "function") {
                        f();
                    }
                }},'catId='+i)            
            } else if (!o.exp) {
                o.exp = true;
                d.className = '';
            } else {
                o.exp = false;
                d.className = 'hidden';
            }            
        }
    },
    cmHl: function(o) {
        div = o.parentNode;
        hl = div.getElementsByTagName('a')[0];
        var a;
        a = $('catsMenu').getElementsByTagName('a');
        for (i=0;i<a.length;i++) {
            a[i].className = (a[i] == hl?'hl':'');
        }
    },
    actList: new Array(),
    // f - executed file, s - source (element id)
    addAct: function(f,s,a) {
        this.actList.push({f: f+';'+s, a: a});
    },
    preAct: function() {
        this.inProgress = false;
        if (this.actList.length) {
            o = this.actList.shift();
            this.inProgress = true;
            this.save(o.f,false,o.a);
            return false;
        } else {
            if (fCtrl.exec()) {
                var formsRs;
                formsRs = document.getElementsByTagName('form'); 
                for(i=0;i<formsRs.length;i++) {
                    var frmItem;
                    frmItem = document.forms[i];
                    if (frmItem.id != 'search_form') {
                        frmItem.submit();   
                    }    
                }
                
            } else {
                return false;
            }
        }
    },
    save: function(f,fn,a,i,sk) {
        ar = f.split(';');
        f = ar[0];
        s = ar[1];
        p = this.getVals(s);
        if (a) {
            a = '&'+a;
        } else {
            a = null;
        }
        AJAX.init(f, {onComplete: function(r) {
            if (typeof($(s)) != "undefined") {
                if (!sk) {
                    $(s).innerHTML = r;
                }
                
            }
            
            if (Act.inProgress) {
                Act.preAct();
            }
            if (typeof(fn) == "function") {
                fn();
            }
            
        }},p+'&mode=save&refId='+($('input_refId') != null ? $('input_refId').value : i)+a,'post');
        return false;        
    },
    getVals: function(s) {
        p = new Array();
        for(a=0;a<2;a++) {
            if ($(s) != null) {
                var inpRes;
                inpRes = $(s).getElementsByTagName((a?'select':'input'));
                for(i=0;i<inpRes.length;i++) {
                    if (typeof(inpRes[i]) != "undefined") {
                        var formItem;
                        formItem = inpRes[i];
                        if ((!a && formItem.type != 'submit') || a) {    
                            p.push(formItem.name+'='+(a?formItem.options[formItem.selectedIndex].value:formItem.value));    
                        }                        
                    }

                }                  
            }
          
        }
        var txts;
        txts = $(s).getElementsByTagName('textarea');
        for(i=0;i<txts.length;i++) {
            var item;
            item = txts[i];
            p.push(item.name+'='+item.value);
        }
        if (p.length) {
            return p.join('&');    
        } else {
            return true;
        }
        
    },
    addProducts: function(t,o) {
        if (Act.skipAct) {
            Act.skipAct = false;
            return false;
        }
        var inputs;
        p = new Array();
        if ($(t) == null) {
            return false;
        }
        inputs = $(t).getElementsByTagName('input');
        for(i=0;i<inputs.length;i++) {
            item = inputs[i];
            if (item.type == "checkbox" && item.id.search(t) != -1 && item.checked) {
                p.push(item.name+'='+item.value);
            }
        }
        if (p.length) {
            p = p.join('&');
        } else {
            p = false;   
        }
        temp = o.value;
        o.disabled = true;
        o.value = 'Načítání ..';
        AJAX.init(DBox.f, {onComplete: function(r) {
            o.disabled = false;
            o.value = temp;
            
        }}, 'spec='+DBox.s+'&add=true&'+p,'post',(p?true:false));
        return false;
    },
    selectAll: function(o) {
        if (typeof(o.state) == "undefined") {
            o.state = false;
        }          
        t = o.parentNode.parentNode.parentNode;
        var rows, cell;
        state = 0;
        rows = t.getElementsByTagName('tr');
        for(i=0;i<rows.length;i++) {
            // skip the first line
            if (i) {
                cell = rows[i].getElementsByTagName('td')[0];
                if ((chbox = cell.firstChild) != null) {
                    state = state + (chbox.checked ? 0 : 1);    
                }                
            }
        }
        for(i=0;i<rows.length;i++) {
            if (i) {
                cell = rows[i].getElementsByTagName('td')[0];
                if ((chbox = cell.firstChild) != null) {
                    if (o.state) {
                        if (state == (rows.length - 1)) {
                            // extended, but rows are unchecked
                            o.state = false;
                        }
                    } else {
                        if (state == 0) {
                            o.state = true;
                        }
                    }
                    //chbox.setAttribute('checked', (o.state ? '' : 'checked'));
                    chbox.checked = (o.state ? false : true);
                    if (typeof(chbox.id) != "undefined") {
                        spl = chbox.id.split('_');
                        spl.shift();
                        id = 'input_' + spl.join('_');
                        $(id).value = (o.state ? 0 : chbox.value);                        
                    }
                }
            }    
        }
        o.state = (o.state ? false : true);
        
    },
    recalculate: function(i,t) {
        price = $('input_total_price').value;
        disc = $('input_total_discount').value;
        AJAX.init('sub/calculation',{node: 'calcInner'},'refId='+i+';tbl='+t+';total_discount='+disc,'post');
    },
    FSExpand: function(o,d) {
        fs = o.parentNode.parentNode;
        if (typeof(fs.exp) == "undefined") {
            fs.exp = d;
        }
        var nodes;
        nodes = fs.childNodes;
        for(i=1;i<nodes.length;i++) {
            item = nodes[i];
            if (item.parentNode == fs) {
                item.style.display = (fs.exp ? 'none' : 'block');
            }
        }
        o.src = Base.vSrc + (fs.exp ? 'plus' : 'min') + '.png';
        fs.exp = (fs.exp ? false : true);
    },
    setUD: function(i) {
        o = $('udInput');
        v = o.previousSibling.value;
        AJAX.init('aed',{onComplete: function(){
            o.className = 'hidden';
        }},'table=CustomerCards;col=discount;pushVal='+v+';refCol=internal_customer_id;refId='+i);
    },
    updateOrder: function(i,cat) {
        src = new Array('variantsInner', 'accessorriesInner');
        temp = new Array();
        for(it=0;it<src.length;it++) {
            temp.push(this.getVals(src[it]));
        }
        temp.push('refId='+i);
        if (cat) {
            temp.push('cat=true');
            i = i+';cat=true';
        }        
        p = temp.join('&');
        AJAX.init('customer.add.items',{node: 'itemsBefore'}, p, 'post');
        AJAX.init('customer.add.items',{node: 'itemsAfter'}, 'refId='+i);
    },
    scriptResult: function(r) {
        if (r == 'success') {
            alert('Operace byla úspěšně dokončena!');
        } else {
            alert('Došlo k neočekávané chybě.');
        }
    },
    expand: function(o,r) {
        
    },
    lightBoxResize: function() {
        var viewportwidth;
        var viewportheight;


        if (typeof window.innerWidth != 'undefined') {
            viewportwidth = window.innerWidth,
            viewportheight = window.innerHeight
        } else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
            viewportwidth = document.documentElement.clientWidth,
            viewportheight = document.documentElement.clientHeight
        } else {
            viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
            viewportheight = document.getElementsByTagName('body')[0].clientHeight
        }    
        maxW = 1000;
        maxH = 750;
        indent = 150;
        if (viewportheight < (maxH + indent) || viewportwidth < (maxW + indent)) {           
            rt = maxW / maxH;
            h = viewportheight - indent;
            w = viewportwidth - indent;
            r = w/h;
            if (r > rt) {
                newH = h;
                newW = Math.floor(h * rt);
            } else {
                newW = w;
                newH = Math.floor(w /rt);             
            }
            var a;
            var item;
            a = document.getElementsByTagName('a');
            for(i=0;i<a.length;i++) {
                item = a[i];
                if (item.rel.search('lightbox') != '-1') {
                    var str = item.href;
                    str = str.replace('width=1000&height='+maxH,'width='+newW+'&height='+newH);
                    item.href = str;
                }
            }
        } 
        window.onresize = function() {
            Act.lightBoxResize();
        } 
    },
    getRedir: function(o,c) {
        o = $('input_'+o);
        v = o.value;
        url = self.location.href;
        res = url.split('?');
        url = res.shift();
        url += '?'+c+'='+v;
        window.location = url;
        return false;
    },
    offerDispType: function(dt) {
        if ($('dispTypeInput') != null) {
            var di = $('dispTypeInput');
            var dispType = di.options[di.selectedIndex].value;
        } else {
            var dispType = dt;
        }


        var o = $('input_search_text');
        var srch = o.value;

        var url = self.location.href;
        res = url.split('?');
        url = res.shift();
        url += '?dispType='+dispType+'&search_text='+srch;

        window.location = url;
    },
    relocate: function(t,o) {
        var link = t;
        location.href = link;
    },
    href: function(t) {
        src = location.href;
        // setting arguments only
        if (t.indexOf('?') == 0) {
            t = t.substr(1).replace('&',';');
            var res = FE.getArgs(t);

            var urlArgs = src.split('?')[1];
            if (urlArgs) {
                urlArgs = urlArgs.replace('&',';');
            }
            alert(urlArgs);
            if (urlArgs.length) {
                for(i=0;i<urlArgs.length;i++) {

                }
            }
        }
    },
    actStop: function(obj) {
        obj = obj.parentNode;
        while(typeof(obj) != 'undefined') {
            
            obj.onclick = function(){};
            
            obj = obj.parentNode;
        }
        
    	if (!e) var e = window.event;
    	e.cancelBubble = true;
    	if (e.stopPropagation) e.stopPropagation();
        return false;
    },
    currSet: function(id, rate, current) {
        var decoy = $('decoy_'+id);
        var inpVal = $(id+'_item_price');
        var inpEdit = $(id+'_was_edited');
        var inpUpd = $(id+'_was_updated');

        if (inpUpd.defVal == null) {
            inpUpd.defVal = inpUpd.value;
        }

        var def = parseFloat(decoy.value);
        if (def) {
            var newVal = def*rate;
            inpVal.value = newVal;
            if (newVal != current) {
                inpEdit.value = 1;
                inpUpd.value = 0;
            } else {
                inpEdit.value = 0;
                inpUpd.value = inpUpd.defVal;
            }
        }
    }
}
window.fCtrl = new fCtrl();
//window.Act = new Act();
Act.addClass = function(v,o) {
    res = o.className.split(' ');
    f = false;    
    for(i=0;i<res.length;i++) {
        if (res[i] == v) {
            f = true;   
        }
    }
    if(!f) {
        o.className += ' ' + v;
    }        
}
Act.removeClass = function(v,o) {
    res = o.className.split(' ');
    for(i=0;i<res.length;i++) {   
        if (res[i] == v) {
            res.splice(i,1);
        }   
    }
    o.className = res.join(' ');
}
Act.reshade = function(t) {
    var rows;
    rows = t.getElementsByTagName('tr');
    for(c=0;c<rows.length;c++) {
        item = rows[c];
        if (item.id) {
            if (c%2==1) {
                Act.removeClass('dark',item);
            } else {
                Act.addClass('dark',item);
            }
        }
    }
}
