// *************** External Links ****************
/*  Modified from HS1/Guru. iFrame based only
    > If link has 'timesonline.co.uk' then target = '_top' <- should be in anyway
    > If Financial Express then target = '_blank' <- should be in anyway
    > If Hemscott then 'newWin' - this function to alter href as new window is customised
*/

addLoadEvent('footer',function() {
  var links = getElementsByTagName(document,'a');
  for(var i=0;i < links.length;i++) {
    var link = links[i];
    if (link.href.indexOf('timesonline.co.uk') > -1){
      if (window.opener){
        if (link.href.indexOf(window.opener.location.host) == -1) link.href = "javascript:returnToOpener('" + link.href + "')";
      }
    }else if (link.href.indexOf('financialexpress') > -1){
      link.target = "_blank";
      link.title = updateTitle(link.title);
    }else if (elementHasClass(link,'external')){
      link.href = "javascript:openWin('" +link.href + "');";
      link.target = "";
      link.title = updateTitle(link.title);
    }
  }
});

function openWin(link){
  window.open(link,'HemscottTimesonline','menubar=no,location=no,status=no,titlebar=no,toolbar=no,scrollbars=yes,resizable=yes,width=996,height=600,left=25,top=25');
}
function returnToOpener(link){
  window.opener.parent.location.href = link;
  window.close();
}
function updateTitle(title){
  if (title == '') title = 'opens in a new window';
  else title += ' (opens in a new window)';
  return title;
}

// Help Window

function helpWindow(url, width, height, scrollbars) {
  OpenWin = this.open(url, "CtrlWindow", "toolbar=no,menubar=no,height="+height+",width="+width+",location=no,scrollbars="+scrollbars+",resizable=no,status=no,left=100,top=100");
}

// Individual Sector Heatmap

function openWin_ihs() {
  var index = document.getElementById('index').value;
  var url = "/timesonline/timesonline.jsp?page=one-sector-heatmap-popup&sector=" + document.getElementById('sector').value;
  if (index != "ALL") url = url + "&index=" + index;
  window.open(url, 'Heatmaps', 'menubar=no,location=no,status=no,titlebar=no,toolbar=no,scrollbars=yes,height=450,width=600,resizable=yes,left=45,top=170');
}

// News Stories

function newsWindow(url) {
  OpenWin = this.open(url, "CtrlWindow", "toolbar=no,menubar=no,height=600,width=990,location=no,scrollbars=yes,resizable=no,status=no,left=50,top=50");
  return false;
}


// *************** Tab Switching ****************

var tabLinks;
var tabs;

function updateTablinks(pageId, elementId) {

  var tabsDiv = getElementById(elementId);

  if (tabsDiv) {
    tabLinks = getElementsByTagName(tabsDiv,'a');

    for (var i=0;i < tabLinks.length;i++) {
      var href= tabLinks[i].href;
      tabLinks[i].href = "javascript:setTab('" + pageId + "','" + elementId + "'," + i + ");";
    }
  }
}

function setTab(containerId, tabsContainer, index) {

  var containerDiv = getElementById(containerId);
	var tabsDiv = getElementById(tabsContainer);

  if (tabsDiv) {
    tabLinks = getElementsByTagName(tabsDiv,'a');
    for (var i=0;i < tabLinks.length;i++) {
      if (i == index) applyClass(tabLinks[i], "selected");
      else removeClass(tabLinks[i], "selected");
    }

    tabs = getElementsByClassName(containerDiv,'div','tab-item');
    for (var i=0;i < tabs.length;i++) {
      if (i == index) tabs[i].style.display = 'block';
      else tabs[i].style.display = 'none';
    }
  }

  /*if (containerId == 'market-tabs') {
    if (index == 0) document.getElementById('delayStatement').innerHTML="Delayed by 15 minutes<br />Data from <a href='http://www.hemscott.com' target='_blank'>Hemscott</a>";
    else document.getElementById('delayStatement').innerHTML="Delayed by 20 minutes<br />Data from <a href='http://www.hemscott.com' target='_blank'>Hemscott</a>";
  }*/
}

// *************** General Functions ****************

function trim(text) {
	//trim whitespace from a string
	text = text.replace(/^\s+/g,''); //leading space
	return text.replace(/\s+$/g,''); //trailing space
}

function addLoadEvent(id,func) {
  //add an onload event which will fire once the given id is located within the document
  //this is more reliable than window.onload and will happen exactly when needed

  function loadEvent() {
    var element = getElementById(id);
    if (element) func(); else window.setTimeout(loadEvent,2000);
  }

  loadEvent();

}
function getElementById(id) {
  //get element with a given id
  return (document.all)? document.all[id] : document.getElementById(id);
}
function getElementsByTagName(container, tagName) {
  //get all html nodes of a given type within a given container
  if (container) return container.getElementsByTagName(tagName);
  else return new Array();
}

function getElementsByClassName(container, tagName, className) {
  //get all html nodes of a given type and class within a given container
  var elements = getElementsByTagName(container, tagName);
  var result = new Array();
  for(var i=0;i < elements.length;i++) {
    var e = elements[i];
    if (elementHasClass(e,className)) result.push(e);
  }
  return result;
}
function elementHasClass(element, className) {
  //return true if the element has the given class name
  className = className.replace(/\-/g, '\\-');
  var re = new RegExp('(^|\\s)' + className + '(\\s|$)');
  return re.test(element.className);
}

function applyClass(element, className) {
  //add a class to an element
  if (!elementHasClass(element, className)) element.className += ' ' + className;
}

function removeClass(element, className) {
  //remove a class from an element
  className = className.replace(/\-/g, '\\-');
  var re = new RegExp('(^|\\s)' + className + '(\\s|$)');
  element.className = element.className.replace(re,'');
}

function getQueryParams(query) {
  //decode query parameters into an associative array
  var params = new Object();
  if (!query) return params;
  var start = query.indexOf('?');
  if (start > -1) query = query.substr(start + 1);
  var paramArray = query.split("&");
  for (var i in paramArray) {
    var param = paramArray[i];
    var splitPos = param.indexOf("=");
    var name = unescape(param.substring(0,splitPos));
    var value = unescape(param.substring(splitPos+1));
    params[name] = value;
  }
  return params;
}
function disclaimerOpen(pageName) {
    pageName = pageName.toLowerCase();
	var height, width;
	var url = "http://lt.morningstar.com/ktz955vs0m/util/disclaimer.aspx";

	switch (pageName){

		case "privacypolicy":
			url = url + "?type=privacy";
			height = 400;
			width = 530;
			break;

		case "termsofuse":
			url = url + "?type=terms";
			height = 200;
			width = 330;
			break;

		default:
			height = 200;
			width = 330;
			break;
	}

	window.open(url, 'msDisclaimerWindow', 'toolbar=no,location=no,menubar=no,width='+width+',height='+height+',scrollbars=yes,resizable=no');

	return false;
}
