﻿var DEFAULT_POPUP_NAME = 'popup';
var DEFAULT_POPUP_WIDTH = 760;
var DEFAULT_POPUP_HEIGHT = 800;

var width;
var height;

function popup(url)
{
	return popup(url, null, null);
}

function popup(url, width, height)
{
	popup(url, width, height, null);
}

function popup(url, width, height, toolbar)
{
	popup(url, width, height, toolbar, null);
}
function popup(url, width, height, toolbar, resizable)
{
	popup(url, width, height, toolbar, resizable, null);
}
function popup(url, width, height, toolbar, resizable, scrollbar)
{
	popup(url, width, height, toolbar, resizable, scrollbar, null);
}

function popup(url, widht, height, toolbar, resizable, scrollbar, name)
{
	popup(url, width, height, toolbar, resizable, scrollbar, name, null);
}

function popup(url, width, height, toolbar, resizable, scrollbar, name, failure)
{
    resizable = 'yes';

	var browserSize = {width : 1024, height : 768};
	var screenSize = {width : 1024, height : 768};

	var xPopup=600;
	var yPopup=800;
	var toolbarVar=1;
	var resizableVar=1;
	var scrollbarVar=1;
	var nameVar = DEFAULT_POPUP_NAME;

	// Get screen resolution and browser size.
	try {    
		screenSize = Util.getScreenResolution();
		browserSize = Util.getBrowserSize();
    }
    
    catch (e) {
    	// Util probably doesn't exist.
    }
	
	// If width, height are less than 0, then set the width, height to the browser size.
	if (width)
		xPopup = (width < 0) ? browserSize.width : width;
	else
		xPopup = DEFAULT_POPUP_WIDTH;
	
	if (height)	
		yPopup = (height < 0) ? browserSize.height : height;
	else
		yPopup = DEFAULT_POPUP_HEIGHT;

	//if(width != null && width <= xMax) xPopup = width;
	//if(height != null && height <= yMax) yPopup = height;
	if(toolbar != null) toolbarVar = toolbar;
	if(resizable != null) resizableVar = resizable;
	if(scrollbar != null) scrollbarVar = scrollbar;
	if(name != null) nameVar = name;

	var xOffset = (screenSize.width - xPopup) / 2;
	var yOffset = (screenSize.height - yPopup) / 2;
	
	xOffset = (xOffset >= 0) ? xOffset : 0;
	yOffset = (yOffset >= 0) ? yOffset : 0;

	var popup = null;
	var windowFeatures = 'width=' + xPopup + ',height=' + yPopup + ',toolbar=' + toolbarVar + ',scrollBars=' + scrollbarVar + ',status=no,resizable=' + resizableVar + ', screenX=' + xOffset + ',screenY=' + yOffset + ',top=' + yOffset + ',left=' + xOffset;
	popup = window.open(url, nameVar, windowFeatures);

	if (popup) {
	    if (!popup.opener)
	         popup.opener = self;
	    if (popup.focus) popup.focus();
    }
    
    else {
    	if (failure) {
	    	failure.call(this, url);
    	}
    }        
}

function popupCover(url, width, height, name, failure)
{
	popupWidth = (width) ? width : 760;
	popupHeight = (height) ? height : null;
	popupName = (name) ? name : null;
	failureHandler = (failure) ? failure : null;
		
	popup(url, popupWidth, popupHeight, 0, 0, null, popupName, failureHandler);
}



function openWindow(url)
{
	openWindow(url, null, null);
}
function openWindow(url, width, height)
{
	openWindow(url, width, height, null);
}
function openWindow(url, width, height, toolbar)
{
	openWindow(url, width, height, toolbar, null);
}
function openWindow(url, width, height, toolbar, resizable)
{
	openWindow(url, width, height, toolbar, resizable, null);
}
function openWindow(url, width, height, toolbar, resizable, scrollbar)
{
    resizable = 'yes';
    var xMax = 1024, yMax=768;
	if (document.all || document.layers)
	{
        var xMax = screen.availWidth, yMax = screen.availHeight;
    }

	var xPopup=600;
	var yPopup=800;
	var toolbarVar=1;
	var resizableVar=1;


	if(width != null && width <= xMax) xPopup = width;
	if(height != null && height <= yMax) yPopup = height;
	if(toolbar != null) toolbarVar = toolbar;
	if(resizable != null) resizableVar = resizable;

	var xOffset = (xMax - xPopup) / 2, yOffset = (yMax - yPopup) / 2;

	var win;
	win = window.open(url,'window','width='+xPopup+',height='+yPopup+',toolbar='+toolbarVar+',location,directories,status,scrollbars,menubar,resizable='+resizableVar+',screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+'')
	if (!win.opener)
         win.opener = self;
    win.focus();
}


