/*******************************************************************************************************************************************

	This file controls the field help popups that appear when you mouseover certain fields in the control panel
	
	History:
		File created on 11/02/2005
		File modified on 01/18/2006 - added support for pulling field def record by passing it's record number to show_field_help()
		
*******************************************************************************************************************************************/
var helpCache = new Array();
var helpFld;
var helpLoadingText = '<div class="valign_middle" style="margin-bottom: 20px"><img src="images/cart/indicator.gif" /><span> Loading</span></div>';
var helpPendingCount = 0;

function set_field_help(helpText) {
	var helpDiv 			= document.getElementById("help_div");
	var helpDivInside		= document.getElementById("help_div_inside");
	var shim 				= document.getElementById('DivShim');
	// let page know how many outstanding requests are pending
	helpPendingCount--;

	// bail if there are any pending help requests that were made after this one
	if (helpPendingCount > 0) return;
	
	helpCache[helpFld] = helpText;
	
	helpDivInside.innerHTML = helpText;
	
	// set position of iframe right behind the div, so it will cover up select inputs
	//shim.style.border = "black solid 1px";
	if (document.all) {
		shim.style.width 	= helpDiv.offsetWidth;
		shim.style.height 	= helpDiv.offsetHeight; // 16 is the height of the balloon tip....
		//shim.style.top 		= newTop; // 16 is the height of the balloon tip....
		shim.style.top 		= helpDiv.style.top; 
		shim.style.left 	= helpDiv.style.left;
		shim.style.zIndex 	= helpDiv.style.zIndex -1 ;
		shim.style.display 	= '';
	}	
}



/*******************************************************************************************************************************************
	Purpose of function is to show a help popup when a field is moused over
	
	Parameters:
		x	(integer)	x coordinate of event
		y	(integer)	y coordinate of event
		tbl	(string)	name of table to pull field info for
		fld	(string)	name of field to pull field info for
		rn	(integer)	record number of field def record, to bypass the lookup. If this is used, tbl and fld should be passed as 0 or empty strings
		
*******************************************************************************************************************************************/
function show_field_help(x,y,tbl,fld,rn) {
	// grab some elements
	var helpDiv 		= document.getElementById("help_div");
	var helpDivInside	= document.getElementById("help_div_inside");
	var shim 			= document.getElementById('DivShim');
	
	// change text inside help div
	//if (helpText) helpDivInside.innerHTML = helpText;
	if (tbl || rn) {
		if (rn == undefined) var rn = 'false';
		
		// hide the shim...this is a new field that is being moused over, and the shim could be a different size
		shim.style.display = "none";
		
		// this will be used as the key to cache the help info
		helpFld = rn != 'false' ? rn : fld;
		
		if (helpCache[rn]) {
			helpDivInside.innerHTML = helpCache[rn];
		} else if (helpCache[fld]) {
			helpDivInside.innerHTML = helpCache[fld];
		} else {
			helpDivInside.innerHTML = helpLoadingText;
			
			// let this page now how many help requests are pending
			helpPendingCount++;
			x_get_field_help(tbl,fld,rn,set_field_help);
		}
	}

	//helpDiv.style.display = '';
	
	var contentDiv 		= document.getElementById("content");
	var contentBottom 	= contentDiv.offsetHeight;
	var balloonTip 		= document.getElementById("balloon_tip");
	var newLeft 		= (x + contentDiv.scrollLeft - 0) - helpDiv.offsetWidth - 7 ;
	var newTop 			= y + 15;

	// if the new left position is off the screen, then set it to 0 so it won't go off
	if (newLeft < 0) {
		newLeft = 0;
		pastLeft = true;
	} else {
		pastLeft = false;
	}

	helpDiv.style.left = newLeft + 'px';
	newBottom = newTop + helpDiv.offsetHeight;

	// if the new bottom of the help div is past the bottom of the content area, then don't allow
	// it to go past.....adjust the top  position to make it stay put
	if (newBottom > contentBottom) {
		newTop = newTop - (newBottom - contentBottom);
		//balloonTip.style.display = 'none';
	//	balloonTip.style.top = newBottom;
		balloonTip.style.display = 'none';
		pastBottom = true;
	} else {
		pastBottom = false;
		balloonTip.style.display = '';
	}

	helpDiv.style.top = newTop;
	
	
	

	// make field_help appear
	helpDiv.style.display = "";

	// set position of iframe right behind the div, so it will cover up select inputs
	if (document.all) {
		shim.style.width 	= helpDiv.offsetWidth ;
		shim.style.height 	= helpDiv.offsetHeight; // 16 is the height of the balloon tip....
		shim.style.top 		= newTop; // 16 is the height of the balloon tip....
		//shim.style.left 	= helpDiv.style.left;
		shim.style.left 	= helpDiv.offsetLeft;
		shim.style.zIndex 	= helpDiv.style.zIndex -1;
		shim.style.display 	= "";
	}
}


function hide_field_help() {
	// grab some elements
	var helpDiv = document.getElementById("help_div");
	var shim = document.getElementById('DivShim');
	var helpDivInside	= document.getElementById("help_div_inside");

	helpDiv.style.display = "none";
	if (document.all) shim.style.display = 'none';
	helpDivInside.innerHTML = helpLoadingText;
}
