﻿function dropDownReplaceHref(dropDown,qsName,qsEmptyValue)
{
    var qsPairRegEx = new RegExp(qsName + "?\=[^&]*");
    var currentHref = window.location.href;
    var selectedValue = dropDown.options[dropDown.selectedIndex].value;
    var qsPair;
    // determine whether or not the value is the empty value
    // therefore we should clear the query string value
    if(selectedValue==qsEmptyValue)
    {
        qsPair = "";
    }
    else
    {
        qsPair = qsName + "=" + escape(selectedValue);
    }
    // create the target url by modifying the existing one
    var targetHref;
    if(currentHref.match(qsPairRegEx))
    {
        // need to replace it
        targetHref = currentHref.replace(qsPairRegEx, qsPair);
    }
    else if(qsPair != "")
    {
        if(currentHref.match(/\?/))
        {
            targetHref = currentHref + "&" + qsPair;
        }
        else
        {
            targetHref = currentHref + "?" + qsPair;
        }
    }
    
    //strip out id
    qsPairRegEx = new RegExp("id?\=[^&]*");
    if(currentHref.match(qsPairRegEx))
    {
        // need to replace it
        targetHref = currentHref.replace(qsPairRegEx, "");
    }
    
    
    // clean up the url
    targetHref = targetHref.replace(/\?&*$/,"");
    targetHref = targetHref.replace(/&&+/,"&");
    targetHref = targetHref.replace(/\?&&*/,"?");
    window.location.href = targetHref;    
}

// alternate display of the Bios
function showBio() {	
	var trs = document.getElementsByTagName('tr');
	if (trs.length == 0) return;
	for (i = 0; i < trs.length; i++)
	{
		// find the td we want to click on
		if (trs[i].className.match('odd') || trs[i].className.match('even'))
		{
			// set click event
			trs[i].onclick=function()
			{
			    var setActive = false;
			    
                for (j = 0; j < this.cells.length; j++)
                {
                    if (this.cells[j].className.match('twist'))
                    {
				        // find the plus image
				        var oImg = this.cells[j].getElementsByTagName('img');
				        
				        // alternate between plus and minus
				        for (m = 0; m < oImg.length; m++)
				        {
					        oImg[m].src = (oImg[m].src.indexOf('plus') > -1) ? oImg[m].src.replace(/plus(\.[a-z0-9]+)$/i,'minus$1') : oImg[m].src.replace(/minus(\.[a-z0-9]+)$/i,'plus$1');
				        }
				       				        
                        // alternate the active state of the parent row (tr)
			            if (this.className.indexOf(''+'active'+'') > -1 && !setActive)
			            {
		                    var regexActive = /active/;
				            this.className = this.className.replace(regexActive,"");
			            } else {
				            this.className += ' active';
				            setActive = true;
			            }
                    }
                }
                    
			    var oBio = this.nextSibling;
				// because FireFox considers whitepace a node keep looking until we get the actual tr
				while (oBio.nodeType != 1 && oBio.nextSibling)
				{
					oBio = oBio.nextSibling;
				}
				
				// alternate display (IE likes display:block while everyone else likes display:table-row)
				oBio.style.display = (oBio.style.display == 'block' || oBio.style.display == 'table-row') ? 'none' : (document.all && !window.opera && (oBio.style.display != 'block' || oBio.style.display != 'table-row')) ? 'block' : 'table-row';
			}
			
			trs[i].onmouseover = function()
		    {
		        this.className += ' highlight';
		    }
		    
			trs[i].onmouseout = function()
		    {
		        var regexHighlight = /highlight/;
				this.className = this.className.replace(regexHighlight,"");
		    }
		}
	}
}

function openNavigatedBio(bioId)
{
	if (!document.getElementById(bioId)) return;
	var tr = document.getElementById(bioId);
	while(null!=tr && tr.tagName!='TR')
	{
	    tr = tr.parentNode;
	}
	if(!tr) return;
	tr.className += ' active';
	var images = tr.getElementsByTagName('img');	
	if(images.length>0)
	{
		images[0].src=images[0].src.replace(/plus(\.[a-z0-9]+)$/i,'minus$1')
	}
	var oBio = tr.nextSibling;
	while (oBio.nodeType != 1 && oBio.nextSibling)
	{
		oBio = oBio.nextSibling;
	}
	oBio.style.display = (oBio.style.display == 'block' || oBio.style.display == 'table-row') ? 'none' : (document.all && !window.opera && (oBio.style.display != 'block' || oBio.style.display != 'table-row')) ? 'block' : 'table-row';
}
