/****************************************************
** functions.js - Main javascript function library **
****************************************************/

/* Init actions */
function changeOnSubmit() { // disable enter key form submission (chrome, safari)
  //oldsubmit = document.forms[0].onsubmit;
  document.forms[0].action = "javascript:return false();";
  /*document.forms[0].onsubmit = function() {
    //oldsubmit();
    return false;
  }*/
}
getTopic( TOPIC_ONLOAD ).addListener( 'changeOnSubmit()' );




/* browser detection, used to get visitor's browser name and version */
var BrowserDetect = {
  init: function () {
    this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
    this.version = this.searchVersion(navigator.userAgent)
      || this.searchVersion(navigator.appVersion)
      || "an unknown version";
    this.OS = this.searchString(this.dataOS) || "an unknown OS";
  },
  searchString: function (data) {
    for (var i=0;i<data.length;i++)  {
      var dataString = data[i].string;
      var dataProp = data[i].prop;
      this.versionSearchString = data[i].versionSearch || data[i].identity;
      if (dataString) {
        if (dataString.indexOf(data[i].subString) != -1)
          return data[i].identity;
      }
      else if (dataProp)
        return data[i].identity;
    }
  },
  searchVersion: function (dataString) {
    var index = dataString.indexOf(this.versionSearchString);
    if (index == -1) return;
    return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
  },
  dataBrowser: [
    {
      string: navigator.userAgent,
      subString: "Chrome",
      identity: "Chrome"
    },
    {   string: navigator.userAgent,
      subString: "OmniWeb",
      versionSearch: "OmniWeb/",
      identity: "OmniWeb"
    },
    {
      string: navigator.vendor,
      subString: "Apple",
      identity: "Safari",
      versionSearch: "Version"
    },
    {
      prop: window.opera,
      identity: "Opera"
    },
    {
      string: navigator.vendor,
      subString: "iCab",
      identity: "iCab"
    },
    {
      string: navigator.vendor,
      subString: "KDE",
      identity: "Konqueror"
    },
    {
      string: navigator.userAgent,
      subString: "Firefox",
      identity: "Firefox"
    },
    {
      string: navigator.vendor,
      subString: "Camino",
      identity: "Camino"
    },
    {    // for newer Netscapes (6+)
      string: navigator.userAgent,
      subString: "Netscape",
      identity: "Netscape"
    },
    {
      string: navigator.userAgent,
      subString: "MSIE",
      identity: "Explorer",
      versionSearch: "MSIE"
    },
    {
      string: navigator.userAgent,
      subString: "Gecko",
      identity: "Mozilla",
      versionSearch: "rv"
    },
    {     // for older Netscapes (4-)
      string: navigator.userAgent,
      subString: "Mozilla",
      identity: "Netscape",
      versionSearch: "Mozilla"
    }
  ],
  dataOS : [
    {
      string: navigator.platform,
      subString: "Win",
      identity: "Windows"
    },
    {
      string: navigator.platform,
      subString: "Mac",
      identity: "Mac"
    },
    {
      string: navigator.platform,
      subString: "Linux",
      identity: "Linux"
    }
  ]

};
BrowserDetect.init();

function includeCss(cssHref) // include a css file dynamically based on filename
{
  var headID = document.getElementsByTagName("head")[0];
  var cssNode = document.createElement('link');
  cssNode.type = 'text/css';
  cssNode.rel = 'stylesheet';
  cssNode.href = urlPrefix + templateHome + 'css/browsers/' + cssHref;
  cssNode.media = 'screen';
  headID.appendChild(cssNode);
}




/* browser hack css includes */
var browserName = BrowserDetect.browser.toLowerCase();
switch(browserName) { // include individual css based on browser name and version
  case "safari": // safari
    includeCss('safari.css');
    break;
  case "firefox": // firefox
    if(BrowserDetect.version == 3) { includeCss('ff3.css'); browserName += "3"; } // ff3
    else if(BrowserDetect.version < 3) { includeCss('ff2.css'); browserName += "2"; } // ff2-
    break;
  case "chrome": // chrome
    includeCss('chrome.css');
    break;
  case "opera": // opera
    includeCss('opera.css');
    break;
  case "explorer": // ie
    if(BrowserDetect.version == 8) { includeCss('ie8.css'); browserName += "7"; } // ie8
    else if(BrowserDetect.version == 7) { includeCss('ie7.css'); browserName += "7"; } // ie7
    else if(BrowserDetect.version < 7) { includeCss('ie6.css'); browserName += "6"; } // ie6-
    break;
}


/* nav menu functions */
var activeNav = [];
var openNav = [];
var navTimer = [];
var selectStatus = [];
var defaultOfficeId = "SanAntonio";

function showNavMenu(navId, navNum) // unhide nav menu content to display
{
  if(getTag(navId + navNum)) { // just in case page hasn't finished loading yet
    openNav[navId] = true;
    if(navNum != activeNav[navId]) { // if not dealing with currently opened menu
      if(activeNav[navId]) { // if any open
        getTag(navId + activeNav[navId]).style.display = "none";
        if(navId == "navMenu") { // also change top nav button class
          getTag("navLink" + activeNav[navId]).className = "";
        }
        else if(navId == "navOfficeInfo") { // change office image too
          getTag("navOfficeImage" + activeNav[navId]).style.display = "none";
        }
      }
      if(navId == "navOfficeInfo" && navNum != defaultOfficeId) { // hide office image too
        getTag("navOfficeInfo" + defaultOfficeId).style.display = "none";
        getTag("navOfficeImage" + defaultOfficeId).style.display = "none";
      }

      getTag(navId + navNum).style.display = "block";
      if(navId == "navMenu") { // also change top nav button class
        getTag("navLink" + navNum).className = "active";
      }
      else if(navId == "navOfficeInfo") { // change office image too
        getTag("navOfficeImage" + navNum).style.display = "block";
      }
      activeNav[navId] = navNum;

      selectStatus[navId] = false;
    }
    if(navTimer[navId]) {
      clearTimeout(navTimer[navId]); // cancel any pending menu hiding
    }
  }
}


function hideNavMenu(navId) // initialize a menu hide with a delay
{
  openNav[navId] = false;
  if(selectStatus[navId] == undefined || selectStatus[navId] == false) { // hide
    navTimer[navId] = setTimeout("hideHelper('" + navId + "')", 250); // queue up menu hiding
  }
/*  else if(browserName.indexOf("explorer") == -1) { // ie handles select hovers weirdly, so do nothing until they have unselected the box
    navTimer[navId] = setTimeout("hideHelper('" + navId + "')", 1000); // queue up menu hiding
  }*/
}

function hideHelper(navId) // hide menu
{
  if(openNav[navId] == undefined || openNav[navId] == false) { // hide
    if(activeNav[navId]) { // if any open
      var doHide = true;
      if(navId == "navMenu") { // also change top nav button class
        getTag("navLink" + activeNav[navId]).className = "";
      }
      else if(navId == "navOfficeInfo") { // change office image too
        if(activeNav[navId] == defaultOfficeId) { // moving off default office, do nothing
          doHide = false;
        }
        else {
          getTag("navOfficeImage" + activeNav[navId]).style.display = "none";
          // turn on default (San Antonio)
          getTag("navOfficeInfo" + defaultOfficeId).style.display = "block";
          getTag("navOfficeImage" + defaultOfficeId).style.display = "block";
        }
      }

      if(doHide) {
        getTag(navId + activeNav[navId]).style.display = "none";
      }
      activeNav[navId] = null;
    }
  }
}

function selectOpen(navId) // don't hide menu when select is open
{
  clearTimeout(navTimer[navId]); // cancel any pending menu hiding
  selectStatus[navId] = true;
}
function selectClose(navId) // select is closed, no longer under anti-close protection
{
  selectStatus[navId] = false;
  clearTimeout(navTimer[navId]); // cancel any pending menu hiding
  navTimer[navId] = setTimeout("hideHelper('" + navId + "')", 1000); // queue up menu hiding
}


/* Utility Functions */
function imgro(item) // rollover image replacement
{
  var imgpath = item.src;
  if(imgpath.match("_ro") == null) {
    var last_dot = imgpath.lastIndexOf(".");
    var x = imgpath.substring(0, last_dot) + "_ro" + imgpath.substring(last_dot);
    item.src = x;
  }
}
function imgundo(item) // unrollover image restoration
{
  var imgpath = item.src;
  if(imgpath.match("_ro") != null) {
    var x = imgpath.replace('_ro.','.');
    item.src = x;
  }
}

function checkEnter(e, funcToCall, funcParam) // uses enter key to submit form
{
  if(!e) {
    e = window.event;
  }
  var characterCode = (e && e.which ? e.which : (e.keyCode ? e.keyCode : event.keyCode));
  //var characterCode = (((!e || e.keycode) && !e.which) ? (!e ? event.keyCode : e.keycode) : e.which);

  if(characterCode == 13) { // enter key
    (e ? e : event).returnValue = false;
    (e ? e : event).cancelBubble = true;
    if(funcParam != undefined && funcParam != "") {
      funcToCall(funcParam); // submit the form with param
    }
    else {
      funcToCall(); // submit the form without param
    }
    return false;
  }
  else {
    return true;
  }
}


/* Email and Print Functions */
var MAILTO_CONFIRMATION = "Transmitting information to us by E-mail does not establish an attorney-client relationship. By clicking \"OK\", you acknowledge that we have no obligation to maintain the confidentiality of any information you submit to us unless we already represent you or we later agree to represent you. Thus, if you are not a client, information you submit to us by E-mail may be disclosed to others or used against you.\n\nIf you would like to discuss becoming a client, please contact one of our attorneys to arrange for a meeting or telephone conference. Thank you for your interest in Thompson Coe.";
var VCARD_CONFIRMATION = MAILTO_CONFIRMATION;

function mailTo(email)
{
  if(confirm(MAILTO_CONFIRMATION)) {
    window.parent.location = "mailto:" + email;
  }
}

function vCard(path)
{
  if(confirm(VCARD_CONFIRMATION)) {
    window.parent.location = path;
  }
}

function printPage()
{
  var url = window.location.href;
  url += (url.indexOf("?") == -1 ? "?" : "&") + "printver=true";
  window.open(url, 'Print', 'width=725,height=475,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,top=20,left=20');
}

function emailPage()
{
  var url = window.location.href;
  //if(confirm(MAILTO_CONFIRMATION)) {
    window.location = "mailto:?body=" + escape(url);
  //}
}


function printPDF( pagePath, templateName, designName )
{
  var url = window.location.href;
  url += (url.indexOf("?") == -1 ? "?" : "&") + "printTo=pdfw";

  if(pagePath) {
    url += "&pagePath=" + pagePath;
  }

  if(templateName) {
    url += "&templateName=" + templateName;
  }

  if(designName) {
    url += "&designName=" + designName;
  }

  window.location = url;
}


/*
  Developed by Robert Nyman, http://www.robertnyman.com
  Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function(className, tag, elm) {
  if(document.getElementsByClassName) {
    getElementsByClassName = function(className, tag, elm) {
      elm = elm || document;
      var elements = elm.getElementsByClassName(className),
        nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
        returnElements = [],
        current;
      for(var i=0, il=elements.length; i<il; i+=1){
        current = elements[i];
        if(!nodeName || nodeName.test(current.nodeName)) {
          returnElements.push(current);
        }
      }
      return returnElements;
    };
  }
  else if(document.evaluate) {
    getElementsByClassName = function(className, tag, elm) {
      tag = tag || "*";
      elm = elm || document;
      var classes = className.split(" "),
        classesToCheck = "",
        xhtmlNamespace = "http://www.w3.org/1999/xhtml",
        namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
        returnElements = [],
        elements,
        node;
      for(var j=0, jl=classes.length; j<jl; j+=1){
        classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
      }
      try  {
        elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
      }
      catch (e) {
        elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
      }
      while ((node = elements.iterateNext())) {
        returnElements.push(node);
      }
      return returnElements;
    };
  }
  else {
    getElementsByClassName = function(className, tag, elm) {
      tag = tag || "*";
      elm = elm || document;
      var classes = className.split(" "),
        classesToCheck = [],
        elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
        current,
        returnElements = [],
        match;
      for(var k=0, kl=classes.length; k<kl; k+=1){
        classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
      }
      for(var l=0, ll=elements.length; l<ll; l+=1){
        current = elements[l];
        match = false;
        for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
          match = classesToCheck[m].test(current.className);
          if(!match) {
            break;
          }
        }
        if(match) {
          returnElements.push(current);
        }
      }
      return returnElements;
    };
  }
  return getElementsByClassName(className, tag, elm);
};


// check all divs in narrow page column (if exists) to see if anything is drawn to the page, if not then hide it (allowing the wide column to be 100% width)
function hideEmptyAreas() {
  var colNames = ["top-col", "narrow-col"];

  for(var j in colNames)
  {
    var thisCol = getElementsByClassName(colNames[j], "td", getTag("contentBody"));
    if(thisCol.length > 0) { // if narrow col exists
      var thisColDivs = thisCol[0].getElementsByTagName("DIV");
      var totalHeight = 0;
      var temp;
      var pb;

      for(var i=0; i<thisColDivs.length; i++) { // loop through all narrow col divs
        if((temp = thisColDivs[i].id) && temp.indexOf("portlet") >= 0) { // one of the main portlets
          temp = thisColDivs[i].offsetHeight;
          if(temp > 0) {
            pb = parseInt(getStyle(thisColDivs[i], "paddingBottom"));
            temp = (temp >= pb ? (temp - pb) : temp);
            totalHeight += temp;
          }
        }
      }

      if(totalHeight <= 0) {
        thisCol[0].style.display = "none";
        
        if(colNames[j] == "narrow-col") { // make wide col fill 100%
          var wideCol = getElementsByClassName("wide-col", "td", getTag("contentBody"));

          if(wideCol.length > 0) { // if wide col exists
            wideCol[0].className = wideCol[0].className.replace("wide-col", "");
          }
        }
      }
      if(colNames[j] == "top-col") { // change top-col to wide-col
        thisCol[0].className = thisCol[0].className.replace("top-col", "page-colA");
      }
    }
  }
}


/***************************************** NAVIGATION MENU *****************************************/
var sub;
var subtree = new Array();
var x = 0;
var openOnLeft = false;
var maxNavWidth;
var offsetHeight = 2;
var offsetWidth = 2;

function opensubmenu(item, divid)
{
  var frame;
  if(maxNavWidth == undefined) { // set init values
    maxNavWidth = getTag("nav").clientWidth;
  }
  var navPos = findPos(getTag("nav"), "header");
  //alert("navPos.x="+navPos.x+", navPos.y="+navPos.y);

  if(item.parentNode.parentNode.className=='submenu') { // if item is part of a submenu, and not the top tier
    while(sub != item.parentNode.parentNode) { // if we mouse over item not in last opened div, we will close last
      if(sub) { /////////// only if the object existed
        sub.style.visibility='hidden';
        getTag('hideframe'+x).style.visibility='hidden';
        x--;
        sub=subtree[x];
      }
    }//////////////////////////////////// end close last div if mouseover diff object

    if(getTag(divid)) { // open new submenu if it exhists  |getElementById(subtree[x].id+'sub'+located)|
      sub = getTag(divid);
      if(sub) { /////////////// only if it exists
        x++;
        //alert("subtree[x-1].style.marginLeft=" + parseInt(subtree[x-1].style.marginLeft) + " + " + subtree[x-1].clientWidth + "\n" + (parseInt(subtree[x-1].style.marginLeft) - sub.clientWidth) + "px");
        frame = getTag('hideframe'+x);


        if(!openOnLeft) { // right side
          var temp = (parseInt(subtree[x-1].style.marginLeft) + subtree[x-1].clientWidth + offsetWidth);
          if((temp + sub.clientWidth) > (navPos.x + maxNavWidth)) { // too wide
            openOnLeft = true;
          }
          else {
            sub.style.marginLeft = temp + "px";
            frame.style.marginLeft = temp + "px";
          }
        }
        if(openOnLeft) { // left side
          sub.style.marginLeft = (parseInt(subtree[x-1].style.marginLeft) - (sub.clientWidth + offsetWidth)) + "px";
          frame.style.marginLeft = (parseInt(subtree[x-1].style.marginLeft) - (sub.clientWidth + offsetWidth)) + "px";
        }
        sub.style.marginTop = (parseInt(subtree[x-1].style.marginTop) + item.offsetTop) + "px";
        frame.style.marginTop = (parseInt(subtree[x-1].style.marginTop) + item.offsetTop) + "px";


        sub.style.visibility = 'visible';
        frame.style.visibility = 'visible';
        frame.style.height = sub.clientHeight;
        frame.style.width = sub.clientWidth;
      }
    }/////////////////////// end new submenu /////////////////
  }//////////////////////end if part of a submenu ///////////////////////

  else { ///////////////////////// if item is the top tier, we want to open first drop down
    frame = getTag('hideframe0');
    if(sub != undefined) { // if this is not the first time we open a dropdown
      for(var y = x; y >= 0; y--) { // close all other drop downs
        subtree[y].style.visibility='hidden';
        getTag('hideframe'+y).style.visibility='hidden';
      }
      clearhidemenu();
    } /////////////////////////////////////////////////////////////////////
    if(getTag(divid)) {
      sub = getTag(divid);
      var pos = findPos(item, "nav");
      var subWidth = sub.clientWidth;
      pos.x += navPos.x;
      pos.y += navPos.y;
      sub.style.marginLeft = pos.x + "px";
      frame.style.marginLeft = pos.x + "px";
      sub.style.marginTop = (pos.y + item.clientHeight) + offsetHeight + "px";
      frame.style.marginTop = (pos.y + item.clientHeight) + offsetHeight + "px";
      if((pos.x + subWidth) > (navPos.x + maxNavWidth)) { // hug right side
        sub.style.marginLeft = (navPos.x + maxNavWidth - subWidth) + "px";
        frame.style.marginLeft = (navPos.x + maxNavWidth - subWidth) + "px";
        openOnLeft = true;
      }
      else {
        openOnLeft = false;
      }

      x = 0;
      sub.style.visibility = 'visible';
      frame.style.visibility = 'visible';
      frame.style.height = sub.clientHeight;
      frame.style.width = sub.clientWidth;
      item.onmouseout=delayhidemenu;
    }
  } /////////////////////// end of top tier dropdown area
  if(sub) {
    subtree[x] = sub;
    sub.onmouseover=clearhidemenu;
    sub.onmouseout=delayhidemenu;
  }
}

function delayhidemenu()
{
  var strx = '';
  for(var y = x; y >= 0; y--) {
    strx += "subtree["+y+"].style.visibility='hidden',";
    strx += "getTag('hideframe"+y+"').style.visibility='hidden',";
  }
  strx = strx.substring(0,strx.length-1);
  strx = "" + strx + "";
  delayhide=setTimeout(strx,500);
}

function clearhidemenu()
{
  if(typeof delayhide!="undefined") {
    clearTimeout(delayhide);
  }
}


/* Return x/y offset of an object up to a limit container */
function findPos(ctrl, limitId)
{
  var pos = {x:0, y:0};
  if(ctrl.offsetParent) {
    while(ctrl && ctrl.id != limitId) {
      pos.x += ctrl.offsetLeft;
      pos.y += ctrl.offsetTop;
      ctrl = ctrl.offsetParent;
    }
  }
  else if(ctrl.x && ctrl.y) {
    pos.x += ctrl.x;
    pos.y += ctrl.y;
  }
  return pos;
}

/* Check/uncheck all checkboxes in container */
function checkAll(containerId, checkStatus)
{
  checkBoxList = getTag(containerId).getElementsByTagName('INPUT');
  for(var i=0; i<checkBoxList.length; i++) {
    if(checkBoxList[i].type == 'checkbox'){
      checkBoxList[i].checked = checkStatus;
    }
  }
}




/* Search url changer */
var fieldDefValues = [];
var fieldIdList = [];
var search_url = [];
var search_isDate = [];
var search_isScope = []; 
function search_init(name_pre)
{
  var fieldNameList = "|";
  var fieldIdArray = fieldIdList[name_pre].split("|");
  for(var i = 0; i < fieldIdArray.length; i++) { // collect all field names from the input tag name
    var token = fieldIdArray[i];
    if(getTag(fieldIdArray[i]).name) {
      fieldNameList += getTag(fieldIdArray[i]).name + "|";
    }
  }

  var params = "";

  for( var fieldName in fieldDefValues[name_pre] ) {
    var fieldValue = getTag(fieldName).value;
    if(fieldValue != fieldDefValues[name_pre][fieldName] || getTag(fieldName).name == "search") { // not default value
      if((fieldValue.replace(/(^\s*)|(\s*$)/g, "").length > 0 && getTag(fieldName).name) || getTag(fieldName).name == "search") { // if string is more than just spaces
        if(fieldValue == fieldDefValues[name_pre][fieldName]) { // remove default value if it got through
          fieldValue = "";
        }
        else {
          fieldValue = escape(fieldValue.replace(/(^\s*)/g, "").replace(/(\s*$)/g, ""));
          /*
          fieldValue = ((getTag(fieldName).type == "select-one" && (fieldValue.indexOf(" ") >= 0 || fieldValue.indexOf("%20") >= 0)) ? 
            ("\"" + fieldValue + "\"") : 
            fieldValue); // dropdown element, surround with quotes if any spaces
          */
        }
        params += "&" + getTag(fieldName).name + "=" + fieldValue; // trim leading/trailing spaces
      }
    }
  }


  // process from/to dates
  if(search_isDate[name_pre] != undefined && search_isDate[name_pre] == true) {
    var fromDate, toDate;
    fromDate = toDate = "";

    if((temp = getTag(name_pre + "fromDate").value) != "" && temp != fieldDefValues[name_pre][name_pre + "fromDate"] && temp) {
      fromDate = temp.split("/")[2] + temp.split("/")[0] + temp.split("/")[1];
    }

    if((temp = getTag(name_pre + "toDate").value) != "" && temp != fieldDefValues[name_pre][name_pre + "toDate"] && temp) {
      toDate = temp.split("/")[2] + temp.split("/")[0] + temp.split("/")[1];
    }

    if(fromDate.length == 8) {
      params += "&date=@" + fromDate + "_";
      if(toDate.length != 8 || toDate < fromDate) {
        params += "x";
      }
    }

    if(toDate.length == 8 && (fromDate.length != 8 || toDate >= fromDate)) {
      if(fromDate.length != 8) {
        params += "&date=@_";
      }
      params += toDate;
    }
  }


  // process scope
  if(search_isScope[name_pre] != undefined && search_isScope[name_pre] == true) {
    var scope = "";
    var numChecked = 0;
    for(i = 0; i < (temp = document.getElementsByName(name_pre + "scope")).length; i++) {
      if(temp[i].checked) { // checked value
        numChecked++;
        scope += (scope == "" ? "&scope=" : ",") + temp[i].value;
      }
    }
    if(numChecked == temp.length) { // all checked, don't need to individualize
      scope = "";
    }
    params += scope;
  }


  params = (params != "" ? "?" + params.substring(1) : "");

  window.location = search_url[name_pre] + params;
}



/* Paging function */
var pl_pageSelected = [];
var delimiter = "";
function pl_changePage(sectionName2, pageNum, numPages, sectionOid) {
  if(pl_pageSelected[sectionName2] == undefined || pl_pageSelected[sectionName2] == false) { // initialize
    pl_pageSelected[sectionName2] = 1;
  }
  var previousPage = pl_pageSelected[sectionName2];
  var previousPage2 = (previousPage == -1 ? "ShowAll" : previousPage);
  var isSearchPaging = (sectionOid != undefined);
  var oidParam = (isSearchPaging ? ", \"" + sectionOid + "\"" : "");

  if(previousPage2 != pageNum) { // make sure we are doing a page change
    if(pageNum != "ShowAll" &&
        (pageNum == "Previous" && pl_pageSelected[sectionName2] > 1) ||
        (pageNum == "Next" && pl_pageSelected[sectionName2] < numPages) ||
        (pageNum >= 1 && pageNum <= numPages && pageNum != pl_pageSelected[sectionName2])
      ) { // changing pages

      if(previousPage != -1) { // not coming from a "view all"
        // revert old page
        getTag(sectionName2 + "Page" + pl_pageSelected[sectionName2]).innerHTML =
          "<a href='javascript:pl_changePage(\"" + sectionName2 + "\", " + pl_pageSelected[sectionName2] + ", " + numPages + oidParam + ");'>" + delimiter + pl_pageSelected[sectionName2] + delimiter + "</a>";
        if(!isSearchPaging) {
          getTag(sectionName2 + "PageContent" + pl_pageSelected[sectionName2]).style.display = "none";
        }


        // new page, determine which
        pl_pageSelected[sectionName2] =
          (pageNum == "Previous" ? (pl_pageSelected[sectionName2] - 1) :
            (pageNum == "Next" ? (pl_pageSelected[sectionName2] + 1) :
            pageNum)
          );
        getTag(sectionName2 + "Page" + pl_pageSelected[sectionName2]).innerHTML =
          "<a href='javascript:pl_changePage(\"" + sectionName2 + "\", " + pl_pageSelected[sectionName2] + ", " + numPages + oidParam + ");' class='active'>" + delimiter + pl_pageSelected[sectionName2] + delimiter + "</a>";
        if(!isSearchPaging) {
          getTag(sectionName2 + "PageContent" + pl_pageSelected[sectionName2]).style.display = "";
        }
      }
      else { // coming from a "view all", always a specific page number
        /*if(isSearchPaging) {
          WOSWebSearch.hideAll( sectionName2, sectionOid );
        }*/

        getTag(sectionName2 + "ShowAll").innerHTML =
          "<a href='javascript:pl_changePage(\"" + sectionName2 + "\", \"ShowAll\", " + numPages + oidParam + ");'>View All &gt;&gt;</a>";
        pl_pageSelected[sectionName2] = pageNum;
        for(var i = 1; i <= numPages; i++) {
          if(i == pageNum) { // unlink current page
            getTag(sectionName2 + "Page" + i).innerHTML =
              "<a href='javascript:pl_changePage(\"" + sectionName2 + "\", " + i + ", " + numPages + oidParam + ");' class='active'>" + delimiter + i + delimiter + "</a>";
          }
          else { // hide page
            if(!isSearchPaging) {
              getTag(sectionName2 + "PageContent" + i).style.display = "none";
            }
          }
        }
      }

      // load new page
      if(isSearchPaging) {
        WOSWebSearch.pageChanged( pl_pageSelected[sectionName2], sectionName2, sectionOid );
      }

      // Fix previous/next links
      if(pl_pageSelected[sectionName2] <= 1) { // no more previous
        getTag(sectionName2 + "Previous").innerHTML =
          "&lt;&lt; Previous" + delimiter;
      }
      else if((previousPage == -1 || previousPage <= 1) && pl_pageSelected[sectionName2] > 1) { // make previous a link
        getTag(sectionName2 + "Previous").innerHTML =
          "<a href='javascript:pl_changePage(\"" + sectionName2 + "\", \"Previous\", " + numPages + oidParam + ");'>&lt;&lt; Previous</a>" + delimiter;
      }

      if(pl_pageSelected[sectionName2] >= numPages) { // no more next
        getTag(sectionName2 + "Next").innerHTML =
          delimiter + "Next &gt;&gt;";
      }
      else if((previousPage == -1 || previousPage >= numPages) && pl_pageSelected[sectionName2] < numPages) { // make next a link
        getTag(sectionName2 + "Next").innerHTML =
          delimiter + "<a href='javascript:pl_changePage(\"" + sectionName2 + "\", \"Next\", " + numPages + oidParam + ");'>Next &gt;&gt;</a>";
      }
    }
    else if(pageNum == "ShowAll") { // show all pages and unlink
      for(var i = 1; i <= numPages; i++) {
        if(i == previousPage) { // restore linkage to current page
          getTag(sectionName2 + "Page" + i).innerHTML =
            "<a href='javascript:pl_changePage(\"" + sectionName2 + "\", " + i + ", " + numPages + oidParam + ");'>" + delimiter + i + delimiter + "</a>";
        }
        else { // display page
          if(!isSearchPaging) {
            getTag(sectionName2 + "PageContent" + i).style.display = "";
          }
        }
      }

      // load all pages
      if(isSearchPaging) {
        WOSWebSearch.showAll( sectionName2, sectionOid );
      }

      getTag(sectionName2 + "Previous").innerHTML =
        "&lt;&lt; Previous" + delimiter;
      getTag(sectionName2 + "Next").innerHTML =
        delimiter + "Next &gt;&gt;";
      getTag(sectionName2 + "ShowAll").innerHTML =
        "<a href='javascript:pl_changePage(\"" + sectionName2 + "\", \"ShowAll\", " + numPages + oidParam + ");' class='active'>View All &gt;&gt;</a>";
      pl_pageSelected[sectionName2] = -1; // flag that we are showing all
    }
  }
}


/* Careers tc@work functions */
function showTcAtWork(id)
{
  getTag("tcAtWork" + id).style.display = "";
}
function hideTcAtWork(id)
{
  getTag("tcAtWork" + id).style.display = "none";
}


/* Check if image is loaded */
function isImageLoaded(img)
{
  if(!img.complete) { // some browsers have built-in load check
    return false;
  }
  if(typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) { // if fail, either should be 0
    return false;
  }
  // No other way of checking: assume it's ok.
  return true;
}
