/*
url-loading object and a request queue built on top of it
*/

/* namespacing object */
var net = {};

net.READY_STATE_UNINITIALIZED = 0;
net.READY_STATE_LOADING = 1;
net.READY_STATE_LOADED = 2;
net.READY_STATE_INTERACTIVE = 3;
net.READY_STATE_COMPLETE = 4;

net.SoapEnvelope = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><#method# xmlns="http://edeptive.com/webservices/">#params#</#method#></soap:Body></soap:Envelope>';

/*--- content loader object for cross-browser requests ---*/
net.ContentLoader = function(url, params, onload, method, soapMethod, contentType, onerror) {
	var temp;
	
    net.currentLoader = this;
    this.req = null;
    this.onload = onload;
    this.onerror = (onerror) ? onerror : this.defaultError;
	this.soapMethod = soapMethod;
	
	if (params && typeof(params) != "string") {
		if (this.soapMethod) {
			temp = "";
			for (key in params) {
				temp += "<" + key + ">" + params[key] + "</" + key + ">";
			}
		} else {
			temp = "";
			for (key in params) {
				temp += (temp.length > 0 ? "&" : "") + key + "=" + (!method || method.toLowerCase() == "get" ? escape(params[key]) : params[key]);
			}
		}
		params = temp;
	}
		
    this.loadXMLDoc(url, method, params, contentType);
};

net.ContentLoader.prototype.loadXMLDoc = function(url, method, params, contentType) {
    if (!method) {
        method = (this.soapMethod ? "POST" : "GET");
    }
	
    if (!contentType && method == "POST") {
        contentType = (this.soapMethod ? "text/xml; charset=utf-8" : "application/x-www-form-urlencoded");
    }
	
	//netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
    if (window.XMLHttpRequest) {
        this.req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        this.req = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
	if (this.soapMethod) {
		params = net.SoapEnvelope.replace(/#method#/gi, this.soapMethod).replace(/#params#/gi, params);
	}
	
    if (this.req) {
        //try {
            var loader = this;
            this.req.onreadystatechange = function() {
                net.ContentLoader.onReadyState.call(loader);
            };
			//netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
            this.req.open(method, url, true);
            if (contentType) {
                this.req.setRequestHeader('Content-Type', contentType);
				if (this.soapMethod) {
					this.req.setRequestHeader("SOAPAction", "http://edeptive.com/webservices/" + this.soapMethod);
				}
            }
            this.req.send(params);
        //} catch (err) {
        //    this.onerror.call(this);
        //}
    }
};

net.ContentLoader.onReadyState = function() {
    var req = this.req;
    var ready = req.readyState;
    if (ready == net.READY_STATE_COMPLETE) {
        var httpStatus = req.status;
        if (httpStatus == 200 || httpStatus === 0) {
        	//netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
            this.onload.call(this);
        } else {
            this.onerror.call(this);
        }
    }
};

net.ContentLoader.prototype.defaultError = function() {
    alert("error fetching data!"
            + "\n\nreadyState:" + this.req.readyState
            + "\nstatus: " + this.req.status
            + "\nheaders: " + this.req.getAllResponseHeaders());
};

net.ContentLoader.prototype.getResult = function() {
	return this.req.responseText;
};

net.ContentLoader.prototype.getSoapResult = function() {
	var rx = new RegExp("<" + this.soapMethod + "Result>([^<]*)</" + this.soapMethod + ">");
	var matches = this.getResult().match(rx);
	return matches[1];
};



