var num_elements = 10;
var elements = new Array(num_elements);

var i = 0;
elements[i++] = "default_div";
elements[i++] = "sidemenu11";  //"bioinformatics";
elements[i++] = "sidemenu123"; //"education";
elements[i++] = "sidemenu12";  //"genomic_medicine";
elements[i++] = "sidemenu13";  //"infectious_disease";
elements[i++] = "sidemenu14";  //"joint_technology_center";
elements[i++] = "sidemenu15";  //"microbial_environment_genomics";
elements[i++] = "sidemenu16";  //"plant_genomics";
elements[i++] = "sidemenu17";  //"policy_center";
elements[i++] = "sidemenu18";  //"synthetic_biology_bioenergy";

//*************************************************************************************************************************************
// This function takes the form field and checks that it is not null and not empty.
//
// @param field : the field to test
// @return true if the field was filled in, false otherwise
//*************************************************************************************************************************************
function validate_required(field) {
  with (field) {
    if(value == null || value == "") { 
      return false;
    } // end if(value == null || value == "")
    else { 
      return true;
    } // end else
  } // end with (field)
} // end function validate_required(field)
//*************************************************************************************************************************************

//*************************************************************************************************************************************
// This function takes a form and checks that the appropriate fields have been filled in correctly. 
//
// @param thisform : a reference to a form object
// @return true if a valid form, false otherwise
//*************************************************************************************************************************************
function validate_form(thisform) {
  with (thisform) {
    if(validate_required(search_field) == false) { 
      return false;
    } // end if(validate_required(search_field) == false)
    else { 
      return true; 
    } // end else
  } // end with (thisform)
} // end function validate_form(thisform)
//*************************************************************************************************************************************

//*************************************************************************************************************************************
// This function is used to grab the y-coordinate of the mouse. 
//
// @param e : the event
//*************************************************************************************************************************************
function mouseY(e) {
  var c = 0;
  e = e || window.event;
  if(e.pageY) {
    c = e.pageY;
  } // end if(e.pageY)
  else if(typeof(e.clientY) == 'number') {
    var dE = document.documentElement;
    c = e.clientY + document.body.scrollTop + (dE ? dE.scrollTop : 0); 
  } // end else if(typeof(e.clientY) == 'number') 

  return c; 
} // end function mouseY(e)
//*************************************************************************************************************************************

//*************************************************************************************************************************************
// This function either opens or closes the quicklinks menu depending on the value of var1. This function was written to fix errors
// generated by the quicklinks menu. 
//
// @param var1 : true if pulling the menu down, false otherwise
// @return : !var1
//*************************************************************************************************************************************
function openclose(var1) {
  if(var1) {
    Effect.BlindDown(var1);
    return false; 
  } // end if(var1)
  else {
    Effect.BlindUp(var1);
    return true; 
  } // end else
} // end function openclose(var1)
//*************************************************************************************************************************************

//*************************************************************************************************************************************
// This function resets the queue for the quicklinks.
//*************************************************************************************************************************************
function reset_queue() {
  document.onmousemove = setY;
  var y = getY();
  var limit = 192;
        
  if(y > limit) { 
    visible = false;
    new Effect.BlindUp("quicklinks", {duration: 0.8, queue: {position:'end', scope:'quickscope', limit:2}} || {}); 
  } // end if(y > limit)
	
  document.onmousemove = null;
} // end function reset_queue()
//*************************************************************************************************************************************

//*************************************************************************************************************************************
// This function determines whether the event is the equivalent of the microsoft mouseleave or mouseenter events. 
// (http://dynamic-tools.net/toolbox/isMouseLeaveOrEnter/)
//
// @param e : the event that triggered this action
// @param handler: the div that this action is triggered
//*************************************************************************************************************************************
function isMouseLeaveOrEnter(e, handler) {
  var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement;
  while (reltg && reltg != handler) { 
    reltg = reltg.parentNode;
  } // end while (reltg && reltg != handler)
  return (reltg != handler); 
} // end function isMouseLeaveOrEnter(e, handler)
//*************************************************************************************************************************************

//*************************************************************************************************************************************
// This function changes the main graphic image to be the image passed in by the calling function. 
//
// @param to_visible : the element to become visible
//*************************************************************************************************************************************
function change_graphic(to_visible) {
  var my_element;
/*	
  for(i = 1; i < num_elements; i++) { 
    document.getElementById(elements[i] + '_link').style.backgroundColor = "#ffffff";
    document.getElementById(elements[i] + '_link').style.color = "#686868";
    document.getElementById(elements[i]).style.visibility = "hidden";
  } // end for(i = 1; i < num_elements; i++) 
  */
  document.getElementById("default_div").style.visibility = "hidden";  

  if(to_visible != "default_div") {
    var visible_link = to_visible + "_link";
    document.getElementById(visible_link).style.backgroundColor = "#686868"; 
    document.getElementById(visible_link).style.color = "#ffffff"; 
  }  // end if(to_visible != "default_div")

  document.getElementById(to_visible).style.visibility = "visible";
} // end function change_graphic(path)
//*************************************************************************************************************************************

//*************************************************************************************************************************************
// For some reason, IE6 is mucking up the top menu on the search page even though it is the same code. This function fixes it. 
//*************************************************************************************************************************************
function fix_ie_6() { 
  var version = navigator.appVersion;
  var index = version.indexOf("MSIE");

  if(index > 0) { 
    version = version.substring(index + 5, index + 8) + 0;
         
    if(version < 6.1) { 
      document.getElementById("main_top_menu").style.width = 641;
    } // end if(version < 6.1) 
  } // end if(index > 0)
} // end function fix_ie_6() 
//*************************************************************************************************************************************

var tempY = 0;
function setY(e){tempY = mouseY(e);}
function getY() { return tempY; }

//*************************************************************************************************************************************
// This function adds an event to a certain event. This is done so the onload calls can be unobtrusive and dynamic. This function 
// comes from http://onlinetools.org/articles/unobtrusivejavascript/chapter4.html .
//
// @param obj : the object to load the event to
// @param evType : the event indicating when to execute the function
// @param fn : the function to execute
// @return : true or false depending on the success of the execution
//*************************************************************************************************************************************
function addEvent(obj, evType, fn) { 
  if(obj.addEventListener) { 
    obj.addEventListener(evType, fn, false); 
    return true; 
  } // end if(obj.addEventListener)
  else if(obj.attachEvent) { 
    var r = obj.attachEvent("on" + evType, fn); 
    return r; 
  } // end else if (obj.attachEvent) 
  else { 
    return false; 
  } // end else
} // end function addEvent(obj, evType, fn)
//*************************************************************************************************************************************


/*
  Developed by Robert Nyman, http://www.robertnyman.com
  Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function(className, tag, elm) {
  if(document.getElementsByClassName) {
    getElementsByClassName = function(className, tag, elm) {
      elm = elm || document;

      var elements = elm.getElementsByClassName(className),
        nodeName = (tag) ? new RegExp("\\b" + tag + "\\b", "i") : null,
        returnElements = [],
        current;

      for(var i = 0, il = elements.length; i < il; i += 1) {
        current = elements[i];

        if(!nodeName || nodeName.test(current.nodeName)) {
          returnElements.push(current);
        } // end if(!nodeName || nodeName.test(current.nodeName))
      } // end for(var i = 0, il = elements.length; i < il; i += 1)

      return returnElements;
    };
  } // end if(document.getElementsByClassName)
  else if(document.evaluate) {
    getElementsByClassName = function(className, tag, elm) {
      tag = tag || "*";
      elm = elm || document;

      var classes = className.split(" "),
        classesToCheck = "",
        xhtmlNamespace = "http://www.w3.org/1999/xhtml",
        namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace) ? xhtmlNamespace : null,
        returnElements = [],
        elements,
        node;

      for(var j = 0, jl = classes.length; j < jl; j += 1) {
        classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
      } // end for(var j = 0, jl = classes.length; j < jl; j += 1)

      try {
        elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
      } // end try
      catch(e) {
        elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
      } // end catch(e)

      while((node = elements.iterateNext())) {
        returnElements.push(node);
      } // end while((node = elements.iterateNext()))

      return returnElements;
    };
  } // end else if(document.evaluate)
  else {
    getElementsByClassName = function(className, tag, elm) {
      tag = tag || "*";
      elm = elm || document;

      var classes = className.split(" "),
        classesToCheck = [],
        elements = (tag === "*" && elm.all) ? elm.all : elm.getElementsByTagName(tag),
        current,
        returnElements = [],
        match;

      for(var k = 0, kl = classes.length; k < kl; k += 1) {
        classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
      } // end for(var k = 0, kl = classes.length; k < kl; k += 1)

      for(var l = 0, ll = elements.length; l < ll; l += 1) {
        current = elements[l];
        match = false;

        for(var m = 0, ml = classesToCheck.length; m < ml; m += 1) {
          match = classesToCheck[m].test(current.className);

          if(!match) {
            break;
          } // end if(!match)
        } // end for(var m = 0, ml = classesToCheck.length; m < ml; m += 1)

        if(match) {
          returnElements.push(current);
        } // end if(match)
      } // end for(var l = 0, ll = elements.length; l < ll; l += 1)

      return returnElements;
    };
  } // end else
  
  return getElementsByClassName(className, tag, elm);
};

//*************************************************************************************************************************************
// This function searches the page for any link with the external-link-new-window class. It adds an onclick event to open those links
// in a new window. 
//*************************************************************************************************************************************
function set_link_properties() { 
  var links = getElementsByClassName('external-link-new-window', 'a');

  for(var i = 0; i < links.length; i++) { 
    links[i].onclick = external_link;
  } // end for(var i = 0; i < links.length; i++) 
} // end function set_link_properties()
//*************************************************************************************************************************************

//*************************************************************************************************************************************
// This function sets the property of the element received to open a window with the address of what is set in the href attribute. It
// returns the success of this operation. 
//
// @param e : the event to add this property to
// @return true if a new window was opened, false otherwise
//*************************************************************************************************************************************
function external_link(e) { 
  e = e || window.event;
  var el = e.target || e.srcElement;
  
  return !window.open(el.href);
} // end function external_link(e)
//*************************************************************************************************************************************

addEvent(window, 'load', set_link_properties);