// JavaScript Document
// AJAX use for retrieving Pubmed abstracts

// SEND & RECEIVE REQUEST 
function fetchAbstract(pmid)
{
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null)
	{
		alert ("Your browser does not support AJAX");
		return;
	}
	var url = "http://wurtmanlab.mit.edu/publications/getAbstract.php"; // this script is attached to a root-directory php file (publications.php) .
	url += "?pmid=" + pmid;
	xmlHttp.open("GET",url,true);
	xmlHttp.onreadystatechange=eval("function(){stateChanged('" + pmid + "');}"); // need eval to pass pmid
	xmlHttp.send(null);
	
}

function stateChanged(pmid)
{
	if(xmlHttp.readyState>=1 && xmlHttp.readyState <=3)
	{
		waiting_string = "<div style=\"text-align:center\"><p><img src=\"img/busy.gif\" width=80 height=5\"> - loading from pubmed - <img src=\"img/busy.gif\" width=80 height=5\"></p></div>";
		document.getElementById('pm' + pmid).innerHTML= waiting_string;
	}		
	else if(xmlHttp.readyState==4) // if the request is complete
	{
		// dump the text into the right div
		document.getElementById('pm' + pmid).innerHTML="<hr style=\"width:50%\" /><div class=\"hideAbstract\"><a href=\"javascript:void()\" onClick=\"hideAbstract(" + pmid + ")\">hide</a></div><p><font style=\"font-weight: bold\">Abstract:</font><br />" + xmlHttp.responseText + "</p>";
	}
}

function hideAbstract(pmid)
{
	document.getElementById('pm' + pmid).innerHTML = '';

}
function GetXmlHttpObject()
{
	var xmlHttp=false;
	try {
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		// newer IE
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			//older IE
		} catch (e) {
			xmlHttp = false;
		}
	}
	if (!xmlHttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlHttp = new XMLHttpRequest();
		} catch (e) {
			xmlHttp=false;
		}
	}
	if (!xmlHttp && window.createRequest) {
		try {
			xmlHttp = window.createRequest();
		} catch (e) {
			xmlHttp=false;
		}
	}
	if (!xmlHttp){
		// There is an error creating the object,
		// just as an old browser is being used.
		alert('There was a problem creating the XMLHttpRequest object');
	} 
	return xmlHttp;
}