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) //catalog
	{
	limitOn('climit');
	limitOff('blimit');
	limitOff('rlimit');
	limitOff('slimit');
	limitOff('glimit');
	toggleRadioLabel('c');
	}
	else if (theForm.p_srch[1].checked==true) //articles
	{
	limitOn('blimit');
	limitOff('climit');
	limitOff('rlimit');
	limitOff('slimit');	
	limitOff('glimit');
	toggleRadioLabel('eb');
	}
	else if (theForm.p_srch[2].checked==true) //reserves
	{
	limitOn('rlimit');
	limitOff('climit');
	limitOff('blimit');
	limitOff('slimit');	
	limitOff('glimit');
	toggleRadioLabel('cr');
	}
	else if (theForm.p_srch[3].checked==true) //reserves
	{
	limitOn('slimit');	
	limitOff('blimit');
	limitOff('climit');
	limitOff('rlimit');
	limitOff('glimit');
	toggleRadioLabel('cs');
	}
	else if (theForm.p_srch[4].checked==true) //google
	{
	limitOn('glimit');
	limitOff('climit');
	limitOff('blimit');
	limitOff('rlimit');
	limitOff('slimit');	
	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){ //for library catalog
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_jsrchtype 
var p_esrchtype 
var p_rsrchtype
var p_ssrchtype
var p_gsrchtype  
var searcharg 
searcharg = theForm.searcharg.value
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") {
	searcharg = 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 == "w") {
	action="http://encore.library.unr.edu/iii/encore/Home,$Search.form.sdirect"
	//resolves to: search/C%7CS"+searcharg+"%7COrightresult%7CU1";	
	sURL = action+"?formids=target&lang=eng&suite=def&reservedids=lang,suite&submitmode=&submitname=&target="+searcharg;
	}
	else if (p_csrchtype == "t") {
	action = "http://innopac.library.unr.edu/search/t?";
	sURL = action+searcharg;
	}
	else if (p_csrchtype == "a") {
	action = "http://innopac.library.unr.edu/search/a?";
	sURL = action+searcharg;
	}	
}	
else if (p_srch == "eb") {
	action = "http://0-search.ebscohost.com.innopac.library.unr.edu:80/login.aspx?";
	//trim searcharg and remove quotation marks
	searcharg = searcharg.replace(/^\s+|\s+$/g,'');
	searcharg = 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
	}					
	var sSplit = searcharg.split(" ");
	if (p_esrchtype == "all") {
	for(i = 0; i < sSplit.length; i++) {
		sSearch += sSplit[i]+"+AND+"; }	
	sSearch = sSearch.substring(0,sSearch.length -5);	
	}
	else if (p_esrchtype == "any") {
	for(i = 0; i < sSplit.length; i++) {
	  sSearch += sSplit[i]+"+OR+"; }	
	sSearch = sSearch.substring(0,sSearch.length -4);	
	}
	else if (p_esrchtype == "phrase") {
	for(i = 0; i < sSplit.length; i++) {
	   sSearch += sSplit[i]+"+"; }		
	var qString = '\"';
	sSearch = sSearch.substring(0,sSearch.length -1);
	sSearch = qString + sSearch + qString;
	}
    //alert("Debugging: Final searcharg " +sSearch);
	sURL = action+"direct=true&db=aph&db=f5h&bquery="+sSearch+"&cli0=RV&clv0=Y&type=1&site=ehost-live";		
	}      
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?";
	searcharg=searcharg.replace(rExp1,' and ');
	searcharg=searcharg.replace(rExp2,'');
	sURL = action+"p_srch=ct&p_title="+searcharg;	
	}
	else if (p_jsrchtype == "cat") {
	action = "http://innopac.library.unr.edu/search/X?SEARCH=";
	sURL = action+"t:("+searcharg+")&m=s&SORT=D"; 
	}		
}	
else if (p_srch == "cr") {
	searcharg = fmtSearch(searcharg);
	for (i=0;i<theForm.p_rsrchtype.length;i++){
	if (theForm.p_rsrchtype[i].checked==true)
	p_rsrchtype=theForm.p_rsrchtype[i].value
	}				
	if (p_rsrchtype == "p") {
	action = "http://innopac.library.unr.edu/search/p?";
	sURL = action+searcharg;
	}	
	else if (p_rsrchtype == "r") {
	action = "http://innopac.library.unr.edu/search/r?";
	sURL = action+searcharg;
	}
}
else if (p_srch == "cs") { 
	for (i=0;i<theForm.p_ssrchtype.length;i++){
	if (theForm.p_ssrchtype[i].checked==true)
	p_ssrchtype=theForm.p_ssrchtype[i].value
	}			
	if (p_ssrchtype == "d") {
	searcharg = "site:delamare.unr.edu+"+searcharg; 
	}	
	else if (p_ssrchtype == "s") {
	searcharg = "site:med.unr.edu+"+searcharg;
	}
	action = "http://knowledgecenter.unr.edu/searchresults.html?";		
  	sURL = action+"cx=006820933171735136265:ygutq8bicsy&q="+searcharg+"&sa=Search&cof=FORID:11";
}
else if (p_srch == "g") {
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 == "s") {
	action = "http://scholar.google.com/scholar?";
	searcharg = searcharg.replace(/\s/g, "+");	
	sURL = action+"q="+searcharg+"&ie=UTF-8&oe=UTF-8&hl=en";
    }	
	else if (p_gsrchtype == "w") {
	action = "http://www.google.com/search?";
	searcharg = searcharg.replace(/\s/g, "+");		
	sURL = action+"q="+searcharg+"&hl=en"; 
	}
}
  window.location.href = sURL;  
  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('rlimit'); limitOff('slimit'); limitOff('glimit');
	persistHelpbox('helpbox');
    }
else if (p_srch == "eb") {
	limitOn('blimit'); limitOff('climit'); limitOff('rlimit'); limitOff('slimit'); limitOff('glimit');
	persistHelpbox('helpbox');
    }
else if (p_srch == "cr") {
	limitOn('rlimit'); limitOff('climit'); limitOff('blimit'); limitOff('slimit'); limitOff('glimit');
	persistHelpbox('helpbox');
	}    
else if (p_srch == "cs") {
	limitOn('slimit'); limitOff('climit'); limitOff('blimit'); limitOff('rlimit'); limitOff('glimit');
	persistHelpbox('helpbox');
    }
else if (p_srch == "g") {
	limitOn('glimit'); limitOff('climit'); limitOff('blimit'); limitOff('slimit'); limitOff('rlimit'); 
	persistHelpbox('helpbox');
    }
}

function whatsthis(theForm) {
for (i=0;i<theForm.p_srch.length;i++){
	if (theForm.p_srch[i].checked==true)
	p_srch=theForm.p_srch[i].value
	}
limitOn('helpbox');
if (p_srch == "c") {
limitOff('spanart'); limitOff('spancse'); limitOff('spanres'); limitOff('spangog'); limitOn('spancat');
}
else if (p_srch == "eb") {
limitOff('spancat'); limitOff('spancse'); limitOff('spanres'); limitOff('spangog'); limitOn('spanart');
}
else if (p_srch == "cr") {
limitOff('spancat'); limitOff('spanart'); limitOff('spancse'); limitOff('spangog'); limitOn('spanres');
}
else if (p_srch == "cs") {
limitOff('spancat'); limitOff('spanart'); limitOff('spanres'); limitOff('spangog'); limitOn('spancse');
}
else if (p_srch == "g") {
limitOff('spancat'); limitOff('spanart'); limitOff('spancse'); limitOff('spanres'); limitOn('spangog');
}
}
