﻿/* faq.js written by Todd Skoglund, August 23rd, 2006 for the secondary fields website of harvard college.  Adapted from a similar script found in the fantastic book DOM Scripting by Jeremy Keith */
/* this script attempts to make an unobtrusive and gracefully degrading method of using javascript to enhance the functionality of the site.  It is meant to alternately show and hide content on a page based on selections that the user makes (specifically, it displays and hides answers to faq questions that the user clicks on). The page remains accessible as the javascript is not necessary and the full content should display if the user has javascript turned off. */


/* this function prepareNav tests for browser compatibility and then adds the onclick function to every link in subnav to call the next function (when clicked) */
function preparePage() {
	if (!document.getElementsByTagName) return false;  //tests the browser's js support
	if (!document.getElementById) return false; //tests the browser's js support
	if (!document.getElementById("faqList")) return false; //tests for the existence of a faqList element, essential to the proper working of the script
	if (document.getElementById("instruction")) {
		document.getElementById("instruction").style.display = "block";
	}
	var faqlist = document.getElementById("faqList"); // gets the element faqList
	var links = faqlist.getElementsByTagName("a"); // makes an array of all the links in the parent element of faqList
	for ( var i=0; i < links.length; i++) {	// loops through all the links in faqList and assigns an event handler (onclick) to each link that calls the next function showHide with the element with the id of the link's title
		if (links[i].getAttribute("title")) { // checks to make sure the link has a title so it will skip regular a tags in the html
			var name = links[i].getAttribute("title"); // finds the title attribute of each link
			var answer = document.getElementById(name); // gets the element with the id equal to the title attribute of the clicked link
			answer.style.display = "none"; // hides all the faqLists except the listed ones so that if no JS is present, the page displays with full content
			links[i].onclick = function() { // creates an onclick event handler for each link and assigns it to call the showHide function with the clicked link as an argument
				return showHide(this); // runs the showContent with the current clicked link as the argument
			}
		}
	}
}

/* this function makes the chosen element display and then makes sure all the other elements remain hidden */
function showHide(whichanswer) {
	var id = whichanswer.getAttribute("title"); // gets the title of the selected link
	var content = document.getElementById(id); // gets the element that has an id equal to the title of the selected link
	if (content.style.display == "none") { // if the element is currently hidden, show it now and return false
		content.style.display = "block";
		return false;
	}
	if (content.style.display != "none") { // if the element is currently not hidden and it's clicked, then hide it and return false
		content.style.display = "none";
		return false;
	}
}

/* this is just a handy generic trick to allow you to add to the list at the bottom of this page to begin any number of functions when the page loads */ 
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/* the following command begins the following list of scripts after the page has loaded. You can add as many as you want thanks to the above addLoadEvent script */
addLoadEvent(preparePage);