+function retrieveFile(thefile)
+{
+ processor = function(xml)
+ {
+ if (is_defined(xml)) {
+ lockedbackup = ""
+ locked.innerHTML = lockedbackup;
+ // code originally used in google chrome (problems with mozilla)
+ // debug(xml.getElementsByTagName("file")[0].childNodes[0].nodeValue);
+ // unlocked.innerHTML = xml.getElementsByTagName("file")[0].childNodes[0].nodeValue;
+ debug(xml.childNodes[0].textContent);
+ unlocked.innerHTML = xml.childNodes[0].textContent;
+
+ } else {
+ debug("file open failed");
+ }
+ };
+ dialogBox.style.display = "none";
+ current_fname = thefile;
+ callServer("open",processor,"file=" + escape(thefile));
+}
+
+function showLibrary(title,callback)
+{
+ var req = null;
+ // pause();
+ if (window.XMLHttpRequest)
+ {
+ req = new XMLHttpRequest();
+ }
+ else if (window.ActiveXObject)
+ {
+ try {
+ req = new ActiveXObject("Msxml2.XMLHTTP");
+ } catch (e)
+ {
+ try {
+ req = new ActiveXObject("Microsoft.XMLHTTP");
+ } catch (e) {}
+ }
+ }
+ req.onreadystatechange = function()
+ {
+
+ rs = req.readyState;
+
+ if(rs == 4)
+ {
+ stat = req.status;
+ stxt = req.statusText;
+ if(stat == 200)
+ {
+ debug(req.responseText);
+ showDialog("<H2>" + title + "</H2>",req.responseText, callback);
+ }
+ }
+ };
+ req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
+ req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
+ req.send();
+
+}
+
+function openDialog()
+{
+ callback = function (fname) { retrieveFile(fname); };
+ showLibrary("Open file", callback);
+}
+
+function saveDialog()
+{
+ callback = function (fname) {
+ dialogBox.style.display = "none";
+ saveFile(fname,
+ (locked.innerHTML.html_to_matita()).sescape(),
+ (unlocked.innerHTML.html_to_matita()).sescape(),
+ false,saveDialog);
+ };
+ showLibrary("Save file as", callback);
+}
+
+function newDialog()
+{
+ callback = function (fname) {
+ dialogBox.style.display = "none";
+ saveFile(fname,"","",false,newDialog);
+ retrieveFile(fname);
+ };
+ showLibrary("Create new file", callback);
+}
+
+
+function saveFile(fname,lockedtxt,unlockedtxt,force,reloadDialog)
+{
+ if (!is_defined(fname)) {
+ fname = current_fname;
+ lockedtxt = (locked.innerHTML.html_to_matita()).sescape();
+ unlockedtxt = (unlocked.innerHTML.html_to_matita()).sescape();
+ force = true;
+ // when force is true, reloadDialog is not needed
+ }
+ processor = function(xml) {
+ if (is_defined(xml)) {
+ if (xml.childNodes[0].textContent != "ok") {
+ if (confirm("File already exists. All existing data will be lost.\nDo you want to proceed anyway?")) {
+ saveFile(fname,lockedtxt,unlockedtxt,true);
+ } else {
+ reloadDialog();
+ }
+ } else {
+ current_fname = fname;
+ debug("file saved!");
+ }
+ } else {
+ debug("save file failed");
+ }
+ resume();
+ };
+ if (is_defined(fname)) {
+ pause();
+ callServer("save",processor,"file=" + escape(fname) +
+ "&locked=" + lockedtxt +
+ "&unlocked=" + unlockedtxt +
+ "&force=" + force);
+ }
+ else { debug("no file selected"); }
+}
+
+function commitAll()
+{
+ processor = function(xml) {
+ if (is_defined(xml)) {
+ debug("commit succeeded(?)");
+ } else {
+ debug("commit failed!");
+ }
+ resume();
+ };
+ pause();
+ callServer("commit",processor);
+}
+