// Finds a document element by its 'id'.
// --
function getDocumentElementById(aId)
{
  return document.getElementById(aId);
}

// Finds a document element by its name.
// --
function getDocumentElementByName(aName)
{
  return document.all.namedItem[aName];
}

// Finds a document image by its name.
// --
function getDocumentImageByName(aName)
{
  return document.images[aName];
}

// Toggles the visibility of a layer
// --
function toggleLayer(aId)
{
  toggleDiv(aId);
}

// Toggles the visibility of a layer
// --
function toggleDiv(aId)
{
  if (isDivHidden(aId)) 
  { 
    unhideDiv(aId);
  }
  else
  { 
    hideDiv(aId);
  }
}

// Determines if a div layer is hidden.
// --
function isDivHidden(aId)
{
  if (aId != null)
  {
    var theDiv = getDocumentElementById(aId);
    return !(theDiv.style.display == "block");
  }
}

// Hides a div layer.
// --
function hideDiv(aId)
{
  if (aId != null)
  {
    var theDiv = getDocumentElementById(aId);
    if (theDiv)
    {
      theDiv.style.display = "none";
    }
  }
}

// Unhides a div layer.
// --
function unhideDiv(aId)
{
  if (aId != null)
  {
    var theDiv = getDocumentElementById(aId);
    if (theDiv)
    {
      theDiv.style.display = "block"; 
    }
  }
}

// Empties the contents of a div layer.
// --
function emptyDiv(aId)
{
  fillDiv(aId, "");
}

// Populates the contents of a div layer.
// --
function fillDiv(aId, aContent)
{
  if (aId != null)
  {
    var theDiv = getDocumentElementById(aId);
    if (theDiv)
    {
      theDiv.innerHTML = aContent; 
    }
  }
}


// Image toggler.
// --
function ImageToggler(aImgId, aToggleImg)
{
  this.currentImage  = getDocumentElementById(aImgId);
  this.nextImageSrc  = aToggleImg;
  new Image().src    = this.nextImageSrc;
}
ImageToggler.prototype = 
{
  toggle : function() 
  {
    var currSrc = this.currentImage.src;
    this.currentImage.src = this.nextImageSrc;
    this.nextImageSrc = currSrc;
  }
}

// Takes a url and fixes its "relativity" to ensure the
// base href is considered, returns the absolute url.
function getAbsoluteUrl(aUrl)
{
  var fixedUrl = aUrl;
  var baseTags = document.getElementsByTagName("BASE");
  if (baseTags && baseTags.length)
  {
    // Could already be absolute.
    if (aUrl.indexOf(baseTags[0].href) == -1)
    {
      fixedUrl = baseTags[0].href + aUrl;
    }
  }
  return fixedUrl;
}

// Shows the contents of the specified url
// in the provided div layer using a GET request.
function showContentInDiv(aDivId, aUrl)
{
  if (!aDivId) { alert("Unable to showContentInDiv() for an invalid layer.");return; }
  var div = getDocumentElementById(aDivId);
  if (!div) { alert("Unable to find layer for showContentInDiv() with layer id "+aDivId+".");return; }
  
  var req = getNewRequest();
  
  var callBack = function ()
        {
          if (isComplete(req))
          {
            if (isSuccess(req))
            {
              div.innerHTML = req.responseText;
            }
            else
            {
              div = "Error while connecting to "+aUrl+": "+req.status;
            }
          }
        };
  doGet(aUrl, callBack, true, req);
}

// Performs a GET request.
function doGet(aUrl, aCallback, aAsynch, aRequest)
{
  doRequest("GET", aUrl, aCallback, aAsynch, null, aRequest);
}
 
// Performs a request of either GET or POST
// to the provided url, using the provided request
// and callback, and sending the provided body (or null).
function doRequest(aType, aUrl, aCallback, aAsynch, aBody, aRequest)
{
  if (!aType) { alert("Call to doRequest() is invalid. Supply a type, either POST or GET.");return; }
  if (!aUrl) { alert("Call to doRequest() is invalid. Supply a url."); }
  if (!aCallback) { alert("Call to doRequest() is invalid. Supply a callback function.");return; }
  
  aRequest.open(aType, getAbsoluteUrl(aUrl), aAsynch);
  aRequest.onreadystatechange = aCallback;
  aRequest.send(aBody);
}

// Gets a new useable XMLHttpRequest, Msxml2.XMLHTTP, or Microsoft.XMLHTTP.
function getNewRequest()
{
  var xmlhttp = false;
  if (typeof XMLHttpRequest != 'undefined') 
  {
    try { xmlhttp = new XMLHttpRequest(); } 
    catch (e) { xmlhttp = false; }
  }
  if (!xmlhttp && (typeof ActiveXObject != 'undefined'))
  {
    try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } 
    catch (e) 
    {
      try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } 
      catch (ee) { xmlhttp = false; }
    }
  }
  return xmlhttp;
}

// Determines if the request was completed.
function isComplete(aReq)
{
  if (!aReq) { alert("Unable to determine isComplete() for an invalid request.");return; }
  return aReq.readyState == 4;
}
  
// Determines if the request is complete and
// the status was a success.
function isSuccess(aReq)
{
  if (!aReq) { alert("Unable to determine isSuccess() for an invalid request.");return; }
  return isComplete(aReq) && (aReq.status == 200);
}
  
// Determines if the request is complete and
// the status was a failure.
function isFailure(aReq)
{
  if (!aReq) { alert("Unable to determine isFailure() for an invalid request.");return; }
  return isComplete(aReq) && !isSuccess(aReq);
}