/*
    Copyright (C) 2005 Lawrence Carvalho (dev@nodetraveller.com)

    This library is free software; you can redistribute it and/or 
    modify it under the terms of the GNU Lesser General Public License 
    as published by the Free Software Foundation; either version 2.1 of 
    the License, or any later version.
    This library is distributed in the hope that it will be useful, but 
    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
    License for more details.

    You should have received a copy of the GNU Lesser General Public License 
    along with this library; if not, write to the Free Software Foundation, Inc., 
    59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
    
*/
/* xmlClient is reusable cross-browser object for retreiving xml. Its supports 
 * Gecko based browsers as well as IE browsers.There is also support other browsers 
 * like Opera. 
 *
 * See www.nodetraveller.com for more info.
 */
function xmlClient(sUrl) {
 this.sUrl = sUrl;
 this.oHandler = null;
 this.oXMLRequest = null;
 this.sParams = "";
 this.bdoAsync = true;
 this.sClosureNode = "";
}

xmlClient.prototype.query = function(oHandler) {
	this.oHandler=oHandler;
	//for browsers with XMLHttpRequest (gecko,opera 7.6,safari)
	if (window.XMLHttpRequest) {
		if (!this.oXMLRequest)
			this.oXMLRequest = new XMLHttpRequest();
	}
	//for IE
	else if (window.ActiveXObject) {
		var aXmlIds = new Array("MSXML2.XMLHTTP.5.0",
								"MSXML2.XMLHTTP.4.0",
								"MSXML2.XMLHTTP.3.0", 
								"MSXML2.XMLHTTP", 
								"MICROSOFT.XMLHTTP.1.0", 
								"MICROSOFT.XMLHTTP.1", 
								"MICROSOFT.XMLHTTP");
		for (var i=0;i<aXmlIds.length;i++){
			try {
				this.oXMLRequest = new ActiveXObject(aXmlIds[i]);
				break;
			} catch(e) {
			
			}
		}
	} else { //for browsers without xmlhttprequest
		if (!this.oIframe) {
			var oIframe  = document.createElement("IFRAME");
			oIframe.setAttribute("id","liveInfoIFrame");
			oIframe.setAttribute("name","liveInfoIFrame");
			oIframe.style.visibility="hidden";
			oIframe.style.position="absolute"
			oIframe.style.top="0px";
			oIframe.style.left="0px";
			document.body.appendChild(oIframe);
			this.oIframe = document.getElementById("liveInfoIFrame");
		}
		
		this.oIframe.setAttribute("src",this.sUrl+"?" + this.sParams);
		window.clearInterval(this.iTimeout);
		this.bJustCalled = true;
		var oSelf =this;
		this.iTimeout = window.setInterval(function() { oSelf.processReqChange(oSelf);},500);
	}
	if (this.oXMLRequest) {
		var oSelf = this;
		this.oXMLRequest.onreadystatechange= function () {oSelf.processReqChange() };//??use closures??
		this.oXMLRequest.open("GET", this.sUrl+"?" + this.sParams,this.bdoAsync);
		this.oXMLRequest.send(null);
	}
}
xmlClient.prototype.setAsync = function (bdoAsync) {
	this.bdoAsync=bdoAsync;
}
xmlClient.prototype.setClosureNode = function(sNodeName) {
	this.sClosureNode=sNodeName;
}

xmlClient.prototype.processIFrame = function() {
	var oLoadedData = null;
	if (document.frames["liveInfoIFrame"]!=null) {
				var sDoc = document.frames["liveInfoIFrame"].document.body.innerHTML;
				if ((sDoc.indexOf("/"+this.sClosureNode)!=-1) || (sDoc.indexOf(this.sClosureNode+"/")!=-1)) {
					oLoadedData = document.frames["liveInfoIFrame"].document;
					this.iReadyState = 4;
					window.clearInterval(this.iTimeout);
				}
				return oLoadedData;
	}

}

xmlClient.prototype.processReqChange = function() {
	if (this.oIframe!=null) {
	
		//if first interval, set readyState to 1 so onInit can be called
		if (this.bJustCalled) {
			this.bJustCalled=false;
			this.iReadyState=1;
		}
		else {
			oLoadedData = this.processIFrame();
			
			if(oLoadedData==null)
				return;
		}
	}

	if (this.oXMLRequest!=null) {
		this.iReadyState = this.oXMLRequest.readyState;
	}
	
	switch (this.iReadyState) {
            case 1:
               this.oHandler.onInit();
            break;
            case 2:
					if (window.XMLHttpRequest) {
						  if ( this.oXMLRequest.status != 200 ) {
							this.oHandler.onError(this.oXMLRequest.status, this.oXMLRequest.statusText);
        	                this.oXMLRequest.abort();
                			}
					}
					else if (this.oIframe!=null) {
						this.oHandler.onError(-1, oLoadedData);
					}
            break;
            case 3:
                    	var contentLength =0;
						if (window.XMLHttpRequest) {
                       try {
						   contentLength = this.oXMLRequest.getResponseHeader("Content-Length");
						} catch(ex) { 
						};
                    if (this.oIframe!=null) {
                    	this.oHandler.onProgress(oLoadedData.length);
                    }
                    this.oHandler.onProgress(this.oXMLRequest.responseText.length);
				  }
            break;
            // Download complete
            case 4:
				if (this.oIframe!=null) {
					this.oHandler.onLoad(oLoadedData);
				
				}
				else {
					//alert(xmlReq.responseText);
					if (!document.importNode){
						this.oHandler.onLoad(this.oXMLRequest.responseText);
					}
					else this.oHandler.onLoad(this.oXMLRequest.responseXML);
				}
            break;
    }
}
//private
xmlClient.prototype.addParam = function (paramName,paramValue) {
	if (this.sParams.length>0)
		this.sParams =this.sParams + "&";
	this.sParams= this.sParams + paramName+"="+paramValue;
}
xmlClient.prototype.clearParameters = function() {
	this.sParams="";
}
xmlClient.prototype.getReadyState =function() {
		return this.iReadyState;
}	

xmlClient.prototype.abort = function() {
	try {
		if (this.oXMLRequest)
			this.oXMLRequest.abort();
		else this.oIframe.setAttribute("src","");
	}
	catch(e) { 
	}
}