
function getSearchQuery() {
  queryString = prompt("Enter a search phrase and click Okay");
  document.searchForm.search.value = queryString;
  document.searchForm.submit();
}

function mpopup(CourseID, StudentID, ModuleID, ChapterID, SectionID, ClassID) {
  mpadwindow = window.open(URL_ROOT + "/memopad_list.html?CourseID=" + CourseID + "&classid="+ClassID+"&StudentID=" + StudentID + "&ModuleID=" + ModuleID + "&ChapterID=" + ChapterID + "&PageID=" + SectionID,"memopad","width=600,height=475,scrollbars,resizable,left=20,top=20");
  if (!mpadwindow.opener) { mpadwindow.opener = self; }
  if (mpadwindow.focus) { mpadwindow.focus(); }
}

function gnotepopup(GlossaryID) {
  padwindow = window.open(URL_ROOT + "/viewglossary.html?gid=" + GlossaryID + "#" + GlossaryID,"test","width=500,height=250,scrollbars,resizable,left=20,top=20");
  if (!padwindow.opener) { padwindow.opener = self; }
  if (padwindow.focus) { padwindow.focus(); }
}

function gcnotepopup(CourseID) {
  padwindow = window.open(URL_ROOT + "/viewglossary.html?CourseID=" + CourseID,"test","width=500,height=250,scrollbars,resizable,left=20,top=20");
  if (!padwindow.opener) { padwindow.opener = self; }
  if (padwindow.focus) { padwindow.focus(); }
}

function fnotepopup(FootnoteID) {
  fnwindow = window.open(URL_ROOT + "/viewfootnotes.html?fid=" + FootnoteID ,"test","width=500,height=250,scrollbars,resizable,left=20,top=20");
  if (!fnwindow.opener) { fnwindow.opener = self; }
  if (fnwindow.focus) { fnwindow.focus(); }
}

function popup(page, w, h) {
  if (!w) w = 450;
  if (!h) h= 300;
  if (!page) page= "#";
  var style = "toolbar=0,location=0,directories=0,status=0,scrollbars=yes,menubar=yes,resizable=yes,copyhistory=no,width="+w+",height="+h;
  PopUp = window.open(page, "PopUp", style)
  PopUp.name = "PopUp";
  if (!PopUp.opener) { PopUp.opener = self; }
  if (PopUp.focus) { PopUp.focus(); }
}

function helppopup(page, w, h) {
  if (!w) w = 450;
  if (!h) h= 300;
  if (!page) page= "#";
  var style = "toolbar=1,location=0,directories=0,status=0,scrollbars=yes,resizable=yes,copyhistory=no,width="+w+",height="+h;
  HelpPopUp = window.open(page, "HelpPopUp", style)
  HelpPopUp.name = "HelpPopUp";
  if (!HelpPopUp.opener) { HelpPopUp.opener = self; }
  if (HelpPopUp.focus) { HelpPopUp.focus(); }
}

function Get_Cookie(name) {
     var start = document.cookie.indexOf(name+"=");
     var len = start+name.length+1;
     if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
     if (start == -1) return null;
     var end = document.cookie.indexOf(";",len);
     if (end == -1) end = document.cookie.length;
     return unescape(document.cookie.substring(len,end));
}

function KB_submit(){
  var w = 600;
  var h= 400;
  var page= "conference.html";
  var style = "toolbar=1,location=1,directories=0,status=1,menubar=1,scrollbars=yes,resizable=yes,copyhistory=no,screenX=10,screenY=10,width="+w+",height="+h;
  PopUp = window.open(page, "PopUp", style)
  PopUp.name = "PopUp";
  if (!PopUp.opener) { PopUp.opener = self; }
  if (PopUp.focus) { PopUp.focus(); }
}

if (navigator.userAgent.indexOf("Macintosh") != -1) {
  var isMac = true;
} else {
  var isMac = false;
}

var isnav = (document.layers)? true:false;
var isexp = (document.all)? true:false;
var ismoz = (!isnav && !isexp);
var togglers = new Array();
var quickrollers = new Array();
// initialize a global to store a reference to a DHTML layer containing an open menu
var openMenu = null;


function mask(n) {
  // mask an event
  blank = n;
}

function searchLayers(n, d){
// search the top-level of the layers array for an image. This script will not
// find images in nested layers.  This script is only run under Netscape.
  var i,x;  if(!d) d=document;
  x=d.images[n];
  if (!x){
    for(i=0;i< d.layers.length;i++){
      x=searchLayers(n,d.layers[i].document);
      if (x)return x;
    }
  } else {
    return x;
  }
}

function quickRoll(i, ai, n){
// constructor function to manage a simple rollover
  this.image = i;
  this.altImage = ai;
  if (isnav && !document.images[n]){ this.tagRef=searchLayers(n)}
  else {
    this.tagRef = document.images[n]
  }

  this.swapImage = qr_swapImage;
}

function qr_swapImage(flag){
// method of quickRoll
//   manage image swapping
  this.tagRef.src = (flag)? this.altImage.src : this.image.src;
}

function toggleButton(b1, b2){
// constructor function to manage a button that can be swapped into two states.
// useful for radio buttons and buttons that need to take on a 'set' and 'unset' state.
  this.defButton = b1;
  this.altButton = b2;
  this.currentButton = this.defButton;
  this.swapButton = tb_swapButton;
  this.swapImage = tb_swapImage;
}

function tb_swapButton(){
// method of toggleButton object
//   Switches between the default button and the alt button
  this.currentButton = (this.currentButton == this.defButton)? this.altButton : this.defButton;
}

 function tb_swapImage(flag){
// method of toggleButton object
//   Passes a swapImage message to the current button
  this.currentButton.swapImage(flag);
}

function toggleMenu(l){
// turns a menu layer reveals and hide a menu layer.
// l is the DHTML layer passed in from the onClick event handler of the <a> tag;
  l.vis = !l.vis;

  // Fix for IE dropdown's being in front of menu's.
  if (isexp) {
	if (l.vis) hide_all_selects(true);
	else hide_all_selects(false);
  }

  // enable/disable event capturing in the browser so we can get rid of the menu if they
  // do not use it.
  document.onmousedown = (l.vis) ? mouseDown : null

  if (isnav){
    // show/hide the menu layer
    l.visibility = (l.vis) ? 'show' : 'hide';
    // more code to enable event capturing in Netscape
    if (l.vis) document.captureEvents(Event.MOUSEDOWN);
  }
  // show/hide the menu layer
  if (isexp || ismoz) {
    l.visibility = (l.vis) ? 'visible' : 'hidden';
  }
  // set a global variable to reference the DHTML layer of the menu if it is being revealed
  openMenu = (l.vis) ? l : null;
}

function mouseDown(e){
// event handler we designate when we show a menu so that we can get rid of it if the user
// clicks somewhere other than the menu.
  // if there is no menu open return the event
  if (openMenu == null) if (isnav) return true;
  // find the mouse down location
  var x = (isnav || ismoz)? e.pageX : (event.clientX+document.body.scrollLeft)
  var y = (isnav || ismoz)? e.pageY : (event.clientY+document.body.scrollTop)
  // if the mouse down is inside the menu return the event
  if (x>=openMenu.locX && x<=openMenu.locX+openMenu.wdth && y>=38 && y<=38+openMenu.hght){
    return true;
  // if the mouse down is outside the menu return the menu button to its default state
  // and hide the menu
  } else {
    if (x>=openMenu.buttonLayer.locX && x<=openMenu.buttonLayer.locX+(openMenu.buttonLayer.wdth) && y>=0 && y<=38+openMenu.buttonLayer.hght) {
      openMenu.button.swapImage();
    } else {
      openMenu.button.swapButton();
      openMenu.button.swapImage();
      toggleMenu(openMenu);
    }
  }
  // return the event
  if (isnav) routeEvent(e);
  return true;
}

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_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 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];}
}

// BEGIN NETSCAPE ONLY RESIZE CODE
// Netscape Resize Fix: When user resizes the browser window NN does some screwy stuff
// with its DHTML layers, so we have to force a reload every time a resize event occurs
// NOTE: A cookie could be used to store a representation of the directory tree so that
//       we could rebuild the tree as it was just before the resize...
if (isnav) {
  navWidth = window.innerWidth;
  navHeight = window.innerHeight;
  //window.onResize = resizeFix;
}

function resizeFix() {
  if (navWidth != window.innerWidth || navHeight != window.innerHeight)
  self.document.location.href = self.document.location.href;
  alert('resized');
}

// Call like this:
// <select ... onchange="setoptions(this, document.forms[0].select2)">
//   <option value=1>Choice One</option>
//   <option value=2>Choice Two</option>
// </select>
// <select name=select2></select>
// [script]
// document.forms[0].select2.data = new Array();
// document.forms[0].select2.data[1] = 'A,Subchoice A|B,Subchoice B';
// document.forms[0].select2.data[2] = 'C,Subchoice C|D,Subchoice D';
// setoptions(document.forms[0].select1, document.forms[0].select2);
// document.forms[0].select2.selectedIndex=1;
// [/script]
function setoptions(s1, s2) {
	s2.options.length=0;
	key = s1.options[s1.selectedIndex].value;
	opts = s2.data[key].split('|');
	s2.options.length = opts.length;
	for (i=0; i<opts.length; i++) {
		pieces = opts[i].split(',');
		s2.options[i].value = pieces[0];
		s2.options[i].text = pieces[1];
	}
}

function hide_all_selects(flag) {
	for (var i=0; i<document.all.length; i++) {
		o = document.all[i];
		if (o.type == 'select-one' || o.type == 'select-multiple') {
			// todo: add check for select in div?
			if (o.style) o.style.display = flag ? 'none' : '';
		}
	}
}

// optional hook for FireBug; you cannot rely on this in production code,
// but it's very helpful for debugging
// http://www.joehewitt.com/software/firebug/
if (document.createEvent) {
	window.printfire = function() {
		printfire.args = arguments;
		var ev = document.createEvent("Events");
		ev.initEvent("printfire", false, true);
		dispatchEvent(ev);
	}
}

function check_regexp(form, checked, nameRegexp) {
	printfire(form);
	printfire(checked);
	printfire(nameRegexp);
	var elms = form.elements;
	for (i = 0; i < elms.length; i++) {
		if (elms[i].name.match(nameRegexp))
			elms[i].checked = checked;
	}
}

// helper for nestedselect_field2
function load_options_keyed(select, keys, items) {
	select.options.length=0; // delete the existing options
	if (!keys) return;
	select.options.length=keys.length;
	select.selectedIndex=0;
	for (i=0; i<keys.length; i++) {
		select.options[i].value = keys[i];
		select.options[i].text = items[keys[i]];
	}
}
