
// 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
}

gup = function() {
var regexS = "guid=[^.*]*";
var regex = new RegExp(regexS);
var results = regex.exec( window.location.href );
if(results == null) return "";
else return results[0].replace("guid=","");
}

function getY( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

function hideAllContents(flag)
{
 divs = document.getElementsByTagName('div');    
 for (var elem=0;elem<divs.length;elem++) 
 {
  thisid = divs[elem].id;
  if ( (thisid != undefined) && (thisid != 'othernews') && (thisid != 'footer') && (thisid != 'linkdiv') && (thisid != 'calendardiv') && (thisid != 'arounddiv') && ((thisid != 'a'+gup()+'-news')||flag)&&((thisid != 'a'+gup()+'-event')||flag) )
  {
   divs[elem].innerHTML='<a style="color:blue" onClick="javascript:showContents(\''+divs[elem].id.substring(1)+'\')">Click for this story...</a>';
  } // end if (divs[elem].id != '')
 } // end for (elem in divs)
 
 // also, if flag is not set (meaning hideAllContents was called by the onLoad
 // event from the body tag, scroll the window to the top of the li for the 
 // un-hidden news item
 if (!flag)
 {
  elem = document.getElementById(gup()+'-li');
  window.scrollTo(0,getY(elem));
 }
}

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

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

  // create the XMLHttpRequest object
  req = new newRequest;
 
  // assign the URL for the request from the elemental_array
  if (element.indexOf("-news")>-1)
  {
   the_url = "http://"+document.domain+"/common/retrieve-news.php?guid="+element;
  }
  else if (element.indexOf("-event")>-1)
  {
   the_url = "http://"+document.domain+"/common/retrieve-event.php?guid="+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;
}

