function webrequest()
{
	this.async = true;
	this.request = null;
	this.method = "POST";
	this.requestUrl = "";
	this.parameters = {};
	this.sendAsFormData = true;

	this.useWait = false;
	this.waitInContainer = null;
	
	this.readyStateChange = ctxFunction(this, webrequest_readyStateChange);
	this.init();
}
AddEventManager(webrequest);

webrequest.prototype.init =
function()
{
	if(window.XMLHttpRequest && !(window.ActiveXObject))
	{
		try 
		{
			this.request = new XMLHttpRequest();
		} catch(e) {
		}
	}
	else if(window.ActiveXObject)
	{
		try 
		{
			this.request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try
			{
				this.request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
			}
		}
	}

	if(this.request)
	{
		this.request.onreadystatechange = this.readyStateChange;
	}
}

webrequest.prototype.addParameter =
function(name, value)
{
	this.parameters[name] = value;
}

webrequest.prototype.getRequestBody = 
function()
{
	var result = [];
	for(var key in this.parameters)
	{
		result.push(key + "=" + encodeURIComponent(this.parameters[key]));		
	}

	return result.join("&");
}

webrequest.prototype.send =
function()
{
	if(this.useWait)
		this.setupWait();

	var requestBody = "";
	if(this.getRequestBody)
		requestBody = this.getRequestBody();
	var url = this.requestUrl;
	if(this.method == "GET")
	{
		url += "?" + requestBody;
		requestBody = "";
	}
	this.request.open(this.method, this.requestUrl, this.async);

	if(this.sendAsFormData)
	{
		this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}
	this.request.send(requestBody);
}




webrequest.prototype.setupWait =
function()
{
	this.coverdiv = coverDiv.create(this.waitInContainer, (this.color || "#000000"));
}

function webrequest_readyStateChange()
{
	if(this.request.readyState == 4)
	{
		if(this.coverdiv)
			this.coverdiv.remove();

		if(this.processResult())
			this.dispatchEvent("complete", null, [this]);
	}
}

webrequest.prototype.processResult =
function()
{
	return true;
}

webrequest.prototype.responseText =
function()
{
	return this.request.responseText;
}

webrequest.prototype.getNamespacePrefix =
function(node)
{
	var prefix = null;
	var attrNodes = node.attributes;
	if(attrNodes && attrNodes.length)
	{
		for(var i=0; i<attrNodes.length; i++)
		{
			if(attrNodes[i].name.indexOf("xmlns") == 0)
			{
				if(attrNodes[i].value == this.wsNamespace)
					prefix = attrNodes[i].name.split(":")[1];
			}
		}		
	}

	if(!prefix)
	{
		for(var i=0; i<node.childNodes.length; i++)
			prefix = this.getNamespacePrefix(node.childNodes[i]);	
	}

	return prefix;
}

function GetXMLString(xml)
{
	if(!xml) return "null";
	if(xml.xml)
		return xml.xml;
	else if(window.XMLSerializer)
	{
		var xser = new XMLSerializer();
		var s = xser.serializeToString(xml);
		return s;
	}
}

function soaprequest()
{
	webrequest.call(this);

	this.soapParameters = [];
	this.method = "POST";
	this.wsMethod = "";
	this.wsNamespace = "";
	this.responseXML = null;
}
soaprequest.prototype = new webrequest();

soaprequest.prototype.addParameter =
function(name, value)
{
	p = new soapParameter(name, value);
	this.soapParameters.push(p);
}

soaprequest.prototype.soapMessage =
function(xmlstr)
{
	var xml = "<?xml version='1.0' encoding='utf-8' ?>";
	xml += "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
	xml += "xmlns:xsd='http://www.w3.org/2001/XMLSchema' ";
	xml += "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
	xml += "<soap:Body>";
	xml += xmlstr;
	xml += "</soap:Body></soap:Envelope>";

	return xml;
}

soaprequest.prototype.getRequestBody =
function()
{
	var ns = this.wsNamespace;
	var action = ((ns.lastIndexOf("/") != ns.length - 1) ? ns + "/" : ns) + this.wsMethod;
	this.request.setRequestHeader("SOAPAction", action);
	this.request.setRequestHeader ("Content-Type", "text/xml; charset=utf-8");

	var xml = this.getSoapBody();
	var soapEnv = this.soapMessage(xml);
	var requestBody = "";

}

soaprequest.prototype.getSoapBody =
function()
{
	var xml = "<" + this.wsMethod + " xmlns='" + this.wsNamespace + "' >";
	for(var i=0; i<this.soapParameters.length; i++)
	{
		var param = this.soapParameters[i];
		var val = "<" + param.name + ">" + param.value + "</" + param.name + ">";
		xml += val;
	}

	xml += "</" + this.wsMethod + ">";

	return xml;
}

soaprequest.prototype.processRequest =
function()
{
	var response = this.request.responseXML;
	//alert(GetXMLString(response));
	if(!response)
	{
		alert(this.request.responseText);
		return false;
	}

	var resultNodes;
	if(navigator.userAgent.indexOf("MSIE") != -1)
	{
		var ns = this.getNamespacePrefix(response);
		resultNodes = response.getElementsByTagName(ns + ":" + this.wsMethod + "Response");
		
	}
	else
		resultNodes = response.getElementsByTagName(this.wsMethod + "Response");
	this.responseXML = resultNodes[0];

	return true;
}

function soapParameter(name, value)
{
	this.name = name;
	this.value = value;
}


function coverDiv()
{
	this.div = null;
	this.waitdiv = null;
}

coverDiv.prototype.remove =
function()
{
	if(!this.div.parentNode) return;

	this.div.parentNode.removeChild(this.div);
	this.waitdiv.parentNode.removeChild(this.waitdiv);
}

coverDiv.create =
function (container, color)
{
	if(!container) return null;

	var coverdiv = document.createElement("DIV");
	coverdiv.style.backgroundColor = (color || "#000000");
	coverdiv.style.opacity = .5;
	coverdiv.style.position = "absolute";
	coverdiv.style.top = "0px";
	coverdiv.style.left = "0px";
	coverdiv.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=50)";
	var ht, wd;
	if(this.waitInContainer == document.body)
	{
		wd = isIE?(document.body.clientWidth+"px"):"100%";
		ht = document.documentElement.clientHeight || document.body.offsetHeight;
		ht = ht+"px";
	}
	else
	{
		wd = (container.offsetWidth) + "px";
		ht = (container.offsetHeight) + "px";
	}
	coverdiv.style.height = ht;
	coverdiv.style.width = wd;
	var pos = container.style.position;
	if(pos != "relative" && pos != "absolute")
		container.style.position = "relative";
	container.appendChild(coverdiv);

	var c = new coverDiv();
	c.div = coverdiv;
	c.getWaitDiv();
	container.appendChild(c.waitdiv);
	
	return c;
}

coverDiv.prototype.getWaitDiv =
function()
{
        if(!this.waitDiv)
        {
                var waitDiv = document.createElement("DIV");
		waitDiv.style.top = "0px";
		waitDiv.style.left = "0px";
                waitDiv.style.position = "absolute";
                waitDiv.style.textAlign = "center";
                this.waitdiv = waitDiv;
                var waitImg = document.createElement("IMG");
                waitImg.src = "img/wait26.gif";
                waitImg.height = 26;
                waitImg.width = 26;
                waitDiv.appendChild(waitImg);
                waitDiv.appendChild(document.createElement("BR"));
                waitDiv.appendChild(document.createTextNode("Loading..."));
        }
        
        return this.waitdiv;
}

