/********************************************************************
*			Miscellaneous routines associated with keeping a list of			*
*			all the transactions issued by this session.									*
*			These routines all run in the context of the main page				*
********************************************************************/
function clearTransactions()
{
	transactionHistory.XMLDocument.loadXML("<TRANSACTIONS/>");
}

/******************************
*		Add a transaction to the	*
*		transaction history.			*
******************************/
function newTransactionHistory(obj)
{
	var track = 0;
	try
	{
		var topNode = transactionHistory.XMLDocument.selectSingleNode("TRANSACTIONS");
		var newNode = transactionHistory.XMLDocument.createNode(1, "TRANSACTION","");
		var dt = new Date();
		newNode.setAttribute("starttime", dt.toString());
		track++;
		
		newNode.setAttribute("method", obj.method);
		newNode.setAttribute("async", obj.async);
		newNode.setAttribute("transactionid", obj.transactionid);
		track++;

		//newNode.setAttribute("id", obj.getAttribute("id"));
		if (obj.getAttribute && obj.getAttribute("transactionid")) 
		{
			newNode.setAttribute("id", obj.getAttribute("transactionid"));
			newNode.setAttribute("transactionid", obj.getAttribute("transactionid"));
		}
		else 
		{
			newNode.setAttribute("id", obj.transactionid);
			newNode.setAttribute("transactionid", obj.transactionid);
		}
		track = 20;

		newNode.setAttribute("status", obj.status);
		track = 22;
		newNode.setAttribute("readystate", obj.readystate);
		track = 24;
		newNode.setAttribute("elapsed", obj.elapsed);
		track = 26;
		newNode.setAttribute("objecttype", obj.objecttype);
		var subNode = transactionHistory.XMLDocument.createNode(1, "URL","");
		subNode.text = obj.url;
		newNode.appendChild(subNode);

		track++;

		subNode = transactionHistory.XMLDocument.createNode(1, "STATUSTEXT","");
		if (obj.statustext) subNode.text = obj.statustext;
		newNode.appendChild(subNode);
		
		track++;
		if (obj.method == "POST")
		{
			subNode = transactionHistory.XMLDocument.createNode(1, "POSTDATA","");
			if (obj.lastpostedtext) subNode.text = obj.lastpostedtext;
			newNode.appendChild(subNode);
		}

		track++;
		topNode.appendChild(newNode);
	}
	catch(e)
	{
		alert("newTransactionHistory routine:\nError logging to transaction history:\n" + 
			e.message + "\ntrack=" + track );
	}
}

/**************************************
*		Add an entry to the transaction		*
*		history based on an DOMDocument		*
*		Since we have to check the status	*
*		of the transaction, return it.		*
**************************************/
function domTransactionHistory(domObj, tranName, url, elapsed)
{
	var tranStatus;
	try
	{
		var sts;					//Status text
		var topNode = transactionHistory.XMLDocument.selectSingleNode("TRANSACTIONS");
		var newNode = transactionHistory.XMLDocument.createNode(1, "TRANSACTION","");
		var dt = new Date();
		newNode.setAttribute("starttime", dt.toString());

		var perr = domObj.parseError;
		
		newNode.setAttribute("method", "DOM");
		newNode.setAttribute("async", domObj.async);
		newNode.setAttribute("id", tranName);
		newNode.setAttribute("transactionid", new Date().getTime());
		newNode.setAttribute("elapsed", elapsed/1000);
		newNode.setAttribute("objecttype", domObj.objecttype);
	
		tranStatus = true;
		if (perr.errorCode == 0)
		{
			var sNode = domObj.selectSingleNode("//STATUS");
			if (!sNode) sNode = domObj.selectSingleNode("//Status");
			if (sNode)
			{
				if (sNode.parentNode.nodeName == "Record")
				{
					sNode = domObj.selectSingleNode("//Data/STATUS");
					if (!sNode) sNode = domObj.selectSingleNode("//Data/Status");
				}
			}
			if (sNode)
			{
				sts = sNode.text;
				if (sts.toUpperCase() != "SUCCESS") tranStatus = false;
				var stNode = sNode.parentNode.selectSingleNode("//STATUSTEXT");
				if (!stNode) stNode = sNode.parentNode.selectSingleNode("//StatusText");
				if (stNode) sts = stNode.text;
			}
			else
			{
				sts = "No STATUS node in output";
				if (url.toUpperCase().indexOf("HTTP:") >= 0)tranStatus = false;
			}
		}
		else
		{
			tranStatus = false;
			sts = perr.reason;
		}
		newNode.setAttribute("status", tranStatus);
		newNode.setAttribute("readystate", domObj.readystate);
		
		var subNode = transactionHistory.XMLDocument.createNode(1, "URL","");
		subNode.text = url;
		newNode.appendChild(subNode);

		subNode = transactionHistory.XMLDocument.createNode(1, "STATUSTEXT","");
		subNode.text = sts;
		newNode.appendChild(subNode);
		topNode.appendChild(newNode);
	}
	catch(e)
	{
		alert("domTransactionHistory\nError logging to transaction history:\n" + e.message);
	}
	return tranStatus;
}

/******************************
*		Add a manual entry to the	*
*		transaction history.			*
******************************/
function dummyTransactionHistory(obj)
{
	try
	{
		var topNode = transactionHistory.XMLDocument.selectSingleNode("TRANSACTIONS");
		var newNode = transactionHistory.XMLDocument.createNode(1, "TRANSACTION","");

		newNode.setAttribute("method", obj.method);
		newNode.setAttribute("async", obj.async);
		newNode.setAttribute("transactionid", obj.transactionid);
		newNode.setAttribute("id", obj.id);
		newNode.setAttribute("status", obj.status);
		newNode.setAttribute("readystate", obj.readystate);
		newNode.setAttribute("elapsed", obj.elapsed);
		
		var subNode = transactionHistory.XMLDocument.createNode(1, "URL","");
		subNode.text = obj.url;
		newNode.appendChild(subNode);

		subNode = transactionHistory.XMLDocument.createNode(1, "STATUSTEXT","");
		subNode.text = obj.statustext;
		newNode.appendChild(subNode);
		
		topNode.appendChild(newNode);
	}
	catch(e)
	{
		alert("dummyTransactionHistory\nError logging to transaction history:\n" + e.message);
	}
}

function updateTransactionHistory(obj)
{
	try
	{
		var tid;
		if (obj.getAttribute) tid = obj.getAttribute("transactionid");
		else tid = obj.transactionid;
		var tNode = transactionHistory.XMLDocument.selectSingleNode(
			"//TRANSACTION[@transactionid='" + tid + "']");
		if (tNode)
		{
			tNode.setAttribute("status", obj.status);
			tNode.setAttribute("readystate", obj.readystate);
			tNode.setAttribute("elapsed", obj.elapsed);
			var subNode = tNode.selectSingleNode("STATUSTEXT");
			subNode.text = obj.statustext;
		}
	}
	catch(e)
	{
		alert("updateTransactionHistory\nError logging to transaction history:\n" + e.message);
	}
}

function showTransactionHistory()
{
	
	document.all("maindisplay").src = "showTransactionHistory.htm";
	
	window.event.returnValue = false;
	return false;
}

function showTransactionNode(id)
{
	var tNode = transactionHistory.XMLDocument.selectSingleNode("//TRANSACTION[@transactionid='" + id + "']");
	if (tNode) alert(tNode.xml);
	else 
	{
		alert("Unable to find a transaction with id=" + id);
	}
}

function showTransaction(id, wnd)
{
	var tNode = transactionHistory.XMLDocument.selectSingleNode("//TRANSACTION[@transactionid='" + id + "']/URL");
	if (tNode) 
	{
		var uri = tNode.text;
		//window.open(uri);

		pObj = new Object();
		pObj.uri = uri;
		pObj.tNode =  tNode;
		pObj.sessionID = top.mainWindow.agt.sessionid;
		pObj.agent = top.mainWindow.agt.agentid;
		pObj.password = top.mainWindow.agt.password;
		pObj.httpobj = top.mainWindow.loginHelpObject;
		pObj.window = top;
		wnd.showModalDialog("showTransactionDialog.htm", pObj, "dialogHeight:400px;dialogWidth:700px;status:no;");
	}
	else alert("Unable to find transaction " + id + "\n" + transactionHistory.XMLDocument.xml);
}

function showPost(id, wnd)
{
	var uNode = transactionHistory.XMLDocument.selectSingleNode("//TRANSACTION[@transactionid='" + id + "']/URL");
	if (!uNode) 
		return alert("Unable to find transaction " + id + "\n" + transactionHistory.XMLDocument.xml);

	var tNode = transactionHistory.XMLDocument.selectSingleNode("//TRANSACTION[@transactionid='" + id + "']/POSTDATA");
	if (!tNode) 
		return alert("Unable to find POSTDATA for transaction " + id + "\n" + transactionHistory.XMLDocument.xml);
	
	pObj = new Object();
	pObj.url = uNode.text;
	pObj.postText = tNode.text;
	pObj.tNode =  transactionHistory.XMLDocument.selectSingleNode("//TRANSACTION[@transactionid='" + id + "']");
	pObj.agent = top.mainWindow.agt.agentid;
	pObj.password = top.mainWindow.agt.password;
	pObj.httpobj = top.mainWindow.loginHelpObject;
	wnd.showModalDialog("showPostDialog.htm", pObj, "dialogHeight:400px;dialogWidth:700px;status:no;");
	return;
	
}
function clearConsole()
{
	consoleHistory.loadXML("<CONSOLES/>");
}

function logConsole(msg)
{
	dt = new Date();

	var topNode = consoleHistory.XMLDocument.selectSingleNode("CONSOLES");
	var newNode = consoleHistory.XMLDocument.createNode(1, "CONSOLE","");

	var hrs = "0" + dt.getHours().toString();
	var mins = "0" + dt.getMinutes().toString();
	var secs = "0" + dt.getSeconds().toString();
	var mill = "00" + dt.getMilliseconds().toString();
	
	var tmstr = hrs.substring(hrs.length-2, hrs.length) + ":";
	tmstr += mins.substring(mins.length-2, mins.length) + ":";
	tmstr += secs.substring(secs.length-2, secs.length) + ".";
	tmstr += mill.substring(mill.length-3, mill.length);
	newNode.setAttribute("TIME", tmstr);
	var CDATASection = consoleHistory.createCDATASection(msg);
	newNode.appendChild(CDATASection);
	topNode.appendChild(newNode);
}
