var lastMouseX;
var lastMouseY;
var curPopupWindow = null;

var beenFocused = false;
document.onmousedown = markFocused;
function markFocused() {
	beenFocused = true;
}

function setFocusOnLoad() {
	if (!beenFocused) setFocus();
}

function setFocus() {
	var sidebarSearch;
	// search for a tabIndexed field to focus on
	for (var firstIndex=1; firstIndex < 10; firstIndex ++ ) {
		var nextIndex = firstIndex;
		for (var frm = 0; frm < document.forms.length; frm++) {
			for (var fld = 0; fld < document.forms[frm].elements.length; fld++) {
				var elt = document.forms[frm].elements[fld];
				if (elt.tabIndex != nextIndex) continue;
				if ((elt.type == "text" || elt.type == "textarea" || elt.type == "password" || elt.type == "select-one") && !elt.disabled && document.forms[frm].name != "site_search") {
					elt.focus();
					if (elt.type == "text") elt.select();
					return true;
				} else {
					nextIndex++;
					fld = 0;
				}
			}
		}
	}
	
	// failed to find a tabIndexed field, try to find the field based on it's natural position.
	for (var frm = 0; frm < document.forms.length; frm++) {
		for (var fld = 0; fld < document.forms[frm].elements.length; fld++) {
			var elt = document.forms[frm].elements[fld];
			// skip buttons, radio, or check-boxes
			// to skip "select" types, remove from if statement
			if ((elt.type == "text" || elt.type == "textarea" || elt.type == "password" || elt.type == "select-one") && !elt.disabled && document.forms[frm].name != "site_search") {
				elt.focus();
				if (elt.type == "text") elt.select();
				return true;
			}
		}
	}
	
	return true;
}

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 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,scrollable=no", true);
}

function openWindow(url, winname) {
	var winl = (screen.width - 790) / 2;
	var wint = (screen.height - 500) / 2;
	if (winname == 'undefined') winname = 'popup';
	var popupWin = window.open(url, winname, 'toolbar=no,menubar=no,scrollbars=yes,resizable=yes,dependent,width=790,height=500,left='+winl+',top='+wint);
	popupWin.focus();
}

function openPrint(url) {
	var printWin = window.open(url, 'printwin', 'toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,dependent,width=600,height=550,left=0,top=0');
	printWin.focus();
}

function openLookup(baseURL, typeID, opt) {
	var str = baseURL + "/lookup/lookup_frame.cfm?typeID=" + typeID;
	if (arguments.length > 2) 
		str += '&opt=' + opt;
	else
		str += '&opt=0';
		
	openPopup(str, "lookup", 430, 300, "width=430,height=300,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no", true);
}
