/* 

This function is used to call soap web services.  It returns a JSON object that 
represets the object requested OR NULL.

Notes:
To be able to determine the expected nesting of xml elements
from the web service call, browse to the asmx file directly and view
the HTTP GET response for the method in question.
  
Throws
A Javascript error if the http request involved returns a status other than
200 (OK), or if the responseText was not an XML Document.  Currently, this method
catches these errors and displays them in a new Window;
*/
function CallWebService(serviceUrl, methodName, args) {
	var queryString = "?";
	if (args == null)
		args = new Object();

	var tsInArgs = true;
	var tsNum = 0;
	var tsName = "ts";
	while (tsInArgs) {
		tsNum++;
		if (!args.hasOwnProperty(tsName + tsNum.toString())) {
			tsInArgs = false;
		}
	}
	args[tsName + tsNum.toString()] = new Date().valueOf();


	for (var name in args) {
		if (args.hasOwnProperty(name)) {
			//window.alert(name + "\n" + args[name]);
			if (queryString.length > 1)
				queryString += "&";

			queryString += encodeURIComponent(name) + "=" + encodeURIComponent(args[name]);
		}
	}

	var url = serviceUrl + "/" + methodName + queryString;

	//window.alert(url);

	var xmlhttp = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	xmlhttp.open("GET", url, false);
	xmlhttp.setRequestHeader("cache-control", "no-cache");
	xmlhttp.send();

	var out;

	try {
		var status = xmlhttp.status;
		if (status != 200) {
			// set error number.
			var e = new Error();
			e.message =
			   "Status: " + status + "\n" +
			   "statusText: " + xmlhttp.statusText + "\n" +
			   "responseText: " + xmlhttp.ResponseText;
			e.number = 0x800A0001;
			e.name = "ServiceError";
			e.description = e.message;
			e.htmlMessage = e.message.replace(/\n/g, "<br />\n");
			throw e;
		}

		//window.alert(xmlhttp.responseText);
		if (xmlhttp.responseXML.documentElement == null) {
			// an error occurred in which we got back an html error page.
			var e = new Error();
			e.message = "An error occured peforming the operation.";
			e.number = 0x800A0001;
			e.name = "ServiceError";
			e.description = e.message;
			e.htmlMessage = xmlhttp.responseText;
			throw e;
		}
		else {
			out = jQuery.xml2json(xmlhttp.responseXML);
			
			// JSON will still convert an empty response as a valid object because it also adds the namespaces
			//	to the resulting JSON object.  Let's test to see if the xsi:nil property is null and return an
			//	empty object.
			for (var prop in out) {
				if (out.hasOwnProperty(prop)) {
					if (prop.toString() == "xsi:nil" && out[prop] == "true") {
						out = null;
						break;
					}
				}
			}
		}
	}
	catch (e) {
		var features = "top=0, left=0, height=600, width=800, status=no, scrollbars=yes, location=no, menubar=no, toolbar=no, titlebar=no, resizable=yes";

		//var errorWindow = window.open("about:blank","_blank",features);
		//errorWindow.document.write(e.htmlMessage);

		throw e;
	}
	return out;
}

