function UITools() {}

new UITools();

// open the popup
// popup id - id of the div element
// element - element to calculate te relative position for the div
// width - width of the div to set (-1 if don't need to set)
// height - height of the div to set to (-1 if don't need to to set)
// forceW - true - always force the width of the div to specified 'width', false - force only if the scrollWidth > width
// forceH - the same for the height
// x_adj - adjustment to the left position of the div
// y_adj - adjustment to the top posistion of the div
UITools.prototype.openPopup = function(popupID, element, width, height, forceW, forceH, x_adj, y_adj)  {
	var div = document.getElementById(popupID);
	if( div )
	{
		var end = calculateOffset(element, "offsetLeft");
		var top = calculateOffset(element, "offsetTop") + element.offsetHeight;
		div.style.left = end + x_adj + "px";
		div.style.top = top + y_adj + "px";

		// show the popup
		div.style.display = "block";
		if( width > -1 )
		{
			if( forceW ) 
				div.style.width = width + "px";
			else
				if( div.scrollWidth > width ) div.style.width = width + "px";
		}
		if( height > -1) 
		{
			if( forceH ) 
				div.style.height = height + "px";
			else
				if( div.scrollHeight > height ) div.style.height = height + "px";
		}

		// if there is an iframe defined with the id <popupID>Frm, 
		// then activate the underling iframe to go over any select tags - IE bug
		var ifrm = document.getElementById(popupID + "Frm");
		if( ifrm )
		{
			ifrm.style.left = div.style.left;
			ifrm.style.top = div.style.top;
			ifrm.style.height = div.offsetHeight + "px";
			ifrm.style.width = div.offsetWidth + "px";
			ifrm.style.display = "";
		}
	}
}

// close popup
UITools.prototype.closePopup = function(popupID)
{
	var div = document.getElementById(popupID);
	if( div ) div.style.display = "none";
	
	// disactivate the underling iframe that goes over any select tags - IE bug
	var ifrm = document.getElementById(popupID + "Frm");
	if( ifrm ) ifrm.style.display = "none";
}

// Find the element's offset relative to the #maincontent <div> tag.
function calculateOffset(field, attr)
{
	var offset = 0;
	while(field)
	{
		offset += field[attr];
		field = field.offsetParent;
	}
	return offset;
}

/* Show/Hide Element
------------------------------------------------------- */
function showDiv(obj) {
	var el = document.getElementById(obj);
	if(el.style.display != "block")	{
		el.style.display = "block";
	}
	else {
		el.style.display = "none";
	}
}