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