//javascript tooltip

function posTop() {return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;}
function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement.scrollLeft? document.documentElement.scrollLeft: document.body.scrollLeft?document.body.scrollLeft:0;}

function getTop(elem) {
  var top=0;
  while(elem!=null) {
    top+=elem.offsetTop;
    elem=elem.offsetParent;
  }
  return top;
}
function getLeft(elem) {
  var left=0;
  while(elem!=null) {
    left+=elem.offsetLeft;
    elem=elem.offsetParent;
  }
  return left;
}


function TooltipTracker(name)
{
  this.name=name;
  this.delay=200;
  this.tooltips=new Array();
  
  this.border="solid #000000 1px";
  this.background="#ffffff";
  this.width="300px";
  this.opacity=1;
}

//function TooltipTracker(name, returnElemt) { /* Add "return to" element */
//  this.name = name;
//  this.delay = 200;
//  this.tooltips = new Array();

//  this.border = "solid #000000 1px";
//  this.background = "#ffffff";
//  this.width = "300px";
//  this.opacity = 1;
//  
//  this.targetFocusElement = elem;
//}


function _TOOLTIPTRACKERCLOSEALL_() {
  for(var x in this.tooltips) {
    this.tooltips[x].hide();
  }
}

function _TOOLTIPTRACKERMAKE_(name, html)
{
  this.tooltips[name]=new JSTooltip(name, html, this);
}
function _TOOLTIPTRACKERGET_(name)
{
  return this.tooltips[name];
}

function _TOOLTIPTRACKERWIDTH_(w) 
{
  this.width=w;
}
function _TOOLTIPTRACKERBORDER_(b) 
{
  this.border=b;
}
function _TOOLTIPTRACKERBG_(bg) 
{
  this.background=bg;
}
function _TOOLTIPTRACKEROPAC_(alpha) 
{
  this.opacity=alpha;
}

/* Add "return to" element */
//function _TOOLTIPTRACKERGETTARGETFOCUSELEMENT_(elem) {
//  this.targetFocusElement = elem;
//}



TooltipTracker.prototype.make=_TOOLTIPTRACKERMAKE_;
TooltipTracker.prototype.getByName=_TOOLTIPTRACKERGET_;
TooltipTracker.prototype.setWidth=_TOOLTIPTRACKERWIDTH_;
TooltipTracker.prototype.setBorder=_TOOLTIPTRACKERBORDER_;
TooltipTracker.prototype.setBackground=_TOOLTIPTRACKERBG_;
TooltipTracker.prototype.setOpacity=_TOOLTIPTRACKEROPAC_;
TooltipTracker.prototype.closeAll = _TOOLTIPTRACKERCLOSEALL_;

/* Add "return to" element */
//TooltipTracker.prototype.getTargetFocusElement = _TOOLTIPTRACKERGETTARGETFOCUSELEMENT_;

function JSTooltip(name, theHtml, tracker)
{
  this.name=name;
  this.tracker=tracker;
  this.closed=false;
 
  this.srcElem=null;
  
  this.ttDiv=document.createElement("DIV");
  this.ttDiv.className="tooltip";
  this.ttDiv.style.fontSize = "9pt";
  this.ttDiv.style.color = "#000099";
  this.ttDiv.style.position = "absolute";
  this.ttDiv.style.width=this.tracker.width;//"200px";
  this.ttDiv.style.background=this.tracker.background;//"#ffffdd";
  //this.ttDiv.style.padding="5px";
  //this.ttDiv.style.display="none";
  this.ttDiv.style.border = this.tracker.border; //"solid #990011 1px"; //"double #000000 3px";
  this.ttDiv.style.opacity=this.tracker.opacity;
  this.ttDiv.style.filter="alpha(opacity="+(this.tracker.opacity*100)+")";
  this.ttDiv.style.zIndex="10000";
  
  var le=document.createElement("div");
  le.className="leftEdge";
  this.ttDiv.appendChild(le);
  var re=document.createElement("div");
  re.className="rightEdge";
  le.appendChild(re);
  var te=document.createElement("div");
  te.className="topEdge";
  re.appendChild(te);
  var be=document.createElement("div");
  be.className="bottomEdge";
  te.appendChild(be);
  
  var tl=document.createElement("div");
  tl.className="topLeft";
  be.appendChild(tl);
  var tr=document.createElement("div");
  tr.className="topRight";
  tl.appendChild(tr);
  var bl=document.createElement("div");
  bl.className="bottomLeft";
  tr.appendChild(bl);
  var br=document.createElement("div");
  br.className="bottomRight";
  bl.appendChild(br);
  
  this.cdiv=document.createElement("div");
  this.cdiv.style.padding="15px";
  br.appendChild(this.cdiv);

  /*this.ifram=document.createElement("IFRAME");
  this.ifram.src="about:blank";
  this.ifram.style.position="absolute";
  this.ifram.style.width="200px";
  this.ifram.style.display="none";
  this.ifram.style.border="none";
  this.ifram.frameBorder="0";
  this.ifram.style.zIndex="9999";
  this.ifram.style.opacity=this.tracker.opacity;
  this.ifram.style.filter="alpha(opacity="+(this.tracker.opacity*100)+")";*/
  
  this.closeLink=closeLink=document.createElement("A");
  this.closeLink.id="clos_"+name;
  this.closeLink.style.display="block";
  this.closeLink.style.textAlign="right";
  this.closeLink.style.fontSize="9pt";
  this.closeLink.href="";
  this.closeLink.onclick = function() {
    var nm = this.id.substring(this.id.lastIndexOf("_") + 1);


    //add to closed tooltip list
    var tt = tracker.getByName(nm);
    tt.closed = true;

    hide_bubble(nm);


    // set focus back to calling field: The calling element isn't tied to the control we want to set focus back to.
    // for instance: the caller was "p6_DisclosureQMark", but we want focus to go onto "p6_CD" when closed.
    // So, special cases are added where form behavior needed to be modified.  Ideally the element that needed 
    // focuse would be referernced in the tracker element
    switch (name) {
      case "workerLastName":
        if (document.getElementById("p1_WorkerLastName") != null) {
          document.getElementById("p1_WorkerLastName").focus();
        }
        break;
      case "workerLastName3":
        if (document.getElementById("p1_WorkerLastName") != null) {
          document.getElementById("p1_WorkerLastName").focus();
        }
        break;
      case "workerFirstName":
        if (document.getElementById("p1_WorkerFirstName") != null) {
          document.getElementById("p1_WorkerFirstName").focus();
        }
        break;
      case "workerFirstName3":
        if (document.getElementById("p1_WorkerFirstName") != null) {
          document.getElementById("p1_WorkerFirstName").focus();
        }
        break;
      case "RDNum":
        if (document.getElementById("p3_DecisionNumberA") != null) {
          document.getElementById("p3_DecisionNumberA").focus();
        }
        break;
      case "Date":
        if (document.getElementById("p3_DateOfReviewDivisionDecision") != null) {
          document.getElementById("p3_DateOfReviewDivisionDecision").focus();
        }
        break;
      case "WhyIncorrect":
        if (document.getElementById("p4_WhyIsDecisionIncorrect") != null) {
          document.getElementById("p4_WhyIsDecisionIncorrect").focus();
        }
        break;
      case "Disclosure":
        if (document.getElementById("p6_CD") != null) {
          document.getElementById("p6_CD").focus();
        }
        break; 
      case "Language":
        if (document.getElementById("p7_Language") != null) {
          document.getElementById("p7_Language").focus();
        }
        break;
      case "Dialect":
        if (document.getElementById("p7_Dialect") != null) {
          document.getElementById("p7_Dialect").focus();
        }
        break;
    }

    return false;
  }
  this.closeLink.appendChild(document.createTextNode("close"));
  
  this.cdiv.innerHTML=theHtml;
  this.cdiv.appendChild(this.closeLink);
  
  this.addedToBody=false;
}

function _JSTOOLTIPWIDTH_(w) 
{
  this.ttDiv.style.width=w;
}
function _JSTOOLTIPBORDER_(b) 
{
  this.ttDiv.style.border=b;
}
function _JSTOOLTIPBG_(bg) 
{
  this.ttDiv.style.background=bg;
}
function _JSTOOLTIPOPAC_(alpha) 
{
  this.ttDiv.style.opacity=alpha;
  this.ttDiv.style.filter="alpha(opacity="+(alpha*100)+")";
  //this.ifram.style.opacity=alpha;
  //this.ifram.style.filter="alpha(opacity="+(alpha*100)+")";
}

function _JSTOOLTIPSHOW_(event, dly)
{
  this.tracker.closeAll();
  if(event==null) event=window.event;
  
  if(!this.addedToBody) {
    //document.body.appendChild(this.ifram);
    document.body.appendChild(this.ttDiv);
    this.addedToBody=true;
  }
  
  var scrollLeft=posLeft();
  var scrollTop=posTop();

  var x=event.clientX+scrollLeft+15;
  var y=event.clientY+scrollTop+15;

  //this.ttDiv.style.display="block";
  this.ttDiv.className="tooltipvisible";
  
  if(!dly) this.moveTo(x, y);
  else window.setTimeout(this.tracker.name+".getByName('"+this.name+"').moveTo("+x+", "+y+");", this.tracker.delay);

  //this.ifram.style.width=this.ttDiv.offsetWidth+"px";
  //this.ifram.style.height=this.ttDiv.offsetHeight+"px";
  //this.ifram.style.display="block";
}

function _JSTOOLTIPSHOW2_(x, y, dly)
{
  this.tracker.closeAll();
  if(!this.addedToBody) {
    //document.body.appendChild(this.ifram);
    document.body.appendChild(this.ttDiv);
    this.addedToBody=true;
  }
  
  
  var scrollLeft=posLeft();
  var scrollTop=posTop();
  

  //this.ttDiv.style.display="block";
  this.ttDiv.className="tooltipvisible";
  
  if(!dly||dly<=0) this.moveTo(x, y);
  else window.setTimeout(this.tracker.name+".getByName('"+this.name+"').moveTo("+x+", "+y+");", this.tracker.delay);

  //this.ifram.style.width=this.ttDiv.offsetWidth+"px";
  //this.ifram.style.height=this.ttDiv.offsetHeight+"px";
  //this.ifram.style.display="block";
}

function _JSTOOLTIPMOVETO_(x, y)
{
  var availWidth=window.innerWidth;
  if(availWidth==null)availWidth=document.body.offsetWidth; 
  
  var ttWidth=this.ttDiv.offsetWidth+20;
  var ttHeight=this.ttDiv.offsetHeight+20;
  
  if(x<0) x=0;
  if((x+ttWidth)>availWidth) x=availWidth-ttWidth;
  
  //if we hit the top: 
  if(y<10&&this.srcElem!=null) {
    //set the bubble below the key instead
    y=this.srcElem.offsetTop+this.srcElem.offsetHeight+25;
    
    //special case: adjust for calendar if it exists: 
    if(this.srcElem.className=="Date") 
        y=y+100;  
   }
  
  //this.ifram.style.left=x+"px";
  //this.ifram.style.top=y+"px";
  this.ttDiv.style.left=x+"px";
  this.ttDiv.style.top=y+"px";
}

function _JSTOOLTIPHIDE_()
{
  //this.ifram.style.display="none";
  //this.ttDiv.style.display="none";
  this.ttDiv.className="tooltip";
  //document.body.style.margin="10px";
}

function _JSTOOLTIPHTML_(theHtml)
{
  this.ttDiv.innerHTML=theHtml;
}

JSTooltip.prototype.show=_JSTOOLTIPSHOW_;
JSTooltip.prototype.show2=_JSTOOLTIPSHOW2_;
JSTooltip.prototype.hide=_JSTOOLTIPHIDE_;
JSTooltip.prototype.moveTo=_JSTOOLTIPMOVETO_;
JSTooltip.prototype.setWidth=_JSTOOLTIPWIDTH_;
JSTooltip.prototype.setBorder=_JSTOOLTIPBORDER_;
JSTooltip.prototype.setBackground=_JSTOOLTIPBG_;
JSTooltip.prototype.setOpacity=_JSTOOLTIPOPAC_;
JSTooltip.prototype.setHtml=_JSTOOLTIPHTML_;

function show_bubble(elem, ttname) {
  var tt=tracker.getByName(ttname);
  if(tt==null) tt=tracker.getByName("nohelp");
  
  tt.show2(getLeft(elem), getTop(elem), 0);
  
  tt.closeLink.focus();
  tt.srcElem=elem;

  var height=tt.ttDiv.offsetHeight;
  tt.moveTo(getLeft(elem)+10, getTop(elem)-height, 0);
  
  return false;
}

function show_bubble3(elem, ttname, xoffset, yoffset) {
    var tt = tracker.getByName(ttname);
    if (tt == null) tt = tracker.getByName("nohelp");

    tt.show2(getLeft(elem), getTop(elem), 0);

    tt.closeLink.focus();
    tt.srcElem = elem;

    var height = tt.ttDiv.offsetHeight;
    tt.moveTo(getLeft(elem) + xoffset, getTop(elem) + yoffset, 0);

    return false;
}

function show_bubble_and_return(elem, isAutoEvent, destinationElem) {
  
  


}

function show_bubble2(elem, isAutoEvent) {
  if(elem==null) return;
  
  var tt=tracker.getByName(elem.className);
  if(tt==null) tt=tracker.getByName("nohelp");
  
  if(!isAutoEvent||!tt.closed) {
  
    tt.show2(getLeft(elem), getTop(elem), 0);
    
    //tt.closeLink.focus();
    tt.srcElem=elem;

    var height;

    //these elements appear near the top of the form and thus need to be possitioned
    //below the element that they are associated with (i.e. they need to be possitioned
    //differently/below unlike the rest of the help bubbles).
    if (elem.className == "WorkSafeBCClaim"
     || elem.className == "CustomerCare"
     || elem.className == "CareCard"
     || elem.className == "RFS"
     || elem.className == "administrative"
     || elem.className == "administrative24"
     || elem.className == "Date3"
     || elem.className == "WorkSafeBCDA"
     || elem.className == "WCATDecision65"
     || elem.className == "WCATDate65"
     || elem.className == "WSBCFile"
     ) {
        
      height = tt.ttDiv.offsetHeight;
      if (getTop(elem) - height < 0) {
        tt.moveTo(getLeft(elem) + 30, getTop(elem) + 38, 0);
      }
      else {
        tt.moveTo(getLeft(elem) + 10, getTop(elem) + 38, 0);
      }

    }
    else {
      height = tt.ttDiv.offsetHeight;
      if (getTop(elem) - height < 0) {
        tt.moveTo(getLeft(elem) + 30, getTop(elem) - height, 0);
      }
      else {
        tt.moveTo(getLeft(elem) + 10, getTop(elem) - height, 0);
      }
    }
  }
  return false;
}
function hide_bubble(ttname) {
  var tt=tracker.getByName(ttname);
  if(tt==null) tt=tracker.getByName("nohelp");
  tt.hide();

  if(tt.srcElem!=null) tt.srcElem.focus();
  return false;
}