]> matita.cs.unibo.it Git - helm.git/blob - matitaB/matita/matitaweb.js
ec19562b248e819a0ab1bb3fb25e35aa59c318d1
[helm.git] / matitaB / matita / matitaweb.js
1 var locked;
2 var unlocked;
3 var workarea;
4 var scriptcell;
5 var goalcell;
6 var goals;
7 var goalview;
8 var filename;
9 var logarea;
10 var advanceButton;
11 var retractButton;
12 var cursorButton;
13 var bottomButton;
14 var dialogBox;
15 var dialogTitle;
16 var dialogContent;
17 var metasenv = "";
18 var lockedbackup = "";
19
20 function initialize()
21 {
22   if (readCookie("session") == null) {
23     window.location = "/login.html"
24   } else {
25     locked = document.getElementById("locked");
26     unlocked = document.getElementById("unlocked");
27     workarea = document.getElementById("workarea");
28     scriptcell = document.getElementById("scriptcell");
29     goalcell = document.getElementById("goalcell");
30     goals = document.getElementById("goals");
31     goalview = document.getElementById("goalview");
32     filename = document.getElementById("filename");
33     logarea = document.getElementById("logarea");
34     advanceButton = document.getElementById("advance");
35     retractButton = document.getElementById("retract");
36     cursorButton = document.getElementById("cursor");
37     bottomButton = document.getElementById("bottom");
38     dialogBox = document.getElementById("dialogBox");
39     dialogTitle = document.getElementById("dialogTitle");
40     dialogContent = document.getElementById("dialogContent");
41   
42     // hide sequent view at start
43     hideSequent();
44
45     // initialize keyboard events in the unlocked script
46     init_keyboard(unlocked);
47   }
48 }
49
50 function init_keyboard(target)
51 {
52     if (target.addEventListener)
53     {
54 //       target.addEventListener("keydown",keydown,false);
55        target.addEventListener("keypress",keypress,false);
56 //       target.addEventListener("keyup",keyup,false);
57 //       target.addEventListener("textInput",textinput,false);
58     }
59     else if (target.attachEvent)
60     {
61 //       target.attachEvent("onkeydown", keydown);
62        target.attachEvent("onkeypress", keypress);
63 //       target.attachEvent("onkeyup", keyup);
64 //       target.attachEvent("ontextInput", textinput);
65     }
66     else
67     {
68 //       target.onkeydown= keydown;
69        target.onkeypress= keypress;
70 //       target.onkeyup= keyup;
71 //       target.ontextinput= textinput;   // probably doesn't work
72     }
73  
74 }
75
76 function keyval(n)
77 {
78     if (n == null) return 'undefined';
79     var s= '' + n;
80     if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
81     while (s.length < 9) s+= ' ';
82     return s;
83 }
84  
85 function string_of_key(n)
86 {
87     if (n == null) return 'undefined';
88     return String.fromCharCode(n);
89 }
90
91 function pressmesg(w,e)
92 {
93    debug(w + '  keyCode=' + keyval(e.keyCode) +
94                  ' which=' + keyval(e.which) +
95                  ' charCode=' + keyval(e.charCode) +
96                  '\n          shiftKey='+e.shiftKey
97               + ' ctrlKey='+e.ctrlKey
98               + ' altKey='+e.altKey
99               + ' metaKey='+e.metaKey);
100 }
101  
102 function suppressdefault(e,flag)
103 {
104    if (flag)
105    {
106        if (e.preventDefault) e.preventDefault();
107        if (e.stopPropagation) e.stopPropagation();
108    }
109    return !flag;
110 }
111
112 function restoreSelection(adjust) {
113     unlocked.focus();
114     if (savedRange != null) {
115         if (window.getSelection)//non IE and there is already a selection
116         {
117             var s = window.getSelection();
118             if (s.rangeCount > 0) 
119                 s.removeAllRanges();
120             range = document.createRange();
121             range.setStart(savedsc,savedso + adjust);
122             range.collapse(true);
123             s.addRange(range);
124         }
125         else 
126             if (document.createRange)//non IE and no selection
127             {
128                 window.getSelection().addRange(savedRange);
129             }
130             else 
131                 if (document.selection)//IE
132                 {
133                     savedRange.select();
134                 }
135     }
136 }
137
138 function lookup_tex(texmacro)
139 {
140   texmacro = texmacro.substring(1);
141   return unescape(macro2utf8[texmacro]);
142 }
143  
144 function keypress(e)
145 {
146    if (!e) e= event;
147    pressmesg('keypress',e);
148    var s = string_of_key(e.charCode);
149    if (s == " ") {
150         j = getCursorPos();
151         i = unlocked.innerHTML.lastIndexOf('\\',j);
152         if (i >= 0) {
153           match = unlocked.innerHTML.substring(i,j);
154           pre = unlocked.innerHTML.substring(0,i);
155           post = unlocked.innerHTML.substring(j);
156           
157           sym = lookup_tex(match);
158           if (typeof sym != "undefined") {
159              len1 = unlocked.innerText.length;
160              unlocked.innerHTML = pre + sym + post;
161              len2 = unlocked.innerText.length;
162              restoreSelection(len2 - len1); 
163              return suppressdefault(e,true);
164           }
165           else {
166              // restoreSelection(0); 
167              return suppressdefault(e,false);
168           }
169         }
170         else return suppressdefault(e,false);
171    } else {
172         return suppressdefault(e,false);
173    }
174 }
175  
176 function debug(txt)
177 {
178         // internet explorer (v.9) doesn't work with innerHTML
179         // but google chrome's innerText is, in a sense, "write only"
180         // what should we do?
181         // logarea.innerText = txt + "\n" + logarea.innerText;
182         logarea.innerHTML = txt; // + "\n" + logarea.innerText;
183 }
184
185 function listhd(l)
186 {
187         ar = l.split("#");
188         debug("hd of '" + l + "' = '" + ar[0] + "'");
189         return (ar[0]);
190 }
191
192 function listtl(l)
193 {
194         i = l.indexOf("#");
195         tl = l.substr(i+1);
196         debug("tl of '" + l + "' = '" + tl + "'");
197         return (tl);
198 }
199
200 function listcons(x,l)
201 {
202         debug("cons '" + x + "' on '" + l + "'");
203         return (x + "#" + l);
204 }
205
206 function listnil()
207 {
208         return ("");
209 }
210
211 function is_nil(l)
212 {
213         return (l == "");
214 }
215
216 function fold_left (f,acc,l)
217 {
218         if (is_nil(l))
219            { debug("'" + l + "' is fold end");
220            return (acc); }
221         else
222            { debug("'" + l + "' is fold cons");
223              return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
224 }
225
226 function listiter (f,l)
227 {
228         if (is_nil(l))
229         { debug("'" + l + "' is nil");
230            return;
231         }
232         else
233         {
234            debug("'" + l + "' is not nil");
235            f(listhd(l));
236            listiter(f,listtl(l));
237         }
238 }
239
240 function listmap (f,l)
241 {
242         debug("listmap on " + l);
243         if (is_nil(l)) 
244            { debug("returning listnil");
245              return(listnil());
246            }
247         else 
248            { debug("cons f(hd) map(f,tl)");
249              return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
250            }
251 }
252
253 var statements = listnil();
254
255 var goalarray;
256 var metalist = listnil();
257
258 function pairmap (f,p)
259 {
260   debug("pairmap of '" + p + "'");
261   ar = p.split("|");
262   return (f(ar[0],ar[1])); 
263 }
264
265 function tripletmap (f,p)
266 {
267   debug("tripletmap of '" + p + "'");
268   ar = p.split("|");
269   return (f(ar[0],ar[1],ar[2])); 
270 }
271
272 function fst (p)
273 {
274   debug("fst");
275   return (pairmap (function (a,b) { return (a); }, p));
276 }
277
278 function p13 (p)
279 {
280   debug("p13");
281   return (tripletmap (function (a,b,c) { return (a); }, p));
282 }
283
284 function p23 (p)
285 {
286   debug("p23");
287   return (tripletmap (function (a,b,c) { return (b); }, p));
288 }
289
290 function p33 (p)
291 {
292   debug("f33");
293   return (tripletmap (function (a,b,c) { return (c); }, p));
294 }
295
296 function populate_goalarray(menv)
297 {
298   debug("metasenv.length = " + menv.length);
299   if (menv.length == 0) {
300       try {
301           hideSequent();
302       } catch (err) { };
303   } else {
304       showSequent();
305       goalarray = new Array();
306       metalist = listnil();
307       var tmp_goallist = "";
308       for (i = 0; i < menv.length; i++) {
309         metano = menv[i].getAttribute("number");
310         metaname = menv[i].childNodes[0].childNodes[0].data;
311         goal = menv[i].childNodes[1].childNodes[0].data;
312         debug ("found meta n. " + metano);
313         debug ("found goal\nBEGIN" + goal + "\nEND");
314         goalarray[metano] = goal;
315         tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
316         metalist = listcons(metano,metalist);
317         debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]); 
318       }
319       goals.innerHTML = tmp_goallist;
320       debug("new metalist is '" + metalist + "'");
321       if (is_nil(metalist)) {
322         switch_goal();
323       }
324       else {
325         switch_goal(listhd(metalist));
326       }
327   }
328 }
329
330 function switch_goal(meta)
331 {
332   if (typeof meta == "undefined") {
333     goalview.innerHTML = "";
334   }
335   else {
336     debug("switch_goal " + meta + "\n" + goalarray[meta]);
337     goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
338   }
339 }
340
341 // the following is used to avoid escaping unicode, which results in 
342 // the server being unable to unescape the string
343 String.prototype.sescape = function() {
344         var patt1 = /%/gi;
345         var patt2 = /=/gi;
346         var patt3 = /&/gi;
347         var patt4 = /\+/gi;
348         var result = this;
349         result = result.replace(patt1,"%25");
350         result = result.replace(patt2,"%3D");
351         result = result.replace(patt3,"%26");
352         result = result.replace(patt4,"%2B");
353         return (result);
354 }
355
356 String.prototype.html_to_matita = function()
357 {
358         var patt1 = /<br(\/|)>/gi;
359         var patt2 = /</gi
360         var patt3 = />/gi
361         var patt4 = /&lt;/gi;
362         var patt5 = /&gt;/gi;
363         var result = this;
364         result = result.replace(patt1,"\n");
365         result = result.replace(patt2,"\005");
366         result = result.replace(patt3,"\006");
367         result = result.replace(patt4,"<");
368         result = result.replace(patt5,">");
369         return (unescape(result));
370 }
371
372 String.prototype.matita_to_html = function()
373 {
374         var patt1 = /</gi
375         var patt2 = />/gi
376         var patt3 = /\005/gi;
377         var patt4 = /\006/gi;
378         var result = this;
379         result = result.replace(patt1,"&lt;");
380         result = result.replace(patt2,"&gt;");
381         result = result.replace(patt3,"<");
382         result = result.replace(patt4,">");
383         return (unescape(result));
384 }
385
386 function pause()
387 {
388         advanceButton.disabled = true;
389         retractButton.disabled = true;
390         cursorButton.disabled = true;
391         bottomButton.disabled = true;
392 }
393
394 function resume()
395 {
396         advanceButton.disabled = false;
397         retractButton.disabled = false;
398         cursorButton.disabled = false;
399         bottomButton.disabled = false;
400 }
401
402 function is_defined(x)
403 {
404         return (typeof x != "undefined");
405 }
406
407 /* servicename: name of the service being called
408  * reqbody: text of the request
409  * processResponse: processes the server response
410  *     (takes the response text in input, undefined in case of error)
411  */
412 function callServer(servicename,processResponse,reqbody)
413 {
414         var req = null; 
415         // pause();
416         if (window.XMLHttpRequest)
417         {
418                 req = new XMLHttpRequest();
419         } 
420         else if (window.ActiveXObject) 
421         {
422                 try {
423                                 req = new ActiveXObject("Msxml2.XMLHTTP");
424                 } catch (e)
425                 {
426                         try {
427                                 req = new ActiveXObject("Microsoft.XMLHTTP");
428                                 } catch (e) {}
429                 }
430         }
431         req.onreadystatechange = function()
432         { 
433
434                 rs = req.readyState;
435
436                 if(rs == 4)
437                 {
438                         stat = req.status;
439                         stxt = req.statusText;
440                         if(stat == 200)
441                         {
442                           debug(req.responseText);
443                           if (window.DOMParser) {
444                             parser=new DOMParser();
445                             xmlDoc=parser.parseFromString(req.responseText,"text/xml");
446                           }
447                           else // Internet Explorer
448                           {
449                             xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
450                             xmlDoc.async="false";
451                             xmlDoc.loadXML(req.responseText);
452                           }     
453                           processResponse(xmlDoc);
454                         } else {
455                           processResponse();
456                         }
457                 } 
458         };
459         req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
460         req.setRequestHeader("Content-type","application/x-www-form-urlencoded");       
461         if (reqbody) {
462                 req.send(reqbody); 
463         } else {
464                 req.send();
465         }
466   
467 }
468
469 function advanceForm1()
470 {
471         processor = function(xml) {
472                 if (is_defined(xml)) {
473                         // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
474                         parsed = xml.getElementsByTagName("parsed")[0];
475                         len = parseInt(parsed.getAttribute("length"));
476                         // len0 = unlocked.innerHTML.length;
477                         unescaped = unlocked.innerHTML.html_to_matita();
478                         parsedtxt = parsed.childNodes[0].nodeValue;
479                         //parsedtxt = unescaped.substr(0,len); 
480                         unparsedtxt = unescaped.substr(len);
481                         lockedbackup += parsedtxt;
482                         locked.innerHTML = lockedbackup;
483                         unlocked.innerHTML = unparsedtxt.matita_to_html();
484                         // len1 = unlocked.innerHTML.length;
485                         // len2 = len0 - len1;
486                         len2 = parsedtxt.length;
487                         metasenv = xml.getElementsByTagName("meta");
488                         populate_goalarray(metasenv);
489                         statements = listcons(len2,statements);
490                         unlocked.scrollIntoView(true);
491                 } else {
492                         debug("advance failed");
493                 }
494                 resume();
495         };
496         pause();
497         callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
498   
499 }
500
501 function gotoBottom()
502 {
503         processor = function(xml) {
504                 if (is_defined(xml)) {
505                         // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
506                         parsed = xml.getElementsByTagName("parsed")[0];
507                         len = parseInt(parsed.getAttribute("length"));
508                         if (len > 0) {
509                           // len0 = unlocked.innerHTML.length;
510                           unescaped = unlocked.innerHTML.html_to_matita();
511                           // not working in mozilla
512                           // parsedtxt = parsed.childNodes[0].nodeValue;
513                           parsedtxt = parsed.childNodes[0].wholeText;
514                           //parsedtxt = unescaped.substr(0,len); 
515                           unparsedtxt = unescaped.substr(len);
516                           lockedbackup += parsedtxt;
517                           locked.innerHTML = lockedbackup; //.matita_to_html();
518                           unlocked.innerHTML = unparsedtxt.matita_to_html();
519                           // len1 = unlocked.innerHTML.length;
520                           len2 = parsedtxt.length;
521                           metasenv = xml.getElementsByTagName("meta");
522                           populate_goalarray(metasenv);
523                           if (len2 > 0)
524                             statements = listcons(len2,statements);
525                           unlocked.scrollIntoView(true);
526                         }
527                 } else {
528                         debug("goto bottom failed");
529                 } 
530                 resume();
531         };
532         pause();
533         callServer("bottom",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
534   
535 }
536
537
538 function gotoPos(offset)
539 {
540         if (!is_defined(offset)) {
541                 offset = getCursorPos();
542         }
543         processor = function(xml) {
544                 if (is_defined(xml)) {
545                         parsed = xml.getElementsByTagName("parsed")[0];
546                         len = parseInt(parsed.getAttribute("length"));
547                         // len0 = unlocked.innerHTML.length;
548                         unescaped = unlocked.innerHTML.html_to_matita();
549                         parsedtxt = parsed.childNodes[0].nodeValue;
550                         //parsedtxt = unescaped.substr(0,len); 
551                         unparsedtxt = unescaped.substr(len);
552                         lockedbackup += parsedtxt;
553                         locked.innerHTML = lockedbackup; //.matita_to_html();
554                         unlocked.innerHTML = unparsedtxt.matita_to_html();
555                         // len1 = unlocked.innerHTML.length;
556                         len2 = parsedtxt.length;
557                         metasenv = xml.getElementsByTagName("meta");
558                         // populate_goalarray(metasenv);
559                         statements = listcons(len2,statements);
560                         unlocked.scrollIntoView(true);
561                         // la populate non andrebbe fatta a ogni passo
562                         if (offset <= len) {
563                                 populate_goalarray(metasenv);
564                                 resume();
565                         } else {
566                                 gotoPos(offset - len);
567                         }
568                 } else {
569                         unlocked.scrollIntoView(true);
570                         populate_goalarray(metasenv);
571                         resume();
572                 }
573         }
574         pause();
575         callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
576 }
577
578 function retract()
579 {
580         processor = function(xml) {
581                 if (typeof xml != "undefined") {
582                         // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
583                         statementlen = parseInt(listhd(statements));
584                         statements = listtl(statements);
585                         /*
586                         lockedlen = locked.innerHTML.length - statementlen;
587                         statement = locked.innerHTML.substr(lockedlen, statementlen);
588                         locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
589                         unlocked.innerHTML = statement + unlocked.innerHTML;
590                         */
591                         lockedlen = lockedbackup.length - statementlen;
592                         statement = lockedbackup.substr(lockedlen, statementlen);
593                         lockedbackup = lockedbackup.substr(0,lockedlen);
594                         locked.innerHTML = lockedbackup;
595                         unlocked.innerHTML = statement + unlocked.innerHTML;
596                         metasenv = xml.getElementsByTagName("meta");
597                         populate_goalarray(metasenv);
598                         unlocked.scrollIntoView(true);
599                 } else {
600                         debug("retract failed");
601                 }
602                 resume();
603         };
604         debug("retract 1");
605         pause();
606         callServer("retract",processor);
607         debug("retract 2");
608 }
609
610 function openFile()
611
612         processor = function(xml)
613         {
614                 if (is_defined(xml)) {  
615                         lockedbackup = "";
616                         locked.innerHTML = lockedbackup;
617                         unlocked.innerHTML = xml.documentElement.wholeText;
618                 } else {
619                         debug("file open failed");
620                 }
621         };
622         callServer("open",processor,"file=" + escape(filename.value)); 
623 }
624
625 function retrieveFile(thefile)
626
627         processor = function(xml)
628         {
629                 if (is_defined(xml)) {  
630                         lockedbackup = ""
631                         locked.innerHTML = lockedbackup;
632                         // code originally used in google chrome (problems with mozilla)
633                         // debug(xml.getElementsByTagName("file")[0].childNodes[0].nodeValue);
634                         // unlocked.innerHTML = xml.getElementsByTagName("file")[0].childNodes[0].nodeValue;
635                         debug(xml.childNodes[0].textContent);
636                         unlocked.innerHTML = xml.childNodes[0].textContent;
637
638                 } else {
639                         debug("file open failed");
640                 }
641         };
642         dialogBox.style.display = "none";
643         current_fname = thefile;
644         callServer("open",processor,"file=" + escape(thefile)); 
645 }
646
647 function showLibrary(title,callback)
648
649         var req = null; 
650         // pause();
651         if (window.XMLHttpRequest)
652         {
653                 req = new XMLHttpRequest();
654         } 
655         else if (window.ActiveXObject) 
656         {
657                 try {
658                                 req = new ActiveXObject("Msxml2.XMLHTTP");
659                 } catch (e)
660                 {
661                         try {
662                                 req = new ActiveXObject("Microsoft.XMLHTTP");
663                                 } catch (e) {}
664                 }
665         }
666         req.onreadystatechange = function()
667         { 
668
669                 rs = req.readyState;
670
671                 if(rs == 4)
672                 {
673                         stat = req.status;
674                         stxt = req.statusText;
675                         if(stat == 200)
676                         {
677                           debug(req.responseText);
678                           showDialog("<H2>" + title + "</H2>",req.responseText, callback);
679                         } 
680                 } 
681         };
682         req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
683         req.setRequestHeader("Content-type","application/x-www-form-urlencoded");       
684         req.send();
685   
686 }
687
688 function openDialog()
689 {  
690         callback = function (fname) { retrieveFile(fname); };
691         showLibrary("Open file", callback);
692 }
693
694 function saveDialog()
695 {  
696         callback = function (fname) { 
697           dialogBox.style.display = "none";
698           saveFile(fname,
699                    (locked.innerHTML.html_to_matita()).sescape(),
700                    (unlocked.innerHTML.html_to_matita()).sescape(),
701                    false,saveDialog); 
702         };
703         showLibrary("Save file as", callback);
704 }
705
706 function newDialog()
707 {
708         callback = function (fname) { 
709           dialogBox.style.display = "none";
710           saveFile(fname,"","",false,newDialog);
711           retrieveFile(fname);
712         };
713         showLibrary("Create new file", callback);
714 }
715
716
717 function saveFile(fname,lockedtxt,unlockedtxt,force,reloadDialog)
718 {
719         if (!is_defined(fname)) {
720             fname = current_fname;
721             lockedtxt = (locked.innerHTML.html_to_matita()).sescape();
722             unlockedtxt = (unlocked.innerHTML.html_to_matita()).sescape();
723             force = true;
724             // when force is true, reloadDialog is not needed 
725         }
726         processor = function(xml) {
727                 if (is_defined(xml)) {
728                   if (xml.childNodes[0].textContent != "ok") {
729                     if (confirm("File already exists. All existing data will be lost.\nDo you want to proceed anyway?")) {
730                        saveFile(fname,lockedtxt,unlockedtxt,true);
731                     } else {
732                       reloadDialog();
733                     }
734                   } else {
735                     current_fname = fname;
736                     debug("file saved!");
737                   }
738                 } else {
739                         debug("save file failed");
740                 }
741                 resume();
742         };
743         if (is_defined(fname)) {
744           pause();
745           callServer("save",processor,"file=" + escape(fname) + 
746                                     "&locked=" + lockedtxt +
747                                     "&unlocked=" + unlockedtxt +
748                                     "&force=" + force);
749         }
750         else { debug("no file selected"); }
751 }
752
753 function commitAll()
754 {
755         processor = function(xml) {
756                 if (is_defined(xml)) {
757                         debug("commit succeeded(?)");
758                 } else {
759                         debug("commit failed!");
760                 }
761                 resume();
762         };
763         pause();
764         callServer("commit",processor);
765 }
766
767 var goalcell;
768
769 function hideSequent() {
770   goalcell.parentNode.removeChild(goalcell);
771   scriptcell.setAttribute("colspan","2");
772 }
773
774 function showSequent() {
775   scriptcell.setAttribute("colspan","1");
776   workarea.appendChild(goalcell);
777 }
778
779 function showDialog(title,content,callback) {
780   dialogTitle.innerHTML = title;
781   dialogContent.innerHTML = content;
782   dialogBox.style.display = "block";
783   dialogBox.callback = callback;
784 }
785
786 function removeElement(id) {
787   var element = document.getElementById(id);
788   element.parentNode.removeChild(element);
789
790
791 var savedsc;
792 var savedso;
793
794 function getCursorPos() {
795   var cursorPos;
796   if (window.getSelection) {
797     var selObj = window.getSelection();
798     savedRange = selObj.getRangeAt(0);
799     savedsc = savedRange.startContainer;
800     savedso = savedRange.startOffset;
801     //cursorPos =  findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
802     cursorPos =  findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
803     /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
804     return(cursorPos);
805   }
806   else if (document.selection) {
807     savedRange = document.selection.createRange();
808     var bookmark = savedRange.getBookmark();
809     /* FIXME the following works wrong when the document is longer than 65535 chars */
810     cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
811     return(cursorPos);
812   }
813 }
814
815 function findNode(list, node, acc) {
816   for (var i = 0; i < list.length; i++) {
817     if (list[i] == node) {
818    //   debug("success " + i);
819       return acc;
820     }
821     if (list[i].hasChildNodes()) {
822        try {
823    //      debug("recursion on node " + i);
824          return (findNode(list[i].childNodes,node,acc))
825        }
826        catch (e) { /* debug("recursion failed"); */ }
827     }
828     sandbox = document.getElementById("sandbox");
829     dup = list[i].cloneNode(true);
830     sandbox.appendChild(dup);
831 //    debug("fail " + i + ": " + sandbox.innerHTML);
832     acc += sandbox.innerHTML.length;
833     sandbox.removeChild(dup);
834   }
835   throw "not found";
836 }
837
838 function test () {
839   debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
840 }
841
842 function readCookie(name) {
843         var nameEQ = name + "=";
844         var ca = document.cookie.split(';');
845         for(var i=0;i < ca.length;i++) {
846                 var c = ca[i];
847                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
848                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
849         }
850         return null;
851 }
852
853 function delete_cookie ( cookie_name )
854 {
855   var cookie_date = new Date();  // current date & time
856   cookie_date.setTime ( cookie_date.getTime() - 1 );
857   document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
858 }
859
860 function delete_session()
861 {
862         delete_cookie("session");
863 }