// <script>
window.onload = function () {
  if (document.getElementById) {
    if (location.href.indexOf("mediadb.php") > 0) {
      document.getElementById("sel_versions").onclick = function () { inputFlag("versions", this.value, false); }
      document.getElementById("sel_tags").onclick = function () { inputFlag("tags", this.value, false); }
      document.getElementById("instructions").onclick = function () {
        inst = document.getElementById("details");
        if (inst.style.display == "block")
          inst.style.display = "none";
        else
          inst.style.display = "block";
      }
      document.getElementById("details").style.display = "none";
      document.getElementById("yesupload").onclick = function () {
        document.getElementById("fileneedsupload").style.display = "block";
        document.getElementById("fileuploaded").style.display = "none";
      }
      document.getElementById("noupload").onclick = function () {
        //document.getElementById("instructions").style.display = "none";
        //document.getElementById("mainform").style.display = "none";
        //document.getElementById("supercontainer").style.display = "none";
        //document.getElementById("uploadinfo").style.display = "block";
        document.getElementById("fileneedsupload").style.display = "none";
        document.getElementById("fileuploaded").style.display = "block";
      }
      document.getElementById("file_thumb").onchange = function () { checkImage(this); }
      document.getElementById("file_titlecard").onchange = function () {
        if (this.value) {
          document.getElementById("file_image").value = "images/titlecards/" + this.value + ".jpg";
          document.getElementById("file_thumbnail").value = "";
        }
      }
      if (location.href.indexOf("action=edit") > 0) {
        document.getElementById("fileneedsupload").style.display = "none";
        document.getElementById("fileuploaded").style.display = "block";
      }
    }
    //else if (location.href.indexOf("fileinfo") > 0) {
      //if (document.getElementById("backlink"))
        //document.getElementById("backlink").onclick = function () { history.go(-1); return false; }
    //}
    else if (location.href.indexOf("download") > 0) {
      getFile();
    }
  }
  else {
    alert("You will need to enter tags manually because your browser is too old.");
  }
}

function inputFlag(whatflag, newflag, auto) {
  flags = document.getElementById("file_" + whatflag).value;
  if (flags.indexOf(newflag) == -1) {
    document.getElementById("file_" + whatflag).value = newflag + "\n" + flags;
    if (newflag == "simkickitup") {
      if (!auto) alert("Because Kick it Up KSFs work fine in DirectMove, those tags have been automatically added for you.");
      inputFlag(whatflag, "simdirectmoveold", true);
      if (flags.indexOf("simstepmania") == -1) 
        if (confirm("Do you want to automatically add the StepMania tag as well?\n\nNote:\nKSF files usually work fine in StepMania, but if your KSF uses non-chronological BPM changes then StepMania may have problems with it."))
          inputFlag(whatflag, "simstepmania", true);
    }
    if (newflag == "simdirectmoveold") {
      if (flags.indexOf("simdirectmove5") == -1) {
        inputFlag(whatflag, "simdirectmove5", true);
        if (!auto) alert("Because old DirectMove KSFs work fine in DirectMove 0.5, that tag has been automatically added for you.");
      }
    }
    if (newflag.substr(0,3) == "sim") {
      document.getElementById("file_type").selectedIndex = 1;
    }
  }
}

function confirmDelete(whatfile,filename) {
  if (confirm("Are you sure you want to delete the file \"" + filename + "\"?"))
    document.location.href = "/media/mediadb.php?action=delete&fileid=" + whatfile;
}

function checkImage(imagefield) {
  if (imagefield.value.length > 0) {
    imagefile = imagefield.value.split(".");
    imagetype = imagefile[imagefile.length-1];
    if ((imagetype.toLowerCase() != "png")
    && (imagetype.toLowerCase() != "jpg")
    && (imagetype.toLowerCase() != "jpeg")
    && (imagetype.toLowerCase() != "gif")) {
      alert("Thumbnails are only accepted in PNG, JPG, or GIF formats.");
      imagefield.value = "";
    }
  }
}

function getFile() {
  if (filetoget) {
    location.href = filetoget;
  }
}
