//Global Variables
var aoe_FileList_List;
var aoe_Footer;
var aoe_Filename;
var aoe_EditArea_Textbox;
var aoe_currentFile;
var aoe_files = new Array();
var aoe_changed = false;
var aoe_updateFileList = false;

//initialize editor
function initAjaxOnlineEditor()
{
  //connect Event-Handler to buttons
  document.getElementById("aoebtn_update").onclick = aoeLoadFileList;

  //reference to HTML elements
  aoe_FileList_List = document.getElementById("aoe_filelist_list");
  aoe_Footer = document.getElementById("aoe_footer");
  aoe_EditArea_Textbox = document.getElementById("aoe_editarea_textbox");
  aoe_Filename = document.getElementById("aoe_filename");

  //load filelist and preparing new file
  aoeLoadFileList();
  aoeNewFile();
}

//load filelist from server
function aoeLoadFileList()
{
  //update status message
  aoe_Footer.innerHTML = "<font style=\"color:#990000\"><b>calling file list from server...</b></font>";

  //calling getFileList.php via AJAX
  var myAjax = new Ajax();
  myAjax.url="php/getFileList.php";

  //processing the response / PHP script returns result
  myAjax.onSuccess = function(txt,xml) {

    //did the PHP script return an error message??
    if (aoeHasError(txt)) {
      myAjax.onError(aoeGetError(txt));
      return null;
    }

    //if filelist has been delivered, change filelist to array
    if (txt) {
      aoe_files = txt.split("\n");
    }

    //display Array files in the Editor
    aoeCreateFileList();

    //update status message
    aoe_Footer.innerHTML = "<font style=\"color:#000099\"><b>file list has been transmitted, "+aoe_files.length+" files found</b></font>";
  }

  //error while transmitting filelist
  myAjax.onError = aoeErrorHandler;

  //calling filelist
  myAjax.doRequest();
}


//translare Array files to HTML elements
function aoeCreateFileList()
{
 //delete present filelist
 aoe_FileList_List.innerHTML = "";

 //sort Array files
 aoe_files.sort();

 //iterate through Array and create HTML elements
 for (i=0; i<aoe_files.length; i++) {

   //create new li element and add to aoe_FileList_List
   var myLi = document.createElement("li");
   aoe_FileList_List.appendChild(myLi);

   //create new a element
   var myA = document.createElement("a");

   //text node / which text should be shown?
   var myText = document.createTextNode(aoe_files[i]);
   myA.appendChild(myText);
   myA.setAttribute("href","javascript:void(0)");
   myA.onclick=aoeLoadFile;

   //appende li by a element
   myLi.appendChild(myA);
 }
}

//load file content from server
function aoeLoadFile()
{
  //the filelist does not have to be updated afterwards
  aoe_updateFileList = false;

  //which file has been selected / should it be loaded?
  aoe_currentFile = this.firstChild.nodeValue;

  //update status display
  aoe_Footer.innerHTML = "<font style=\"color:#990000\"><b>loading "+aoe_currentFile+" ...</b></font>";

  //calling loadFile.php via AJAX
  var myAjax = new Ajax();
  myAjax.url="php/loadFile.php";

  //filename has been submitted to PHP script as parameter
  myAjax.params="filename="+aoe_currentFile;

  //processing response / PHP script returns result
  myAjax.onSuccess = function(txt,xml) {

    //did the PHP script return an error message?
    if (aoeHasError(txt)) {
      myAjax.onError(aoeGetError(txt));
      return null;
    }

    //call has been successfull
    //display filecontent, update status- and filename display
    aoe_EditArea_Textbox.value=txt;
    aoe_Filename.innerHTML = aoe_currentFile;
    aoe_Footer.innerHTML = "<font style=\"color:#000099\"><b>"+aoe_currentFile+" has been loaded</b></font>";
  }

  //error during transmission of filecontent
  myAjax.onError = aoeErrorHandler;

  //calling file
  myAjax.doRequest();
}

//does the file exist in the filelist?
function aoeFileExists(file)
{
  for(i=0; i<aoe_files.length; i++) {
   if (aoe_files[i] == file) {
     return true; //files does already exist
   }
  }
}

//checks return value of a PHP script, if it is an error message
function aoeHasError(msg)
{
  //does msg have content?
  if (!msg) return false;

  //is there a string namesd AOEERROR: at the beginning of msg?
  if (msg.indexOf("AOEERROR:") == 0) {
    return true;
  } else {
    return false;
  }
}

//preparing the error message of a PHP script
function aoeGetError(msg)
{
  //remove AOEERROR: from error message
  return msg.replace(/AOEERROR:/, "");
}

//general function for all error cases
function aoeErrorHandler(msg)
{
  //display error as alert message window
  alert(msg);

  //write error into status display
  aoe_Footer.innerHTML = msg;
}

window.onload=initAjaxOnlineEditor;
