var lastMouseX;
var lastMouseY;
var curPopupWindow = null;

function setLastMousePosition(e) {
	if (navigator.appName.indexOf("Microsoft") != -1) e = window.event;
	lastMouseX = e.screenX;
	lastMouseY = e.screenY;
}

function openPopup(url, name, pWidth, pHeight, features, snapToLastMousePosition) {
	openPopupFocus(url, name, pWidth, pHeight, features, snapToLastMousePosition, true);
}

function openPopupFocus(url, name, pWidth, pHeight, features, snapToLastMousePosition, closeOnLoseFocus) {
	closePopup();

	features += ",scrollbars,resizable,dependent,width=" + pWidth + ",height=" + pHeight;
	if (snapToLastMousePosition) {
		if (lastMouseX - pWidth < 0) {
			lastMouseX = pWidth;
		}
		if (lastMouseY + pHeight > screen.height) {
			lastMouseY -= (lastMouseY + pHeight + 50) - screen.height;
		}
		lastMouseX -= pWidth;
		lastMouseY += 10;
		features += ",screenX=" + lastMouseX + ",left=" + lastMouseX + "screenY=" + lastMouseY + ",top=" + lastMouseY;
	}

	if (closeOnLoseFocus) {
		curPopupWindow = window.open(url, name, features, false);
		curPopupWindow.focus();
	} else {
		win = window.open(url, name, features, false);
		win.focus();
	}
}

function closePopup() {
	if (curPopupWindow != null) {
       
		if (!curPopupWindow.closed)
			curPopupWindow.close();
		
		curPopupWindow = null;
	}
}

function gotoAction(url) {
	if (url.length > 0)
		window.location = url;
}

function gotoHelpAction(sel, url) {
	if (url.length > 0) {
		window.location = url;
		sel.selectedIndex = 0;
	}
}

function showDate(baseURL, fld, frm) {
	var str = baseURL + "/lookup/cal.cfm?frm=" + frm + "&fld=" + fld;
	if (eval("document." + frm + "." + fld).value.length > 0)
		str = str + "&cdate=" + eval("document." + frm + "." + fld).value;
	
	openPopup(str, "calwin", 170, 190, "width=170,height=190,toolbar=no,status=no,directories=no,menubar=no,resizable=no,scrollbars=no", true);
}

function openWindow(url) {
	var popupWin = window.open(url, 'newWin',  'status,scrollbars,resizable,menubar,toolbar,dependent,width=750,height=550,left=50,top=50');
	popupWin.focus();
}

function openLookup() {
	openPopup("../lookup/lookup.cfm", "lookup", 430, 300, "left=200,top=200,width=430,height=300,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollbars=no", false);
}

function openNotes(recID, num, root, jobID) {
	openPopup(root + "/notes/index.cfm?recID=" + recID + '&hidenum=' + num + '&jobID=' + jobID, "noteWin", 550, 500, "", false);
}

function openHelp(url) {
	var popupWin = window.open(url, 'newWin',  'status,scrollbars,resizable,menubar,toolbar,dependent,width=550,height=350,left=25,top=25');
	popupWin.focus();
}