/****************************
Roboshrub Inc.
Delete Menu
http://roboshrub.blogspot.com
*****************************/

// Sets up the delete window.
function showDelete() {
  deleteList();
  g('delPrompt').innerHTML="<h3>Delete Menu</h3>";
  showWindow('delWindow');
}

// Populates a list of saved files for the "Delete" window.
function deleteList() {
  try {
    var temp=getCookie().split(":SPLIT:");
    g('delContainer').innerHTML="";
    totaldel=temp.length; // Total number of saves.
      
    /* Creates checkboxes for each saved file. This
     * allows the user to delete or copy more than one
     * file at once.
     */
    for (i=temp.length-2; i>=0; i--) {
      var currentName=temp[i].split(":PARSE:")[0];
      g('delContainer').innerHTML+="<label for=\"del"+i+"\"><input type=\"checkbox\" id=\"del"+i+"\" \/> "+currentName+"</label><br \/>";
    }
      
    // Checks if there were no saves.
    if (g('delContainer').innerHTML=="") {
      g('delContainer').innerHTML="<div class=\"nofile\">There are no saved files.<\/div>";
    }
  }
  catch(e) {
    g('delContainer').innerHTML="<div class=\"nofile\">There are no saved files.<\/div>";
  }
}

// Disables/enables all elements in the Delete window.
function disableDelete(a) {
  g('sall').disabled=a;
  g('snon').disabled=a;
  g('invs').disabled=a;
  g('copy').disabled=a;
  g('d1').disabled=a;
  g('d2').disabled=a;
  for (i=0; i<totaldel-1; i++) {
    g("del"+i).disabled=a;
  }
}

// Returns to the main screen from the delete window.
function justEndDel() {
  g('delPrompt').innerHTML="<h3>Delete Menu</h3>";
  turnOff('delWindow');
  turnOn('mainWin');
  (main.filters)?main.filters.alpha.opacity=100:main.style.opacity=1;
  g('promptSpacer').style.display="none";
  disableDelete(false);
}

// Cancels delete and copy functions.
function cancelDel() {
  g('delPrompt').innerHTML="<h3>Delete Menu</h3>";
  var temp=getCookie().split(":SPLIT:");
  totaldel=temp.length;
  for (i=0; i<totaldel-1; i++) {
      g("del"+i).disabled=false;
    }
  disableDelete(false);
}

// Selects all checkboxes in the "Delete" window.
function selectAll() {
  if (totaldel==-1) {
    return;
  }
  try {
    for (i=0; i<totaldel-1; i++) {
      g("del"+i).checked=true;
    }
  }
  catch(e) {
    return;
  }
}

// Deselects all checkboxes in the "Delete" window.
function selectNone() {
  if (totaldel==-1) {
    return;
  }
  try {
    for (i=0; i<totaldel-1; i++) {
      g("del"+i).checked=false;
    }
  }
  catch(e) {
    return;
  }
}

// Inverts selection of checkboxes.
function invertSelection() {
  if (totaldel==-1) {
    return;
  }
  try {
    for (i=0; i<totaldel-1; i++) {
      if (g("del"+i).checked) {g("del"+i).checked=false;}
      else {g("del"+i).checked=true;}
    }
  }
  catch(e) {
    return;
  }
}

// Deletes checked saves.
function actualDelete() {
  if (totaldel==-1) {
    alert("There is nothing to delete.");
    return;
  }
  try {
    main=g('mainWin');
    
    var delFound=false;
    var temp="";
    var q=getCookie().split(":SPLIT:");
    
    /* If save if not checked for deletion, add it to temp.
     * If not, indicate that a save has been deleted.
     */
    for (i=0; i<totaldel-1; i++) {
      if (!g("del"+i).checked) {
        temp+=q[i];
        temp+=":SPLIT:";
      }
      else {
        delFound=true;
        disableDelete(true);
      }
    }

    // Checks for no selected saves.
    if (!delFound) {
      g('delPrompt').innerHTML="You haven't selected any files.";
      return;
    }
    
    // Verifies that the user wishes to delete the selected saves.
    g('delPrompt').innerHTML="Do you want to <strong>delete</strong> the selected files?<br />";
    g('delPrompt').innerHTML+="<input type='button' class='miniButton' style='margin:5px;' onclick='finishDel();' value='Yes' />";
    g('delPrompt').innerHTML+="<input type='button' class='miniButton' style='margin:5px;' onclick='cancelDel();' value='No' />";
    
  }
  catch(e) {
    return;
  }
}

// Completes deletion after OK from prompt.
function finishDel() {
  var temp="", q=getCookie().split(":SPLIT:");
  
  // Keeps only the ones the user didn't check.
  for (i=0; i<totaldel-1; i++) {
    if (!g("del"+i).checked) {
      temp+=q[i];
      temp+=":SPLIT:";
    }
    else {
      if (activeFile==i) {
        activeFile=-1;
      }
      if (activeFile>i) {
        activeFile--;
      }
    }
  }
  setCookie(temp); // Resets value of cookie to temp (the saves that weren't deleted).
  disableDelete(false);
  g('delPrompt').innerHTML="All selected files have been deleted.";
  deleteList();
}

// Copy the checked saves.
function copySel() {

  // There's no previous saved data.
  if (getCookie()=="" || getCookie()=="unknown") {
    g('delPrompt').innerHTML="There is nothing to copy.";
    return;
  }
  disableDelete(true);
  
  var anyChecked=false;
  
  for (i=0; i<totaldel-1; i++) {
    if (g("del"+i).checked) {
      anyChecked=true;
    }
  }
    
  // Checks for no selected saves.
  if (!anyChecked) {
    cancelDel();
    g('delPrompt').innerHTML="You haven't selected any files.";
    return;
  }
  
  g('delPrompt').innerHTML="Do you want to <strong>copy</strong> the selected files?<br /><br />";
  g('delPrompt').innerHTML+="<input type='button' class='miniButton' style='margin:5px;' onclick='completeCopy();' value='Yes' />";
  g('delPrompt').innerHTML+="<input type='button' class='miniButton' style='margin:5px;' onclick='cancelDel();' value='No' />";
}

// Completes copy after OK from prompt.
function completeCopy() {
  var temp="";
  var q=getCookie().split(":SPLIT:");
  var anyChecked=false;
  
  for (i=0; i<totaldel-1; i++) {
    if (g("del"+i).checked) {
      anyChecked=true;
      temp+=q[i].split(":PARSE:")[0]+"(2):PARSE:";
      temp+=q[i].split(":PARSE:")[1]+":PARSE:";
      temp+=q[i].split(":PARSE:")[2]+":PARSE:";
      temp+=giveTime();
      if (q[i].split(":PARSE:")[4]) {
        temp+=":PARSE:";
        temp+=q[i].split(":PARSE:")[4];
      }
      temp+=":SPLIT:";
    }
  }
    
  // Checks for no selected saves.
  if (!anyChecked) {
    g('delPrompt').innerHTML="You haven't selected any files.";
    disableDelete(false);
    return;
  }
  setCookie(getCookie()+temp); // Adds new copies to the end of the cookie.
  disableDelete(false);
  g('delPrompt').innerHTML="All selected files have been copied.";
  deleteList();
}