
function queryArgsFromURL(strURL)
{
	var args = {};
	
	var a1 = strURL.split("?");
	
	if (a1.length < 2)
		return args;
		
	var a2 = a1[1].split("&");
	for(var i=0; i < a2.length; i++)
	{
		var a3 = a2[i].split("=");
		
		var strName	= a3[0].toLowerCase();
		
		var strVal = a3.length > 1 ? a3[1] : "";
			
		args[strName] = strVal;
	}
	
	return args;
}

function queryURLFromArgs(args)
{
	var str = "?";
	
	for(var a in args)
	{
		if (str.length > 1)
			str += "&";
			
		str += a + "=" + args[a];
	}
	
	return str;
}

function fixupNavLinks(id)
{
	// Find our query params
	var qryTransfer = { "code" : true, "ctry" : true, "inforeq" : true };
	var qryArgs = {};
	
	var qryArgs = queryArgsFromURL(window.location.href);
	
	var el = document.getElementById(id);
	
	fixupNavLinksEl(el, qryArgs);
}

function fixupNavLinksEl(el, qryArgs)
{
	if (el.nodeName.toUpperCase() == 'A')
	{
		var args = queryArgsFromURL(el.href);

		// Merge, let current override query
		for(var a in qryArgs)
		{
			if (args[a] == undefined)
				args[a] = qryArgs[a];
		}
		
		el.href = el.href.split("?")[0] + queryURLFromArgs(args);
	}
	
	for(var i=0; i < el.childNodes.length; i++)
	{
		fixupNavLinksEl(el.childNodes[i], qryArgs);
	}
}


/**********************************************************************
 * Tabbed Box script
 *
 * Usage:
 *
 * create a tabbed box as follows:
 *
 * var ts = new TabbedBox(parent, tabs, initial);
 *
 * parent	= a page element that will be the container for the tabbed box
 * tabs		= a JS array where each item is an object with the properties below
 * initial	= id of the initial tab to have selected
 *
 * Each tab object has the following properties:
 *
 * caption		: text to display on the tab
 * id			: an identifer for the tab
 * docId		: (optional) The identifier within the document of an element to use
 *                for the content of the tab.  When the tab is selected
 *                this element will be removed from its current location
 *                and added to the tabbed box content area.
 * fncRender	: (optional) A function that will be called to render the contents
 *                of the tab.  The container div will be passed as an argument.
 * fncInit		: (optional) A function that will be called after the tab content
 *                has been rendered for the first time.
 * fncClick		: (optional) A function that will be called whenever the tab is clicked
 *
 * For example, the follow code will create a tabbed box with the two tabs labelled
 * "Shares" and "Managed funds".  Each tab specifies the id of a document element
 * to use as the tab content and a function to call to initialize each tab.  The
 * tabbed box will be a child of the "container" element and the first tab shown
 * will be the "Shares" tab.
 *
 * var tabs =
 * [
 *		{
 *			caption	: "Shares",
 *			id		: "s",
 *			docId	: "SharesTab",
 *			fncInit	: function() { qs_shares_loadInfoRequest(); }
 *		},
 *		{
 *			caption	: "Managed funds",
 *			id		: "mf",
 *			docId	: "ManagedTab",
 *			fncInit	: function() { qs_managed_loadInfoRequest(); }
 *		}
 * ];
 *
 * var tb = new TabbedBox(document.getElementById("container"), tabs, "s");	
 *
 */

function createThisCallback(obj,strFunc)
{
	var temp=obj;var args=[];
	for (var i=2;i<arguments.length;i++)
		args.push(arguments[i]);
	return function ()
	{
		for (var i=0;i<arguments.length;i++)
			args.push(arguments[i]);
		if (temp[strFunc])
			return temp[strFunc].apply(obj,args);
	}
}

function TabbedBox(parent, tabs, initial)
{
	this.tabs		= tabs;
	this.tabdiv		= [];
	this.tabimg		= [];
	this.content	= new Array(tabs.length);
	this.iSel		= -1;
	
	this.getSelectedTabIndex = function()
	{
		return this.iSel;
	}
	
	this.handleTabClick = function(iTab)
	{
		if (this.iSel == iTab)
			return;
		// Turn off current sel
		if (this.iSel != -1)
		{
			this.tabdiv[this.iSel].className = "tb_nonsel";
			this.tabimg[this.iSel].src = "/img/tabs_crnr.gif";
			
			if (this.content[this.iSel])
			{
				this.content[this.iSel].style.display = "none";
				//this.content[this.iSel].style.visibility = "hidden";
			}
		}
		
		// Turn on new
		this.tabdiv[iTab].className = "tb_sel";
		this.tabimg[iTab].src = "/img/tabs_current_crnr.gif";
		
		this.iSel = iTab;
		
		this.updateTabContent();
	}
	
	this.updateTabContent = function()
	{
		if (!this.content[this.iSel])
		{
			this.content[this.iSel] = this.tdContent.appendChild(document.createElement("div"));
			
			// If docId then move it
			if (this.tabs[this.iSel].docId)
			{
				var e = document.getElementById(this.tabs[this.iSel].docId);
				
				e.parentNode.removeChild(e);
				
				this.content[this.iSel].appendChild(e);
				
				// Make sure it is visible
				e.style.display = "block";
				e.style.visibility = "visible";
			}
			
			// render if specified
			if (this.tabs[this.iSel].fncRender)
				this.tabs[this.iSel].fncRender(this.content[this.iSel]);
				
			// Init if specified
			if (this.tabs[this.iSel].fncInit)
				this.tabs[this.iSel].fncInit();
		}
			
		this.content[this.iSel].style.display = "block";
		this.content[this.iSel].style.visibility = "visible";
		
		if (this.tabs[this.iSel].fncClick)
			this.tabs[this.iSel].fncClick();
	}
	
	var table = parent.appendChild(document.createElement("table"));
	table.cellPadding = "0";
	table.cellSpacing = "0";
	
	var tbody = table.appendChild(document.createElement("tbody"));
	
	var tr = tbody.appendChild(document.createElement("tr"));	
	
	// Add each tab
	var iCol = 0;
	var iPercent = 0;
	for(var i=0; i < tabs.length; i++)
	{
		if (i > 0)
		{
			// add 1px spacer
			var td = tr.appendChild(document.createElement("td"));
			td.valign="top";
			td.width = "1%";
			var div = td.appendChild(document.createElement("div"));
			div.className = "tb_tabspc";
			div.innerHTML = "&nbsp;";
			iCol++;
			
			iPercent += 1;
		}
		
		var fSel	= tabs[i].id == initial;
		
		if (fSel)
			this.iSel = i;
			
		var td = tr.appendChild(document.createElement("td"));
		td.width = "10%";
		
		td.className = "tb_tabtd";
		
		var divTemp = td.appendChild(document.createElement("div"));
		divTemp.style.position = "relative";
		
		var div;
		
		if (tabs[i].tabId)
		{
			div = document.getElementById(this.tabs[i].tabId);
			
			div.parentNode.removeChild(div);
			div.style.display="block";
			divTemp.appendChild(div);
		}
		else
		{
			div	= divTemp.appendChild(document.createElement("div"));
			div.innerHTML	= tabs[i].caption;
		}
		
		div.className	= fSel ? "tb_sel" : "tb_nonsel";
		
		this.tabdiv.push(div);
		
		div.onclick = createThisCallback(this, "handleTabClick", i);
			
		var img = divTemp.appendChild(document.createElement("img"));
			
		img.src = fSel ? "/img/tabs_current_crnr.gif" : "/img/tabs_crnr.gif";
		img.className = i == 0 ? "tb_img_first" : (i == (tabs.length-1) ? "tb_img_last" : "tb_img_mid");
		
		this.tabimg.push(img);
		
		iPercent += 10;
		iCol++;
	}
	
	// Add blank space area
	var td = tr.appendChild(document.createElement("td"));
	td.valign="top";
	td.width = (100 - iPercent) + "%";
	var div = td.appendChild(document.createElement("div"));
	div.className = "tb_blank";
	
	iCol++;
	
	var tr = tbody.appendChild(document.createElement("tr"));
	
	var td = tr.appendChild(document.createElement("td"));
	
	td.className = "tb_content";
	td.colSpan = iCol;
	
	this.tdContent = td;
	
	this.updateTabContent();
}


/**********************************************************************
 * Lookup
 */
var lookupParams = 
{
	"quotesearchMF" : 
	{
		textinputid		: "qs_managed_code",
		ctryid			: "qs_managed_ctry",
		inforeqid		: "qs_managed_inforeq",
		searchbtnid		: "qs_managed_btn",
		urltest			: "http://moneyv2.syd.ninemsn.com.au/lookup/default.aspx?feedid=0&code={code}&ctry={ctry}",
		urltestxsl		: "http://moneyv2.syd.ninemsn.com.au/funds/xsl/quote_useXSL.xsl",
		urllookup		: "http://moneyv2.syd.ninemsn.com.au/lookup/default.aspx?feedid=1&code={code}&page={page}",
		urllookupxsl	: "http://moneyv2.syd.ninemsn.com.au/funds/xsl/FundLookup.xsl",
		urlgood			: "/funds/quote.aspx?code={code}&ctry={ctry}",
		resultspaged	: true,
		testneedsprefix	: true,
		boxheight		: 200
	},
	"quotesearchSH" : 
	{
		textinputid		: "qs_shares_code",
		ctryid			: "qs_shares_ctry",
		inforeqid		: "qs_shares_inforeq",
		searchbtnid		: "qs_shares_btn",
		urltest			: "http://moneyv2.sbkdev.ninemsn.com.au/lookup/default.aspx?feedid=2&code={code}&ctry={ctry}",
		urltestxsl		: "http://moneyv2.sbkdev.ninemsn.com.au/stylesheet/marketdata/quotecheck.xsl",
		urllookup		: "http://moneyv2.sbkdev.ninemsn.com.au/lookup/default.aspx?feedid=3&code={code}&page={page}",
		urllookupxsl	: "http://moneyv2.sbkdev.ninemsn.com.au/stylesheet/marketdata/sharelookup.xsl",
		urlgood			: "/shares-and-funds/research-a-company/results.aspx?code={code}&ctry={ctry}&inforeq={inforeq}",
		resultspaged	: true,
		failuredoc		: "<?xml version=\"1.0\"?><sresults name=\"{code}\"/>",
		boxheight		: 100
	}
};

var iLookupPage = 0;
var idLookup;
var divLookupBox;
var strLookupCode;

function doLookup(id)
{
	iLookupPage = 0;
	idLookup = id;
	
	var param = lookupParams[id];
	
	var ef	= document.getElementById(param.textinputid);
	var btn	= document.getElementById(param.searchbtnid);
	
	strLookupCode	= ef.value.toUpperCase();
	ef.value = strLookupCode;
	
	var strCtry			= document.getElementById(param.ctryid).value;
	var strInfoReq		= document.getElementById(param.inforeqid).value;
	
	// If bypass go straight to page
	if (param.bypasslookup)
	{
		window.location.href = formatURL(param.urlgood, strLookupCode, strCtry);
		return;
	}
	
	var iPage = 0;
	
	var posEF	= calcAbsolutePos(ef);
	var posBTN	= calcAbsolutePos(btn);
	
	x1 = posEF.x - 1;
	y1 = posEF.y - 1;
	x2 = posEF.x + posEF.width + 2;
	y2 = posEF.y + posEF.height + 1;
	
	/*
	var x1 = Math.min(posEF.x, posBTN.x) - 5;
	var y1 = Math.min(posEF.y, posBTN.y) - 5;
	
	var x2 = Math.max(posEF.x+posEF.width, posBTN.x+posBTN.width) + 5;
	var y2 = Math.max(posEF.y+posEF.height, posBTN.y+posBTN.height) + 5;
	*/
	
	if (!divLookupBox)
		divLookupBox = document.body.appendChild(document.createElement("div"));
	
	divLookupBox.className = "loadingBox";
	
	divLookupBox.innerHTML = "Searching for: " + strLookupCode;
	
	setElementPos(divLookupBox, x1, y1, x2-x1, y2-y1);
	btn.disabled = true;
	
	function handleTest(str)
	{
		if (!str)
		{
			alert("Problem retrieving data, please retry.");
			hideLookup();
			btn.disabled = false;
			return;
		}
		
		function handleTestTr(strXML)
		{
			if (strXML.indexOf("__lookup__") == -1)
			{
				// Good code
				window.location.href = formatURL(param.urlgood, strLookupCode, strCtry, strInfoReq);
			}
			else
			{
				// Bad, do lookup
				var strURL = formatURL(param.urllookup, strLookupCode, strCtry);
				loadDoc(strURL, handleLookup);
			}
		}
		
		if (param.testneedsprefix)
			str = "<?xml version=\"1.0\" ?>" + str;
			
		transformDoc(str, param.urltestxsl, handleTestTr);
	}	

	var strURL = formatURL(param.urltest, strLookupCode, strCtry);
	
	loadDoc(strURL, handleTest);
}

function handleLookup(str)
{
	
	var param = lookupParams[idLookup];
	
	// Hack, sometimes lookup does not return valid xml
	doc = createDoc(str);
	
	if (doc.childNodes.length == 0 && param.failuredoc)
	{
		str = param.failuredoc.replace("{code}", strLookupCode);
	}
	
	function handleLookupTr(strNew)
	{
		var ef	= document.getElementById(param.textinputid);
		
		var posEF	= calcAbsolutePos(ef);
		
		var iHeight = strNew.indexOf("Sorry") == -1 ? param.boxheight : 60;
		
		setElementPos(divLookupBox, posEF.x+1, posEF.y + posEF.height+1, 350, iHeight);
		
		divLookupBox.className = "lookupBox";
		
		divLookupBox.innerHTML = strNew;
		
		var btn	= document.getElementById(param.searchbtnid);
		
		btn.disabled = false;
	}
	
	transformDoc(str, param.urllookupxsl, handleLookupTr);
}



function SetLookupPageNo(iOffset)
{
	iLookupPage += iOffset;
	
	var param = lookupParams[idLookup];

	divLookupBox.className = "loadingBox";
	
	divLookupBox.innerHTML = "Searching for: " + strLookupCode;
	
	loadDoc(formatURL(param.urllookup, strLookupCode, "AUS"), handleLookup);
}

function DoLookupCode(strCode)
{
	var param = lookupParams[idLookup];
	
	var ef	= document.getElementById(param.textinputid);
	
	ef.value = strCode;
	
	hideLookup();
}

function CloseLookup()
{
	hideLookup();
}

function hideLookup()
{
	if (divLookupBox)
		divLookupBox.style.display = "none";
		
	if (ifrOverlay)
		ifrOverlay.style.display = "none";
}

function formatURL(strURL, strCode, strCtry, strInfoReq, iPage)
{
	strURL = strURL.replace("{code}", strCode ? strCode : "");
	strURL = strURL.replace("{ctry}", strCtry ? strCtry : "");
	strURL = strURL.replace("{inforeq}", strInfoReq ? strInfoReq : "");
	strURL = strURL.replace("{page}", iLookupPage);
	
	return strURL;
}


/**********************************************************************
 * Glossary
 */

/**
 * Displays a Glossary term from the term given
 */
function glossaryTerm(term) {
  filename = "/af/glossary?term="+escape(term);
  window.open(filename, "Glossary", "width=450,height=200,resizable=no,scrollbars=yes");
}

/**
 * Displays a Glossary term from the id given
 */
function glossaryId(id) {
  filename = "/investor/shares/glossary.asp?glossaryId="+id;
  myRemote = window.open(filename, "Glossary", "width=450,height=200,resizable=no,scrollbars=yes");
  myRemote.focus();
}

/* Identifies if the pop-up is for Glossary or Info */

function glossaryId_info(id, glossary) {
  if (glossary == "glossary")
  {
	glossaryId(id);
  }
  else
  {
 	filename = "/investor/shares/glossary.asp?glossaryId="+id+"&glossary="+glossary;
	window.open(filename, "Glossary", "width=450,height=200,resizable=no,scrollbars=yes");
  }
  
}

