var xmlObjs = new Array()

function getXML(oReq, fn, obj) {
    var initial = new Date()
	var xmlRequest = new Object()
	xmlRequest.xmlhttp = (obj)?obj:getXMLHttpObj();
	xmlRequest.oReq = oReq
	xmlRequest.fn = fn
	xmlRequest.usage = 0;
	xmlRequest.startTime = initial.getTime()	
	if(ie)
		xmlRequest.xmlhttp.open("POST", "/m", true);
	xmlRequest.xmlhttp.onreadystatechange= function() {onReadySC(xmlRequest)};
	if(!ie)
		xmlRequest.xmlhttp.open("POST", "/m", true);
	xmlRequest.xmlhttp.setRequestHeader("Content-Type", "text/xml");
    xmlRequest.xmlhttp.send(oReq);
}
function checkReadyState(xmlRequest) {
	switch(xmlRequest.xmlhttp.readyState)
	{
		case 1: case 2: case 3:
			return false;
		break;
		case 0: case 4:
			return true;
		break;			
	}
}
function resendRequest(xmlRequest) {
	if(xmlRequest.usage < 10)
	{
		xmlRequest.usage = xmlRequest.usage + 1;
		xmlRequest.xmlhttp.abort();
		setTimeout(function(){getXML(xmlRequest.oReq, xmlRequest.fn, xmlRequest.xmlhttp)},1000);
	}
	else
	{
		xmlRequest.usage = 0;
		alert("We are having communication problems. Please logout, login, then try your request again. Thank you.");
	}
}
function onReadySC(xmlRequest) {
	if(checkReadyState(xmlRequest))
	{
		try
		{
			clearTimeout(xmlRequest.timeout)
			var oStat = xmlRequest.xmlhttp.status
			if(oStat==200)
			{
				setLatencyMeterVal(xmlRequest.startTime)
				if(resolveAlertandAccess(xmlRequest.xmlhttp.responseXML))
				{
					xmlRequest.fn(xmlRequest.xmlhttp.responseXML)
				}
			}
			else
			{
				if(oStat == 12029 || oStat == 12030 || oStat == 12031 || oStat == 12152)
				{
					try{top.routing_showAlert("A connection to the server was lost. Trying to connect again. Please wait. If the problem does not resolve itself, you may have to login again. Thank you.")}catch(e){}
					resendRequest(xmlRequest)
				}
				else if(oStat < 12000)
				{
					try{top.routing_showAlert("A connection to the server can not be made. Please wait. If the problem does not resolve itself, you may have to login again. Thank you.")}catch(e){}
					resendRequest(xmlRequest)
				}
			}
		}
		catch(e)
		{
			checkReadyState(xmlRequest);
			resendRequest(xmlRequest);
		}
	}
	else
	{
		clearTimeout(xmlRequest.timeout)
		var cd = new Date()
		var cdTime = cd.getTime()
		var delay = 60000
		var httpTimer = (xmlRequest.startTime + delay) - cdTime
		if(httpTimer < 0){httpTimer = 1}
		xmlRequest.timeout = window.setTimeout(
			function()
			{
				if(!checkReadyState(xmlRequest))
				{
					xmlRequest.xmlhttp.onreadystatechange = function(){};
					try{top.routing_showAlert("A connection to the server timed out. Trying to connect again. Please wait. If the problem does not resolve itself, you may have to login again. Thank you.")}catch(e){}
					resendRequest(xmlRequest)
				}
			},httpTimer
		)
	}
}
function resolveAlertandAccess(oXml) {
	if (!oXml)
	{
		return false;
	}
	var oErrors = btn(oXml,"errors")
	var oTS = btn(oXml,"timestamp")
	var oMC = btn(oXml,"marketClosed")
	var oAlert = btn(oXml,"alert")
	var oVer = btn(oXml,"version")

	if(oVer.length > 0)
	{		
		for(var x = 0; x < oVer.length; x++)
		{
			setPath(sNode(oVer[x]))
		}
	}
	if(oTS.length > 0)
	{		
		for(var x = 0; x < oTS.length; x++)
		{
			top.chainTimeStamp	= sNode(oTS[x])
		}
	}
	if(oAlert.length > 0)
	{		
		for(var x = 0; x < oAlert.length; x++)
		{
			try
			{		
				top.routing_showAlert(sNode(oAlert[x]))
			}
			catch(e){}
		}
	}
	else
	{
		try
		{
			top.routing_hideAlert()
		}
		catch(e){}
	}
	if(oMC.length > 0)
	{		
		for(var x = 0; x < oMC.length; x++)
		{
			try
			{		
				top.marketClosedFlag = sNode(oMC[x])
				if(top.marketClosedFlag == 'true')
				{
					if(top.bi("mktOnOff"))
						top.bi("mktOnOff").innerHTML = 'CLOSED'	
				}
				else
				{
					if(top.bi("mktOnOff"))
						top.bi("mktOnOff").innerHTML = '<font style="color:#71BD29;">Open</font>'
				} 
			}
			catch(e){}
		}
	}
	if(oErrors.length > 0)
	{		
		for(var x = 0; x < oErrors.length; x++)
		{
			if(btn(oErrors[x],"access") && btn(oErrors[x],"access").length > 0)
			{
				if(sNode(btn(oErrors[x],"access")[0]).toLowerCase() == "denied")
				{
					if(top.thisUndocked)
					{
						top.subtabs_uninit();
						top.window.close();
						return false;
					}
					if(!top.skipDefaultDenied)
					{
						top.uninit();
						top.location.href = "/securehost/tool/login/";
						return false;
					}
				}
			}
			if(btn(oErrors[x],"action") && btn(oErrors[x],"action").length > 0)
			{

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

function setLatencyMeterVal(startTime, cont) {
	var current = new Date()
	var newTime = current.getTime()
	var dif = newTime - startTime
	averageTime[aveTimeCount] = dif
	aveTimeCount  = aveTimeCount + 1
	if(aveTimeCount > aveReqUse - 1)
	{
		aveTimeCount = 0
	}
}

