// ***************************************************************************
// *
// * EPAPER crossbrowser utilities
// * -------------------------------------------------------------------------
// * This file covers crossbrowser functions and javascriptextensions
// *
// * Created: 03.03.2005 pg
// *
// * c2005 Comyan GMBH
// ***************************************************************************

// Browser variables
// ***************************************************************************
var sAgent = navigator.userAgent.toLowerCase();
var iemac = ( parseInt(navigator.appVersion, 10) >= 4 && sAgent.indexOf("opera") == -1 && sAgent.indexOf("msie") != -1 && sAgent.indexOf("msie 4") == -1 && sAgent.indexOf("mac") != -1 ? true : false );
var ie = ( parseInt(navigator.appVersion, 10) >= 4 && sAgent.indexOf("opera") == -1 && sAgent.indexOf("msie") != -1 && sAgent.indexOf("msie 4") == -1 ? true : false );
var ie5 = ( ie && sAgent.indexOf("msie 5") != -1 ? true : false );
var ie55 = ( ie && sAgent.indexOf("msie 5.5") != -1 ? true : false );
var opera = ( sAgent.indexOf("opera") != -1 ) ? ( sAgent.substr( sAgent.indexOf( "opera" ) + 6, 4 ) > "7.02" ) ? true : false : false;
var gecko = ( sAgent.indexOf("mozilla") != -1 &&  sAgent.indexOf("gecko") != -1  && sAgent.indexOf("safari") == -1 ) ? true : false;
var safari = ( sAgent.indexOf("mozilla") != -1 &&  sAgent.indexOf("gecko") != -1  && sAgent.indexOf("safari") != -1 ) ? true : false;
var ns = ( sAgent.indexOf("mozilla") != -1 &&  sAgent.indexOf("netscape") != -1 ) ? true : false;
var ns4 = document.layers ? true : false;
var firefox = ( sAgent.indexOf("firefox") != -1 ) ? true : false;

// Function [push] for adding an item to a stack
// ***************************************************************************
if (window.Array && Array.prototype && !Array.prototype.push)
  Array.prototype.push = _Array_push;
function _Array_push ()
{
  for (var a = 0; a < arguments.length; a++)
    this[this.length] = arguments[a];
  return this.length;
}

// Function [pop] for getting an last item added from a stack
// ***************************************************************************
if (window.Array && Array.prototype && !Array.prototype.pop)
  Array.prototype.pop = _Array_pop;
function _Array_pop ()
{
  var lastElement = this[this.length-1];
  this.length = Math.max(this.length-1,0);
  return lastElement;
}

// Function [unshift]
// ***************************************************************************
if (window.Array && Array.prototype && !Array.prototype.unshift)
  Array.prototype.unshift = _Array_unshift;
function _Array_unshift ()
{
  var len;

  for (var a = 0; a < arguments.length; a++)
  {
    len = this.length;
    for (var i = len; i > 0; i--)
      this[i] = this[i-1];
    this[0] = arguments[a];
  }
  return this.length;
}

// Function [setCookie] sets an cookie
// ***************************************************************************
function setCookie (cookieName, cookieValue, expires, path, domain, secure)
{
  if ( document.cookie )
  {
    document.cookie =
      escape(cookieName) + '=' + escape(cookieValue)
      + (expires ? '; EXPIRES=' + expires.toGMTString() : '')
      + (path ? '; PATH=' + path : '')
      + (domain ? '; DOMAIN=' + domain : '')
      + (secure ? '; SECURE' : '');
  }
}

// Function [getCookie] gets values from an cookie
// ***************************************************************************
function getCookie (cookieName)
{
  var cookieValue = '';
  var posName;

  if ( document.cookie )
  {
    posName = document.cookie.indexOf(escape(cookieName) + '=');
    if (posName != -1)
    {
      var posValue = posName + (escape(cookieName) + '=').length;
      var endPos = document.cookie.indexOf(';', posValue);
      if (endPos != -1)
        cookieValue = unescape(document.cookie.substring(posValue, endPos));
      else
        cookieValue = unescape(document.cookie.substring(posValue));
    }
  }
  return cookieValue;
}

// Function [getOffsetLeft] evaluates the left position of an element
// ***************************************************************************
function getOffsetLeft (el)
{
  var ol = el.offsetLeft;
  while ((el = el.offsetParent) != null)
    ol += el.offsetLeft;
  return ol;
}

// Function [getOffsetTop] evaluates the top position of an element
// ***************************************************************************
function getOffsetTop (el)
{
  var ot = el.offsetTop;
  while((el = el.offsetParent) != null)
   ot += el.offsetTop;
  return ot;
}

// Function [deleteCode] deletes code from an element
// ***************************************************************************
function deleteCode( sElement )
{
  var el = getElement( sElement );
  if ( el )
  {
    while (el.hasChildNodes())
      if ( el.lastChild )
        el.removeChild(el.lastChild);
      else
        break;
  }
}

// Function [insertCode] inserts code to an element after the beginning
// ***************************************************************************
function insertCode( sElement, sHtml )
{
  var el;

  if ( ie || opera )
  {
    el = getElement( sElement );
    if ( el )
      el.insertAdjacentHTML("afterBegin", sHtml);
  }
  else if ( document.getElementById )
  {
    var range = document.createRange();
    el = document.getElementById( sElement );
    if ( el )
    {
      range.setStartAfter( el );
      var docFrag = range.createContextualFragment( sHtml );
      el.appendChild( docFrag );
    }
  }
}

// Function [getElement] gets an element specified bei its id/name
// ***************************************************************************
function getElement( sId )
{
  var el;

  if ( ie )
    el = document.all[sId];
  else if ( document.getElementById )
    el = document.getElementById( sId );
  return el;
}

// Function [setVisible] sets an element visible or invisible
// ***************************************************************************
function setVisible( what, bValue )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( el )
    el.style.visibility = ( bValue ? 'visible' : 'hidden' );
}

// Function [isVisible] checks if an element is visible or not
// ***************************************************************************
function isVisible( el )
{
  if ( !el )
    return false;
  if ( el.style.visibility == 'visible' )
    return true;
  return false;
}

// Function [setPadding] sets padding of an element
// ***************************************************************************
function setPadding( what, l, t, r, b )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( el )
  {
    el.style.paddingLeft = l;
    el.style.paddingRight = r;
    el.style.paddingTop = t;
    el.style.paddingBottom = b;
  }
}

// Function [setMargin] sets margin of an element
// ***************************************************************************
function setMargin( what, l, t, r, b )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( el )
  {
    el.style.marginLeft = l;
    el.style.marginRight = r;
    el.style.marginTop = t;
    el.style.marginBottom = b;
  }
}

// Function [setClip] sets clipping of an element
// ***************************************************************************
function setClip( what, l, t, r, b )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( el )
    el.style.clip = 'rect(' + t + 'px ' + r + 'px ' + b + 'px ' + l + 'px )';
}

// Function [setBorder] sets the border of an element
// ***************************************************************************
function setBorder( what, l, t, r, b )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( el )
  {
    el.style.borderLeft = l;
    el.style.borderRight = r;
    el.style.borderTop = t;
    el.style.borderBottom = b;
  }
}

// Function [getClientWidth] gets the width of the client window
// ***************************************************************************
function getClientWidth()
{
  if ( ie )
    return document.body.clientWidth;
  return window.innerWidth;
}

// Function [getClientHeight] gets the height of the client window
// ***************************************************************************
function getClientHeight()
{
  if ( ie )
    return document.body.clientHeight;
  return window.innerHeight;
}

// Function [setBackground] sets the background color of an element
// ***************************************************************************
function setBackground( what, sValue )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( !el )
    return;
  if ( el )
    el.style.background = sValue;
}

// Function [setColor] sets the foreground color of an element
// ***************************************************************************
function setColor( what, sValue )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( !el )
    return;
  if ( el )
    el.style.color = sValue;
}

// Function [setWidth] sets the width of an element
// ***************************************************************************
function setWidth( what, iValue )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( el )
    el.style.width = iValue;
}

// Function [getWidth] gets the width of an element
// ***************************************************************************
function getWidth( what )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( !el )
    return 0;
  return (typeof( el.offsetWidth ) == 'undefined' ? 0 : el.offsetWidth );
}

// Function [setHeight] sets the height of an element
// ***************************************************************************
function setHeight( what, iValue )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( el )
    el.style.height = iValue;
}

// Function [getHeight] gets the height of an element
// ***************************************************************************
function getHeight( what )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( !el )
    return 0;
  return (typeof( el.offsetHeight ) == 'undefined' ? 0 : el.offsetHeight );
}

// Function [setLeft] sets left position of an element
// ***************************************************************************
function setLeft( what, iValue )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( el )
    el.style.left = iValue;
}

// Function [setTop] sets top position of an element
// ***************************************************************************
function setTop( what, iValue )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( el )
    el.style.top = iValue;
}

// Function [setTitle] sets title of an element
// ***************************************************************************
function setTitle( what, sValue )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( el )
    el.title = sValue;
}

// Function [setDecoration] sets decoration of an element
// ***************************************************************************
function setDecoration( what, sValue )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( el )
    el.style.textDecoration = sValue;
}

// Function [setClass] sets class name property
// ***************************************************************************
function setClass( what, sValue )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( el )
    el.className = sValue;
}

// Function [setFocus] sets the focus for an element
// ***************************************************************************
function setFocus( what )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = cbUtils_getElement( what );
  if ( !el )
    return;
  if ( el.focus )
    el.focus();
}

// Function [setOpacity] sets opacity of an element
// ***************************************************************************
function setOpacity( what, sValue )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( el )
    if ( ie )
    {
      if ( sValue == '' )
        el.style.filter = '';
      else
        el.style.filter = 'alpha(opacity=' + sValue + ')';
    }
    else if ( ns || gecko )
    {
      if ( sValue == '' )
        el.style.MozOpacity = '';
      else
        el.style.MozOpacity = parseInt( sValue ) / 100;
    }
}

// Function [setCursor] sets cursor of an element
// ***************************************************************************
function setCursor( what, sValue )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( !el )
    return;
  if ( ie5 && sValue == 'pointer' )
    sValue = 'hand';
  el.style.cursor = sValue;
}

// Function [setImage] sets the source of an image element
// ***************************************************************************
function setImage( what, sValue )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( !el )
    return;
  el.src = sValue;
}

// Function [hasContent] checks, if an element has an innerHTML-content
// ***************************************************************************
function hasContent( what )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( !el )
    return false;
  if ( el.innerHTML == '' )
    return false;
  return true;
}

// Function [displayElement] shows/hides an element. This sets the element
// visible and moves it out of screen.
// ***************************************************************************
function displayElement( what, bShow )
{
  var el;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( el )
  {
    setVisible( el, bShow );
    if ( !bShow )
    {
      setLeft( el, -2000 );
      setTop( el, -2000 );
    }
  }
}

// Decodes &; chars
// ***************************************************************************
function decodeHTML( sText )
{
  sText = sText.replace( /&Agrave;/g, 'À');
  sText = sText.replace( /&Aacute;/g, 'Á');
  sText = sText.replace( /&Acirc;/g, 'Â');
  sText = sText.replace( /&Atilde;/g, 'Ã');
  sText = sText.replace( /&Auml;/g, 'Ä');
  sText = sText.replace( /&Aring;/g, 'Å');
  sText = sText.replace( /&AElig;/g, 'Æ');
  sText = sText.replace( /&Ccedil;/g, 'Ç');
  sText = sText.replace( /&Egrave;/g, 'È');
  sText = sText.replace( /&Eacute;/g, 'É');
  sText = sText.replace( /&Ecirc;/g, 'Ê');
  sText = sText.replace( /&Euml;/g, 'Ë');
  sText = sText.replace( /&Igrave;/g, 'Ì');
  sText = sText.replace( /&Iacute;/g, 'Í');
  sText = sText.replace( /&Icirc;/g, 'Î');
  sText = sText.replace( /&Iuml;/g, 'Ï');
  sText = sText.replace( /&ETH;/g, 'Ð');
  sText = sText.replace( /&Ntilde;/g, 'Ñ');
  sText = sText.replace( /&Ograve;/g, 'Ò');
  sText = sText.replace( /&Oacute;/g, 'Ó');
  sText = sText.replace( /&Ocirc;/g, 'Ô');
  sText = sText.replace( /&Otilde;/g, 'Õ');
  sText = sText.replace( /&Ouml;/g, 'Ö');
  sText = sText.replace( /&Oslash;/g, 'Ø');
  sText = sText.replace( /&Ugrave;/g, 'Ù');
  sText = sText.replace( /&Uacute;/g, 'Ú');
  sText = sText.replace( /&Ucirc;/g, 'Û');
  sText = sText.replace( /&Uuml;/g, 'Ü');
  sText = sText.replace( /&Yacute;/g, 'Ý');
  sText = sText.replace( /&THORN;/g, 'Þ');
  sText = sText.replace( /&szlig;/g, 'ß');
  sText = sText.replace( /&agrave;/g, 'à');
  sText = sText.replace( /&aacute;/g, 'á');
  sText = sText.replace( /&acirc;/g, 'â');
  sText = sText.replace( /&atilde;/g, 'ã');
  sText = sText.replace( /&auml;/g, 'ä');
  sText = sText.replace( /&aring;/g, 'å');
  sText = sText.replace( /&aelig;/g, 'æ');
  sText = sText.replace( /&ccedil;/g, 'ç');
  sText = sText.replace( /&egrave;/g, 'è');
  sText = sText.replace( /&eacute;/g, 'é');
  sText = sText.replace( /&ecirc;/g, 'ê');
  sText = sText.replace( /&euml;/g, 'ë');
  sText = sText.replace( /&igrave;/g, 'ì');
  sText = sText.replace( /&iacute;/g, 'í');
  sText = sText.replace( /&icirc;/g, 'î');
  sText = sText.replace( /&iuml;/g, 'ï');
  sText = sText.replace( /&eth;/g, 'ð');
  sText = sText.replace( /&ntilde;/g, 'ñ');
  sText = sText.replace( /&ograve;/g, 'ò');
  sText = sText.replace( /&oacute;/g, 'ó');
  sText = sText.replace( /&ocirc;/g, 'ô');
  sText = sText.replace( /&otilde;/g, 'õ');
  sText = sText.replace( /&ouml;/g, 'ö');
  sText = sText.replace( /&oslash;/g, 'ø');
  sText = sText.replace( /&ugrave;/g, 'ù');
  sText = sText.replace( /&uacute;/g, 'ú');
  sText = sText.replace( /&ucirc;/g, 'û');
  sText = sText.replace( /&uuml;/g, 'ü');
  sText = sText.replace( /&yacute;/g, 'ý');
  sText = sText.replace( /&thorn;/g, 'þ');
  sText = sText.replace( /&yuml;/g, 'ÿ');
  return sText;
}

// Function [getValue] gets a value from an element
// ***************************************************************************
function getValue( what )
{
  var el;
  var sSeperator = '¶';
  var sValue = '';
  var i;
  var sTemp;
  var sType;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( !el )
    return sValue;
  if ( arguments.length > 1 )
    sSeperator = arguments[1];
  if ( el.name == undefined )
    sType = 'radio';
  else
    sType = el.type.toLowerCase();
  switch( sType  )
  {
    case 'checkbox':
      sValue = el.value;
      if ( !el.checked )
        sValue = 'off';
      else
        if ( sValue == '' )
          sValue = 'on';
      break;
    case 'radio':
      if ( el.length )
      {
        for (i = 0; i < el.length; i++)
          if ( el[i].checked )
          {
            sValue = el[i].value;
            break;
          }
      }
      else
        sValue = el.value;
      break;
    case 'select-one':
      i = el.selectedIndex;
      if ( i > -1 )
        sValue = el.options[i].value;
      break;
    case 'select-multiple':
      sValue = sSeperator;
      for (i = 0; i < el.options.length; i++)
        if ( el.options[i].selected )
        {
          sTemp = el.options[i].value;
          if ( sTemp == '' || sTemp == '*' )
          {
            sValue += sTemp + sSeperator;
            break;
          }
          sValue += sTemp + sSeperator;
        }
      break;
    default:
      sValue = el.value;
      break;
  }
  return sValue;
}

// Function [setValue] sets a value for an element
// ***************************************************************************
function setValue( what, sValue )
{
  var sSeperator = '¶';
  var sType;
  var i;
  var iCount;

  if ( typeof( what ) == 'object' )
    el = what;
  else
    el = getElement( what );
  if ( !el )
    return;
  if ( arguments.length > 2 )
    sSeperator = arguments[2];
  if ( el.length && !el.options )
    el = oField[0].type.toLowerCase();
  else
    sType = el.type.toLowerCase();
  switch( sType  )
  {
    case 'checkbox':
      el.checked = ( sValue == 'off' || sValue == '' ? false : true );
      break;
    case 'radio':
      if ( sValue == '' )
      {
        if ( el.length )
          el[0].checked = true;
        else
          el.checked = true;
      }
      else
      {
        if ( el.length )
        {
          for (i = 0; i < el.length; i++)
            if ( el[i].value == sValue )
            {
              el[i].checked = true;
              break;
            }
          if ( i == el.length )
            el[0].checked = true;
        }
        else
          if ( el.value == sValue )
            el.checked = true;
      }
      break;
    case 'select-one':
      if ( sValue.indexOf( sSeperator ) > -1 )
      {
        sValue = sValue.substr( 1 );
        i = sValue.indexOf( sSeperator );
        sValue = sValue.substr( 0, i );
      }
      for (i = 0; i < el.options.length; i++)
        if ( el.options[i].value == sValue || ( sValue == '' && el.options[i].value == '*' ) )
        {
          el.options[i].selected = true;
          return;
        }
        if ( el.options.length > 0 )
          el.options[0].selected = true;
        break;
    case 'select-multiple':
      if ( sValue.indexOf( sSeperator ) == -1 )
        sValue = sSeperator + sValue + sSeperator;
      iCount = '';
      for (var i = 0; i < el.options.length; i++)
        if ( sValue.indexOf( '¶' + el.options[i].value + '¶' ) > -1 )
        {
          el.options[i].selected = true;
          iCount++;
          if ( el.options[i].value == '' || el.options[i].value == '*' )
            break;
        }
        else
          el.options[i].selected = false;
      if ( el.options.length > 0 && iCount == 0 )
        el.options[0].selected = true;
      break;
    default:
      el.value = sValue;
      break;
  }
}



