var overpopup = 0;
var popupstatus = 0;
var releases = new Array();
var curnum = 0;
function $(id) {
  return document.getElementById(id);
}
function mouseclicked(e) {
  if (popupstatus && !overpopup) {
    hidepopup();
  }
}
function getScrollXY() {
  var x, y;
  x = window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft;
  y = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
  return [x?x:0, y?y:0];
}
function getDim() {
  var w, h;
  w = window.innerWidth || document.body.clientWidth || document.documentElement.clientWidth;
  h = window.innerHeight || document.body.clientHeight || document.documentElement.clientHeight;
  return [w?w:0, h?h:0];
}
function showpopup(e, num) {
  var popup = $("popup"), pc = $("popupcontent"), scrolled = getScrollXY(), dim = getDim();
  curnum = num;
  if (typeof(releases[num]) == "undefined") {
    pc.innerHTML = "Loading...";
    dopopup(num);
  } else {
    pc.innerHTML = releases[num];
  }
  popup.style.width = "600px";
  popup.style.height = "400px";
  popup.style.left = scrolled[0]+(dim[0]-600)/2+"px";
  popup.style.top = scrolled[1]+(dim[1]-400)/(navigator.userAgent.toLowerCase().indexOf("msie") != -1?4:2)+"px";
  popup.style.display = "block";
  popupstatus = 1;
  return false;
}
function hidepopup() {
  $("popup").style.display = "none";
  popupstatus = 0;
}
function getXmlHttp() {
  var xmlHttp;
  try {
    xmlHttp = new XMLHttpRequest();
  } catch(e) {
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        return false;
      }
    }
  }
  return xmlHttp;
}
function dopopup(num) {
  var xmlHttp = getXmlHttp();
  if (!xmlHttp) {
    return false;
  }
    xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState != 4) {
      return;
    }
    if (xmlHttp.status == 200) {
      releases[num] = xmlHttp.responseText;
      $("popupcontent").innerHTML = releases[num];
    } else {
      $("popupcontent").innerHTML = "An error occurred.";
    }
  }
    xmlHttp.open("POST", "ajax.php", true);
  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlHttp.send("action=popup&num="+num);
  }
document.onmousedown = mouseclicked;
lctimeout = null;
function checkLink() {
  if ($("addlink").length > 3 && $("addlink").value.indexOf("://") == -1) {
    $("addlink").value = "http://"+$("addlink").value;
  }
  if ($("addlink").value.length < 10) {
    $("linkchecker").innerHTML = "";
    return false;
  }
  var xmlHttp = getXmlHttp();
  if (!xmlHttp) {
    return false;
  }
  clearTimeout(lctimeout);
  $("linkchecker").innerHTML = "Checking link...";
  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState != 4) {
      return;
    }
    if (xmlHttp.status == 200) {
      var resp = xmlHttp.responseText;
      if (resp == "-1") {
        $("linkchecker").innerHTML = '<span style="color: #990000;">This link already exists in the database!</span>';
      } else {
        if (resp == "0") {
          $("linkchecker").innerHTML = '<span style="color: #990000;">This is an invalid link!</span>';
        } else {
          $("linkchecker").innerHTML = '<span style="color: #009900;">Valid link.</span>';
          lctimeout = setTimeout('$("linkchecker").innerHTML = "";', 5000);
        }
      }
    } else {
      $("linkchecker").innerHTML = '<span style="color: #990000;">An error occurred.</span>';
    }
  }
    xmlHttp.open("POST", "ajax.php", true);
  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlHttp.send("action=checklink&link="+escape($("addlink").value));
}
function clearInput() {
  clearTimeout(lctimeout);
  $("addlink").value = "";
  $("addartist").value = "";
  $("addrelease").value = "";
  $("addpassword").value = "";
  $("addyear").value = "";
  $("addbitrate").value = "";
  $("addcomments").value = "";
  $("addstyle1").selectedIndex = 0;
  $("addstyle2").selectedIndex = 0;
  $("addstyle3").selectedIndex = 0;
  $("addstyle4").selectedIndex = 0;
}
function updatePopup() {
  var i, opts = new Array("link", "release", "artist");
  for (i = 0; i < opts.length; i++) {
    if (i == $("popupselect").selectedIndex) {
      $("editing"+opts[i]).style.display = "block";
    } else {
      $("editing"+opts[i]).style.display = "none";
    }
  }
}
function reloadPopup() {
  $("popupcontent").innerHTML = "Loading...";
  if (typeof(releases[curnum]) != "undefined") {
    delete releases[curnum];
  }
  dopopup(curnum);
}
function submitEdit() {
  var getvars = new Array(), els = $("editform"+$("popupselect").selectedIndex).elements;
  var button = $("submitbutton"+$("popupselect").selectedIndex)
  button.disabled = "disabled";
  button.value = "Submitting Edit...";
  for (var i = 0; i < els.length; i++) {
    if (els[i].name == "e1vbr") {
      if (els[i].checked) {
        getvars[getvars.length] = "e1vbr=on";
      }
    } else {
      if (els[i].name.length > 0) {
        getvars[getvars.length] = els[i].name+"="+encodeURIComponent(els[i].value);
      }
    }
  }
  var xmlHttp = getXmlHttp();
  if (!xmlHttp) {
    return false;
  }
    xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState != 4) {
      return;
    }
    if (xmlHttp.status == 200) {
      alert(xmlHttp.responseText);
    } else {
      alert("An error occurred.");
    }
    button.value = "Submit Edit";
    button.disabled = "";
  }
    xmlHttp.open("POST", "ajax.php", true);
  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlHttp.send("action=edit&"+getvars.join("&"));
}
function markAsBad() {
  var sure = confirm("Mark this link as a bad link? (Removed, invalid, etc.)");
  if (!sure) {
    return false;
  }
  var button = $("badbutton");
  button.disabled = "disabled";
  button.value = "Marking as Bad...";
  var xmlHttp = getXmlHttp();
  if (!xmlHttp) {
    return false;
  }
    xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState != 4) {
      return;
    }
    if (xmlHttp.status == 200) {
      if (xmlHttp.responseText == "1") {
        button.value = "Marked as Bad";
        alert("Successfully marked as bad.");
      } else {
        alert(xmlHttp.responseText);
      }
    } else {
      button.disabled = "";
      button.value = "Mark as Bad";
      alert("An error occurred.");
    }
  }
    xmlHttp.open("POST", "ajax.php", true);
  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlHttp.send("action=bad&id="+$("linkid").value);
}
