// Functions that get used on more than one page go in this file, any functions for the home page need to be included in main-home.js.

//	primaryNav dropdown menu for IE versions <IE7, not needed for other browsers

  sfHover = function() {
  	var sfEls = document.getElementById("primaryNav").getElementsByTagName("LI");
  	for (var i=0; i<sfEls.length; i++) {
  		sfEls[i].onmouseover=function() {
  			this.className+=" sfhover";
  		}
  		sfEls[i].onmouseout=function() {
  			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
  		}
  	}
  }
  if (window.attachEvent) window.attachEvent("onload", sfHover);

//	used to show/hide block elements on the page

function toggle_visibility(display_block)
{
	if (document.getElementById(display_block).style.display == "block") {
		document.getElementById(display_block).style.display="none";
	}
	else {
		document.getElementById(display_block).style.display = "block";
	}
}

//	For general pop-up windows (be sure to pass the window width & height in onclick call)

function openWindow(url, name, width, height) {
	var str = "height=" + height + ",innerHeight=" + height;
       str += ",width=" + width + ",innerWidth=" + width;
            if (window.screen) {
                        var ah = screen.availHeight;
                        var aw = screen.availWidth;
                        var xc = (aw - width) / 2;
                        var yc = (ah - height) / 2;
                        str += ",left=" + xc + ",screenX=" + xc;
                        str += ",top=" + yc + ",screenY=" + yc;
            }
            window.open(url, name, str);
            window_handle = window.open(url, name);
            window_handle.focus();
}

// Allows multiple functions to load

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

//	Opens PDF links in a new window instead of "target="blank", which doesn't validate under XHTML Strict. 

function doPopups()
{
 if (!document.getElementsByTagName) return false;
 var links = document.getElementsByTagName("a");
 for (var i=0; i < links.length; i++) {
  if (links[i].href.indexOf('.pdf') !== -1) {
   links[i].onclick =
    function() {
     window.open(this.href,'popper','resizable,scrollbars');
     return false;
    }
  }
 }
}
// Opens external link in a new window 

function externalLink(url) {
     //window.open(this.href,'external','resizable,scrollbars');
	 window.open(url,'external','resizable=1,scrollbars=1');
     return false;
}

addLoadEvent(doPopups);

//	Table Stripe Function - table rows are defined by even/odd classes 

	// this function is needed to work around a bug in IE related to element attributes

  function hasClass(obj) {
     var result = false;
     if (obj.getAttributeNode("class") != null) {
         result = obj.getAttributeNode("class").value;
     }
     return result;
  }   

 function stripe(id) {

    // flag used to keep track of whether the current row is odd or even 
    var even = false;
  
    // default colors for striped rows, can be overridden via class attributes
    var evenColor = arguments[1] ? arguments[1] : "#fff";
    var oddColor = arguments[2] ? arguments[2] : "#e8edf1";
  
    // obtain a reference to the desired table if no such table exists, abort
    var table = document.getElementById(id);
    if (! table) { return; }
    
    // tables can have more than one tbody element, so we'll have to get the list of child
    var tbodies = table.getElementsByTagName("tbody");

    // and iterate through them...
    for (var h = 0; h < tbodies.length; h++) {
    
     // find all the &lt;tr&gt; elements... 
      var trs = tbodies[h].getElementsByTagName("tr");
      
      // ... and iterate through them
      for (var i = 0; i < trs.length; i++) {

        // avoid rows that have a class attribute, or backgroundColor style
        if (! hasClass(trs[i]) &&
            ! trs[i].style.backgroundColor) {
 		  
          // get all the cells in this row...
          var tds = trs[i].getElementsByTagName("td");
        
          // and iterate through them...
          for (var j = 0; j < tds.length; j++) {
        
            var mytd = tds[j];

            // avoid cells that have a class attribute, or backgroundColor style
            if (! hasClass(mytd) &&
                ! mytd.style.backgroundColor) {
        
              mytd.style.backgroundColor =
                even ? evenColor : oddColor;
            
            }
          }
        }
        // flip from odd to even, or vice-versa
        even =  ! even;
      }
    }
  }
	
//	Function for checking SSO status. 
function getCookie(name)
{
        var dc = document.cookie;
        var prefix = name + "=";
        var begin = dc.indexOf("; " + prefix);
        if (begin == -1)
        {
                begin = dc.indexOf(prefix);
                if (begin != 0) return null;
        } else
                begin += 2;
        var end = document.cookie.indexOf(";", begin);
        if (end == -1)
                end = dc.length;
        return unescape(dc.substring(begin + prefix.length, end));
}