//
// you have to make an elmental_array like the one below in the page, before you include this file
//
// var elemental_array = {
//	"majorin" : "http://"+document.domain+"/common/high-school-questions/what-do-i-want-to-major-in.shtml",
//	"thingstodo" : "http://"+document.domain+"/common/high-school-questions/are-there-things-to-do-on-campus.shtml",
//	"prepforcollege" : "http://"+document.domain+"/common/high-school-questions/what-should-i-do-in-high-school-to-prepare-for-college.shtml"
// };

// This is a list of XMLHttpRequest-creation factory functions to try
_factories = [
    function() { return new XMLHttpRequest(); },
    function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
    function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
];

// When we find a factory that works, store it here.
_factory = null;

// Create and return a new XMLHttpRequest object.
//
// The first time we're called, try the list of factory functions until
// we find one that returns a non-null value and does not throw an
// exception. Once we find a working factory, remember it for later use.
//
newRequest = function() {
    if (_factory != null) return _factory();

    for(var i = 0; i < _factories.length; i++) {
        try {
            var factory = _factories[i];
            var request = factory();
            if (request != null) {
                _factory = factory;
                return request;
            }
        }
        catch(e) {
            continue;
        }
    }
    // If we get here, none of the factory candidates succeeded,
    // so throw an exception now and for all future calls.
    _factory  = function() {
        throw new Error("XMLHttpRequest not supported");
    }
    _factory(); // Throw an error
}

function hideAllContents()
{
	for (key in elemental_array) {
		elem = document.getElementById(key);
		if (elem)
		{
		 // if the link to show the contents is coded into the page,
		 // then window.link_already_exists will be set, and all we
		 // have to do is hide the contents of elem
		 if (window.link_already_exists) 
	 	 {
		  newElement = document.createElement(elem.tagName);
		  newElement.id=elem.id;
		  theParent= elem.parentNode;
		  theParent.removeChild(elem);
		  theParent.appendChild(newElement);
		  
		 }
		 else
		 {
		  if (!hidden_text) { hidden_text = "Click here for answer..."; }
		  elem.innerHTML = '<a style="color:blue" onClick="javascript:showContents(\'' + key + '\')">'+ hidden_text +'</a>';
		  closeSpanName = key+'Close';
 		  closeElem = document.getElementById(closeSpanName);
 		  if (closeElem) {
  			closeElem.innerHTML = '';
 		  }
		 } 
		
		 // if necessary, clear the close box
 		 closeSpanName = key+'Close';
 		 closeElem = document.getElementById(closeSpanName);
 		 if (closeElem) {
  			closeElem.innerHTML = "";
 		 }

 		}
	}
}

function showContents(element)
{
 // first, hide everything
 hideAllContents();
 
 // get the element that we need to change
 elem = document.getElementById(element);

 // let the user know something is happening
 elem.innerHTML = "Retrieving answer...";

 // create the XMLHttpRequest object
 req = new newRequest;
 
 // assign the URL for the request from the elemental_array
 the_url = elemental_array[element];
 
 // give the request object the url, use "false" to make the request synchronous
 req.open("GET",the_url,false);
 
 // set the User-Agent header so we know in the server logs that this page requested the include files
 req.setRequestHeader("User-Agent", "XMLHttpRequest"); 	

 // do the request
 req.send(null);

 // create a "close" link
 closeText = '<a href="javascript:hideAllContents();">Close</a>';
 closeSpanName = element+'Close';
 closeElem = document.getElementById(closeSpanName);
 if (closeElem) {
  closeElem.innerHTML = closeText;
 }

 // replace the innerHTML of the element with the response text
 elem.innerHTML = req.responseText;
 //elem.innerHTML = the_url;
}
