/*<![CDATA[  */

/*    
    if (!document.getElementById)
    {
        html='';
        html +='<style type="text/css">';
        html +='.'+ SWAP_CLASS + ' {display:block;}';
        html +='</style>';
        document.write(html);
    }
*/
    var SWAP_CLASS='swap';
    var SWAP_SRC_PREFIX='src';
    var SWAP_DEBUG=true;
    function swap_one(id, src, mode)
    {
        var log='swap_one ';
        if (!document.getElementById) return false; // log + 'no dom ';
        tgt=document.getElementById(id);
        if (!tgt) return false; // log+' not find ' ;
        log+=' #'+tgt.id+' ';
        if (!mode)
            if (tgt.store=='none') mode='show';
            else if (tgt.style.display=='none') mode='show';
            else mode='hide';
        log+=mode+' ';
        if (false) {}
        else if (mode=='show') log+=swap_show(tgt, src);
        else if (mode=='hide') log+=swap_hide(tgt, src);
        return log;
    }

    function swap_show(tgt, src, store)
    {
        var log='swap_show';
        if (!tgt) return log+" no tgt";
        if (!tgt.style) 
        {
            if (!document.getElementById) return log + ' no dom';
            tgt=document.getElementById(tgt);
            log+=' #'+tgt;
        }
        if (!tgt || !tgt.style) return log + ' not a node';
        tgt.style.display='';
        if (store==null || store != false) 
        {
            tgt.store=tgt.style.display;
            log+=" store"+tgt.store+" ";
        }
        // store user action
        if (!src) src=SWAP_SRC_PREFIX + tgt.id;
        if (!src.className) src=document.getElementById(src);
        if (!src) return log+" no src #"+src;
        if (src.value=='+') src.value='-';
        else if (src.className != null) 
        {
            style=src.className.replace(/ ?swap_hide ?/gi, '');
            // style=style+' swap_show';
            src.className=style;
        }
        else if (src.innerHTML) src.innerHTML.replace('+', '-');
        return log;
    }

    function swap_hide(tgt, src, restore)
    {
        var log='swap_hide';
        if (!tgt) return log+" no tgt";
        if (!tgt.style) 
        {
            if (!document.getElementById) return log + ' no dom';
            tgt=document.getElementById(tgt);
            log+=' #'+tgt;
        }
        if (!tgt || !tgt.style) return log + ' not a node';
        // store display
        tgt.display=tgt.style.display;
        if (!restore) 
        {
            tgt.style.display='none'; 
            tgt.store=tgt.style.display; 
        }
        else 
        {
            tgt.style.display=tgt.store;
            log+=' restore ';
        } 
        if (!src) src=SWAP_SRC_PREFIX + tgt.id;
        if (!src.className) src=document.getElementById(src);
        if (!src) return log+" no src #"+src;
        if (src.value=='-') {  src.value='+'; }
        else if (src.tagName != null) 
        {
            
            var style=src.className;
            // style=style.replace(/ ?swap_show ?/gi, '');
            src.className=style+' swap_hide';
        }
        else if (src.innerHTML) src.innerHTML.replace('+', '-');
        return log;
    }

    function swap_over(tgt, src)
    {
        return 'swap_over ' + swap_show(tgt, src, false);
    }
    
    function swap_out(tgt, src)
    {
        return 'swap_out ' + swap_hide(tgt, src, true);
    }
    

    function swap_hideAll(tgt, level, swap_class)
    {
        if (!level) level=0;
        if (!swap_class) swap_class=SWAP_CLASS;
        if (tgt.className) if (tgt.className.search(swap_class) != -1)
            {
                if (level != 0) level--; 
                if (level==0) swap_hide (tgt);
            }
        if (!tgt.childNodes) return;
        var i=0;
        while (tgt.childNodes[i]) 
            {swap_hideAll(tgt.childNodes[i], level, swap_class); i++; }
        return true;
    }    
    function swap_showAll(tgt, level, swap_class)
    {
        if (!level) level=0;
        if (!swap_class) swap_class=SWAP_CLASS;
        if (tgt.className) if (tgt.className.search(swap_class) != -1) 
            {
                if (level !=0) level--; 
                if (level == 0) swap_show(tgt);
            }
        if (!tgt.childNodes) return;
        var i=0;
        while (tgt.childNodes[i]) 
            {swap_showAll(tgt.childNodes[i], level, swap_class); i++; }
    }

    function swap_all(id, level, mode, swap_class)    
    {
        if (!document.getElementById) return false;
        var node=document.getElementById(id);
        if (!node) node=document.documentElement;
        if (mode=='hide') hide=true;
        else if (mode=='show') hide=false;
        else hide=node.swap;
        if (hide) {node.swap=false; return swap_hideAll(node, level, swap_class)}
        else {node.swap=true; return swap_showAll(node, level, swap_class)}
        return false;
    }

    var SWAP_CLASS='xml_margin';


    function save(text, fileName)
    {
        log="";
        if (document.execCommand)
        {
            if (!document.frames[0]) return log + "no frame to write in";
            win=document.frames[0];
            win.document.open("text/html", "replace");
            win.document.write(text);
            win.document.close();
            win.focus();
            win.document.execCommand('SaveAs', fileName);
        }
     /*
     
     function writeToFile(fileName,text) {
 
netscape.security.PrivilegeManager.enablePrivilege('UniversalFileAccess');
    var fileWriter = new java.io.FileWriter(fileName);
    fileWriter.write (text, 0, text.length);
    fileWriter.close();
}

if (document.layers) {
    writeToFile('file.txt','Hello World');
}
		*/
    }

    function dom(xml)
    {
        if (window.DOMParser) return (new DOMParser()).parseFromString(xml, "text/xml");
        else if (window.ActiveXObject)
        {
            var doc = new ActiveXObject("Microsoft.XMLDOM");
            doc.async = false;
            doc.loadXML(xml);
            return doc;
        }
        else
        {
            alert(NOXML);
            return null;
        }
    }
    
    function xml_save()
    {
        if (!document.getElementById) return false;
        o=document.getElementById('edit');
        if (!o.innerText) return false; 
        var edit=o.contentEditable; 
        o.contentEditable=false; 
        var xml=o.innerText; 
        o.contentEditable=edit; 
        var doc=dom(xml); 
        if (!doc || !doc.documentElement) if (!confirm('The document you want to save is not well-formed. Continue ?')) return false;  
        save(xml)
    }
    
    function xml_edit(button, edit)
    {
        if (!document.getElementById) return false;
        if (document.body.isContentEditable==null) return false;
        var o=document.getElementById('edit'); 
        if(!o) return false; 
        if (edit == null) edit=!o.isContentEditable; 
        if (edit) { 
            if (button) button.className='but_in'; 
            o.contentEditable=true;
            document.cookie="edit=true";
        } 
        else { 
            if (button) button.className=''; 
            o.contentEditable=false; 
            document.cookie="edit=false";
        }
    }

    function xml_submit(form)
    {
        if (!form) form=document.forms[0];
        if (!document.getElementById) return false;
        o=document.getElementById('edit'); 
        if (!o.innerText) return false; 
        var edit=o.contentEditable; 
        o.contentEditable=false; 
        var xml=o.innerText; 
        o.contentEditable=edit; 
        var doc=dom(xml); 
        if (!doc || !doc.documentElement) if (!confirm('The document you want to upload is not well-formed. Continue ?')) return false;  
        if (!form || !form.xml) return false;
        form.xml.value=xml;
        if(form.url) form.url.value=window.location.href;
        return true;
    }
    
    function edit_key(o)
    {
        if (!event) return;
        var key=event.keyCode;
        if (!o) o=document.getElementById('edit');
        if (!o.isContentEditable) return true;
        if (key==9) {
            if (event.shiftKey) document.execCommand("Outdent"); 
            else document.execCommand("Indent"); 
            window.event.cancelBubble = true;
            window.event.returnValue = false;
            return false;
        }
        if (key==83) {
            if (!event.ctrlKey) return;
            xml_save();
            window.event.cancelBubble = true;
            window.event.returnValue = false;
            return false;
        }
        if (key==85) {
            if (!event.ctrlKey) return;
            form=document.forms[0];
            if (!form) return;
            if (xml_submit(form)) form.submit();
            window.event.cancelBubble = true;
            window.event.returnValue = false;
            return false;
        }
    }
    
    function xml_load()
    {
        if (!document.getElementById || !document.execCommand || document.body.isContentEditable==null) return false; 
        var o=document.getElementById('bar'); 
        if (!o || !o.style) return; 
        o.style.display='';
        xml_edit(document.forms[0].butEdit, (document.cookie.search("edit=true") != -1));
    }
/*]]>*/

