// JavaScript Document

// RickR - BEGIN Code to be commented. ****************************************

var globalTimerId; // This is a global variable to handle timer opaque ids.;

function BrowserInfo(userAgentString, platform)
{
    this.family;            
    this.name;              
    this.browserVersion;    
    this.geckoVersion;      
    this.aolVersion;        
    
    // BEGIN Browser Detection *************************************************
    if(!userAgentString)
    {
        userAgentString = navigator.userAgent; // Allows testing of different user agent strings.
    }
    if(!platform)
    {
        platform = navigator.platform; // Allows testing of different platform strings.
    }
    
    
    // Check for Internet Explorer.  See http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/overview/aboutuseragent.asp
    var patternMSIE = /MSIE\b\s+\b(\d+\.\d+)\b/i;
    var aMSIEMatches = userAgentString.match(patternMSIE);
    if(aMSIEMatches != null)
    {
        this.family = (platform.toLowerCase().indexOf("win") > -1) ? "WinIE" : "OtherIE";
        this.name = this.family;
        this.browserVersion = aMSIEMatches[1];
    }
    
    // Check for Opera. See http://www.opera.com/support/search/supsearch.dml?index=570
    // Check should come after IE because Opera sometimes likes to identify itself as IE.
    var patternOpera = /(Opera\b\s+\b|Opera\/)(\d+\.\d+)\b/i;
    var aOperaMatches = userAgentString.match(patternOpera);
    if(aOperaMatches != null)
    {
        this.family = "Opera";
        this.name = this.family;
        this.browserVersion = aOperaMatches[2];
    }
    
    // Check for Gecko.  See http://www.mozilla.org/build/revised-user-agent-strings.html
    var patternGecko = /Gecko\/(\d+)\b/i;
    var aGeckoMatches = userAgentString.match(patternGecko);
    if(aGeckoMatches != null)
    {
        this.family = "Gecko";
        this.geckoVersion = aGeckoMatches[1];
    }

    // Check for Firefox and Netscape.
    var patternGeckoFamily = /(Firefox|Netscape\d?)\/(\d+\.\d+)\b/i;
    var aGeckoFamilyMatches = userAgentString.match(patternGeckoFamily);
    if(aGeckoFamilyMatches != null)
    {
        this.name = aGeckoFamilyMatches[1];
        this.browserVersion = aGeckoFamilyMatches[2];
    }

    // Check for KHTML after Gecko.
    // Some KHTML browsers like Safari will claim to be Gecko.
    var patternKHTML = /KHTML/i;
    var aKHTMLMatches = userAgentString.match(patternKHTML);
    if(aKHTMLMatches != null)
    {
        this.family = "KHTML";
    }

    // Check for Safari.  See http://developer.apple.com/internet/safari/faq.html#anchor2
    // Safari is KHTML, but not all KHTML browsers are Safari (e.g. Konqueror).
    var patternSafari = /Safari\/(\d+\.\d+|\d+)\b/i;
    var aSafariMatches = userAgentString.match(patternSafari);
    if(aSafariMatches)
    {
        this.name = "Safari";
        this.browserVersion = aSafariMatches[1];
    }
    
    // Check for AOL.  See http://webmaster.info.aol.com/detection.html
    var patternAOL = /(AOL\b\s+\b|AOL\/)(\d+\.\d+)\b/i;
    var aAOLMatches = userAgentString.match(patternAOL);
    if(aAOLMatches != null)
    {
        this.aolVersion = aAOLMatches[2];
    }

    // END Browser Detection *************************************************
    
    // Geometry object provides methods for getting info on the window's position on the screen,
    // window and document size, and document's scrollbars.
    this.geometry = {};
    // Window Position on the Screen
    this.geometry.getPosTop = function(){return testForFeature([window.screenTop, window.screenY])}
    this.geometry.getPosLeft = function(){return testForFeature([window.screenLeft, window.screenX])}
    // Window Size
    this.geometry.getWindowHeight = function(){return testForFeature([window.outerHeight])}
    this.geometry.getWindowWidth = function(){return testForFeature([window.outerWidth])}
    // Viewport Size within the Window
    this.geometry.getViewportHeight = function(){return testForFeature([window.innerHeight, document.documentElement.clientHeight, document.body.clientHeight])}
    this.geometry.getViewportWidth = function(){return testForFeature([window.innerWidth, document.documentElement.clientWidth, document.body.clientWidth])}
    // Scrollbars on the Document within the Viewport
    this.geometry.getScrollTop = function(){return testForFeature([window.pageYOffset, document.documentElement.scrollTop, document.body.scrollTop])}
    this.geometry.getScrollLeft = function(){return testForFeature([window.pageXOffset, document.documentElement.scrollLeft, document.body.scrollLeft])}
}

function DynamicSelect(DOM_Id, valueSeparator)
{
    function getSelectNode(DOM_Id) 
    {
        if(document.getElementById)
        {
            var oNode = document.getElementById(DOM_Id); // Returns null if element doesn't exist.
            if(oNode == null)// TODO: Do we still need this?
            {
                oNode = document.createElement("select");
            }
        }
        return oNode;
    }
    
    this.selectElement = getSelectNode(DOM_Id);
    this.selectElementOriginal = this.selectElement.cloneNode(true); // Keep a backup that does not reference the DOM node.
    this.selectElement.valueSeparator = (valueSeparator == null ? "$" : valueSeparator);
    this.selectElement.relatedSelects = new Array();
    this.selectElement.relatedSelects.refresh = function(e)
    {
        var oOriginalOption;
        for(var i = 0; i < this.relatedSelects.length; i++) // Loop through the relatedSelect elements.
        {
            this.relatedSelects[i].selectElement.options.length = 0; // Clear the list of options.
            for(var j = 0; j < this.relatedSelects[i].selectElementOriginal.options.length; j++) // Loop through the original options for the select.
            {
                oOriginalOption = this.relatedSelects[i].selectElementOriginal.options[j];
                // Returns TRUE if the original select's value == ""
                // OR (Both of these conditions have to be met for this to return TRUE)
                //          1. The original select's value contains the parent select's current selectedIndex value plus the separator (e.g. original has "BI$" and parent hs "BI").
                //      AND 2. The parent select's value != "" (Otherwise the entire original select is loaded).
                if((oOriginalOption.value.length == 0) || ((oOriginalOption.value.indexOf(this.options[this.selectedIndex].value + this.valueSeparator) > -1) && (this.options[this.selectedIndex].value.length > 0)))
                {
                    this.relatedSelects[i].selectElement.options[this.relatedSelects[i].selectElement.options.length] = new Option(oOriginalOption.text, oOriginalOption.value, oOriginalOption.defaultSelected);
                }
            }
        }
    }
    this.init = function(ArrayOfSelectedValues)
    {
        this.selectElement.relatedSelects.refresh.call(this.selectElement); // TODO: Fix for MSIE 5.01.  Works in MSIE 5.5+.
        // function.call() does not exist in MSIE 5.01 or earlier, but we could use this.selectElement.onchange or something like that.
        
        if(ArrayOfSelectedValues != null)
        {
            // Loop through related select objects and select the correct value.
            for(var i = 0; i < this.selectElement.relatedSelects.length; i++) // Loop through the relatedSelect elements.
            {
                for(var j = 0; j < this.selectElement.relatedSelects[i].selectElement.options.length; j++) // Loop through the options for the select.
                {
                    for(var k = 0; k < ArrayOfSelectedValues.length; k++) // Loop through array to match values.
                    {
                        if(this.selectElement.relatedSelects[i].selectElement.options[j].value == ArrayOfSelectedValues[k])
                        {
                            this.selectElement.relatedSelects[i].selectElement.options[j].selected = true;
                        }    
                    }
                }
            }
        }
    }
    
    addEventHandler(this.selectElement, "change", this.selectElement.relatedSelects.refresh, false, false);
}

function DynamicImage(DomId, HoverImgURL, ActiveImgURL)
{
    this.element = document.getElementById(DomId);
    if(this.element)
    {
        if(this.element.tagName.toLowerCase() == "img")
        {
            this.element.srcOriginal = this.element.src;
            this.element.srcHover = HoverImgURL ? HoverImgURL : this.element.srcOriginal;
            this.element.srcActive = ActiveImgURL ? ActiveImgURL : this.element.srcHover;
        }
        else
       {
            throw("Only img tags can be used with Dynamic Image objects.");
        }
    }
    this.hover = function(e)
    {
        var node = getCurrentTarget(e);
        if(node){node.src = this.srcHover;}
    }
    this.active = function(e)
    {
        var node = getCurrentTarget(e);
        if(node){node.src = this.srcActive;}
    }
    this.restore = function(e)
    {   
        var node = getCurrentTarget(e);
        if(node){node.src = this.srcOriginal;}
    }
    this.preload = function(e)
    {
        var node = getCurrentTarget(e);
        PreloadImages([HoverImgURL, ActiveImgURL]);
    }
    this.init = function(e)
    {
        if(this.element)
        {
            // Using IE's attachEvent, which causes problems with this keyword.
            addEventHandler(this.element, "mouseover", this.hover, false, false);
            addEventHandler(this.element, "mousedown", this.active, false, false);
            addEventHandler(this.element, "mouseout", this.restore, false, false);
        }
        addEventHandler(window, "load", this.preload, false, true);
    }
    this.init();
}

function DynamicMenu(parentId, menuId, menuSet, hoverUrl, activeUrl)
{

    // First 3 Arguments are required.
    if(!(parentId && menuId))
    {
        return;
    }
    
    // Get the parent element.
    this.element = document.getElementById(parentId);
    this.element.originalImageUrl = (this.element.tagName.toLowerCase() == "img" ? this.element.src : this.element.style.backgroundImage);
    this.element.hoverImageUrl = (hoverUrl ? hoverUrl : this.element.originalImageUrl);
    this.element.activeImageUrl = (activeUrl ? hoverUrl : this.element.originalImageUrl);
    
    // Get the menu element.
    this.element.menuElement = document.getElementById(menuId);
    this.element.menuElement.menuSet = (menuSet ? menuSet : [this.element.menuElement.id]);
    
    // Share properties.
    this.element.menuSet = this.element.menuElement.menuSet;
    
    this.hover = function(e)
    {
        DynamicMenu.restoreMenus(this.menuSet);
        if(this.tagName.toLowerCase() == "img")
        {
            this.src = this.hoverImageUrl;
        }
        else
        {
            ; // TODO: Code for handling backgroundImage.
        }
        window.clearTimeout(globalTimerId); this.menuElement.style.visibility = "visible";
    }

    this.restore = function(e)
    {
        if(this.tagName.toLowerCase() == "img")
        {
            this.src = this.originalImageUrl;
        }
        else
        {
            ; // TODO: Code for handling backgroundImage.        
        }
        var menuSetArray = this.menuSet;
        globalTimerId = window.setTimeout(function(){DynamicMenu.restoreMenus(menuSetArray)}, 100);
    }
   
    this.keepInView = function(e)
    {
         window.clearTimeout(globalTimerId);
    }
   
    this.restoreMenus = function(e)
    {
        var menuSetArray = this.menuSet;
        globalTimerId = window.setTimeout(function(){DynamicMenu.restoreMenus(menuSetArray)}, 100);
    }

    this.init = function(e)
    {
        // TODO: Change to avoid repeat loading.
        PreloadImages([this.element.hoverImageUrl, this.element.activeImageUrl]);
        addEventHandler(this.element, "mouseover", this.hover, false, false);
        addEventHandler(this.element, "mouseout", this.restore, false, false);
        addEventHandler(this.element.menuElement, "mouseover", this.keepInView, false, false);
        addEventHandler(this.element.menuElement, "mouseout", this.restoreMenus, false, false);
      
    }
}    

DynamicMenu.restoreMenus = function(DomIdArray)
{
    var node;
    for(var i = 0; i < DomIdArray.length; i++)
    {
        node = document.getElementById(DomIdArray[i]);
        node.style.visibility = "hidden";
    }
}

function PreloadImages(URLArray)
{
    var oImage = new Image();
    for(var i = 0; i < URLArray.length; i++)
    {
        oImage.src = URLArray[i];
    }
}

// HACK: IE 5 - 7
// Helper Functions for Events
function stopEventBubble(e)
{
    if(window.addEventListener)
    {
        e.stopPropagation();
    }
    else if(window.attachEvent)
    {
        e = e ? e : window.event;
        e.cancelBubble = true;
    }
}

function preventEventDefault(e)
{
    if(window.addEventListener)
    {
        e.preventDefault();
    }
    else if(window.attachEvent)
    {
        e.returnValue = false;
    }
}

// Function gets around "this" keyword problem with attachEvent.
// DOESN'T WORK FOR BUBBLED EVENTS!
// TODO: Deprecate this.
function getCurrentTarget(e)
{
    var node;
    e = e ? e : window.event;
    if(e.currentTarget)
    {
        node = e.currentTarget;
    }
    else if(e.srcElement)
    {
        node = e.srcElement;
    }
    return node;
}

function addEventHandler(node, eventName, handler, capturing, useAttachEvent)
{
    // TODO: Enforce arguments.
    if(window.addEventListener)
    {
        node.addEventListener(eventName, handler, capturing);
    }
    else 
    {
        eventName = "on" + eventName;
        if(useAttachEvent && window.attachEvent)
        {
            node.attachEvent(eventName, handler);
        }
        else
        {
            var evalCode = "node." + eventName + " = " + handler + ";";
            eval(evalCode);        
        }
    }
}

// *****************************************************************************

// Checks for the existence of an object member among an array of similar
// objects and returns the first value.  This is used to check for cross-browser
// support of a feature.
function testForFeature(arrayOfObjects)
{
    for(var i = 0; i < arrayOfObjects.length; i++)
    {
        if(arrayOfObjects[i])
        {
            return arrayOfObjects[i];
        }
    }
}

ExternalLinkManager = new Object();
ExternalLinkManager.init = function(e)
{
    for(var i = 0; i < document.links.length; i++)
    {
        if(document.links[i].className == "ExternalLink")
        {
            addEventHandler(document.links[i], "click", ExternalLinkManager.LinkHandler, false, false); 
        }
    }
}

ExternalLinkManager.LinkHandler = function(e)
{
    var node = this;
    ExternalLinkManager.FollowLink(node.href);
    if(window.addEventListener)
    {
        preventEventDefault(e);
    }
    return false;
}

ExternalLinkManager.FollowLink = function(url)
{
    var ExternalLinkWindow = window.open(url);
}

function hack_ie6_firstChildCSSSelector(e)
{
    var Browser = new BrowserInfo();
    if(Browser.name == "WinIE" && Browser.browserVersion < 7)
    {
        /*
        #Navigation1 h3:first-child
        {
            margin-top: 0em;
            border-top-style: none;
            border-top-width: 0px;
            padding-top: 0em;
        }
        */
        var eNavigation1 = document.getElementById("Navigation1");
        if(eNavigation1 != null)
        {
            var eH3s = eNavigation1.getElementsByTagName("h3");
            if(eH3s[0] != null)
            {
                eH3s[0].style.marginTop = "0em";
                eH3s[0].style.borderTopStyle = "none";
                eH3s[0].style.borderTopWidth = "0px";
                eH3s[0].style.paddingTop = "0em";
            }
        }
        
        /*        
        #HighlightedResultsContainer div:first-child, #ResultsListing div:first-child
        {
            border-top-style: none;
        }
        */
        var eHighlightedResultsContainer = document.getElementById("HighlightedResultsContainer");
        if(eHighlightedResultsContainer != null)
        {
            var eDIVs = eHighlightedResultsContainer.getElementsByTagName("div");
            if(eDIVs[0] != null)
            {
                eDIVs[0].style.borderTopStyle = "none";
            }
        }
        var eResultsListing = document.getElementById("ResultsListing");
        if(eResultsListing != null)
        {
            var eDIVs = eResultsListing.getElementsByTagName("div");
            if(eDIVs[0] != null)
            {
                eDIVs[0].style.borderTopStyle = "none";
            }
        }
        /*
        #Navigation2 #Nav2Sec1 div:first-child
        {
            border-top-width: 0px;
        }
        */
        var eNav2Sec1 = document.getElementById("Nav2Sec1");
        if(eNav2Sec1 != null)
        {
            var eDIVs = eNav2Sec1.getElementsByTagName("div");
            if(eDIVs[0] != null)
            {
                eDIVs[0].style.borderTopWidth = "0px";
            }
        }
               
        /*
        div.AddressList tr:first-child td
        {
            border-top-width: 0px;
        }
        */
        var eDIVs = document.getElementsByTagName("div");
        for(var i = 0; i < eDIVs.length; i++)
        {
            if(eDIVs[i].className.search(/\bAddressList\b/) > -1)
            {
                var eTRs = eDIVs[i].getElementsByTagName("tr");
                if(eTRs[0] != null)
                {
                    var eTDs = eTRs[0].getElementsByTagName("td");
                    for(var j = 0; j < eTDs.length; j++)
                    {
                        eTDs[j].style.borderTopWidth = "0px";
                    }
                }
            }
        }
    }
}


// Global Event Handlers
addEventHandler(window, "load", ExternalLinkManager.init, false, true);
addEventHandler(window, "load", hack_ie6_firstChildCSSSelector, false, true);

// RickR - END Code to be commented. *******************************************

// TODO: Get rid of these Dreamweaver Functions

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

// TODO: Custom Functions

function open_window(url){
  window.open(url,"new","toolbar=1,location=1,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=640,height=480");
 }


 
if (window.createPopup && document.compatMode &&  
document.compatMode=="CSS1Compat") 
{ 
  document.onreadystatechange = onresize = function fixIE6AbsPos() 
  { 
    if (!document.body) return; 
    if (document.body.style.margin != "0px") document.body.style.margin = 0; 
    onresize = null; 
    document.body.style.height = 0; 
    setTimeout(function(){ document.body.style.height =  
document.documentElement.scrollHeight+'px'; }, 1); 
    setTimeout(function(){ onresize = fixIE6AbsPos; }, 100); 
  } 
} 

function entsub(myform)
 {
  if (window.event && window.event.keyCode == 13)
   myform.submit();
  else
    return true;
 }
 
 

function ElementContent(id,content,btnName)
{
    
    document.getElementById(id).value = content;
    cmCreateManualLinkClickTag('/search/results.jsp?cm_sp=Search-_-PopularSearchTerms-_-'+content);
    document.form_search.submit();
}
function ShowHoverImage()
{
    var HoverImage=document.getElementById("SiteNav1");
    var eImgs = HoverImage.getElementsByTagName("img");
    if(eImgs[0] != null)
    {
         eImgs[0].src = "/media/generic_site_content/images/popularsearch_active.gif";
    }  
    var TabContainer=document.getElementById("menuPopularSearch");
    TabContainer.style.visibility="visible";
   
    
}

function HideHoverImage()
{
    var HoverImage=document.getElementById("SiteNav1");
    var eImgs = null;
    if(HoverImage != null){
        eImgs = HoverImage.getElementsByTagName("img");
        if(eImgs[0] != null)
        {
             eImgs[0].src = "/media/generic_site_content/images/popularsearch_normal.gif";
        }
        var TabContainer=document.getElementById("menuPopularSearch");
        TabContainer.style.visibility="hidden";   
    }
}


function StyleTheLast() 
{
lis=document.getElementById('TopSearchList').getElementsByTagName('li');
n=lis.length-1;
lis[n].id='TopSearchLastLi'
}
