
/*add and remove event functions from ejohn.org*/
function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}

function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

function showHide(eleId) {

  var ele = document.getElementById(eleId);
  var currstate =  getStyle(ele, "display");
  if(currstate == "none") 
   ele.style.display = "block";
  else
   ele.style.display = "none"; 
}

function staffToggle() {
  showHide('staff');
  var ele = document.getElementById('staffdetails');
  var halfWidth = "345px";
  
  if(ele.style.width == halfWidth) {
   ele.style.width="100%";
   ele = document.getElementById('expandlink1');
   if(ele)
     ele.innerHTML = " show staff list";
   ele = document.getElementById('expandlink2');
   if(ele)
     ele.innerHTML = " show staff list";
   }
  else {
   ele.style.width= halfWidth;
   ele = document.getElementById('expandlink1');
   if(ele)
     ele.innerHTML = " expand";
   ele = document.getElementById('expandlink2');
   if(ele)
     ele.innerHTML = " expand";
     
  }

}

function openWin(addr, wname, wdetails) {

if(!addr || addr == "")
 return;
 
if(wname  && wname != "")
 var winName = wname;
else
 var winName = "win2";
 
if(wdetails && wdetails != "")
 var winDetails = wdetails;
else
 if(winName == "winPDF" || winName == "abstract")
  var winDetails = "width=450,height=550,resizable=yes,scrollbars=yes,menubar=yes,left=5,top=20";
 
 else if(winName == "ttmxSample")
  var winDetails = "width=580,height=520,resizable=yes,scrollbars=yes,toolbar=no,left=5,top=20";
 
else if(winName == "ttmxDemo")
  var winDetails = "width=792,height=550,resizable=yes,scrollbars=yes,toolbar=no,left=5,top=20";

 else
  var winDetails = "width=450,height=550,resizable=yes,scrollbars=yes,toolbar=no,left=5,top=20";
 
window.open(addr,winName,winDetails).focus();

}

function childClose() {
if(window.opener)
 window.opener.focus();
window.close();

}

function openPopup(destination,specialsettings)
{	
	//programmer can over-ride window features with optional 'specialsettings' parameter
	var settings = "";
	var winname = "ttmx";
	if(specialsettings == null || specialsettings == "")
	{
	 var winwidth = 790;
	 var winheight = 580;
	 TopPosition=(screen.availHeight)?(screen.availHeight-winheight)/2:50;
	 settings='width='+winwidth+',height='+winheight+',top='+TopPosition+',left='+(window.screenX+25)+',location=no,directories=no,status=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes';
	}
	else
	 settings = specialsettings;
	 
	Popup = window.open(destination,winname,settings);
	Popup.focus();
}


var displayAreaId = "staffdetails";
var xmlHttp;
function GetXmlHttpObject()
{

  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return xmlHttp;
}

function getPage(pageURL)
{


if (pageURL.length==0)
  {
  document.getElementById(displayAreaId).innerHTML="";
  return;
  }

//we pass in the name as a query-string-- this lets us do the ajax onload for search engines or deep-links.

var pURL = "/staff/" + pageURL.substr(pageURL.indexOf('?')+1) ;

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }

xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",pURL,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById(displayAreaId).innerHTML= xmlHttp.responseText;

//var xmlDoc=xmlHttp.responseXML.documentElement;

//document.getElementById(displayAreaId).innerHTML= xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;

}
}

function loadPerson() {
var person = document.location.search; //if page gets called with /staff/?some_person -- then go get staff/some_person.html
if(person.length > 0)
 getPage(person);

}

//Change Slogan Code Here
//sample call:
//   <script type="text/javascript" src="/scripts/prochange.js">changeSlogans('some-element-id-here');</script>

	var slogans = new Array(
			 "\"Change is not made without inconvenience, even from worse to better.\" &ndash; Richard Hooker"
			,"\"It is in changing that we find purpose.\" &ndash; Heraclitus "
			,"\"To change is difficult.  Not to change is fatal.\" &ndash; Anonymous"
			,"\"People do not resist change. They resist being changed.\" &ndash; Peter Senge"
			,"\"Growth means change.\" &ndash;  Alan D. Wolfelt"
			,"\"Nothing endures but change.\" &ndash; Heraclitus"
			,"\"Don't fear change, embrace it.\" &ndash; Anthony J. D'Angelo"
			,"\"If you never change your mind, why have one\"? &ndash; Edward De Bono"
			,"\"Things do not change, we change.\" &ndash; Henry David Thoreau"
			,"\"Change is not merely necessary to life&mdash;it is life.\" &ndash; Alvin Toffler "
			,"\"You must be the change you wish to see in the world.\" &ndash; Mahatma Gandhi"
			,"\"Most theories are about change, not about changing.\" &ndash;  James O. Prochaska"
			,"\"The only person we can truly change is our self.\" &ndash;  L. Hunter Louins"
			,"\"Humans have an innate capacity to change.\" &ndash; Bob Doppelt"
			,"\"It's not about the sacrifice, it's about the change.\" &ndash; Anonymous"
			,"\"Change precipitates conflict, while conflict engenders change.\" &ndash; Anonymous"
			,"\"Change is a process, not an event.\" &ndash;  James O. Prochaska"
			,"\"People must take responsibility for their own change.\" &ndash;  The Change Companies&trade;"
					
		);
	var sloganCt = Math.floor(Math.random()*(slogans.length+1));
	var sloganLoopId;
	var slogansChanging = true;
	
function stopSlogans(eleId) {
	if(slogansChanging == true){
		slogansChanging = false;
		clearInterval(sloganLoopId);
	}
	else 
	 change1slogan(eleId);
}
function changeSlogans(eleId) {
	var ele = document.getElementById(eleId);
	if(ele) {
		//add onclick to stop changing or allow manual click-through
		ele.onclick=function() {stopSlogans(this.id);}
		change1slogan(eleId);
		sloganLoopId = setInterval("change1slogan('"+eleId+"')", 6200);
		
	}
}
function change1slogan(eleId) {
	var ele = document.getElementById(eleId);
	sloganCt++;
	if(sloganCt >= slogans.length)
	 sloganCt = 0;
	
	ele.innerHTML = slogans[sloganCt];
}
//END SLOGAN CHANGE CODE

//BEGIN RESERACH PAGE PUBLICATION KEYWORD & ABSTRACT CLICK CODE
function trim(s) {
	//generic string trim function
	return s.replace(/^\s+|\s+$/g, "");
	
}

function caseInsensitiveSort(a,b) {
	//for use with generic Array.sort() function
	var x = String(a).toUpperCase();
	var y = String(b).toUpperCase();
	if(x>y)
	 return 1
	else if(x<y)
	 return -1;
	else
	 return 0;
}

function showAllPubs() {
		var ele = document.getElementById('researchpubs');
		if(ele)
		 var allCitations = ele.getElementsByTagName('div');
		if(allCitations) {
			for (var i=0;i<allCitations.length; i++) {
				if(allCitations[i].className.indexOf('citation') >= 0)
			  	allCitations[i].style.display = "";
			}
		ele = document.getElementById('keywordsortinfo');
		if(ele)
		 ele.innerHTML = "";
		ele = document.getElementById('keywordsortinfodefault');
		if(ele)
		 ele.style.display = "";
		
		}
}

function allAbstracts(action) {
	//example calls:
	//		allAbstracts('show');
	//		allAbstracts('hide');
	var ele = document.getElementById('researchpubs');
	if(ele)
	 var allSpans = ele.getElementsByTagName('span');
	if(allSpans) {
	for (var i=0; i<allSpans.length; i++)
	  if(allSpans[i].className.indexOf('abstract') >= 0)
		  if(action == "show")
			allSpans[i].style.display = "block";
		  else if(action == "hide")
			allSpans[i].style.display = "none";
	}//end if allspans
	
	//outside the loop, we'll show/hide the proper link for usability
	if(action == "show") {
		ele = document.getElementById('showAbstractsLink');
		if(ele)
		 ele.style.display = "none";
		ele = document.getElementById('hideAbstractsLink');
		if(ele)
		 ele.style.display = "inline";
	}
	else if(action == "hide") {
		ele = document.getElementById('showAbstractsLink');
		if(ele)
		 ele.style.display = "";
		ele = document.getElementById('hideAbstractsLink');
		if(ele)
		 ele.style.display = "none";	
	}
	
	
}

function showOnlyPubs(keyword) {
	var allPubs = document.getElementById('researchpubs');
	var allKeywords = allPubs.getElementsByTagName('span');
	for (var i=0;i<allKeywords.length; i++) {
		if(allKeywords[i].className.indexOf('keywords') >= 0) {
		 	if(allKeywords[i].innerHTML.indexOf(keyword) < 0)
				allKeywords[i].parentNode.style.display = "none";
			else
				allKeywords[i].parentNode.style.display = "";
			}
	}
	//tell user what they clicked
	document.getElementById('keywordsortinfo').innerHTML = "\"" + keyword + "\" publications <a href=\"#\" onclick=\"showAllPubs();return false;\">show all</a>";
	document.getElementById('keywordsortinfodefault').style.display="none";
}


function gatherPubKeywords() {
	var allPubs = document.getElementById('researchpubs');
	var allKeywords = allPubs.getElementsByTagName('span');
	var kwdelim = "/";
	var keywords = new Array();
	for (var i=0;i<allKeywords.length; i++) {
		if(allKeywords[i].className.indexOf('keywords') >= 0) {
		 //each span's first 10 chars start with "Keywords: ", so strip that off the front, then trim the results and convert to an array
		 kw = trim(allKeywords[i].innerHTML.substr(10)).split(kwdelim);
		 			 
		 //now loop through all the keywords and push them onto our big array of keywords
		 for(var j=0;j<kw.length;j++)
		  keywords.push(kw[j]);
		}
	}
	//now we want to get just the unique vals for our keyword list
	keywords.sort(caseInsensitiveSort);
	var i=0;
	var tmp = "";
	while(i < keywords.length) {
		tmp = keywords[i];
		while(i+1 < keywords.length && tmp == keywords[i+1]) {
		 keywords.splice(i+1,1); //remove next item from the array if it matches our current val
		}
		i++;
	}
	//now we should have a sorted list of unique keywords, write them out
	var kwLinks = "";
	for(i=0;i<keywords.length;i++)
	  kwLinks += "<a class=\"kwfilter\" href=\"#\" onclick=\"showOnlyPubs('" + keywords[i] + "');return false;\">" + keywords[i] + "</a> ";
	
	//now write out our list of clickable keywords to a particular element
	var ele = document.getElementById('keywordlist')
	if(ele)
	 ele.innerHTML = kwLinks;		

} //end gatherKeywords function

//END PUBLICATION KEYWORD & ABSTRACT CLICK CODE

function calculateBMI(ht_ft_Id, ht_in_Id, wt_lbs_Id, resultsId) {
	var height;
	var weight;
	var ele;
	ele = document.getElementById(ht_ft_Id);
	if(ele && !isNaN(ele.value))
	 	height= 12.0 * parseInt(ele.value);
	ele = document.getElementById(ht_in_Id);
	if(ele && !isNaN(ele.value))
		height = height + parseFloat(ele.value);
	ele = document.getElementById(wt_lbs_Id);
	if(ele && !isNaN(ele.value))
		weight = parseFloat(ele.value);
	ele = document.getElementById(resultsId);
	var thebmi = Math.round(weight * 703 * 10 / height / height) / 10;
	if(ele) {
		if(isNaN(thebmi))
			ele.innerHTML = "Unable to calculate-- please check height & weight and try again."
		else
			ele.innerHTML = thebmi;
	}
}
