// Redirects the client browser to the provided url after the provided time period (millis).
// -jl
function redirectAfter(aUrl, aWaitMillis)
{
  if (aUrl && aWaitMillis)
  {
    callLater("redirect('"+aUrl+"')", aWaitMillis);
  }
}

// Redirects the client browser to the provided url making sure
// the current page is not added to the browser history.
// -jl
function redirect(aUrl)
{
  if (aUrl)
  {
    document.location.replace(getAbsoluteUrl(aUrl));
  }
}
// Takes a url and fixes its "relativity" to ensure the
// base href is considered, returns the absolute url.
// -jl
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;
}

// Sets up a function to be called after some number
// of milliseconds.
// -jl
function callLater(aFunctionName, aMillis)
{
  if (aFunctionName && aMillis)
  {
    setTimeout(aFunctionName, aMillis);
  }
}

// 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);
}

function unpaint(aElement)
{
  paint(aElement, "transparent");
}

function paint(aElement, aColor)
{
  if (aElement)
  {
    aElement.style.backgroundColor = aColor;
  }
}

// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth() 
{
  return window.innerWidth != null 
         ? window.innerWidth 
         : document.documentElement 
           && 
           document.documentElement.clientWidth 
           ? document.documentElement.clientWidth 
           : document.body != null 
             ? document.body.clientWidth 
               : null;
} 
function pageHeight() 
{
  return  window.innerHeight != null
          ? window.innerHeight 
          : document.documentElement 
            && 
            document.documentElement.clientHeight 
            ? document.documentElement.clientHeight 
            : document.body != null
              ? document.body.clientHeight 
              : null;
} 
function posLeft() 
{
  return typeof window.pageXOffset != 'undefined' 
         ? window.pageXOffset 
         : document.documentElement 
           && 
           document.documentElement.scrollLeft 
           ? document.documentElement.scrollLeft 
           : document.body.scrollLeft 
             ? document.body.scrollLeft 
               : 0;
} 
function posTop() 
{
  return typeof window.pageYOffset != 'undefined' 
         ? window.pageYOffset 
         : document.documentElement 
           && 
           document.documentElement.scrollTop 
           ? document.documentElement.scrollTop 
           : document.body.scrollTop 
             ? document.body.scrollTop 
             : 0;
} 
function posRight()
{
  return posLeft() + pageWidth();
} 
function posBottom() 
{
  return posTop() + pageHeight();
}                    