//var ie = (event.target)?false:true;

var getXML = function(oReq, fn) {
	var xmlRequest = {
		xmlhttp: xhr(),
		oReq: oReq,
		fn: fn
	};

	if (isIE) {
		xmlRequest.xmlhttp.open("POST", "/m", true);
	}
	
	xmlRequest.xmlhttp.onreadystatechange = function() {
		onReadySC(xmlRequest);
	};
	
    if (!isIE) {
		xmlRequest.xmlhttp.open("POST", "/m", true);
	}
	
	xmlRequest.xmlhttp.setRequestHeader("Content-Type", "text/xml");
    xmlRequest.xmlhttp.send(oReq);
};

var onReadySC = function(xmlRequest) {
	
	var checkReadyState = function(xmlRequest) {
		if (xmlRequest.xmlhttp) {
			return xmlRequest.xmlhttp.readyState == 4;
		} else {
			return false;
		}
	}

    if (checkReadyState(xmlRequest)) {
		try {
			var oStat = xmlRequest.xmlhttp.status;
			if (oStat == 200) {
				if (resolveAlertandAccess(xmlRequest.xmlhttp.responseXML)) {
					xmlRequest.fn(xmlRequest.xmlhttp.responseXML);
				} else {
				//build a failed to get response message
				}
			} else {

				if (oStat == 12029 || oStat == 12030 || oStat == 12031 || oStat == 12152 || oStat < 12000) {
					try {
						// build a connect error message

					} catch(e) {

					}
				}
			}
		}
		catch(e) {

		}
		xmlRequest.xmlhttp = null;
	} else {
		/*
		clearTimeout(xmlRequest.timeout);

		var delay = 15000;
		var httpTimer = (xmlRequest.startTime + delay) - top.oh.utils.dates.getTimeStamp();

		if (httpTimer < 0) {
			httpTimer = 1;
		}
		xmlRequest.timeout = setTimeout( function() {
			if (!checkReadyState(xmlRequest)) {
				xmlRequest.xmlhttp.onreadystatechange = function() {};
				if (top.xhrErrorCount >= maxXhrErrorCountForDisplay) {
					top.routing_displayErrMsg("A connection to the server timed out. Trying to connect again. Please wait while we try to reconnect. If the problem does not resolve itself, you may have to login again.  Thank you.");
				}
				resendRequest(xmlRequest);
			}
		},httpTimer);
		*/
	}
};
var resolveAlertandAccess = function(oXml) {
	if (!oXml) {
		return false;
	}
	var oErrors = btn(oXml,"errors");

	for (var i = 0; i < oErrors.length; i++) {
		var error = oErrors[i];
		var oA = btn(error,"access");
		if (oA.length > 0) {
			for (var j = 0; j < oA.length; j++) {
				if (sNode(oA[j]).toLowerCase() == "denied") {
					// need to add denied
				}
			}
		}
	}

	return true;
};

var xhrAbort = {
	get: function() {

	},
	abortAll: function() {
		for (var i = 0; i < this.xmlObjs.length; i++) {
			this.xmlObjs[i].abort();
		}
	}
};

var xmlObjs       = new Array();
var lastXMLObjsID = 0;

function xhr() {
	var makeObj = true;
	if(xmlObjs.length > 0)
	{
		for(var i = 0; i < xmlObjs.length; i++)
		{
			if(xmlObjs[i].readyState == 4)
			{
				makeObj = false;
                lastXMLObjsID = i;
				return xmlObjs[i];
				break;
			}
			else
			{
				makeObj = true;
			}
		}
	}
	if(makeObj == true)
	{
		if(window.XMLHttpRequest)
		{
			xmlObjs[xmlObjs.length] = new XMLHttpRequest();
		}
		else
		{
			xmlObjs[xmlObjs.length] = new ActiveXObject("Msxml2.XMLHTTP");
		}
        lastXMLObjsID = xmlObjs.length - 1;
		return xmlObjs[xmlObjs.length - 1];
	}
};

var sNode = function(obj) {
    return obj[0] ? obj[0].firstChild.data : obj.firstChild.data;
};
	
var btn = function(obj, tag)  {
	var nodes = obj.getElementsByTagName(tag);
	return nodes;
};

var replaceSpecials = function(value) {
	return value.replace(/[&]/g,'&amp;').replace(/[<]/g,'&lt;').replace(/[>]/g,'&gt;')
};



