// JavaScript Document

var currencyHTML="";


function worldIndices()
{
	var indexXmlHttp;
	
	indexXmlHttp = ajaxFunction(indexXmlHttp);
	
	if( typeof(indexXmlHttp) == "Boolean" )
	{
		if(indexXmlHttp == false)
		{
			return;
		}
	}
	
		
	indexXmlHttp.onreadystatechange=function(){worldIndicesGet(indexXmlHttp);};
		
	worldIndicesLoopFunction();
}

function worldIndicesGet(indexXmlHttp)
{
	var xmlHttp = indexXmlHttp;
	
	var greencolor = "#339933";
	var redcolor  = "#CC0000";

	if(xmlHttp.readyState==4)
    {
	  if( xmlHttp.responseXML != null )
	  {		
        var xmldocEle = xmlHttp.responseXML.documentElement;
			
	  
	    if(xmldocEle != null)
	    {	   
		    parseAndFillIndices(xmldocEle, "US");
			parseAndFillIndices(xmldocEle, "Europe");
			parseAndFillIndices(xmldocEle, "Asia");
			parseAndFillIndices(xmldocEle, "EmergingMarkets");
	    }
	  
	  }
	  
    }

	
}


function parseAndFillIndices(xmldocEle, region)
{
			eleArr = xmldocEle.getElementsByTagName(region + 'Index');
		    if(eleArr != null)
		    {
			  	var length = eleArr.length;
				var i;
				
				//alert(length);
				
				
				for(i=0; i<length; i++)
				{
			      var topEle = eleArr[i];
				  var childArr = topEle.childNodes;
				  var childLength = childArr.length;
				  var text = "";
				  
				  
				  var name, ltp, perChg, chg, timeStamp;
				  //alert(topEle.nodeName);
				  
				  for(j=0; j<childLength; j++)
				  {
					var node = childArr[j];
					var nodename = node.nodeName;
					if( nodename == "Name")
					{
						name = node.childNodes[0].nodeValue;
					}
					if( nodename == "CurVal")
					{
						ltp = node.childNodes[0].nodeValue;
					}
					if( nodename == "PerChg")
					{
						perChg = node.childNodes[0].nodeValue;
						
					}
					
					if( nodename == "Chg")
					{
						chg = node.childNodes[0].nodeValue;
						
					}
					if( nodename == "TimeStamp")
					{
						timeStamp = node.childNodes[0].nodeValue;
						timeStamp = timeStamp + " IST";
					}
				  }
				  
				  //text = text + "\n" + childArr[j].nodeValue;  
				  
				  var perClass="worldIndexLoserPerChg";
				  var chgClass="worldIndexLoserChg";
				  if(chg >0)
				  {
					  perClass="worldIndexGainerPerChg";
					  chgClass="worldIndexGainerChg";
				  }
				  
				  //alert(name);
								
				  addRow(region+"Table", region, name, ltp, chg, chgClass, perChg, perClass, timeStamp);
				
				  				 
				}
				
				
				
			
		    }//end if eleArr
	
}




function worldIndicesLoopFunction()
{
	var indexXmlHttp;
	
	indexTimeout=setTimeout(function(){worldIndicesLoopFunction();},60000);
	//alert(indexURL);

    indexXmlHttp = ajaxFunction();

	indexXmlHttp.onreadystatechange=function(){worldIndicesGet(indexXmlHttp);};

	indexXmlHttp.open(hJ,h1G,true);
	indexXmlHttp.send(null);
	
	

}

 function addRow(tableId, region, name, ltp, chg, chgClass, perChg, perClass, timeStamp){
    var tbody = document.getElementById
(tableId).getElementsByTagName("TBODY")[0];

	//Delete the existing rows
    var nodelist = tbody.childNodes;

	var reqdEle = document.getElementById(region+name+"ltp");
	var oldVal=null;
	if(reqdEle != null)
	{
		oldVal = reqdEle.childNodes[0].nodeValue;
		//alert(name + " " + oldVal);
	}
	
	var oldRowNode = document.getElementById(region+name+"Row"); 
	//alert(oldRowNode);
	
	

    var row = document.createElement("TR")
	row.id=region+name+"Row";
    var td1 = document.createElement("TD")
    td1.appendChild(document.createTextNode(name))
	td1.id=name;
	
    var td2 = document.createElement("TD")
	td2.setAttribute('tdType', 'ltp');
	td2.id=region+name+"ltp";
    td2.appendChild (document.createTextNode(ltp))
	
	var td3 = document.createElement("TD")
	td3.setAttribute('tdType', 'chg');
	td3.id=region+name+"Chg";
	td3.appendChild (document.createTextNode(chg))
	
	var td4 = document.createElement("TD")
	td4.setAttribute('tdType', 'perChg');
	td4.id=region+name+"PerChg";
	td4.appendChild (document.createTextNode(perChg))
	
	
	
	var td5 = document.createElement("TD")
	td5.id=region+name+"timeStamp";
    td5.appendChild (document.createTextNode(timeStamp))
	td5.style.fontSize="7pt";
	
	if(chg >0)
	{
		td4.style.color="#006600";
		td3.style.color="#006600";
	}
	else
	{
		td4.style.color="#CC0000";
		td3.style.color="#CC0000";
	}
	
	
    row.appendChild(td1);
    row.appendChild(td2);
	row.appendChild(td3);
	row.appendChild(td4);
	row.appendChild(td5);
    
	if(oldRowNode != null)
	{
		tbody.replaceChild(row, oldRowNode );
		//alert(name);
	}
	else
	{
		tbody.appendChild(row);
	}
	
	if(oldVal!=null)
		blinkColors(td2, oldVal, ltp, 0, "#EFF4FB"); 
  }// JavaScript Document
