// 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.
    
// used to zebra stripe table rows

var stripe = function() {

    if (!document.getElementsByTagName || !document.createTextNode) return;
        
        var tableTag = document.getElementsByTagName('table');
            
        for (var j=0; j < tableTag.length; j++) {
            
                var pos = 0;
            var tableClassStr = document.getElementsByTagName('table')[j].className;
          pos = tableClassStr.indexOf('noStripes');  // position of noStripes string
    
            if ( pos < 0 ) {  // cannot find noStripes class

                  var even = true;
            var trs = tableTag[j].getElementsByTagName('tr');      
            for (var i = 0; i < trs.length; i++) {
                if (trs[i].parentNode.nodeName == 'TBODY') {
                                  trs[i].onmouseover = function(){
                        this.className += (!this.className) ? 'ruled' : ' ruled';
                    }
                    trs[i].onmouseout = function(){
                        this.className = this.className.replace(' ruled', '').replace('ruled', '');
                    }
                                                                            
                    if(even)
                        trs[i].className += (!trs[i].className) ? 'even' : ' even';
                                          
                    even = !even;
                }
            } // end of FOR
            } // End of IF condition                
        } // end of BIG FOR loop                        
}
onload = stripe;
    

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

  sfHover = function() {
    if (!document.getElementById("primaryNav")) return false;
    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";
    }
}


//used to show/hide block elements on the page and toggle switch between +/-

function toggle_link_visibility(listId, toggleId) 
{
    toggle_visibility(listId);
    
    var toggleText = document.getElementById(toggleId).innerHTML;
    if (toggleText == '+') {
        document.getElementById(toggleId).innerHTML = '-';
    } else {
        document.getElementById(toggleId).innerHTML = '+';
    }
}



//  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();
                        return false;
}


// 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) {

    // obtain a reference to the desired table if no such table exists, abort
    if (! id) { id = "stripedTable"; }
    var table = document.getElementById(id);
    if (! table) { return; }

    // 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";
    
    // 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;
      }
    }
  }
addLoadEvent(stripe);
*/

//  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));
}



// Function for clearing search box default text
// Edited for Bug 353 so that code clears only Search www.F5.com
function searchBox_clear() {            
    // Commented out for bug 353
    // if (document.searchBoxForm.q.value != "")
    //     document.searchBoxForm.q.value = "";
        
    if (!document.getElementById("searchBox")) 
        return false;
        
    var searchBox = document.getElementById("searchBox");
    if (searchBox.value == "Search www.F5.com") 
        searchBox.value = "";

    return true;
}

    function searchBox_submit() {            
        if ((document.searchBoxForm.q.value != "") && (document.searchBoxForm.q.value == "Search our site"))
            document.searchBoxForm.q.value = "";
            
        document.searchBoxForm.submit();    
    }



//  Function for adding breadcrumb divider in IE7.
function arrowAdd() {
    if (!document.getElementById("breadCrumbs")) return false;
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
    var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
        if (ieversion==7) {   
            var breadLine = document.getElementById("breadCrumbs");
            var item = breadLine.getElementsByTagName("li");
            for (i = 0; i<item.length-1; i++) {
            var pd = document.createTextNode("> ");
            item[i].appendChild(pd);        
            }  
        }
    }
} 
 
// Pre load the following functions when the page loads
addLoadEvent(arrowAdd);

// Retrieve the data from the requested URL and place it into the
// page at the specified document ID
function loadPageData(url, docId) {
    var docElem = document.getElementById(docId); 
    docElem.innerHTML = getPageData(url);
}

//Retrieve the data from the requested URL and return it.
function getPageData(url) {
	var req = false;
	 // branch for native XMLHttpRequest object
	 if(window.XMLHttpRequest && !(window.ActiveXObject)) {
	     try {
	         req = new XMLHttpRequest();
	     } catch(e) {
	         req = false;
	     }
	 // branch for IE/Windows ActiveX version
	 } else if(window.ActiveXObject) {
	     try {
	         req = new ActiveXObject("Msxml2.XMLHTTP");
	     } catch(e) {
	         try {
	             req = new ActiveXObject("Microsoft.XMLHTTP");
	         } catch(e) {
	             req = false;
	         }
	     }
	 }
	 if(req) {
	     req.open("GET", url, false);
	     req.send("");		     
	     return req.responseText;
	 }
}
