var reqMarketMoversTop, reqMarketMoversFlop, reqMostActives;
var MarketMoversContainer, MostActivesContainer;

var htmlMarketMoversTop = "";
var htmlMarketMoversFlop = "";
var selectedExchange="";
var dashboard_path = "";
function loadMarketMovers(id,exchange) {
        selectedExchange = exchange;
	MarketMoversContainer = id;
	document.getElementById(MarketMoversContainer).innerHTML = "<img class='inProgress' src='../images/inprogress.gif' alt='Loading' />";
	reqMarketMoversTop = initRequest();
	//var urlTop = dashboard_path + 'market_overview.ajax?BLOCKSIZE=4&TYPE=TOP&ID_EXCHANGE=' + exchange;
        var urlTop = dashboard_path + 'ef_market_overview.idms?ID_EXCHANGE=' + exchange +'&BLOCKSIZE=4&TYPE=top';
	reqMarketMoversTop.open("POST", urlTop, true);
	reqMarketMoversTop.onreadystatechange = callBackMarketMoversTop;        
	reqMarketMoversTop.send(null);
        
}

function callBackMarketMoversTop() 
{
	if (reqMarketMoversTop.readyState == 4) 
	{
		if (reqMarketMoversTop.status == 200) 
		{
			if (reqMarketMoversTop.responseText.length > 0) 
			{
				//var jsonExpression = "(" + reqMarketMoversTop.responseText + ")";
				//var jsonObj = eval(jsonExpression);
				displayMarketMoversTopXML(reqMarketMoversTop.responseXML);
			}
		}
	}
}

function displayMarketMoversTopXML(xmlData)
{
	if (xmlData !=null)
	{
		htmlMarketMoversTop = "<div class='gainers'>";
		htmlMarketMoversTop += "<table>";
		htmlMarketMoversTop += "<thead><tr><th colspan='5' class='gainers'>Gainers</th></tr></thead>";
		//htmlMarketMoversTop += "<tbody><tr><th class=\"leftcap\">Symbol</th><th class=\"leftcap\">Company</th><th class=\"rightcap\">Price</th><th class=\"rightcap\">Change</th><th class=\"rightcap\">Change %</th></tr>";
                htmlMarketMoversTop += "<tbody><tr>" + glHeader + "</tr>";
                var quotes = xmlData.getElementsByTagName("quote");
		for (i=0;i<quotes.length;i++)
		{
                        var quote = quotes[i];
			htmlMarketMoversTop += "<tr><td><a href='quote.idms?SYMBOL_US=" + quote.getElementsByTagName("symbol")[0].firstChild.nodeValue + LOGON + "'>" + quote.getElementsByTagName("symbol")[0].firstChild.nodeValue + "</a></td><td onmouseover='overlib(\""+ quote.getElementsByTagName("name")[0].firstChild.nodeValue +"\")' onmouseout='return nd();'>" + quote.getElementsByTagName("name")[0].firstChild.nodeValue.truncate(13) + "</td><td class=\"rightcap\">" + quote.getElementsByTagName("last")[0].firstChild.nodeValue + "</td><td class=\"rightcap\"><span class='positive'>" + quote.getElementsByTagName("chg")[0].firstChild.nodeValue + "</span></td><td class=\"rightcap\"><span class='positive'>" + quote.getElementsByTagName("pchg")[0].firstChild.nodeValue + "</span></td></tr>";
		}
		htmlMarketMoversTop += "</tbody></table></div>";

		// document.getElementById(MarketMoversContainer).innerHTML = ShowMarketMoversExchange(json.idExchange);
		document.getElementById(MarketMoversContainer).innerHTML = htmlMarketMoversTop;
                calcHeight();
		reqMarketMoversFlop = initRequest();
		//var urlFlop = dashboard_path + 'market_overview.ajax?BLOCKSIZE=4&TYPE=FLOP&ID_EXCHANGE=' + json.idExchange;
                var urlFlop = dashboard_path + 'ef_market_overview.idms?ID_EXCHANGE=' + selectedExchange +'&BLOCKSIZE=4&TYPE=flop';
		reqMarketMoversFlop.open("POST", urlFlop, true);
		reqMarketMoversFlop.onreadystatechange = callBackMarketMoversFlop;
		reqMarketMoversFlop.send(null);
	}
}

function callBackMarketMoversFlop() 
{
	if (reqMarketMoversFlop.readyState == 4) 
	{
		if (reqMarketMoversFlop.status == 200) 
		{
			if (reqMarketMoversFlop.responseText.length > 0) 
			{
				//var jsonExpression = "(" + reqMarketMoversFlop.responseText + ")";
				//var jsonObj = eval(jsonExpression);
				displayMarketMoversFlopXml(reqMarketMoversFlop.responseXML);
			}
		}
	}
}
var glHeader ="";
function displayMarketMoversFlopXml(xmlData)
{
        htmlMarketMoversFlop = "";
	if (xmlData!=null)
	{ 
		htmlMarketMoversFlop = "<div class='losers'>";
		htmlMarketMoversFlop += "<table><thead><tr><th colspan='5' class='losers'>Losers</th></tr></thead>";
		//htmlMarketMoversFlop += "<tbody><tr><th class=\"leftcap\">Symbol</th><th class=\"leftcap\">Company</th><th class=\"rightcap\">Price</th><th class=\"rightcap\">Change</th><th class=\"rightcap\">Change %</th></tr>";
                htmlMarketMoversFlop += "<tbody><tr>" + glHeader + "</tr>";
                var quotes = xmlData.getElementsByTagName("quote");
		for (i=0;i<quotes.length;i++)
		{
                        var quote = quotes[i];
			htmlMarketMoversFlop += "<tr><td><a href='quote.idms?SYMBOL_US=" + quote.getElementsByTagName("symbol")[0].firstChild.nodeValue + LOGON + "'>" + quote.getElementsByTagName("symbol")[0].firstChild.nodeValue + "</a></td><td onmouseover='overlib(\""+ quote.getElementsByTagName("name")[0].firstChild.nodeValue +"\")' onmouseout='return nd();'>" + quote.getElementsByTagName("name")[0].firstChild.nodeValue.truncate(13) + "</td><td class=\"rightcap\">" + quote.getElementsByTagName("last")[0].firstChild.nodeValue + "</td><td class=\"rightcap\"><span class='negative'>" + quote.getElementsByTagName("chg")[0].firstChild.nodeValue + "</span></td><td class=\"rightcap\"><span class='negative'>" + quote.getElementsByTagName("pchg")[0].firstChild.nodeValue + "</span></td></tr>";
		}
		htmlMarketMoversFlop += "</tbody></table></div>";
		document.getElementById("MarketMovers").innerHTML += htmlMarketMoversFlop;
                calcHeight();
	}
}

function loadMostActives(id,exchange) {
	MostActivesContainer = id;
	document.getElementById(MostActivesContainer).innerHTML = "<img class='inProgress' src='../images/inprogress.gif' alt='Loading' />";
	reqMostActives = initRequest();
	//var url = dashboard_path + 'market_overview.ajax?BLOCKSIZE=10&TYPE=VOLUME&ID_EXCHANGE=' + exchange;
	var url = dashboard_path + 'ef_market_overview.idms?ID_EXCHANGE=' + exchange +'&BLOCKSIZE='+10;
        reqMostActives.open("POST", url, true);
	reqMostActives.onreadystatechange = callBackMostActives;
	reqMostActives.send(null);        
}

function callBackMostActives() 
{
	if (reqMostActives.readyState == 4) 
	{
		if (reqMostActives.status == 200) 
		{
			if (reqMostActives.responseText.length > 0) 
			{
				//var jsonExpression = "(" + reqMostActives.responseText + ")";
				//var jsonObj = eval(jsonExpression);
				//displayMostActives(jsonObj);                               
                                displayMostActivesXml(reqMostActives.responseXML);
			}
		}
	}
}

function displayMostActivesXml(xmlData)
{
var htmlMostActivesList = "";
htmlMostActivesList = '<div id="most_actives">	<table>	<tr>';
        htmlMostActivesList += mostActiveHeader;
        htmlMostActivesList +=  '</tr>';

	if (xmlData!=null)
	{
                var quotes = xmlData.getElementsByTagName("quote");               
		for(i=0;i<quotes.length;i++)
		{                       
                        var quote = quotes[i];                        
			if (quote.getElementsByTagName("changeSign")[0].firstChild.nodeValue == "-1")
			{
				posneg = "Negative" + ";";
			}
			else if (quote.getElementsByTagName("changeSign")[0].firstChild.nodeValue == "1")
			{
				posneg = "Positive" + ";";
			}
			else
			{
				posneg = "Neutral" + ";";
			}

			htmlMostActivesList += '<tr>';
			htmlMostActivesList += '<td><a href="quote.idms?SYMBOL_US='+quote.getElementsByTagName("symbol")[0].firstChild.nodeValue+ LOGON + '">'+quote.getElementsByTagName("symbol")[0].firstChild.nodeValue+'</a></td>';
			htmlMostActivesList += '<td class="rightcap"><span class="'+posneg+'">'+quote.getElementsByTagName("chg")[0].firstChild.nodeValue+'</span></td>';
			htmlMostActivesList += '<td class="rightcap"><span class="'+posneg+'">'+quote.getElementsByTagName("pchg")[0].firstChild.nodeValue+'</span></td>';
			htmlMostActivesList += '<td class="rightcap">'+quote.getElementsByTagName("volume")[0].firstChild.nodeValue+'</td>';
			htmlMostActivesList += '<td class="rightcap">'+quote.getElementsByTagName("last")[0].firstChild.nodeValue+'</td>';
			htmlMostActivesList +=	'</tr>';
		}
		document.getElementById(MostActivesContainer).innerHTML = htmlMostActivesList;
                calcHeight();
	}
}

//XML Http Request Object
function initRequest() {
	var xmlHttpRequest;
	try
	{ 
		xmlHttpRequest = new XMLHttpRequest(); 
	}
	catch(e)
	{
		try
		{
			xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
			}
		}
	}
	return xmlHttpRequest;
}