var curPopupWindow = null;

// sets focus based on existance of username
function loader() {
  var username = document.login_nop.username.value;
  if (username != null && username.length > 0) {
    document.login.pw.focus();
  } else {
    document.login_nop.username.focus();
  }
}

function handleLogin() {
  document.login.un.value = document.login_nop.username.value;
  document.login.width.value = screen.width;
  document.login.height.value = screen.height;
}

function closePopup() {
	if (curPopupWindow != null) {
	   
		if (!curPopupWindow.closed) {
			curPopupWindow.close();
		}
		curPopupWindow = null;
	}
}

/**
 * Handles popup windows.
 * If snapToLastMousePosition is true, then the popup will open up near the mouse click.
 * If closeOnLoseFocus 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, 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);
    } 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);
    }

}

/**
 * Used by login to check if caps lock is on while entering a password
 */
function checkCaps( e ) {
	var key = 0;
	var shifted = false;

	// IE
	if ( document.all ) {
		key = e.keyCode;
	// Everything else
	} else {
		key = e.which;
	}
	
	shifted = e.shiftKey;
	
	var pwcaps = document.getElementById('pwcaps');
	
	var upper = (key >= 65 && key <= 90);
	var lower = (key >= 97 && key <= 122);
	
	if ( (upper && !shifted) || (lower && shifted) ) {
		pwcaps.style.visibility='visible';
	} else if ( (lower && !shifted) || (upper && shifted) ) {
	    pwcaps.style.visibility='hidden';
	}
}
