
//Cookies, code borrowed from Netscape
function GetCookie(Name)
{
   var search = Name + "="
   if (document.cookie.length > 0) { // if there are any cookies
      offset = document.cookie.indexOf(search) 
      if (offset != -1) { // if cookie exists 
         offset += search.length 
         // set index of beginning of value
         end = document.cookie.indexOf(";", offset) 
         // set index of end of cookie value
         if (end == -1) 
            end = document.cookie.length
         return unescape(document.cookie.substring(offset, end))
      }}
}
function SetCookie(name, value, days)
{
   var expires = new Date();
   expires.setTime(today.getTime() + 1000*60*60*24*days);
   document.cookie = name + "=" + escape(value)
   + ((days == 0) ? "" : ("; expires=" + expires.toGMTString()))
   + ("localhost","");
}

// today and MeSH are global because used in setcookies and startup
var today = new Date();
if (document.cookie.length==0)
	{
	SetCookie("MeSH","NO",0);
	SetCookie("nagged","NO",0);
	}

// Startup stuff
// Automatically create the "is" object
// Get CGI input variable pairs
var Vars = new Array("Name","Value");
var VarsIndex = 0;
function init()
	{
//	Get CGI input variable pairs
	var i=0;
	var posamp;
	var pair;
	while (i<location.search.length)
		{
		posamp=location.search.indexOf("&", posamp);
		if (posamp==-1){posamp=location.search.length};
		pair=location.search.substring(i+1,posamp);
		Vars[VarsIndex] = new Array("Name","Value")
		Vars[VarsIndex][0] = pair.substring(0,pair.indexOf("="));
		Vars[VarsIndex][1] = pair.substring(pair.indexOf("=")+1);
//No work	alert(Vars[VarsIndex]["Name"]+'\n'+Vars[VarsIndex]["Value"]);
//Works		alert(Vars[VarsIndex][0]+'\n'+Vars[VarsIndex][1]);
		i=posamp;
		posamp=posamp+pair.length
		VarsIndex=VarsIndex+1;
		}
	if (VarsIndex>0)
		{
		document.SEARCHFORM.SearchString.value=unescape(Replacer(Vars[1][1],"+"," "))
		if (Vars[0][1]=="treatment")
			{
			document.SEARCHFORM.filter[4].click();
			}
		if (Vars[0][1]=="physical")
			{
			document.SEARCHFORM.filter[6].click();
			}
		if (Vars[0][1]=="adverse")
			{
			document.SEARCHFORM.filter[10].click();
			}
		}
	if (VarsIndex>2)
		{
		if (Vars[2][1]>0)
			{
			document.SEARCHFORM.limit_pubdate.selectedIndex=Vars[2][1];
			}
		}
	}
//end of startup stuff

function OpenNewWindow(URL,name,dfault)
{
if (name=="MeSH")
	{
	var msg="Enter term to check. ";
	msg+="If this leads to a term with a comma, retype as:\n";
	msg+="     'anemia, sickle cell' -> CHANGE TO -> 'sickle cell anemia'";
	dfault=dfault.toUpperCase();
	var i=6;
	var pos=0;
	while (i>5)
		{
		i=Math.max(dfault.indexOf(" AND ", pos+5)+5,dfault.indexOf(" OR ", pos+4)+4);
		dfault= dfault.substring(pos);
		pos = i;
		}
	dfault=prompt(msg,dfault);
	URL+="http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=mesh&orig_db=mesh&cmd=search&cmd_current=&term="+trim(dfault);
	}
if (dfault==null && name=="MeSH")
	{}
else
	{
	SetCookie("MeSH","NO",0);
	if (name=="HighImpact")
		{NWindow=window.open(URL,name,"scrollbars=yes,menubar=no,toolbar=yes,address=no,status=no,resizable=yes,width=700,height=500");}
	else
		{NWindow=window.open(URL,name,"scrollbars=yes,menubar=no,toolbar=yes,address=no,status=no,resizable=yes,width=500,height=300");}
	}
//if(NWindow==null) {return;};
}

function trim(url)
	{
	if (url==null){url=""};
	var loc="";
	var i=0;
	var pos=0;
	while((i = url.indexOf(" ", pos))!= -1)
		{
		   loc += url.substring(pos,i) + "+";
		   pos = i + 1;
		}
	if(i==-1)
	  loc += url.substring(pos);
	return loc;
	}

function AreYouSure(SEARCHFORM)
{
if (document.SEARCHFORM.SearchString.value.indexOf(" and ")>0 || document.SEARCHFORM.SearchString.value.indexOf(" or ")>0)
	{
	var msg="Excuse me, but...\n";
	msg+="PubMed requires that 'and' and 'or' be capitalized.\n\n"
	msg+="Please change to \"AND\" and \"OR\" and search again.\n"
	//alert(msg)
	//return false;
	}
if (document.SEARCHFORM.SearchString.value.indexOf(" AND ")>0 && document.SEARCHFORM.SearchString.value.indexOf(" AND ")<document.SEARCHFORM.SearchString.value.indexOf(" OR ") && document.SEARCHFORM.SearchString.value.indexOf(")")<0)
	{
	var msg="Excuse me, but you included \"AND\" followed by \"OR\".\n";
	msg+="This is OK, but you should know that PubMed will automatically group your terms as follows:\n\n"
	msg+="(first term AND second term) OR third term\n\n"
	msg+="Click \"OK\" if this is OK, else click \"Cancel\" if you want to rearrange your search terms or use parentheses.\n\n"
	//if (!confirm(msg))
	//	{
	//	return false
	//	}
	}
if (document.SEARCHFORM.limit_letter.checked && document.SEARCHFORM.filter[9].checked)
	{
	var msg="Warning:\n\n";
	msg+="If you are searching for RARE adverse effects, you might want to *unclick* the 'limit to letters' box as many of these may be case reports in a letter to a journal.\n\nClick cancel if you want to change this limit."
	if (!confirm(msg))
		{return false};
	}
	
	if (GetCookie("nagged")!="YES")
	//the following challenges occur one per session
	//truncate (inactivated), filter, MeSH
		{
		var i=0;
		var filtered="";
		while (i<(document.SEARCHFORM.filter.length))
		  {
		  if (document.SEARCHFORM.filter[i].checked) {filtered="yes"};
		  i++;
		  }
		if (filtered!="yes")
		  {
		  var msg="Do you want to focus your search with a filter? Focusing allows validated search strategies to select for best articles.\n\n"
		  msg+="Click \"OK\" if you really do not want to filter.\n"
		  msg+="Otherwise, click \"Cancel\", and then set a filter at the bottom of the search form."
		  if (!confirm(msg)){window.status='Enter search terms...'; return false}
		  }
		if (GetCookie("MeSH")!="NO") //e.g. this is inactivated
		  {
		  var msg="You have not used the MeSH browser to check whether you chose the best terms.\n"
		  msg+="Click \"OK\" if you really do not want to check terms.\n"
		  msg+="Otherwise, click \"Cancel\", and then click \"MeSH browser\"."
		  //if (!confirm(msg)){window.status='Enter search terms...'; return false}
		  }
		if (GetCookie("MeSH")=="YES" && document.SEARCHFORM.SearchString.value.indexOf("*")<0 && document.SEARCHFORM.SearchString.value.indexOf("$")<0)
		  {
		  var msg="Do you want to TRUNCATE any of your search terms?\n";
		  msg+="Truncation sometimes finds more articles by searching for all articles containing words that start with a common root.\n"
		  msg+="This is especially important since you have not used the MeSH browser.\n\n"
		  msg+="Examples:\n"
		  msg+="hypertension should be hypertens*\n"
		  msg+="  In order to find articles with either of the words \"hypertension\" or \"hypertensive\"\n\n"
		  msg+="diabetes should be diabet*\n"
		  msg+="  In order to find articles with either of the words \"diabetes\" or \"diabetic\"\n\n"
		  msg+="Click \"OK\" if you really do not want to truncate.\n"
		  msg+="Otherwise, click \"Cancel\", and truncate search term(s) as needed."
		  if (!confirm(msg)){window.status='Enter search terms...'; return false};
		  }
		if (GetCookie("MeSH")!="YES" && document.SEARCHFORM.SearchString.value.indexOf("*")<0 && document.SEARCHFORM.SearchString.value.indexOf("$")<0)
		  {
		  var terms=ParseTerms("AreYouSure");
		  if (terms.length>0)
			{
			  var msg="Your search line includes \""+terms+"\".\n\n";
			  msg+="Search terms with more than two words double the chance that a search will fail. Unless \""+terms+"\" is a MeSH term, consider either:\n\n"
			  msg+="  shortening each term to two of less words\n"
			  msg+="     OR\n"
			  msg+="  adding \"AND\" between two of the words\n\n"
			  msg+="Click \"OK\" if you really do not want to change your search.\n"
			  msg+="Otherwise, click \"Cancel\", and make change(s) as needed."
			  if (!confirm(msg))
				{
			     	return false
				window.status='Enter search terms...';
				};
			}
		  }
		}
	SetCookie("nagged","YES",0);
	window.status="searching...";
	return true;
}
function ParseTerms(requestor)
	{
	var i=1;
	var pos=0;
	var posand=0;
	var posor=0;
	var SearchString=document.SEARCHFORM.SearchString.value.toUpperCase();
	var result="";
	var term;
	while (i<SearchString.length)
		{
		posand=SearchString.indexOf(" AND ", pos);
		i=posand;
		posor=SearchString.indexOf(" OR ", pos);
		if ((posor>0 && posor<posand) || posand==-1){i=posor};
		if (i==-1){i=SearchString.length};
		term=SearchString.substring(pos,i);
		if (requestor=="MeSH"){result=term};
		if (requestor=="AreYouSure")
			{
			var pos2=term.indexOf(" ",term.indexOf(" ",1)+1)
			if (requestor=="AreYouSure" && pos2>1 && pos2<term.length-1)
				{
				if (result.length==0)
					{
					result=term
					}
					else
					{
					result+="\" and \"" + term
					}
				}
			}
		if (posand==i){pos=i+5}else{pos=i+4};
		}
	return result;
	}

function filt()
	{
	if (document.SEARCHFORM.filter[0].checked)
		{
		document.SEARCHFORM.limit_FULLTEXT.checked=true;
		}
	else	{
		document.SEARCHFORM.limit_FULLTEXT.checked=false;
		}
	}
function Replacer(itxt,OldChars,NewChars)
	{
	if (itxt==null){itxt=""};
	var i=0;
	while (i!=-1)
		{
		i = itxt.indexOf(OldChars, i);
		if (i>-1){itxt=itxt.substring(0,i) + NewChars+itxt.substring(i+OldChars.length)};
		}
	return itxt;
	}
function CitMat()
	{
	var URL;
	//if (document.SEARCHFORM.Proxy.value=="UTHSCSA")
	//	{
	//	URL="http://liblog.uthscsa.edu/cgi-bin/ezpiii.cgi?url="
	//	URL=URL+"http://www.ncbi.nlm.nih.gov/entrez/query/static/citmatch.html"
	//	}
	//else
	//	{
		URL="http://www.ncbi.nlm.nih.gov/entrez/query/static/citmatch.html"
	//	}
	NWindow=window.open(URL,name,"scrollbars=yes,menubar=no,toolbar=yes,address=no,status=no,resizable=yes,width=600,height=400");
	}
function StartProxy()
	{
	var URL;
	if (document.SEARCHFORM.Proxy.value=="UTHSCSA")
		{
		URL="http://clinical.uthscsa.edu.libproxy.uthscsa.edu/pubmed/"
		location.href=URL;
		}
	else
		{
		}
	}
window.onload = init();

