var tsajax=
{
	IE:null,
	curObj:null,
	data:null,
	responseText:null,
	xml:null,
	xmlDoc:null,
	ldresources:"",
	startload:null,
	endload:null,
	loadtime:null,
	status:0,
	returntype:"text/xml",
	success:false,
	remid:null,
	callback:null,
	callbacknotify:null,
    notify:false,	
    passobj:null,
	cErr:null,
	showerrors:0,
	doparse:true,
    setXML:function(x)
    {
        var pe;
        x=x==undefined?this.data:x;
        try
        {
            if (window.ActiveXObject)
            {
                this.xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                this.xmlDoc.async=false;
				//xmlDoc.setPoperty('SelectionLanguage', 'XPath');
				//xmlDoc.resolveExternals = false;
				//xmlDoc.setProperty("SelectionNamespaces","xmlns:xhtml='http://www.w3.org/1999/xhtml'");
				//xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");  
                this.xmlDoc.loadXML(x);
            }
            else
            {
                var errorNS='http://www.mozilla.org/newlayout/xml/parsererror.xml';
                var p=new DOMParser();
                this.xmlDoc=p.parseFromString(x,this.returntype);
                this.xmlDoc.parseError=new Object({errorCode:0,line:0,srcText:null,reason:null});
                if (this.xmlDoc.documentElement.nodeName == 'parsererror' && this.xmlDoc.documentElement.namespaceURI==errorNS)
                {
                    this.xmlDoc.parseError.errorCode = 1;
                    var s=this.xmlDoc.documentElement.getElementsByTagNameNS(errorNamespace, 'sourcetext')[0];
                    if (s != null) this.xmlDoc.parseError.srcText = s.firstChild.data
                    this.xmlDoc.parseError.reason = this.xmlDoc.documentElement.firstChild.data;
                }
            }
            if (this.xmlDoc.parseError.errorCode!=0)
            {                
                this.xml=null;
                this.setError("Code:" + this.xmlDoc.parseError.errorCode + "\nparse error line(" + this.xmlDoc.parseError.line + ")"+ this.xmlDoc.parseError.srcText,2);
            }
            else
            {    
                this.xml=this.xmlDoc.documentElement;
            }
        }
        catch(e)
        {
            this.xml=null;
            this.setError(tsfuncs.parseError(e),1,true);
        }
    },
	createXMLObj:function(showerror,rt)
	{
		var r;
		this.cErr=null;
		rt=rt==undefined?this.returntype:rt;
		if (this.curObj!=null) return this.curObj;
		if( typeof(XMLHttpRequest) != 'undefined' )
		{
			try 
			{
				this.curObj = new XMLHttpRequest();
				if (this.curObj.overrideMimeType) this.curObj.overrideMimeType(rt);
				return this.curObj;
			}
			catch(e)
			{
        		this.setError(e.description,showerror==1?0:1);
			}
		}
		else
		{		
    		var axo = ['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0'], i;
    		for( i=0; i < axo.length; i++ )
    		{
    			try
    			{ 
    			    this.cErr=null;
    				this.curObj=new ActiveXObject(axo[i]);
					//this.curObj.setRequestHeader("Content-Type",rt);
    				return this.curObj;
    			}
    			catch(e)
    			{
            		this.cErr=e.description;
    			}
    		} 
        }
		this.curObj=null;
		this.setError(this.cErr,showerror==1?0:1);
		return null;
	},
	setError:function(d,el,force)
	{
	    this.cErr=d;
        if ((this.showerrors>=el || force) && this.showerrors!=-1) alert(this.cErr);
    },
	shouldsend:function(milli,lastrec)
	{
	    return (this.elapsed()>milli || lastrec!=this.remid);
	},
	elapsed:function()
	{
	    return this.endload==null?0:(tsfuncs.getTimer()-this.endload);
	},
	exists:function(pg)
	{
	    return this.send(pg);
	},
	loadpage:function(pg,div)
	{
		this.doparse=false;
		this.returntype="text/html";
		if (!this.send(pg,"",false)) this.data="";
		this.doparse=true;
		this.returntype="text/xml";
		if (div!=undefined) this.loadElement(div,false,"Could not load "+pg);
		return this.data;
	},
	send:function(page,query,usepost,user,pw,rid,obj,cb,rt)
	{
	    this.startload=tsfuncs.getTimer();
        this.cErr=null;
	    this.remid=rid;
	    this.success=false;
	    this.data=this.xml=null;
        this.callback=cb==undefined?null:cb;
	    this.passobj=obj==undefined?null:obj;
		if (page==undefined || page==null) return false;
		user=user==undefined || user==null?undefined:user;
		pw=pw==undefined || pw==null?undefined:pw;
		usepost=usepost==undefined || usepost==null?false:usepost;
		query=query==undefined || query==null?"":query;
		if (this.createXMLObj(true,rt)==null) return false;
		if (query.length!=0)
		{
			query=(query.substr(0,1)=="&" || query.substr(0,1)=="?")?query.substring(1):query;
			query="?"+query;
		}
		try
	    {
	        var op=(!usepost|| this.IE);
			this.curObj.onreadystatechange=this.returnevent;
		    this.curObj.open(op?"GET":"POST", page+(op?query:""), (this.callback!=null),user,pw);
		    this.curObj.send(op?null:query);
            if (this.callback==null)
            {
                this.setreturn();
                return this.success;
            }
            else
            {
                return true;
            }		
        }
        catch(e)
        {
    		this.setError(e.description,1);
            return false;
        }
	},
	setreturn:function()
	{
	    this.endload=tsfuncs.getTimer();
	    this.loadtime=this.endload-this.startload;
		this.status=this.curObj.status;
		this.data=this.curObj.responseText;
		this.responseText=this.data;
		if (this.doparse) this.setXML();
		this.success=this.status==200;
		if (this.callback!=null) 
		{
		    this.callback(this.success,this.passobj);
		}
    },
	returnevent:function()
	{
	    if (tsajax.notify && tsajax.callback!=null) 
	    {
	        if (tsajax.callbacknotify!=null)
	        {
	            tsajax.callbacknotify(tsajax.curObj.readyState,tsajax.passobj);
	        }
	        else
	        {
	            tsajax.callback(null,tsajax.curObj.readyState);
	        }
	    }
	    if (tsajax.curObj.readyState==4) tsajax.setreturn();
    },
	loadElement:function(id,doClear,def)
	{
	    var e;
        this.cErr=null;
	    if (typeof(id)=="string") e=document.getElementById(id); else e=id;
	    if (e)
	    {
    	    if (this.success || doClear)
            {
                def=this.data==""?(def==undefined?"":def):this.data;
                e.innerHTML=doClear?"&nbsp;":def;
                return true;
            }
            else
            {
                this.setError("Could not load from server error code:"+this.status+"\n"+def,1);
            }
        }
        else
        {
            this.setError("Could not load into element specified ("+id+")",1);
        }
        return false;
    },
    XPath:function(expr, node) 
    {
    	try
    	{
	    	var r=new Object({i:0,length:0,next:function(){this.i++;return this.node();}});
	        node=node!=undefined?node:this.xml;
	        expr=expr==undefined || expr==""?"//*":expr;
	        if (node==null || node==undefined) return null;
		    if (document.evaluate) 
		    {
		    	r.nodes=this.xmlDoc.evaluate(expr,node,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
				r.node=function(nn)
				{
					if (nn==undefined)
					{
						nn=this.i;
					}
					else
					{
						nn=nn<0 || nn>this.length?-1:nn;
						this.i=nn;
					}
					return nn==-1?null:this.nodes.snapshotItem(nn);
				};
				r.length=r.nodes.snapshotLength;
		    }
		    else 
		    {
		    	r.nodes=node.selectNodes(expr);
				r.node=function(nn)
				{
					if (nn==undefined)
					{
						nn=this.i;
					}
					else
					{
						nn=nn<0 || nn>this.length?-1:nn;
						this.i=nn;
					}
					return nn==-1?null:this.nodes[nn];
				};
				r.length=r.nodes.length;
			}
			return r;
		}
		catch(e)
		{
			this.setError(tsfuncs.parseError(e),1,true);
			return null;
		}
	},
	XPItem:function(expr,node)
	{
	    var nd=this.XPath(expr,node);
	    return nd.node(0);
	},
	GNN:function(node)
	{
	    return node==undefined?this.xml.nodeName:node.nodeName;
    },
    GN:function(expr,node)
    {
        return this.XPItem(expr,node);
    },
	GNT:function(node)
	{
	    return node.firstChild?node.firstChild.nodeValue:null;
    },
	GNA:function(node,att)
	{
        return node!=null?node.getAttribute(att):null;
    },
	loadresource:function(fn,id,rep)
	{
		var e;
		id=id==undefined?fn:id;
		rep=rep==true;
		var ld=this.ldresources.indexOf(fn)>=0;
		if ((ld && rep) || !ld)
		{
			e=tsfuncs.geID(id);
			if (e) 
			{
			    try
			    {
				    e.removeNode(true);
				}
				catch(er)
				{
				    e.parentNode.removeChild(e);
				}
			}
			if (fn.toLowerCase().indexOf(".js")!=-1)
			{
				e=document.createElement('script')
				e.setAttribute("type","text/javascript");
				e.setAttribute("src", fn);
			}
			else if (fn.toLowerCase().indexOf(".css")!=-1)
			{
				e=document.createElement("link")
				e.setAttribute("rel", "stylesheet");
				e.setAttribute("type", "text/css");
				e.setAttribute("href", file);
			}
			else
			{
				e=null;
			}
			if (e)
			{
				e.id=id;
				document.getElementsByTagName("head").item(0).appendChild(e);
				if (!ld) this.ldresources+=fn+"|";
			}
		}
	}
    
}
tsajax.IE=(navigator.appName.indexOf("Internet Explorer")!=-1?true:false);