/*
Customisation of the ASCIIMathMLeditor to open a window from which the asciimath can be printed and saved.
28/3/06 Only works with Firefox and Mozilla (?) at moment.  See notes at end about IE.
*/

//Don't like using doubleblanks to indicate ASCIIMath
doubleblankmathdelimiter = false;

//Set up global variable for the print window
var pwin;
var docloaded;

function AMblankPrintWindow() {
    pwin = window.open("blank_window.html","PrintWindow","resizable,status,menubar,scrollbars,width=560,height=500");
    pwin.focus();
    }

function AMprintWindow() {
    if (((pwin == null)||(pwin.closed))||(!pwin.document.getElementById("rawAM"))) pwin = window.open("change_this_name_before_saving.html","PrintWindow","resizable,status,menubar,scrollbars,width=560,height=500");
    AMeditor2printWindow();
    }


var attempts = 0

function AMeditor2printWindow(){
    docloaded = pwin.docloaded
    if(!docloaded){
        if(attempts++ < 60){
            setTimeout("AMeditor2printWindow();", 1000);
        } else {
            attempts = 0;
        }
    } else if(isIE){
        mathcolor="darkblue";
        var outnode = pwin.document.getElementById("forprint");
        var outnodehtml = document.getElementById("outputNode").innerHTML;
        outnode.innerHTML = outnodehtml;
        // put a copy of the ASCIIMath as a comment in the div "rawAM"
            var strnode = pwin.document.getElementById("rawAM");
            var str = document.getElementById("inputText").value;
            newnode = pwin.document.createElement("div");
            newnode.setAttribute("id","rawAM");
            newnode.appendChild(pwin.document.createComment(str));
            strnode.parentNode.replaceChild(newnode,strnode);
        // the following line is required so that the content of the generated window can be saved 
        // thanks to Keith Brunton for this trick
        pwin.document.write(pwin.document.body.parentNode.innerHTML);
        pwin.document.close();
        pwin.focus();
        mathcolor="red";
        attempts=0;    
    } else {
        mathcolor="darkblue";
        // get the output node we want to send the output to
            var outnode = pwin.document.getElementById("forprint");
        //script from AMdisplay(now) changing output node to be in pwin
            var str = document.getElementById("inputText").value;
            newnode = pwin.document.createElementNS("http://www.w3.org/1999/xhtml","div");
            newnode.setAttribute("id","forprint");
            outnode.parentNode.replaceChild(newnode,outnode);
            outnode = pwin.document.getElementById("forprint");
            var n = outnode.childNodes.length;
            for (var i = 0; i < n; i++)
                outnode.removeChild(outnode.firstChild);
            outnode.appendChild(pwin.document.createComment(str));
            AMprocessNode(outnode,true);
        // put a copy of the ASCIIMath as a comment in the div "rawAM"
            var strnode = pwin.document.getElementById("rawAM");
            newnode = pwin.document.createElementNS("http://www.w3.org/1999/xhtml","div");
            newnode.setAttribute("id","rawAM");
            newnode.appendChild(pwin.document.createComment(str));
            strnode.parentNode.replaceChild(newnode,strnode);
        pwin.focus();
        mathcolor="red";
        attempts= 0;
    }
}

function AMprintWindow2editor(){
    if (((pwin == null)||(pwin.closed))||(!pwin.document.getElementById("rawAM"))) return;
    var str = pwin.document.getElementById("rawAM").firstChild.data;
    document.getElementById("inputText").value = str;
    AMdisplay(true);
}



/* 
28/3/06
Attempt to get code to work with IE.  It works okay for displaying a print window.  
However when saving with IE the dynamically generated content doesn't save.  A way 
round this might be to use the document.open() method and write the whole file directly.

Note: The code above doesn't work with IE because nodes can't be transferred from 
document to pwin.document .



function AMeditor2printWindowIE(){
    var outnode = pwin.document.getElementById("forprint");
    var str = document.getElementById("outputNode").innerHTML;
    outnode.innerHTML = str;
    // put a copy of the ASCIIMath as a comment in the div "rawAM"
        var strnode = pwin.document.getElementById("rawAM");
        newnode = pwin.document.createElement("div");
        newnode.setAttribute("id","rawAM");
        newnode.appendChild(pwin.document.createComment(str));
        strnode.parentNode.replaceChild(newnode,strnode);
    pwin.focus();
}

*/
