function nosend(){
  hideOverWindow();
}
function send(){
  xmlHttp = new XMLHttpRequest();
  if(xmlHttp == null){
	// display some error message
	return;
  }
  xmlHttp.onreadystatechange = sendReply;
  var addy = "email.php?";
  addy += "f=" + encodeURI(document.getElementById("from").value);
  addy += "&s=" + encodeURI(document.getElementById("subj").value);
  addy += "&m=" + encodeURI(document.getElementById("msg").value);
  if(document.getElementById("reply").checked){
	addy += "&r=Y";
  }
  if(document.getElementById("loc").checked){
	addy += "&l=" + encodeURI(window.location);
  }
  xmlHttp.open("GET", addy, true);
  xmlHttp.send(null);
  document.getElementById("nosend").disabled = true;
  var sb = document.getElementById("send");
  sb.disabled = true;
  sb.value = "Sending...";
}
function sendReply(){
  if(xmlHttp.readyState != 4 && xmlHttp.readyState != "complete")
	return;
  document.getElementById("nosend").disabled = false;
  var sb = document.getElementById("send");
  sb.disabled = false;
  sb.value = "Send";
  if(xmlHttp.responseText == "WIN"){
	document.getElementById("subj").value = "";
	document.getElementById("msg").value = "";
	hideOverWindow();
	alert("Your message was sent.");
  } else {
	alert("There was an error sending your message.\nPerhaps retry?");
  }
}

function getOverWindow(){
  var ret = document.getElementById("over-window");
  if(ret != null){
	return ret;
  }
  ret = document.createElement("div");
  ret.id = "over-window";
  ret.style.width = "500px";
  ret.style.minHeight = "200px";
  ret.style.position = "absolute";
  ret.style.left = "50%";
  ret.style.marginLeft = "-250px";
  ret.style.top = "125px";
  ret.style.backgroundColor = "#FFF7AF";
  ret.style.borderStyle = "outset";
  ret.style.borderWidth = "3px";
  ret.style.display = "none";
  ret.style.zIndex = "2";
  var clb = document.createElement("div");
  clb.id = "over-window-close-div";
  clb.style.position = "relative";
  clb.style.textAlign = "right";
  clb.style.fontSize = "small";
  clb.innerHTML = "<a href=\"javascript:hideOverWindow();\">[close]</a>";
  ret.appendChild(clb);

  var liw = document.createElement("div");
  liw.id = "log-div";
  liw.style.margin = "2px 3px 3px 3px";
  var ld = "<h3 style=\"display:inline\">Why?</h3>";
  ld += "<p>So you can rate math jokes!</p>";
  ld += "<h3 style=\"display:inline\">How?</h3>";
  ld += "<p>You need to have an ";
  ld += "<a href=\"http://www.openid.net\">OpenID</a>. ";
  ld += "If you know your OpenID url, just enter it in the space above. ";
  ld += "If you don't know about OpenID, you might have one anyway. ";
  ld += "Check <a href=\"http://www.openid.net/get\">this out</a>.</p>";
  ld += "<p>Also, you'll need to have cookies enabled. Sorry.</p>";
  liw.innerHTML = ld;
  ret.appendChild(liw);

  var ew = document.createElement("div");
  ew.id = "email-div";
  ew.style.margin = "2px 3px 3px 3px";
  var ed = "<p style=\"text-align:center;margin-top:0\">Email theidiot, here at sumidiot.com</p>";
  ed += "<span style=\"display:inline-block;width:80px;text-align:right;\">Email:</span><input type=\"text\" id=\"from\" value=\"[Not Required]\" style=\"width:300px\" /><br />";
  ed += "<span style=\"display:inline-block;width:80px;text-align:right;\">Subject:</span><input type=\"text\" id=\"subj\" value=\"\" style=\"width:300px\" /><br />";
  ed += "<span style=\"display:inline-block;width:80px;text-align:right;vertical-align:top\">Message:</span><textarea cols=\"55\" rows=\"10\" id=\"msg\" value=\"\"></textarea><br />";
  ed += "<input type=\"checkbox\" id=\"reply\"/>";
  ed += "Please reply to my email.<br />";
  ed += "<input type=\"checkbox\" id=\"loc\"/>";
  ed += "Send current address: " + window.location + "<br />";
  ed += "<div style=\"text-align:right\">";
  ed += "<input type=\"submit\" id=\"nosend\" value=\"Nevermind\" onclick=\"javascript:nosend();\"/>";
  ed += "<input type=\"submit\" id=\"send\" value=\"Send\" onclick=\"javascript:send();\" />";
  ed += "</div>";
  ew.innerHTML = ed;
  ret.appendChild(ew);

  document.body.appendChild(ret);
  return ret;
}
function showOverWindow(){
  var ow = getOverWindow();
  ow.style.display = "block";
}
function hideOverWindow(){
  var ow = getOverWindow();
  ow.style.display = "none";
}
function loggingInOW(){
  var ow = getOverWindow();
  document.getElementById("log-div").style.display = "block";
  document.getElementById("email-div").style.display = "none";
  showOverWindow();
}
function emailOW(){
  var ow = getOverWindow();
  document.getElementById("log-div").style.display = "none";
  document.getElementById("email-div").style.display = "block";
  document.getElementById("nosend").disabled = false;
  var sb = document.getElementById("send");
  sb.value = "Send";
  sb.disabled = false;
  showOverWindow();
}
