/////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2004-2005 SumTotal Systems, Inc. All rights reserved.
//
// The copyright to the computer software herein is proprietary and remains
// the property of SumTotal Systems, Inc. It may be used and/or copied only with
// the written consent of SumTotal Systems, Inc. or in accordance with the terms and
// conditions stipulated in the agreement/contract under which this software
// has been supplied.
//
// $Id: SYS_ModalNS.js,v 1.14 2006/05/18 23:47:45 dpinipolu Exp $
// $Date: 2006/05/18 23:47:45 $
// $RCSfile: SYS_ModalNS.js,v $
// $Revision: 1.14 $
//
// Abstract:
//
//    Browser class to identify browsers and operating system
//
// Revision History:
//
//    2002-02-25   Sameer Tandon    Created file
//
/////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Global for brower version branching.

// One object tracks the current modal dialog opened from this window.
var g_DialogWin = new Object()
var g_blnModal  = null;
// Generate a modal dialog.
// Parameters:
//    url -- URL of the page/frameset to be loaded into dialog
//    width -- pixel width of the dialog window
//    height -- pixel height of the dialog window
//    returnFunc -- reference to the function (on this page)
//                  that is to act on the data returned from the dialog
//    args -- [optional] any data you need to pass to the dialog
function _UTL_OpenModalWindowInNS(url, width, height, returnFunc, name, args) {
	if ((g_DialogWin.win && !g_DialogWin.win.closed))	{
		g_DialogWin.win.close();
	}
    if (!g_DialogWin.win || (g_DialogWin.win && g_DialogWin.win.closed)) {
        // Initialize properties of the modal dialog object.
        g_DialogWin.returnFunc = returnFunc;
        g_DialogWin.returnedValue = "";
        g_DialogWin.name = name;
        g_DialogWin.url = url;
        g_DialogWin.width = width;
        g_DialogWin.height = height;
		g_DialogWin.args = args;
        // Keep name unique so Navigator doesn't overwrite an existing dialog.
        g_DialogWin.name = (new Date()).getSeconds().toString()
        // Assemble window attributes and try to center the dialog.
        if (g_objBrowser.IsNetscape() && g_objBrowser.Version() < 500) 
         {
            // Center on the main window.
            g_DialogWin.left = window.screenX + 
               ((window.outerWidth - g_DialogWin.width) / 2)
            g_DialogWin.top = window.screenY + 
               ((window.outerHeight - g_DialogWin.height) / 2)
            var attr = "scrollbars=yes,screenX=" + g_DialogWin.left + 
               ",screenY=" + g_DialogWin.top + ",resizable=no,modal=yes,dialog=no,width=" + 
               g_DialogWin.width + ",height=" + g_DialogWin.height
        } else {
            // The best we can do is center in screen.
            g_DialogWin.left = (screen.width - g_DialogWin.width) / 2
            g_DialogWin.top = (screen.height - g_DialogWin.height) / 2
            var attr = "scrollbars=yes,left=" + g_DialogWin.left + ",top=" + 
               g_DialogWin.top + ",resizable=no,modal=yes,dialog=no,width=" + g_DialogWin.width + 
               ",height=" + g_DialogWin.height
        }
        // Generate the dialog and make sure it has focus.
        // In Shopping cart, Modal Dialog is called after 'this' window is closed.
        // Hence need to open the new window from parent instead of 'this'.
        if(window)
            g_DialogWin.win=window.open(g_DialogWin.url, g_DialogWin.name, attr, args)
        else if(parent)
            g_DialogWin.win=parent.open(g_DialogWin.url, g_DialogWin.name, attr, args)
            
        // Save a reference to parent. Similar to top.dialogArguments for IE. 
        if(!g_DialogWin.win.opener) 
            g_DialogWin.win.opener = self ;

        if(g_DialogWin.win)
        {
            g_blnModal = setTimeout('_UTL_CheckModal();',1000)
        }
        g_DialogWin.win.focus();
    } else {
        g_DialogWin.win.focus();
    }
}


function _UTL_CheckModal()
{
    clearTimeout(g_blnModal);
    if (!g_DialogWin.win || (g_DialogWin.win && g_DialogWin.win.closed))
    {
        _UTL_unblockEvents();
    }
    else
    {
        _UTL_blockEvents();
        g_DialogWin.win.focus();
    }   
}
// Event handler to inhibit Navigator form element 
// and IE link activity when dialog window is active.
function _UTL_deadend(e) {
    
    // disables the click and right click on the parent window when modal window is open.
    // 1: Left button Click; 2: Center button Click;
    
    if (g_DialogWin.win && !g_DialogWin.win.closed ) 
        return false;
        
}

// Since links in IE4 cannot be disabled, preserve 
// IE link onclick event handlers while they're "disabled."
// Restore when re-enabling the main window.
var IELinkClicks

// Disable form elements and links in all frames for IE.
function disableForms() {
    IELinkClicks = new Array()
    for (var h = 0; h < frames.length; h++) {
        for (var i = 0; i < frames[h].document.forms.length; i++) {
            for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
                frames[h].document.forms[i].elements[j].disabled = true
            }
        }
        IELinkClicks[h] = new Array()
        for (i = 0; i < frames[h].document.links.length; i++) {
            IELinkClicks[h][i] = frames[h].document.links[i].onclick
            frames[h].document.links[i].onclick = _UTL_deadend();
        }
    }
}

// Restore IE form elements and links to normal behavior.
function _UTL_enableForms() {
    for (var h = 0; h < frames.length; h++) {
        for (var i = 0; i < frames[h].document.forms.length; i++) {
            for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
                frames[h].document.forms[i].elements[j].disabled = false
            }
        }
        for (i = 0; i < frames[h].document.links.length; i++) {
            frames[h].document.links[i].onclick = IELinkClicks[h][i]
        }
    }
}

// Grab all Navigator events that might get through to form
// elements while dialog is open. For IE, disable form elements.
function _UTL_blockEvents() {    
    if (g_objBrowser.IsNetscape() && g_objBrowser.Version() < 500) {
        //alert("events blocked # 1");
        window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
        window.onmousedown=_UTL_deadend;
    } 
    else {
        disableForms()
    }
    window.onfocus = checkModal
}

// As dialog closes, restore the main window's original
// event mechanisms.
function _UTL_unblockEvents() {
    if (g_objBrowser.IsNetscape() && g_objBrowser.Version() < 500) {
        window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
        window.onclick = null
        window.onfocus = null
    } 
    else {
        _UTL_enableForms()
    }
}

// Invoked by onFocus event handler of EVERY frame,
// return focus to dialog window if it's open.
function checkModal() {
    if (g_DialogWin.win && !g_DialogWin.win.closed) {
        g_DialogWin.win.focus()    
    }
    else
        _UTL_unblockEvents;
}

window.onunload = function()
{
    if (g_DialogWin.win && !g_DialogWin.win.closed) {
        g_DialogWin.win.close();
    }

}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

