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($g(o.fn.node)) != "undefined") {
                                if (o.fn.add) {                                  
                                    $g(o.fn.node).firstChild.innerHTML += result;
                                } else {
                                    $g(o.fn.node).innerHTML = result;    
                                }
                                
                                var tables;
                                tables = $g(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);
                    }
                    
                    
                    /* thickBox */
                    TB_init();
                }
            }
            
            var cacheVal = this.handlers[t+'-'+f.node];
            if (a.search('refId=') != -1 && typeof(cacheVal) != 'undefined') {
                cacheVal = STMA.removeVal(cacheVal, 'refId');
            }
            if (STMA.searchVal(args, 'noCache')) {
                cacheVal = '';    
            } else {
                if (args) {
                    args += '&'; 
                }                
            }        
            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;
                }    
                if (STMA.searchVal(args, 'refId')) {
                    refId = STMA.searchVal(args, 'refId');
                    temp += '&refId='+refId;
                }   
                if (STMA.searchVal(args, 'unfin')) {
                    unfin = STMA.searchVal(args, 'unfin');
                    temp += '&unfin='+unfin;
                }              
                if (STMA.searchVal(args, 'fin')) {
                    fin = STMA.searchVal(args, 'fin');
                    temp += '&fin='+fin;
                }    
                if (STMA.searchVal(args, 'parentId')) {
                    parentId = STMA.searchVal(args, 'parentId');
                    temp += '&parentId='+parentId;
                }
                if (STMA.searchVal(args, 'code')) {
                    code = STMA.searchVal(args, 'code');
                    temp += '&code='+code;
                }                                                   
                args += temp;
                obj = STMA.searchVal(args, 'objType');
                if (!objType && obj) {
                    temp += '&objType='+obj;
                }
                if (STMA.searchVal(args, 'edit')) {
                    temp += '&edit=true';
                }
                cacheVal = temp;  
            } else {
                /* load values */
                args += cacheVal;
            }

            this.handlers[t+'-'+f.node] = cacheVal;

            
            this.xml.open('POST', root+'process/'+t); //
            
            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;
    },
    refresh: function(f,n,c,a) {
        vals = this.handlers[f+'-'+n];
        if (vals != null) {
            temp = new Array();
            if (a) {
                temp = '';
                if (c) {
                    temp = 'action='+c+';';
                }
                temp += a;
            } else {
                var tables;
                tables = $g(n).getElementsByTagName('table');
                if (tables.length) {
                    for(i=0;i<tables.length;i++) {
                        var item;
                        item = tables[i];
                        var inputs;
                        inputs = item.getElementsByTagName('input');
                        if (inputs.length) {
                            for(a=0;a<inputs.length;a++) {
                                input = inputs[a];
                                temp.push(input.name+'='+input.value);
                            }
                        }
                    }    
                }
                
                if (c) {
                    temp.push('action='+c);    
                }
                temp = temp.join(';');
                                                
            }
            
            STMA.AJAX.init(f, {node: n}, temp);
        } else {
            return false;
        }
    }
}

AJAX = STMA.AJAX;
