//------------------------------------------------------------------------------
// Copyright (c) by Oliver Musebrink.
//
// Name: OM_Util.js
// Desc: Some helpful JavaScript functions compatible with
//       Mozilla Firefox, Netscape Navigator and Microsoft Internet Explorer.
//------------------------------------------------------------------------------


// Detect Netscape 4 that needs different code
var ns4 = ((navigator.appName.indexOf("Netscape") != -1) && (!document.getElementById));


//------------------------------------------------------------------------------
// Name: OM_GetFrame()
// Desc: Get a frame.
//------------------------------------------------------------------------------
function OM_GetFrame(name)
{
  // Top frame
  if(name == '_top')
  {
    return(top);
  }

  // Parent frame
  else if(name == '_parent')
  {
    return(parent);
  }

  // This frame
  else if(name == '_self')
  {
    return(this);
  }

  // Other frame
  else
  {
    return(OM_GetFrameRecursively(top, name));
  }
}


//------------------------------------------------------------------------------
// Name: OM_GetFrameRecursively()
// Desc: Helper function for OM_GetFrame().
//------------------------------------------------------------------------------
function OM_GetFrameRecursively(parentframe,
                                name)
{
  var frame = null;
  var i;


  // No more frames
  if(parentframe.frames == null)
  {
    return(null);
  }

  // Compare sub frame names
  for(i = 0; i < parentframe.frames.length; i++)
  {
    if(parentframe.frames[i].name == name)
    {
      return(parentframe.frames[i]);
    }
  }

  // Recursively continue with sub frames
  for(i = 0; i < parentframe.frames.length; i++)
  {
    // Get frame
    frame = OM_GetFrameRecursively(parentframe.frames[i], name);

    // Frame was found
    if(frame != null)
    {
      return(frame);
    }
  }


  // Retunr frame
  return(frame);
}


//------------------------------------------------------------------------------
// Name: OM_GetObject()
// Desc: Returns object(s) with the given ID or name.
//------------------------------------------------------------------------------
function OM_GetObject()
{
  var args  = OM_GetObject.arguments,
      frame = null,
      obj   = null;


  // Frame dependent search
  if(args.length == 2)
  {
    // Get frame
    frame = OM_GetFrame(args[0]);

    if(frame != null)
    {
      // Get object(s)
      obj = OM_GetObjectRecursively(frame, args[1]);
    }
  }
  else if(args.length == 1)
  {
    // Get object(s)
    obj = OM_GetObjectRecursively(top, args[0]);
  }


  // Return object(s)
  return(obj);
}


//------------------------------------------------------------------------------
// Name: OM_GetObjectRecursively()
// Desc: Helper function for OM_GetObject().
//------------------------------------------------------------------------------
function OM_GetObjectRecursively(currentframe,
                                 idname)
{
  var obj = null;
  var i;


  // Get object by ID
  obj = currentframe.document.getElementById(idname);

  if(obj != null)
  {
    return(obj);
  }

  // Get object(s) by name
  obj = currentframe.document.getElementsByName(idname);

  if(obj != null)
  {
    if(obj.length > 0)
    {
      return(obj);
    }
  }

  // Recursively continue with sub frames
  for(i = 0; i < currentframe.frames.length; i++)
  {
    // Get object(s)
    obj = OM_GetObjectRecursively(currentframe.frames[i], idname);

    if(obj != null)
    {
      return(obj);
    }
  }


  // Return object(s)
  return(obj);
}


//------------------------------------------------------------------------------
// Name: OM_HideObject()
// Desc: Hides an element.
//------------------------------------------------------------------------------
if(ns4)
{
  // Netscape 4 version
  OM_HideObject = function(idname)
  {
    var obj = null;
    var i;


    // Get object
    obj = OM_GetObject(idname);

    // Hide object
    if(obj != null)
    {
      if(obj.length)
      {
        for(i = 0; i < obj.length; i++)
        {
          if(obj[i].style != null)
          {
            if(obj[i].style.visibility != null)
            {
              obj[i].style.visibility = "hide";
            }

            if(obj[i].style.display != null)
            {
              obj[i].style.display = "none";
            }
          }
        }
      }
      else
      {
        if(obj.style != null)
        {
          if(obj.style.visibility != null)
          {
            obj.style.visibility = "hide";
          }

          if(obj.style.display != null)
          {
            obj.style.display = "none";
          }
        }
      }
    }
  }
}
else
{
  // Other version
  OM_HideObject = function(idname)
  {
    var obj = null;


    // Get object
    obj = OM_GetObject(idname);

    // Hide object
    if(obj != null)
    {
      if(obj.length)
      {
        for(i = 0; i < obj.length; i++)
        {
          if(obj[i].style != null)
          {
            if(obj[i].style.visibility != null)
            {
              obj[i].style.visibility = "hidden";
            }

            if(obj[i].style.display != null)
            {
              obj[i].style.display = "none";
            }
          }
        }
      }
      else
      {
        if(obj.style != null)
        {
          if(obj.style.visibility != null)
          {
            obj.style.visibility = "hidden";
          }

          if(obj.style.display != null)
          {
            obj.style.display = "none";
          }
        }
      }
    }
  }
}


//------------------------------------------------------------------------------
// Name: OM_ShowObject()
// Desc: Shows an element.
//------------------------------------------------------------------------------
if(ns4)
{
  // Netscape 4 version
  OM_ShowObject = function(idname)
  {
    var obj = null;
    var i;


    // Get object
    obj = OM_GetObject(idname);

    // Hide object
    if(obj != null)
    {
      if(obj.length)
      {
        for(i = 0; i < obj.length; i++)
        {
          if(obj[i].style != null)
          {
            if(obj[i].style.visibility != null)
            {
              obj[i].style.visibility = "show";
            }

            if(obj[i].style.display != null)
            {
              obj[i].style.display = "block";
            }
          }
        }
      }
      else
      {
        if(obj.style != null)
        {
          if(obj.style.visibility != null)
          {
            obj.style.visibility = "show";
          }

          if(obj.style.display != null)
          {
            obj.style.display = "block";
          }
        }
      }
    }
  }
}
else
{
  // Other version
  OM_ShowObject = function(idname)
  {
    var obj = null;


    // Get object
    obj = OM_GetObject(idname);

    // Show object
    if(obj != null)
    {
      if(obj.length)
      {
        for(i = 0; i < obj.length; i++)
        {
          if(obj[i].style != null)
          {
            if(obj[i].style.visibility != null)
            {
              obj[i].style.visibility = "visible";
            }

            if(obj[i].style.display != null)
            {
              obj[i].style.display = "block";
            }
          }
        }
      }
      else
      {
        if(obj.style != null)
        {
          if(obj.style.visibility != null)
          {
            obj.style.visibility = "visible";
          }

          if(obj.style.display != null)
          {
            obj.style.display = "block";
          }
        }
      }
    }
  }
}


//------------------------------------------------------------------------------
// Name: OM_PreloadImages()
// Desc: Preloads images.
//------------------------------------------------------------------------------
function OM_PreloadImages()
{
  var args = OM_PreloadImages.arguments;
  var i,
      j;


  // create new image list for the current document
  if(document.aImageList == null)
  {
    document.aImageList = new Array();
  }

  // Load images
  j = document.aImageList.length;

  for(i = 0; i < args.length; i++)
  {
    // Create new image
    document.aImageList[j + i] = new Image();

    // Set image source
    if(document.aImageList[j + i] != null)
    {
      document.aImageList[j + i].src = args[i];
    }
  }
}


//------------------------------------------------------------------------------
// Name: OM_SetImageSource()
// Desc: Sets an image's source.
//------------------------------------------------------------------------------
function OM_SetImageSource(idname,
                           imgSrc)
{
  var img = null;


  // Get image
  img = OM_GetObject(idname);

  // Set image source
  if(img != null)
  {
    img.src = imgSrc;
  }
}


//------------------------------------------------------------------------------
// Name: OM_SwapImages()
// Desc: Swaps an image.
//------------------------------------------------------------------------------
function OM_SwapImages(idname,
                       imgSrc1, imgWidth1, imgHeight1,
                       imgSrc2, imgWidth2, imgHeight2)
{
  var img = null;


  // Get image
  img = OM_GetObject(idname);

  // Set image source
  if(img != null)
  {
    if(img.src.search(imgSrc2) != -1)
    {
      // Set image 1
      img.src    = imgSrc1;
      img.width  = imgWidth1;
      img.height = imgHeight1;
    }
    else if(img.src.search(imgSrc1) != -1)
    {
      // Set image 2
      img.src    = imgSrc2;
      img.width  = imgWidth2;
      img.height = imgHeight2;
    }
  }
}


//------------------------------------------------------------------------------
// Name: OM_ReloadPage()
// Desc: Reloads the current page.
//------------------------------------------------------------------------------
function OM_ReloadPage()
{
  location.reload();
}


//------------------------------------------------------------------------------
// Name: OM_PreviousPage()
// Desc: Loads the page previously visited.
//------------------------------------------------------------------------------
function OM_PreviousPage()
{
  history.go(-1);
}


//------------------------------------------------------------------------------
// Name: OM_NextPage()
// Desc: Loads the page last visited.
//------------------------------------------------------------------------------
function OM_NextPage()
{
  history.go(1);
}


//------------------------------------------------------------------------------
// Name: OM_LoadPage()
// Desc: Loads a page.
//------------------------------------------------------------------------------
function OM_LoadPage()
{
  var args  = OM_LoadPage.arguments;
  var frame = null;
  var i;


  // Use top frame and set location
  if(args.length == 1)
  {
    location = args[0];
  }


  // Use specified frame
  if(args.length == 2)
  {
    // Get frame
    frame = OM_GetFrame(args[0]);

    // Set location
    if(frame != null)
    {
      frame.location = args[1];
    }
  }
}


//------------------------------------------------------------------------------
// Mapping table
//
// ( 2)    32 -  33    <-->     0 -  1
// (25)    35 -  59    <-->     2 - 26
// ( 1)    61          <-->    27
// (28)    63 -  90    <-->    28 - 55
// ( 1)    92          <-->    56
// (33)    94 - 126    <-->    57 - 89
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
// Name: OM_MapCharacter()
// Desc: Maps a character.
//------------------------------------------------------------------------------
function OM_MapCharacter(ch)
{
  // Map character
  if     (ch >= 32 && ch <=  33) ch -= 32;
  else if(ch >= 35 && ch <=  59) ch -= 33;
  else if(ch == 61             ) ch -= 34;
  else if(ch >= 63 && ch <=  90) ch -= 35;
  else if(ch == 92             ) ch -= 36;
  else if(ch >= 94 && ch <= 126) ch -= 37;


  // Return mapped character
  return(ch);
}


//------------------------------------------------------------------------------
// Name: OM_RemapCharacter()
// Desc: Remaps a character.
//------------------------------------------------------------------------------
function OM_RemapCharacter(ch)
{
  // Remap character
  if     (ch >=  0 && ch <=  1) ch += 32;
  else if(ch >=  2 && ch <= 26) ch += 33;
  else if(ch == 27            ) ch += 34;
  else if(ch >= 28 && ch <= 55) ch += 35;
  else if(ch == 56            ) ch += 36;
  else if(ch >= 57 && ch <= 89) ch += 37;


  // Return remapped character
  return(ch);
}


//------------------------------------------------------------------------------
// Name: OM_DecodeLink()
// Desc: Decodes a link.
//------------------------------------------------------------------------------
function OM_DecodeLink(link)
{
  var declink,
      password,
      str;
  var ch,
      i,
      j,
      l;


  // Invalid argument
  if(link == null)
  {
    return(null);
  }


  // Get password length
  str = link.substr(0,   2 );
  l   = parseInt   (str, 16);

  // Get password
  password = link.substr(2, l);

  // Decode link
  declink = "";
  j       = 0;

  for(i = l + 2; i < link.length; i++)
  {
    // Get character
    ch = link.charCodeAt(i);

    // Decode character
    ch  = OM_MapCharacter(ch);
    ch += (90 - (password.charCodeAt(j) % 90));
    ch %= 90;
    ch  = OM_RemapCharacter(ch);

    // Next password character
    j++;

    if(j >= password.length)
    {
      j = 0;
    }

    // Add character
    declink += String.fromCharCode(ch);
  }


  // Return decoded link
  return(declink);
}


//------------------------------------------------------------------------------
// Name: OM_EncryptedLink()
// Desc: Opens an encrypted link.
//------------------------------------------------------------------------------
function OM_EncryptedLink(link)
{
  // Decrypt link and set location
  location.href = OM_DecodeLink(link);
}

