var currentlyVisible = null;
function showPopup( objectId, element )
{
	var popupToShow = document.getElementById( objectId );
	var showPopup = true;

	if( currentlyVisible )
	{
		if( currentlyVisible.id == objectId ) showPopup = false;

		currentlyVisible.style.display = "none";
		currentlyVisible = null;
	}

	if( popupToShow && showPopup )
	{
		popupToShow.style.display = "block";
		currentlyVisible = popupToShow;

		if( element )
		{
		  var elemLeft = findLeft( element );
			popupToShow.style.position = "absolute";
 			popupToShow.style.top = (findTop( element ) + element.offsetHeight) + "px";
      if ( (elemLeft + popupToShow.offsetWidth) > document.body.offsetWidth ) {
        popupToShow.style.left = (document.body.offsetWidth - popupToShow.offsetWidth) + "px";
      } else {
        popupToShow.style.left = (elemLeft) + "px";
      }
		}
	}
}

function findTop( element )
{
	if( element.offsetParent )
	{
		return( element.offsetTop + findTop( element.offsetParent ));
	}
	else
	{
		return element.offsetTop;
	}
}

function findLeft( element )
{
	if( element.offsetParent )
	{
		return( element.offsetLeft + findLeft( element.offsetParent ));
	}
	else
	{
		return( element.offsetLeft );
	}
}

function toggle( element, target )
{
  checkbox = document.getElementById( element );
  target = document.getElementById(target);

  if (checkbox.checked) {
  	target.style.display = '';

  	var iata_num = document.getElementById('iatanumber');
  	// trim whitespace
  	iata_num.value = iata_num.value.replace(/^\s+|\s+$/, '');
  	if (iata_num.value == "") iata_num.value = 'Enter IATA#';
  }	else {
  	target.style.display='none';
  }
}
