function Browser() {
  var ua, s, i;
  this.isIE    = false;
  this.isNS    = false;
  this.version = null;
  ua = navigator.userAgent;
  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.
  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}
var browser = new Browser();

//Global Variables
var XmlHttp,XmlHttpNews;

//Creating object of XMLHTTP For AJAX Method
function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;	
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}
/*
function CreateXmlHttp1()
{
	try
	{
		XmlHttp1 = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp1 = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp1 = null;	
		}
	}	
	if(!XmlHttp1 && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp1 = new XMLHttpRequest();
	}
}
*/
function CreateXmlHttpDate()
{
	try
	{
		XmlHttpDate = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpDate = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpDate = null;	
		}
	}	
	if(!XmlHttpDate && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpDate = new XMLHttpRequest();
	}
}
function CreateXmlHttpCon()
{	
	try
	{
		XmlHttpCon = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpCon = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpCon = null;	
		}
	}	
	if(!XmlHttpCon && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpCon = new XMLHttpRequest();
	}
}
function CreateXmlHttpNews()
{
	try
	{
		XmlHttpNews = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpNews = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpNews = null;	
		}
	}	
	if(!XmlHttpNews && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpNews = new XMLHttpRequest();
	}
}

//For Loading ticker from TickerData.aspx using AJAX
function getTickerData(Exchg)
{
	var NTab = document.getElementById("NTab");
	var MTab = document.getElementById("MTab");
	
	if (Exchg == "MCX")
	{
		NTab.className = 'topgainFont'
		MTab.className = 'NewsHeader'
	}
	else
	{
		NTab.className = 'NewsHeader'
		MTab.className = 'topgainFont'
	}
	
	CreateXmlHttp()
	document.body.style.cursor = "progress";
	document.getElementById("MarqueeId").innerHTML = " <img src='../images/loading1.gif'>";
	document.getElementById("MarqueeSpot").innerHTML = " <img src='../images/loading1.gif'>";
	
	var requestUrl = "../Ajax/TickerData.aspx?Exchg="+ Exchg +"&IpTrack="+ window.location.hostname +"&timeStamp="+new Date().getTime();
	
	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = function(){getDataResponse(Exchg);};
		XmlHttp.open("GET", requestUrl,  true);
		XmlHttp.send(null);
	}
}

//Called when response comes back from server Only For Ticker
function getDataResponse(Exchg)
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{
			var arrCol,Time,Symbol,Unit,Place,Value,strImg;
			var arrColF,FSymbol,ExpDate,FValue,strImgF;
			var SpotDet="",FutureDet="";
			var MarqueeId = document.getElementById("MarqueeId");
			var MarqueeSpot = document.getElementById("MarqueeSpot");
			var Mq_Data = XmlHttp.responseText;
			
			if(Mq_Data != "")
			var arrRow = Mq_Data.split("^");
			
			var arrSpot = arrRow[0].split("|");
			var arrFuture = arrRow[1].split("|");
			
			for(i=0; i<arrSpot.length-1; i++)
			{
				arrCol = arrSpot[i].split("~");
				Symbol = arrCol[0];
				if(Exchg == "MCX")
				{
					Unit = arrCol[1];
					Place = arrCol[2];
					Value = arrCol[3];
					strImg = arrCol[4];
				}
				else
				{
					Place = arrCol[1];
					Value = arrCol[2];
					strImg = arrCol[3];
				}
				
				if(Exchg == "MCX")
				{
					SpotDet = SpotDet + Symbol + "&nbsp;" + Unit + "&nbsp;" + Place + "&nbsp;" + Value + "&nbsp;" + strImg + "&nbsp;&nbsp;";
				}
				else
				{
					SpotDet = SpotDet + Symbol + "&nbsp;" + Place + "&nbsp;" + Value + "&nbsp;" + strImg + "&nbsp;&nbsp;";
				}
			}

			for(i=0; i<arrFuture.length-1; i++)
			{
				arrColF = arrFuture[i].split("~");
				FSymbol = arrColF[0];
				ExpDate = arrColF[1];
				FValue = arrColF[2];
				strImgF = arrColF[3];
				
				FutureDet = FutureDet + FSymbol + "&nbsp;" + ExpDate + "&nbsp;" + FValue + "&nbsp;" + strImgF + "&nbsp;&nbsp;";
			}				
			MarqueeId.innerHTML = "<marquee onmouseover='this.stop();' id='marq' onmouseout='this.start();' trueSpeed scrollAmount='1' scrollDelay='35' direction='left' loop='repeat' width='100%'>" + FutureDet + "</marquee>";
			MarqueeSpot.innerHTML = "<marquee onmouseover='this.stop();' id='marq' onmouseout='this.start();' trueSpeed scrollAmount='1' scrollDelay='35' direction='left' loop='repeat' width='100%'>" + SpotDet + "</marquee>";
			
			document.body.style.cursor = "auto";
			ExchngTimeClock(Exchg);
		}
	}
}
//**** Function ExchngTimeClock Is used to find out the market hours --- The ticker will automatically refresh only in market timings *****//			
function ExchngTimeClock(Exchg)
{
	var time = new Date()
	var hour = time.getHours()
	if (hour >= 10 && hour <= 20)
		clockTimeoutID = setTimeout("getTickerData('"+ Exchg +"')",600000);
}


//For Commodity ExpDate Using AJAX
function GetExpDate(Exchg,Symbol)
{
	CreateXmlHttpDate();
	document.body.style.cursor = "progress";
	var requestUrl = "../Ajax/ExpDateData.aspx?Exchg="+ Exchg +"&Symbol="+Symbol;	
	if(XmlHttpDate)
	{
		XmlHttpDate.onreadystatechange = function(){getDateResp()};
		XmlHttpDate.open("GET", requestUrl,  true);
		XmlHttpDate.send(null);
	}
}

//Called when response comes back from server Only For ExpDateData
function getDateResp()
{
	// To make sure receiving response data from server is completed
	if(XmlHttpDate.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpDate.status == 200)
		{			
			var ExpDate   = document.getElementById("ExpDate");
			var strData = XmlHttpDate.responseText

			if(strData != "") 
			{	
				var arrExpDate = strData.split("|");
				ExpDate.length = 0; 	
				for(i=0; i<arrExpDate.length-1; i++) 
				{	
					var strDate = arrExpDate[i];
					var arrExpDateCode = strDate.split("~");
					ExpDate.options[i] = new Option();
					ExpDate.options[i].value = arrExpDateCode[0];
					ExpDate.options[i].text = arrExpDateCode[1];
				}
			}
			else 
			{
					ExpDate.length = 0;
					ExpDate.options[0] = new Option(); 
					ExpDate.options[0].value = "";
					ExpDate.options[0].text = "No Exp Date";			
			}
			document.body.style.cursor = "auto";
		}
		else {
					ExpDate.length = 0;
					ExpDate.options[0] = new Option(); 
					ExpDate.options[0].value = "";
					ExpDate.options[0].text = "server is not ready";
					document.body.style.cursor = "auto";		
		}
	}
}

//Check Country Using AJAX
function ChkCountry()
{
	country = document.getElementById("DD_Country").value;
	CreateXmlHttpCon();
	
	var requestUrl = "../Ajax/ConData.aspx?Country="+ country;
    //alert(requestUrl);
	if(XmlHttpCon)
	{
		XmlHttpCon.onreadystatechange = function(){getConResp()};
		XmlHttpCon.open("GET", requestUrl,  true);
		XmlHttpCon.send(null);
	}
}

//Called when response comes back from server Only For Country
function getConResp()
{
	// To make sure receiving response data from server is completed
	if(XmlHttpCon.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpCon.status == 200)
		{
			var State = document.getElementById("DD_State");
			var strData = XmlHttpCon.responseText
			alert(strData);
			if(strData != "")
			{
				var arrState = strData.split("|");
				State.length = 0;
				
				for(i=0; i<arrState.length-1; i++) 
				{	
					var strState = arrState[i];
					var arrStateCode = strState.split("~");
					State.options[i] = new Option();
					State.options[i].value = arrStateCode[0];					
					State.options[i].text = arrStateCode[1];
				}				
			}
			else 
			{
				State.length = 0;
				State.options[0] = new Option(); 
				State.options[0].value = "Other";
				State.options[0].text = "Other";			
			}
		}
		else
		{
			State.length = 0;
			State.options[0] = new Option(); 
			State.options[0].value = "";
			State.options[0].text = "server is not ready";
		}
	}
}


function popUpChart()
{
 	var chartSRC = document.ImgChartTab.src;
 	var imgUrl;
	chartSRC = new String(chartSRC)
	ChartTyp = chartSRC.substr((chartSRC.length-16),4);
	if(ChartTyp=='Gold')
		imgUrl = "http://www.theFinancials.com/free/EX_Commodity_PreciousMetals.html";
	 else
		imgUrl = "http://www.theFinancials.com/free/EX_Commodity_PreciousMetals.html";	
		
	var newWin=window.open(imgUrl,'MyWindow','x=0,y=0,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=1,copyhistory=no,width=580,height=350,screenX=0,screenY=0,left=210,top=200');
	newWin.focus();		
}
 
 
 // To change the  Tab value of Loser & Gainers  of Marketstat Control by default Gainers will be inline
 function ShownHide(val)
 {
	if(val == "")
	{
		val = "G";
	}
	
	var MCXGTable = document.getElementById("MCXGTable");
	var MCXLTable = document.getElementById("MCXLTable");
	var NCDEXGTable = document.getElementById("NCDEXGTable");
	var NCDEXLTable = document.getElementById("NCDEXLTable");
	var TabG = document.getElementById("TabG");
	var TabL = document.getElementById("TabL");
	
	MCXGTable.style.display = 'none'
	MCXLTable.style.display='none'
	NCDEXGTable.style.display='none'
	NCDEXLTable.style.display='none'
	
	if (val == "G")
	{
		MCXGTable.style.display = 'inline'
		NCDEXGTable.style.display = 'inline'
		TabG.className = 'Headertxt'
		TabL.className = 'Headertxt1'
	}
	else
	{
		MCXLTable.style.display = 'inline'
		NCDEXLTable.style.display = 'inline'
		TabG.className = 'Headertxt1'
		TabL.className = 'Headertxt'
	}
 }
 
 function ChartTab(opt)
 {
	var opt;
	var GoldSpotId = document.getElementById("GoldSpotPrice");
	var SilverSpotId  =  document.getElementById("SilverSpotPrice");
	var GoldImg = document.getElementById("GoldImg");
	var SilverImg = document.getElementById("SilverImg");
	
	if(opt=='G')
	{
		GoldImg.src="../images/GoldOn.gif"
		SilverImg.src="../images/SilverOff.gif"
		GoldImg.className = "CursorDefault";
		SilverImg.className = "CursorHand";
		document.ImgChartTab.src = "http://cmlinks.com/pub/chart/gold_chart.gif";
		GoldSpotId.style.display = "inline";
		SilverSpotId.style.display = "none";
	}	
	else if(opt='S')
	{
		GoldImg.src="../images/GoldOff.gif"
		SilverImg.src="../images/SilverOn.gif"
		GoldImg.className = "CursorHand";
		SilverImg.className = "CursorDefault";
		document.ImgChartTab.src = "http://cmlinks.com/pub/chart/silver_chart.gif";
		GoldSpotId.style.display = "none";
		SilverSpotId.style.display = "inline";
	}
 }
 
 function fillSymbol(Exchg)
{
	var arrSymbol;
	var Symbol = document.getElementById("Symbol");
	cnt=1;
	if(Exchg!=''){
		if(Exchg=='MCX'){
			arrSymbol     = arrM_Symbol;
			arrSymbolText = arrM_CommName;
		}
		else if(Exchg=='NCDEX'){
			arrSymbol	  = arrN_Symbol;
			arrSymbolText = arrN_CommName;
		}
		Symbol.length = 0;
		Symbol.options[0] = new Option();
		Symbol.options[0].value = "";
		Symbol.options[0].text = "Select Symbol";
		for(var i=0; i < arrSymbol.length; i++)
		{
				Symbol.options[cnt] = new Option(); 
				Symbol.options[cnt].value = arrSymbol[i];
				Symbol.options[cnt].text = arrSymbolText[i];
				cnt = cnt + 1;
		}
	 }	
  } 
function CommCharts()
		{
		var CommQuotesTbl = document.getElementById("CommQuotesTbl");
		var Exchg   = document.getElementById("Exchg").value;
		var Symbol   = document.getElementById("Symbol").value;
		var ExpDate   = document.getElementById("ExpDate").value;
		var Type   = document.getElementById("DD_CType").value;
		var Period   = document.getElementById("DD_Period").value;
		
		//var StrUrl = "Review.aspx?mno=4&index=5&id=2&Exchg="+Exchg+"&Symbol="+Symbol+"&ExpDate="+ExpDate+"&Type="+Type+"&Period="+Period
		var StrUrl = "Review.aspx?mno=4&index=5&id=2&Exchg="+Exchg+"&Symbol="+Symbol+"&ExpDate="+ExpDate+"&Type="+Type+"&Period="+Period+"&pgOpt=1"
		
		if(Exchg=="")
			alert("Please Select Exchange..!");
		else if(Symbol=="")
			alert("Please Select a Symbol..!");
		else if(ExpDate=="")
			alert("Please Select Expiry Date..!");
		else if(Type=="")
			alert("Please Select a Chart Type..!");
		else if(Period=="")
			alert("Please Select Period..!");
		else
		{
			window.location = StrUrl;
		}
	}

function getCommQuotes()
{	
	var Exchg   = document.getElementById("Exchg").value;
	var Symbol   = document.getElementById("Symbol").value;
	var ExpDate   = document.getElementById("ExpDate").value;
		
	//var StrUrl = "Review.aspx?mno=4&index=4&id=1&Exchg="+ Exchg +"&Symbol="+ Symbol +"&ExpDate="+ ExpDate
	var StrUrl = "../Commodity/Review.aspx?id=1&pgOpt=1&Exchg="+ Exchg +"&Symbol="+ Symbol +"&ExpDate="+ ExpDate + ""

	if(Exchg=="" || Exchg == "Select Exchange")
	{
		alert("Please Select Exchange");
		return false;
	}
	else if(Symbol=="")
    {
     	alert("Please Select Symbol");
     	return false;
     }
	else if(ExpDate=="")
	{
	 alert("Please Select Expiry Date");
	 return false;
	}
	else
	{
		window.location = StrUrl;
	}
}

function ChangeCommNews(CtlName)
{	
	var currentDivObj = document.getElementById("Td_Data");
	
	document.getElementById("Td_CurrData1").style.display = 'none';
	currentDivObj.innerHTML = "<table cellspacing=0 cellpadding=0 width='100%' align=center ><tr><td><img src='../images/loading1.gif'></td></tr></table>";
	document.body.style.cursor = "progress";
	CreateXmlHttpNews();
	
	var requestUrl = "../Ajax/Newsdata.aspx?CtlName="+CtlName;
	//alert(requestUrl);
	if (XmlHttpNews)
	{
		XmlHttpNews.onreadystatechange = function(){ChangeNewsData(CtlName)};
		XmlHttpNews.open("GET",requestUrl,true);
		XmlHttpNews.send(null);
	}
}

function ChangeNewsData(CtlName)
{
	if (XmlHttpNews.readyState == 4)
	{
		var td_id = document.getElementById("Td_Data");
		if (XmlHttpNews.status == 200)
		{		
			td_id.innerHTML = XmlHttpNews.responseText;
			document.body.style.cursor = "auto";
		}
		else
		{
			td_id.innerHTML = "There was a problem retrieving data from the server.";
			document.body.style.cursor = "auto";
		}
	}
		NewsTimeClock(CtlName);
}

function NewsTimeClock(CtlName)
{	
	var time = new Date()
	var hour = time.getHours()	
	if (hour >= 10 && hour <= 20)
		clockTimeoutID = setTimeout("ChangeCommNews('"+ CtlName +"')",600000);
}


