var	_gecko			= (navigator.userAgent.indexOf("Gecko") != -1)?1:0;
var	_msie			= (navigator.userAgent.indexOf("MSIE") != -1)?1:0;

// Quote colors
var noChangeColor		= "E0E0FF";
var positiveChangeColor = "80FF80";
var negativeChangeColor = "FFC0C0";

// Connection information
var serverName		= "j1.forexpf.ru";
var serverPort		= "80";
var serverPath		= "proftrading"
var initPagePath	= "informers/initquotepage.jsp";
var reloadPagePath	= "informers/reloadquotepage.jsp";

// HTML information
var chartServerName	= "www.forexpf.ru";
var chartServerPort	= "80";
var chartServerPath	= "chart";
var chartWidth		= 900;
var chartHeight		= 700;

// Refresh timers & time
var	blink				= null;
var oldChild			= null;
var	TimerID				= -1;
var refreshRateSlow		= 60;
var refreshRateNormal	= 15;
var refreshRateFast		= 5;
var	refreshRate			= refreshRateNormal;
var lastUpdateTime		= '00:00:00';

// Button colors
var	bottonHighlightedColor			= 'black';
var	buttonHighlightedBGColor		= '#00FFFF';
var buttonHighlightedBorderColor	= 'black';
var	bottonNormalColor				= 'white';
var	buttonNormalBGColor				= '#008080';
var buttonNormalBorderColor			= 'white';

// *************************
// *                       *
// * HTML javascript calls *
// *                       *
// *************************
function InitQuotes()
{
	blink = document.getElementById('reloadBlink');
	setNormalTimer();
	oldChild = loadData();
//	if ( parent )
//		parent.quotesReady();
}

function showChart( chartName )	// cs
{
	wn = "chart_" + chartName;
	wf = 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1';
	wf += ',width=' + chartWidth;
	wf += ',height=' + chartHeight;
		
	window.open( getChartLocation(chartName), wn, wf );
}

function setSlowTimer()
{
	startReloadTimer( refreshRateSlow );
	setButtonHighlighted( 'slowSpeed', true );
	setButtonHighlighted( 'normalSpeed', false );
	setButtonHighlighted( 'highSpeed', false );
}

function setNormalTimer()
{
	startReloadTimer( refreshRateNormal );
	setButtonHighlighted( 'slowSpeed', false );
	setButtonHighlighted( 'normalSpeed', true );
	setButtonHighlighted( 'highSpeed', false );
}

function setFastTimer()
{
	startReloadTimer( refreshRateFast );
	setButtonHighlighted( 'slowSpeed', false );
	setButtonHighlighted( 'normalSpeed', false );
	setButtonHighlighted( 'highSpeed', true );
}

// ********************************
// *                              *
// * Internal javascript function *
// *                              *
// ********************************

function startReloadTimer( newRefreshRate )
{
	clearTimeout( TimerID );

	refreshRate = newRefreshRate;

	TimerID = setTimeout( "updateQuotes()", refreshRate*990 );
}

function updateQuotes()
{
	oldChild = reloadData( oldChild, lastUpdateTime );

	startReloadTimer( refreshRate );
}

function loadData()
{
	blink.style.display = 'block';

	scr				= document.createElement('script');
	scr.type		= 'text/javascript';
	scr.language	= 'JavaScript';
	scr.src			= getInitDataLocation();
	document.getElementsByTagName('head')[0].appendChild(scr);

	startReloadTimer( refreshRate );

	return scr;
}
  
function reloadData( oldChild, timestamp )
{
	if ( oldChild != null )
	    document.getElementsByTagName('head')[0].removeChild(oldChild);

	blink.style.display = 'none';

	scr				= document.createElement('script');
	scr.type		= 'text/javascript';
	scr.language	= 'JavaScript';
	scr.src			= getReloadDataLocation(timestamp);
	document.getElementsByTagName('head')[0].appendChild(scr);

	return scr;
}

function setButtonHighlighted( buttonid, highlighted )
{
	var button = document.getElementById( buttonid );
	if ( highlighted )
	{
		button.style.color				= bottonHighlightedColor;
		button.style.backgroundColor	= buttonHighlightedBGColor;
		button.style.borderColor		= buttonHighlightedBorderColor;
	}
	else
	{
		button.style.color				= bottonNormalColor;
		button.style.backgroundColor	= buttonNormalBGColor;
		button.style.borderColor		= buttonNormalBorderColor;
	}
}

// *************
// * Locations *
// *************
function getInitDataLocation()
{
	return 'http://' + serverName + ':' + serverPort + '/' + serverPath + '/' + initPagePath;
}

function getReloadDataLocation( timestamp )
{
	return 'http://' + serverName + ':' + serverPort + '/' + serverPath + '/' + reloadPagePath + '?lastUpdateTime='+ timestamp;
}
	
function getChartLocation( chartName )
{
	return 'http://' + chartServerName + ':' + chartServerPort + '/' + chartServerPath + '/' + chartName;
}

// *****************************************
// *                                       *
// *  Streamning javascript shortcut-calls *
// *                                       *
// *****************************************
function ii(inid, pr, ch, chRt, ts)		{	return initInstrumentQuoteInx(inid, pr, ch, chRt, ts);		}
function is(inid, b, a, ch, ts)			{	return initInstrumentQuoteStock(inid, b, a, ch, ts);		}
function inf(inid, pr, ch, ts)			{	return initInstrumentQuoteFutures(inid, pr, ch, ts);		}
function ci(inst, pr, ts)				{	return changeInstrumentQuoteInx(inst, pr, ts);				}
function cs(inst, b, a, ts)				{	return changeInstrumentQuoteStock(inst, b, a, ts);			}
function cf(inst, pr, ts)				{	return changeInstrumentQuoteFutures(inst, pr, ts);			}
function ccf(inst, pr, ts)				{	return changeInstrumentQuoteCurrencyFutures(inst, pr, ts);	}

function setLastUpdateTime( timestamp )
{
	lastUpdateTime = timestamp;

	blink.style.display = 'none';
}

function initInstrumentQuoteInx( instrumentId, price, change, changeRate, timestamp )	// ii
{
	var inst = document.getElementById(instrumentId);
	if ( inst == null )	return true;
	timestamp = adjustTimeStamp( timestamp );

	inst.cells[1].innerHTML = price;
	inst.cells[2].innerHTML = change;
	inst.cells[3].innerHTML = changeRate + "%";
	inst.cells[4].innerHTML = timestamp;

	var color	= getChangeColor( change );

	inst.cells[1].bgColor = color;
	inst.cells[2].bgColor = color;
	inst.cells[3].bgColor = color;

	return true;
}

function initInstrumentQuoteStock( instrumentId, bid, ask, change, timestamp )	// is
{
	var inst = document.getElementById(instrumentId);
	if ( inst == null )	return true;
	timestamp = adjustTimeStamp( timestamp );

	inst.cells[1].innerHTML = bid;
	inst.cells[2].innerHTML = ask;
	inst.cells[3].innerHTML = timestamp;

	var color	= getChangeColor( change );

	inst.cells[1].bgColor = color;
	inst.cells[2].bgColor = color;

	return true;
}

function initInstrumentQuoteFutures( instrumentId, price, change, timestamp )	// inf
{
	var inst = document.getElementById(instrumentId);
	if ( inst == null )	return true;
	timestamp = adjustTimeStamp( timestamp );

	inst.cells[1].innerHTML = price;
	inst.cells[2].innerHTML = change;
	inst.cells[3].innerHTML = timestamp;

	var color	= getChangeColor( change );

	inst.cells[1].bgColor = color;
	inst.cells[2].bgColor = color;

	return true;
}

function changeInstrumentQuoteInx( instrumentId, price, timestamp )	// ci
{
	var inst = document.getElementById(instrumentId);
	if ( inst == null )	return true;
	timestamp = adjustTimeStamp( timestamp );

	var oldPrice	= inst.cells[1].innerHTML;
	var change		= Math.round((price - oldPrice) * 100)/100;
	var changeRate	= Math.round(change / oldPrice * 10000)/100;

	inst.cells[1].innerHTML = price;
	inst.cells[2].innerHTML = change;
	inst.cells[3].innerHTML = changeRate + "%";
	inst.cells[4].innerHTML = timestamp;

	var color	= getChangeColor( change );

	inst.cells[1].bgColor = color;
	inst.cells[2].bgColor = color;
	inst.cells[3].bgColor = color;

	return true;
}

function changeInstrumentQuoteFutures( instrumentId, price, timestamp )	// cf
{
	var inst = document.getElementById(instrumentId);
	if ( inst == null )	return true;
	timestamp = adjustTimeStamp( timestamp );

	var oldPrice	= inst.cells[1].innerHTML;
	var change		= Math.round((price - oldPrice) * 100)/100;

	inst.cells[1].innerHTML = price;
	inst.cells[2].innerHTML = change;
	inst.cells[3].innerHTML = timestamp;

	var color	= getChangeColor( change );

	inst.cells[1].bgColor = color;
	inst.cells[2].bgColor = color;

	return true;
}

function changeInstrumentQuoteStock( instrumentId, bid, ask, timestamp )	// cs
{
	var inst = document.getElementById(instrumentId);
	if ( inst == null )	return true;
	timestamp = adjustTimeStamp( timestamp );

	var oldBid		= inst.cells[1].innerHTML;
	var oldAsk		= inst.cells[2].innerHTML
	var changeBid	= bid - oldBid;
	var changeAsk	= ask - oldAsk;

	inst.cells[1].innerHTML = bid;
	inst.cells[2].innerHTML = ask;
	inst.cells[3].innerHTML = timestamp;

	var colorBid = getChangeColor( changeBid );
	var colorAsk = getChangeColor( changeAsk );

	inst.cells[1].bgColor = colorBid;
	inst.cells[2].bgColor = colorAsk;

	return true;
}

function changeInstrumentQuoteCurrencyFutures( instrumentId, price, timestamp )	// ccf
{
	var inst = document.getElementById(instrumentId);
	if ( inst == null )	return true;
	timestamp = adjustTimeStamp( timestamp );

	var oldPrice	= inst.cells[1].innerHTML;
	var change		= Math.round((price - oldPrice) * 10000)/10000;

	inst.cells[1].innerHTML = price;
	inst.cells[2].innerHTML = change;
	inst.cells[3].innerHTML = timestamp;

	var color	= getChangeColor( change );

	inst.cells[1].bgColor = color;
	inst.cells[2].bgColor = color;

	return true;
}

function getChangeColor( change )
{
	if ( change < 0 )	return negativeChangeColor;
	if ( change > 0 )	return positiveChangeColor;
	return noChangeColor;
}

// This function is added to adjust the time for minus 2 hour
function adjustTimeStamp( timestamp )
{
/*	if ( timestamp.length == 8 )
	{
		var iHour		= parseInt( timestamp.substr( 0,2 ) );
		var iMinutes	= parseInt( timestamp.substr( 3,2 ) );
		var iSeconds	= parseInt( timestamp.substr( 6,2 ) );
		iHour = ( iHour < 2 ? iHour+22 : iHour-2 );
		timestamp	= (iHour < 10 ? '0' : '' )+iHour+':'+(iMinutes < 10 ? '0' : '' )+iMinutes+':'+(iSeconds < 10 ? '0' : '' )+iSeconds;
	}
*/
	return timestamp;
}
