var sWidthToolTipDefault = 150;
/*----------------------------------------------------------------------------\
|                               Help Tip 1.12                                 |
|-----------------------------------------------------------------------------|
| GPL - The GNU General Public License    http://www.gnu.org/licenses/gpl.txt |
| Permits anyone the right to use and modify the software without limitations |
| as long as proper  credits are given  and the original  and modified source |
| code are included. Requires  that the final product, software derivate from |
| the original  source or any  software  utilizing a GPL  component, such  as |
| this, is also licensed under the GPL license.                               |
|-----------------------------------------------------------------------------|
| 2002-09-27 |                                                                |
| 2001-11-25 | Added a resize to the tooltip if the document width is too     |
|            | small.                                                         |
| 2002-05-19 | IE50 did not recognise the JS keyword undefined so the test    |
|            | for scroll support was updated to be IE50 friendly.            |
| 2002-07-06 | Added flag to hide selects for IE                              |
| 2002-10-04 | (1.1) Restructured and made code more IE garbage collector     |
|            | friendly. This solved the most nasty memory leaks. Also added  |
|            | support for hiding the tooltip if ESC is pressed.              |
| 2002-10-18 | Fixed verrical position in case of scrolled document.          |
| 2002-12-02 | Mozilla bug workaround related to mousedown and move.          |
|-----------------------------------------------------------------------------|
| Dependencies: helptip.css (To set up the CSS of the helptooltip class)     |
|-----------------------------------------------------------------------------|
| Usage:                                                                      |
|                                                                             |
|   <script type="text/javascript" src="helptip.js">< /script>                |
|   <link type="text/css" rel="StyleSheet" href="helptip.css" />              |
|                                                                             |
|   <a class="helpLink" href="?" onclick="showHelp(event, 'String to show');  |
|      return false">Help</a>                                                 |
|-----------------------------------------------------------------------------|
| Created 2001-09-27 | All changes are in the log above. | Updated 2002-12-02 |
\----------------------------------------------------------------------------*/

function HideShowElement( elmID, overDiv, h )
{//alert('por ocultar los:' + elmID + ' para el obj: ' + overDiv )
	if ( document.all )
	{
		for( i = 0; i < document.getElementsByTagName( elmID ).length; i++ )
		{
		  obj = document.getElementsByTagName( elmID )[i];
		  if( !obj || !obj.offsetParent )
		  {
			continue;
		  }
		  // Find the element's offsetTop and offsetLeft relative to the BODY tag.
		  objLeft   = obj.offsetLeft;
		  objTop    = obj.offsetTop;
		  objParent = obj.offsetParent;
		  while( objParent.tagName.toUpperCase() != 'BODY' )
		  {
			objLeft  += objParent.offsetLeft;
			objTop   += objParent.offsetTop;
			objParent = objParent.offsetParent;
		  }
		  objHeight = obj.offsetHeight;
		  objWidth = obj.offsetWidth;


		  if(( overDiv.offsetLeft-12 + overDiv.offsetWidth ) <= objLeft );
		  else if(( overDiv.offsetTop-0 + overDiv.offsetHeight ) <= objTop );//-15
		  else if( overDiv.offsetTop-15 >= ( objTop + objHeight ));
		  else if( overDiv.offsetLeft-12 >= ( objLeft + objWidth ));
		  else
		  {
			obj.style.visibility = h;
		  }
		}
	}
}
/*
function showHelpTip(e, sHtml, bHideSelects) {

	// find anchor element
	var el = e.target || e.srcElement;
	var typeElement = e.tagName
	while (el.tagName != typeElement)
		el = el.parentNode;
	
	// is there already a tooltip? If so, remove it
	if (el._helpTip) {
		helpTipHandler.hideHelpTip(el);
//helpTipHandler.showSelects(true);
return;
	}
	*/
var sWidthToolTip = 100;
var oImage = null;
function showHelpTip(e, sHtml, bHideSelects, sWidth, oObjImage)
{
	oImage = oObjImage;
	if ((arguments.length >= 4) && (sWidth != null))
		sWidthToolTip = sWidth;
	else sWidthToolTip = sWidthToolTipDefault;
	// find anchor element
	var el = e.target || e.srcElement;
	var typeElement = e.tagName
	while (el.tagName != typeElement)
		el = el.parentNode;
	
	// is there already a tooltip? If so, remove it
	if (el._helpTip) {
		helpTipHandler.hideHelpTip(el);
//helpTipHandler.showSelects(true);
return;
	}
	
	
	helpTipHandler.hideSelects = Boolean(bHideSelects);

	// create element and insert last into the body
	helpTipHandler.createHelpTip(el, sHtml);
	
	// position tooltip
	helpTipHandler.positionToolTip(e);

	// add a listener to the blur event.
	// When blurred remove tooltip and restore anchor
	el.onblur = helpTipHandler.anchorBlur;
	el.onkeydown = helpTipHandler.anchorKeyDown;
}

var helpTipHandler = {
	hideSelects:	false,
	
	helpTip:		null,
	
	showSelects:	function (bVisible) {
		/////if (!this.hideSelects) return;
		// only IE actually do something in here
		var selects = [];

var elmID = "SELECT";
var overDiv = this.helpTip;//"testHelpTip";
var h = bVisible;
//alert( this.helpTip.id )


/*	if ( document.all )
	{
		for( i = 0; i < document.getElementsByTagName( elmID ).length; i++ )
		{
		  obj = document.getElementsByTagName( elmID )[i];
		  if( !obj || !obj.offsetParent )
		  {
			continue;
		  }
		  // Find the element's offsetTop and offsetLeft relative to the BODY tag.
		  objLeft   = obj.offsetLeft;
		  objTop    = obj.offsetTop;
		  objParent = obj.offsetParent;
		  while( objParent.tagName.toUpperCase() != 'BODY' )
		  {
			objLeft  += objParent.offsetLeft;
			objTop   += objParent.offsetTop;
			objParent = objParent.offsetParent;
		  }
		  objHeight = obj.offsetHeight;
		  objWidth = obj.offsetWidth;
		  if(( overDiv.offsetLeft + overDiv.offsetWidth ) <= objLeft );
		  else if(( overDiv.offsetTop + overDiv.offsetHeight ) <= objTop );
		  else if( overDiv.offsetTop >= ( objTop + objHeight ));
		  else if( overDiv.offsetLeft >= ( objLeft + objWidth ));
		  else
		  {
			obj.runtimeStyle.visibility = bVisible ? "" : "hidden";	
			//obj.style.visibility = h;
		  }
		}
	}*/

/*
		if (document.all)
			selects = document.all.tags("SELECT");
		var l = selects.length;
		for	(var i = 0; i < l; i++)
			selects[i].runtimeStyle.visibility = bVisible ? "" : "hidden";	
*/

	},
	
	create:	function () {
		var d = document.createElement("DIV");
		d.id = "boxHelpTip"
		d.className = "helptooltip";
		d.onmousedown = this.helpTipMouseDown;
		d.onmouseup = this.helpTipMouseUp;
		/*alert( sWidthToolTip )
		if ( sWidthToolTip < 0 )
			d.style.width = ""
		else
			d.style.width = sWidthToolTip;// +"px";
			*/
		document.body.appendChild(d);		
		this.helpTip = d;
	},
	
	createHelpTip:	function (el, sHtml) {
		if (this.helpTip == null) {
			this.create();
		}

		var d = this.helpTip;
		//document.getElementById( d.id ).innerHTML = sHtml;
		//alert( document.getElementById( d.id ).innerText )
		//d.innerHTML = sHtml;
		//alert(sHtml);
		//alert( d.innerText);
		
		//var sText = "";
		//sText+= (d.innerText==undefined)?d.innerHTML:d.innerText;
		//alert( sHtml )
		//alert( replace(sHtml,"\"","&quot;") );
		//sHtml.replace(,"/"/g","&quot;");
		
		////var dCopy = document.createElement("DIV");
		////document.body.appendChild(dCopy);
		//dCopy.className = "helptooltip";
		////dCopy.innerHTML = sHtml;
		////dCopyWidth = dCopy.offsetWidth;
		//alert( dCopy.innerHTML )
		////alert( dCopyWidth + " // " + dCopy.clientWidth );
		
		d.innerHTML=sHtml;
		d._boundAnchor = el;
		el._helpTip = d;
		return d;
	},
	
	// Allow clicks on A elements inside tooltip
	helpTipMouseDown:	function (e) {
		var d = this;
		var el = d._boundAnchor;
		if (!e) e = event;
		var t = e.target || e.srcElement;
		while (t.tagName != "A" && t != d)
			t = t.parentNode;
		if (t == d) return;
		
		el._onblur = el.onblur;
		el.onblur = null;
	},
	
	helpTipMouseUp:	function () {
		var d = this;
		var el = d._boundAnchor;
		el.onblur = el._onblur;
		el._onblur = null;
		el.focus();
	},	
	
	anchorBlur:	function (e) {
		var el = this;
		helpTipHandler.hideHelpTip(el);
	},
	
	anchorKeyDown:	function (e) {
		if (!e) e = window.event
		if (e.keyCode == 27) {	// ESC
			helpTipHandler.hideHelpTip(this);
		}
	},
	
	removeHelpTip:	function (d) {
		d._boundAnchor = null;
		d.style.filter = "none";
		d.innerHTML = "";
		d.onmousedown = null;
		d.onmouseup = null;
		d.parentNode.removeChild(d);
		//d.style.display = "none";
	},
	
	hideHelpTip:	function (el) {
		var d = el._helpTip;
		/*	Mozilla (1.2+) starts a selection session when moved
			and this destroys the mouse events until reloaded
		d.style.top = -el.offsetHeight - 100 + "px";
		*/		
		
		d.style.visibility = "hidden";
		//d._boundAnchor = null;

		el.onblur = null;
		el._onblur = null;
		el._helpTip = null;
		el.onkeydown = null;
		
		this.showSelects(true);
	},
	
	positionToolTip:	function (e) {
		this.showSelects(false);		
		var scroll = this.getScroll();
		var d = this.helpTip;
		//alert( sWidthToolTip )
		if ( sWidthToolTip < 0 )
			d.style.width = ""
		else
			d.style.width = sWidthToolTip;
//d.style.width = sWidthToolTip;
		// width
		/*
		if (d.offsetWidth >= scroll.width)
			d.style.width = scroll.width - 10 + "px";
		else
			d.style.width = "";
		*/
		// left
		ss =scroll.width - d.offsetWidth
		////alert( d.offsetWidth + "**" + scroll.width + "--" + d.clientWidth)
		
		//alert( e.clientX + " -- " + ss )
		if (e.clientX > ss)
			d.style.left = ss + scroll.left-20 + "px";//less 20 for mozilla and netscape
		else
			d.style.left = e.clientX - 2 + scroll.left - d.offsetWidth + "px";// " - d.offsetWidth" 07/01/05
			
		if (parseInt(d.style.left) < 0 ) d.style.left = 5 + "px";
		//d.style.width = d.offsetWidth
		//alert( d.style.left )
		// top
		if (e.clientY + d.offsetHeight + 18 < scroll.height)
			d.style.top = e.clientY + 18 + scroll.top + "px";
		else if (e.clientY - d.offsetHeight > 0)
			d.style.top = e.clientY + scroll.top - d.offsetHeight + "px";
		else
			d.style.top = scroll.top + 5 + "px";
		


		/*****************************************/
		if (oImage)
		{
			if (oImage.offsetParent)
			{
				objLeft   = oImage.offsetLeft;
				objTop    = oImage.offsetTop;
				objParent = oImage.offsetParent;
					
				while (objParent.tagName.toUpperCase() != "BODY")
				{
					objLeft  += objParent.offsetLeft;
					objTop   += objParent.offsetTop;
					objParent = objParent.offsetParent;
				}
				
				objBottom = objTop + oImage.offsetHeight - 1;
				objRight = objLeft + oImage.offsetWidth - 1;
				
				divLeft = parseInt(d.style.left);
				divRight = divLeft + d.offsetWidth;
				divTop = parseInt(d.style.top);
				divBottom = divTop + d.offsetHeight;
				
				if ((
				((objLeft >= divLeft) && (objLeft <= divRight)) ||
				(((objRight) >= divLeft) && (objRight <= divRight)) ||
				((objLeft <= divLeft) && (objRight >= divRight))
				)
				&&
				(
				((objTop >= divTop) && (objTop <= divBottom)) ||
				((objBottom >= divTop) && (objBottom <= divBottom)) ||
				((objTop <= divTop) && (objBottom >= divBottom))
				))
				{
					if (objTop > divTop)
					{
						iNewTop = parseInt(d.style.top) - (divBottom - objTop);
						if (iNewTop >= 0)
						{
							d.style.top = iNewTop + "px";
						}
						else
						{
							if (objLeft > divLeft)
							{
								iNewLeft = parseInt(d.style.left) - (divRight - objLeft);
							}
							else
							{
								iNewLeft = parseInt(d.style.left) + (objLeft - divLeft);
							}
							d.style.left = iNewLeft + "px";
						}
					}
					else
					{
						iNewTop = parseInt(d.style.top) + (objTop - divTop);
						if ((iNewTop + d.offsetHeight) <= scroll.height)
						{
							d.style.top = iNewTop + "px";
						}
						else
						{
							if (objLeft > divLeft)
							{
								iNewLeft = parseInt(d.style.left) - (divRight - objLeft);
							}
							else
							{
								iNewLeft = parseInt(d.style.left) + (objLeft - divLeft);
							}
							d.style.left = iNewLeft + "px";
						}
					}
				}

			}
		}
		/*****************************************/
		
		d.style.visibility = "visible";
	},
	
	// returns the scroll left and top for the browser viewport.
	getScroll:	function () {
		if (document.all && typeof document.body.scrollTop != "undefined") {	// IE model
			var ieBox = document.compatMode != "CSS1Compat";
			var cont = ieBox ? document.body : document.documentElement;
			return {
				left:	cont.scrollLeft,
				top:	cont.scrollTop,
				width:	cont.clientWidth,
				height:	cont.clientHeight
			};
		}
		else {
			return {
				left:	window.pageXOffset,
				top:	window.pageYOffset,
				width:	window.innerWidth,
				height:	window.innerHeight
			};
		}
		
	}

};