function authorize() {
  var http_request = getHttpRequest();

  if (! http_request) {
    alert("Your browser is not currently supported. Please contact support@flipbook.tv and inform them of this error.");
    return false;
  }

  http_request.onreadystatechange = function() { updateAuthorization(http_request); };
  http_request.open("POST", "/authorize/", true);
  http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
  var uuid = document.getElementById("uuid").value;
  var password = document.getElementById("flipbookPassword").value;
  http_request.send("uuid=" + uuid + "&password=" + password);
}

function confirmDeleteMovie() {
  var title = document.getElementById("title").value;
  var resp = confirm("Are you sure you want to delete your flipbook '" +
                     title + "'?");
  if (resp) {
    return true;
  } else {
    return false;
  }
}

function deleteComment(comment) {
  var resp = confirm("Are you sure you want to delete this comment?");
  if (resp) {
    var http_request = getHttpRequest();

    if (! http_request) {
      alert("Your browser is not currently supported. Please contact support@flipbook.tv and inform them of this error.");
      return false;
    }

    http_request.onreadystatechange = function() { updateDeleteComment(http_request, comment); };
    http_request.open("GET", "/comments/delete/" + comment + "/");
    http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    http_request.send(null);
    return false;
  } else {
    return false;
  }
}

function displayLogin() {
  document.getElementById("signin").style.display = "none";
  document.getElementById("create").style.display = "none";
  document.getElementById("signinform").style.display = "";
  selectAll("username");
}

function flag(uuid) {
  var http_request = getHttpRequest();

  if (! http_request) {
    alert("Your browser is not currently supported. Please contact support@flipbook.tv and inform them of this error.");
    return false;
  }

  http_request.onreadystatechange = function() { updateFlag(http_request, "movieflag"); };
  http_request.open("GET", "/flag/" + uuid + "/");
  http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
  http_request.send(null);
  return false;
}

function flagComment(comment) {
  var http_request = getHttpRequest();

  if (! http_request) {
    alert("Your browser is not currently supported. Please contact support@flipbook.tv and inform them of this error.");
    return false;
  }

  http_request.onreadystatechange = function() { updateFlag(http_request, "commentFlag" + comment); };
  http_request.open("GET", "/comments/flag/" + comment + "/");
  http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
  http_request.send(null);
  return false;
}

function getHttpRequest() {
  var http_request = false;

  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  return http_request;
}

function login() {
  var http_request = getHttpRequest();

  if (! http_request) {
    alert("Your browser is not currently supported. Please contact support@flipbook.tv and inform them of this error.");
    return false;
  }

  http_request.onreadystatechange = function() { updateLogin(http_request); };
  http_request.open("POST", "/login/", true);
  http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
  var username = document.getElementById("username").value;
  var password = document.getElementById("password").value;
  http_request.send("username=" + username + "&password=" + password);
}

function logout() {
  var http_request = getHttpRequest();

  if (! http_request) {
    alert("Your browser is not currently supported. Please contact support@flipbook.tv and inform them of this error.");
    return false;
  }

  http_request.onreadystatechange = function() { updateLogout(http_request); };
  http_request.open("GET", "/logout/");
  http_request.send(null);
}

function postComment() {
  var http_request = getHttpRequest();

  if (! http_request) {
    alert("Your browser is not currently supported. Please contact support@flipbook.tv and inform them of this error.");
    return false;
  }

  http_request.onreadystatechange = function() { updatePostComment(http_request); };
  http_request.open("POST", "/comments/post/", true);
  http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
  var comment = document.getElementById("id_comment").value;
  var honeypot = document.getElementById("id_honeypot").value;
  var content_type = document.getElementById("id_content_type").value;
  var object_pk = document.getElementById("id_object_pk").value;
  var timestamp = document.getElementById("id_timestamp").value;
  var security_hash = document.getElementById("id_security_hash").value;
  http_request.send("comment=" + comment + "&honeypot=" + honeypot +
                    "&content_type=" + content_type +
                    "&object_pk=" + object_pk + "&timestamp=" + timestamp +
                    "&security_hash=" + security_hash);
  return false;
}

function processEdit(action) {
  if (action == "delete") {
    document.getElementById("editaction").value = "delete";
    document.getElementById("editform").submit();
  } else if (action == "cancel") {
    history.go(-1);
  } else if (action == "save") {
    document.getElementById("editaction").value = "save";
    document.getElementById("editform").submit();
  } else {
    alert("Unknown action: " + action);
    return false;
  }
}

function selectAll(id) {
  document.getElementById(id).focus();
  document.getElementById(id).select();
}

function star(action, uuid) {
  var http_request = getHttpRequest();

  if (! http_request) {
    alert("Your browser is not currently supported. Please contact support@flipbook.tv and inform them of this error.");
    return false;
  }

  http_request.onreadystatechange = function() { updateStar(http_request, action, uuid); };
  http_request.open("GET", "/star/" + action + "/" + uuid + "/");
  http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
  http_request.send(null);
  return false;
}

function updateAuthorization(http_request) {
  if (http_request.readyState == 4) {
    if (http_request.status == 200) {
      document.location.reload();
    } else {
      document.getElementById("flipbookPasswordMessage").innerHTML = "invalid password";
      selectAll("flipbookPassword");
    }
  }
}

function updateDeleteComment(http_request, comment) {
  if (http_request.readyState == 4) {
    if (http_request.status == 200) {
      var comment = document.getElementById("c" + comment);
      while (comment.hasChildNodes()) {
        comment.removeChild(comment.firstChild);
      }
      flag.appendChild(document.createTextNode("Comment removed."));
    } else {
      // Do something?
    }
  }
}

function updateFlag(http_request, nodename) {
  if (http_request.readyState == 4) {
    var flag = document.getElementById(nodename);
    while (flag.hasChildNodes()) {
      flag.removeChild(flag.firstChild);
    }
    flag.appendChild(document.createTextNode("Thanks for flagging."));
  }
}

function updateLogin(http_request) {
  if (http_request.readyState == 4) {
    if (http_request.status == 200) {
      document.getElementById("mine").style.display = "";
      document.getElementById("browse").setAttribute("class", "theirs");
      document.getElementById("login").style.display = "none";
      document.getElementById("signout").style.display = "";
      document.location.reload();
    } else {
      document.getElementById("message").innerHTML = "invalid username or password";
      selectAll("username");
    }
  }
}

function updateLogout(http_request) {
  if (http_request.readyState == 4) {
    if (http_request.status == 200) {
      document.getElementById("mine").style.display = "none";
      document.getElementById("browse").setAttribute("class", "browse");
      document.getElementById("login").style.display = "";
      document.getElementById("signin").style.display = "";
      document.getElementById("create").style.display = "";
      document.getElementById("signinform").style.display = "none";
      document.getElementById("signout").style.display = "none";
      document.location = "/";
    }
  }
}

function updatePostComment(http_request) {
  if (http_request.readyState == 4) {
    if (http_request.status == 200) {
      document.getElementById("id_comment").value = '';
      document.location.reload();
    } else {
      // Do something?
    }
  }
}

function updateStar(http_request, action, uuid) {
  if (http_request.readyState == 4) {
    if (http_request.status == 200) {
      var stara = document.getElementById('stara');
      var starcount = document.getElementById('starcount');
      var stari = document.getElementById('stari');
      if (action == 'add') {
        stara.setAttribute('onClick', "return star('remove', '" + uuid + "');");
        starcount.childNodes[0].nodeValue = parseInt(starcount.childNodes[0].nodeValue) + 1;
        stari.setAttribute('src', '/static/images/goldstar.png');
      } else {
        stara.setAttribute('onClick', "return star('add', '" + uuid + "');");
        starcount.childNodes[0].nodeValue = parseInt(starcount.childNodes[0].nodeValue) - 1;
        stari.setAttribute('src', '/static/images/regstar.png');
      }
    } else {
      // Do something?
    }
  }
}

function validateAccount() {
  var account = document.getElementById("account");
  var retval = true;
  if (account.username.value === "") {
    retval = false;
    var usernameWarningText = document.createTextNode("Please enter your username.");
    document.getElementById("username_warning").appendChild(usernameWarningText);
  }
  if (account.email.value === "") {
    retval = false;
    var emailWarningText = document.createTextNode("Please enter a valid email address.");
    document.getElementById("email_warning").appendChild(emailWarningText);
  }
  if (account.password1.value === "") {
    retval = false;
    var passwordWarningText = document.createTextNode("Please enter a password.");
    document.getElementById("password1_warning").appendChild(passwordWarningText);
  }
  if (account.password2.value === "") {
    retval = false;
    var passwordconfWarningText = document.createTextNode("Please confirm your password.");
    document.getElementById("password2_warning").appendChild(passwordconfWarningText);
  }
  if (account.password1.value && account.password2.value && account.password1.value != account.password2.value) {
    retval = false;
    var passwordWarningText = document.createTextNode("Your passwords do not match. Please re-enter your password.");
    document.getElementById("password1_warning").appendChild(passwordWarningText);
  }
  return retval;
}
