function init(theForm) {
if ((navigator.userAgent.indexOf("Safari") > 0) && (navigator.userAgent.indexOf("AppleWebKit\/125.5.5") || navigator.userAgent.indexOf("AppleWebKit\/125.3") || navigator.userAgent.indexOf("AppleWebKit\/125") > 0))
  {
  var labels = document.getElementsByTagName("label");
  for (i = 0; i < labels.length; i++)
    {
    labels[i].addEventListener("click", addLabelFocus, false);
    }
  }  
limitOff('helpbox');
focusLabels();
for (i=0;i<theForm.p_srch.length;i++)
	{	
	if (theForm.p_srch[0].checked==true)
	//climit, catalog is selected
	{
	limitOn('climit');
	limitOff('blimit');
	limitOff('mlimit');
	limitOff('glimit');	
	toggleRadioLabel('c');
	}
	else if (theForm.p_srch[1].checked==true)
	//blimit, photos is selected
	{
	limitOn('blimit');
	limitOff('climit');
	limitOff('mlimit');
	limitOff('glimit');
	toggleRadioLabel('eb');
	}
	else if (theForm.p_srch[2].checked==true)
	//mlimit, finding aids is selected
	{
	limitOn('mlimit');
	limitOff('blimit');
	limitOff('climit');
	limitOff('glimit');
	toggleRadioLabel('cs');
	}
	else if (theForm.p_srch[3].checked==true)
	//glimit, architectural records is selected
	{
	limitOn('glimit');
	limitOff('climit');
	limitOff('blimit');
	limitOff('mlimit');
	toggleRadioLabel('g');
	}
  }
}

function focusLabels() {
	if (!document.getElementsByTagName) {
	return(false);
	}
	var labels = document.getElementsByTagName("label");
	for (var i=0; i<labels.length; i++) {
	  if (!labels[i].attributes['for']) {
	  continue;
	  }
	  labels[i].onclick = function()
	   {
	    var id = this.attributes['for'].nodeValue;
	    //this.getAttribute("for");
	    if (!document.getElementById(id)) {
	    return(false);
	    }
	    var element = document.getElementById(id);
	    element.focus();
	    //alert(element.value);
	    //var whichLabel = 'lbl'+element.value;
	    //alert('Which Label: '+whichLabel);
	    toggleRadioLabel(element.value);
	    }
	  }
	}

function addLabelFocus()
  {
  var item = document.getElementById(this.getAttribute("for"));
  item.focus();
  if (item.getAttribute("type") == "checkbox")
    {
    if (!item["checked"])
      {
      item["checked"] = true;
      }
    else
      {
      item["checked"] = false;
      }
    }
  else if (item.getAttribute("type") == "radio")
    {
    var allRadios = document.getElementsByTagName("input");
    var radios = new Array();
    for (i = 0; i < allRadios.length; i++)
      {
      if (allRadios[i].getAttribute("name") == item.getAttribute("name"))
        {
        radios.push(allRadios[i]);
        }
      }
    for (i = 0; i < radios.length; i++)
      {
      if (radios[i]["checked"] && 
      radios[i].getAttribute("id") != item.getAttribute("id"))
        {
        radios[i]["checked"] = false;
        }
      }
    item["checked"] = true;
    }
  }

function toggleRadioLabel(eValue) {
var theForm = document.forms[0] 
var whichLabel 
var p_srch 
// standards compliant
if (document.getElementById)
{
for (i=0;i<theForm.p_srch.length;i++){
	p_srch=theForm.p_srch[i].value; 
	whichLabel = 'lbl'+p_srch;
	var style2 = document.getElementById(whichLabel).style;
	if (p_srch == eValue) 
	{	
	style2.backgroundColor = "#7985a3";
	style2.color = "#fff";
	} 
	else 
	{	
	style2.backgroundColor = "transparent";
	style2.color = "#000";
	}
	}
}
else if (document.all)
	{
// old IE
for (i=0;i<theForm.p_srch.length;i++){
	p_srch=theForm.p_srch[i].value; 
	whichLabel = 'lbl'+p_srch;
	var style2 = document.all[eValue].style;
	if (p_srch == eValue) 
	{	
	style2.backgroundColor = "#7985a3";
	style2.color = "#fff";
	} 
	else 
	{	
	style2.backgroundColor = "transparent";
	style2.color = "#000";
	}
	}
}
else if (document.layers)
{
// NN4
for (i=0;i<theForm.p_srch.length;i++){
	p_srch=theForm.p_srch[i].value; 
	whichLabel = 'lbl'+p_srch;
	var style2 = document.layers[eValue].style;
	if (p_srch == eValue)
	{	
	style2.backgroundColor = "#7985a3";
	style2.color = "#fff";
	} 
	else 
	{	
	style2.backgroundColor = "transparent";
	style2.color = "#000";
	}
	}
}
}

function Form_Validator(theForm) {
if (theForm.searcharg.value == "")
  {
    alert("Please type a word or phrase in the search box!");
    theForm.searcharg.focus();
    return (false);
  } else {    
    DoSearch(theForm);
    return (false);
    }
}    

function fmtSearch(searcharg){
if (searcharg.indexOf("++") != -1) {searcharg = searcharg.replace(/\+\+/, "%2B%2B");}
if (searcharg.indexOf("#") != -1) {searcharg = searcharg.replace(/#/g, "%23");}
if (searcharg.indexOf("&") != -1) {searcharg = searcharg.replace(/&/g, "%26");}
if (searcharg.indexOf("?") != -1) {searcharg = searcharg.replace(/\?/g, "%3F");}
if (searcharg.indexOf("?") != -1) {searcharg = searcharg.replace(/\//g, "%2F");}
return searcharg;
}

function DoSearch(theForm) {
var action 
var p_srch 
var p_csrchtype 
var p_jsrchtype 
var p_esrchtype 
var p_gsrchtype  
var searcharg 
searcharg = theForm.searcharg.value
var newsearcharg = searcharg
var rExp1 = /&/g
var rExp2 = /\*/g
var rExp3 = /\"/g
var sURL

for (i=0;i<theForm.p_srch.length;i++){
	if (theForm.p_srch[i].checked==true)
	p_srch=theForm.p_srch[i].value
	}	
if (p_srch == "c") {
	newsearcharg = fmtSearch(searcharg);
	for (i=0;i<theForm.p_csrchtype.length;i++){
	if (theForm.p_csrchtype[i].checked==true)
	p_csrchtype=theForm.p_csrchtype[i].value
	}					
	if (p_csrchtype == "s") {
	action = "http://innopac.library.unr.edu/search/X?SEARCH=";
	sURL = action+newsearcharg+"&b=sc&SORT=D";
	}
	else if (p_csrchtype == "a") {
	action = "http://innopac.library.unr.edu/search/X?SEARCH=";
	sURL = action+newsearcharg+"&b=arch&SORT=D";
	}
}	
else if (p_srch == "eb") {
	action = "http://www.library.unr.edu/specoll/photos.aspx?";
	//trim searcharg and remove quotation marks
	newsearcharg = searcharg.replace(/^\s+|\s+$/g,'');
	newsearcharg = searcharg.replace(rExp3,'');	
	var sSearch = "";
	//alert("Debugging: First searcharg " +searcharg);	
	//which radio button is selected	
	for (i=0;i<theForm.p_esrchtype.length;i++){
	if (theForm.p_esrchtype[i].checked==true)
	p_esrchtype=theForm.p_esrchtype[i].value
	}					
	if (p_esrchtype == "s") {
	sSearch = "p_choice=s&p_search="+newsearcharg;	
	}
	else if (p_esrchtype == "t") {
	sSearch = "p_choice=t&p_search="+newsearcharg;	
	}
	else if (p_esrchtype == "p") {
	sSearch = "p_choice=p&p_search="+newsearcharg;	
	}
    //alert("Debugging: Final searcharg " +sSearch);
	sURL = action+sSearch;		
	}      
else if (p_srch == "j") {
	for (i=0;i<theForm.p_jsrchtype.length;i++){
	if (theForm.p_jsrchtype[i].checked==true)
	p_jsrchtype=theForm.p_jsrchtype[i].value
	}			
	if (p_jsrchtype == "web") {
	action = "http://www.library.unr.edu/ejournals/full/ejdbsearch.aspx?";
	newsearcharg=searcharg.replace(rExp1,' and ');
	newsearcharg=searcharg.replace(rExp2,'');
	sURL = action+"p_srch=ct&p_title="+newsearcharg;	
	}
	else if (p_jsrchtype == "cat") {
	action = "http://innopac.library.unr.edu/search/X?SEARCH=";
	sURL = action+"t:("+newsearcharg+")&m=s&SORT=D"; 
	}		
}	
else if (p_srch == "g") {
	action = "http://www.library.unr.edu/specoll/architecture.aspx?";
	//trim searcharg and remove quotation marks
	newsearcharg = searcharg.replace(/^\s+|\s+$/g,'');
	newsearcharg = searcharg.replace(rExp3,'');	
	var sSearch = "";
	//alert("Debugging: First searcharg " +searcharg);	
	//which radio button is selected	
	for (i=0;i<theForm.p_gsrchtype.length;i++){
	if (theForm.p_gsrchtype[i].checked==true)
	p_gsrchtype=theForm.p_gsrchtype[i].value
	}					
	if (p_gsrchtype == "r") {
	sSearch = "p_choice=r&p_search="+newsearcharg;	
	}
	else if (p_gsrchtype == "b") {
	sSearch = "p_choice=b&p_search="+newsearcharg;	
	}
	else if (p_gsrchtype == "l") {
	sSearch = "p_choice=l&p_search="+newsearcharg;	
	}
    //alert("Debugging: Final searcharg " +sSearch);
	sURL = action+sSearch;		
	}      
else if (p_srch == "cs") { 
	for (i=0;i<theForm.p_msrchtype.length;i++){
	if (theForm.p_msrchtype[i].checked==true)
	p_msrchtype=theForm.p_msrchtype[i].value
	}			
	newsearcharg = fmtSearch(searcharg);
	if (p_msrchtype == "m") {
	action = "http://www.library.unr.edu/specoll/mssresults.html?";	
	sURL = action+"cx=006820933171735136265:vgd383kzgpg&q="+searcharg+"&sa=Search&cof=FORID:11";
	}
	else if (p_msrchtype == "c") {
	sURL="http://innopac.library.unr.edu/search/X?SEARCH="+newsearcharg+"&SORT=D&m=t&m=p&b=sc";
	}
  }	  	
  var resultswin;
  resultswin = window.open(sURL,'frmwin');
  if (window.focus) {resultswin.focus()}
  return (false);
}
  
function limitOn(whichLayer)
{
if (document.getElementById)
{
// standards compliant
var style2 = document.getElementById(whichLayer).style;
style2.display = "inline";
}
else if (document.all)
{
// old IE
var style2 = document.all[whichLayer].style;
style2.display = "inline";
}
else if (document.layers)
{
// NN4
var style2 = document.layers[whichLayer].style;
style2.display = "inline";
}
}

function limitOff(whichLayer){
if (document.getElementById)
{
// standards compliant
var style2 = document.getElementById(whichLayer).style;
style2.display = style2.display? "":"none";
}
else if (document.all)
{
// old IE
var style2 = document.all[whichLayer].style;
style2.display = style2.display? "":"none";
}
else if (document.layers)
{
// NN4
var style2 = document.layers[whichLayer].style;
style2.display = style2.display? "":"none";
}
}

function persistHelpbox(whichLayer){
var theForm = document.forms[0];
if (document.getElementById)
{
// standards compliant
var style2 = document.getElementById(whichLayer).style;
style2.display == "inline"? whatsthis(theForm):"";
}
else if (document.all)
{
// old IE
var style2 = document.all[whichLayer].style;
style2.display == "inline"? whatsthis(theForm):"";
}
else if (document.layers)
{
// NN4
var style2 = document.layers[whichLayer].style;
style2.display == "inline"? whatsthis(theForm):"";
}
}

function toggleSearch(theForm) {
for (i=0;i<theForm.p_srch.length;i++){
	if (theForm.p_srch[i].checked==true)
	p_srch=theForm.p_srch[i].value
	}
if (p_srch == "c") {
	limitOn('climit'); limitOff('blimit'); limitOff('mlimit'); limitOff('glimit');
	persistHelpbox('helpbox');
    }
else if (p_srch == "eb") {
	limitOn('blimit'); limitOff('climit'); limitOff('mlimit'); limitOff('glimit');
	persistHelpbox('helpbox');
    }
else if (p_srch == "cs") {
	limitOn('mlimit'); limitOff('climit'); limitOff('blimit'); limitOff('glimit');
	persistHelpbox('helpbox');
    }
else if (p_srch == "g") {
	limitOn('glimit'); limitOff('climit'); limitOff('mlimit'); limitOff('blimit');
	persistHelpbox('helpbox');
    }
}
function whatsthis(theForm) {
limitOn('helpbox');
var helpwindow = '';
var strBegin = "<html>\n<head>\n<script type='text/javascript' src='js/openwinquicksearch.js'></script>\n</head>\n<body style='background: #ecedf1;'>\n<div id='infotext' style='font: 82%/1.4 verdana, sans-serif;'>\n";
var strEnd = "</div>\n</body></html>";
	
for (i=0;i<theForm.p_srch.length;i++){
	if (theForm.p_srch[i].checked==true)
	p_srch=theForm.p_srch[i].value
	}

if (p_srch == "c") {
var strInfoText = strBegin +
	" <b>Library Catalog</b><br />The catalog lists books, videos, music, books on CD, journals, government publications, and other materials the library owns or that you may access.<br /><br />" +
	" The keyword search looks for words IN the title, author, subjects, and notes " +
	" in the catalog record. Title and author searches find matches starting with the FIRST word.<br /><br />For more search options," +
	" click &quot;Books in the Library Catalog&quot; on the <a href='http://www.library.unr.edu/' onclick='return openwin(this.href,\"700\")'>Library home page<\/a>.<br /><br />" +
	" The catalog does NOT list journal articles." +
strEnd;
limitOff('spanart'); limitOff('spancse'); limitOff('spangog'); limitOn('spancat');
//openhelpwin(strInfoText)
}
else if (p_srch == "eb") {
var strInfoText = strBegin +
	" <b>Articles</b><br />Find journal articles in <i>Academic Search Premier</i> and <i>Masterfile Premier</i>.<br /><br />If search results return <b>too few items</b>:<br />Remove non-essential words from your search; " +
	" search for other words that describe your topic; or click &quot;Refine Results&quot; and " +
	" uncheck &quot;Scholarly (Peer Reviewed) Journals.&quot;<br /><br />If the search returns <b>too many articles</b>:<br />On the results screen select " +
	" a &quot;Narrow Results by Subject&quot; option; add another word or phrase to the search; search for a more specific term (e.g. &quot;impressionism&quot; instead of &quot;art&quot;); or click &quot;Refine Results&quot; to set more limits.<br /><br />If your search is unsuccessful, select a " +
	" <a href='http://www.library.unr.edu/subjects/Default.aspx' onclick='return openwin(this.href,\"700\")'>subject guide</a> and search one of the recommended resources for that subject area.<br /><br />For more help, please " +
	" <a href='http://www.library.unr.edu/depts/reference/askalibrarian.aspx' onclick='return openwin(this.href,\"700\")'>Ask us!</a>" +
	strEnd;
limitOff('spancat'); limitOff('spancse'); limitOff('spangog'); limitOn('spanart');
//openhelpwin(strInfoText)
}
else if (p_srch == "cs") {
var strInfoText = strBegin +
	" <b>Library Webs</b><br />Search UNR Library websites for pages containing your search words.<br /> " +
	" <br />This search includes the DeLaMare Library and Savitt Medical Library webs, as well as the &quot;University Libraries&quot; website." +
	strEnd;
limitOff('spancat'); limitOff('spanart'); limitOff('spangog'); limitOn('spancse');
//openhelpwin(strInfoText)
}
else if (p_srch == "g") {
var strInfoText = strBegin +
	" <b>Google Scholar</b><br />Search <a href='http://www.library.unr.edu/googlescholar.aspx' onclick='return openwin(this.href,\"700\")'>Google Scholar</a> to find " +
	" <a href='http://www.library.unr.edu/instruction/help/peer.html' onclick='return openwin(this.href,\"700\")'>peer-reviewed</a> books and articles.<br /><br /> " + 
	" Select Google to search the World Wide Web for websites that match your search<br /><br /> " +
	" For more Google searches, such as Google Images " +
	" or Google Unclesam, the search engine for government publications, close this window and click the &quot;more search Options&quot; link. " +
	strEnd;
limitOff('spancat'); limitOff('spanart'); limitOff('spancse'); limitOn('spangog');
//openhelpwin(strInfoText)
}
}

//open small help window
function openhelpwin(str) {	
	helpwindow=window.open('','infowin','left=500,top=200,height=350,width=300,toolbar=no,scrollbars=yes,resizable=yes');
	if (!helpwindow.opener) helpwindow.opener = self;	
	helpwindow.document.open();
	helpwindow.document.write(str);
	helpwindow.document.close();
	if (window.focus) {helpwindow.focus()}
	return false;
}


