var fMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument != 'undefined');
var fSafari = navigator.userAgent.toLowerCase().indexOf("safari") != -1;
var fIE = !fMozilla && !fSafari; // default to IE
var ifrOverlay;
var blnEnableV21 = false;  // version 2.1 money has a few differences

// Disable field until body is loaded
function enableAfterLoad(id) {
  var el = document.getElementById(id);
  if (el) {
    el.disabled = true;

    function enableItem() {
      el.disabled = false;
    }

    if (fIE)
      window.attachEvent("onload", enableItem);
    else
      window.addEventListener("load", enableItem, false);
  }
}

// Following are querystring params that are transferred to nav links
var g_qryFixupTransfer =
	{
	  "code": true,
	  "ctry": false,
	  "inforeq": true,
	  "mode": true
	};

function queryArgsFromURL(strURL, fAll) {
  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();

    if (fAll || g_qryFixupTransfer[strName]) {
      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 URLIsFunds(url) {
  return url.indexOf("/funds/") != -1;
}

function URLIsStocks(url) {
  return url.indexOf("/research-a-company/") != -1 || url.indexOf("/shares-and-funds/charts") != -1 || url.indexOf("/shares-and-funds/quotes") != -1;
}

function fixupNavLinks(id) {
  // Only fixup when on a stocks/funds page
  var fStocks = URLIsStocks(window.location.href);
  var fFunds = URLIsFunds(window.location.href);

  if (!fFunds && !fStocks)
    return;

  // Find our query params
  var qryArgs = queryArgsFromURL(window.location.href);

  var el = document.getElementById(id);

  if (el) fixupNavLinksEl(el, qryArgs, fStocks);
}

function fixupNavLinksEl(el, qryArgs, fStocks) {
  if (el) {
    if (el.nodeName.toUpperCase() == 'A') {
      // Only fixup matching section
      if (URLIsStocks(el.href) == fStocks) {
        var args = queryArgsFromURL(el.href, true);

        // 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.item(i), qryArgs, fStocks);
    }
  }
}

function getCannexParam(id) {
  var sel = document.getElementById(id);

  if (sel.selectedIndex == 0)
    return "";
  else
    return sel.value;
}

/**********************************************************************
* 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.tabspan = [];
  this.tablcnr = [];
  this.tabrcnr = [];
  this.content = new Array(tabs.length);
  this.iSel = -1;

  if (!initial || initial.length == 0)
    initial = tabs[0].id;

  this.getSelectedTabIndex = function() {
    return this.iSel;
  }

  this.handleTabClick = function(iTab) {
    if (this.iSel == iTab)
      return;
    // Turn off current sel
    if (this.iSel != -1) {
      this.tabspan[this.iSel].className = "tb_tabnonsel";
      this.tablcnr[this.iSel].className = "tb_tableftnonsel";
      this.tabrcnr[this.iSel].className = "tb_tabrightnonsel";
      if (this.content[this.iSel]) {
        this.content[this.iSel].style.display = "none";
      }
    }

    // Turn on new
    this.tabspan[iTab].className = "tb_tabsel";
    this.tablcnr[iTab].className = "tb_tableftsel";
    this.tabrcnr[iTab].className = "tb_tabrightsel";

    this.iSel = iTab;

    this.updateTabContent();

    if (this.tabs[this.iSel].fncClick)
      this.tabs[this.iSel].fncClick();
  }

  this.updateTabContent = function() {
    if (!this.content[this.iSel]) {
      this.content[this.iSel] = this.divContent.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";

  }


  var pdiv = parent.appendChild(document.createElement("div"));
  pdiv.className = "tb_tabs";

  // Add each tab
  for (var i = 0; i < tabs.length; i++) {
    var fSel = tabs[i].id == initial;

    if (fSel)
      this.iSel = i;

    // add left tab corner holder
    var span = pdiv.appendChild(document.createElement("span"));
    span.className = fSel ? "tb_tableftsel" : "tb_tableftnonsel";
    span.innerHTML = "&nbsp;";
    this.tablcnr.push(span);

    var span;

    if (tabs[i].tabId) {
      span = document.getElementById(this.tabs[i].tabId);

      span.parentNode.removeChild(span);
      pdiv.appendChild(span);

    }
    else {
      span = pdiv.appendChild(document.createElement("span"));
      span.innerHTML = tabs[i].caption;
    }
    span.style.display = span.style.display == 'none' ? 'inline' : span.style.display;
    span.className = fSel ? "tb_tabsel" : "tb_tabnonsel";
    this.tabspan.push(span);

    span.onclick = createThisCallback(this, "handleTabClick", i);

    // add right tab corner holder
    var span = pdiv.appendChild(document.createElement("span"));
    span.className = fSel ? "tb_tabrightsel" : "tb_tabrightnonsel";
    span.innerHTML = "&nbsp;";
    this.tabrcnr.push(span);

    if (i < tabs.length - 1) {
      // add spacer
      var span = pdiv.appendChild(document.createElement("span"));
      span.className = "tb_tabspace";
      span.innerHTML = "&nbsp;";
    }
  }


  var cdiv = parent.appendChild(document.createElement("div"));

  cdiv.className = "tb_content";

  this.divContent = cdiv;

  this.updateTabContent();
}

/**********************************************************************
* Lookup
*/
var lookupParams =
{
  "quotesearchMF":
	{
	  textinputid: "qs_managed_code",
	  ctryid: "qs_managed_ctry",
	  inforeqid: "qs_managed_inforeq",
	  searchbtnid: "qs_managed_btn",
	  urltest: "http://{domain}/lookup/default.aspx?feedid=0&code={code}&ctry={ctry}&page={page}",
	  urllookup: "http://{domain}/lookup/default.aspx?feedid=1&code={code}&page={page}",
	  urlgood: "/shares-and-funds/funds/quote.aspx?code={code}&ctry={ctry}&inforeq={inforeq}",
	  resultspaged: true,
	  testneedsprefix: true,
	  boxheight: fMozilla ? 221 : 200,
	  inforeq_urlgood:
		{
		  Profile: "/shares-and-funds/funds/profile.aspx?code={code}&ctry={ctry}&inforeq={inforeq}"
		}
	},
  "quotesearchSH":
	{
	  textinputid: "qs_shares_code",
	  ctryid: "qs_shares_ctry",
	  inforeqid: "qs_shares_inforeq",
	  searchbtnid: "qs_shares_btn",
	  urltest: "http://{domain}/lookup/default.aspx?feedid=2&code={code}&ctry={ctry}",
	  urlgood: "/shares-and-funds/research-a-company/results.aspx?code={code}&ctry={ctry}&inforeq={inforeq}",
	  resultspaged: true,
	  boxheight: fMozilla ? 121 : 100,
	  inforeq_urlgood:
		{
		  Overview: "/shares-and-funds/research-a-company/overview.aspx?subsectionid=4060&subsectionname=Researchacompany_Overview&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Charts: "/shares-and-funds/charts?subsectionid=4267&subsectionname=charts&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Quotes: "/shares-and-funds/quotes?subsectionid=4275&subsectionname=quotes&code={code}&ctry={ctry}&inforeq={inforeq}"
		}
	},
  "homefundsearch":
	{
	  textinputid: "qs_shares_code",
	  ctryid: "qs_shares_country",
	  inforeqid: "qs_shares_inforeq",
	  searchbtnid: "rhc_fund_btn",
	  urltest: "",
	  urltestxsl: "",
	  urllookup: "",
	  urllookupxsl: "",
	  urlgood: "",
	  resultspaged: true,
	  boxheight: fMozilla ? 121 : 100,
	  inforeq_urltest:
		{
		  Charts: "http://{domain}/lookup/default.aspx?feedid=2&code={code}&ctry={ctry}",
		  Quotes: "http://{domain}/lookup/default.aspx?feedid=2&code={code}&ctry={ctry}",
		  Quote: "http://{domain}/lookup/default.aspx?feedid=0&code={code}&ctry={ctry}&page={page}",
		  Profile: "http://{domain}/lookup/default.aspx?feedid=0&code={code}&ctry={ctry}&page={page}"
		},
	  inforeq_urllookup:
		{
		  Quote: "http://{domain}/lookup/default.aspx?feedid=1&code={code}&page={page}",
		  Profile: "http://{domain}/lookup/default.aspx?feedid=1&code={code}&page={page}"
		},
	  inforeq_urlgood:
		{
		  Charts: "/shares-and-funds/charts?subsectionid=4067&subsectionname=Researchacompany_Charts&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Quotes: "/shares-and-funds/quotes?subsectionid=4068&subsectionname=Researchacompany_Quotes&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Quote: "/shares-and-funds/funds/quote.aspx?code={code}&ctry={ctry}&inforeq={inforeq}",
		  Profile: "/shares-and-funds/funds/profile.aspx?code={code}&ctry={ctry}&inforeq={inforeq}"
		}
	},
  "homefundsearch":
	{
	  textinputid: "qs_shares_code",
	  ctryid: "qs_shares_ctry",
	  inforeqid: "qs_shares_inforeq",
	  searchbtnid: "qs_shares_btn",
	  urltest: "",
	  urltestxsl: "",
	  urllookup: "",
	  urllookupxsl: "",
	  urlgood: "",
	  resultspaged: true,
	  boxheight: fMozilla ? 121 : 100,
	  inforeq_urltest:
		{
		  Charts: "http://{domain}/lookup/default.aspx?feedid=2&code={code}&ctry={ctry}",
		  Quotes: "http://{domain}/lookup/default.aspx?feedid=2&code={code}&ctry={ctry}",
		  Quote: "http://{domain}/lookup/default.aspx?feedid=0&code={code}&ctry={ctry}&page={page}",
		  Profile: "http://{domain}/lookup/default.aspx?feedid=0&code={code}&ctry={ctry}&page={page}"
		},
	  inforeq_urllookup:
		{
		  Quote: "http://{domain}/lookup/default.aspx?feedid=1&code={code}&page={page}",
		  Profile: "http://{domain}/lookup/default.aspx?feedid=1&code={code}&page={page}"
		},
	  inforeq_urlgood:
		{
		  Charts: "/shares-and-funds/charts?subsectionid=4067&subsectionname=Researchacompany_Charts&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Quotes: "/shares-and-funds/quotes?subsectionid=4068&subsectionname=Researchacompany_Quotes&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Quote: "/shares-and-funds/funds/quote.aspx?code={code}&ctry={ctry}&inforeq={inforeq}",
		  Profile: "/shares-and-funds/funds/profile.aspx?code={code}&ctry={ctry}&inforeq={inforeq}"
		}
	},
  "rhcfundsearch":
	{
	  textinputid: "rhc_fund_code",
	  ctryid: "rhc_fund_country",
	  inforeqid: "rhc_fund_inforeq",
	  searchbtnid: "rhc_fund_btn",
	  urltest: "",
	  urltestxsl: "",
	  urllookup: "",
	  urllookupxsl: "",
	  urlgood: "",
	  resultspaged: true,
	  boxheight: fMozilla ? 121 : 100,
	  inforeq_urltest:
		{
		  Charts: "http://{domain}/lookup/default.aspx?feedid=2&code={code}&ctry={ctry}",
		  Quotes: "http://{domain}/lookup/default.aspx?feedid=2&code={code}&ctry={ctry}",
		  Quote: "http://{domain}/lookup/default.aspx?feedid=0&code={code}&ctry={ctry}&page={page}",
		  Profile: "http://{domain}/lookup/default.aspx?feedid=0&code={code}&ctry={ctry}&page={page}"
		},
	  inforeq_urllookup:
		{
		  Quote: "http://{domain}/lookup/default.aspx?feedid=1&code={code}&page={page}",
		  Profile: "http://{domain}/lookup/default.aspx?feedid=1&code={code}&page={page}"
		},
	  inforeq_urlgood:
		{
		  Charts: "/shares-and-funds/charts?subsectionid=4067&subsectionname=Researchacompany_Charts&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Quotes: "/shares-and-funds/quotes?subsectionid=4068&subsectionname=Researchacompany_Quotes&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Quote: "/shares-and-funds/funds/quote.aspx?code={code}&ctry={ctry}&inforeq={inforeq}",
		  Profile: "/shares-and-funds/funds/profile.aspx?code={code}&ctry={ctry}&inforeq={inforeq}"
		}
	},
  "rhcsearch":
	{
	  textinputid: "rhc_code",
	  ctryid: "rhc_ctry",
	  inforeqid: "rhc_inforeq",
	  searchbtnid: "rhc_btn",
	  urltest: "http://{domain}/lookup/default.aspx?feedid=2&code={code}&ctry={ctry}",
	  urlgood: "/shares-and-funds/research-a-company/results.aspx?code={code}&ctry={ctry}&inforeq={inforeq}",
	  resultspaged: true,
	  boxheight: fMozilla ? 121 : 100,
	  inforeq_urlgood:
		{
		  Overview: "/shares-and-funds/research-a-company/overview.aspx?subsectionid=4060&subsectionname=Researchacompany_Overview&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Charts: "/shares-and-funds/charts?subsectionid=4267&subsectionname=charts&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Quotes: "/shares-and-funds/quotes?subsectionid=4275&subsectionname=quotes&code={code}&ctry={ctry}&inforeq={inforeq}"
		}
	},
  "headLookupSearch81817":
	{
	  textinputid: "sq_code81817",
	  ctryid: "sq_ctry81817",
	  inforeqid: "sq_inforeq81817",
	  searchbtnid: "sq_btn81817",
	  urltest: "",
	  urltestxsl: "",
	  urllookup: "",
	  urllookupxsl: "",
	  urlgood: "",
	  resultspaged: true,
	  boxheight: fMozilla ? 121 : 100,
	  inforeq_urltest:
		{
		  Charts: "http://{domain}/lookup/default.aspx?feedid=2&code={code}&ctry={ctry}",
		  Quotes: "http://{domain}/lookup/default.aspx?feedid=2&code={code}&ctry={ctry}",
		  Quote: "http://{domain}/lookup/default.aspx?feedid=0&code={code}&ctry={ctry}&page={page}",
		  Profile: "http://{domain}/lookup/default.aspx?feedid=0&code={code}&ctry={ctry}&page={page}"
		},
	  inforeq_urllookup:
		{
		  Quote: "http://{domain}/lookup/default.aspx?feedid=1&code={code}&page={page}",
		  Profile: "http://{domain}/lookup/default.aspx?feedid=1&code={code}&page={page}"
		},
	  inforeq_urlgood:
		{
		  Charts: "/shares-and-funds/charts?subsectionid=4067&subsectionname=Researchacompany_Charts&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Quotes: "/shares-and-funds/quotes?subsectionid=4068&subsectionname=Researchacompany_Quotes&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Quote: "/shares-and-funds/funds/quote.aspx?code={code}&ctry={ctry}&inforeq={inforeq}",
		  Profile: "/shares-and-funds/funds/profile.aspx?code={code}&ctry={ctry}&inforeq={inforeq}"
		}
	},
  "headLookupSearch6755":
	{
	  textinputid: "sq_code6755",
	  ctryid: "sq_ctry6755",
	  inforeqid: "sq_inforeq6755",
	  searchbtnid: "sq_btn6755",
	  urltest: "",
	  urltestxsl: "",
	  urllookup: "",
	  urllookupxsl: "",
	  urlgood: "",
	  resultspaged: true,
	  boxheight: fMozilla ? 121 : 100,
	  inforeq_urltest:
		{
		  Charts: "http://{domain}/lookup/default.aspx?feedid=2&code={code}&ctry={ctry}",
		  Quotes: "http://{domain}/lookup/default.aspx?feedid=2&code={code}&ctry={ctry}",
		  Quote: "http://{domain}/lookup/default.aspx?feedid=0&code={code}&ctry={ctry}&page={page}",
		  Profile: "http://{domain}/lookup/default.aspx?feedid=0&code={code}&ctry={ctry}&page={page}"
		},
	  inforeq_urllookup:
		{
		  Quote: "http://{domain}/lookup/default.aspx?feedid=1&code={code}&page={page}",
		  Profile: "http://{domain}/lookup/default.aspx?feedid=1&code={code}&page={page}"
		},
	  inforeq_urlgood:
		{
		  Charts: "/shares-and-funds/charts?subsectionid=4067&subsectionname=Researchacompany_Charts&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Quotes: "/shares-and-funds/quotes?subsectionid=4068&subsectionname=Researchacompany_Quotes&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Quote: "/shares-and-funds/funds/quote.aspx?code={code}&ctry={ctry}&inforeq={inforeq}",
		  Profile: "/shares-and-funds/funds/profile.aspx?code={code}&ctry={ctry}&inforeq={inforeq}"
		}
	},
  "headLookupSearch7849":
	{
	  textinputid: "sq_code7849",
	  ctryid: "sq_ctry7849",
	  inforeqid: "sq_inforeq7849",
	  searchbtnid: "sq_btn7849",
	  urltest: "http://{domain}/lookup/default.aspx?feedid=2&code={code}&ctry={ctry}",
	  urlgood: "/shares-and-funds/research-a-company/results.aspx?code={code}&ctry={ctry}&inforeq={inforeq}",
	  resultspaged: true,
	  boxheight: fMozilla ? 121 : 100,
	  inforeq_urlgood:
		{
		  Overview: "/shares-and-funds/research-a-company/overview.aspx?subsectionid=4060&subsectionname=Researchacompany_Overview&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Charts: "/shares-and-funds/charts?subsectionid=4267&subsectionname=charts&code={code}&ctry={ctry}&inforeq={inforeq}",
		  Quotes: "/shares-and-funds/quotes?subsectionid=4275&subsectionname=quotes&code={code}&ctry={ctry}&inforeq={inforeq}"
		}
	},
  "headLookupSearch7914":
	{
	  textinputid: "sq_code7914",
	  ctryid: "sq_ctry7914",
	  inforeqid: "sq_inforeq7914",
	  searchbtnid: "sq_btn7914",
	  urltest: "http://{domain}/lookup/default.aspx?feedid=0&code={code}&ctry={ctry}&page={page}",
	  urllookup: "http://{domain}/lookup/default.aspx?feedid=1&code={code}&page={page}",
	  urlgood: "/shares-and-funds/funds/quote.aspx?code={code}&ctry={ctry}&inforeq={inforeq}",
	  resultspaged: true,
	  testneedsprefix: true,
	  boxheight: fMozilla ? 221 : 200,
	  inforeq_urlgood:
		{
		  Profile: "/shares-and-funds/funds/profile.aspx?code={code}&ctry={ctry}&inforeq={inforeq}"
		}
	}
};

function fixupLookups() {
  // Tweak the known lookup dropdowns to show appropriate inforeq depending on ctry
  function fixSingleLookup(id) {
    var param = lookupParams[id];
    var sel = document.getElementById(param.inforeqid);
    var ct = document.getElementById(param.ctryid);
    if (!sel || !ct)
      return;

    var ctry = ct.value;

    for (var i = 0; i < sel.options.length; i++) {
      var fAllowed = true;
      var val = sel.options[i].value;
      if (val != "Quotes" && val != "Charts")
        fAllowed = (ctry != "NZ");

      sel.options[i].style.color = fAllowed ? "#000000" : "#bbb";
      sel.options[i].disabled = fAllowed ? "" : "disabled";
    }

    sel.onchange = createCallback(preventSelectDisabled, sel);
    preventSelectDisabled(sel);

    ct.onchange = fixupLookups;
  }

  function preventSelectDisabled(oSelect) {
    var isOptionDisabled = oSelect.options[oSelect.selectedIndex].disabled;
    if (isOptionDisabled) {
      // find first not disabled
      for (var i = 0; i < oSelect.options.length; i++) {
        if (!oSelect.options[i].disabled) {
          oSelect.selectedIndex = i;
          break;
        }
      }
      return false;
    }
    else oSelect.defaultSelectedIndex = oSelect.selectedIndex;
    return true;
  }

  fixSingleLookup("headLookupSearch7849");
  fixSingleLookup("headLookupSearch7914");
  fixSingleLookup("headLookupSearch6755");
}

var iLookupPage = 0;
var idLookup;
var divLookupBox;
var strLookupCode;

var knownASX = { "ADB": true, "ALL": true, "AMC": true, "ANZ": true, "ASX": true, "AXA": true, "BBG": true, "BEN": true, "BHP": true, "BXB": true, "BLD": true, "CBA": true, "CCL": true, "CPU": true, "CSL": true, "CSR": true, "DJS": true, "ERG": true, "FGL": true, "FXJ": true, "GFF": true, "GNS": true, "GPT": true, "JHX": true, "HVN": true, "IAG": true, "LGL": true, "LLC": true, "MBL": true, "MQG": true, "MIG": true, "NAB": true, "NCM": true, "NEM": true, "OEC": true, "ORG": true, "OSH": true, "OST": true, "CMJ": true, "PPX": true, "QAN": true, "QBE": true, "RIO": true, "SEV": true, "SGB": true, "SHL": true, "STO": true, "SUN": true, "TAH": true, "TCL": true, "TEN": true, "TLS": true, "TOL": true, "WBC": true, "WES": true, "WOW": true, "WPL": true };

function getDomain() {
  var str = window.location.href;

  return str.split("//")[1].split("/")[0];
}

function GetURL(param, urlname) {
  var strInfoReq = document.getElementById(param.inforeqid).value;

  var strURL = param[urlname];
  if (param["inforeq_" + urlname] && param["inforeq_" + urlname][strInfoReq])
    strURL = param["inforeq_" + urlname][strInfoReq];

  return strURL;
}

function doLookup(id) {
  // for loookup from news site
  if (id == "headLookupSearch86355" || id == "headLookupSearch86898") {
    var sid = id.substring(id.indexOf("Search") + 6)
    var newsCode = document.getElementById("sq_code" + sid).value.toUpperCase();
    var newsInfoReq = document.getElementById("sq_inforeq" + sid).value;

    if (knownASX[newsCode]) {
      if (newsInfoReq == "Charts") {
        window.location.href = "http://money.ninemsn.com.au/shares-and-funds/charts?subsectionid=4067&subsectionname=Researchacompany_Charts&code=" + newsCode + "&ctry=AX&inforeq=Charts";
      } else if (newsInfoReq == "Quotes") {
        window.location.href = "http://money.ninemsn.com.au/shares-and-funds/quotes?subsectionid=4068&subsectionname=Researchacompany_Quotes&code=" + newsCode + "&ctry=AX&inforeq=Quotes";
      } else if (newsInfoReq == "Quote") {
        window.location.href = "http://money.ninemsn.com.au/shares-and-funds/funds/quote.aspx?code=" + newsCode + "&ctry=AX&inforeq=Quote";
      } else if (newsInfoReq == "Profile") {
        window.location.href = "http://money.ninemsn.com.au/shares-and-funds/funds/profile.aspx?code=" + newsCode + "&ctry=AX&inforeq=Profile";
      }
    } else {
      //open popup with lookup code
      var newwindow;
      var url = "http://investorv2.ninemsn.com.au/investorv2/lookupcode.asp?section=newslookup&lookup_code=" + newsCode + "&lookup_country=AU&lookup_cat=;&field=document.code";
      newwindow = window.open(url, "codelookup", "height=680,width=400");

      if (!newwindow.opener) {
        newwindow.opener = window;
      }
      if (window.focus) {
        newwindow.focus();
      }
    }
    return false;
  }

  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 = 'AX';
  var strInfoReq = document.getElementById(param.inforeqid).value;

  // If bypass or known code go straight to page
  if (param.bypasslookup || (strInfoReq != "Quote" && strInfoReq != "Profile" && strCtry == "AX" && knownASX[strLookupCode])) {
    var targetURL = formatURL(GetURL(param, "urlgood"), strLookupCode, strCtry, strInfoReq);
    if (id.toLowerCase() == "quotesearchSH" || id.toLowerCase() == "headlookupsearch7849") 
    { 
        if (strInfoReq.toLowerCase() == "recommendations") {
            targetURL = targetURL + "&sectionid=2338&sectionname=sharesAndFunds&subsectionid=4069&subsectionname=Researchacompany_BuySellHold";
        } else if (strInfoReq.toLowerCase() == "announcements") {
            targetURL = targetURL + "&sectionid=2338&sectionname=sharesAndFunds&subsectionid=4070&subsectionname=Researchacompany_Announcements";
        } else if (strInfoReq.toLowerCase() == "mainview") {
            targetURL = targetURL + "&sectionid=2338&sectionname=sharesAndFunds&subsectionid=4071&subsectionname=Researchacompany_Statistics";
        } else if (strInfoReq.toLowerCase() == "profile") {
            targetURL = targetURL + "&sectionid=2338&sectionname=sharesAndFunds&subsectionid=4072&subsectionname=Researchacompany_Profile";
        } else if (strInfoReq.toLowerCase() == "shareholders") {
            targetURL = targetURL + "&sectionid=2338&sectionname=sharesAndFunds&subsectionid=4073&subsectionname=Researchacompany_MajorShareholders";
        } else if (strInfoReq.toLowerCase() == "historical") {
            targetURL = targetURL + "&sectionid=2338&sectionname=sharesAndFunds&subsectionid=4074&subsectionname=Researchacompany_HistoricalFinancials";
        } else if (strInfoReq.toLowerCase() == "balancesheet") {
            targetURL = targetURL + "&sectionid=2338&sectionname=sharesAndFunds&subsectionid=4075&subsectionname=Researchacompany_BalanceSheets";
        } else if (strInfoReq.toLowerCase() == "forecasts") {
            targetURL = targetURL + "&sectionid=2338&sectionname=sharesAndFunds&subsectionid=4076&subsectionname=Researchacompany_ForecastEarnings";
        } else if (strInfoReq.toLowerCase() == "performance") {
            targetURL = targetURL + "sectionid=2338&sectionname=sharesAndFunds&subsectionid=4077&subsectionname=Researchacompany_SegmentResults";
        } else if (strInfoReq.toLowerCase() == "liquidity") {
            targetURL = targetURL + "&sectionid=2338&sectionname=sharesAndFunds&subsectionid=4078&subsectionname=Researchacompany_Liquidity";
        } else if (strInfoReq.toLowerCase() == "interimdata") {
            targetURL = targetURL + "&sectionid=2338&sectionname=sharesAndFunds&subsectionid=4079&subsectionname=Researchacompany_InterimData";
        }       
    }
    window.location.href = targetURL;
    if (window.event)
      window.event.returnValue = false;
    return false;
  }

  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.createElement("div");
    document.body.appendChild(divLookupBox);
  }

  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;
    }

    if (str.indexOf("__good__") != -1) {
      // Good code
      window.location.href = formatURL(GetURL(param, "urlgood"), strLookupCode, strCtry, strInfoReq);
    }
    else {
      handleLookup(str);
    }
  }

  var strURL = formatURL(GetURL(param, "urltest"), strLookupCode, strCtry);
  //alert(strURL);
  //alert(GetURL(param, "urltest"));
  loadDoc(strURL, handleTest);
}

function handleLookup(str) {
  var param = lookupParams[idLookup];

  // Display lookup
  var ef = document.getElementById(param.textinputid);

  var posEF = calcAbsolutePos(ef);

  var iHeight = str.indexOf("Sorry") == -1 ? param.boxheight : 60;

  setElementPos(divLookupBox, posEF.x + 1, posEF.y + posEF.height + 1, 350, iHeight);

  divLookupBox.className = "lookupBox";

  divLookupBox.innerHTML = str;

  var btn = document.getElementById(param.searchbtnid);

  btn.disabled = false;
}



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);
  strURL = strURL.replace("{domain}", getDomain());

  return strURL;
}

/**********************************************************************
* Cursor pos
*/
var mouseX;
var mouseY;

window.onload = init;
function init() {
  if (window.Event) {
    document.captureEvents(Event.MOUSEMOVE);
  }

  document.onmousemove = getXY;
}

mouseX = 0;
mouseY = 0;

function getXY(evnt) {
  mouseY = fMozilla ? evnt.pageY : (document.body.scrollTop + event.clientY);
  mouseX = fMozilla ? evnt.pageX : (document.body.scrollLeft + event.clientX);
  //	mouseX = (window.Event) ? e.pageX : event.screenX;
  //	mouseY = (window.Event) ? e.pageY : event.screenY;

  //window.status = mouseX + ", " + mouseY + ", " + event.clientY+ ", " + event.layerY+ ", " + event.offsetY+ ", " + event.screenY;
}

/**********************************************************************
* 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
*/

var elGlos;
var glosX;
var glosY;

function closeGlossary() {
  if (elGlos)
    hideElementPos(elGlos);
}

function glossaryId(id) {
  if (!elGlos)
    elGlos = document.body.appendChild(document.createElement("div"));

  elGlos.className = "glossaryLoading";
  elGlos.innerHTML = "Searching...";
  glosX = mouseX;
  glosY = mouseY;

  setElementPos(elGlos, mouseX, mouseY, 100, 25);

  var strURL = "/lookup/default.aspx?feedid=8&gid=" + id;

  function handleDoc(str) {
    setElementPos(elGlos, glosX, glosY, 300, 50);

    elGlos.className = "glossaryLoaded";
    elGlos.innerHTML = str;
  }

  loadDoc(strURL, handleDoc);

  //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");
  }

}

/*
* Expands the heights of div1 and div2 so that they are equal and as high as the parent.
*/
function expandDivHeight(parentid, div1id, div2id) {
  var p = document.getElementById(parentid);
  var d1 = document.getElementById(div1id);
  var d2 = document.getElementById(div2id);

  if (p != undefined && d1 != undefined && d2 != undefined)
    d2.style.height = d1.style.height = p.offsetHeight;
}


/* START navrender.js */
/******************************************************************************************
* This file is used to temporarily change the value of the JS_SUB_SECTIONID to trick the
* HL nav template into expanding the nav to a particular subsection. This is needed for
* the moneyguides section because the section and subsection id's are both 0, but the nav
* needs to expand to show the user where they are in the hierachy. The
* genToSubsectionMapping object is used to store the mapping between the geneircid (which
* is specified in the query string) to the subsection that should be expanded.
* Created by: Domagoj Filipovic (06/06/06)
******************************************************************************************/

//Mapping of the generic id's to the subsection id's
var genToSubsectionMapping =
{
  "714": 4482,
  "715": 4483,
  "716": 4484,
  "717": 4486,
  "718": 4487,
  "719": 4488,
  "720": 4489,
  "721": 4490,
  "727": 4684
};
var JS_SUB_SECTIONID;
var JS_SUB_SECTIONID_old; //Temporarily holds the real subsection id while the nav is rendered.
var genericId = extractGenericId();

//Returns the genericId variable from the query string
function extractGenericId() {
  var url = document.location.href;
  var parts = url.split("?")[1];
  if (parts) {
    var bits = parts.split("&");
    for (var bitset in bits) {
      var bit = bits[bitset].split("=");
      if (bit[0] && bit[0].toLowerCase() == "genericid")
        return bit[1];
    }
  }

  return ""; //genericid not present
}

//Used in conjunction with afterNavRender() to set the subsection id to trick the nav into
//expanding the nav for a particular generic id as specified by the genToSubsectionMapping.
function beforeNavRender() {
  if (genToSubsectionMapping[genericId]) {
    JS_SUB_SECTIONID_old = JS_SUB_SECTIONID;
    JS_SUB_SECTIONID = genToSubsectionMapping[genericId];
  }
}

//Used in conjunction with beforeNavRender() to set the subsection id back to their original value.
//This is needed in case any controls use this value after it has been changed by beforeNavRender().
function afterNavRender() {
  if (genToSubsectionMapping[genericId])
    JS_SUB_SECTIONID = JS_SUB_SECTIONID_old;
}
/* END navrender.js */
/* START font.js */
var Font =
{
  current: 11,
  baseSize: 9,
  heightDiff: undefined,
  buttonSmall: undefined,
  buttonMedium: undefined,
  buttonLarge: undefined,
  elem: undefined,
  increase: function(elem) {
    this.current = this.checkInt(Cookies.gc('fs'), this.current);
    this.current += (this.current >= 18 ? 0 : 1);
    this.setSize(elem, this.current);
    Cookies.ac('fs', this.current, 1440, document.domain);
  },
  decrease: function(elem) {
    this.current = this.checkInt(Cookies.gc('fs'), this.current);
    this.current -= (this.current <= 9 ? 0 : 1);
    this.setSize(elem, this.current);
    Cookies.ac('fs', this.current, 1440, document.domain);
  },
  initFont: function(small, medium, large, elem) {
    this.buttonSmall = { top: document.getElementById(small[0]), bottom: document.getElementById(small[1]), size: small[2] };
    this.buttonMedium = { top: document.getElementById(medium[0]), bottom: document.getElementById(medium[1]), size: medium[2] };
    this.buttonLarge = { top: document.getElementById(large[0]), bottom: document.getElementById(large[1]), size: large[2] };
    this.elem = document.getElementById(elem);
    var previousSize = Cookies.gc('fontSize');

    if (previousSize != undefined)
      this.setSize(previousSize);
    else	//default to medium
      this.setSize(this.buttonMedium.size);
  },
  setSize: function(v, ev) {
    var ccol = document.getElementById("contentcol");
    if (ccol != undefined && ev != undefined && this.heightDiff == undefined) {
      this.heightDiff = ccol.offsetHeight - this.elem.offsetHeight;
    }
    if (this.elem != undefined && this.elem.style) {
      v = this.checkInt(v, this.current);
      var curSize = this.current;

      if (this.elem.currentStyle) //for IE
        curSize = parseInt(this.elem.currentStyle.fontSize);
      else if (window.getComputedStyle) //for firefox
        curSize = parseInt(window.getComputedStyle(this.elem, "").getPropertyValue("font-size"));

      if (isNaN(curSize)) curSize = this.current;
      this.baseSize = curSize;
      this.applyToChildren(this.elem, v - curSize);
    }
    var rcol = document.getElementById("rghtcol");
    var lcol = document.getElementById("lftcol"); ;
    if (rcol != undefined && lcol != undefined && ccol != undefined && this.elem != undefined) {
      ccol.style.overflow = "visible";
      lcol.style.height = rcol.style.height = ccol.style.height = this.elem.offsetHeight + this.heightDiff;
      ccol.style.overflow = "hidden";
    }

    if (this.elem.currentStyle) {
      Cookies.ac('fontSize', v, 525600, "ninemsn.com.au");
      /*	
      var srcId = event.srcElement.id;
      this.setSelectedClass(this.buttonSmall, srcId);
      this.setSelectedClass(this.buttonMedium, srcId);
      this.setSelectedClass(this.buttonLarge, srcId);
      */
    }
    else {
      this.setStoredClass(this.buttonSmall, v);
      this.setStoredClass(this.buttonMedium, v);
      this.setStoredClass(this.buttonLarge, v);
    }
  },
  applyToChildren: function(elem, v) {
    if (elem != undefined && elem.style) {
      //set to current element
      var curSize = this.current;
      if (elem.currentStyle) //for IE
        curSize = parseInt(elem.currentStyle.fontSize);
      else if (window.getComputedStyle) //for firefox
        curSize = parseInt(window.getComputedStyle(elem, "").getPropertyValue("font-size"));

      if (isNaN(curSize)) curSize = this.current;
      /*if(elem.parentElement.style.fontSize != "" && elem.style.fontSize == "")
      elem.style.fontSize = (parseInt(elem.parentElement.style.fontSize) + (curSize - this.baseSize)) + "px";
      else
      */
      elem.style.fontSize = (parseInt(curSize + v) >= 9) ? parseInt(curSize + v) + "px" : "9px";

      //go through all children and apply to them
      //for(var currentElem = elem.firstChild; currentElem != undefined; currentElem = currentElem.nextSibling)
      //	this.applyToChildren(currentElem, v);
    }
  },
  checkInt: function(i, dv) {
    if (!dv) dv = 0;
    var iv = parseInt(i);
    return (iv.toString() == "NaN" ? dv : iv);
  },
  setSelectedClass: function(btnSet, srcId) {
    if ((btnSet.top != undefined && btnSet.top.id == srcId) || (btnSet.bottom != undefined && btnSet.bottom.id == srcId)) {
      if (btnSet.top != undefined) btnSet.top.className = "selected";
      if (btnSet.bottom != undefined) btnSet.bottom.className = "selected";
    }
    else {
      if (btnSet.top != undefined) btnSet.top.className = "";
      if (btnSet.bottom != undefined) btnSet.bottom.className = "";
    }
  },
  setStoredClass: function(btnSet, size) {
    if ((btnSet.size == size) || (btnSet.size == size)) {
      if (btnSet.top != undefined) btnSet.top.className = "selected";
      if (btnSet.bottom != undefined) btnSet.bottom.className = "selected";
    }
  }
}
/* END font.js */


/* START util.js */
/*
* Calculates the absolute position of an element within the page.
* Is useful when wanting to display popups absolutely.
* Returns an object with four properties: x, y, width, height
*/
function calcAbsolutePos(el) {
  if (el) {
    var o = new Object;

    o.width = el.offsetWidth;
    o.height = el.offsetHeight;

    o.x = 0;
    o.y = 0;
    while (el != null) {
      o.x += el.offsetLeft;
      o.y += el.offsetTop;

      el = el.offsetParent;
    }

    return o;
  }
}

/*
* Used in conjunction with setElementPos()
* Hides both the element and the iframe overlay if used
*/
function hideElementPos(el) {
  if (el) {
    el.style.display = "none";
    if (ifrOverlay)
      ifrOverlay.style.display = "none";
  }
}

/*
* Positions an element absolutely, positions an iframe underneath to hide selects. Used for the "floating box".
*/
function setElementPos(el, x, y, width, height, z) {
  if (el) {
    if (fMozilla) {
      el.style.left = x;
      el.style.top = y;
      el.style.width = width;
      el.style.height = height;
    }
    else {
      el.style.pixelLeft = x;
      el.style.pixelTop = y;
      if (width)
        el.style.pixelWidth = width;
      if (height)
        el.style.pixelHeight = height;
    }

    if (!fMozilla) {
      if (!ifrOverlay) {
        ifrOverlay = document.body.appendChild(document.createElement("iframe"));
        ifrOverlay.style.zIndex = 1000;
        ifrOverlay.style.position = "absolute";
      }
    }

    el.style.display = "block";


    if (z != undefined)
      el.style.zIndex = z;
    else
      el.style.zIndex = 1001;

    if (!fMozilla) {
      ifrOverlay.style.pixelLeft = el.style.pixelLeft;
      ifrOverlay.style.pixelTop = el.style.pixelTop;
      ifrOverlay.style.pixelWidth = el.style.pixelWidth;
      ifrOverlay.style.pixelHeight = el.style.pixelHeight;
      ifrOverlay.style.display = "block";
    }
  }
}


/*
* Creates an XML document
* Works for either mozilla or ie
*/
function createDoc(str) {
  if (fMozilla) {
    //create a DOMParser
    var objDOMParser = new DOMParser();

    //create new document from string
    var objDoc = objDOMParser.parseFromString(str, "text/xml");

    return objDoc;
  }
  else {
    var myxml = new ActiveXObject("Microsoft.XMLDOM")
    myxml.async = false;
    myxml.loadXML(str);

    return myxml;
  }
}

/*
* Given XML conent in a string format this function will load
* the XSL from the specified URL then transform the XML content using XSLT
* Works for either mozilla or ie
*/
function transformDoc(strDoc, strURLXSL, fnc) {
  if (!fIE) {
    var objDoc;

    if (typeof DOMParser == "undefined") {
      var req = new XMLHttpRequest;
      req.open("GET", "data:text/xml;charset=utf-8," + encodeURIComponent(strDoc), false);
      if (req.overrideMimeType)
        req.overrideMimeType("text/xml");
      req.send(null);
      objDoc = req.responseXML;
    }
    else {
      var objDOMParser = new DOMParser();

      objDoc = objDOMParser.parseFromString(strDoc, "text/xml");
    }

    var processor = new XSLTProcessor();

    var testTransform = document.implementation.createDocument("", "test", null);

    function onload() {
      var xmlSerializer = new XMLSerializer();

      processor.importStylesheet(testTransform);

      var newDocument = processor.transformToDocument(objDoc);

      fnc(xmlSerializer.serializeToString(newDocument));
    }

    testTransform.addEventListener("load", onload, false);

    testTransform.load(strURLXSL);

  }
  else {
    var myxml = new ActiveXObject("Microsoft.XMLDOM")
    myxml.async = false;
    myxml.loadXML(strDoc);

    var myxsl = new ActiveXObject("Microsoft.XMLDOM")
    myxsl.async = true;
    myxsl.load(strURLXSL);

    myxsl.onreadystatechange = function() {
      if (myxsl.readyState != 4)
        return;

      fnc(myxml.transformNode(myxsl));
    };
  }
}

/* END util.js */

