function wizPopup(mylink, windowname, width, height) {

	var x = (800 - width)/2;
	var y = (600 - height)/2;
	if (screen) {
		y = (screen.availHeight - height)/2;
		x = (screen.availWidth - width)/2;
	}
	window.top.frames["dataFrame"].wizWindow = window.open(mylink, windowname, 'scrollbars=no,resizable=no,width=' + width + ',height=' + height + ',top=' + y + ',left=' + x + ',screenX=' + x + ',screenY=' + y);
	if (!window.top.frames["dataFrame"].wizWindow) reportPopupFail();
	return;
}

function setWizardPage(enableFlag)	{ popupWindow.wizardPage = enableFlag; }
function getWindowHandle()			{ return popupWindow.windowHandle; }
function setWindowHandle(handle) {
	if (handle) {
		new popupWindow();
		popupWindow.windowHandle = handle;
		popupWindow.windowHandle.focus();
	}
	else reportPopupFail();
}
function tabbedPopupCheck(which, width) {
	// For the newer browsers that support tabs (IE-7 / Netscape-7 and later)
	// this checks to see if the pop-up appears to have opened in a tab rather
	// than in a separate pop-up window, and warns the user if so, along with
	// a bit of help on how to fix it.
	//
	// which = 1: use getWindowHandle() for a pop-up
	// which = 2: use getWindowHandle2() for a pop-up from pop-up
	// width: expected width of pop-up window.

	if (document.cookie.indexOf("tabbedWinAlert=done") != -1) return;

	var win = (which == 2) ? getWindowHandle2() : getWindowHandle();
	if (!win.document || !win.document.body) {
		// Need to call repeatedly until the pop-up window is loaded
		setTimeout("tabbedPopupCheck(" + which + ", " + width + ");", 500);
		return;
	}

	var isIE7 = false, isNS7 = false, isNS8 = false;
	if (document.getElementById) {
		var bwVer = navigator.appVersion;
		var bwAgent = navigator.userAgent
		var ix = bwVer.indexOf("MSIE ");
		if (ix > -1) {
			// Internet Explorer 5+
			isIE7 = (parseInt(bwVer.substr(ix+5)) >= 7);
		}
		else if (parseInt(bwVer) >= 5) {
			if (bwAgent.indexOf("Netscape") > -1) {
				// Netscape 6+
				var ix = bwAgent.indexOf("Netscape");
				var bwRev = bwAgent.substr(bwAgent.indexOf("/",ix) + 1);
				var ix = bwRev.indexOf(" ");
				if (ix > 0) bwRev = bwRev.substr(0,ix);
				isNS7 = (parseInt(bwRev) >= 7);
				isNS8 = (parseInt(bwRev) >= 8);
			} else {
				// Mozilla
				var endIx = bwAgent.indexOf(") Gecko");
				var begIx = bwAgent.lastIndexOf(" ", endIx) + 1;
				if (bwAgent.substr(begIx,3) == "rv:") begIx += 3;
				var bwRev = bwAgent.substring(begIx,endIx);
				isNS7 = (parseInt(bwRev) >= 1);
			}
		}
	}

	var winWidth;
	var fixStr;
	if (isIE7) {
		winWidth = win.document.body.offsetWidth;
		fixStr = "In the Tabs settings in Internet Explorer's Internet Options,"
				+ " select the option to open pop-ups in a new window.";
	} else if (isNS7) {
		winWidth = win.innerWidth;
		fixStr = "In Netscape's Site Controls for the SonicWALL (" + location.hostname + ")"
				+ " uncheck the option to open pop-ups in a new tab.";
		if (isNS8) {
			fixStr += "\n\nYou should also enable all Advanced JavaScript"
					+ " options in the SonicWALL's Site Controls too.";
		}
	} else {
		// An older browser without tabs
		return;
	}
	if (winWidth > (Number(width)+50)) {
		alert("The pop-up window appears to have probably been opened in a browser tab.\n\n"
			+ "To ensure proper functionality you must set your browser to open this as a pop-up window:\n\n"
			+ fixStr);
		// Set a cookie to prevent this warning being shown repeatedly
		var when = new Date();
		var now = when.getTime();
		when.setTime(now + 7*24*360000);
		var year = when.getFullYear();
		when.setFullYear(year+1);
		document.cookie = "tabbedWinAlert=done";
	}
}
function closeActivePopup() {
	if (popupWindow.wizardPage) {
		return;
	}
	if (popupWindow.windowHandle && !popupWindow.windowHandle.closed) {
		popupWindow.windowHandle.close();
		popupWindow.windowHandle = 0;
	}
}
function popupWindow() {}
popupWindow.closeWindow		= false;
popupWindow.wizardPage		= false;
popupWindow.windowHandle	= 0;

// popup from a propertysheet, itself a popup
function closeActivePopup2() {
	if (popupFromPopupWindow.windowHandle && !popupFromPopupWindow.windowHandle.closed) {
		popupFromPopupWindow.windowHandle.close();
		popupFromPopupWindow.windowHandle = 0;
	}
}

function popupFromPopupWindow() {}
popupFromPopupWindow.closeWindow	= false;
popupFromPopupWindow.windowHandle	= 0;

function getWindowHandle2() { return popupFromPopupWindow.windowHandle; }

function setWindowHandle2(handle) {
	if (handle) {
		new popupFromPopupWindow();
		popupFromPopupWindow.windowHandle = handle;
		popupFromPopupWindow.windowHandle.focus();
	}
	else reportPopupFail();
}

function reportPopupFail() {
	alert("Failed to open a pop-up window that is required by the SonicWALL GUI. "
		+ "Please ensure that pop-ups are not blocked for '" + location.hostname + "'");
}

//////////////////////////////////////////////////////

function popup(mylink, windowname, width, height) {
	if (!window.focus) {
		return true;
	}
	closeActivePopup();
	var href;
	if (typeof(mylink) == 'string') {
		href = mylink;
	} else {
		href = mylink.href;
	}
	var x = (800 - width)/2;
	var y = (600 - height)/2;
	if (screen) {
		y = (screen.availHeight - height)/2;
		x = (screen.availWidth - width)/2;
	}
	if (windowname == 'helpWindow') {
		setWindowHandle(window.open(href, windowname, 'scrollbars=yes,resizable=yes,width=' + width + ',height=' + height + ',top=' + y + ',left=' + x + ',screenX=' + x + ',screenY=' + y));
	}
	else if (windowname == 'dialogScroll') {
		setWindowHandle(window.open(href, windowname, 'scrollbars=yes,resizable=yes,width=' + width + ',height=' + height + ',top=' + y + ',left=' + x + ',screenX=' + x + ',screenY=' + y));
	}
	else if (windowname == 'dialogResizable') {
		setWindowHandle(window.open(href, windowname, 'scrollbars=no,resizable=yes,width=' + width + ',height=' + height + ',top=' + y + ',left=' + x + ',screenX=' + x + ',screenY=' + y));
	}
	else {
		setWindowHandle(window.open(href, windowname, 'scrollbars=no,resizable=no,width=' + width + ',height=' + height + ',top=' + y + ',left=' + x + ',screenX=' + x + ',screenY=' + y));
	}
	tabbedPopupCheck(1, width);
	return false;
}

var helpWindow;
function showHelp(helpContext)
{
	var path;

	path = 'help_' + helpContext + '.html';
	helpWindow = window.open(path, 'helpWindow', 'scrollbars=yes,resizable=yes,width=600,height=500');
	if (helpWindow) helpWindow.focus();
	else reportPopupFail();
}

var newHelpWindow;
function showHelp2(helpContext)
{
	path = 'help_' + helpContext + '.html';
	newHelpWindow = window.open(path, 'newHelpWindow');
	if (newHelpWindow) newHelpWindow.focus();
	else reportPopupFail();
}

function popupFromPopup(mylink, windowname, width, height) {
	closeActivePopup2();
	var href;
	if (typeof(mylink) == 'string') {
		href = mylink;
	} else {
		href = mylink.href;
	}
	var x = (800 - width)/2;
	var y = (600 - height)/2;
	if (screen) {
		y = (screen.availHeight - height)/2;
		x = (screen.availWidth - width)/2;
	}
	if (windowname == 'helpWindow') {
		setWindowHandle2(window.open(href, windowname, 'scrollbars=no,resizable=yes,width=' + width + ',height=' + height + ',top=' + y + ',left=' + x + ',screenX=' + x + ',screenY=' + y));
	}
	else if (windowname == 'dialogScroll') {
		setWindowHandle2(window.open(href, windowname, 'scrollbars=yes,resizable=yes,width=' + width + ',height=' + height + ',top=' + y + ',left=' + x + ',screenX=' + x + ',screenY=' + y));
	}
	else {
		setWindowHandle2(window.open(href, windowname, 'scrollbars=no,resizable=no,width=' + width + ',height=' + height + ',top=' + y + ',left=' + x + ',screenX=' + x + ',screenY=' + y));
	}
	tabbedPopupCheck(2, width);
	return false;
}

function targetopener(mylink, closeme, closeonly, form) {
	if (!(window.focus && top.opener)) {
		return true;
	}
	top.opener.focus();
	if (!closeonly) {
		top.opener.location.href = mylink.href;
	}
	if (closeme) {
		window.close();
	}
	if (form) {
		form.submit();
		return true;
	}
	return false;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//						Check for errors
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var waitForError = 0;
function checkForError() {
	if (waitForError == 0) {
		waitForError = setInterval("checkForUpdateError()", 250);
	}
}
function checkForUpdateError() {
	if (!top.opener || top.opener.closed
	 || !top.opener.top.frames["statusFrame_0006B1219E84"]
	 || !top.opener.top.frames["statusFrame_0006B1219E84"].document) {
		clearInterval(waitForError);
		waitForError = 0;
		if (canClose) {
			top.close();
		}
		return;
	}
	if (top.opener.top.frames["statusFrame_0006B1219E84"].document.statusBarForm) {
		clearInterval(waitForError);
		waitForError = 0;
		if (top.opener.top.frames["statusFrame_0006B1219E84"].errorExists == 1) {
			setErrorMessage(top.opener.top.frames["statusFrame_0006B1219E84"].strErrorMessage);
		} else {
			if (popupFromPopupWindow.windowHandle) {
				popupFromPopupWindow.windowHandle.close();
				popupFromPopupWindow.windowHandle = 0;
			}
			checkRefreshNeeded();
			if (canClose) {
				top.close();
			}
			setErrorMessage("Ready");
		}
	}
}
function setErrorMessage(strError) {
	if (document.getElementById('errorStatus')) {
		if (strError == "Ready") {
			document.getElementById('errorStatus').style.color = "black";
		} else {
			document.getElementById('errorStatus').style.color = "red";
		}
		document.getElementById('errorStatus').innerHTML = " " + strError;
	}
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//					Check for StatusBar to load 'Please Wait...' form
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var waitForLoad=0;
function doSubmit(str) {
	doSubmit2(str, document.thisForm);
}
function doSubmit2(str, form) {
	if (form.cgiaction) {
		form.cgiaction.value = str;
	}
	top.opener.top.frames["statusFrame_0006B1219E84"].location.href="/wait2.html";
	setErrorMessage("Please Wait...");
	if (waitForLoad == 0) {
		waitForLoad = setInterval("checkForWaitLoad()", 1000);
	}
}
function checkForWaitLoad() {
	if (top.opener.top.frames["statusFrame_0006B1219E84"].location.pathname == "/wait2.html") {
		if (top.opener.top.frames["statusFrame_0006B1219E84"].document.statusWaitForm) {
			clearInterval(waitForLoad);
			waitForLoad = 0;
			submitForm();
		}
	}
}

/* If popup itself contains frames */
var waitForLoadFrames=0;
function doSubmitFrames(str) {
	document.thisForm.cgiaction.value = str;
	top.opener.top.frames["statusFrame_0006B1219E84"].location.href="/wait2.html";
	setErrorMessage("Please Wait...");
	if (waitForLoadFrames == 0) {
		waitForLoadFrames = setInterval("checkForWaitLoadFrames()", 1000);
	}
}
function checkForWaitLoadFrames() {
	if (top.opener.top.frames["statusFrame_0006B1219E84"].location.pathname == "/wait2.html") {
		if (top.opener.top.frames["statusFrame_0006B1219E84"].document.statusWaitForm) {
			clearInterval(waitForLoadFrames);
			waitForLoadFrames = 0;
			submitFormFrames();
		}
	}
}

