/*
 *  File:     pexodmodaldialog.js
 *
 *  Usage:    Setup:
 *            <link rel="stylesheet" type="text/css" href="standardincludes/pexodmodaldialog.css" />
 *            <script src="standardincludes/pexodmodaldialog.js" type="text/javascript"></script>
 *            var oModalDialog = new ModalDialog( "oModalDialog" );
 *
 *            NOTE:
 *            You have to have the style sheet in order to make this work.
 *
 *
 *            Display:
 *            <span onclick="oModalDialog.ShowDialog( 'testpopup_popup.php', 500, 200, TestCallback )">
 *            where
 *            testpopup_popup.php is the content of the dialog
 *            500, 200 are the width and height of the dialog
 *            TestCallback is the callback function to be called when the dialog closes - can be null
 *
 *            Close:
 *            <span modaldialog_parm_1="pone" id="closespanner" onclick="window.top[ 'oModalDialog' ].HideDialog( 'closespanner' );">close</span>
 *            where
 *            modaldialog_parm_1 is an example of a parameter that the callback is interested in,
 *            and is made available by the inclusion of the 'this' parameter
 *
 */

// Tab key trap. iff popup is shown and key was [TAB], suppress it.
// @argument e - event - keyboard event that caused this function to be called.
function keyDownHandler( e )
{
    if( oModalDialog )
    {
        // Tab key trap. iff popup is shown and key was [TAB], suppress it.
        if( oModalDialog.IsDialogVisible() && e.keyCode == 9 )
            return false;

        // Escape - user is bailing out of the dialog, so just close it with no
        // return values
        if( oModalDialog.IsDialogVisible() && e.keyCode == 27 )
            oModalDialog.HideDialog("");
    }
    else
    {
        return true;
    }
}
// If using Mozilla or Firefox, use Tab-key trap.
if( !document.all )
{
	document.onkeypress = keyDownHandler;
}

//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// requires a string containing the name of the instance
function ModalDialog( sInstanceName )
{
	// name of callback - function to call when the dialog closes
	var m_sCallback = "";
	// ids, and hence connections to style sheet, of the built components
	var m_sIdBackground = "BackgroundOverlayDiv";
	var m_sIdContainer = "DialogContainerDiv";
	var m_sIFrame = "DialogIFrame";
	// default values, never actually used
	var m_nWidth = 100;
	var m_nHeight = 100;
	// used for event handlers, a kludge to allow them to reach a specific instance of this class
	var m_sInstanceName = sInstanceName;
	// internal status
	var m_bDialogIsShowing = false;
	var m_bHideSelects = false;
	var m_aTabIndexes = new Array();
	var m_aTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME"); // Pre-defined list of tags we want to disable/enable tabbing into
	// the built pieces of the dialog system
	var m_oBackgroundOverlayDiv = null;
	var m_oDialogIFrame = null;
	var m_oDialogContainerDiv = null;

	dputil_AddEventHandler( window, "load", m_sInstanceName+".InitDialog()" );
	dputil_AddEventHandler( window, "resize", m_sInstanceName+".SizeAndPositionDialog()" );
	dputil_AddEventHandler( window, "scroll", m_sInstanceName+".SizeAndPositionDialog()" );

	//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
	//
	// PRIVATE METHODS:
	//
	//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

	//----------------------------------------------------
	// For IE.  Go through predefined tags and disable tabbing into them.
	function pDisableTabIndexes()
	{
		if (document.all)
		{
			var i = 0;
			for (var j = 0; j < m_aTabbableTags.length; j++)
			{
				var tagElements = document.getElementsByTagName(m_aTabbableTags[j]);
				for (var k = 0 ; k < tagElements.length; k++)
				{
					m_aTabIndexes[i] = tagElements[k].tabIndex;
					tagElements[k].tabIndex="-1";
					i++;
				}
			}
		}
	}

	//----------------------------------------------------
	// For IE. Restore tab-indexes.
	function pRestoreTabIndexes()
	{
		if( document.all )
		{
			var i = 0;
			for( var j=0; j<m_aTabbableTags.length; j++ )
			{
				var tagElements = document.getElementsByTagName(m_aTabbableTags[j]);
				for( var k = 0 ; k < tagElements.length; k++ )
				{
					tagElements[k].tabIndex = m_aTabIndexes[i];
					tagElements[k].tabEnabled = true;
					i++;
				}
			}
		}
	}

	//----------------------------------------------------
	// Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
	// IE has a problem with wanted select form tags to always be the topmost z-index or layer
	function pHideSelectBoxes()
	{
		for(var i = 0; i < document.forms.length; i++)
		{
			for(var e = 0; e < document.forms[i].length; e++)
			{
				if(document.forms[i].elements[e].tagName == "SELECT")
				{
					document.forms[i].elements[e].style.visibility="hidden";
				}
			}
		}
	}

	//----------------------------------------------------
	// Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
	// IE has a problem with wanted select form tags to always be the topmost z-index or layer
	function pDisplaySelectBoxes()
	{
		for(var i = 0; i < document.forms.length; i++)
		{
			for(var e = 0; e < document.forms[i].length; e++)
			{
				if(document.forms[i].elements[e].tagName == "SELECT")
				{
					document.forms[i].elements[e].style.visibility="visible";
				}
			}
		}
	}

	//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
	//
	// PRIVILEGED METHODS:
	//
	//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

	//----------------------------------------------------
	// build HTML components:
	// div to create overlay to hide existing page,
	// div to contain the iframe, and provide border
	// iframe to contain the guts of the dialog
	this.InitDialog = function()
	{
		var oBody = document.getElementsByTagName( "body" )[0];
		// the background
		var oBackground = document.createElement( "div" );
		m_oBackgroundOverlayDiv = oBody.appendChild( oBackground );
		m_oBackgroundOverlayDiv.id = m_sIdBackground;
		//m_oBackgroundOverlayDiv.style.top = "0px";
		//m_oBackgroundOverlayDiv.style.left = "0px";
		//m_oBackgroundOverlayDiv.style.height = "100%";
		//m_oBackgroundOverlayDiv.style.width = "100%";
		// iframe holder
		var oHolder = document.createElement( "div" );
		m_oDialogContainerDiv = oBody.appendChild( oHolder );
		m_oDialogContainerDiv.id = m_sIdContainer;
		// iframe
		var oIFrame = document.createElement( "iframe" );
		m_oDialogIFrame = m_oDialogContainerDiv.appendChild( oIFrame );
		m_oDialogIFrame.setAttribute( "src", "" );
		m_oDialogIFrame.setAttribute( "scrolling", "auto" );
		m_oDialogIFrame.setAttribute( "frameborder", "0" );
		m_oDialogIFrame.id = m_sIFrame;
		//m_oDialogIFrame.style.width = "100%";
		//m_oDialogIFrame.style.height = "100%";
		// IE has to hide selects on underlying page,
		// otherwise they poke through the overlay
		var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
		if( brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1 )
		{
			m_bHideSelects = true;
		}
	}

	//----------------------------------------------------
	//
	this.ShowDialog = function( url, width, height, sCallback )
	{
		// save this so that it can be called when the dialog is closed
		m_sCallback = sCallback;
		m_bDialogIsShowing = true;
		pDisableTabIndexes();
		m_oBackgroundOverlayDiv.style.visibility = "visible";
		m_oDialogContainerDiv.style.visibility = "visible";
		m_oDialogIFrame.style.visibility = "visible";
		// save the requested dimensions of the dialog
		m_nWidth = width;
		m_nHeight = height;
		// set the sizes to the dialog
		m_oDialogContainerDiv.style.width = m_nWidth + "px";
		m_oDialogContainerDiv.style.height = m_nHeight + "px";
		// position the dialog and resize the background overlay
		this.SizeAndPositionDialog();
		// set the url
		m_oDialogIFrame.src = url;
		// for IE
		if( m_bHideSelects == true )
		{
			pHideSelectBoxes();
		}
	}

	//----------------------------------------------------
	// resize the overlay and keep dialog centered, and
	// keep the width of the dialog within bounds,
	// there is some care to be compatible with both IE and FF
	this.SizeAndPositionDialog = function()
	{
		var oBody = document.getElementsByTagName( "body" )[0];
		if( true == m_bDialogIsShowing )
		{
			var nScrollTop = oBody.scrollTop;
			var nScrollLeft = oBody.scrollLeft;
			var nClientHeight = oBody.clientHeight;
			var nClientWidth = oBody.clientWidth;
			// resize the background overlay
			m_oBackgroundOverlayDiv.style.height = nScrollTop + nClientHeight + "px";
			m_oBackgroundOverlayDiv.style.width = nScrollLeft + nClientWidth + "px";
			// horizontal reposition
			if( m_nWidth > nClientWidth )
			{
				m_oDialogContainerDiv.style.left =  "0px";
			}
			else
			{
				m_oDialogContainerDiv.style.left = (nScrollLeft + (nClientWidth-m_nWidth)/2) + "px";
			}
			// vertical reposition
			if( m_nHeight > nClientHeight )
			{
				m_oDialogContainerDiv.style.top =  "0px";
			}
			else
			{
				//m_oDialogContainerDiv.style.top = (nScrollTop + (nClientHeight-m_nHeight)/2) + "px";
                m_oDialogContainerDiv.style.top = "50px";
			}
		}
	}

	//----------------------------------------------------
	// how to do callback ... ?
	this.HideDialog = function( sCloserId )
	{
		m_bDialogIsShowing = false;
		pRestoreTabIndexes();
		if( m_oBackgroundOverlayDiv == null )
		{
			return;
		}
		m_oBackgroundOverlayDiv.style.visibility = "hidden";
		m_oDialogContainerDiv.style.visibility = "hidden";
		m_oDialogIFrame.style.visibility = "hidden";
		m_oDialogIFrame.src = "";
		// display all select boxes
		if( m_bHideSelects == true )
		{
			pDisplaySelectBoxes();
		}
		// call the callback with pointer to element provided
		// by the closing function - if there are any parameters
		// required by the callback function, it will have to
		// look at the attributes of the element to find them
		if( m_sCallback )
		{
			var oCloser = m_oDialogIFrame.contentWindow.document.getElementById( sCloserId );
			m_sCallback( oCloser );
		}
	}

	//----------------------------------------------------
	// return visibility of the dialog
	this.IsDialogVisible = function()
	{
		return m_bDialogIsShowing;
	}
}

