
/**
Description of an event

This code was taken from the TradeBetX.com homepage and so some
information may not be used on this site.

evID 	- Identity number for the event
evName	- Name of the Event
evStart	- The date and time the event opens for trading
numContracts- The number of contracts contained in the event
evClass	- The Event class of this event
isLive	- Is true if this event is an 'in-running' event
startTime- The 'kick off' time of the event if it's 'in-running'
evGrpName- The event group name
startTimeStamp  - The 'kick-off' time in a time stamp format
**/   
function EvtListing(evID, evName, evStart, numContracts, evClass, isLive, startTime, evGrpName, startTimeStamp)
{
this.evID=evID;
this.evName= evName;
this.evStart = evStart;
this.numContracts = numContracts;
this.evClass = evClass; 
this.isLive = isLive;
this.startTime = startTime;
this.evGrpName = evGrpName;
this.startTimeStamp=startTimeStamp;
}

/**
Description of an Event Group

Contains an array of events in the event group

grpID	- Identity number for the group
grpName	- Name of the Group
evAry	- Array of the events in this event group
**/
function EvGrp(grpID, grpName, evAry)
{
this.grpID=grpID;
this.grpName= grpName;
this.evAry=evAry;
}

/**
Description of an Event Class

clsName	- The name of the class
grpAry	- An array containing all the event groups in this event class
**/
function EvCls(clsName, grpAry)
{
this.clsName = clsName;
this.grpAry = grpAry;
}

function Coupon(id, name, classID, className){
	this.id=id;
	this.name=name;
	this.classID=classID;
	this.className=className;
}


// And array of contract ids
var conIDAry = new Array();



//An array of event classes
var evClsAry = new Array();

// An array of all the event groups
var allGroupsAry = new Array();

// A group
var grp;

//An array of groups
var grpAry;

//An array of events
var eventAry;

var showallDisplay = 'block';
function createMenu()
{
	var marketDataFrame = document.getElementById("md");

	var menu = document.getElementById("sidemenu");
	while(menu.childNodes[0])
	{
		menu.removeChild(menu.childNodes[0]);
	}
	var submenu = document.createElement("UL");
//	submenu.className = "selected";

	var showAll = document.createElement("LI");
	showAll.innerHTML = "<a href=\"#\" onclick=\"createMenu();document.getElementById('show-all').style.display = 'none'; return false;\">All Markets</a><ul><li></li></ul>";
	showAll.id= "show-all";

	
	submenu.appendChild(showAll);


	var showAll = document.createElement("LI");
	showAll.innerHTML = "<a href=\"/aav2/trading/tradingHTML.jsp\">Most Active Today</a><ul><li></li></ul>";
	showAll.id= "mostActive";
	showAll.className = "selected";

	
	submenu.appendChild(showAll);

/*


	var selectedEvents = document.createElement("LI");
	selectedEvents.innerHTML = "<a href=\"#\" onclick=\"showNext(this.parentNode);document.getElementById('show-all').style.display = document.getElementById('show-all') == 'block' ? 'none':'block';return false;\">Selected Events</a>";
	selectedEvents.className = "selected";

	couponList = document.createElement("UL");
	for(var couponIndex = 0; couponIndex < coupons.length; couponIndex++)
	{
		if(coupons[couponIndex].name != "homepage")
		{
		var couponItem = document.createElement("LI");
		couponItem.className = "eventGroup";
		couponItem.innerHTML = "<a href=\"/aav2/trading/tradingHTML.jsp?evID="
					+ coupons[couponIndex].id
					+"&eventSelect=coupon_"
					+ coupons[couponIndex].id				
					+"&updateList=true"
					+"&showExpired=false"
					+"\" title=\"Event ID = "
					+ coupons[couponIndex].id					
					+ "\">" 
					+ coupons[couponIndex].name 
					+ "</a>";
		couponList.appendChild(couponItem);
		}
	}
	selectedEvents.appendChild(couponList);	
	submenu.appendChild(selectedEvents);
*/


	var featuredEventsList = document.createElement("LI");
	featuredEventsList.innerHTML = "<a href=\"#\" onclick=\"showNext(this.parentNode);document.getElementById('show-all').style.display = document.getElementById('show-all') == 'block' ? 'none':'block';return false;\">In-Running Events</a>";
	featuredEventsList.id="featuredEventsMenu";
	featuredEventsList.className = "selected";

	inrunningList = document.createElement("UL");
	for(var inrunningIndex = 0; inrunningIndex < featuredEvents.length; inrunningIndex++)
	{
		var inrunningItem = document.createElement("LI");
		inrunningItem.className = "eventGroup";
		if(marketDataFrame == null)
		{
			inrunningItem.innerHTML = '<a href="/aav2/trading/tradingHTML.jsp?evID='
				+ featuredEvents[inrunningIndex].evID
				+'&eventSelect='
				+ featuredEvents[inrunningIndex].evID				
				+'&updateList=true'
				+'&showExpired=false'
				+'" title="Event ID = inrunning'
				+ '">' 
				+ '<img class="featuredIcon" src="/aav2/images/featured.gif" height="9" width="9"/>'
				+ featuredEvents[inrunningIndex].evName 
				+ '</a>';
		}
		else
		{
			inrunningItem.innerHTML = '<a href="#" onclick="tradeEvent('
				+ featuredEvents[inrunningIndex].evID
				+ ','
				+ featuredEvents[inrunningIndex].evID
				+',false);return false;" title="Event ID = inrunning'
				+ '">' 
				+ '<img class="featuredIcon" src="/aav2/images/featured.gif" height="9" width="9"/>'
				+ featuredEvents[inrunningIndex].evName 
				+ '</a>';	
		}
		inrunningList.appendChild(inrunningItem);

	}
	featuredEventsList.appendChild(inrunningList);	
	submenu.appendChild(featuredEventsList);

	if(loggedIn == true)
	{


		var portfolioList = document.createElement("LI");
		portfolioList.innerHTML = "<a href=\"#\" onclick=\"showNext(this.parentNode);document.getElementById('show-all').style.display = document.getElementById('show-all') == 'block' ? 'none':'block';return false;\">My Portfolios</a>";
		portfolioList.className = "selected";

		portfolioMenu = document.createElement("UL");

		var portfolioItem = document.createElement("LI");
		portfolioItem.className = "eventGroup";
		
		if(marketDataFrame == null)
		{
			portfolioItem.innerHTML = "<a href=\"/aav2/trading/tradingHTML.jsp?evID=pf1"
						+"&eventSelect=pf1"
						+"&updateList=true"
						+"&showExpired=false"
						+"\" title=\"Event ID = pf1\">" 
						+ "Portfolio 1"
						+ "</a>";
		}
		else
		{
			portfolioItem.innerHTML = '<a href="#" onclick="tradeEvent(\'pf1\',\'pf1\',false);return false;" title="Event ID = pf1">'
						+ 'Portfolio 1'
						+ '</a>';
		
		}
		
		portfolioMenu.appendChild(portfolioItem);
		var portfolioItem = document.createElement("LI");
		portfolioItem.className = "eventGroup";
		if(marketDataFrame == null)
		{
			portfolioItem.innerHTML = "<a href=\"/aav2/trading/tradingHTML.jsp?evID=pf2"
						+"&eventSelect=pf2"
						+"&updateList=true"
						+"&showExpired=false"
						+"\" title=\"Event ID = pf2"
						+ "\">" 
						+ "Portfolio 2"
						+ "</a>";
		}
		else
		{
			portfolioItem.innerHTML = '<a href="#" onclick="tradeEvent(\'pf2\',\'pf2\',false);return false;" title="Event ID = pf2">'
						+ 'Portfolio 2'
						+ '</a>';

		}

						
		portfolioMenu.appendChild(portfolioItem);

		var portfolioItem = document.createElement("LI");
		portfolioItem.className = "eventGroup";
		if(marketDataFrame == null)
		{
			portfolioItem.innerHTML = "<a href=\"/aav2/trading/tradingHTML.jsp?evID=pf3"
						+"&eventSelect=pf3"
						+"&updateList=true"
						+"&showExpired=false"
						+"\" title=\"Event ID = pf3"
						+ "\">" 
						+ "Portfolio 3"
						+ "</a>";
		}
		else
		{
			portfolioItem.innerHTML = '<a href="#" onclick="tradeEvent(\'pf3\',\'pf3\',false);return false;" title="Event ID = pf3">'
						+ 'Portfolio 3'
						+ '</a>';

		}
		portfolioMenu.appendChild(portfolioItem);

		var portfolioItem = document.createElement("LI");
		portfolioItem.id = "addToPortfolio";
		portfolioItem.className = "eventGroup";
		portfolioItem.style.background = "white";
		portfolioItem.innerHTML = "<form method=\"get\" action=\"/aav2/search.jsp\"><span class=\"form-label\">Add Contract:</span><input id=\"portfolioInput\" name=\"searchText\"><button>OK</button></form>";
		portfolioMenu.appendChild(portfolioItem);
		
		portfolioList.appendChild(portfolioMenu);	
		submenu.appendChild(portfolioList);
	}

		var visitedList = document.createElement("LI");
		visitedList.innerHTML = "<a href=\"#\" onclick=\"showNext(this.parentNode);document.getElementById('show-all').style.display = document.getElementById('show-all') == 'block' ? 'none':'block';return false;\">Visited Events</a>";
		visitedList.className = "selected";

		visitedMenu = document.createElement("UL");
		visitedMenu.id = "visitedMenu";
	if(visEvAry.length > 0)
	{		
		createVisitedMenu(visitedMenu);
	}
	else
	{
		var blankLine = document.createElement("LI");
		blankLine.innerHTML = "<a href=\"#\" onclick=\"return false;\">&nbsp;</a>";
		blankLine.className = "blankline";
		visitedMenu.appendChild(blankLine);
	
	}
		visitedList.appendChild(visitedMenu);

		submenu.appendChild(visitedList);
	

	var blankLine = document.createElement("LI");
	blankLine.innerHTML = "<a href=\"#\" onclick=\"return false;\">&nbsp;</a>";
	blankLine.className = "blankline";
	submenu.appendChild(blankLine);
	if(window.location.href.match(/showExpired=true/))
	{
		var showExpired = true;
	}
	else
	{
		var showExpired = false;
	}
	for(var i = 0; i < evClsAry.length; i++)
	{
		var listing = document.createElement("LI");

		// this.ParentNode refers to the <li> which contains the link
		listing.className = "selected";
		listing.innerHTML = "<a href=\"#\" onclick=\"showNext(this.parentNode);document.getElementById('show-all').style.display = document.getElementById('show-all') == 'block' ? 'none':'block';return false;\">" + evClsAry[i].clsName + "</a>";
		listing.id = "eventClassMenu" + i;
		var groupMenu = document.createElement("UL");
		
		
		for(var j = 0; j < evClsAry[i].grpAry.length; j++)
		{
			var groupListing = document.createElement("LI");
			groupListing.className = "eventGroup";
			groupListing.innerHTML = "<a href=\"#\" id=\"group"+ evClsAry[i].grpAry[j].grpID +"\" onclick=\"showNext(this.parentNode);return false;\">" + evClsAry[i].grpAry[j].grpName + "</a>";
			
			createEventMenu(groupListing, evClsAry[i].grpAry[j]);

			groupMenu.appendChild(groupListing);
			listing.appendChild(groupMenu);
		}
		if(evClsAry[i].clsName.match(/HorseRacing/) != null) //Adding poolbet link
		{
			var groupListing = document.createElement("LI");
			groupListing.className = "eventGroup";
			groupListing.innerHTML = "<a href=\"/poolbet/\" style=\"color: red;\">Horseracing Pools NEW</a>";	
			/*
			
			var eventMenu = document.createElement("UL");
			var eventListing = document.createElement("LI");
			eventListing.className = "event";
			eventListing.innerHTML = "Poolbet";
			eventMenu.appendChild(eventListing);
			groupListing.appendChild(eventMenu);*/

			
			groupMenu.appendChild(groupListing);
			listing.appendChild(groupMenu);		
		}


		
		submenu.appendChild(listing);
	}


	var blankLine = document.createElement("LI");
	blankLine.innerHTML = "<a href=\"#\" onclick=\"return false;\">&nbsp;</a>";
	blankLine.className = "blankline";

	submenu.appendChild(blankLine);

	menu.appendChild(submenu);


	for(var i = 0; i < coupons.length; i++)
	{		
		couponsClassMenu = getCouponClassMenu(coupons[i].className);
		if(couponsClassMenu != null)
		{
	
			var couponListing = document.createElement("LI");
			couponListing.backgroundImage = "none";
			couponListing.className = "eventGroup";
			couponListing.id = "coupon_" + coupons[i].id;

			if(marketDataFrame == null)
			{
				couponListing.innerHTML = "<a href=\"/aav2/trading/tradingHTML.jsp?eventSelect=coupon_"
					+ coupons[i].id				
					+"&updateList=true"
					+"&showExpired="
					+ showExpired
					+"\" title=\"Event ID = "
					+ coupons[i].id					
					+ "\">" 
					+ coupons[i].name
					+ "</a>";
			}
			else
			{
				couponListing.innerHTML = '<a href="#" onclick=tradeEvent('
					+ coupons[i].id
					+ ','
					+ '"coupon_'
					+ coupons[i].id	
					+ '",'
					+ showExpired
					+');return false;" title="Event ID = '
					+ coupons[i].id
					+ '">' 
					+ coupons[i].name 
					+ '</a>';	
			}

			couponsClassMenu.appendChild(couponListing);
		}
	}

}
function createEventMenu(groupListing, grpAry)
{
	var marketDataFrame = document.getElementById("md");


	if(window.location.href.match(/showExpired=true/))
	{
		var showExpired = true;
	}
	else
	{
		var showExpired = false;
	}
	var eventMenu = document.createElement("UL");
	for(var k = 0; k < grpAry.evAry.length; k++)
	{
		var eventListing = document.createElement("LI");
		eventListing.className = "event";


		var featuredIcon = "";
		if(isFeatured(grpAry.evAry[k].evID))
		{
			featuredIcon = '<img class="featuredIcon" src="/aav2/images/featured.gif" height="9" width="9"/>';
		}


		if(marketDataFrame == null)
		{
			eventListing.innerHTML = '<a href="/aav2/trading/tradingHTML.jsp?evID='
				+ grpAry.evAry[k].evID
				+'&eventSelect='
				+ grpAry.evAry[k].evID				
				+'&updateList=true&showExpired='
				+ showExpired
				+'" title="Event ID = '
				+ grpAry.evAry[k].evID					
				+ '">' 
				+ featuredIcon
				+ grpAry.evAry[k].evName 
				+ '</a>';
		}
		else
		{

			eventListing.innerHTML = '<a href="#" onclick=tradeEvent('					
					+ grpAry.evAry[k].evID
					+','
					+ grpAry.evAry[k].evID
					+','					
					+ showExpired
					+');return false;" title="Event ID = '
				+ grpAry.evAry[k].evID					
				+ '">' 
				+ featuredIcon
				+ grpAry.evAry[k].evName 
				+ '</a>';
		}

		eventMenu.appendChild(eventListing);
		groupListing.appendChild(eventMenu);
	}
}
function createVisitedMenu(visitedMenu)
{
	var marketDataFrame = document.getElementById("md");
	while(visitedMenu != null && visitedMenu.childNodes[0])
	{
		visitedMenu.removeChild(visitedMenu.childNodes[0]);
	}

	for(i = 0; i < visEvAry.length; i++)
	{
		var visitedItem = document.createElement("LI");
		visitedItem.className = "selected";

		visitedItem.innerHTML = '<a href="#">'+visEvAry[i].evName + '</a>';

		if(marketDataFrame == null)
		{
			visitedItem.innerHTML = "<a href=\"/aav2/trading/tradingHTML.jsp?evID="
				+ visEvAry.evID
				+"&eventSelect="
				+ visEvAry.evID				
				+"&updateList=true"
				+"&showExpired=false"
				+"\" title=\"Event ID = Visited Event\">" 
				+ visEvAry[i].evName
				+ "</a>";
		}
		else
		{
			visitedItem.innerHTML = '<a href="#" onclick="tradeEvent(\''
				+ visEvAry[i].evID
				+'\',\''
				+ visEvAry[i].evID
				+'\',false);return false;" title="Event ID = Visited Event">'
				+ visEvAry[i].evName
				+ '</a>';
		}		

		if(visitedMenu != null)
		{
			visitedMenu.appendChild(visitedItem);
		}
	}
}
function getGroupListing(groupID)
{
	for(var i = 0; i < evClsAry.length; i++)
	{
		for(var j = 0; j < evClsAry[i].grpAry.length; i++)
		{
			if(evClsAry[i].grpAry[j].grpID == groupID)
			{
				return evClsAry[i].grpAry[j];
			}
		}
	}
	return null;
}
function tradeEvent(eventId, eventSelect, showExpired)
{
	var md = document.getElementById("md");
	if(md != null)
	{

		md.src = "/aav2/trading/marketDataScreen.jsp?evID="+eventId+"&eventSelect="+eventSelect+"&updateList=true&showExpired="+showExpired;
	}
}

function getCouponClassMenu(className)
{
	if(className != "")
	{
		var links = document.getElementsByTagName("A");
		for(var i = 0; i < links.length; i++)
		{
			var regex = new RegExp(className);
			if( links[i].innerHTML.match(regex) )
			{
				return links[i].parentNode.getElementsByTagName("UL")[0];
			}
		}
	}
	return null;
}

function isFeatured(eventID)
{

	if(typeof featuredEvents != 'undefined')
	{
		for(var i = 0; i < featuredEvents.length; i++)
		{
			if(featuredEvents[i].evID == eventID)
			{
				return true;
			}
		}	
	}
}

/**
	Hides and shows different levels in the menu

	parentElement			- 	The parent li of the link that was 
							clicked to call this function
**/
function showNext(parentElement)
{	
	elements = parentElement.childNodes;

	for(i = 0; i < elements.length; i++)
	{
		// We have to check for <ul>s as the parent <li> 
		// also contains the <a>nchor link that was clicked 
		if(elements[i].nodeName == "UL")
		{
			//If the lower menu items are visible hide them
			if((elements[i].style.display == "block" || parentElement.id == "show-all"))
			{
				//Set the parent nodes' class to be selected
				setSiblingNodesClassName(parentElement, "selected", "block");

				elements[i].style.display = "none";

				//Set all menu items below this to display none
				items = parentElement.getElementsByTagName("UL");
				for(j = 0; j < items.length; j++)
				{
					items[j].style.display = "none";
				}
			}
			else
			{
				//Set all nodes on the parent node's level to be unselected
				setSiblingNodesClassName(parentElement, "unselected", "none");

				//Set all the nodes on the lower level to be selected
				setSiblingNodesClassName(elements[i].childNodes[0],"selected", "block");
				elements[i].style.display = "block";
			}
		}

	}
	document.getElementById("featuredEventsMenu").style.display = "none";
}

/**
	Set the class and display value of nodes on a particular level

	node			- 	A node on the level
	className		- 	The css class you wish to set the nodes to
	displayValue	- 	The display style you wish to set the nodes
						to, the node specified in node will not have
						it's display style set
**/
function setSiblingNodesClassName(node, className, displayValue)
{
	//All nodes on the same level as node
	siblingNodes = node.parentNode.childNodes;
	
	// In order to tell which node node is in the loop
	// we set it's id to be current, it's original id 
	// will be restored at the end of the function
	savedId = node.id;
	node.id = "current";
	
	for(j = 0; j < siblingNodes.length; j++)
	{
		if(siblingNodes[j].nodeName == "LI")
		{
			if(!siblingNodes[j].className.match(/special-event/) && 
				!siblingNodes[j].className.match(/blankline/) )
			{
				siblingNodes[j].className = className;
			}
			
			//We don't want to set the display value of the current node
			//(It'd get hidden when we want it to be the only node 
			//displayed on the level)
			if(siblingNodes[j].id != "current" && displayValue == "none" || displayValue == "block")
			{
				if(siblingNodes[j].id != "show-all" && !siblingNodes[j].className.match(/special-event/))
				{
					siblingNodes[j].style.display = displayValue;
				}
			}
		}
	}

	//Restore the node's id
	node.id = savedId
}

/**
	Hide and shows the menu on the left of the page

	Also changes the width of the main-text div to fill the
	space left by the menu

	The widths for the main-text div are hardcoded, meaning
	they'll have to be edited here as well as in the relevant
	stylesheet. This is not very satifactory but other 
	solutions (eg using tables or allowing wrapping weren't 
	suitable)	

	id			- The id of the div which contains the menu
**/
function toggleLeftMenu( id )
{
	menu = document.getElementById(id);
	main = document.getElementById("main-text");
	closer = document.getElementById("left-closer");
	
	if(menu.style.display == "none")	//Show menu
	{
		menu.style.display = "block";
		
		main.style.marginLeft = "21%";
		if(main.style.width == "73%")
		{
			main.style.width = "55%";
		}
		else
		{
			main.style.width = "73%";
		}
		closer.style.left = "-1.5em";
		closer.innerHTML = "X";
	}
	else	//Hide menu
	{
		menu.style.display = "none";
		main.style.marginLeft = "2em";

		if(main.style.width == "73%")
		{
			main.style.width = "92%";
		}
		else
		{
			main.style.width = "73%";
		}
		closer.style.left = "0";
		closer.innerHTML = "open";
	}
}

/**
	Hide and shows the menu on the left of the page

	Roughly equivalent to toggleLeftMenu(), see it's 
	comments for more details

	id			- The id of the div which contains the menu
**/
function toggleRightMenu( id )
{
	menu = document.getElementById(id);
	main = document.getElementById("main-text");
	closer = document.getElementById("right-closer");
	if(menu.style.display == "none")
	{
		menu.style.display = "block";
		main.style.marginRight = "21%";
		if(main.style.width == "73%")
		{
			main.style.width = "55%";
		}
		else
		{
			main.style.width = "73%";
		}
		closer.style.left = "1.5em";
		closer.innerHTML = "X";
	}
	else
	{
		menu.style.display = "none";
		main.style.marginRight = "2em";
		if(main.style.width == "73%")
		{
			main.style.width = "92%";
		}
		else
		{
			main.style.width = "73%";
		}
		closer.style.left = "0";
		closer.innerHTML = "open";
	}
}


/**
	Sets the bid prices in the order book display

	Called from the trading applet

	id				- The row to be set
	quantity		- The quantity value
	price			- The price value
**/
function setBid( id, quantity, price )
{
	var table = document.getElementById("bid");
	if(table.rows.length < id+1)
	{
		addTableRow(table, 2);
	}
	if(table.rows[id+1])
	{
		var quantityCell = table.rows[id+1].cells[0];
	}
	
	setElem(quantityCell, quantity, id);
	
	if(table.rows[id + 1])
	{
		var priceCell = table.rows[id + 1].cells[1];
	}
	setElem(priceCell, price, id);

	if(0 == id)
	{
		if(top == self)
		{
			try
			{
				parent.selConBestBid = price;
			}
			catch(e)
			{
			
			}
		}
	}
	
}

function addTableRow(table, numCells)
{
	var row = document.createElement("TR");
	for(var i = 0; i < numCells; i++)
	{
		var cell = document.createElement("TD");
		cell.innerHTML = "&nbsp;";
		row.appendChild(cell);
	}
	table.tBodies[0].appendChild(row);
	return table;
}
/**
	Sets the offer prices in the order book display

	Called from the trading applet

	id				- The row to be set
	quantity		- The quantity value
	price			- The price value
**/
function setOffer( id, quantity, price )
{
	var table = document.getElementById("ask");

	if(table.rows.length - 1 <= id)
	{
		addTableRow(table, 2);
	}
	if(table.rows[id + 1])
	{
		var quantityCell = table.rows[id+1].cells[1];
	}
	setElem(quantityCell, quantity, id);
	
	if(table.rows[id + 1])
	{
		var priceCell = table.rows[id+1].cells[0];
	}
	setElem(priceCell, price, id);

	elem = document.getElementById('orderRow');

	if(0 == id)
	{
		if(top == self)
		{
			try
			{
				parent.selConBestAsk = price;
			}
			catch(e)
			{
			}
		}
	}
}

// The colours used as backgrounds for
// the prices in the order book
var colourAry = new Array("#FFE0C1","#C2E0E0","#C4ECFF","#FFFFD2","#EFEFEF","#FFE8FF");

/** Set the text and background of an 
 order book price or quantity cell
**/
function setElem (element, html, id) 
{
	if(element != null)
	{
		element.innerHTML=html;
		element.style.background=colourAry[id % colourAry.length];
	}
}

/**
	Set the height of the priceApplet

	Called from the applet itself

	height			- The height to be set
**/
function setHeight(height)
{
	var applet = document.getElementById("priceApplet");
	applet.height=height;

	var appletContainer = document.getElementById("applet-container");
	appletContainer.style.height = height + 25 + "px";
}

/**
	Set the displayed symbol in the order book

	Called from the trading applet

	conID			- The contract ID
	symbol			- The symbol of the contract
**/
function setTitle(conID, symbol)
{
	var elem = document.getElementById("obHeader");
	elem.innerHTML=symbol.replace(/&/, "&amp;");

}


/**
	Clear all the prices displayed in the order book
**/
function clearOrders()
{

	var bid = document.getElementById("bid");
	var ask = document.getElementById("ask");
	
	for(var i =1; i < bid.rows.length; i++)
	{
		clear(bid.rows[i]);
	}
	for(var i =1; i < ask.rows.length; i++)
	{
		clear(ask.rows[i]);
	}
}


/**
	Set the details of the Selected Contract

	Called by the trading applet

	newConID				- The Contract ID of the new selected contract
	symbol					- The symbol of the selected contract
	name					- The name of the selected contract
**/
function setSelConDetails(newConID, symbol, name, maxPriceValue)
{
	var oldConID = selConID;

    selConID = newConID
    selConSymbol =symbol;
	selConName =name;
	maxPrice = maxPriceValue;

	var contract = document.getElementById("contract-name");
	contract.innerHTML = selConName;

	var z = new Date().getTime();
}

/**
	Replace the contents of the element with the 
	given id to a non-breaking space

	id						- The id of the html element
**/
function clear(row) 
{
	if(row != null)
	{
		row.cells[0].innerHTML="&nbsp;";
		row.cells[0].style.height = 0;
		row.cells[0].style.background='white';

		row.cells[1].innerHTML="&nbsp;";
		row.cells[1].style.height = 0;
		row.cells[1].style.background='white';

	}
}

/**
	Display the time of the last update

	Called by the trading applet

	time				- The formatted time string to display
**/
function setTime( time)
{
	var timer = document.getElementById("timeCell");
	timer.innerHTML = time;
	
}

/**
	Sets the status bar of the browser

	args					- Text to display
**/
function doAlert(args)
{
	//Currently a dummy function to stop the browser complaining/crashing
}

/**
	Sets the price and quantity in the order ticket
	
	price					- The value for the price input
	quantity				- The value for the quantity input

**/
function setOrder(price, quantity)
{

	setPrice(price);
	if(quantity)
		setQuantity(quantity);
	else
		setQuantity(orderSize);
	upDateRisk();
}

/**
	Sets the price input in the order ticket

	val						- The price
**/
function setPrice(val)
{
	var elem = window.frames['orderTicketFrame'].document.getElementById("limitPriceInput");
	if(elem)
	{
		elem.value=val;
	}
}

/**
	Sets the quantity input in the order ticket

	val						- The quantity
**/
function setQuantity(val)
{
	if(val)
	{
		var elem = window.frames['orderTicketFrame'].document.getElementById("quantityInpt");
		if(elem)
		{
			elem.value=val;
		}
	}

}

/**
	Displays the menu item that coresponds with the given id

	eventID					- The id of the event we wish to display
**/
function setSelectedEvt(eventID,eventGroupID)
{
	var menu = document.getElementById("sidemenu");
	var allItems = menu.getElementsByTagName("LI");
	var links = menu.getElementsByTagName("A");
	var link = null;
	for(i = 0; i < links.length; i++)
	{
		title = links[i].getAttribute("title");
		if(title != null && title == "Event ID = " + eventID)
		{
			link = links[i];
			break;
		}
	}

	if(link != null)
	{

		for(var i = 0; i < allItems.length; i++)
		{
			if(allItems[i].id == "show-all")
			{
				allItems[i].style.display = "block";
			}
			else
			{
				allItems[i].style.display = "none";
			}
		
		
		}
		var showallDisplay = "block";
		if(link != null)
		{
			link.style.display = "block";
			link.parentNode.style.display = "block";
			link.parentNode.parentNode.style.display = "block";
			link.parentNode.parentNode.parentNode.style.display = "block";
			link.parentNode.parentNode.parentNode.parentNode.style.display = "block";
			link.parentNode.parentNode.parentNode.parentNode.parentNode.style.display = "block";

			setSiblingNodesClassName(link.parentNode.parentNode.parentNode.parentNode.parentNode, "unselected", "none");
			setSiblingNodesClassName(link.parentNode.parentNode.parentNode, "unselected", "none");
			setSiblingNodesClassName(link.parentNode, "selected", "block");
	
		}
	}
	else if(typeof eventGroupID != 'undefined')
	{
		link = null;
		for(i = 0; i < links.length; i++)
		{
			id = links[i].id;
			if(id != null && id == "group" + eventGroupID)
			{
				link = links[i];
				break;
			}
		}

		if(link != null)
		{


			var eventClassName = "";
			var groupMenu = link.parentNode;
			var groupArray = null;
			for(var i = 0; i < evClsAry.length; i++)
			{
				for(var j = 0; j < evClsAry[i].grpAry.length; j++)
				{
					if(eventGroupID == evClsAry[i].grpAry[j].grpID)
					{
						eventClassName = evClsAry[i].clsName;						
						groupArray = evClsAry[i].grpAry[j];

						break;
					}
				}
			}
			
			if(groupArray != null)
			{
				var showExpired = false;
				if(window.location.href.match(/showExpired=true/))
				{
					showExpired = true;
				}

				getEvent(eventClassName,groupArray.grpID,showExpired,true);
				
				
				
				link.style.display = "block";
				link.parentNode.style.display = "block";
				link.parentNode.parentNode.style.display = "block";
				link.parentNode.parentNode.parentNode.style.display = "block";



				setSiblingNodesClassName(link.parentNode.parentNode.parentNode.parentNode.parentNode, "unselected", "none");
				setSiblingNodesClassName(link.parentNode.parentNode.parentNode, "unselected", "none");
				setSiblingNodesClassName(link.parentNode, "unselected", "none");
				setSiblingNodesClassName(link, "selected", "block");
				setSiblingNodesClassName(groupMenu.childNodes[0], "selected", "block");
				document.getElementById('show-all').style.display = "block";
				var groupLink = parent.getElementById("group" + groupArray.grpID); 
				if(groupLink != null)
				{
					groupMenu = groupLink.parentNode;
					parent.showNext(groupMenu);
				}
			
			}
		}
	}
}
function setCancelAllInEvent(evID){
	var elem = getElementById("cancelAllLink");
	if(elem && evID && evID != "" && !isNaN(evID)){
		elem.innerHTML="<a href=# onclick='return cancelAllInEvent("+evID+")'>Cancel All In Event</a>";
	}else if(elem){
		elem.innerHTML="";
	}
}

function changeEvent(){
	var evID = getElementById("eventSelect").value;
	var applet = getElementById("priceApplet");


	if(evID == "pf1"){
		applet.setContractIDs(portfolio1);
	}else if(evID == "pf2"){
		applet.setContractIDs(portfolio2);
	}else if(evID == "pf3"){
		applet.setContractIDs(portfolio3);
	}else if (evID && evID != "" ){
		applet.setEventID(evID);
	}

	setCancelAllInEvent(evID);

}

var selectAdded = false;
function addEvent(elem, text, value)
{
	var option = document.createElement("OPTION");
	option.text=text;
	option.value=value;
	
	if(navigator.org=='netscape' || navigator.org=='mozilla')
	{
	    elem.add(option, null);
	}
	else
	{
	    elem.add(option);
	}

	if(m_eventID==value && !selectAdded)
	{
		option.selected=true;
		selectAdded=true;
	}
		
		
	return option;
}


function populateSelect(){

	var selEvent = getElementById("eventSelect");
	
	if(selEvent==null)
		return;
/*
	var liveEvents = parent.liveEvents;
	if(null != liveEvents && liveEvents.length>0){
		addEvent(selEvent, "  " , "");
		option = addEvent(selEvent, "--Upcoming Events-- " , "");
		option.style.color="#ff0000";
		option.style.backgroundColor="#f0f0f0";	
	
		for(j=0; j<liveEvents.length; j++){
			theEvent = liveEvents[j];
			addEvent(selEvent,  " "+theEvent.evName, theEvent.evID);
		}
	}
*/
	//add the visited events
	if(null != visEvAry  && visEvAry.length>0){
		addEvent(selEvent, "  " , "");
		var option = addEvent(selEvent, "--Visited Events-- " , visEvAry[0].evID);
		option.style.color="#ff0000";
		option.style.backgroundColor="#f0f0f0";	
	
		for(var j=0; j<visEvAry.length; j++){
			var theEvent = visEvAry[j];
			var option = addEvent(selEvent,  " "+theEvent.evName, theEvent.evID);
			
				
		}
	}
}


function contractDetail(conID)
{
	var date = new Date();
	var z = date.getTime();
	var win = window.open('/aav2/trading/contractInfo.jsp?conDetailID='+conID+"&z="+z,
						    'tsConDetail',
							'width=720,height=625,top=3,left=3,resizable=yes,menubar=no,status=no,toolbar=no,dependent=yes, scrollbars=1');
	win.focus();
	return false;
}

function getOrder(id,conID)
{
	var date = new Date();
	var z = date.getTime();
	window.open('/aav2/trading/orderDetail.jsp?orderID='+id+"&contractID="+conID+"&z="+z,'resultWin','width=500,height=250,top=3,left=3,resizable=yes,menubar=no,status=no,toolbar=no,scrollbars=yes,dependent=yes');
	return;
}

function usernameInput(event)
{
	if(event.keyCode)
	{
		if(event.keyCode == 13)
		{
			if(document.intrade1.password)
			{
				document.intrade1.password.focus();
			}
		}
	}	
}
function passwordInput(event)
{
	if(event.keyCode)
	{
		if(event.keyCode == 13)
		{
			doSubmit();
		}
	}	
}

function isNoConfirmSet(){
	var elem = getElementById("noConfirmChk");
	if(elem){
		return elem.checked;
	}	
	return false;
}

function setOrderConfirm(){
	if(isNoConfirmSet()) {
		alert("WARNING: When this flag is set, there will be no confirmation screen displayed when entering an order!");
		SetCookie("noConf", 1, true);
	}else{
		DeleteCookie("noConf");
	}

}

function initAutoConfirm(){
	var noConf = GetCookie("noConf");
	if(null != noConf){
		var elem = getElementById("noConfirmChk");
		if(elem){
			elem.checked=true;
		}	
	}
}

function tradingInit()
{
//	getAppletJS();
	initTradePage();
}


function initTradePage(){
	initOrderSize();
	initAutoConfirm();
	populateSelect();
	if(frames['reports'].init)
	{
		frames['reports'].init();
	}
}

var applet = document.getElementById("priceApplet");

var serverTime="";

function getAppletJS(){

	if(null== applet  )
	{
		applet = getElementById("priceApplet");
	}
	if(applet)
	{
		try
		{
			var newServerTime = applet.getTime();
			if(newServerTime != serverTime)
			{
				serverTime = newServerTime;
				setTime(serverTime);
			}
			var js = applet.getJavaScript();
			if(null!=js && js )
			{
				eval("this."+js);
			}			

		}catch(e){
			applet = getElementById("priceApplet");
		}

	}	

	setTimeout("getAppletJS()", 300);
}


function doRightClickTrade(isBidSideClicked){
	if(orderSize >lotsAvailable)
		setQuantity(lotsAvailable);

	if(isBidSideClicked)
		submitOrder(false);
	else
		submitOrder(true);
	return false;
}

function setLotsAvailable(lots){
	if(isNaN(lots))
		lotsAvailable = 0;
	else
		lotsAvailable=Number(lots);
}

function getFaqs(incpage,id)
{
	window.open('/aav2/rulesAndFaqs.jsp?helpPage=banking', 'faq', 'width=900, scrollbars=1');
}


function changeContract(conID){
	return frames['md'].changeContract(conID);
}

function changeContractAndEvent(conID){
	if(null == applet)
	  applet = getElementById("priceApplet");
	if(applet)
		applet.setSelectedContract(conID);
	return false;
}

function setSelectedEvent(evID){
	getElementById("eventSelect").value=evID;
	setCancelAllInEvent(evID);
}


function loadClassEvents(className)
{
	getEventsFrame = document.getElementById("getEventsFrame");
	getEventsFrame.src = "/aav2/menu/getEvents.jsp?classname=" + className;
	
}

function addEvents(eventGroup)
{
	
	var menu = document.getElementById("sidemenu");
	var listItem = document.getElementById("group" + eventGroup.grpID );
	var link = listItem.childNodes[0];	//Possibly fragile code, depends
										//on the link being the first 
										//child node of the list item
											
	var eventMenu = document.createElement("UL");
	eventMenu.className = "unselected";
	for(var k = 0; k < eventGroup.evAry.length; k++)
	{
		var eventListing = document.createElement("LI");
		eventListing.innerHTML = "<a href=\"/aav2/trading/tradingHTML.jsp?evID="
			+ eventGroup.evAry[k].evID
			+"\" title=\"Event ID = "
			+ eventGroup.evAry[k].evID
			+ "\">" 
			+ eventGroup.evAry[k].evName 
			+ "</a>";

		eventMenu.appendChild(eventListing);
		listItem.appendChild(eventMenu);
	}

}


function togglePrices(convertingToDollars, lotValue)
{
	var priceCells = new Array();
	priceCells[0] = document.getElementById("bp0");
	priceCells[1] = document.getElementById("bp1");
	priceCells[2] = document.getElementById("bp2");
	priceCells[3] = document.getElementById("bp3");
	priceCells[4] = document.getElementById("bp4");

	priceCells[5] = document.getElementById("op0");
	priceCells[6] = document.getElementById("op1");
	priceCells[7] = document.getElementById("op2");
	priceCells[8] = document.getElementById("op3");
	priceCells[9] = document.getElementById("op4");

	var quantityCells = new Array();
	
	if( (convertingToDollars && !priceCells[0].innerHTML.match(/\$/) )
		|| 
		(!convertingToDollars && priceCells[0].innerHTML.match(/\$/))
		)
	{

	
		if(convertingToDollars)
		{   
			var dollarSign = "$";
			var decimalPlaces = 2;

	//		lotValue = 1 / lotValue;
		}
		else
		{
	//		var lotValueInv = 1 / lotValueInv;
			dollarSign = "";
			decimalPlaces = ("" + lotValue).length - (("" + lotValue).indexOf('.') + 2);
		}              

		for(var i = 0; i < priceCells.length; i++)
		{
			if(priceCells[i])
			{
				var value = priceCells[i].innerHTML.replace(/\$/, "");
				if(quantityCells[i])
				{
					var quantityValue = quantityCells[i].innerHTML.replace(/\$/, "");
					if(Number(value))
					{
						quantityCells[i].innerHTML = dollarSign + (quantityValue * value * lotValue).toFixed(decimalPlaces);
					}
					
				}
			
				if(Number(value))
				{
					priceCells[i].innerHTML = dollarSign + (value * lotValue).toFixed(decimalPlaces);
				}
			}
		}

		var reports = frames["reports"];
		if(reports && reports.location.href.match(/positions.jsp/) && reports.reportType)
		{

			reports.togglePrices(convertingToDollars, lotValue, reports.reportType());
		}	
	}

}
function showDetails()
{
	contractDetail(selConID);
}

