STMA.AJAX = {
    inProgress: false,
    queue: new Array(),
    pop: 'lol',
    fn: false,
    xml: false,
    tempArgs: '',
    init: function(t,f,a,s) {
        if (!this.inProgress) {
            this.inProgress = true;
            
            if (f.onStart) {
                f.onStart();
            }
            
            root = (s ? '/shared/' : STMA.root);
            
            args = STMA.args(a);
            
            if (this.tempArgs) {
                args += '&'+this.tempArgs;
                this.tempArgs = '';
            }   
                     
            act = STMA.searchVal(args,'action');
            this.fn = f;
            if (act == 'add') {
                this.fn.add = true;
            }            
            
            
            
            this.xml = this.getXMLHTTP();
            
            this.xml.onreadystatechange = function() {
                var o = STMA.AJAX;
                if (o.xml.readyState == 4) {
                    if (typeof(o.fn) != "undefined") {
                        var result = o.xml.responseText;
                      
                        if (typeof(o.fn.node) != "undefined") {
                            if (typeof($(o.fn.node)) != "undefined") {
                                if (o.fn.add) {                                  
                                    $(o.fn.node).firstChild.innerHTML += result;
                                } else {
                                    $(o.fn.node).innerHTML = result;    
                                }
                                
                                var tables;
                                tables = $(o.fn.node).getElementsByTagName('table');
                                if (tables.length) {
                                    for(i=0;i<tables.length;i++) {
                                        table = tables[i];
                                        STMA.Forms.Table.redraw(table.id);
                                    }    
                                }
                            }
                        }
                        
                        if (typeof(o.fn.onComplete) != "undefined") {
                            o.fn.onComplete(result);
                        }
                    }
                    
                    o.inProgress = false;
                    
                    if (o.queue.length) {
                        item = o.queue.shift();
                        o.init(item.target, item.focus, item.args, item.shared);
                    }
                    
                }
            }
            
            if (args) {
                args += '&'; 
            }
            
            var cacheVal = this.handlers[t+'-'+f.node];
            if (typeof(cacheVal) == "undefined") {
                /* save values */
                var temp;
                temp = 'ajaxFile='+t;
                if (f.node) {
                    temp += '&ajaxNode='+f.node;
                }
                var objType;
                if (!STMA.searchVal(args, 'objType')) {
                    objType = STMA.Core.objType; 
                }
                if (objType != null) {
                    temp += '&objType='+objType;
                }       
                args += temp;
                obj = STMA.searchVal(args, 'objType');
                if (!objType && obj) {
                    temp += '&objType='+obj;
                }
                cacheVal = temp;  
            } else {
                /* load values */
                args += cacheVal;
            }
            
            this.handlers[t+'-'+f.node] = cacheVal;

            
            this.xml.open('POST', root+'process/'+t); // +'.inc.php'
            
            this.xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            this.xml.setRequestHeader("Content-length", args.length);
            this.xml.setRequestHeader("Connection", "close");
                    
            this.xml.send(args);     
        } else {
            this.cache(t,f,a,s);
        }
    },
    cache: function(t,f,a,s) {
        item = { target: t, focus: f, args: a, shared: s};
        this.queue.push(item);
    },
    handlers: new Array(),
    getXMLHTTP : function() {
    	var x = null;
    	try { 
    		x = new XMLHttpRequest();
    	} catch (e) { 
    		try {
    			x=new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			x=new ActiveXObject("Microsoft.XMLHTTP");
    		}
    	}
    	return x;
    }
}
