// 
function GetInstrument(clientMessage)
{
	var oInstrumentRequest = new XMLHttpRequest();
	
	//
	oInstrumentRequest.onreadystatechange = function() {
	if (oInstrumentRequest.readyState == 4) 
	{
		var errMessage = 'None';
		try 
		{
			// Turn the JSON response into a JavaScript Object
			//
			var oJSON = eval('(' + oInstrumentRequest.responseText + ')');
			for( var currInst in oJSON ) 
			{
				for(var currType in oJSON[currInst])
				{
					for (currAction in oJSON[currInst][currType])
					{
						switch(currAction)
						{
							case('update') :
								for (var htmlId in oJSON[currInst][currType][currAction])
								{
									var htmlElem = document.getElementById(htmlId);
									if(htmlElem)
									{
										htmlElem.innerHTML = oJSON[currInst][currType][currAction][htmlId];
									}
								}
								break;
							case ('setClass') :
								for (var htmlId in oJSON[currInst][currType][currAction])
								{
									var htmlElem = document.getElementById(htmlId);
									if(htmlElem)
									{
										htmlElem.childNodes[1].childNodes[0].childNodes[2].className = oJSON[currInst][currType][currAction][htmlId];
									}
								}
								break;
								
							case('highlight') :
								for (var htmlId in oJSON[currInst][currType][currAction])
								{
									var htmlElem = document.getElementById(htmlId);
									var newColor = 'rgb(' + oJSON[currInst][currType][currAction][htmlId]['r'] + ',' + oJSON[currInst][currType][currAction][htmlId]['g'] + ',' + oJSON[currInst][currType][currAction][htmlId]['b'] + ')';
									if(htmlElem)
									{											
										htmlElem.parentNode.style.backgroundColor = newColor;
										setTimeout('FadeInstrument("' + htmlId + '" ,"' + oJSON[currInst][currType][currAction][htmlId]['r'] + '" ,"' + oJSON[currInst][currType][currAction][htmlId]['g'] + '" ,"' + oJSON[currInst][currType][currAction][htmlId]['b'] + '")', 500);
									}
								}
								break;
							
							case('backbase') :
								for (var functionName in oJSON[currInst][currType][currAction])
								{
									if(bpc)
									{
										bpc.execute(oJSON[currInst][currType][currAction][functionName]);
									}
								}
								break;	
											
							case ('chartpath') :
								var htmlId = 'chart_img_' + currInst;
								var htmlElem = document.getElementById(htmlId);
								if(htmlElem)
								{
									htmlElem.src = oJSON[currInst][currType][currAction];
								}
								
								break;
							case('listtable') :
								for (var targetTable in oJSON[currInst][currType][currAction])
								{
									if(bpc)
									{
										bpc.render(oJSON[currInst][currType][currAction][targetTable], 'replacechildren', 'id("' + targetTable + '")');
									}
								}
								break;
							case ('search') :
								bpc.execute('<s:task b:action="set" b:target="id(\'' + oJSON[currInst][currType][currAction]['name'] + '\')/@trad:MainType" b:value="600" />');
								bpc.execute('<s:task b:action="trigger" b:event="do-close" b:target="id(\'' + oJSON[currInst][currType][currAction]['name'] + '\')" />');
								bpc.render('<trad:popup-window id="popup-win" trad:title="Trading News" style="width:610px;left:75px;" >' +
										   '<trad:popup-windowbody><div id="SearchBody">' + oJSON[currInst][currType][currAction]['result'] + 
										   '</div></trad:popup-windowbody></trad:popup-window>', 'aslastchild', 'id(\'WorkingArea\')');
								break;
							
						}
					}
				}
			}
		}
		catch(e)
		{
			errMessage = 'GetInstrument Error: ';
			for(var errProp in e)
			{
				errMessage += errProp + ":" + e[errProp] + "\n";
			}
			
			//alert(errMessage);
		}

		// We're done with the request, now do it again.
		GetInstrument(errMessage);
	}
	}// End Function OnReadyStateChange
	if(clientMessage.length > 1000)
	{
		clientMessage = clientMessage.substring(0, 1000)
	}
	
	var sURI = '/tnapp/server.php?time='+(new Date().valueOf()) + '&MsgClient=' + escape(clientMessage);
	oInstrumentRequest.open('GET', sURI , true);
	oInstrumentRequest.send(null);
}

function FadeInstrument(sInstrument, red, green, blue) 
{
				
	var currElement = document.getElementById(sInstrument).parentNode;
	if (currElement)
	{
		var sBG = currElement.style.backgroundColor;

		var aNowColor = sBG.substring(sBG.indexOf('(') + 1, sBG.indexOf(')')).split(',');

// Set the text color
//		if(aNowColor[2] == 0 && parseFloat(currElement.innerHTML) < 0)
//		{
//			currElement.style.color = '#DD0000';
//		}
//		else if(aNowColor[2] == 0)
//		{
//			currElement.style.color = '#00DD00';
//		}

		// Animate the background color
		var aEndColor = [231, 231, 231];

		var aNewColor = [
			Math.min(255, parseInt(aNowColor[0]) + Math.ceil((aEndColor[0] - aNowColor[0]) / 2)),
			Math.min(255, parseInt(aNowColor[1]) + Math.ceil((aEndColor[1] - aNowColor[1]) / 2)),
			Math.min(255, parseInt(aNowColor[2]) + Math.ceil((aEndColor[2] - aNowColor[2]) / 2))
		];

		currElement.style.backgroundColor = 'rgb(' + aNewColor.join(',') + ')';

		// Little hack: blue is neither in red or in blue so we can use it to check if we've reached white
		// Store the timeout so we don't start 2 animations on the same element
		if(aNowColor[2]  != aEndColor[2]) 
		{
			currElement.iFadeTimeout = setTimeout('FadeInstrument("' + sInstrument + '" ,"' + red +'" ,"' + green + '" ,"' + blue + '")',100);
		}
		else
		{
			currElement.iFadeTimeout = null;
		}
	}
}
			
function getElementWidth(el) 
{
	return el[0].offsetWidth;
}
function getElementHeight(el) 
{
	return el[0].offsetHeight;
}