//
// 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.innerText)
		{
 		 text_to_keep = elem.innerText;
 		}
 		else
 		{
 		 text_to_keep = elem.textContent;
 		}
		elem.innerHTML = '<a onMouseOver="javascript:this.style.textDecoration=\'underline\';" onMouseOut="javascript:this.style.textDecoration=\'none\';" style="color:blue;" onClick="javascript:showContents(\'' + key + '\')">'+ text_to_keep +'</a>';
		elem.style.background='';
	}
}

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

 // 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);

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

 // highlight the menu item for this topic
 highlight(element);

}

function highlight(element)
{
// does soemthing to highlight element
elem=document.getElementById(element);
if (elem.innerText)
{
 text_to_keep = elem.innerText;
}
else
{
 text_to_keep = elem.textContent;
}
elem.innerHTML='<span style="background:#BCC2Cf">'+text_to_keep+'</span>';
}


function pickRandomTopic()
{
// pick a random key from elemental_array, and fill in content_div with it
// also, make a call to highlight()

 // get the element that we need to change
 elem = document.getElementById('content_div');

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

 // create the XMLHttpRequest object
 req = new newRequest;

 counter = 0;
 for (key in elemental_array) {
   counter++;
 }
 
 rando=Math.floor(Math.random()*counter);
 counter=0; 
 for (key in elemental_array) {
   if (counter == rando)
   {
    the_url=elemental_array[key];
    element=key;
   }
   counter++;
 }
  
 // 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);

 // replace the innerHTML of the element with the response text
 elem.innerHTML = req.responseText;
 
 // highlight the menu item for this topic
 highlight(element);
 
}