var     ns  = (document.layers) ? true : false;                // ns4x                                           
var     dom = (document.getElementById) ? true : false;        // DOM compatible browser (ie6+, ns6+, mozilla...)
var     ie  = (document.all) ? true : false;                   // ie4x                                           

function  changeCss(e, cls)
{
  if (dom && e) {
    e.className = cls;
  }
}


function	formCount(l, c, m) {
  var e = document.getElementById(c);
  if (!e)
    return;
  e.innerHTML = m-l;
}

function        cssShow(id, v)
{
  var e = document.getElementById(id);
  if (e && e.style) {
    var s = v ? '' : 'none';
    e.style.display = s;
  }
}

function	cssShowHide(id)
{
  var e = document.getElementById(id);
  if (e && e.style) {
    e.style.display = e.style.display == '' ? 'none' : '';
  }
}

function	check_opt(o)
{
  for (var i=o.length-1; i>=0; i--)
    {
      if (o[i].checked)
	return true;
    }
  return false;
}


function        getCoordsOfElement(e)
{
  if (!e)
    return new coords(0, 0);

  var r = new coords(e.offsetLeft, e.offsetTop);
  var l = null
    for (e = e.offsetParent; e; e = e.offsetParent) {
      //alert(e.tagName+'='+e.id);
      r.add(e.offsetLeft, e.offsetTop);
      if (e.offsetTop != 0)
      l = e;
    }
  return r;
}
function        coords(ex, ey)
{
  if (!ex)
    this.x = 0;
  else
    this.x = ex;
  if (!ey)
    this.y = 0;
  else
    this.y = ey;

  this.addC = function(c) { this.x += c.x; this.y += c.y; };
  this.add = function(nx, ny) {this.x += nx; this.y += ny; }
  this.addX = function(nx) { this.x += nx; };
  this.addY = function(ny) { this.y += ny; };
}


/*
 *	javascript tooltip
 *	based on http://www.dhtmlgoodies.com/scripts/tooltip_shadow/tooltip_shadow.html
 */
var tooltip = false;
var tooltip_timeout = null;
var tooltipShadow = false;
var tooltipIframe = false;
var tooltip_is_msie = (navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('opera')==-1 && document.all)?true:false;
function	showTooltip(elt, tooltipTxt, hide_timeout)
{
  var tooltipShadowSize = 4;
  var tooltipMaxWidth = 250;
  var tooltipMinWidth = 100;

  if (tooltip_timeout)
    window.clearTimeout(tooltip_timeout);

  var bodyWidth = Math.max(document.body.clientWidth, document.documentElement.clientWidth) - 20;
  
  var coords = getCoordsOfElement(elt);

  if (!tooltip){
    tooltip = document.createElement('DIV');
    tooltip.id = 'tooltip';
    tooltipShadow = document.createElement('DIV');
    tooltipShadow.id = 'tooltipShadow';
    
    document.body.appendChild(tooltip);
    document.body.appendChild(tooltipShadow);
    
    if (tooltip_is_msie){
      tooltipIframe = document.createElement('IFRAME');
      tooltipIframe.frameborder='5';
      tooltipIframe.style.backgroundColor='#FFFFFF';
      tooltipIframe.src = '#'; 
      tooltipIframe.style.zIndex = 100;
      tooltipIframe.style.position = 'absolute';
      document.body.appendChild(tooltipIframe);
    }    
  }

  tooltip.onclick = function() { hideTooltip(); };
  
  tooltip.style.display = 'block';
  tooltipShadow.style.display = 'block';
  if (tooltip_is_msie)
    tooltipIframe.style.display = 'block';
  
  //   var st = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
  //   if (navigator.userAgent.toLowerCase().indexOf('safari') >= 0)
  //     st = 0;

  var leftPos = coords.x + 10;
  var topPos = coords.y + 25 ;

  tooltip.style.width = null; // Reset style width if it's set
  tooltip.innerHTML = tooltipTxt;
  tooltip.style.left = leftPos + 'px';
  tooltip.style.top = coords.y + 25 + 'px';

  tooltipShadow.style.left = leftPos + tooltipShadowSize + 'px';
  tooltipShadow.style.top = topPos + tooltipShadowSize + 'px';
  
  /* Exceeding max width of tooltip ? */
  if (tooltip.offsetWidth > tooltipMaxWidth)
    tooltip.style.width = tooltipMaxWidth + 'px';
  
  var tooltipWidth = tooltip.offsetWidth;
  if (tooltipWidth < tooltipMinWidth)
    tooltipWidth = tooltipMinWidth;
    
  tooltip.style.width = tooltipWidth + 'px';
  tooltipShadow.style.width = tooltip.offsetWidth + 'px';
  tooltipShadow.style.height = tooltip.offsetHeight + 'px';
  
  if ((leftPos + tooltipWidth) > bodyWidth) {
    tooltip.style.left = (tooltipShadow.style.left.replace('px', '') - ((leftPos + tooltipWidth)-bodyWidth)) + 'px';
    tooltipShadow.style.left = (tooltipShadow.style.left.replace('px', '') - ((leftPos + tooltipWidth) - bodyWidth) + tooltipShadowSize) + 'px';
  }
  
  if (tooltip_is_msie) {
    tooltipIframe.style.left = tooltip.style.left;
    tooltipIframe.style.top = tooltip.style.top;
    tooltipIframe.style.width = tooltip.offsetWidth + 'px';
    tooltipIframe.style.height = tooltip.offsetHeight + 'px';
  }

  if (hide_timeout) {
    if (tooltip_timeout)
      window.clearTimeout(tooltip_timeout);
    tooltip_timeout = tooltip_timeout = window.setTimeout('hideTooltip()', hide_timeout);
  }
}

function hideTooltip()
{
  if (tooltip_timeout)
    window.clearTimeout(tooltip_timeout);
  tooltip.style.display = 'none';
  tooltipShadow.style.display = 'none';
  if (tooltip_is_msie)
    tooltipIframe.style.display = 'none';
}

function	showHideFolder(id)
{
  var e = document.getElementById(id);
  if (!e)
    return;
  if (!e.style)
    return;
  var v = (e.style.display == '');
  e.style.display = v ? 'none' : '';
  var i = document.getElementById(id+'-img');
  if (!i)
    return;
  // folder-closed.gif
  i.src = './images/' + (v ? 'folder-closed.gif' : 'folder-opened.gif');
}
