// Finest Online Popup Window Code
// AKB - 15/08/2008 - Created
// AKB - 11/11/2008 - Updated to support only one window.

/***  Config Section  ***/
var popupHeight = 768; // window.screen.height * 0.85;
var popupWidth = 1024; // window.screen.width * 0.85;
/***  Config Section - END  ***/

var windowHandle = null;
var windowName = 'FolNamedWindow1';

// Get the common property string we want to use for all popups
function getPupupProperties() {

	// Build the property string
	return 'height=' + popupHeight + ', width='+ popupWidth +', scrollbars=yes, toolbars=no, resizable=yes, menubar=no';

}

//
// Main entry point
//
// Try to open a FOL window as best we can trying to make sure
// that we don't overwrite an already open window and that
// we prompt the user if they already have a window open.
function openFOLWindow(url)
{

	// Check if any windows are open
	if (isWindowOpen(windowHandle)) {

		// Make sure they want to open the window
		if (!confirmNewApp())
			return false;

		// Open the window
		windowHandle = window.open(url,windowName, getPupupProperties());

		// Finished
		return false;
	}

	// Try to open the window
	res = carefullyOpenWindow(url);

	// Don't let the normal hyper-link continue
	return false;

}

// Try to open this popup window but check first to make
// sure it isn't already open but we have lost the 
// handle to it.
function carefullyOpenWindow(url) {
	if (isWindowOpenPrevious(windowName)) {
		if (!confirmNewApp()) return false;
	}

	// Open the window
	windowHandle = window.open(url,windowName, getPupupProperties());

	// Return true
	return true;
}

// Prompt the user to confirm what they would like to do since the window is already open
function confirmNewApp() {
	return confirm("You already have an Insurance application in progress.  If you select to continue, you will lose the information you have entered so far.\n\nWould you still like to start a new application?");
}


// Check if any of the FOL windows are open already
function isAnyWindowOpen() {
	
	// Loop through and check if any of the windows are open
	for (i = 0; i < numWindows; i++) {
		if (isWindowOpen(windowHandles[i])) return true;
	}

	// No windows open
	return false;
}

// Check if the window on this handle is open
function isWindowOpen(handle) {

	// Check if we have a handle to the window
	if (handle == null) {
		return false;
	}

	// Check if the window is closed
	return !handle.closed;

}

// Check if a window has been previously opened with the given name
// The catch with this function is that if the window hasn't
// already been opened it will be after calling this function.  Don't
// call this function unless you plan to use the window straight away!
function isWindowOpenPrevious(name) {

	// Get the handle for the window
	handle = window.open('',name, getPupupProperties());

	// Try to check the title
	try {
		
		if (handle.document.title == '') {
			return false;
		} else {
			return true;
		}

	} catch(err) {
		return true;
	}

}
