﻿function wv(ASymbolName){
	var symbolElm = document.getElementById("w" + ASymbolName + "n");
	var percElm = document.getElementById("w" + ASymbolName + "pc");
	var lastElm = document.getElementById("w" + ASymbolName + "vc");
	if (symbolElm && percElm && lastElm){
		symbolElm.style.backgroundColor="#F5F5DC";
		percElm.style.backgroundColor="#F5F5DC";
		lastElm.style.backgroundColor="#F5F5DC";
		symbolElm.style.cursor="pointer";
		percElm.style.cursor="pointer";
		lastElm.style.cursor="pointer";
	}
}
function wu(ASymbolName){
	var symbolElm = document.getElementById("w" + ASymbolName + "n");
	var percElm = document.getElementById("w" + ASymbolName + "pc");
	var lastElm = document.getElementById("w" + ASymbolName + "vc");
	if (symbolElm && percElm && lastElm){
		symbolElm.style.backgroundColor="";
		percElm.style.backgroundColor="";
		lastElm.style.backgroundColor="";
	}
}
function cl(AUrl){
	document.location.href = AUrl;
}

function wmFlashOff(AElm)
{
	elm = document.getElementById(AElm);
	elm.style.backgroundColor = "";
}	

function RefreshWorldMarketsBox()
{
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
		 xmlhttp = false;
		}
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}	
	
	var url = "/services/WorldMarketsBoxXML.ashx?a=" + Math.round(Math.random() * 999999999999)
	xmlhttp.open("GET", url,true);
	xmlhttp.onreadystatechange = onReadyStateHandler;
	xmlhttp.send(null);
	
	function onReadyStateHandler()
	{
		if (xmlhttp.readyState == 4)
		{
			var xml = xmlhttp.responseXML;
			var path = "/data/s";
			if (window.ActiveXObject)
			{
				var nodes = xml.selectNodes(path);
				for (i = 0; i < nodes.length; i++){
					parseNode(nodes[i]);
				}
			}else{
				var nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);	
				var result = nodes.iterateNext();
				while (result)
				{
					parseNode(result);
					result = nodes.iterateNext();
				}
			}
		}
	}

	
	function parseNode(ANode)
	{
		var symbolName = ANode.getAttribute("n");	
		if (document.getElementById("w" + symbolName + "n"))
		{
			var lastPrice = document.getElementById("w" + symbolName + "l").innerHTML;
			if (lastPrice.length <= 0) {lastPrice = 0};
			var percChange = ANode.getAttribute("p");
			var currentPrice = ANode.getAttribute("l");
			document.getElementById("w" + symbolName + "l").innerHTML = currentPrice;
			document.getElementById("w" + symbolName + "p").innerHTML = formatFloat(percChange, 2) + "%";
			document.getElementById("w" + symbolName + "v").innerHTML = formatFloat(currentPrice, 2);
			flashOn(lastPrice, percChange, currentPrice, "w" + symbolName + "n", "w" + symbolName + "p", "w" + symbolName + "v");
		}
	}
	
	function flashOn(ALastPrice, APercChange, ACurrentPrice, ASymbolElmName, APercElmName, AValueElmName)
	{
		var flashOffInterval = 2000;
		var moveUpBgColor = "#b9e1a9";
		var moveDownBgColor = "#FDCBCB";
		var winnerColor = "#005D00";
		var looserColor = "#B40000";
		
		var percElm = document.getElementById(APercElmName);
		var valueElm = document.getElementById(AValueElmName);
		var symbolElm = document.getElementById(ASymbolElmName);

		if (APercChange > 0)
		{
			symbolElm.style.color = winnerColor;
			valueElm.style.color = winnerColor;
			percElm.style.color = winnerColor;
		}
		else
		{
			if (APercChange < 0)
			{
				symbolElm.style.color = looserColor;
				valueElm.style.color = looserColor;
				percElm.style.color = looserColor;
			}
			else
			{
				symbolElm.style.color = "";
				valueElm.style.color = "";
				percElm.style.color = "";
			}
		}

		if ((ACurrentPrice > 0)&&(ALastPrice > 0)){
			if (ACurrentPrice > ALastPrice)
			{
				percElm.style.backgroundColor = moveUpBgColor;
				valueElm.style.backgroundColor = moveUpBgColor;
			}
			else
			{
				if (ACurrentPrice < ALastPrice)
				{
					percElm.style.backgroundColor = moveDownBgColor;
					valueElm.style.backgroundColor = moveDownBgColor;
				}
			}
		}
		setTimeout("wmFlashOff('" + APercElmName + "')", 2000);
		setTimeout("wmFlashOff('" + AValueElmName + "')", 2000);						
	}
	
}
