AJAX = {
    root : '',
    xml : false,
    connLim : 100,
    statId : 'ajaxStat',
    list : new Array(),
    temp: null,
    getHTTPObj : function() {
    	var x=null;
    	try { 
    	   // Firefox, Opera 8.0+, Safari
    		x=new XMLHttpRequest();
    	} catch (e) { 
    	   //Internet Explorer
    		try {
    			x=new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			x=new ActiveXObject("Microsoft.XMLHTTP");
    		}
    	}
    	return x;
    },
    decoy: function() {
        
    },
    // t - target file, f - included functions, p - included parameters, m - method (get/post), d - use DBox fn
    init : function(t,f,p,m,d) {
        if (typeof(f) != "undefined") {
            if (typeof(f.onStart) == "function") {
                AJAX.f.onStart();
            }  
            if (f.flash) {
                f.node.innerHTML = 'Loading...';
            }
        }      
        if (this.xml == false) {

            if (typeof(m) == "undefined") m = 'get';
            if (typeof(p) == "undefined") {
                p = 'langId='+FE.langId;
            } else {
                p = p.split(';');
                p.push('langId='+FE.langId);
                if (p.length) {
                    p = p.join('&');  
                }
            }
            this.xml = this.getHTTPObj();
            this.f = f;
            show = false;
            if (typeof(DBox.fn) != "undefined" && d) {
                if (DBox.a) {
                    if (p != '') {
                        p = p+'&'+DBox.a;
                    } else {
                        p = DBox.a;
                    }
                }
                DBox.fn();
                
            }            
            

            this.timer = window.setTimeout(function(){
                
                icon = document.createElement('img');
                icon.src = Base.vSrc+'ajax.gif';
                icon.className = 'middle';
                if ($(AJAX.statId) != null) {
                    with ($(AJAX.statId)) {
                        appendChild(icon);
                        innerHTML += '<small>&nbsp;<strong>'+t+'</strong>.inc.php</small>';
                        className = '';
                    }                       
                }                 
             
            },this.connLim);

            this.xml.onreadystatechange = function() {
                if (AJAX.xml.readyState == 4) {
                    if (AJAX.timer) {
                        window.clearTimeout(AJAX.timer);
                    }
                    if ($(AJAX.statId) != null) {
                        with ($(AJAX.statId)) {
                            className = 'hidden';
                            innerHTML = '';
                        }                        
                    }
                    if (AJAX.temp == null) {
                        var nd = document.createElement('div');
                        nd.className = 'hidden';
                        nd.innerHTML = 'lorem ipsum';
                        document.body.appendChild(nd);
                        AJAX.temp = nd;
                    }
                    
                    // find dynamically loaded scripts
                    var toEval = '';
                    AJAX.temp.innerHTML = AJAX.xml.responseText;
                    var scripts = AJAX.temp.getElementsByTagName('script');
                    if (scripts.length) {
                        var item = null;;
                        for(i=0;i<scripts.length;i++) {
                            toEval += scripts[i].innerHTML+';';
                            
                        }
                    }
                    // flush temp element - avoid duplicate IDs etc
                    AJAX.temp.innerHTML = '';
                    
                    
                    
                    


                    if (typeof(f) != "undefined") {
                        if (typeof(f.onComplete) == "function") {
                            AJAX.f.onComplete(AJAX.xml.responseText);
                        }
                        if (typeof(f.node) != "undefined") {
                            if (f.additive) {
                                n = $(f.node);
                                if (n.tagName == "TABLE") {
                                    tr = n.getElementsByTagName('tr')[1];
                                    td = tr.getElementsByTagName('td')[1];
                                    if (td.colSpan > 1) {
                                        tr.parentNode.removeChild(tr);
                                    }
                                }
                                $(f.node).innerHTML += AJAX.xml.responseText;   
                            } else {
                                
                                if ($(f.node) != null) {
                                    $(f.node).innerHTML = AJAX.xml.responseText;
                                }  
                            }
                        }
                        if (typeof(f.onFinish) == "function") {
                            AJAX.f.onFinish(AJAX.xml.responseText);
                        }
                    }
                    
                    // eval scripts
                    if (toEval.length) {
                        eval(toEval);
                    }
                    
                    AJAX.xml = false;
                    
                    if (AJAX.list.length) {
                        AJAX.next();
                    } else if (AJAX.pass) {
                        AJAX.pass();
                    }
                }
            }
            
            if (t.search('/shared/') != '-1') {
                tgtFile = '/data'+t;
            } else {
                tgtFile = this.root+t;
            }
            if (m == 'get') {
                if (p) p = '?' + p;
                this.xml.open('GET', tgtFile+'.inc.php'+p, true);
                this.xml.send(null);
            } else {
                this.xml.open('POST', tgtFile+'.inc.php', true);
                
                this.xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                this.xml.setRequestHeader("Content-length", p.length);
                this.xml.setRequestHeader("Connection", "close");                               
                this.xml.send(p);                
            }            
        } else {
            this.cache(t,f,p,m);
        }

        // add post alternative
    },
    cache : function(t,f,p,m) {
        this.list.push(new Array(t,f,p,m));
    },
    next: function() {
        n = this.list[this.list.length-1];
        this.list.pop();
        this.init(n[0],n[1],n[2],n[3]);
        
    }
}
