var lastMouseX;
var lastMouseY;
var curPopupWindow = null;
var win = null;

function setLastMousePosition(e) {
  if (navigator.appName.indexOf("Microsoft") != -1) e = window.event;
  lastMouseX = e.screenX;
  lastMouseY = e.screenY;
}

function openFinePrint(baseURL) {
  openPopup(baseURL, "finePrint", 350, 300, "width=400, height=350, directories=no, location=no, toolbar=no, status=yes, menubar=no, resizable=no, scrollbars=yes", true, true);
}

function openLookup(baseURL, width) {
  openPopup(baseURL, "lookup", 350, 300, "width="+width+", height=350, directories=no, location=no, toolbar=no, status=yes, menubar=no, resizable=no, scrollbars=no", true, true);
}

function openMovie(baseURL, width, height) {
  openPopup(baseURL, "finePrint", width, height, "width="+width+", height="+height+", directories=no, location=no, toolbar=no, status=no, menubar=no, resizable=no, scrollbars=no", false, true);
}

function openPopup(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;
    if (navigator.appName.indexOf("Microsoft") != -1) {
      features += ", left=" + lastMouseX + ", top=" + lastMouseY;
    } else {
      features += ", screenX=" + lastMouseX + ", screenY=" + lastMouseY;
    }
  } else {
		// open the window centered on the screen
		var winl = (screen.width-pWidth)/2;
		var wint = (screen.height-pHeight)/2;
		if (winl < 0) winl = 0;
		if (wint < 0) wint = 0;
    if (navigator.appName.indexOf("Microsoft") != -1) {
      features += ", left=" + winl + ", top=" + wint;
    } else {
      features += ", screenX=" + winl + ", screenY=" + wint;
    }
	}
  if (closeOnLoseFocus) {
    curPopupWindow = window.open(url, name, features, false);
    curPopupWindow.focus ();
  } 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);
    win.focus ();
  }
}

function closePopup() {
  if (curPopupWindow != null) {
    if (!curPopupWindow.closed) {
      curPopupWindow.close();
    }
    curPopupWindow = null;
  }
}

String.prototype.replace = stringReplace;

function stringReplace(findText, replaceText) {
  var originalString = new String(this);
  var pos = 0;
  var len = findText.length;
  pos = originalString.indexOf(findText);
  while (pos != -1) {
    preString = originalString.substring(0, pos);
    postString = originalString.substring(pos + len, originalString.length);
    originalString = preString + replaceText + postString;
    pos = originalString.indexOf(findText);
  }
  return originalString;
}

// Remember and recall whether the user wants their login name (email) saved on the login screen
function setEmailState() {
	document.loginForm.emailAddress.focus();
  emailAddress = getCookie("email");
  if ((emailAddress != null)) {
    document.loginForm.emailAddress.value = emailAddress;
    document.loginForm.rememberEmail.checked = true;
    document.loginForm.password.focus();
	}
}

function rememberEmailState() {
  var expires = new Date ();
  var oneYear = 60 * 60 * 24 * 365 * 1000;
  if (document.loginForm.rememberEmail.checked) {
    expires.setTime (expires.getTime() + oneYear);
  } else {
    expires.setTime (expires.getTime() - oneYear);
  }
  emailAddress = document.loginForm.emailAddress.value;
  setCookie("email", emailAddress, expires);
}

function doLogin() {
  rememberEmailState();
  document.loginForm.submit();
}

// Remember and recall whether the user wants their login name (email) saved on the login screen
function setToolbarEmailState() {
	document.downloadToolbarForm.emailAddress.focus();
  var location = new String(document.location);
  emailAddress = getCookie("email");
  if ((emailAddress != null)) {
    document.downloadToolbarForm.emailAddress.value = emailAddress;
    document.downloadToolbarForm.password.focus();
	}
}


/**
 * Disables the given button and submits the form using the given dispatch value.
 * Call this method from the "onclick" event-handler for submit buttons.  Use this
 * only with forms that post to lookupDispatchActions.
 */
function submitDispatchForm(button, form, dispatchValue) {
	button.disabled = true;
	form.dispatch.value = dispatchValue;
	form.submit();
}  
