var trades_dummystring = "";
var tradestypid = 1;
//!Initalisierung der Javascriptfunktionen f&uuml;r die Top5Trades
/*!
 * Es werden die Texte der Top5Trades neu geladen und im HTML aktualisiert.
 */ 
function initTop5Trades(ue_typid)
{
  tradestypid = ue_typid;
  var initT5T = window.setInterval('aktualisierenTop5Trades()', 7000);
  var pictureT5T = window.setInterval('reloadPictureT5T()', 120000);
}

//!Neu Laden der Chart Picturs f&uuml;r den Intraday der Top5Trades
function reloadPictureT5T()
{
  var now = new Date();
  var trades_dummystring = parseInt( now.getTime() / 1000 );
  if(document.getElementsByTagName("img"))
  {
	  var images = document.getElementsByTagName("img");
	  for(var i =0; i < images.length; ++i)
	  {
	  	if(images[i].src.search(/small/) != -1)
	  		images[i].src = "images/charts/small/" + images[i].alt + ".png?" + trades_dummystring;
	  }
  }
}

//!Abholung des JSON Dokuments
/*! mit den evtl. ver&auml;nderten Inhalten der Top5Trades.
 */
function aktualisierenTop5Trades() 
{
  if ( window.XMLHttpRequest )
  {
    reqT5T = new XMLHttpRequest();
    if ( reqT5T )
    {
	  reqT5T.onreadystatechange = procesReqTop5Trades;
	  reqT5T.open("GET", "/cgi-bin/top5trades"+tradestypid+".json", true);
	  reqT5T.setRequestHeader("Pragma", "no-cache");
	  reqT5T.setRequestHeader("Cache-Control", "must-revalidate");
	  reqT5T.setRequestHeader("If-Modified-Since", document.lastModified);
	  reqT5T.send(null);
	}
  }
  else if ( window.ActiveXObject )
  {
    reqT5T = new ActiveXObject("Microsoft.XMLHTTP");
    if ( reqT5T )
    {
      reqT5T.onreadystatechange = procesReqTop5Trades;
      reqT5T.open("GET", "/cgi-bin/top5trades"+tradestypid+".json", true);
      reqT5T.setRequestHeader("Pragma", "no-cache");
      reqT5T.setRequestHeader("Cache-Control", "must-revalidate");
      reqT5T.setRequestHeader("If-Modified-Since", document.lastModified);
      reqT5T.send();
    }
  }
}

//! Status des zur&uuml;ckgesendeten JSON Dokument &uuml;berpr&uuml;fen 
/*! Wenn das angeforderte JSON Dokument zur&uuml;ck geschickt wurde und 
 *  es vollst&auml;ndigt ist, wird die Parse Routime aufgerufen
 */
function procesReqTop5Trades()
{
  if (reqT5T)
  {
	if ( reqT5T.readyState == 4 && reqT5T.status == 200 )
	{
	    parseTop5Trades(reqT5T.responseText);
	}
  }
}

//! Parsen des JSON Dokuments
/*! und das HTML Dokument entsprechend ver&auml;ndern.
 * <br>param jsonDocument JSON Dokument
 */
function parseTop5Trades(jsonDocument)
{
	//alert(tradestypid);
	jsonContent = eval("(" + jsonDocument + ")");
	var index = "";
	for(j=0; j < jsonContent.length; ++j)
	{
		//alert(jsonContent.length);
		if(tradestypid == 1)
		{
			if(j==0)
		 		index = "dax";
		 	else if(j==1)
		 		index = "mdax";
		 	else if(j==2)
		 		index = "tecdax";
		 	else if(j==3)
		 		index = "sdax";
		 	else if(j==5)
		 		index = "franz_toptitel";
		 	else if(j==4)
		 		index = "eurostoxx";
		 	else if(j==6)
		 		index = "TRADEGATE-NASDAQ100";
		 	else if(j==7)
		 		index = "TRADEGATE-US-BlueChip";
		 	else if(jsonContent.length == 11 && j==8)
		 		index = "TRADEGATE-PREPOX";
		 	else if(jsonContent.length == 11 && j==9 || jsonContent.length == 10 && j==8)
		 		index = "sonstige_franz";
		 	else if(jsonContent.length == 11 && j==10 || jsonContent.length == 10 && j==9)
		 		index = "sonst";
		 	else
		 		index="index";
		}
		else
		{
			if(j==0)
				index = "Sonstige"; 	
		}
		
		for(i=1; i < jsonContent[j].length+1; ++i)
		{
		 	if(document.getElementById(index+i+"_bid"))
		 	{	
			 	if(document.getElementById(index+i+"_bid"))
				 	document.getElementById(index+i+"_bid").innerHTML = jsonContent[j][i-1].bid;
				if(document.getElementById(index+i+"_ask"))
				 	document.getElementById(index+i+"_ask").innerHTML = jsonContent[j][i-1].ask;
				if(document.getElementById(index+i+"_trades"))
			 		document.getElementById(index+i+"_trades").innerHTML = jsonContent[j][i-1].trades;
			 	if(document.getElementById(index+i+"_delta"))
			 	{
			 		document.getElementById(index+i+"_delta").innerHTML = jsonContent[j][i-1].delta;
			 		document.getElementById(index+i+"_delta").className = jsonContent[j][i-1].class_delta;
			 	}
				if(document.getElementById(index+i+"_link"))
				{
				 	document.getElementById(index+i+"_link").innerHTML = jsonContent[j][i-1].name;
				 	document.getElementById(index+i+"_link").href = "orderbuch.php?isin="+jsonContent[j][i-1].isin;
				}
				if(document.getElementById(index+i+"_tr"))
				{
				 	document.getElementById(index+i+"_tr").title = jsonContent[j][i-1].isin;
				 	document.getElementById(index+i+"_tr").name = jsonContent[j][i-1].name;
				 	document.getElementById(index+i+"_tr").lang = jsonContent[j][i-1].index;
				 	
				 	document.getElementById(index+i+"_tr").onmouseover = function() { displaySmallChart(this.id.substr(0,this.id.length-4), this.title, this.name, false); };
				}
		 	}
	 	}
	}
	
}