/*********************************
 * Various functions fixing the inconsistencies of DOM implementation by
 * different browsers.
 *********************************/

var browser = '';
var version = 0.0;
var str = ''
// Perform browser identification on load
switch(navigator.appName)
  { case 'Microsoft Internet Explorer':
        browser='ie';
        str = navigator.appVersion;
        str = str.substring(str.indexOf('MSIE')+4,str.length);
        version = parseFloat(str);
	    break;
    case 'Netscape':
	    if(navigator.vendorSub!='')
          { browser='ns';
            version=parseFloat(navigator.vendorSub);
          }
        else
          { browser='mz'
	        version=parseFloat(navigator.appVersion);
          }
        break;
	default:
	    browser='unknown';
	    break;
  }

/* Function:  setRowBG
 * Arguments: row  - Row object	
 *            bgc  - background color
 *     Works around the Internet Explorer flaw of not updating cell
 *     backgroungs when row background is changed
 */
function setRowBG(row,bgc)
{   if(browser == 'ns')
      { row.style.backgroundColor=bgc;
      }
    else
      { for(var i=0; i<row.childNodes.length; i++)
	    row.childNodes[i].style.backgroundColor=bgc;
      }
    return;
}


function IEWidthFix()
{   if(browser == 'ie')
      { setTimeout('IEResize();',20);
      }
    return;
}

function IEResize()
{  window.resizeBy(-1,-1);
   setTimeout('window.resizeBy(-1,-1);',2);
}

/*******************************************************************************
 * Usage:
 * - add call to initToolTips() to the BODY onLoad event.
 * - use the following attributes with html tags to display tooltips:
 *      tooltip='text of the tool tip'
 *      ttclass='custom_style' (optional - if not present default is used)
 *      ttdelay='delay_time_ms' - time in ms to delay the appearance of the 
 *              tooltip, if not present default is used.
 *      ttontime='on_time_ms' - duration in ms the tool tip is being displayed,
 *              if not present the tooltip is shown until the mouse moves out.
 ******************************************************************************/

// Define default delay and style for tooltips.
var ttDefDelay = 500;
var ttDefStyle = 'tooltip';

// variable holding current tooltip node.
var tooltip = null;


/* Function: 	displayToolTip
   Arguments: 	none
	Renders the current tooltip visible.
*/
function displayToolTip()
{ if(tooltip)
     { tooltip.style.display='block';
       var onTime=tooltip.getAttribute('ontime');
       if(onTime) setTimeout('deleteToolTip()',onTime);       
     }
}

/* Function: 	constructToolTip
   Arguments: 	event object
	Constructs tooltip node.
*/
function constructToolTip(e)
{ var target=e.target;
    if(target.nodeName=='#text') target=target.parentNode;
    if(target.getAttribute('tooltip'))
      { if(tooltip) deleteToolTip(e);
        tooltip = document.createElement('DIV');
        document.body.appendChild(tooltip);
        var ttClass=target.getAttribute('ttclass');
        if(! ttClass) ttClass=ttDefStyle;
        tooltip.setAttribute('class',ttClass);
        var ttOnTime=target.getAttribute('ttontime');
        if(ttOnTime) tooltip.setAttribute('ontime',ttOnTime);        
        tooltip.appendChild(
                       document.createTextNode(target.getAttribute('tooltip')));
        moveToolTip(e);
        var ttDelay=target.getAttribute('ttdelay');
        if(! ttDelay) 
            ttDelay=ttDefDelay;
        setTimeout('displayToolTip()',ttDelay);
      }    
}

/* Function: 	constructIEToolTip
   Arguments: 	event object
	Constructs Internet Explorer compatible tooltip node.
*/
function constructIEToolTip(e)
{ var target=e.srcElement;
    if(! target.getAttribute('tooltip')) return;
    if(tooltip) deleteToolTip(e);
    tooltip = document.createElement('DIV');
    document.body.appendChild(tooltip);
    var ttClass=target.getAttribute('ttclass');
    if(! ttClass) ttClass=ttDefStyle;
    tooltip.className=ttClass;
    var ttOnTime=target.getAttribute('ttontime');
    if(ttOnTime) tooltip.setAttribute('ontime',ttOnTime);        
    tooltip.appendChild(
                       document.createTextNode(target.getAttribute('tooltip')));
    moveIEToolTip(e);
    var ttDelay=target.getAttribute('ttdelay');
    if(! ttDelay) 
        ttDelay=ttDefDelay;
    setTimeout('displayToolTip()',ttDelay); 
    return;
}

/* Function: 	deleteToolTip
   Arguments: 	none
	Deletes tooltip node.
*/
function deleteToolTip()
{   if(tooltip)
      { document.body.removeChild(tooltip);
        tooltip=null;
      }
}

/* Function: 	moveToolTip
   Arguments: 	event object
	Moves tooltip to follow the mouse.
*/
function moveToolTip(e)
{   if(tooltip)
      { var dw=document.width;
        var scrollLeft=e.pageX - e.clientX;
        if(e.clientX < 0.5 * dw)
	  { tooltip.style.left=(e.pageX + 15) + 'px';
            tooltip.style.right='';
            tooltip.style.marginLeft='';
            tooltip.style.marginRight=(20 - scrollLeft) + 'px';
          }
        else
          { tooltip.style.right=(dw-(e.pageX - 45)) + 'px';
            tooltip.style.left='';
            tooltip.style.marginLeft=(scrollLeft + 20) + 'px';
            tooltip.style.marginRight='';
          }
        tooltip.style.top=(e.pageY + 5) + 'px';
      }
    return;
}

/* Function: 	moveIEToolTip
   Arguments: 	event object
	Moves tooltip to follow the mouse in Internet Explorer 
        compatible manner.
*/
function moveIEToolTip(e)
{   if(tooltip)
      { var dw=document.body.clientWidth;
        var mpX=e.clientX+document.body.scrollLeft;
        var mpY=e.clientY+document.body.scrollTop;
        if(e.clientX < 0.5 * dw)
	  { tooltip.style.left=(mpX + 15) + 'px';
            tooltip.style.right=(20 - document.body.scrollLeft) + 'px';
          }
        else
          { tooltip.style.right=(dw-(e.clientX-15)) + 'px';
            tooltip.style.left='';
          }
        tooltip.style.top=(mpY + 5) + 'px';
      }
}


/* Function: 	initIEToolTips
   Arguments: 	document node
	For Internet Explorer iterates attachs events to the node if tooltip
        attribute is defined and then iterates node children calling itself. 
*/
function initIEToolTips(node)
{   if(node.nodeType==1 && node.getAttribute('tooltip'))
      {   node.attachEvent('onmouseover',constructIEToolTip);
          node.attachEvent('onmouseout',deleteToolTip);
          node.attachEvent('onmousemove',moveIEToolTip);
      }
    for(var i=0; i<node.childNodes.length; i++)
        initIEToolTips(node.childNodes[i]);
    
}

/* Function: 	initToolTips
   Arguments: 	none
	If browser is Internet Explorer calls IE specific initialization
        function, otherwise adds event listeners to the document. 
*/
function initToolTips()
{   if(browser=='ie')
      { initIEToolTips(document.body);
      }
    else
      { document.addEventListener('mouseover',constructToolTip,true);
        document.addEventListener('mouseout',deleteToolTip,true);
        document.addEventListener('mousemove',moveToolTip,true);
      }
}