//clears text fields and sets menu items to first option
//used to clear a form that has been filled out automatically
//with data from the previous search.

function clearForm ( theForm )
{
  for (var i=0; i < theForm.length; i++)
    {
      var e = theForm.elements[i];
      if (e.type == "text") 
        {
	  e.value = "";
        }
      else if (e.type == "select-one")
        {
	  e.selectedIndex = 0;
	}
     }
}


// collects data from all search form elements and
// builds a URL for the bbaction form action.

function buildBBaction( theForm, winName, type, resultstableForm) 
{
  // uh for "url hold"
  var uh = '';
  // run through the menu form elements
  for (var i=0; i < theForm.elements.length; i++)
    {
      // build cgi params
      var v = ''; //to hold the value
      if (theForm.elements[i].type == "select-one")
        {
          var si = theForm.elements[i].selectedIndex; 
          v = theForm.elements[i].options[si].value;
        }
      else
        {
          v = theForm.elements[i].value;
        }

      v = v.replace(/\s/g, "+");
      // quotes are not allowed. Perl CGI module doesn't like them.
      v = v.replace(/\'/g, "");
      v = v.replace(/\"/g, "");

      var p = '';        
      p += "&";
      p += theForm.elements[i].name;
      p += "=";
      p += v;
      // jump through some hoops to make sure we only get checked
      // values of radio and checkbox form elements
      if ((theForm.elements[i].type == 'checkbox') || (theForm.elements[i].type == 'radio'))
        { 
         if (theForm.elements[i].checked == true)
	   {
	     uh += p;  
	   }
        }
       else
        {
	   uh += p;
        }
    }	
  // next, run through the resultstable form to get the selected colls
  // bbidno_checked for all selected colls
  var bbidno_checked = '';
  if (resultstableForm.bbidno.length)
  {
    for (var i=0; i < resultstableForm.bbidno.length; i++)
    {
      // p for holding cgi params as we build them
      var p = '';
      p += "&";
      p += resultstableForm.bbidno[i].name;
      p += "=";
      p += resultstableForm.bbidno[i].value;
      // if the image is selected, we add the params to bbidno_checked
      if (resultstableForm.bbidno[i].checked == true)
        {
          bbidno_checked += p;
        }
     }
  }
  else if (resultstableForm.bbidno.checked)
  {
    bbidno_checked += "&" + resultstableForm.bbidno.name + "=" + resultstableForm.bbidno.value;
  } 
  // if there were no selected bbidnos, set uh to '',
  // which is all colls.	
  if (bbidno_checked == '')
    {
      //do nothing, I guess
    }
  else
    {
      uh += bbidno_checked;
      // tack the params on to the end of the url that is in the form's
      // action and then go to the url.
      //nn and ie behave differently here. 
      //theForm.action will be completely empty for ie. 
      //for nn, it has the cgi url plus parameters that were
      //passed in to the cgi, which we strip off because we
      //don't want them. 
        var u = theForm.action;
        u = u.replace(/\?.*/g, "");

        u = u + "?" + uh;

      if (type == "popup")
        {
          popupDiagWindow( u, winName );		
        }
      else
        {
          window.open( u, winName);
        }
    }
}



//used by index.tpl
function MM_openInfoWindow(theURL,winName) { //v2.0
var infoWindow = 0;
var winY = winavailheight - ( winavailheight - 50 );
var winX = ( winavailwidth - 475 ); 
var winparams = "";

  if (ie4)
  {
     winparams = 'scrollbars=yes,resizable=yes,location=no,width=455,height=500,left='+winX+',top='+winY;

  }
  else
  {
	 winparams = 'scrollbars=yes,resizable=yes,location=no,width=455,height=500,screenX='+winX+',screenY='+winY;
  }
    if ( infoWindow && !infoWindow.closed )
    {
        infoWindow.focus();  
    }
    else
    {
	infoWindow = window.open( '', winName, winparams );
           infoWindow.focus( );    
    }
    infoWindow.document.location.href = theURL;
 }


// collects data from all search form elements and
// builds a URL for the action.

function formToUrl( theForm, winName, type ) 
{
  // uh for "url hold"
  var uh = '';
  // run through the menu form elements
  for (var i=0; i < theForm.elements.length; i++)
    {
      // build cgi params
      var v = ''; //to hold the value
      if (theForm.elements[i].type == "select-one")
        {
          var si = theForm.elements[i].selectedIndex; 
          v = theForm.elements[i].options[si].value;
        }
      else
        {
          v = theForm.elements[i].value;
        }
      // spaces need to be pluses
      v = v.replace(/\s/g, "+");
      // quotes are not allowed. Perl CGI module doesn't like them.
      v = v.replace(/\'/g, "");
      v = v.replace(/\"/g, "");

      var p = '';        
      p += "&";
      p += theForm.elements[i].name;
      p += "=";
      p += v;
      // jump through some hoops to make sure we only get checked
      // values of radio and checkbox form elements
      if ((theForm.elements[i].type == 'checkbox') || (theForm.elements[i].type == 'radio'))
        { 
          if (theForm.elements[i].checked == true)
	    {
	      uh += p;  
	    }
        }
      else
         {
	   uh += p;
         }
    }	
    // tack the params on to the end of the url that is in the form's
    // action and then go to the url.
//      gotourl( theForm.action + "?" + uh);

// NOTE: ie and nn behave differently in regards to theForm.action values.
// look elsewhere in this file for more info about theForm.action.
    var u = theForm.action + "?" + uh;

//	alert (u);
    if (type == "popup")
      {
        popupDiagWindow( u, winName );		
      }
      else if (type == "notoolbar")
      {
        MM_openBrWindow(u, winName, 'scrollbars=yes, resizable=yes');	   
      }
    else
      {
        window.open( u, winName);
      }
}


//used by entry.tpl and bbentry.tpl to return to window of results
function focusOnOpener()
{
  var wo = window.opener;
  wo.focus();
}


//change the visibility of a layer
//used with thumbfull results page
function show(tab, vb) //tab=recnum vb=visibility
{
  if (document.getElementById) //ns6, ie5, ie6
    {
      var elm=document.getElementById(tab);
      elm.style.visibility=vb;
    }
  else if (document.all) //ie4
    {
      var elm=document.all[tab];
      elm.style.visibility=vb;
    }
  else //ns4
    {
      document.layers[tab].visibility = vb;
    }
}


//used for showing and hiding full record layer in the thumbfull view
function display(data) 
{
  for (var i = 0; i < descriptdata.length; i++) 
   {
     var dd=descriptdata[i];
     if (descriptdata[i] == data) 
       {
	 show(data, "visible"); 
       }
     else 
       {
	 show(dd, "hidden");
       }
   }
}







function gotourl(url)
{
	parent.document.location.href = url;
}


function checkAllRecords(db_form)
{
	var ele = "";
	for (var i = 0; i < db_form.elements.length; i++) 
	{
		ele = db_form.elements[i];
		if (ele.type == "checkbox")
		{
			ele.checked = true;
  		}
  	}
}


function clearAllRecords(db_form)
{
	var ele = "";
	for (var i = 0; i < db_form.elements.length; i++) 
	{
		ele = db_form.elements[i];
		if (ele.type == "checkbox")
		{
			ele.checked = false;
  		}
  	}
}



function MM_openBrWindow(theURL,winName,features) { //v2.0
  var w = window.open(theURL,winName,features);
  w.focus();
  }

function MM_openTipsWindow(theURL,winName) { //v2.0
var tipsWindow = 0;
var winY = winavailheight - ( winavailheight - 50 );
var winX = ( winavailwidth - 275 ); 
var winparams = "";

  if (ie4)
  {
     winparams = 'scrollbars=yes,resizable=yes,width=255,height=500,left='+winX+',top='+winY;

  }
  else
  {
	 winparams = 'scrollbars=yes,resizable=yes,width=255,height=500,screenX='+winX+',screenY='+winY;

  }


    if ( tipsWindow && !tipsWindow.closed )
    {
        tipsWindow.focus();  
    }
    else
    {
	tipsWindow = window.open( '', winName, winparams );
           tipsWindow.focus( );    
    }

    tipsWindow.document.location.href = theURL;



  }



function buildAction() 
{
  // uh for "url hold"
  var uh = '';
  // run through the search form elements
  for (var i=0; i < document.the_form.elements.length; i++)
    {
      // build cgi params
      var v = ''; //to hold the value
      if (document.the_form.elements[i].type == "select-one")
        {
          var si = document.the_form.elements[i].selectedIndex; 
          v = document.the_form.elements[i].options[si].value;
        }
      else
        {
          v = document.the_form.elements[i].value;
        }
      v = v.replace(/\s/, "+");
      var p = '';        
      p += "&";
      p += document.the_form.elements[i].name;
      p += "=";
      p += v;
      // jump through some hoops to make sure we only get checked
      // values of radio and checkbox form elements
      if ((document.the_form.elements[i].type == 'checkbox') || (document.the_form.elements[i].type == 'radio'))
        { 
         if (document.the_form.elements[i].checked == true)
	   {
	     uh += p;  
	   }
        }
      else
        {
	  uh += p;
        }
    }	
  // next, run through the colpick form to get the selected colls
  // c_checked for all selected colls
  var c_checked = '';
  // c_unchecked for all unselected colls
  // if no colls are checked, we'll use the unselected colls, 
  // which should be all colls since non were selected.
  var c_unchecked = ''; 
  for (var i=0; i < document.DBselect.c.length; i++)
    {
    // p for holding cgi params as we build them
    var p = '';
    p += "&";
    p += document.DBselect.c[i].name;
    p += "=";
    p += document.DBselect.c[i].value;
    // if the coll is selected, we add the params to c_checked
    if (document.DBselect.c[i].checked == true)
      {
        c_checked += p;
      }
    // otherwise we add the params to c_unchecked
    else
      {
        c_unchecked += p;
      }
  }

  if (document.DBselect.c.type == 'hidden')
  {
    p += "&";
    p += document.DBselect.c.name;
    p += "=";
    p += document.DBselect.c.value;
    c_checked += p;
  }
  // if there were no selected colls, use the unselected colls,
  // which is all colls.	
  if (c_checked == '')
    {
      uh += c_unchecked;
    }
  else
    {
      uh += c_checked;
    }

  // tack the params on to the end of the url that is in the form's
  // action and then go to the url.

  gotourl( document.the_form.action + "?" + uh);
}

  

// these seemingly out of place vars are indeed used.
// or at least some of them are.  
var diagWindow = 0;
var winavailwidth = screen.availWidth;
var winavailheight = screen.availHeight;
var winY = ( winavailheight/8 );
var winX = ( winavailwidth/8 ); 
var ns4 = (document.layers)? true:false
var ie4 = (document.all)? true:false
var winparams = "";



function popupDiagWindow( URLstr, winName )
{

  if (ie4)
  {
     winparams = 'scrollbars=yes,resizable=yes,width=625,height=550,left='+winX+',top='+winY;

  }
  else
  {
	 winparams = 'scrollbars=yes,resizable=yes,width=625,height=550,screenX='+winX+',screenY='+winY;

  }


    if ( diagWindow && !diagWindow.closed )
    {
        diagWindow.focus();  
    }
    else
    {
	diagWindow = window.open( '', "bookbag", winparams );
           diagWindow.focus( );    
    }

    diagWindow.document.location.href = URLstr;
}



function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

//start collpick stuff

// -------------- Serves collpick.tpl

function checkAllColls(db_form)
{
	var ele = "";
	for (var i = 0; i < db_form.elements.length; i++) 
	{
		ele = db_form.elements[i];
		if ( (ele.type == "checkbox") && (ele.name == "c") )
		{
			ele.checked = true;
  		}
  	}
  	swapOptions(db_form);
}

function clearAllColls(db_form)
{
	var ele = "";
	for (var i = 0; i < db_form.elements.length; i++) 
	{
		ele = db_form.elements[i];
		if ( (ele.type == "checkbox") && (ele.name == "c") )
		{
			ele.checked = false;
  		}
  	}
		setOptionText('DC');
}

function swapOptions(db_form)
{
	var checkedColls = new Array();
	var ckcount = 0;
	var ele = "";
	
	for (var i = 0; i < db_form.elements.length; i++) 
	{
		ele = db_form.elements[i];
		if ( (ele.type == "checkbox") && (ele.name == "c") )
		{
			if (ele.checked)
			{
				ckcount++;
				checkedColls[checkedColls.length] = db_form.elements[i].value;
			}
		
		}
	
	}
	
	switch(ckcount)
	{
		case 0 :			
		
			setOptionText('DC');

		break
		
		case 1 :			
		
			setOptionText(checkedColls[0]);


		break
		
		
		default :
			setOptionText('DC');

	
		break
		
	}
   
}

//setOptionText
//used by xcoll search form to update rgn select menus
//with xcoll or coll specific category names
//input: CIDname-- coll id

function setOptionText(CIDname)
{
	// determine whether xcoll or coll specific 
	// categs names will be used.
        if (CIDname == '')
        {
        	var CollID = 'DC'; //xcoll
        }
        else
        {
	        var CollID = eval(CIDname); //coll specific
	}

        // run through all elements of form (is there a better way)
	// and find those that are rgnX
	for (var n=0; n < document.the_form.elements.length; n++)
	{	  
          var pattern = /rgn/i;
	  if (pattern.test(document.the_form.elements[n].name))
	  {
	    //clear the menu
            document.the_form.elements[n].length = 0;
	
            for (i=0; i < CollID.length; i++)
            {
	      var optiontext = CollID[i];
	      if (CollID[i] == 'All Categories')
	      {
		var selectedIndex = i;
		optiontext = "Anywhere in record";
	      }

              document.the_form.elements[n].length++;
              document.the_form.elements[n].options[i].text = optiontext;
              document.the_form.elements[n].options[i].value = CollID[i];
            }
            document.the_form.elements[n].options[selectedIndex].selected = true;
          }
        }

}


// -------------------------------------------

//---- from entry.tpl

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function sniffBrowser()
{
  var ns4 = (document.layers) ? true : false;
  var ie4 = (document.all && !document.getElementById) ? true : false;
  var ie56 = (document.all && document.getElementById) ? true : false;
  var ns6 = (!document.all && document.getElementById) ? true : false;


  // figure out if browser is a netscape browser or an ie browser
  var brwsr = 'ie';
  if ( (ns4)  || (ns6) )
    {
      brwsr = 'ns';
    }

  var platform = "win";  // use as default

  if (navigator.userAgent.indexOf("Mac") != -1)
    {
      platform = 'mac';
    }       
  if (navigator.userAgent.indexOf("Win") != -1)
    {
      platform = 'win';
    }       

  //  alert (platform+' '+brwsr+' '+layersmode);
  //  alert (platform+' '+brwsr);
  var ret = new Array(2);
  ret[0] = platform;
  ret[1] = brwsr;
  //  ret[2] = layersmode;
  return ret;
}

function MM_showHideLayers() 
{
  var i,args=MM_showHideLayers.arguments;
  var elm, v;
  for (i=0; i<args.length; i+=1)
    {
      var arg = args[i];
            if (arg == "show")
      	{
      	  arg = "visible";
      	}
            else if (arg == "hide")
      	{
      	  arg = "hidden";
      	}
      if (arg == "")
	{
	  //be sure to do nothing
	}
      else if ((arg == "visible") || (arg == "hidden"))
	{
	  v = arg;
	  if (elm.style)
	    {
	      elm.style.visibility=v;
	      v="";
	    }
	  else if (elm)
	    {
	      document.layers[elm].visibility=v;
	      v="";
	    }
	  else
	    {
	      alert("javascript layers problem in function  MM_showHideLayers");
	    }
	}
      else if ( document.getElementById )
	{
	  elm = document.getElementById(arg);
	}
      else if ( document.all )
      	{
	  alert("does this ever happen");
      	  elm = document.all[arg];
      	}
      else if ( document.layers )
      	{
      	  elm = arg;
      	}
      else
	{
	  v = "";
	}
    }
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_nbGroup(event, grpName) { //v3.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : args[i+1];
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    if ((nbArr = document[grpName]) != null)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = args[i+1];
      nbArr[nbArr.length] = img;
  } }
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//used for jumping from the popup collinfo window back to the main window
//could be used elsewhere as well
function dlxsLink(url)
{
  var target = url+'?sid='+insTarget;
  wincaller.document.location.href = target;
}

function mainWinLink(url)
{
wincaller.document.location.href = url;
}

