/*
	Author: Stephen W. Cote
	Email: wranlon@hotmail.com
	
	Copyright 2002, All Rights Reserved.
	
	Do not copy, archive, or distribute without prior written consent of the author.
*/

_js_package("org.cote.js.xml");

org.cote.js.xml={
	_xml_request:null,
	_xml_request_id:null,
	_xml_requests:[],
	_xml_requests_map:[],

	serialize:function(n){
		var v;
		if(typeof XMLSerializer=="function"){
			return (new XMLSerializer()).serializeToString(n);
		}
		else if(typeof n.xml == "string"){
			return n.xml;
		}
	},
	getCDATAValue:function(n){
		var c,d="",i=0,e;
		c = n.childNodes;
		for(;i<c.length;i++){
			e=c[i];
			if(e.nodeName=="#cdata-section") d+=e.nodeValue;
		}
		return d;
	},
	queryNodes:function(x,p,n,a,v){
		return org.cote.js.xml._queryNode(x,p,n,a,v,1);
	},
	queryNode:function(x,p,n,a,v){
		return org.cote.js.xml._queryNode(x,p,n,a,v,0);
	},
	_queryNode:function(x,p,n,a,v,z){
		/*
			 x = xdom
			 p = parent path
			 n = node path
			 a = attribute name
			 v = attribute value
		*/

		var i=0,b,e,c,r=[];
		if(!z) r = null;
		
		c = x.getElementsByTagName(p);
		
		if(typeof n=="string"){

			if(!c.length){
				if(!z) return null;
				else return r;
			}
			c = c[0]; 
			e = c.getElementsByTagName(n);
		}
		else e = c;

		
		for(;i<e.length;i++){
			b = e[i];
			if((!a && !v) || (b.getAttribute(a) == v)){
				/*
					single query
				*/
				if(!z){
					r = b;
					break;
				}

				else r[r.length]=b;
				
			}
		}
		return r;
	},
	_handle_xml_request_load:function(xml_id){
		var _x=org.cote.js.xml,o,v;
		if(typeof _x._xml_requests_map[xml_id] == "number"){
			o = _x._xml_requests[_x._xml_requests_map[xml_id]];
			
			v = {"xdom":null,"id":xml_id};
			if(o.obj != null && o.obj.responseXML != null){
				v.xdom = o.obj.responseXML;
			}
			o.completed = 1;
			org.cote.js.message.MessageService.publish("onloadxml",v);
			if(typeof o.handler=="function") o.handler("onloadxml",v);

		}	
	},
	_handle_xml_request_readystatechange:function(xml_id){
		var _x=org.cote.js.xml,o;
		if(typeof _x._xml_requests_map[xml_id] == "number"){
			o = _x._xml_requests[_x._xml_requests_map[xml_id]];
			if(typeof o.obj == "object" && o.obj.readyState == 4){

				_x._handle_xml_request_load(xml_id);
			}
		}
	},
	/*
		getXml(path,handler,id);
		
		id is optional.  Use this where two or more xml transactions will be directed
		through the same handler.
		
	*/
	getXml:function(p,h,a,i){
	/*
			p = path
			h = handler
			a = async
			i = id
	*/
		return org.cote.js.xml._request_xmlhttp(p,h,a,i,0,null);
	},

	/*
		_request_xml is asynchronous.
	*/
	_request_xmlhttp:function(p,h,a,i,x,d){
		/*
			p = path
			h = handler
			a = async
			i = id
			x = is_post as bool
			d = data as string or DomDocument
		*/
		
		var _x=org.cote.js.xml,f,o=null,v,_m=org.cote.js.message.MessageService,y,z;
		if(typeof x=="undefined") x = 0;
		if(typeof d=="undefined") d = null;
		z = (x?"POST":"GET");
		if(typeof i!="string") i=org.cote.js._get_gunid();

		y = _x._xml_requests.length;

		_x._xml_requests[y] = {url:p,id:i,obj:null,handler:h,method:(x?1:0),completed:0};
		_x._xml_requests_map[i]=y;
		

		if(typeof XMLHttpRequest=="function"){
			o = _x._xml_requests[y].obj = new XMLHttpRequest();
			var m,e=new RegExp("^/");
			if(!p.match(e)){
				m=location.pathname;
				m=m.substring(0,m.lastIndexOf("/")+1);
				p=m + p;
			}
			p=location.protocol + "//" + location.host + p;

			_x._xml_requests[y].url = p;
			if(a){
				f = function(){org.cote.js.xml._handle_xml_request_load(i);};
				o.addEventListener("load",f,false);
			}
		}
		else{
			try{
				o = _x._xml_requests[y].obj = new ActiveXObject("Microsoft.XMLHTTP");
				if(a){
					f = function(){org.cote.js.xml._handle_xml_request_readystatechange(i);};
					o.onreadystatechange=f;
				}
			}
			catch(e){
				_m.sendMessage("XMLError: " + (e.description?e.description:e.message),"512.4",1);
			}
		}
		var foo = (a?true:false);
		o.open(z,p,foo);
		o.send(d);
		if(!a)
			return _x._xml_requests[y].obj.responseXML;
		
		return 1;

	},

	getInnerText:function(s){
		var r = "",a,i,e;
		if(typeof s == "string") return s;
		if(typeof s=='object' && s.nodeType==3) return s.nodeValue;
		if(s.hasChildNodes()){
			a = s.childNodes;
			for(i=0;i<a.length;i++){
				e = a[i];
				if(e.nodeType==3) r+=e.nodeValue;
				if(e.nodeType==1 && e.hasChildNodes()){
					r+=this.getInnerText(e);
				}
			}
		}
		return r;
	},
	setInnerXHTML:function(t,s,p){
		/*
			t = target
			s = source
			p = preserve
		*/
		var i,y,e,a,l,x,n,v;
		if(!p){
			for(i=t.childNodes.length-1;i>=0;i--){
				t.removeChild(t.childNodes[i]);
			}
		}

		y=(s && typeof s=='object')?s.nodeType :(typeof s=="string")?33:-1;
		switch(y){
			case 1:
				e=document.createElement(s.nodeName);
				a=s.attributes;
				l=a.length;
				for(x=0;x<l;x++){
					n=a[x].nodeName;
					v=a[x].nodeValue;
					if(n=="style"){
						e.style.cssText=v;
					}
					else if(n=="id"){
						e.id=v;
					}
					else if(n=="class" && org.cote.js.dom.browser.client.bswitch==1){
						e.className=v;
					}
					else if(n.match(/^on/i)){
						eval("e." + n + "=function(){" + v  +"}");
					}
					else{
						e.setAttribute(n,v);
					}
				}
	
				if(s.hasChildNodes()){
					a=s.childNodes;
					l=a.length;
					for(x=0;x<l;x++){
						this.setInnerXHTML(e,a[x],1);
					}
				}
				t.appendChild(e);
				break;
			case 3:
				e=s.nodeValue;
				if(e){
					e=e.replace(/^\s*/,"");
					e=e.replace(/\s*$/,"");
					e=e.replace(/\s+/g," ");
					t.appendChild(document.createTextNode(e));
				}
				break;
			case 8:
				/*
					Ignore comments
				*/
				break;
			case 33:
				e=s;
				if(e){
					e=e.replace(/^\s*/,"");
					e=e.replace(/\s*$/,"");
					e=e.replace(/\s+/g," ");
					t.appendChild(document.createTextNode(e));
				}
				break;
			default:

		}
	}
};

