var _windowCounter = 0;
var _lastPopup;

function OpenWindow(url, options, width, height)
{
    options = MergeWindowOptions(options, width, height);
    
    var name = GetWindowName();

    _lastPopup = window.open('', name, options);
    _lastPopup.location.href = ResolveUrl(url);
	_lastPopup.focus();
}

function OpenNamedWindow(url, name, options, width, height)
{
    options = MergeWindowOptions(options, width, height);
    
    _lastPopup = window.open('', name, options);
	_lastPopup.location.href = ResolveUrl(url);
	_lastPopup.focus();
}

function CloseWindow(name)
{
    var win;
    
    if(name == null)
    {
        win = self;
    }
    else
    {
        win = window.open('', name, 'width=0,height=0,top=10000,left=10000');
    }
    
    if(win != null)
    {
        win.opener = self;
        win.close();
    }
}

function GetWindow(name)
{
    return window.open('', name);
}

function MergeWindowOptions(options, width, height)
{
    if(width == 0)
    {
        width = screen.availWidth;
    }
    
    if(height == 0)
    {
        height = screen.availHeight;
    }
    
    var top = (screen.availHeight - height) / 2;
    var left = (screen.availWidth - width) / 2;
    
    var searchOptions = new Array("top=", "left=", "width=", "height=", "scrollbars");
    var defaultOptions = new Array("top=" + top, "left=" + left, "width=" + width, "height=" + height, "scrollbars=yes");
    
    if(options == null)
    {
        options = "";
    }
    
    var optionArray = options.split(",");
    var inArray;
    
    for(var i = 0; i < searchOptions.length; i++)
    {
        inArray = false;
        
        for(var j = 0; j < optionArray.length; j++)
        {
            if(optionArray[j].substring(0, searchOptions[i].length) == searchOptions[i])
            {
                inArray = true;
                break;
            }
        }
        
        if(!inArray)
        {
            optionArray.push(defaultOptions[i]);
        }
    }    

    return optionArray.join(",");    
}

function GetWindowName()
{
    var name = "ChildWindow";
    
    _windowCounter++;
    name += _windowCounter;
    
    return name;
}

function CloseAllChildWindows()
{
    var win;
    
    for(var i = 0; i < 10; i++)
    {
        win = window.open('', "ChildWindow" + i);
        
        if(win != null)
        {
            win.opener = self;
            win.close();
        }    
    }
}

function SetWindowStatus(win, statusMessage)
{
    win.status = statusMessage;
    return true;
}

function SetDefaultWindowStatus(win, statusMessage)
{
    win.defaultStatus = statusMessage;
    return true;
}

if(typeof(_personName) != "undefined")
{
    //document.title += " - " + _personName;
}

