//*******************************************************************
//*  QLMObject constructor
//*  Input: id (required): id of positioned div.
//*		left,top,width,height optional arguments.
//*******************************************************************
function QLMObject(id,x,y,w,h) {
  this.el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? getLayerReference(id,document): null;
  if (!this.el)
    return null;
  this.doc = (document.layers)? this.el.document: this.el;
  this.css = (this.el.style)? this.el.style: this.el;
  var px = (document.layers||window.opera)? "": "px";
  this.x = x || 0;	
  if (x) 
    this.css.left = this.x+px;
  this.y = y || 0;
  if (y) 
    this.css.top = this.y+px;
  this.width = w? w: (this.el.offsetWidth)? this.el.offsetWidth: (this.css.clip.width)? this.css.clip.width: 0;
  this.height = h? h: (this.el.offsetHeight)? this.el.offsetHeight: (this.css.clip.height)? this.css.clip.height: 0;
  if (w){
    (document.layers)? this.css.clip.width=w+px: this.css.width=w+px;
  }
  if (h){
    (document.layers)? this.css.clip.height=h+px: this.css.height=h+px;
  }
  this.obj = id + "QLMObject"; 	eval(this.obj + "=this");
}

function showObject()
{ 
  this.css.visibility = "visible"; 
}

function hideObject(){
  this.css.visibility = "hidden";
}

function objectshiftTo(x,y) {
  if (x!=null) 
    this.x=x; 
  if (y!=null) 
    this.y=y;	
	// rounded below (this.x/y can hold decimals)
  if (this.css.moveTo) { 
    this.css.moveTo(Math.round(this.x),Math.round(this.y)); 
  } 
  else { 
    this.css.left=Math.round(this.x)+"px"; 
    this.css.top=Math.round(this.y)+"px"; 
  }
}

function objectshiftBy(x,y) 
{
  this.shiftTo(this.x+x,this.y+y);
}

function objectwriteLayer(cntnt) 
{
  if (typeof this.doc.innerHTML!="undefined") {
    this.doc.innerHTML = cntnt;
  } 
  else if (document.layers) {
    this.doc.write(cntnt);
    this.doc.close();
  }
}

function objectsetBackgroundColor(bg) {
  if (document.layers)
    this.doc.bgColor=bg;
  else
    this.css.backgroundColor=bg;
}

// assign methods 
QLMObject.prototype.show             = showObject;
QLMObject.prototype.hide             = hideObject;
QLMObject.prototype.shiftTo          = objectshiftTo;
QLMObject.prototype.shiftBy          = objectshiftBy;
QLMObject.prototype.objectwriteLayer = objectwriteLayer;
QLMObject.prototype.setBgClr = objectsetBackgroundColor;


function getLayerReference(layer,doc) {
  if (document.layers) {
    var theLayer;
    for (var i=0; i<doc.layers.length; i++) {
      theLayer = doc.layers[i];
      if (theLayer.name == layer)
        return theLayer;
      else if (theLayer.document.layers.length > 0) 
        if ((theLayer = getLayerReference(layer,theLayer.document)) != null)
          return theLayer;
    }
    return null;
  }
}

//*****************************************************************
//* Utility functions
//*****************************************************************
QLMObject.prototype.centerIn = function(outer) {
var outerWidth, outerHeight, innerWidth, innerHeight, x, y;
  if (eval(outer)==window) {
    outerWidth=getWinWidth();
    outerHeight=getWinHeight();
  }
  else {
    outerWidth=outer.width;
    outerHeight=outer.height;
  }
  innerWidth=this.width;
  innerHeight=this.height;
  x=Math.round((outerWidth-innerWidth)/2);
  y=Math.round((outerHeight-innerHeight)/2);
  this.shiftTo(x,y);
}

function getWidth(object,id) {
var currwidth=0;
  if (document.getElementById||document.all) {
    var element;
    if (id)
      element = (document.getElementById)? document.getElementById(id): document.all[id];
    else
      element = object;
    if (element.offsetWidth)
      currwidth = element.offsetWidth;
  } 
  else if (object.document) 
    currwidth = object.document.width;	//netscape4
  return currwidth;
}

function getHeight(object,id) {
var currheight=0;
  if (document.getElementById||document.all) {
    var element;
    if (id)
      element = (document.getElementById)? document.getElementById(id): document.all[id];
    else
      element = object;
    if (element.offsetHeight)
      currheight = element.offsetHeight;
  } 
  else if (object.document) 
    currheight = object.document.height;	//netscape4
  return currheight;
}

function getWinWidth() {
var windowwidth = 0;
  if (document.documentElement && document.documentElement.clientWidth) 
    windowwidth = document.documentElement.clientWidth;
  else if (document.body && document.body.clientWidth) 
    windowwidth = document.body.clientWidth;
  else if (document.body && document.body.offsetWidth) 
    windowwidth = document.body.offsetWidth; // netscape6
  else if (window.innerWidth)
    windowwidth = window.innerWidth-18;
  return windowwidth;
}

function getWinHeight() {
var windowheight = 0;
  if (window.innerHeight)
    windowheight = window.innerHeight-18;
  else if (document.documentElement && document.documentElement.clientHeight) 
    windowheight = document.documentElement.clientHeight;
  else if (document.body && document.body.clientHeight) 
    windowheight = document.body.clientHeight;
  return windowheight;
}	

function getScrollY() {
var scroll = 0;
  if (document.documentElement && document.documentElement.scrollTop)
    scroll = document.documentElement.scrollTop;
  else if (document.body && document.body.scrollTop) 
    scroll = document.body.scrollTop; 
  else if (window.pageYOffset)
    scroll = window.pageYOffset;
  else if (window.scrollY)
    scroll = window.scrollY;
  return scroll;
}

function getScrollX() {
var scroll = 0;
  if (document.documentElement && document.documentElement.scrollLeft)  
    scroll = document.documentElement.scrollLeft;
  else if (document.body && document.body.scrollLeft) 
    scroll = document.body.scrollLeft; 
  else if (window.pageXOffset)
    scroll = window.pageXOffset;
  else if (window.scrollX)
    scroll = window.scrollX;
  return scroll;
}
//*****************************************************************
//* Tooltip section
//*****************************************************************
var WindowsWidth, WindowsHeight, verticalscroll, horizontalscroll;
var TooltipTimer1, TooltipTimer2;
var mouseX, mouseY;

window.onload = QLMInitToolTip;
window.onresize = QLMOnResize;	

var OriginalWidth, OriginalHeight;
if (document.layers) {
  OriginalWidth = window.innerWidth;
  OriginalHeight = window.innerHeight;
}

function QLMOnResize() {
  if (document.layers) {
    if (window.innerWidth != OriginalWidth || window.innerHeight != OriginalHeight)
      window.location.reload();
  }
  else{
    WindowsWidth = getWinWidth();
    WindowsHeight = getWinHeight();
  }
}

function QLMInitToolTip() {
  QLMTooltip = new QLMObject('QLMTooltipDivision');
  if (!QLMTooltip) 
    return;
  if (!document.layers && typeof QLMTooltip.doc.innerHTML == "undefined"){
    QLMTooltip = null;	// for opera
    return; 
  }
  if (QLMTooltip.el.style) {	// ns4 would lose all this on rewrites
    QLMTooltip.css.width = QLMTooltipWidth+"px";
    QLMTooltip.css.fontFamily = QLMTooltipFontFamily;
    QLMTooltip.css.fontSize = QLMTooltipFontSize;
    QLMTooltip.css.lineHeight = QLMTooltipLineHeight;
    QLMTooltip.css.color = QLMTooltipFontColor;
    QLMTooltip.css.backgroundColor = QLMTooltipBgColor;
    QLMTooltip.css.borderColor = QLMTooltipBorderColor;
    QLMTooltip.css.borderWidth = QLMTooltipBorderWidth+"px";
    QLMTooltip.css.padding = QLMTooltipPadding+"px";
    QLMTooltip.css.borderStyle = QLMTooltipBorderStyle;
    if (QLMTooltipBgImg)
      QLMTooltip.css.backgroundImage = "url("+QLMTooltipBgImg+")";
  }
 // used in calculating tip position
  WindowsWidth = getWinWidth();
  WindowsHeight = getWinHeight();
}

function StartTooltip(evt,cntnt,replace) {
  if(TooltipTimer1)
    clearTimeout(TooltipTimer1);	
  if (TooltipTimer2)
    clearTimeout(TooltipTimer2); 
  // set up mousemove 
  if (QLMTooltipFollowMouse) {
    if (document.addEventListener) {
      document.addEventListener("mousemove",QLMFindMousePosition,true);
  }
  else if (document.attachEvent) {
    document.attachEvent("onmousemove",QLMFindMousePosition);
  }
  else{
    if (document.layers && document.captureEvents) 
				document.captureEvents(Event.MOUSEMOVE);
			document.onmousemove = QLMFindMousePosition;
		}
	}
        QLMTooltip.css.padding = QLMTooltipPadding+"px";
        QLMTooltip.css.borderStyle = QLMTooltipBorderStyle;
        QLMTooltip.css.width=QLMTooltipWidth;
	QLMTooltip.objectwriteLayer(cntnt);	// write QLMTooltip content to QLMTooltipDivision
	// to get document area in view, check scroll amounts
	verticalscroll = getScrollY();
	horizontalscroll = getScrollX();
	// get coordinates of mouseover event

	if(!replace){
 	  evt = (window.event)? window.event: evt;
	  if (evt) {
		mouseX = QLMMouseX(evt);
		mouseY = QLMMouseY(evt);
	  }
	  // get QLMTooltip width and height, for calculating position
	  // and yes style.width was set, but browsers differ ...
	  QLMTooltip.width = getWidth(QLMTooltip.el);	
	  QLMTooltip.height = getHeight(QLMTooltip.el);
	  // again here, delay needed by some browsers
	  if (!document.layers)
		setTimeout("QLMTooltip.height=getHeight('','QLMTooltipDivision')",100);
	  // position the QLMTooltip, delay for getHeight
	  setTimeout("QLMPositionTip()",120);	
	  TooltipTimer1=setTimeout(QLMTooltip.obj+".show()",200);	// show QLMTooltip
        }
}

function QLMPositionTip() {
var x, y;
  QLMTooltip.height = getHeight(QLMTooltip.el); // important here if images not preloaded
  if ((mouseX + QLMTooltipOffX + QLMTooltip.width) > WindowsWidth + horizontalscroll) 
    x = mouseX - (QLMTooltip.width + QLMTooltipOffX);
  else
    x = mouseX + QLMTooltipOffX;
  if ((mouseY + QLMTooltipOffY + QLMTooltip.height) > WindowsHeight + verticalscroll) 
		y = (mouseY - (QLMTooltip.height + QLMTooltipOffY) > verticalscroll)?  mouseY - (QLMTooltip.height + QLMTooltipOffY): WindowsHeight + verticalscroll - (QLMTooltip.height + QLMTooltipOffY);
  else
    y = mouseY + QLMTooltipOffY;
  if ((QLMTooltip.width) > WindowsWidth + horizontalscroll) 
    x=0;
  if ((QLMTooltip.height) > WindowsHeight + verticalscroll) 
    y=0;
  if(x<0)
    x=0;
  if(y<0)
    y=0;
  QLMTooltip.shiftTo(x,y);
}

function QLMHideToolTip() {
  if(!QLMToolTipCanOpen)
    return;
  if (!QLMTooltip) 
    return;
  TooltipTimer2=setTimeout(QLMTooltip.obj+".hide()",175);

  if (document&&QLMTooltipFollowMouse) {
    if (document.removeEventListener) {
      document.removeEventListener("mousemove",QLMFindMousePosition,true);
    }
    else if (document.detachEvent) {
      document.detachEvent("onmousemove",QLMFindMousePosition);
    } 
    else {
      if (document.layers && document.releaseEvents)
	document.releaseEvents(Event.MOUSEMOVE);
      document.onmousemove = null;
    }
  }
}

function QLMFindMousePosition(event) {
  evt = (window.event)? window.event: event;
  if (event) {
    mouseX = QLMMouseX(event);
    mouseY = QLMMouseY(event);
  }
  QLMPositionTip();	
}

function QLMMouseX(event) {
  if(qlmoffsetx)
    return (event.pageX)? event.pageX+qlmoffsetx: event.clientX + qlmoffsetx;
  else
    return (event.pageX)? event.pageX+qlmoffsetx: event.clientX + getScrollX()+qlmoffsetx;
}

function QLMMouseY(event) {
  if(qlmoffsety)
    return (event.pageY)? event.pageY+qlmoffsety: event.clientY + qlmoffsety;
  else
    return (event.pageY)? event.pageY+qlmoffsety: event.clientY + getScrollY()+qlmoffsety;
}


function findpivotrows(tableid,table,numberofcells,rowindex)
{
  var object=null;
  var i=0;
  for(i=0;i<numberofcells;i++){
    object=document.getElementById('pivot'+tableid+i);
    if(object){
      if(object.options.length){
        var value=object.options[object.selectedIndex].value;
        if(value!="All"){
          if(Option(table.rows[rowindex].cells[i].innerText,table.rows[rowindex].cells[i].innerText).value!=value){
            return false;
          }
        }
      }
    }
  }
  return true;
}

function buildpivottable(tableid,testvalue)
{
  var object;

  object=document.getElementById(tableid);
  numberofcells=object.rows[0].cells.length
  var length=object.rows.length;
  for(i=0;i<length;i++){
    window.status="now checking "+i;
    if(findpivotrows(tableid,object,numberofcells,i)||i==0){
      var length1=object.rows[i].cells.length;
      object.rows[i].style.display="";
    }
    else{
      var length1=object.rows[i].cells.length;
      object.rows[i].style.display="none";
    }
  }
  return;
}

function pivotopenselection(selection,tableid,cellnumber)
{
  object=document.getElementById(tableid);
  if(selection.options.length==0){
    option=new Option("All","All");
    selection.options.add(option);
 
    var list=new String();
    for(i=1;i<object.rows.length;i++){
      if(list.indexOf(object.rows[i].cells[cellnumber].innerText+"\n")==-1){
        list=list+object.rows[i].cells[cellnumber].innerText+"\n";
        option=new Option(object.rows[i].cells[cellnumber].innerText,object.rows[i].cells[cellnumber].innerText);
        selection.options.add(option);
      }
    }
  }
}

function pivotopenselectionfromid(selection,tableid,cellnumber)
{
  object=document.getElementById(selection);
  pivotopenselection(object,tableid,cellnumber);
}

function togglepivot(tableid)
{
var state="";
var table;
var numberofcells;
var setstate=1;

  table=document.getElementById(tableid);
  numberofcells=table.rows[0].cells.length
  for(i=0;i<numberofcells;i++){
    object=document.getElementById('pivot'+tableid+i);
    if(object){
      if(state==""&&setstate){
        setstate=0;
        state=object.style.display;
        if(state=='')
          state="none";
        else if(state=='none'){
          state="";
        }
      }
      object.style.display=state;
      setTimeout("pivotopenselectionfromid('"+object.id+"','"+tableid+"',"+i+");",100);
    }
  }
}
