var lastMouseX;
var lastMouseY;
var curPopupWindow = null;
var embeddedPopupWindow = null;
var simplePopupWindow = null;

function openSimplePopup(url, name)
{
	simplePopupWindow = window.open(url,name);
	simplePopupWindow.focus();
}

function setLastMousePosition(e) {
    if (navigator.appName.indexOf("Microsoft") != -1) e = window.event;
    lastMouseX = e.screenX;
    lastMouseY = e.screenY;
}

/**
 * Handles popup windows.
 * If snapToLastMousePosition is true, then the popup will open up near the mouse click.
 * If closeOnFocus is true, then it will close when the user clicks back into the browser window that opened it.
 */
function openPopupFocus(url, name, pWidth, pHeight, features, snapToLastMousePosition, closeOnFocus) {
    closePopup();
    
    if (snapToLastMousePosition) {
        if (lastMouseX - pWidth < 0) {
            lastMouseX = pWidth;
        }
        if (lastMouseY + pHeight > screen.height) {
            lastMouseY -= (lastMouseY + pHeight + 50) - screen.height;
        }
        //lastMouseX -= pWidth;
        lastMouseY += 10;
        lastMouseX += 5;

        features += "screenX=" + lastMouseX + ",left=" + lastMouseX + "screenY=" + lastMouseY + ",top=" + lastMouseY;
    }

	
    if (closeOnFocus) {
        curPopupWindow = window.open(url, name, features, false);
        curPopupWindow.focus();
    } else { 
        // assign the open window to a dummy var so when closePopup() is called it won't be assigned to curPopupWindow
        win = window.open(url, name, features, false);
        win.focus();
    }
}

function openEmbeddedPopup(url, name)
{
	/*var oldPopup = embeddedPopupWindow;
	
	embeddedPopupWindow = window.open(url,name);
	embeddedPopupWindow.focus();

	// Close existing
	if (oldPopup != null)
	{
		oldPopup.focus();
		IECommand(45);	
	}	*/
	
	embeddedPopupWindow = window.open(url,name);
	embeddedPopupWindow.focus();
	
	//alert("Only one window at a time, please close current window and click 'Export' again!");

}

function IECommand(olecmdid)
{
	var OLECMDID = olecmdid;
	/* OLECMDID values:
	* 1 - open window
	* 4 - save as
	* 7 - print preview
	* 6 - print
	* 45 - close
	* For more commands see:
	* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/oen_a2z_22sk.asp
	*/
	var PROMPT = 2; // 2 DONTPROMPTUSER 
	var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
	document.body.insertAdjacentHTML('beforeEnd', WebBrowser); 
	WebBrowser1.ExecWB(OLECMDID, PROMPT);
	WebBrowser1.outerHTML = "";
}

function closePopup() {
    if (curPopupWindow != null) {
        
        if (!curPopupWindow.closed) {
            curPopupWindow.close();
        }
        curPopupWindow = null;
    }
}
