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();

	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 openLookup(baseURL, tbl, frm, fld_id, fld_txt, param) {
	var str = baseURL + "/includes/lookup/lookup.cfm?tbl=" + tbl + "&frm=" + frm + "&fld_id=" + fld_id + "&fld_txt=" + fld_txt;

	if (param == undefined) param = '';
	
	str += '&param=' + param;
	
	openPopup(str, "lookup", 430, 300, "width=430,height=300,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no", true);
}

function openCombo(baseURL, frm, fld, evt) {
	var str = baseURL + "/includes/lookup/combo.cfm?frm=" + frm + "&fld=" + fld + "&evt=" + evt;
	
	openPopup(str, "combo", 300, 200, "width=300,height=200,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no", true);
}

function showDate(baseURL, fld, frm) {
	var str = baseURL + "/includes/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,scrollable=no", true);
}

function openWindow(url) {
  var popupWin = window.open(url, 'newWin',  'status=no,scrollbars,resizable,menubar,toolbar,dependent,width=777,height=457,left=50,top=50');
	popupWin.focus();
}

function openPTWindow(url) {
  var popupWin = window.open(url, 'newWin',  'scrollbars,resizable,dependent,width=777,height=457,left=50,top=50');
	popupWin.focus();
}

function openMap(url) {
  var mapWin = window.open(url, 'mapWin',  'status,scrollbars,resizable,menubar,toolbar,dependent,width=770,height=502,left=50,top=50');
	mapWin.focus();
}

function openWindowGolf(url) {
  popupWin = window.open(url, 'golfWin',  'status,scrollbars,resizable,toolbar=yes,dependent,width=640,height=550,left=50,top=50');
	popupWin.focus();
}	

function openPolicy(url) {
  popupWin = window.open(url, 'PWin',  'status,scrollbars,resizable,toolbar=yes,dependent,width=610,height=550,left=50,top=50');
	popupWin.focus();
}	
