// Online Specific Scripts

// showId - Show/Hide div's by id/name
//          Have to put an id and a name tag on the <DIV> because getElementsByName
//          works differently with IE and FireFox
function showId(sElem) {
  // Distinguish between object and string
  var eName = (typeof sElem == 'string') ? sElem : sElem.id

  // Set cookie to hold current state: H = hidden, V = visible
  // A cookie with no expire date expires when we close the browser
  var strCookie = sElem.id + "="

  var e = document.getElementsByName(eName)
  for (var i = 0; i < e.length; i++) {
    // DIV's hold the content: change the display property appropriately
    if (e[i].tagName == 'DIV')
      e[i].style.display = (e[i].style.display == 'block') ? 'none' : 'block'

    // SPAN's hold the controls: change the control appropriately
    if (e[i].tagName == 'SPAN') {
      if (e[i].innerHTML.indexOf('[-]') > -1) {
        e[i].innerHTML = '[+]'
        e[i].title = 'Show'
        document.cookie = strCookie + "H"
      }
      else {
        e[i].innerHTML = '[-]'
        e[i].title = 'Hide'
        document.cookie = strCookie + "V"
      }
    }
  }
}

// setDispById - Show/Hide div's by id/name
//          Have to put an id and a name tag on the <DIV> because getElementsByName
//          works differently with IE and FireFox
function setDispById(eName, eState) {
  var e = document.getElementsByName(eName)
  for (var i = 0; i < e.length; i++) {
    // DIV's hold the content: change the display property appropriately
    if (e[i].tagName == 'DIV')
      e[i].style.display = (eState == 'H') ? 'none' : 'block'

    // SPAN's hold the controls: change the control appropriately
    if (e[i].tagName == 'SPAN') {
      if (eState == 'H') {
        e[i].innerHTML = '[+]'
        e[i].title = 'Show'
      }
      else {
        e[i].innerHTML = '[-]'
        e[i].title = 'Hide'
      }
    }
  }
}

// ReadCookie: read the value of the cookie with the specified name.
function ReadCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}

// CMSOU - Content Management System - Outline Update
function cmsou(ccode)
{
  // only when ctrl is pressed
  if (event.ctrlKey) {
    // Open a new window
    winHref = "http://www.onlc.com/scripts/outline_upd.asp?ccode=" + ccode;
    wNew = window.open(winHref, "_blank");
  }
}

// PMCMS - Poor Man's Content Management System
function pmcms(name)
{
  // only when ctrl is pressed
  if (event.ctrlKey) {
    // Get the current file
    var me = document.location.href;

    var ix  = me.lastIndexOf('/');
    if ( ix > -1 ) me = me.substring( ix + 1 );
    if (me.length == 0)
      me = "default.htm"

    var ix  = me.indexOf('?');
    if ( ix > -1 ) me = me.substring( 0, ix );

    // Open a new window
    winHref = "pmcms.asp?srcFile=" + me + "&section=" + name;
    wNew = window.open(winHref, "_blank");
  }
}

// Expand Collapse Div's & Span's by id
function showHide(name)
{
   var obj = (document.getElementById) ? document.getElementById(name) : (document.all) ? document.all(name) : null;
   obj.style.display = (obj.style.display == 'block') ? 'none' : 'block';
}

function showHideCtrl(name)
{
   // only when ctrl is pressed
   if (event.ctrlKey) {
      var obj = (document.getElementById) ? document.getElementById(name) : (document.all) ? document.all(name) : null;
      obj.style.display = (obj.style.display == 'block') ? 'none' : 'block';
   }
}

function addRuleToStyleElement (name, selector, rule)
{
   if (document.styleSheets)
   {
      // IE
      if (document.styleSheets[0].rules)
         document.styleSheets[name].addRule(selector, rule)
         
      // NS 6
      else if (document.styleSheets[0].cssRules)
      {
         var ssLen = document.styleSheets.length - 1
         var csLen = document.styleSheets[ssLen].cssRules.length
         document.styleSheets[ssLen].insertRule(selector + '{ ' + rule + ' } ', csLen);
      }
   }
}

// function for changing stylesheets using document.styleSheets
function setStyleSheet(theme) 
{
   for ( i = 0; i < document.styleSheets.length; i++ ) {
      if ( document.styleSheets[i].title ) {
         document.styleSheets[i].disabled = true;
         if ( document.styleSheets[i].title == theme )
            document.styleSheets[i].disabled = false;
      }
   }
}
