// JScript File

// Summary: Opens a new window. Can be centered, positioned, absolutely sized and 
//          sized to a % of screen. Also sets focus to the new window.
// Params
// url:     URL to open.
// name:    Name of window.
// width:   Width of window. If <= 1, the width will be that % of the screen. 
//          If the width is greater than the width of the screen, the width is reduced to 90% of the screen.
// height:  Height of window. If <= 1, the height will be that % of the screen.
//          If the height is greater than the height of the screen, the height is reduced to 90% of the screen.
// top:     Top of window. If -1, window will be centered vertically.
// left:    Left location of window. If -1, window will be centered horizontally. 
// options: Window options: location=yes,resizable=yes,scrollbars=yes,menubar=yes,toolbar=yes,status=yes
//
// Returns: The new window object.
function OpenWindow(url, name, width, height, top, left, options) 
{
    var winOptions = "";
    var screenWidth;
    var screenHeight;
    var screenAvailWidth;
    var screenAvailHeight;

    if (window.screen)
    {
        screenWidth       = screen.width;
        screenHeight      = screen.height;
        screenAvailWidth  = screen.availWidth;
        screenAvailHeight = screen.availHeight;
    }
    else //if we cannot determine the screen size, assume 800X600. 
    {
        screenWidth       = 800;
        screenHeight      = 600;
        screenAvailWidth  = 800;
        screenAvailHeight = 600;
        winOptions = "resizable=yes,";
    }
    
    if (width > screenWidth)
    {
        width = .9;
    }

    if (height > screenHeight)
    {
        height = .9;
    }

    if (width <= 1) 
    { 
        width = Math.floor(screenWidth * width); 
    }
    
    if (height <= 1) 
    { 
        height = Math.floor(screenHeight * height); 
    }

    if (top == -1)
    {
        top  = Math.floor( (screenAvailHeight - height) / 2);        
    }

    if (left == -1)
    {
        left = Math.floor( (screenAvailWidth - width) / 2);
    }
        
    winOptions += "top=" + top + ",left=" + left + ",width=" + width + ",height=" + height;

    if (options) 
    { 
        winOptions += "," + options; 
    }

    var win = window.open(url, name, winOptions);
    
    if (win) 
    { 
        try
        {
            win.window.focus(); 
        }
        catch(err)
        {
            // do nothing. an error could happen when trying to focus to a window  
            // on a different domain.
        }

    }
    
    return win;
}


String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
