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

// Sets up the save window.
function showSave() {
  g('savePrompt').innerHTML="<h3>Save Menu</h3>";
  showWindow('saveWindow');
}

// Toggles the disabled status of the password box.
function toggleDis() {
  if (!g('passBox').checked) {
    g('passEnter').disabled='disabled';
    g('passEnter').value='';
  }
  else {
    g('passEnter').disabled=false;
    g('passEnter').focus();
  }
}

// Saves the current data in a cookie.
function validateSave() {

  var encoded=g('encoder').value;
  var decoded=g('decoder').value;
  var saveName=g('saveInput').value;
  main=g('mainWin');
  var saveData;
  
  /* If there's no saved data, create it anew. If there's
   * already something there, add the current window to the end.
   */
  if (!getCookie() || getCookie()=='' || getCookie()=='undefined') {
    saveData=saveName+":PARSE:"+encoded+":PARSE:"+decoded;
  }
  else {
    saveData=getCookie()+saveName+":PARSE:"+encoded+":PARSE:"+decoded;
  }
  
  // Adds the current time.
  saveData+=":PARSE:"+giveTime();
  
  // Checks to see if the user wanted a password.
  if (g('passEnter').value!="") {
    var pass=g('passEnter').value;
    saveData+=":PARSE:"+pass;
  }
  
  // Prevents the user from using :PARSE: or :SPLIT: in any text field, because that would mess everything up.
  if (saveName.match(":PARSE:")||saveName.match(":SPLIT:")||g('passEnter').value.match(":SPLIT:")||g('passEnter').value.match(":PARSE:")||g('encoder').value.match(":PARSE:")||g('encoder').value.match(":SPLIT:")||g('decoder').value.match(":PARSE:")||g('decoder').value.match(":SPLIT:")) {
    g('savePrompt').innerHTML="This file can't be saved because it contains either <strong>:SPLIT:</strong> or <strong>:PARSE:</strong><br /><br />These keywords are reserved.";
    return;
  }
  
  try {
    activeFile=getCookie().split(":SPLIT:").length-1;
  }
  catch (e) {
    activeFile=0; // No saved data exists.
  }
  
  setCookie(saveData+":SPLIT:");
  g('saveInput').value='';
  g('passEnter').value='';
  g('passEnter').disabled="disabled";
  g('passBox').checked=false;
  turnOff('saveWindow');
  turnOn('mainWin');
  (main.filters)?main.filters.alpha.opacity=100:main.style.opacity=1;
  g('promptSpacer').style.display="none";
}

function pureSave() {
  // File has never been saved.
  if (activeFile<0) {
    showSave();
    return;
  }
  
  var saveData=g('encoder').value+":PARSE:"+g('decoder').value;
  var files=getCookie().split(":SPLIT:");
  var finalString="";
  var tempFile="";
  
  for (i=0; i<files.length-1; i++) {
    // Replace older version of file.
    if (i==activeFile) {
      tempFile+=files[i].split(":PARSE:")[0]+":PARSE:"+saveData+":PARSE:"+giveTime();
      if (files[i].split(":PARSE:")[4]) {
        tempFile+=":PARSE:"+files[i].split(":PARSE:")[4];
      }
    }
    else {
      finalString+=files[i]+":SPLIT:";
    }
  }
  finalString+=tempFile+":SPLIT:"; // Add changed file at end.
  
  try {
    activeFile=getCookie().split(":SPLIT:").length-2;
  }
  catch (e) {
    activeFile=0; // No saved data exists.
  }
  setCookie(finalString);
  in_transit=false;
}