/**
 * SUBMODAL v1.6
 * Used for displaying DHTML only popups instead of using buggy modal windows.
 */

/*
 * Popup Global Variables
*/
var subModal_gPopupMask      = null;
var subModal_gPopupContainer = null;
var subModal_gPopupIsShown   = false;
var subModal_gPopupTitle     = "eZoobase";
var subModal_gPopupMessage   = "Elaborazione richiesta in corso";
var subModal_gHideSelects    = false;
var subModal_gTabIndexes     = new Array();
var subModal_gTabbableTags   = new Array("A", "BUTTON", "TEXTAREA", "INPUT", "IFRAME");
var subModal_gLoadable       = true;
var subModal_gDelayTime      = 200;


/*
 * X-browser event handler attachment and detachment
 *
 * @argument obj    - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn     - event handler
 */
function addEvent(obj, evType, fn)
{
    if (obj.addEventListener)
        { obj.addEventListener(evType, fn, false); return true; } 
    else if (obj.attachEvent)
        { var rt = obj.attachEvent("on" + evType, fn); return rt; } 
    else 
        { return false; }
}

/*
 * X-browser event handler attachment and detachment
 *
 * @argument obj    - the object to detach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn     - event handler
 */
function removeEvent(obj, evType, fn, useCapture)
{
    if (obj.removeEventListener)
        { obj.removeEventListener(evType, fn, useCapture); return true; } 
    else if (obj.detachEvent)
        { var rt = obj.detachEvent("on" + evType, fn); return rt; } 
    else 
        { alert("Handler could not be removed"); }
}

/**
 * X-browser Get Viewport dimension
 */
function getViewportHeight() 
{
	if (window.innerHeight != window.undefined) 
	    return window.innerHeight;
	if (document.compatMode=='CSS1Compat') 
	    return document.documentElement.clientHeight;
	if (document.body) 
	    return document.body.clientHeight; 
	return window.undefined; 
}

function getViewportWidth() 
{
	if (window.innerWidth!=window.undefined) 
	    return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') 
	    return document.documentElement.clientWidth; 
	if (document.body) 
	    return document.body.clientWidth; 
	return window.undefined; 
}

/**
 * getScrollTop
 *      Gets the real scroll top & left
 */
function getScrollTop() 
{
	if (self.pageYOffset)                                                       // all except Explorer
	    { return self.pageYOffset; }
	else if (document.documentElement && document.documentElement.scrollTop)    // Explorer 6 Strict
	    { return document.documentElement.scrollTop; }
	else if (document.body)                                                     // all other Explorers
	    { return document.body.scrollTop; }
}

function getScrollLeft() 
{
	if (self.pageXOffset)                                                       // all except Explorer
	    { return self.pageXOffset; }
	else if (document.documentElement && document.documentElement.scrollLeft)   // Explorer 6 Strict
	    { return document.documentElement.scrollLeft; }
	else if (document.body)                                                     // all other Explorers
	    { return document.body.scrollLeft; }
}

/*
 * Initializes popup code on load.	
 */
function initPopUp() 
{
	theBody = document.getElementsByTagName('BODY')[0];
	popmask = document.createElement('div');
	popmask.id = 'popupMask';
	popcont = document.createElement('div');
	popcont.id = 'popupContainer';
	popcont.innerHTML = '' +
		'<div id="popupInner">' +
			'<div id="popupTitleBar"></div>' +
			'<div id="popupMessage"></div>' +
			'<div id="popupBody">' +
			    '<img src="../images/subModalPG.gif" id="popProgressImg" />' + 
			'</div>' +
		'</div>';
	theBody.appendChild(popmask);
	theBody.appendChild(popcont);
	// Save Element reference
	subModal_gPopupMask = document.getElementById("popupMask");
	subModal_gPopupContainer = document.getElementById("popupContainer");
	// Check hide SELECT needed
	subModal_gHideSelects = checkOldExplorer();
	// Add Hook to WebRequestManager
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(OnBeginRequest);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(OnEndRequest);
}

function endPopUp() 
{
	// Remove Hook to WebRequestManager
    Sys.WebForms.PageRequestManager.getInstance().remove_beginRequest(OnBeginRequest);
    Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(OnEndRequest);
}

function checkOldExplorer()
{
	return ((parseInt(window.navigator.appVersion.charAt(0), 10) <= 6) && (window.navigator.userAgent.indexOf("MSIE") > -1));
}

/*
showPopUp
    * @argument title - title of PopUp
*/
function showPopWin(title) 
{
    if (subModal_gLoadable) 
    {
        subModal_gPopupIsShown = true;
        disableTabIndexes();
        subModal_gPopupMask.style.display = "block";
        subModal_gPopupContainer.style.display = "block";
        // set the title
        document.getElementById("popupTitleBar").innerHTML = "<b>" + subModal_gPopupTitle + "</b>";
        // set the message
        document.getElementById("popupMessage").innerHTML = subModal_gPopupMessage;
        // set the size of the overlay
        setMaskSize();
        // calculate where to place the window on screen
        centerPopWin();
        // for IE
        if (subModal_gHideSelects == true) 
        {
            hideSelectBoxes();
        }
        subModal_gLoadable = true;
    }
}


/*
    centerPopWin
*/
function centerPopWin() 
{
	if (subModal_gPopupIsShown == true) 
	{
	    var PageTop     = parseInt(getScrollTop(),10);
	    var PageLeft    = parseInt(document.getElementsByTagName("BODY")[0].scrollLeft,10);
	    var TitleHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
	    
		setMaskSize();
		subModal_gPopupContainer.style.top = (PageTop + ((getViewportHeight() - (subModal_gPopupContainer.offsetHeight + TitleHeight)) / 2)) + "px";
		subModal_gPopupContainer.style.left =  (PageLeft + ((getViewportWidth() - subModal_gPopupContainer.offsetWidth) / 2)) + "px";
	}
}

/**
 * Sets the size of the popup mask.
 *
 */
function setMaskSize() 
{
	var theBody = document.getElementsByTagName("BODY")[0];
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) 
	    { popHeight = fullHeight; } 
	else 
	    { popHeight = theBody.scrollHeight; }
	if (fullWidth > theBody.scrollWidth) 
	    { popWidth = fullWidth; } 
	else 
	    { popWidth = theBody.scrollWidth; }
	subModal_gPopupMask.style.height = popHeight + "px";
	subModal_gPopupMask.style.width = popWidth + "px";
}

/*
 * hidePopWin
 */
function hidePopWin() 
{
	var theBody = document.getElementsByTagName("BODY")[0];

	subModal_gPopupIsShown = false;
	theBody.style.overflow = "";
	restoreTabIndexes();
	if (subModal_gPopupMask == null) 
	    { return; }
	subModal_gPopupMask.style.display = "none";
	subModal_gPopupContainer.style.display = "none";
	// display all select boxes
	if (subModal_gHideSelects == true) 
	    { displaySelectBoxes(); }
}

// Tab key trap. If 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 (subModal_gPopupIsShown && e.keyCode == 9)  return false;
}

// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexes() 
{
    if (document.all) 
	{
	    var i = 0;
		for (var j = 0; j < subModal_gTabbableTags.length; j++) 
		{
			var tagElements = document.getElementsByTagName(subModal_gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) 
			{
				subModal_gTabIndexes[i] = tagElements[k].tabIndex;
				tagElements[k].tabIndex = "-1";
				i++;
			}
		}
	}
}

// For IE. Restore tab-indexes.
function restoreTabIndexes() 
{
	if (document.all) 
	{
		for (var j = 0; j < subModal_gTabbableTags.length; j++) 
		{
			var tagElements = document.getElementsByTagName(subModal_gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				tagElements[k].tabIndex = subModal_gTabIndexes[i];
				tagElements[k].tabEnabled = true;
			}
        }
        subModal_gTabIndexes.splice(0, subModal_gTabIndexes.length);
	}
}

/*
 * 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 hideSelectBoxes() 
{
    var x = document.getElementsByTagName("SELECT");

    for (i=0; x && i < x.length; i++) 
        { x[i].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 wanting select form tags to always be the topmost z-index or layer.
 */
function displaySelectBoxes() 
{
    var x = document.getElementsByTagName("SELECT");

    for (i=0; x && i < x.length; i++)
    {
        x[i].style.visibility = "visible";
    }
}

// Page Event handler
function OnBeginRequest(sender, args) 
{
    setTimeout(showPopWin, subModal_gDelayTime);
}

function OnEndRequest (sender, args)
{
    if (subModal_gPopupIsShown) {
        hidePopWin();
        subModal_gLoadable = true;
    }
    else 
    {
        subModal_gLoadable = false;
    }
}

// If using Mozilla or Firefox, use Tab-key trap.
if (!document.all) 
    { document.onkeypress = keyDownHandler; }
addEvent(window, "load", initPopUp);
addEvent(window, "unload", endPopUp);
addEvent(window, "resize", centerPopWin);
addEvent(window, "scroll", centerPopWin);

