// Declare a single namespace in the global scope
var dk;
if (!dk) dk = {};
else if (typeof(dk) != 'object')
	throw new Error('Der findes allerede en global variabel ved navn \u0027dk\u0027, som ikke er et object');
if (!dk.qbrix) dk.qbrix = {};
else if (typeof(dk.qbrix) != 'object')
	throw new Error('Der findes allerede en variabel ved navn \u0027dk.qbrix\u0027, som ikke er et object');

/************************************************************
* Relies on:
*    General.js
*
************************************************************/

dk.qbrix.MyPopup = 
{
    Popup : null,   // A window object
    FormName : '',
    Size : [],
    FurtherArgs : [],
    Readonly : false,
    Offset : [150, 150],
    
    Set : function(form_name, width, height, /*optional array*/further_arg_names)
    {
        if (this.Popup != null)
            this.Popup.close();
        this.Popup = null;
        
        this.FormName = form_name;
        this.Size = [width, height];
        if (arguments.length < 4) further_arg_names = [];
        this.FurtherArgs = further_arg_names;

        if (top.attachEvent)
            this.Offset = [150,150];
        else
            this.Offset = [300,150];
        
    },
    
    Open : function(/*optional integer*/item_id, /*optional array*/further_arg_values)
    {
        // Close eventual dialogs before opening a new one
        if (this.Popup != null)
            this.Popup.close();
        this.Popup = null;

        if (arguments.length < 2 || further_arg_values == null) further_arg_values = [];
        if (arguments.length < 1 || item_id == null) item_id = -1;
        
        var s = this.FormName + '?id=' + item_id.toString() + '&readonly=' + (this.ReadOnly ? '1' : '0');
        if (this.FurtherArgs.length == further_arg_values.length)
        {
            for (var n=0; n < this.FurtherArgs.length; n++)
                s += '&' + this.FurtherArgs[n] + '=' + further_arg_values[n].toString();
        }

        this.Popup = window.open(s, 
				              this.FormName.replace(/[^a-z]/g, '') + (new Date()).getTime().toString(), // Necessary to avoid same name when opening subarrangements
				              'width=' + this.Size[0].toString() + 'px,' +
				              'height=' + this.Size[1].toString() + 'px,' +
				              'top=' + (dk.qbrix.Window.GetPosition()[1] + this.Offset[0]).toString() + ',' +
				              'left=' + (dk.qbrix.Window.GetPosition()[0] + this.Offset[1]).toString() + ',' +
				              'scroll=0,' + 
				              'status=0');
       
        // Attach close event to current window (not to the popupwindow)
        s = function()
        {
            if (dk.qbrix.MyPopup.Popup != null)
                dk.qbrix.MyPopup.Popup.close();  
        }
        
        if (top.attachEvent)
        {
            top.detachEvent('onunload', s);
            top.attachEvent('onunload', s);
        }
        else
        {
            top.removeEventListener('unload', s, false);
            top.addEventListener('unload', s, false);
        }
    },
    
    Close : function(/*optional object*/refresh_frame, /*optional function*/close_function)
    {
        var opener = null;
        var s;
        
        if (arguments.length < 1 || typeof(refresh_frame) != 'object') refresh_frame = null;
    
        // Execute eventual close-function
        if (close_function != null) close_function();
        
        // Close window
        if (this.Popup != null)
            this.Popup.close();
        this.Popup = null;
        
        // Refresh parent window
	    if (refresh_frame != null)
	    {
	        if (typeof(refresh_frame.__doPostBack) == 'function')
	            refresh_frame.__doPostBack('','');
	        else
	            refresh_frame.location.replace(refresh_frame.location.toString().replace('#', ''));
	    }
    }
}

