/* 
 *  Handles indirect events from server.  updating plot, queries, etc.
 */
function PreviewBot(){
       
    this.SetPreview = SetPreview;
    this.TogglePButton = TogglePButton;

    function TogglePButton(forceDisable){
    return;
         if(frames['pframe'].document.getElementById('canPlot').value == 'False' || forceDisable){
            document.getElementById('pbutton').style.borderStyle = 'inset';
            document.getElementById('pbutton').disabled = true;
         }else{
            document.getElementById('pbutton').style.borderStyle = 'solid';
            document.getElementById('pbutton').disabled = false;
         }
    }
    function SetPreview(){
        p = document.getElementById('centerPane');   
        while(p.childNodes.length > 0)
            p.removeChild(p.childNodes[0]);
        var i = null;
        this.TogglePButton(false);
        switch(frames['pframe'].document.getElementById('status').value){
            case "0"://initial load
                p.innerHTML = frames['pframe'].document.getElementById('welcomeText').innerHTML;
                document.getElementById('postPlotDiv').style.visibility = 'hidden';
                break;
            case "1":
                var v = frames['pframe'].document.getElementById('tokens').innerHTML
                if(v != null && v != '')
                    p.innerHTML = frames['pframe'].document.getElementById('tokens').innerHTML;
                else 
                    p.innerHTML = frames['pframe'].document.getElementById('welcomeText').innerHTML;
                document.getElementById('postPlotDiv').style.visibility = 'hidden';
                break;
            case "2"://new plot
                i = frames['pframe'].document.getElementById('pimg');
                i = document.importNode(i,true);
                var tDiv = document.getElementById('topDiv');
                tDiv.style.height = document.body.clientHeight;
                tDiv.style.width = document.body.clientWidth;
                p.appendChild(i);
                document.getElementById('qinfoPane').innerHTML = frames['pframe'].document.getElementById('tokens').innerHTML;
                document.getElementById('queryPane').innerHTML = frames['pframe'].document.getElementById('dmdx').innerHTML;
                document.getElementById('uxmin').value = frames['pframe'].document.getElementById('pxmin').value;
                document.getElementById('uxmax').value = frames['pframe'].document.getElementById('pxmax').value;
                document.getElementById('uymin').value = frames['pframe'].document.getElementById('pymin').value;
                document.getElementById('uymax').value = frames['pframe'].document.getElementById('pymax').value;
                ceb.DontLimit();
                
                peb.SetLimits(
                  frames['pframe'].document.getElementById('pxmin').value,
                  frames['pframe'].document.getElementById('pxmax').value,
                  frames['pframe'].document.getElementById('pymin').value,
                  frames['pframe'].document.getElementById('pymax').value
                );
                peb.SetMargins(
                    frames['pframe'].document.getElementById('ptopm').value,
                    frames['pframe'].document.getElementById('prightm').value,
                    frames['pframe'].document.getElementById('pbottom').value,
                    frames['pframe'].document.getElementById('pleftm').value,
                    frames['pframe'].document.getElementById('pwidth').value,
                    frames['pframe'].document.getElementById('pheight').value
                );
                peb.InitCanvas();
                document.getElementById('postPlotDiv').style.visibility = 'visible';
                break;
            case "3"://error
                e = frames['pframe'].document.getElementById('perror');
                e = document.importNode(e,true);
                p.appendChild(e);
                document.getElementById('postPlotDiv').style.visibility = 'hidden';
                break;
        }
        
        var stat = document.getElementById('statusThingy');
        stat.style.background = 'green';    
        stat.style.color = 'white';
        stat.innerHTML = 'Ready';
    }
}