var xmlHttpReq = null;
var responseType = "xml";
// //////////////////////////////////////////////////////////////////////
//
// This is used to invoke XML Web Services on remote computers.
//
// The method makeXmlCall uses the XMLHTTP object to send the
// given arguments to the given URL and retrieve an XML file.
//
// Example:
//
//          var dom = makeXmlHttpCall(
//              "http://www.uptodate.com",
//      "/utd/internal/services/x",
//              "userName=jim&password=hello"
//          );
//          var ok = getElementByName(dom,"isLoggedIn");
//          if (ok == "true") {
//              ....
//          }
//
// //////////////////////////////////////////////////////////////////////

// Regular expression to replace a plus signs with spaces.
var regEx = /\+/g;

// //////////////////////////////////////////////////////////////////////
//
// makeXmlHttpCall
//
// Opens a connection to the given URL, sends the arguments,
// and returns the results as an XML DOM object.
//
// //////////////////////////////////////////////////////////////////////
var resultHandler = null;

function makeXmlHttpCall(serviceUrl, arguments, handler) {
	return makeXmlHttpCall(serviceUrl, arguments, handler, "json");
}

function makeXmlHttpCall(serviceUrl, arguments, handler, respType) {
	if (respType == "true") {
		responseType = "json";
	} else if (respType == "false") {
		responseType = "xml";
	} else {
		responseType = respType;
	}
	
	var success = false;
	resultHandler = handler;
	
	try{
		xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(sc) {
			xmlHttpReq = null;
		}
	}
	
	if(!xmlHttpReq && typeof XMLHttpRequest != "undefined") {
		xmlHttpReq = new XMLHttpRequest();
	}

	if (xmlHttpReq != null) {
		xmlHttpReq.open('POST', serviceUrl, true);
		xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		if (handler != null) {
			xmlHttpReq.onreadystatechange = processReqChange;
		}
		
		xmlHttpReq.setRequestHeader("Cookie",  document.cookie);
			
	    try {
	        xmlHttpReq.send(arguments);
	        success = true;
	    } catch (e) {
			//Error calling service!
	    }
	    return success;
	}
}


// //////////////////////////////////////////////////////////////////////
// Default result handler
// //////////////////////////////////////////////////////////////////////
function processReqChange() {
	if (xmlHttpReq.readyState == 4) {
		if (xmlHttpReq.status == 200 || xmlHttpReq.status == 304) {
			if (responseType == "json") {
				resultHandler(eval("(" + xmlHttpReq.responseText + ")"));
			} else if (responseType == "xml") {
				resultHandler(xmlHttpReq.responseXML);
			} else {
				resultHandler(xmlHttpReq.responseText);
			}
		} else if (xmlHttpReq.status == 404 || xmlHttpReq.status == 500) {
			resultHandler(null);
		}
 	}
}

// //////////////////////////////////////////////////////////////////////
// getElementValue
//
// Returns the xmlElement's value
// //////////////////////////////////////////////////////////////////////

function getElementValue(node) {
    try {
    	var s = "";
    	if (node != null) {
	        for (var ii=0; ii<node.childNodes.length; ii++) {
	        	s += node.childNodes[ii].nodeValue;
	        }
			if (s != null) {
				s = s.replace(regEx," ");
				s = unescape(s);
			}
		}
        return s;
    } catch (e) {
        throw(e);
    }
}

// //////////////////////////////////////////////////////////////////////
// gets the first child element by name
// //////////////////////////////////////////////////////////////////////
function getChildElement(domObject, elementName) {
	var childElement = null;
	elementName = elementName.toUpperCase();
	for (var ii=0, len=domObject.childNodes.length; ii<len; ii++) {
		var tagName = domObject.childNodes[ii].tagName;
		if (tagName) {
			if (tagName.toUpperCase() == elementName) {
				childElement = domObject.childNodes[ii];
				break;
			}
		}
	}
	return childElement;
}

// //////////////////////////////////////////////////////////////////////
// get all child elements with the name
// //////////////////////////////////////////////////////////////////////
function getChildElements(domObject, elementName) {
	var elements = new Array();
	for (var ii=0, len=domObject.childNodes.length; ii<len; ii++) {
		if (domObject.childNodes[ii].tagName == elementName) {
			elements[ii] = domObject.childNodes[ii];
		}
	}
	return elements;
}

// //////////////////////////////////////////////////////////////////////
// gets all elemnets that matches the name
// //////////////////////////////////////////////////////////////////////
function getElements(domObject, elementName) {
	return domObject.documentElement.getElementsByTagName(elementName);
}
