function getSearchQuery(url) {
  search = prompt('Enter a search phrase');
  if (search == null || search == '') return false;
  document.location = url + (url.indexOf('?') != -1 ? '&' : '?')
    + 'search=' + encodeURI(search);
  return false;
}

function mpopup(CourseID, StudentID, ModuleID, ChapterID, SectionID, ClassID) {
  mpadwindow = window.open("/memopad_list.html?CourseID=" + CourseID + "&classid="+ClassID+"&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("/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("/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("/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 popupFull(page, w, h) {
  if (!w) w = 700;
  if (!h) h= 500;
  if (!page) page= "#";
  var style = "toolbar=1,location=1,directories=1,status=1,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 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 dropdowns being in front of menus.
  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];}
}


function hide_all_selects(flag) {
        var selects = document.getElementsByTagName('select');
        for (var i in selects) {
                // todo: add check for select in div?
                if (selects[i].style) selects[i].style.display = flag ? 'none' : '';
        }
}

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

// helper for subdropdown_field
function load_options_keyed(select, items, selected, empty, none) {
	var i = 0;
	select.options.length=0; // delete the existing options
	select.selectedIndex=0;
	if (!items) {
		select.options[i++] = new Option(none, '');
		return;
	}
	if (empty) select.options[i++] = new Option(empty, '');
	for (var key in items) {
		select.options[i] = new Option(items[key], key);
		if (selected == key) select.selectedIndex = i;
		i++;
	}
}

function toggleVis(tb) {
	e=getElementFromID(tb);
	e.style.display=(e.style.display=='none') ? '' : 'none';
}
function toggleLink(obj) {
	if(obj.src.search('plus') > 0){
		obj.src=obj.src.replace('plus','minus');
	}else{
		obj.src=obj.src.replace('minus','plus');
	}
}
function toggleExpander(id) {
	clicker = getElementFromID(id);
	if (clicker.alt == '-') {
		clicker.alt = '+';
		clicker.src = '\/images\/expand.gif';
	} else {
		clicker.alt = '-';
		clicker.src = '\/images\/collapse.gif';
	}
}

function getElementFromID(n) {
	if (document.all) {
		var e=document.all[n];
	} else {
		var e=document.getElementById(n);
	}
	return e;
}

function openCourseDocPopup(courseId) {
	var url = "/courseDoc.php?courseId="+courseId;
	var window = open(url,'','menubar=no,resizable=no,toolbar=no,scrollbars=yes,width=600,height=400');
}

/**
 * A hack for IE ActiveX weirdness.
 * (see http://msdn.microsoft.com/workshop/author/dhtml/overview/activating_activex.asp)
 * For whatever reason, calling this instead of calling document.write directly
 * makes the script work.
 */
function document_write(content) {
	document.write(content);
}

document.getElementsByClass = function(className, tagName) {
	if (!tagName) tagName = '*';
	var out = new Array();
	var elements = this.getElementsByTagName(tagName);
	for (var i=0; i<elements.length; i++) {
		var classes = elements[i].className.split(' ');
		for (var j=0; j<classes.length; j++) {
			if (classes[j] == className) {
				out[out.length] = elements[i];
				break;
			}
		}
	}
	return out;
}

function storeCookie(name, value) {
	document.cookie = escape(name) + '=' + escape(value);
}

function getCookie(name) {
	var cookie = ";" + document.cookie + ";";
	var start = cookie.indexOf(";" + name + "=");
	if (start == -1) return null;
	start += name.length + 2;
	return unescape(cookie.substring(start, cookie.indexOf(";", start)));
}

function toggle_display(id) {
	var elm = getElementFromID(id);
	if (!elm) return;
	if (elm.style.display == 'none') {
		elm.style.display = '';
	} else {
		elm.style.display = 'none';
	}
}

/**
 * This is intimately related to the smarty {tabs} tag.
 */
function switch_tabs(tab_group, tab_id) {
	var tabs = getElementFromID('tabs_' + tab_group).getElementsByTagName("table");
	var which = "tabs_" + tab_group + "_" + tab_id;
	for (var i=0; i<tabs.length; ++i) {
		var tds = tabs[i].getElementsByTagName("td");
		if (tabs[i].id == which) {
			tabs[i].className = "tab_active";
			getElementFromID(tabs[i].id + "_body").style.display = "";
		} else {
			tabs[i].className = "tab_inactive";
			getElementFromID(tabs[i].id + "_body").style.display = "none";
		}
	}
}
