// $Id: utilities.js,v 1.3 2010/09/02 13:18:48 obo Exp $

// General JS utilities (not usually using Dojo)

// Clear all the boxes on the Index Performance page
function clearAllBoxes() {
  var allTickers = document.getElementsByName("index");
  for (i = 0; i != allTickers.length; i++) {
    allTickers[i].checked=false;
  }
}

// onMouse event-handlers to switch background color
function switchBG(element, color) {
 element.style.backgroundColor = color;
}

// onMouse event handler to follow link
function linkTo(path) {
  location.href = path;
}

// Switch image to highlight countries on "Search by Region" page
function switchImage(url) {
  dojo.byId("byRegionMap").src = url;
  return false;
}

// Check keywords in pushtool
function checkKeywords(width, height, href) {
 var left = screen.width - width - 10;
 var top = (screen.height - height) / 2;
 var popup = window.open(href, 'keywords',
     "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left + ",resizable=1"
     + "directories=0,location=0,menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0");
 popup.window.focus();
 return false;
}

// Printing content part only
function printPage(width, height, querystring) {
 var left = (screen.width - width) / 2;
 var top = (screen.height - height) / 2;
 var dothtml = /\.html(\#.*)?(\?.*)?(\#.*)?/;
 var href = location.href.replace(dothtml, ".htm");
 var popup = window.open(href + querystring, 'popup',
     "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left + ",resizable=1"
     + "directories=0,location=0,menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0");
 popup.window.focus();
 return false;
}

// Clear (unset) all checkboxes on Index Performance page
function clearAllBoxes() {
  dojo.query(".form-checkbox").forEach(
    function(node) {
      node.checked = false;
    }
  );
  return false;
}

// Switched the selected index, using the sub-index selector pulldown, on the overview page
function indexSelected(paneId, pane, index) {
  var link = "/indices/detail/"+pane+".html?symbol="+index;
  var pane = dijit.byId(paneId);
  if (pane) {
    pane.href = link;
    pane.refresh();
  }
}

// Sort the components list by some order (usually "byName" or "byWeight").
// NB: Assumes paneId = "lev1_3" and pane = "components"
function sortComponentsList(index, order) {
  var pane = dijit.byId("lev1_3");
  if (pane) {
    pane.href = "/indices/detail/components.html?symbol=" + index + "&sort=" + order;
    pane.refresh();
  }
}
