var sControl;
var objXmlHttp;
var sSec;
var sControl1;
var objXmlHttp1;

///The below function is used to get dynamic data based on the Market Section that the user has clicked.
function getMarketContent(sSection, sCtrl)
{
    sSec = "";
    sSec = sSection;
    sUrl = "";
    sControl = sCtrl;
    if(sSection=="MktMon")
        sUrl = "/mktmon/indmonhome.html";
    else if(sSection=="NSE")
        sUrl = "/nse50.asp";
    else if(sSection=="BSE")
        sUrl = "/bse30.asp";
    else if(sSection=="MORE")
        sUrl = "/home_more.asp";
    
    document.getElementById(sControl).innerHTML = "<table cellspacing='0' cellpadding='0' border='0' width='100%' height='165px'>"+
                                                  " <tr style='height: 165px'>"+
                                                  "     <td align='center' valign='middle'><img src='/images/loading.gif' alt='Loading...' /></td>"+
                                                  " </tr>"+
                                                  "</table>";
    
    if(sUrl != "")
    {
        objXmlHttp = null;
        objXmlHttp = GetXmlHttpObject(stateChanged);
        objXmlHttp.open("GET", sUrl , true);
	    objXmlHttp.send(null);
	    //objXmlHttp.get();
    }
    else
    {
        document.getElementById(sControl).innerHTML = "";
    }
}//end of function getMarketContent(section)

///Function called when asynchronous response received.
function stateChanged()
{
    if (objXmlHttp.readyState==4 || objXmlHttp.readyState=="complete")
    {
	    var sData = objXmlHttp.responseText;
        document.getElementById(sControl).innerHTML = sData;
        if(sSec=="MORE")
        {
            GetIndices();
            CommPrices();
        }
        /*else if(sSec=="BSE")
        {
            if(currbtstamp.replace(' ', '') == "")
                document.getElementById('spnBSE30').innerHTML = showDecimals(formatCom(currBse), 1) + "&nbsp;(" + showDecimals(formatValue(currBse-prevBse, 2), 1) + " pts)";
        }
        else if(sSec=="NSE")
        {
            if(currntstamp.replace(' ', '') == "")
                document.getElementById('spnNSE50').innerHTML = showDecimals(formatCom(currNse), 1) + "&nbsp;(" + showDecimals(formatValue(currNse-prevNse, 2), 1) + " pts)";
        }*/
        /*var sData = objXmlHttp.responseText;
        var objSpan = document.createElement("span");
        objSpan.innerHTML = sData;
        document.getElementById(sControl).appendChild(objSpan);*/
	    //document.getElementById(sControl).innerHTML=objXmlHttp.responseText;
	}
}

function showDecimals(nValue, nDecimals)
{
    var sDec = nValue.toString().split('.');
    var sRetVal = nValue.toString();
    if(sDec.length==1)
    {
        sRetVal = sRetVal + ".";
        for(i=0; i < nDecimals; i++)
            sRetVal = sRetVal + "0";
    }
    else
    {
        if(sDec[1].length > nDecimals)
        {
            sRetVal = Math.round(nValue, nDecimals);
        }
        else if(sDec[1].length < nDecimals)
        {
            for(i=sDec[1].length; i < nDecimals; i++)
                sRetVal = sRetVal + "0";
        }    
    }
    
    return sRetVal;
}

///initailises the Xml Http object as per the user's browser
function GetXmlHttpObject(handler)
{
	var objXHttp=null;

	/*if (navigator.userAgent.indexOf("Opera")>=0)
	{
	    alert("This example doesn't work in Opera");
	    return;
	}*/
	
	if (navigator.userAgent.indexOf("MSIE")>=0 || navigator.userAgent.indexOf("Opera")>=0)
	{
	    if (window.XMLHttpRequest)
        {
            // code for all new browsers
            objXHttp=new XMLHttpRequest();
            if(handler!=null)
                objXHttp.onreadystatechange=handler;
            return objXHttp;
        }
        else if (window.ActiveXObject)
        {
            // code for IE5 and IE6
            objXHttp=new ActiveXObject("Microsoft.XMLHTTP");
            if(handler!=null)
                objXHttp.onreadystatechange=handler;
            return objXHttp;
        }

        if(objXHttp==null)
        {
	        var strName="Msxml2.XMLHTTP";
	        if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
	        {
	            strName="Microsoft.XMLHTTP";
	        }
	        try
	        {
	            objXHttp=new ActiveXObject(strName);
	            if(handler!=null)
                    objXHttp.onreadystatechange=handler;
	            return objXHttp;
	        }
	        catch(e)
	        {
	            alert("Error. Scripting for ActiveX might be disabled");
	            return;
	        }
	    }
	}
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
	    objXHttp=new XMLHttpRequest();
	    if(handler!=null)
	    {
            objXHttp.onload=handler;
	        objXHttp.onerror=handler;
	    }
	    return objXHttp;
	}
}//end of function GetXmlHttpObject(handler)

function getMarketContentNew(sSection, sCtrl)
{
    sSec = "";
    sSec = sSection;
    sUrl = "";
    sControl = sCtrl;
    if(sSection=="MktMon")
        sUrl = "/mktmon/indmonhome_new.html";
    else if(sSection=="NSE")
        sUrl = "/nse50_new.asp";
    else if(sSection=="BSE")
        sUrl = "/bse30_new.asp";
    else if(sSection=="MORE")
        sUrl = "/home_more_new.asp";
    
    document.getElementById(sControl).innerHTML = "<table cellspacing='0' cellpadding='0' border='0' width='100%' height='165px'>"+
                                                  " <tr style='height: 165px'>"+
                                                  "     <td align='center' valign='middle'><img src='/images/loading.gif' alt='Loading...' /></td>"+
                                                  " </tr>"+
                                                  "</table>";
    
    if(sUrl != "")
    {
        objXmlHttp = null;
        objXmlHttp = GetXmlHttpObject(stateChanged);
        objXmlHttp.open("GET", sUrl , true);
	    objXmlHttp.send(null);
	    //objXmlHttp.get();
    }
    else
    {
        document.getElementById(sControl).innerHTML = "";
    }
}//end of function getMarketContentNew(section)


function getPopularStories(sCtrl)
{
	objXmlHttp1 = null;
	sControl1 = sCtrl;
	var sUrl1 = "/popular_new.asp";
        objXmlHttp1 = GetXmlHttpObject(stateChanged1);
        objXmlHttp1.open("GET", sUrl1 , true);
	objXmlHttp1.send(null);
	//var sData = objXmlHttp1.responseText;
        //document.getElementById(sControl).innerHTML = sData;
}//end of function getPopularStories()

///Function called when asynchronous response received.
function stateChanged1()
{
    if (objXmlHttp1.readyState==4 || objXmlHttp1.readyState=="complete")
    {
	var sData = objXmlHttp1.responseText;
        document.getElementById(sControl1).innerHTML = sData;
    }
}