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

var activeFile=-1; // Currently open file.

// Sets up the load window.
function showLoad() {
  g('loadPrompt').innerHTML="<h3>Load Menu</h3>";
  showWindow('loadWindow');
  loadList();
}

// Disables/enables all elements in the Load window.
function disableLoad(a) {
  try {
  g('rfile').disabled=a;
  g('ginfo').disabled=a;
  g('cpass').disabled=a;
  g('l1').disabled=a;
  g('l2').disabled=a;
  var temp=getCookie().split(":SPLIT:");
  for (i=0; i<temp.length-1; i++) {
    g("load"+i).disabled=a;
  }
  }catch(e){}
}

// Returns to the main screen from the Load window.
function justEndLoad() {
  g('loadPrompt').innerHTML="<h3>Load Menu</h3>";
  turnOff('loadWindow');
  turnOn('mainWin');
  (main.filters)?main.filters.alpha.opacity=100:main.style.opacity=1;
  g('promptSpacer').style.display="none";
  disableLoad(false);
}

// Cancels password and renaming.
function cancelLoad() {
  g('loadPrompt').innerHTML="<h3>Load Menu</h3>";
  var temp=getCookie().split(":SPLIT:");
  for (i=0; i<temp.length-1; i++) {
      g("load"+i).disabled=false;
    }
  disableLoad(false);
}

// These arrays store the names of the months and week. They are used when the user accesses the info of a file.
var month=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var week=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

// Returns the current time in the pattern (all in integers):
// Day of the month | Day of the Week | Month | Year | Hour | Minute | AM/PM
function giveTime() {
  var clock, year, day, weekday, themonth, hour, min2, ampm=true, actualDate;
  clock=new Date();
  year=clock.getYear();
  themonth=clock.getMonth();
  weekday=clock.getDay();
  day=clock.getDate();
  hours=clock.getHours();
  min2=clock.getMinutes();
  if (year<1900) {
    year+=1900;
  }
  actualDate=day+"|"+weekday+"|"+themonth+"|"+year+"|";
  if (hours>12) {
    ampm=false;
    hours-=12;
  }
  else if (hours==0) {
    hours=12;
  }
  else if (hours==12) {
    ampm=false;
  }
  actualDate+=hours+"|";
  actualDate+=(min2<10)?"0"+min2+"|":min2+"|"; // Adds zero prefix to the minute value if it is under 10.
  actualDate+=(ampm)?"AM":"PM";
  return actualDate;
}

// Parses date pattern into human readable text.
function readDate(savedDate) {
  var q=savedDate.split("|");
  // 12 Hour Clock
  if (!E_24HOUR) {
    return (q[4]+":"+q[5]+" "+q[6]+" on "+week[q[1]]+", "+month[q[2]]+" "+q[0]+", "+q[3]);
  }
  // 24 Hour Clock
  if (q[6]=="PM") {
    q[4]=parseInt(q[4])+12;
  }
  else if (q[4]==12) {
    q[4]="00";
  }
  else if (parseInt(q[4])<10) {
    q[4]="0"+q[4];
  }
  return (q[4]+":"+q[5]+" on "+week[q[1]]+", "+q[0]+" "+month[q[2]]+", "+q[3]);
}

/* Displays an alert telling the user the file's name,
 * the date it was last saved, and whether or not the
 * file is password protected.
 */
function getInfo() {
  if (loaded==-1) {
    g('loadPrompt').innerHTML="You haven't selected a file.";
    return;
  }
  var temp=getCookie().split(":SPLIT:");
  var q=temp[loaded].split(":PARSE:");
  var message="\""+q[0]+"\" was last saved at "+readDate(q[3])+".<br /><br />";
  message+=(!q[4])?"It is <strong>NOT</strong> password protected.":"It is password protected.";
  g('loadPrompt').innerHTML=message;
}

// Populates a list of saved files for the "Load" window.
function loadList() {
  loaded=-1;
  try {
    var temp=getCookie().split(":SPLIT:");
    g('loadContainer').innerHTML="";

    /* Creates radio buttons for each saved file. Clicking
     * the buttons sets the variable "loaded" to the file's
     * position in cryptor.
     */
    for (i=temp.length-2; i>=0; i--) {
      var currentName=temp[i].split(":PARSE:")[0];
      g('loadContainer').innerHTML+="<label for=\"load"+i+"\"><input type=\"radio\" id=\"load"+i+"\" name=\"loader\" onclick=\"loaded="+i+"\" \/> "+currentName+"</label><br \/>";
    }
      
    // Checks if there were no saves.
    if (g('loadContainer').innerHTML=="") {
      g('loadContainer').innerHTML="<div class=\"nofile\">There are no saved files.<\/div>";
    }
  }
  catch(e) {
    g('loadContainer').innerHTML="<div class=\"nofile\">There are no saved files.<\/div>";
  }
}

// Takes a save from the cookie and writes it to the current window.
function loadSave() {
  if (loaded==-1) {
    g('loadPrompt').innerHTML="You haven't selected a file.";
    return;
  }
  var temp=getCookie().split(":SPLIT:");
  var q=temp[loaded].split(":PARSE:");
  
  // If password exists, prompts user for it.
  if (q[4]) {
    disableLoad(true);
    g('loadPrompt').innerHTML="Please enter the file's <strong>password</strong> to continue.<br /><br />";
    g('loadPrompt').innerHTML+="<form onsubmit='continueLoadingFromPass();return false;' style='margin:5px auto;' action=''><fieldset class='notMainFieldset'>";
    g('loadPrompt').innerHTML+="<input id='verbox' type='password' /><input type='button' class='miniButton' style='margin-left:5px;' onclick='continueLoadingFromPass();' value='OK'>";
    g('loadPrompt').innerHTML+="</fieldset></form>";
    g('verbox').focus();
  }
  else {
    setValue('encoder',q[1]);
    setValue('decoder',q[2]);
    activeFile=loaded;
    loaded=-1;
    justEndLoad();
    g('skinBox').src='images/skin.gif';
    g('skinBox').alt="Edit Skin";
    g('skinBox').title="Edit Skin";
  }
}

// Continues loading a file that has a password.
function continueLoadingFromPass() {
  var temp=getCookie().split(":SPLIT:");
  var q=temp[loaded].split(":PARSE:");
  
  // Checks old password to make sure it's the right one.
  if (q[4]!=g('verbox').value) {
    cancelLoad();
    g('loadPrompt').innerHTML="That password is incorrect.";
    return;
  }
  setValue('encoder',q[1]);
  setValue('decoder',q[2]);
  activeFile=loaded;
  loaded=-1;
  justEndLoad();
}

// Changes the password of a save.
function changePass() {
  if (loaded==-1) {
    g('loadPrompt').innerHTML="You haven't selected a file.";
    return;
  }
  var temp=getCookie().split(":SPLIT:");
  var q=temp[loaded].split(":PARSE:");
  
  disableLoad(true);
  g('loadPrompt').innerHTML="Do you want to change the password of \""+q[0]+"\"?<br /><br />";
  g('loadPrompt').innerHTML+="<input type='button' class='miniButton' style='margin:5px;' onclick='finishPass();' value='Yes' />";
  g('loadPrompt').innerHTML+="<input type='button' class='miniButton' style='margin:5px;' onclick='cancelLoad();' value='No' />";
}

// Asks for previous password.
function finishPass() {
  var temp=getCookie().split(":SPLIT:");
  var q=temp[loaded].split(":PARSE:");
  
  // If save had previous password, asks for it.
  if (q[4]) {
    g('loadPrompt').innerHTML="Please enter the <strong>current</strong> password.<br /><br />";
    g('loadPrompt').innerHTML+="<form onsubmit='verifyOldPassword();return false;' style='margin:5px auto;' action=''><fieldset class='notMainFieldset'>";
    g('loadPrompt').innerHTML+="<input id='verbox' type='password' /><input type='button' class='miniButton' style='margin-left:5px;' onclick='verifyOldPassword();' value='OK'>";
    g('loadPrompt').innerHTML+="</fieldset></form>";
    g('verbox').focus();
  }
  else {
    g('loadPrompt').innerHTML="Please enter the <strong>NEW</strong> password.<br /><br />";
    g('loadPrompt').innerHTML+="<form onsubmit='setNewPassword();return false;' style='margin:5px auto;' action=''><fieldset class='notMainFieldset'>";
    g('loadPrompt').innerHTML+="<input id='newbox' type='password' /><input type='button' class='miniButton' style='margin-left:5px;' onclick='setNewPassword();' value='OK'>";
    g('loadPrompt').innerHTML+="</fieldset></form>";
    g('newbox').focus();
  }
}

// Verifies the old password before changing to a new one.
function verifyOldPassword() {
  var temp=getCookie().split(":SPLIT:");
  var q=temp[loaded].split(":PARSE:");
  
  // Checks old password to make sure it's the right one.
  if (q[4]!=g('verbox').value) {
    cancelLoad();
    g('loadPrompt').innerHTML="That password is incorrect.";
    return;
  }

  g('loadPrompt').innerHTML="Please enter the <strong>NEW</strong> password.<br /><br />";
  g('loadPrompt').innerHTML+="<form onsubmit='setNewPassword();return false;' style='margin:5px auto;' action=''><fieldset class='notMainFieldset'>";
  g('loadPrompt').innerHTML+="<input id='newbox' type='password' /><input type='button' class='miniButton' style='margin-left:5px;' onclick='setNewPassword();' value='OK'>";
  g('loadPrompt').innerHTML+="</fieldset></form>";
  g('newbox').focus();
}

// Sets the new password.
function setNewPassword() {
  var temp=getCookie().split(":SPLIT:");
  var q=temp[loaded].split(":PARSE:");
  
  var p=(q[0]+":PARSE:"+q[1]+":PARSE:"+q[2]+":PARSE:"+giveTime()+":PARSE:"+g('newbox').value); // New copy of save, with new password and timestamp.
  var toCookie="";
  
  /* Adds all saves to the variable "toCookie." When the current save is reached,
   * it is ignored and the new copy of it with a more current timestamp and
   * the new password in entered in its place.
   */
  for (i=0; i<temp.length-1; i++) {
    if (i!=loaded) {
      toCookie+=temp[i];
    }
    else {
      toCookie+=p;
    }
    toCookie+=":SPLIT:";
  }
  setCookie(toCookie);
  disableLoad(false);
  loadList();
  g('loadPrompt').innerHTML="\""+q[0]+"\" has had its password changed.";
}

// Renames selected save.
function rename() {
  if (loaded==-1) {
    g('loadPrompt').innerHTML="You haven't selected a file.";
    return;
  }
  var temp=getCookie().split(":SPLIT:");
  var q=temp[loaded].split(":PARSE:");
  
  disableLoad(true);
  g('loadPrompt').innerHTML="Do you want to rename \""+q[0]+"\"?<br /><br />";
  g('loadPrompt').innerHTML+="<input type='button' class='miniButton' style='margin:5px;' onclick='checkNameForPass();' value='Yes' />";
  g('loadPrompt').innerHTML+="<input type='button' class='miniButton' style='margin:5px;' onclick='cancelLoad();' value='No' />";
}

// Asks the user for the file's password before allowing rename.
function checkNameForPass() {
  var temp=getCookie().split(":SPLIT:");
  var q=temp[loaded].split(":PARSE:");
  
  // If password exists, prompts user for it.
  if (q[4]) {
    g('loadPrompt').innerHTML="Please enter the file's <strong>password</strong> to continue.<br /><br />";
    g('loadPrompt').innerHTML+="<form onsubmit='continueNamingFromPass();return false;' style='margin:5px auto;' action=''><fieldset class='notMainFieldset'>";
    g('loadPrompt').innerHTML+="<input id='verbox' type='password' /><input type='button' class='miniButton' style='margin-left:5px;' onclick='continueNamingFromPass();' value='OK'>";
    g('loadPrompt').innerHTML+="</fieldset></form>";
    g('verbox').focus();
  }
  else {
    g('loadPrompt').innerHTML="Please enter the file's <strong>NEW</strong> name.<br /><br /><form onsubmit='setNewName();return false;' style='margin:5px auto;'><input id='newbox' type='text' /><input type='button' class='miniButton' style='margin-left:5px;' onclick='setNewName();' value='OK'></form>";
    g('newbox').focus();
  }
}

// Verify file's password before allowing rename.
function continueNamingFromPass() {
  var temp=getCookie().split(":SPLIT:");
  var q=temp[loaded].split(":PARSE:");
  
  // Checks old password to make sure it's the right one.
  if (q[4]!=g('verbox').value) {
    cancelLoad();
    g('loadPrompt').innerHTML="That password is incorrect.";
    return;
  }
  g('loadPrompt').innerHTML="Please enter the file's <strong>NEW</strong> name.<br /><br />";
  g('loadPrompt').innerHTML+="<form onsubmit='setNewName();return false;' style='margin:5px auto;' action=''><fieldset class='notMainFieldset'>";
  g('loadPrompt').innerHTML+="<input id='newbox' type='text' /><input type='button' class='miniButton' style='margin-left:5px;' onclick='setNewName();' value='OK'>";
  g('loadPrompt').innerHTML+="</fieldset></form>";
  g('newbox').focus();
}

// Sets the new name of the file.
function setNewName() {
  var temp=getCookie().split(":SPLIT:");
  var q=temp[loaded].split(":PARSE:");
  
  var p=(g('newbox').value+":PARSE:"+q[1]+":PARSE:"+q[2]+":PARSE:"+giveTime()); // Contents of renamed save.
  
  // Adds password if one exists.
  if (q[4]) {
    p+=":PARSE:"+q[4];
  }
  var toCookie="";
  
  /* Cycles through all the saves. For all other saves, add
   * to the variable "toCookie." When it reaches the selected
   * save, enter the new value.
   */
  for (i=0; i<temp.length-1; i++) {
    if (i!=loaded) {
      toCookie+=temp[i];
    }
    else {
      toCookie+=p;
    }
    toCookie+=":SPLIT:";
  }
  setCookie(toCookie);
  g('loadPrompt').innerHTML="The file has been renamed.";
  loadList();
  disableLoad(false);
}