/*
 * <copyright>
 *  Copyright (c) 2003 by Hyperwave AG
 * </copyright>
 *
 * <file>
 *  Name:        cswindowlib.js
 *  Created:     2003-05-16 by sweber
 *  Description: collection of function to handle the window events "onload", "onunload", "onresize"
 *
 *  $Id: cswindowlib.js 143440 2008-08-27 14:53:30Z jschipf $
 * </file>
 */


//--------------------------------------------------------------------
/**
 * This function sizes the iframe with the passed id so that the whole content is visible.
 * Is for example used in cluster handling and for (full) collection heads
 *
 * @param theFrame: object: iframe object that should be sized
 */

function sizeframe(theFrame)
{
  try {
    var doc = theFrame.contentWindow.document;
    var dummy = doc.createElement("div");
    doc.body.appendChild(dummy);

    var has_body_style = false;
    if (doc.styleSheets) {
      for (var i = 0; i < doc.styleSheets.length; i ++) {
        var rules = doc.styleSheets[i].cssRules || doc.styleSheets[i].rules; // one for FF and one for IE
        for (var j = 0; j < rules.length; j ++) {
          var a_rule = rules[j];
          if (a_rule.selectorText &&
              a_rule.selectorText.toLowerCase() == "body") {
            var dummy2 = doc.createElement("div");

            if (a_rule.style.margin && a_rule.style.margin.match(/[1-9]/)) { // has a value not zero
              has_body_style = true;
              dummy2.style.margin = a_rule.style.margin;
            }
            if (a_rule.style.borderWidth && a_rule.style.borderWidth.match(/[1-9]/)) {
              has_body_style = true;
              dummy2.style.border = a_rule.style.border;
            }
            if (a_rule.style.padding && a_rule.style.padding.match(/[1-9]/)) {
              has_body_style = true;
              dummy2.style.padding = a_rule.style.padding;
            }

            if (has_body_style) {
              dummy.appendChild(dummy2);
            }
          }
        }
      }
    }
    var height1=dummy.offsetTop + (has_body_style ? dummy.offsetHeight : 0);
    var height2=doc.body.scrollHeight;
    if (height2<=300)
    {  // 300 is the initial height of iframe. When the size is less than this
       // the scroll information is no longer correct
      height2=height1;
    }

    theFrame.style.height = Math.max(height1,height2);
    doc.body.removeChild(dummy);
    doc.body.style.overflowX = "auto";
    doc.body.style.overflowY = "hidden";
    return;
  }
  catch (e) {
  }
  
  try
  {
    var id = theFrame.id;
    var element=document.getElementById(id);
    if (navigator.appName.indexOf("Netscape")!=-1)
    {
      try
      {
        var h=eval(id+".document.body.offsetHeight");
      }
      catch(e)
      { // in this case [id].document is not defined, we assume the initial height 
        h=300;
      }
      
      if (h!=300)
      {
        element.style.height = h + 35;
      }
      else
      {
        element.style.height = h+element.contentWindow.scrollMaxY+35;
      }
    }
    else
    {
      var element2=eval(id);
      element.style.height = element2.document.body.scrollHeight;
    }
  }
  catch(e) {
  }
}

function hw_htmlEscape(string)
{
  if (typeof string !="string") return string;
  var outstr=string;
  outstr=outstr.replace(/&/g,'&amp;');
  outstr=outstr.replace(/</g,"&lt;");
  outstr=outstr.replace(/>/g,"&gt;");
  outstr=outstr.replace(/\"/g,'&quot;');
  outstr=outstr.replace(/\'/g,'&#39;');
  return outstr;
}

function hw_htmlUnescape(string)
{
  if (typeof string !="string") return string;
  var outstr=string;
  outstr=outstr.replace(/&lt;/g,"<");
  outstr=outstr.replace(/&gt;/g,">");
  outstr=outstr.replace(/&quot;/g,'\"');
  outstr=outstr.replace(/&#39;/g,'\'');
  outstr=outstr.replace(/&amp;/g,'&');
  return outstr;
}

/**
 * This function escape a string for use in js strings.
 * mode 0: CR -> \r, LF -> \n, \ -> \\, ' -> \', " -> \"
 * mode 1: CR -> \r, LF -> \n, \ -> \\
 *
 * @param string: string: string to escape
 * @param mode: number | 0: mode for escapenig
 *
 * @return: string: escaped string
 */
function escapeString(string, mode)
{
  if (!string)
    return "";
  if (mode & 1) {
    var re = /([\r\n\\])/g;
  }
  else {
    var re = /([\"\'\r\n\\])/g;
  }
  return string.replace(re, function (theMatch, theChar) {
    switch (theChar) {
      case "\r": return "\\r";
      case "\n": return "\\n";
      default: return "\\" + theChar;
    }
  });
}

/**
 * This function unescape a string as complement to escapeString.
 * mode 0: \r -> CR, \n -> LF, \\ -> \, \' -> ', \" -> "
 * mode 1: \r -> CR, \n -> LF, \\ -> \
 *
 * @param string: string: string to unescape
 * @param mode: number | void (0): mode for unescapenig
 *
 * @return: string: unescaped string
 */
function unescapeString(string, mode)
{
  if (!string)
    return "";
  if (mode & 1) {
    var re = /\\([\\nr])/g;
  }
  else {
    var re = /\\([\"\'\\nr])/g;
  }
  return string.replace(re, function (theMatch, theChar) {
    switch (theChar) {
      case "r": return "\r";
      case "n": return "\n";
      default: return theChar;
    }
  });
}

if (!window.FILE_CSWINDOWLIBJS_INCLUDED) {
  CSWINDOWLIBJS_CATCHERRORS = false;
  // global onload array
  ONLOAD_ARRAY = [];
  // if exists, add original onload to the array
  if (typeof window.onload == "function") {
    ONLOAD_ARRAY[ONLOAD_ARRAY.length] = window.onload;
  }
  
  // global onunload array
  ONUNLOAD_ARRAY = [];
  // if exists, add original onunload to the array
  if (typeof window.onunload == "function") {
    ONUNLOAD_ARRAY[ONUNLOAD_ARRAY.length] = window.onunload;
  }
  
  // global onresize array
  ONRESIZE_ARRAY = [];
  // if exists, add original onresize to the array
  if (typeof window.onresize == "function") {
    ONRESIZE_ARRAY[ONRESIZE_ARRAY.length] = window.onresize;
  }
  
  function cswindowlib_doIt(execArray) {
    for (var i = 0; i < execArray.length; i ++) {
      var exec = execArray[i];

      if (CSWINDOWLIBJS_CATCHERRORS) {
        try {
          if (typeof exec == "string") {
            eval(exec);
          }
          else {
            exec();
          }
        }
        catch (e) {
          alert(e.name + ': ' + e.message);
        }
      }
      else {
        if (typeof exec == "string") {
          eval(exec);
        }
        else {
          exec();
        }
      }
    }
  }

  // overwrite the onload function
  window.onload = function () {
    cswindowlib_doIt(ONLOAD_ARRAY);
  }
  
  // overwrite the onunload function
  window.onunload = function () {
    cswindowlib_doIt(ONUNLOAD_ARRAY);
  }

   
  // overwrite the onresize function
  window.onresize = function () {
    cswindowlib_doIt(ONRESIZE_ARRAY);
  }
  
  //--------------------------------------------------------------------
  /**
   * handler for onload's
   *
   * @param aFuncStr: string: string to eval onload
   */
  function addToOnload(aFuncStr) {
    if (aFuncStr) {
      ONLOAD_ARRAY[ONLOAD_ARRAY.length] = aFuncStr;
    }
  }
  
  //--------------------------------------------------------------------
  /**
   * handler for onunload's
   *
   * @param aFuncStr: string: string to eval onunload
   */
  function addToOnunload(aFuncStr) {
    if (aFuncStr) {
      ONUNLOAD_ARRAY[ONUNLOAD_ARRAY.length] = aFuncStr;
    }
  }
  
  //--------------------------------------------------------------------
  /**
   * handler for onresize's
   *
   * @param aFuncStr: string: string to eval onresize
   */
  function addToOnresize(aFuncStr) {
    if (aFuncStr) {
      ONRESIZE_ARRAY[ONRESIZE_ARRAY.length] = aFuncStr;
    }
  }

  //--------------------------------------------------------------------
  /**
   * Calling this function, an interval will be started checking the
   * opener window. When the opener window is closed, the current window
   * will be closed automatically.
   */
  function closeOnOpenerClose()
  {
    function checkOpener() {
      try {
        if (!window.opener || window.opener.closed) {
          window_close();
        }
      }
      catch (e) {
        window_close();
      }
    }
    setInterval(checkOpener, 250);
  }
}

window.FILE_CSWINDOWLIBJS_INCLUDED = true;
