]> matita.cs.unibo.it Git - helm.git/blob - matitaB/matita/matitaweb.js
Matitaweb:
[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 var matita;
20
21 function text_of_html(h)
22 {
23   if(document.all) {
24      return h.innerText;
25   } else {
26      return h.textContent;
27   }
28 }
29
30 function unescape_html(s)
31 {
32   u = document.getElementById("unescape");
33   u.innerHTML = s;
34   return text_of_html(u)
35 }
36
37 function filterByClass (elements,cname){
38   var itemsfound = new Array;
39   for(var i=0;i<elements.length;i++){
40     if(elements[i].className == cname){
41       itemsfound.push(elements[i]);
42     }
43   }
44   return itemsfound;
45 }
46
47 function initialize()
48 {
49   if (readCookie("session") == null) {
50     window.location = "/login.html"
51   } else {
52     body = document.body;
53     titlebar = document.getElementById("titlebar");
54     matitaTitle = document.getElementById("matitaTitle");
55     apparea = document.getElementById("matitaapparea");
56     locked = document.getElementById("locked");
57     unlocked = document.getElementById("unlocked");
58     toparea = document.getElementById("toparea");
59     workarea = document.getElementById("workarea");
60     scriptcell = document.getElementById("scriptcell");
61     sidearea = document.getElementById("sidearea");
62     disambcell = document.getElementById("disambcell");
63     goalcell = document.getElementById("goalcell");
64     goals = document.getElementById("goals");
65     goalview = document.getElementById("goalview");
66     filename = document.getElementById("filename");
67     logarea = document.getElementById("logarea");
68     advanceButton = document.getElementById("advance");
69     retractButton = document.getElementById("retract");
70     cursorButton = document.getElementById("cursor");
71     bottomButton = document.getElementById("bottom");
72     dialogBox = document.getElementById("dialogBox");
73     uploadBox = document.getElementById("uploadBox");
74     dialogTitle = document.getElementById("dialogTitle");
75     dialogContent = document.getElementById("dialogContent");
76
77     matita = new Object();
78     matita.disambMode = matita.proofMode = false;
79
80     // hide sequent view at start
81     initializeLayout();
82     updateSide();
83
84     changeFile("test.ma");
85
86     // initialize keyboard events in the unlocked script
87     init_keyboard(unlocked);
88
89     init_autotraces();
90
91   }
92 }
93
94 function init_autotraces() {
95     $("#unlocked .autotactic").tooltip({ 
96       delay: 0, 
97       showURL: false, 
98       bodyHandler: function() { 
99         return (trace_of($(this)[0])); 
100       }
101     });
102     $("#locked .autotactic").tooltip({ 
103       delay: 0, 
104       showURL: false, 
105       bodyHandler: function() { 
106         return (trace_of($(this)[0]));
107       }
108     });
109 }
110
111 function trace_of(node) {
112   return text_of_html(filterByClass(node.childNodes,"autotrace")[0]);
113 }
114
115 function changeFile(name) {
116     current_fname = name;
117     matitaTitle.innerHTML = "Matita - cic:/matita/" + name;
118 }
119
120 function init_keyboard(target)
121 {
122     if (target.addEventListener)
123     {
124 //       target.addEventListener("keydown",keydown,false);
125        target.addEventListener("keypress",keypress,false);
126 //       target.addEventListener("keyup",keyup,false);
127 //       target.addEventListener("textInput",textinput,false);
128     }
129     else if (target.attachEvent)
130     {
131 //       target.attachEvent("onkeydown", keydown);
132        target.attachEvent("onkeypress", keypress);
133 //       target.attachEvent("onkeyup", keyup);
134 //       target.attachEvent("ontextInput", textinput);
135     }
136     else
137     {
138 //       target.onkeydown= keydown;
139        target.onkeypress= keypress;
140 //       target.onkeyup= keyup;
141 //       target.ontextinput= textinput;   // probably doesn't work
142     }
143  
144 }
145
146 function keyval(n)
147 {
148     if (n == null) return 'undefined';
149     var s= '' + n;
150     if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
151     while (s.length < 9) s+= ' ';
152     return s;
153 }
154  
155 function string_of_key(n)
156 {
157     if (n == null) return 'undefined';
158     return String.fromCharCode(n);
159 }
160
161 function pressmesg(w,e)
162 {
163    debug(w + '  keyCode=' + keyval(e.keyCode) +
164                  ' which=' + keyval(e.which) +
165                  ' charCode=' + keyval(e.charCode) +
166                  '\n          shiftKey='+e.shiftKey
167               + ' ctrlKey='+e.ctrlKey
168               + ' altKey='+e.altKey
169               + ' metaKey='+e.metaKey);
170 }
171  
172 function suppressdefault(e,flag)
173 {
174    if (flag)
175    {
176        if (e.preventDefault) e.preventDefault();
177        if (e.stopPropagation) e.stopPropagation();
178    }
179    return !flag;
180 }
181
182 function restoreSelection(r) {
183     unlocked.focus();
184     if (r != null) {
185         if (window.getSelection)//non IE and there is already a selection
186         {
187             var s = window.getSelection();
188             if (s.rangeCount > 0) 
189                 s.removeAllRanges();
190             s.addRange(r);
191         }
192         else 
193             if (document.createRange)//non IE and no selection
194             {
195                 window.getSelection().addRange(r);
196             }
197             else 
198                 if (document.selection)//IE
199                 {
200                     r.select();
201                 }
202     }
203 }
204
205 function lookup_tex(texmacro)
206 {
207   texmacro = texmacro.substring(1);
208   return unescape(macro2utf8[texmacro]);
209 }
210
211 function strip_tags(tagname,classname) 
212 {
213     var tags = unlocked.getElementsByTagName(tagname);
214     if (is_defined(classname)) {
215         tags = filterByClass(tags,classname);
216     }
217     for (i = 0; i < tags.length; i++) {
218         var children = tags[i].childNodes;
219         for (j = 0; j < children.length; j++) {
220             tags[i].parentNode.insertBefore(children[j],tags[i]);
221         }
222     }
223     for (var i = 0;tags.length > i;i++) {
224       tags[0].parentNode.removeChild(tags[0]);
225     }
226 }
227
228 function strip_interpr() {
229         strip_tags("A");
230         alert("strip_interpr ended");
231 }
232  
233 function keypress(e)
234 {
235    if (!e) e= event;
236    pressmesg('keypress',e);
237    var s = string_of_key(e.charCode);
238    strip_tags("span","error");
239    if (s == " ") {
240         j = getCursorPos();
241         i = unlocked.innerHTML.html_to_matita().lastIndexOf('\\',j);
242         if (i >= 0) {
243           match = unlocked.innerHTML.html_to_matita().substring(i,j);
244           sym = unescape_html(lookup_tex(match));
245           if (sym != "undefined") {
246              if (window.getSelection) { // non IE
247                 savedRange.setStart(savedsc,savedso - (j-i));
248                 savedRange.deleteContents();
249                 savedRange.insertNode(document.createTextNode(sym));
250                 savedsc.parentNode.normalize();
251                 if (savedRange.collapsed) { // Mozilla
252                   savedRange.setEnd(savedsc,savedRange.endOffset + sym.length);
253                 }
254                 savedRange.collapse(false);
255              } else {
256                 savedRange.moveStart(i-j);
257                 savedRange.text(sym);
258                 savedRange.collapse(false);
259              }
260              restoreSelection(savedRange); 
261              return suppressdefault(e,true);
262           }
263           else {
264              // restoreSelection(0); 
265              return suppressdefault(e,false);
266           }
267         }
268         else return suppressdefault(e,false);
269    } else {
270         return suppressdefault(e,false);
271    }
272 }
273  
274 var logtxt = "";
275
276 function debug(txt)
277 {
278         // internet explorer (v.9) doesn't work with innerHTML
279         // but google chrome's innerText is, in a sense, "write only"
280         // what should we do?
281         // logarea.innerText = txt + "\n" + logarea.innerText;
282         logtxt = /* logtxt + "\n" +*/ txt;
283 }
284
285 function showLog() {
286   logWin = window.open( "", "Matita Log",
287      "width=600,height=450,status,scrollbars,resizable,screenX=20,screenY=40,left=20,top=40");
288   logWin.document.write('<html><head><title>Matita Log' + '</title></head>');   
289   logWin.document.write('<body><textarea style="width:100%;height:100%;">' +
290     logtxt + '</textarea></body></html>');
291   logWin.document.close(); 
292 }
293
294 function listhd(l)
295 {
296         ar = l.split("#");
297         debug("hd of '" + l + "' = '" + ar[0] + "'");
298         return (ar[0]);
299 }
300
301 function listtl(l)
302 {
303         i = l.indexOf("#");
304         tl = l.substr(i+1);
305         debug("tl of '" + l + "' = '" + tl + "'");
306         return (tl);
307 }
308
309 function listcons(x,l)
310 {
311         debug("cons '" + x + "' on '" + l + "'");
312         return (x + "#" + l);
313 }
314
315 function listnil()
316 {
317         return ("");
318 }
319
320 function list_append(l1,l2)
321 { return (l1 + l2) }
322
323 function is_nil(l)
324 {
325         return (l == "");
326 }
327
328 function fold_left (f,acc,l)
329 {
330         if (is_nil(l))
331            { debug("'" + l + "' is fold end");
332            return (acc); }
333         else
334            { debug("'" + l + "' is fold cons");
335              return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
336 }
337
338 function listiter (f,l)
339 {
340         if (is_nil(l))
341         { debug("'" + l + "' is nil");
342            return;
343         }
344         else
345         {
346            debug("'" + l + "' is not nil");
347            f(listhd(l));
348            listiter(f,listtl(l));
349         }
350 }
351
352 function listmap (f,l)
353 {
354         debug("listmap on " + l);
355         if (is_nil(l)) 
356            { debug("returning listnil");
357              return(listnil());
358            }
359         else 
360            { debug("cons f(hd) map(f,tl)");
361              return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
362            }
363 }
364
365 var statements = listnil();
366
367 var goalarray;
368 var metalist = listnil();
369
370 function pairmap (f,p)
371 {
372   debug("pairmap of '" + p + "'");
373   ar = p.split("|");
374   return (f(ar[0],ar[1])); 
375 }
376
377 function tripletmap (f,p)
378 {
379   debug("tripletmap of '" + p + "'");
380   ar = p.split("|");
381   return (f(ar[0],ar[1],ar[2])); 
382 }
383
384 function fst (p)
385 {
386   debug("fst");
387   return (pairmap (function (a,b) { return (a); }, p));
388 }
389
390 function p13 (p)
391 {
392   debug("p13");
393   return (tripletmap (function (a,b,c) { return (a); }, p));
394 }
395
396 function p23 (p)
397 {
398   debug("p23");
399   return (tripletmap (function (a,b,c) { return (b); }, p));
400 }
401
402 function p33 (p)
403 {
404   debug("f33");
405   return (tripletmap (function (a,b,c) { return (c); }, p));
406 }
407
408 function populate_goalarray(menv)
409 {
410   debug("metasenv.length = " + menv.length);
411   if (menv.length == 0) {
412       try {
413           hideSequent();
414       } catch (err) { };
415   } else {
416       showSequent();
417       goalarray = new Array();
418       metalist = listnil();
419       var tmp_goallist = "";
420       for (i = 0; i < menv.length; i++) {
421         metano = menv[i].getAttribute("number");
422         metaname = menv[i].childNodes[0].childNodes[0].data;
423         goal = menv[i].childNodes[1].childNodes[0].data;
424         debug ("found meta n. " + metano);
425         debug ("found goal\nBEGIN" + goal + "\nEND");
426         goalarray[metano] = goal;
427         tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
428         metalist = listcons(metano,metalist);
429         debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]); 
430       }
431       goals.innerHTML = tmp_goallist;
432       debug("new metalist is '" + metalist + "'");
433       if (is_nil(metalist)) {
434         switch_goal();
435       }
436       else {
437         switch_goal(listhd(metalist));
438       }
439   }
440 }
441
442 function switch_goal(meta)
443 {
444   if (typeof meta == "undefined") {
445     goalview.innerHTML = "";
446   }
447   else {
448     debug("switch_goal " + meta + "\n" + goalarray[meta]);
449     goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
450   }
451 }
452
453 // the following is used to avoid escaping unicode, which results in 
454 // the server being unable to unescape the string
455 String.prototype.sescape = function() {
456         var patt1 = /%/gi;
457         var patt2 = /=/gi;
458         var patt3 = /&/gi;
459         var patt4 = /\+/gi;
460         var result = this;
461         result = result.replace(patt1,"%25");
462         result = result.replace(patt2,"%3D");
463         result = result.replace(patt3,"%26");
464         result = result.replace(patt4,"%2B");
465         return (result);
466 }
467
468 String.prototype.html_to_matita = function()
469 {
470         var patt1 = /<br(\/|)>/gi;
471         var patt2 = /</gi
472         var patt3 = />/gi
473         var patt4 = /&lt;/gi;
474         var patt5 = /&gt;/gi;
475         var patt6 = /&nbsp;/gi;
476         var result = this;
477         result = result.replace(patt1,"\n");
478         result = result.replace(patt2,"\005");
479         result = result.replace(patt3,"\006");
480         result = result.replace(patt4,"<");
481         result = result.replace(patt5,">");
482         result = result.replace(patt6," ");
483         return (unescape(result));
484 }
485
486 String.prototype.matita_to_html = function()
487 {
488         var patt1 = /</gi
489         var patt2 = />/gi
490         var patt3 = /\005/gi;
491         var patt4 = /\006/gi;
492         var result = this;
493         result = result.replace(patt1,"&lt;");
494         result = result.replace(patt2,"&gt;");
495         result = result.replace(patt3,"<");
496         result = result.replace(patt4,">");
497         return (unescape(result));
498 }
499
500 function is_defined(x)
501 {
502         return (typeof x != "undefined");
503 }
504
505 /* servicename: name of the service being called
506  * reqbody: text of the request
507  * processResponse: processes the server response
508  *     (takes the response text in input, undefined in case of error)
509  */
510 function callServer(servicename,processResponse,reqbody)
511 {
512         var req = null; 
513         // pause();
514         if (window.XMLHttpRequest)
515         {
516                 req = new XMLHttpRequest();
517         } 
518         else if (window.ActiveXObject) 
519         {
520                 try {
521                                 req = new ActiveXObject("Msxml2.XMLHTTP");
522                 } catch (e)
523                 {
524                         try {
525                                 req = new ActiveXObject("Microsoft.XMLHTTP");
526                                 } catch (e) {}
527                 }
528         }
529         req.onreadystatechange = function()
530         { 
531
532                 rs = req.readyState;
533
534                 if(rs == 4)
535                 {
536                         stat = req.status;
537                         stxt = req.statusText;
538                         if(stat == 200)
539                         {
540                           debug(req.responseText);
541                           if (window.DOMParser) {
542                             parser=new DOMParser();
543                             xmlDoc=parser.parseFromString(req.responseText,"text/xml");
544                           }
545                           else // Internet Explorer
546                           {
547                             xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
548                             xmlDoc.async="false";
549                             xmlDoc.loadXML(req.responseText);
550                           }     
551                           processResponse(xmlDoc);
552                         } else {
553                           processResponse();
554                         }
555                 } 
556         };
557         req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
558         req.setRequestHeader("Content-type","application/x-www-form-urlencoded");       
559         if (reqbody) {
560                 req.send(reqbody); 
561         } else {
562                 req.send();
563         }
564   
565 }
566
567 function advOneStep(xml) {
568         var parsed = xml.getElementsByTagName("parsed")[0];
569         var ambiguity = xml.getElementsByTagName("ambiguity")[0];
570         var disamberr = xml.getElementsByTagName("disamberror")[0];
571         if (is_defined(parsed)) {
572         // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
573             var len = parseInt(parsed.getAttribute("length"));
574             // len0 = unlocked.innerHTML.length;
575             var unescaped = unlocked.innerHTML.html_to_matita();
576             var parsedtxt = parsed.childNodes[0].wholeText;
577             //parsedtxt = unescaped.substr(0,len); 
578             var unparsedtxt = unescaped.substr(len);
579             lockedbackup += parsedtxt;
580             locked.innerHTML = lockedbackup;
581             unlocked.innerHTML = unparsedtxt.matita_to_html();
582             // len1 = unlocked.innerHTML.length;
583             // len2 = len0 - len1;
584             var len2 = parsedtxt.length;
585             metasenv = xml.getElementsByTagName("meta");
586             statements = listcons(len2,statements);
587             unlocked.scrollIntoView(true);
588             return len;
589         }
590         else if (is_defined(ambiguity)) {
591             var start = parseInt(ambiguity.getAttribute("start"));
592             var stop = parseInt(ambiguity.getAttribute("stop"));
593             var choices = xml.getElementsByTagName("choice");
594
595             matita.ambiguityStart = start;
596             matita.ambiguityStop = stop;
597             matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
598             matita.interpretations = [];
599         
600             var unlockedtxt = unlocked.innerHTML.html_to_matita();
601             var pre = unlockedtxt.substring(0,start).matita_to_html();
602             var mid = unlockedtxt.substring(start,stop).matita_to_html();
603             var post = unlockedtxt.substring(stop).matita_to_html();
604             unlocked.innerHTML = pre + 
605                     "<span class=\"error\" title=\"disambiguation error\">" +
606                     mid + "</span>" + post;
607
608             var title = "<H3>Ambiguous input</H3>";
609             disambcell.innerHTML = title;
610             for (i = 0;i < choices.length;i++) {
611                 matita.interpretations[i] = new Object();
612
613                 var href = choices[i].getAttribute("href");
614                 var title = choices[i].getAttribute("title");
615                 var desc = choices[i].childNodes[0].nodeValue;
616
617                 matita.interpretations[i].href = href;
618                 matita.interpretations[i].title = title;
619                 matita.interpretations[i].desc = desc;
620                 
621                 var choice = document.createElement("input");
622                 choice.setAttribute("type","radio");
623                 choice.setAttribute("name","interpr");
624                 choice.setAttribute("href",href);
625                 choice.setAttribute("title",title);
626                 if (i == 0) choice.setAttribute("checked","");
627                 
628                 disambcell.appendChild(choice);
629                 disambcell.appendChild(document.createTextNode(desc));
630                 disambcell.appendChild(document.createElement("br"));
631             }
632
633             var okbutton = document.createElement("input");
634             okbutton.setAttribute("type","button");
635             okbutton.setAttribute("value","OK");
636             okbutton.setAttribute("onclick","do_disambiguate()");
637             var cancelbutton = document.createElement("input");
638             cancelbutton.setAttribute("type","button");
639             cancelbutton.setAttribute("value","Cancel");
640             cancelbutton.setAttribute("onclick","cancel_disambiguate()");
641
642             disambcell.appendChild(okbutton);
643             disambcell.appendChild(cancelbutton);
644
645             matita.disambMode = true;
646             updateSide();
647             throw "Stop";
648         }
649         else if (is_defined(disamberr)) {
650             // must be fixed in a daemon: it makes sense to return a 
651             // disambiguation error with no choices
652             if (disamberr.childNodes.length > 0) {
653               set_cps(disamberr.childNodes);
654               matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
655               matita.disambMode = true;
656               next_cp(0);
657             }
658             throw "Stop";
659         }
660         else {
661             var error = xml.getElementsByTagName("error")[0]; 
662             unlocked.innerHTML = error.childNodes[0].wholeText;
663             // debug(xml.childNodes[0].nodeValue);
664             throw "Stop";
665         }
666
667 }
668
669 function advanceForm1()
670 {
671         processor = function(xml) {
672            try {
673                 if (is_defined(xml)) {
674                     advOneStep(xml);
675                     populate_goalarray(metasenv);
676                     init_autotraces();
677                 } else {
678                         debug("advance failed");
679                 }
680             } catch (e) { 
681                     // nothing to do 
682             };
683             resume();
684         };
685         pause();
686         callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
687   
688 }
689
690 // get or set <choicepoint>'s (in case of disamb error)
691 function get_cps() {
692         return matita.choicepoints
693 }
694
695 function set_cps(cps) {
696         matita.choicepoints = cps;
697 }
698
699 // get radio buttons for <choice>'s in a given cp
700 function get_choice_opts(i) {
701    var res = [];
702    var choices = get_cps()[i].childNodes;
703    for (var j = 0;j < choices.length;j++) {
704       var href = choices[j].getAttribute("href");
705       var title = choices[j].getAttribute("title");
706       var desc;
707       if (is_defined(title) && title != null) {
708            desc = title;
709       } else if (is_defined(href) && href != null) {
710            desc = href;
711       } else {
712            desc = null;
713       }
714   
715       res[j] = document.createElement("input");
716       res[j].setAttribute("type","radio");
717       res[j].setAttribute("name","choice");
718       res[j].setAttribute("choicepointno",i);
719       res[j].setAttribute("choiceno",j);
720       res[j].setAttribute("href",href);
721       res[j].setAttribute("title",title);
722       if (desc != null) res[j].setAttribute("desc",desc);
723       
724       if (j == 0) res[j].setAttribute("checked","");
725   }
726   return res;
727 }
728
729 // get radio buttons for <failure>'s in a given choice
730 function get_failure_opts(i,j) {
731    var res = [];
732    var failures = get_cps()[i].childNodes[j].childNodes;
733    for (var k = 0;k < failures.length;k++) {
734       var start = failures[k].getAttribute("start");
735       var stop = failures[k].getAttribute("stop");
736       var title = failures[k].getAttribute("title");
737   
738       res[k] = document.createElement("input");
739       res[k].setAttribute("type","radio");
740       res[k].setAttribute("name","failure");
741       res[k].setAttribute("choicepointno",i);
742       res[k].setAttribute("choiceno",j);
743       res[k].setAttribute("failureno",k);
744       res[k].setAttribute("start",start);
745       res[k].setAttribute("stop",stop);
746       res[k].setAttribute("title",title);
747       
748       if (k == 0) res[k].setAttribute("checked","");
749   }
750   return res;
751 }
752
753 function next_cp(curcp) {
754         var cp = get_cps()[curcp];
755         var start = parseInt(cp.getAttribute("start"));
756         var stop = parseInt(cp.getAttribute("stop"));
757
758         matita.errorStart = start;
759         matita.errorStop = stop;
760         // matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
761         
762         var unlockedtxt = matita.unlockedbackup;
763         var pre = unlockedtxt.substring(0,start).matita_to_html();
764         var mid = unlockedtxt.substring(start,stop).matita_to_html();
765         var post = unlockedtxt.substring(stop).matita_to_html();
766         unlocked.innerHTML = pre + 
767                 "<span class=\"error\" title=\"error location\">" +
768                 mid + "</span>" + post;
769
770         var title = "<H3>Error diagnostics</H3>";
771         disambcell.innerHTML = title;
772         var choices = get_choice_opts(curcp);
773         for (var i = 0;i < choices.length;i++) {
774             disambcell.appendChild(choices[i]);
775             var desc = choices[i].getAttribute("desc");
776             if (!is_defined(desc) || desc == null) {
777                     desc = "Interpretation " + i;
778             }
779             disambcell.appendChild(document.createTextNode(desc));
780             disambcell.appendChild(document.createElement("br"));
781         }
782         
783         // update index of the next choicepoint
784         new_curcp = (curcp + 1) % get_cps().length;
785
786         var okbutton = document.createElement("input");
787         okbutton.setAttribute("type","button");
788         okbutton.setAttribute("value","OK");
789         okbutton.setAttribute("onclick","show_failures()");
790         var cancelbutton = document.createElement("input");
791         cancelbutton.setAttribute("type","button");
792         cancelbutton.setAttribute("value","Close");
793         cancelbutton.setAttribute("onclick","cancel_disambiguate()");
794         var tryagainbutton = document.createElement("input");
795         tryagainbutton.setAttribute("type","button");
796         if (new_curcp > 0) {
797             tryagainbutton.setAttribute("value","Try something else");
798         } else {
799             tryagainbutton.setAttribute("value","Restart");
800         }
801         tryagainbutton.setAttribute("onclick","next_cp(" + new_curcp + ")");
802
803         disambcell.appendChild(okbutton);
804         disambcell.appendChild(tryagainbutton);
805         disambcell.appendChild(cancelbutton);
806
807         //disable_toparea();
808
809         //matita.disambMode = true;
810         updateSide();
811         
812 }
813
814 function show_failures() {
815
816         var choice = document.getElementsByName("choice")[get_checked_index("choice")];
817         var cpno = parseInt(choice.getAttribute("choicepointno"));
818         var choiceno = parseInt(choice.getAttribute("choiceno"));
819         var choicedesc = choice.getAttribute("desc");
820
821         var title = "<H3>Error diagnostics</H3>";
822         var subtitle;
823         if (is_defined(choicedesc) && choicedesc != null) {
824                 subtitle  = "<p>Errors at node " + cpno + " = " + choicedesc + "</p>";
825         } else {
826                 subtitle = "<p>Global errors:</p>";
827         }
828
829         disambcell.innerHTML = title + subtitle;
830         var failures = get_failure_opts(cpno,choiceno);
831         for (var i = 0;i < failures.length;i++) {
832             disambcell.appendChild(failures[i]);
833             disambcell.appendChild(document.createTextNode(failures[i].getAttribute("title")));
834             disambcell.appendChild(document.createElement("br"));
835         }
836         
837         var okbutton = document.createElement("input");
838         okbutton.setAttribute("type","button");
839         okbutton.setAttribute("value","Show error loc");
840         okbutton.setAttribute("onclick","show_err()");
841         var cancelbutton = document.createElement("input");
842         cancelbutton.setAttribute("type","button");
843         cancelbutton.setAttribute("value","Close");
844         cancelbutton.setAttribute("onclick","cancel_disambiguate()");
845         var backbutton = document.createElement("input");
846         backbutton.setAttribute("type","button");
847         backbutton.setAttribute("value","<< Back");
848         backbutton.setAttribute("onclick","next_cp(" + cpno + ")");
849
850         disambcell.appendChild(backbutton);
851         disambcell.appendChild(okbutton);
852         disambcell.appendChild(cancelbutton);
853         
854 }
855
856 function show_err() {
857         var radios = document.getElementsByName("failure");
858         for (i = 0; i < radios.length; i++) {
859             if (radios[i].checked) {
860                 var start = radios[i].getAttribute("start");
861                 var stop = radios[i].getAttribute("stop");
862                 var title = radios[i].getAttribute("title");
863                 var unlockedtxt = matita.unlockedbackup;
864                 var pre = unlockedtxt.substring(0,start).matita_to_html();
865                 var mid = unlockedtxt.substring(start,stop).matita_to_html();
866                 var post = unlockedtxt.substring(stop).matita_to_html();
867                 unlocked.innerHTML = pre + 
868                         "<span class=\"error\" title=\"Disambiguation failure\">" +
869                         mid + "</span>" + post;
870                 break;
871             }
872         }
873 }
874
875 function gotoBottom()
876 {
877         processor = function(xml) {
878                 if (is_defined(xml)) {
879                         // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
880                         var parsed = xml.getElementsByTagName("parsed");
881                         var localized = xml.getElementsByTagName("localized")[0];
882                         var ambiguity = xml.getElementsByTagName("ambiguity")[0];
883                         var generic_err = xml.getElementsByTagName("error")[0];
884                         var disamberr = xml.getElementsByTagName("disamberror")[0];
885                         for (var i = 0;i < parsed.length; i++) {
886                           var len = parsed[i].getAttribute("length");
887                           // len0 = unlocked.innerHTML.length;
888                           var unescaped = unlocked.innerHTML.html_to_matita();
889                           // the browser may decide to split textnodes: use wholeText!
890                           var parsedtxt = parsed[i].childNodes[0].wholeText;
891                           //parsedtxt = unescaped.substr(0,len); 
892                           var unparsedtxt = unescaped.substr(len);
893                           lockedbackup += parsedtxt;
894                           locked.innerHTML = lockedbackup; //.matita_to_html();
895                           unlocked.innerHTML = unparsedtxt.matita_to_html();
896                           // len1 = unlocked.innerHTML.length;
897                           var len2 = parsedtxt.length;
898                           statements = listcons(len2,statements);
899                         }
900                         unlocked.scrollIntoView(true);
901                         metasenv = xml.getElementsByTagName("meta");
902                         init_autotraces();
903                         populate_goalarray(metasenv);
904
905                         if (is_defined(ambiguity)) {
906                             var start = parseInt(ambiguity.getAttribute("start"));
907                             var stop = parseInt(ambiguity.getAttribute("stop"));
908                             var choices = xml.getElementsByTagName("choice");
909
910                             matita.ambiguityStart = start;
911                             matita.ambiguityStop = stop;
912                             matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
913                             matita.interpretations = [];
914                         
915                             var unlockedtxt = unlocked.innerHTML.html_to_matita();
916                             var pre = unlockedtxt.substring(0,start).matita_to_html();
917                             var mid = unlockedtxt.substring(start,stop).matita_to_html();
918                             var post = unlockedtxt.substring(stop).matita_to_html();
919                             unlocked.innerHTML = pre + 
920                                     "<span class=\"error\" title=\"disambiguation error\">" +
921                                     mid + "</span>" + post;
922
923                             var title = "<H3>Ambiguous input</H3>";
924                             disambcell.innerHTML = title;
925                             for (i = 0;i < choices.length;i++) {
926                                 matita.interpretations[i] = new Object();
927
928                                 var href = choices[i].getAttribute("href");
929                                 var title = choices[i].getAttribute("title");
930                                 var desc = choices[i].childNodes[0].nodeValue;
931
932                                 matita.interpretations[i].href = href;
933                                 matita.interpretations[i].title = title;
934                                 matita.interpretations[i].desc = desc;
935                                 
936                                 var choice = document.createElement("input");
937                                 choice.setAttribute("type","radio");
938                                 choice.setAttribute("name","interpr");
939                                 choice.setAttribute("href",href);
940                                 choice.setAttribute("title",title);
941                                 if (i == 0) choice.setAttribute("checked","");
942                                 
943                                 disambcell.appendChild(choice);
944                                 disambcell.appendChild(document.createTextNode(desc));
945                                 disambcell.appendChild(document.createElement("br"));
946                             }
947
948                             var okbutton = document.createElement("input");
949                             okbutton.setAttribute("type","button");
950                             okbutton.setAttribute("value","OK");
951                             okbutton.setAttribute("onclick","do_disambiguate()");
952                             var cancelbutton = document.createElement("input");
953                             cancelbutton.setAttribute("type","button");
954                             cancelbutton.setAttribute("value","Cancel");
955                             cancelbutton.setAttribute("onclick","cancel_disambiguate()");
956
957                             disambcell.appendChild(okbutton);
958                             disambcell.appendChild(cancelbutton);
959
960                             matita.disambMode = true;
961                             updateSide();
962                         }
963                         else if (is_defined(disamberr)) {
964                             // must be fixed in a daemon: it makes sense to return a 
965                             // disambiguation error with no choices
966                             if (disamberr.childNodes.length > 0) {
967                               set_cps(disamberr.childNodes);
968                               matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
969                               matita.disambMode = true;
970                               next_cp(0);
971                             }
972                             throw "Stop";
973                         }
974                         else if (is_defined(localized)) {
975                             unlocked.innerHTML = localized.childNodes[0].wholeText;
976                         }
977                         else if (is_defined(generic_err)) {
978                             debug("Unmanaged error:\n" ^ generic_err.childNodes[0].wholeText);
979                         }
980                 } else {
981                         debug("goto bottom failed");
982                 } 
983                 resume();
984         };
985         pause();
986         callServer("bottom",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
987 }
988
989
990 function gotoTop()
991 {
992         processor = function(xml) {
993                 if (is_defined(xml)) {
994                   if (xml.childNodes[0].textContent != "ok") {
995                      debug("goto top failed");
996                   }
997                   else
998                         statements = listnil();
999                         /*
1000                         lockedlen = locked.innerHTML.length - statementlen;
1001                         statement = locked.innerHTML.substr(lockedlen, statementlen);
1002                         locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
1003                         unlocked.innerHTML = statement + unlocked.innerHTML;
1004                         */
1005                         unlocked.innerHTML = lockedbackup + unlocked.innerHTML;
1006                         lockedbackup = "";
1007                         locked.innerHTML = lockedbackup;
1008                         init_autotraces();
1009                         hideSequent();
1010                         unlocked.scrollIntoView(true);
1011                 } else {
1012                         debug("goto top failed");
1013                 } 
1014                 resume();
1015         };
1016         pause();
1017         callServer("top",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
1018   
1019 }
1020
1021 function gotoPos(offset)
1022 {
1023         if (!is_defined(offset)) {
1024                 offset = getCursorPos();
1025         }
1026         processor = function(xml) {
1027                 if (is_defined(xml)) {
1028                     try {
1029                         /*
1030                         parsed = xml.getElementsByTagName("parsed")[0];
1031                         len = parseInt(parsed.getAttribute("length"));
1032                         // len0 = unlocked.innerHTML.length;
1033                         unescaped = unlocked.innerHTML.html_to_matita();
1034                         parsedtxt = parsed.childNodes[0].wholeText;
1035                         //parsedtxt = unescaped.substr(0,len); 
1036                         unparsedtxt = unescaped.substr(len);
1037                         lockedbackup += parsedtxt;
1038                         locked.innerHTML = lockedbackup; //.matita_to_html();
1039                         unlocked.innerHTML = unparsedtxt.matita_to_html();
1040                         // len1 = unlocked.innerHTML.length;
1041                         len2 = parsedtxt.length;
1042                         metasenv = xml.getElementsByTagName("meta");
1043                         // populate_goalarray(metasenv);
1044                         statements = listcons(len2,statements);
1045                         unlocked.scrollIntoView(true);
1046                         // la populate non andrebbe fatta a ogni passo
1047                         */
1048                         var len = advOneStep(xml);
1049                         if (offset <= len) {
1050                                 init_autotraces();
1051                                 populate_goalarray(metasenv);
1052                                 resume();
1053                         } else {
1054                                 gotoPos(offset - len);
1055                         }
1056                     } catch (er) {
1057                         init_autotraces();
1058                         populate_goalarray(metasenv);
1059                         resume();
1060                     }
1061                 } else {
1062                         init_autotraces();
1063                         unlocked.scrollIntoView(true);
1064                         populate_goalarray(metasenv);
1065                         resume();
1066                 }
1067         }
1068         pause();
1069         callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
1070 }
1071
1072 function retract()
1073 {
1074         processor = function(xml) {
1075                 if (typeof xml != "undefined") {
1076                         // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
1077                         statementlen = parseInt(listhd(statements));
1078                         statements = listtl(statements);
1079                         /*
1080                         lockedlen = locked.innerHTML.length - statementlen;
1081                         statement = locked.innerHTML.substr(lockedlen, statementlen);
1082                         locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
1083                         unlocked.innerHTML = statement + unlocked.innerHTML;
1084                         */
1085                         lockedlen = lockedbackup.length - statementlen;
1086                         statement = lockedbackup.substr(lockedlen, statementlen);
1087                         lockedbackup = lockedbackup.substr(0,lockedlen);
1088                         locked.innerHTML = lockedbackup;
1089                         unlocked.innerHTML = statement + unlocked.innerHTML;
1090                         metasenv = xml.getElementsByTagName("meta");
1091                         init_autotraces();
1092                         populate_goalarray(metasenv);
1093                         unlocked.scrollIntoView(true);
1094                 } else {
1095                         debug("retract failed");
1096                 }
1097                 resume();
1098         };
1099         pause();
1100         callServer("retract",processor);
1101 }
1102
1103 function openFile()
1104
1105         processor = function(xml)
1106         {
1107                 if (is_defined(xml)) {  
1108                         lockedbackup = "";
1109                         locked.innerHTML = lockedbackup;
1110                         unlocked.innerHTML = xml.documentElement.wholeText;
1111                 } else {
1112                         debug("file open failed");
1113                 }
1114         };
1115         callServer("open",processor,"file=" + escape(filename.value)); 
1116 }
1117
1118 function retrieveFile(thefile)
1119
1120         processor = function(xml)
1121         {
1122                 if (is_defined(xml)) {  
1123                         changeFile(thefile);
1124                         matita.disambMode = false;
1125                         matita.proofMode = false;
1126                         updateSide();
1127                         lockedbackup = ""
1128                         locked.innerHTML = lockedbackup;
1129                         // code originally used in google chrome (problems with mozilla)
1130                         // debug(xml.getElementsByTagName("file")[0].childNodes[0].nodeValue);
1131                         // unlocked.innerHTML = xml.getElementsByTagName("file")[0].childNodes[0].nodeValue;
1132                         debug(xml.childNodes[0].textContent);
1133                         if (document.all) { // IE
1134                           unlocked.innerHTML = xml.childNodes[0].text;
1135                         } else {
1136                           unlocked.innerHTML = xml.childNodes[0].textContent;
1137                         }
1138                         init_autotraces();
1139
1140                 } else {
1141                         debug("file open failed");
1142                 }
1143         };
1144         abortDialog("dialogBox");
1145         callServer("open",processor,"file=" + escape(thefile)); 
1146 }
1147
1148 function showLibrary(title,callback,reloadDialog)
1149
1150         var req = null;
1151         dialogBox.reload = reloadDialog; 
1152         // pause();
1153         if (window.XMLHttpRequest)
1154         {
1155                 req = new XMLHttpRequest();
1156         } 
1157         else if (window.ActiveXObject) 
1158         {
1159                 try {
1160                                 req = new ActiveXObject("Msxml2.XMLHTTP");
1161                 } catch (e)
1162                 {
1163                         try {
1164                                 req = new ActiveXObject("Microsoft.XMLHTTP");
1165                                 } catch (e) {}
1166                 }
1167         }
1168         req.onreadystatechange = function()
1169         { 
1170
1171                 rs = req.readyState;
1172
1173                 if(rs == 4)
1174                 {
1175                         stat = req.status;
1176                         stxt = req.statusText;
1177                         if(stat == 200)
1178                         {
1179                           debug(req.responseText);
1180                           showDialog("<H2>" + title + "</H2>",req.responseText, callback);
1181                         } 
1182                 } 
1183         };
1184         req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
1185         req.setRequestHeader("Content-type","application/x-www-form-urlencoded");       
1186         req.send();
1187   
1188 }
1189
1190 function uploadDialog()
1191 {  
1192         uploadBox.style.display = "block";
1193 }
1194
1195 function uploadOK()
1196 {   
1197    var file = document.getElementById("uploadFilename").files[0];
1198 //   if (file) { 
1199 //       var filecontent = file.getAsText("UTF-8");
1200 //       locked.innerHTML = lockedbackup;
1201 //       unlocked.innerHTML = filecontent;
1202 //       uploadBox.style.display = "none";
1203 //   }
1204    if (file) { 
1205       var reader = new FileReader();
1206       reader.onerror = function (evt) {
1207            debug("file open failed");
1208       }
1209       reader.onload = function (evt) {
1210            lockedbackup = "";
1211            locked.innerHTML = lockedbackup;
1212            unlocked.innerHTML = "";
1213            unlocked.appendChild(document.createTextNode(evt.target.result));
1214            uploadBox.style.display = "none";
1215       }
1216       try { reader.readAsText(file, "UTF-8"); }
1217       catch (err) { /* nothing to do */ };
1218       uploadBox.style.display = "none";
1219    }
1220 }
1221
1222 function openDialog()
1223 {  
1224         callback = function (fname) { retrieveFile(fname); };
1225         showLibrary("Open file", callback, openDialog);
1226 }
1227
1228 function saveDialog()
1229 {  
1230         callback = function (fname) { 
1231           abortDialog("dialogBox");
1232           saveFile(fname,
1233                    (locked.innerHTML.html_to_matita()).sescape(),
1234                    (unlocked.innerHTML.html_to_matita()).sescape(),
1235                    false,saveDialog); 
1236         };
1237         showLibrary("Save file as", callback, saveDialog);
1238 }
1239
1240 function newDialog()
1241 {
1242         callback = function (fname) { 
1243           abortDialog("dialogBox");
1244           saveFile(fname,"","",false,newDialog,true);
1245         };
1246         showLibrary("Create new file", callback, newDialog);
1247 }
1248
1249
1250 function saveFile(fname,lockedtxt,unlockedtxt,force,reloadDialog,reloadFile)
1251 {
1252         if (!is_defined(reloadFile)) { reloadFile = true };
1253         if (!is_defined(fname)) {
1254             fname = current_fname;
1255             lockedtxt = (locked.innerHTML.html_to_matita()).sescape();
1256             unlockedtxt = (unlocked.innerHTML.html_to_matita()).sescape();
1257             force = true;
1258             // when force is true, reloadDialog is not needed 
1259         }
1260         processor = function(xml) {
1261                 if (is_defined(xml)) {
1262                   if (xml.childNodes[0].textContent != "ok") {
1263                     if (confirm("File already exists. All existing data will be lost.\nDo you want to proceed anyway?")) {
1264                        saveFile(fname,lockedtxt,unlockedtxt,true,reloadDialog,reloadFile);
1265                     } else {
1266                       reloadDialog();
1267                     }
1268                   } else {
1269                     changeFile(fname);
1270                     debug("file saved!");
1271                     if (reloadFile) { retrieveFile(fname); }
1272                   }
1273                 } else {
1274                         debug("save file failed");
1275                 }
1276                 resume();
1277         };
1278         if (is_defined(fname)) {
1279           pause();
1280           callServer("save",processor,"file=" + escape(fname) + 
1281                                     "&locked=" + lockedtxt +
1282                                     "&unlocked=" + unlockedtxt +
1283                                     "&force=" + force);
1284         }
1285         else { debug("no file selected"); }
1286 }
1287
1288 function createDir() {
1289    abortDialog("dialogBox");
1290    dirname = prompt("New directory name:\ncic:/matita/","newdir");
1291    if (dirname != null) {
1292         processor = function(xml) {
1293                 if (is_defined(xml)) {
1294                   if (xml.childNodes[0].textContent != "ok") {
1295                       alert("An error occurred :-(");
1296                   }
1297                 } else {
1298                       alert("An error occurred :-(");
1299                 }
1300                 dialogBox.reload();
1301         };
1302         pause();
1303         callServer("save",processor,"file=" + escape(dirname) + 
1304                                     "&locked=&unlocked=&force=false&dir=true");
1305    } else {
1306       dialogBox.reload();
1307    }
1308 }
1309
1310 function commitAll()
1311 {
1312         processor = function(xml) {
1313                 if (is_defined(xml)) {
1314                         debug(xml.getElementsByTagName("details")[0].textContent);
1315                         alert("Commit executed: see details in the log.\n\n" +
1316                               "NOTICE: this message does NOT imply (yet) that the commit was successful.");
1317                 } else {
1318                         alert("Commit failed!\n(maybe you don't have permissionis to commit?)");
1319                 }
1320                 resume();
1321         };
1322         pause();
1323         callServer("commit",processor);
1324 }
1325
1326 function updateAll()
1327 {
1328         processor = function(xml) {
1329                 if (is_defined(xml)) {
1330                         alert("Update executed.\n\n" +
1331                               "Details:\n" +
1332                               xml.getElementsByTagName("details")[0].textContent);
1333                 } else {
1334                         alert("Update failed!\n(maybe you don't have permissions to update?)");
1335                 }
1336                 resume();
1337         };
1338         pause();
1339         callServer("update",processor);
1340 }
1341
1342 var goalcell;
1343
1344 function hideSequent() {
1345         matita.proofMode = false;
1346         updateSide();
1347 }
1348
1349 function showSequent() {
1350         matita.proofMode = true;
1351         updateSide();
1352 }
1353
1354 function showDialog(title,content,callback) {
1355   dialogTitle.innerHTML = title;
1356   dialogContent.innerHTML = content;
1357   dialogBox.callback = callback;
1358
1359   //Get the screen height and width
1360   var maskHeight = $(document).height();
1361   var maskWidth = $(window).width();
1362   
1363   //Set heigth and width to mask to fill up the whole screen
1364   $('#mask').css({'width':maskWidth,'height':maskHeight});
1365   
1366   //transition effect           
1367   $('#mask').fadeIn(100);       
1368   $('#mask').fadeTo(200,0.8);   
1369   
1370   //Get the window height and width
1371   var winH = $(window).height();
1372   var winW = $(window).width();
1373   
1374   //Set the popup window to center
1375   $('#dialogBox').css('top',  winH/2-$('#dialogBox').height()/2);
1376   $('#dialogBox').css('left', winW/2-$('#dialogBox').width()/2);
1377   
1378   //transition effect
1379   $('#dialogBox').fadeIn(200); 
1380
1381   dialogBox.style.display = "block";
1382 }
1383
1384 function abortDialog(dialog) {
1385   document.getElementById(dialog).style.display = "none";
1386   $('#mask').hide();
1387 }
1388
1389 function removeElement(id) {
1390   var element = document.getElementById(id);
1391   element.parentNode.removeChild(element);
1392
1393
1394 var savedsc;
1395 var savedso;
1396
1397 function getCursorPos() {
1398   var cursorPos;
1399   if (window.getSelection) {
1400     var selObj = window.getSelection();
1401     savedRange = selObj.getRangeAt(0);
1402     savedsc = savedRange.startContainer;
1403     savedso = savedRange.startOffset;
1404     //cursorPos =  findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
1405     cursorPos =  findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
1406     /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
1407     return(cursorPos);
1408   }
1409   else if (document.selection) {
1410     savedRange = document.selection.createRange();
1411     var bookmark = savedRange.getBookmark();
1412     /* FIXME the following works wrong when the document is longer than 65535 chars */
1413     cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
1414     return(cursorPos);
1415   }
1416 }
1417
1418 function findNode(list, node, acc) {
1419   for (var i = 0; i < list.length; i++) {
1420     if (list[i] == node) {
1421    //   debug("success " + i);
1422       return acc;
1423     }
1424     if (list[i].hasChildNodes()) {
1425        try {
1426    //      debug("recursion on node " + i);
1427          return (findNode(list[i].childNodes,node,acc))
1428        }
1429        catch (e) { /* debug("recursion failed"); */ }
1430     }
1431     sandbox = document.getElementById("sandbox");
1432     dup = list[i].cloneNode(true);
1433     sandbox.appendChild(dup);
1434 //    debug("fail " + i + ": " + sandbox.innerHTML);
1435     acc += sandbox.innerHTML.html_to_matita().length;
1436     sandbox.removeChild(dup);
1437   }
1438   throw "not found";
1439 }
1440
1441 function test () {
1442   debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
1443 }
1444
1445 function get_checked_index(name) {
1446         var radios = document.getElementsByName(name);
1447         for (i = 0; i < radios.length; i++) {
1448             if (radios[i].checked) {
1449                     return i;
1450             }
1451         }
1452         return null;
1453 }
1454
1455 function cancel_disambiguate() {
1456         matita.disambMode = false;
1457         resume();
1458         // enable_toparea();
1459         // enable_editing();
1460         strip_tags("span","error");
1461         updateSide();
1462 }
1463
1464 function do_disambiguate() {
1465         var i = get_checked_index("interpr");
1466         if (i != null) {
1467             var pre = matita.unlockedbackup
1468                       .substring(0,matita.ambiguityStart).matita_to_html();
1469             var mid = matita.unlockedbackup
1470                       .substring(matita.ambiguityStart,matita.ambiguityStop)
1471                       .matita_to_html();
1472             var post = matita.unlockedbackup
1473                        .substring(matita.ambiguityStop).matita_to_html();
1474
1475             var href = matita.interpretations[i].href;
1476             var title = matita.interpretations[i].title;
1477
1478             if (is_defined(title)) {
1479                 mid = "<A href=\"" + href + "\" title=\"" + title + "\">" + mid + "</A>";
1480             } else {
1481                 mid = "<A href=\"" + href + "\">" + mid + "</A>";
1482             }
1483
1484             unlocked.innerHTML = pre + mid + post;
1485
1486             matita.disambMode = false;
1487             enable_toparea();
1488             enable_editing();
1489             updateSide();
1490         }
1491 }
1492
1493 function do_showerror() {
1494         var i = get_checked_index("choice");
1495         if (i != null) {
1496             var pre = matita.unlockedbackup
1497                       .substring(0,matita.ambiguityStart).matita_to_html();
1498             var mid = matita.unlockedbackup
1499                       .substring(matita.ambiguityStart,matita.ambiguityStop)
1500                       .matita_to_html();
1501             var post = matita.unlockedbackup
1502                        .substring(matita.ambiguityStop).matita_to_html();
1503
1504             var href = matita.interpretations[i].href;
1505             var title = matita.interpretations[i].title;
1506
1507             if (is_defined(title)) {
1508                 mid = "<A href=\"" + href + "\" title=\"" + title + "\">" + mid + "</A>";
1509             } else {
1510                 mid = "<A href=\"" + href + "\">" + mid + "</A>";
1511             }
1512
1513             unlocked.innerHTML = pre + mid + post;
1514
1515         }
1516 }
1517
1518 function readCookie(name) {
1519         var nameEQ = name + "=";
1520         var ca = document.cookie.split(';');
1521         for(var i=0;i < ca.length;i++) {
1522                 var c = ca[i];
1523                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
1524                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
1525         }
1526         return null;
1527 }
1528
1529 function delete_cookie ( cookie_name )
1530 {
1531   var cookie_date = new Date();  // current date & time
1532   cookie_date.setTime ( cookie_date.getTime() - 1 );
1533   document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
1534 }
1535
1536 function delete_session()
1537 {
1538         delete_cookie("session");
1539 }
1540
1541 function disable_toparea() {
1542         var offset = $('#toparea').offset();
1543         $('#whitemask').css('top',offset.top);
1544         $('#whitemask').css('left',offset.left);
1545         $('#whitemask').css('width',$('#toparea').outerWidth() + "px");
1546         $('#whitemask').css('height',$('#toparea').outerHeight() + "px");
1547         $('#whitemask').fadeTo('fast',0.7);
1548 }
1549
1550 function enable_toparea() {
1551         $('#whitemask').hide();
1552 }
1553
1554 function disable_editing() {
1555         unlocked.contentEditable = false;
1556 }
1557
1558 function enable_editing() {
1559         unlocked.contentEditable = true;
1560 }
1561
1562 function pause()
1563 {
1564         // advanceButton.disabled = true;
1565         // retractButton.disabled = true;
1566         // cursorButton.disabled = true;
1567         // bottomButton.disabled = true;
1568         disable_toparea();
1569         disable_editing();
1570 }
1571
1572 function resume()
1573 {
1574         // advanceButton.disabled = false;
1575         // retractButton.disabled = false;
1576         // cursorButton.disabled = false;
1577         // bottomButton.disabled = false;
1578         if (!matita.disambMode) {
1579                 enable_toparea();
1580                 enable_editing();
1581         }
1582 }
1583