// Image functions
function MM_preloadImages()
{
	var d = document;
	if (d.images)
	{
		if (!d.MM_p)
			d.MM_p = new Array();
    
		var i, j = d.MM_p.length, a = MM_preloadImages.arguments;
		for (i = 0; i < a.length; i++)
		{
			if (a[i].indexOf("#") != 0)
			{
				d.MM_p[j] = new Image;
				d.MM_p[j++].src = a[i];
			}
		}
	}
}

function MM_swapImage()
{
	var i, j = 0, x, a = MM_swapImage.arguments;
	document.MM_sr = new Array;
	for (i = 0; i < (a.length-2); i += 3)
	{
		if ( (x=MM_findObj(a[i])) != null)
		{
			document.MM_sr[j++] = x;
			if (!x.oSrc)
				x.oSrc = x.src;
			x.src = a[i+2];
		}
	}
}

function MM_swapImgRestore()
{
	var i, x, a = document.MM_sr;
	for (i = 0; a && i<a.length && (x=a[i]) && x.oSrc; i++)
		x.src = x.oSrc;
}

function MM_findObj(n, d)
{
	var p, i, x;
	if (!d)
		d = document;
	if ( (p=n.indexOf("?")) > 0 && parent.frames.length)
	{
		d = parent.frames[n.substring(p+1)].document;
		n = n.substring(0, p);
	}
	if (!(x=d[n]) && d.all)
		x = d.all[n];

	for (i = 0; !x && i<d.forms.length; i++)
		x = d.forms[i][n];

	for (i = 0; !x && d.layers && i<d.layers.length; i++)
		x = MM_findObj(n, d.layers[i].document);
	
	return x;
}

var winArray = new Array();
window.onunload = function()
{
	for (var i = 0; i < winArray.length; i++)
		winArray[i].close();
};

// Print Report
function OpenPrintReport(server, type, countryid, symbol)
{
	var url = server + "/" + type + "/PrintReport.aspx?Country=" + countryid + "&Symbol=" + symbol;
	var win = window.open(url, 'PrintReport', 'width=500,height=255,top=30,left=30,menubar=yes,toolbar=yes,scrollbars=yes,resizable=yes');
	winArray.push(win);
	win.focus();
}

// Open a feekback window
function openFeedback(url)
{
	var win = window.open(url, 'Feedback', 'width=750,height=600,top=30,left=30,toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes');
	winArray.push(win);
    win.focus();
}

// Show ComStock Terms and Conditions.
function OpenwinSPTermsAndConditions()
{
	var url = "/SPTermsAndConditions.html";
	var win = window.open(url, 'ComStockTerm', 'width=450,height=440,top=30,left=30,resizable=yes');
	winArray.push(win);
    win.focus();
}

// Show BATS Terms and Conditions.
function OpenBats()
{
	var url = "/Bats.html";
	var win = window.open(url, 'BatsTerm', 'width=450,height=320,top=30,left=30,resizable=yes');
	winArray.push(win);
    win.focus();
}

// Open definition page.
function OpenHelp(url)
{
	var win = window.open(url, "DataDefinitions", "width=466,height=300,top=30,left=30,scrollbars=yes,resizable=yes");
	winArray.push(win);
	win.focus();
}

// Premium data point definitions
function OpenDataDefs(server, url)
{
	var win = window.open(server+url, 'DataDefinitions', 'width=471,height=300,top=30,left=30,scrollbars=yes,resizable=yes');
	winArray.push(win);
	win.focus();
}

// Open News link
function OpenNews(url)
{
	var win;
	if (url.indexOf("/news/tpx/extnews.asp?rdurl=") > 0)
	{
		win = window.open(url, 'TopixNews', 'toolbar=yes,titlebar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=750,height=650');
		winArray.push(win);
		win.focus();
	}
	else if (url.indexOf("globaldocuments.morningstar.com/documentlibrary/") > 0) {
		win = window.open(url, '', 'toolbar=yes,titlebar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes');
		winArray.push(win);
		win.focus();
	}
	else if (url.indexOf(".morningstar.com/") > 0)
	{
		document.location = url;
	}
	else
	{
		win = window.open(url, 'News', 'toolbar=yes,titlebar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=750,height=650');
		winArray.push(win);
		win.focus();
	}
}

// Open window
function openWin(url)
{
	var win = window.open(url, 'newWindow', 'width=750,height=600,top=30,left=30,toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes');
	winArray.push(win);
    win.focus();
}

// Advertisement
var topFrame, midFrame, botFrame;

function LoadFrame()
{
	var adObj = document.getElementById("Ad");

	topFrame = document.createElement("iframe");
	topFrame.id = "hiddenFrameTop";
	topFrame.name = "hiddenFrameTop";
	topFrame.width = 0;
	topFrame.height = 0;
	topFrame.frameborder = 0;
	topFrame.src = "/Ad/Ads.html?Ticker=" + Ticker + "&ads=" + AdsTop + "&site=" + AdsSite;
	adObj.appendChild(topFrame);

	midFrame = document.createElement("iframe");
	midFrame.id = "hiddenFrameMid";
	midFrame.name = "hiddenFrameMid";
	midFrame.width = 0;
	midFrame.height = 0;
	midFrame.frameborder = 0;
	midFrame.src = "/Ad/Ads.html?Ticker=" + Ticker + "&ads=" + AdsMiddle + "&site=" + AdsSite;
	adObj.appendChild(midFrame);

	botFrame = document.createElement("iframe");
	botFrame.id = "hiddenFrameBot";
	botFrame.name = "hiddenFrameBot";
	botFrame.width = 0;
	botFrame.height = 0;
	botFrame.frameborder = 0;
	botFrame.src = "/Ad/Ads.html?Ticker=" + Ticker + "&ads=" + AdsBottom + "&site=" + AdsSite;
	adObj.appendChild(botFrame);
}

function RemoveFrame()
{
	var adObj = document.getElementById("Ad");
	adObj.removeChild(topFrame);
	adObj.removeChild(midFrame);
	adObj.removeChild(botFrame);
}

function displayAd(adid, text)
{
	var id = document.getElementById(adid);
	if (id == null)
		return;
	if (adid == "AdFrame2")
	{
		if (text.indexOf("empty.gif") > 0)
		{
			AdsCount--;
			return;
		}
		var space = "<br><img src='http://im.morningstar.com/im/dot_clear.gif' height=10>";
		text += space;

		// If there is a overlay, we need to adjust the overlay position
		AdjustOverlayPos();
	}
	id.innerHTML = text;

	AdsCount--;
	if (AdsCount <= 0)
		RemoveFrame();
}

function AdjustOverlayPos()
{
	var id = document.getElementById("VisitorOverlay");
	if (id == null)
		return;
	id.style.top = id.offsetTop + 60;
}

