/*
 *    techexploror.js
 *    Copyright  2000 by the IBM Corporation.  All Rights Reserved.
 *
 *    Email to techexpl@us.ibm.com.
 *    Author: Thomas Ling, lingt@us.ibm.com
 */


function isNetscape() 
{
    return navigator.appName == "Netscape";
}


function isIE()
{
    return navigator.appName == "Microsoft Internet Explorer";
}


function isIntroPluginNetscape( ) 
{
    if ( !isNetscape() )
        return false;

    return navigator.plugins["IBM techexplorer Hypermedia Browser, Introductory Edition"] != null;
}


function isProPluginNetscape( ) 
{
    if ( !isNetscape() )
        return false;

    var bPro = (navigator.plugins["IBM techexplorer Hypermedia Browser [Trial Version], Professional Edition"] != null ||
                navigator.plugins["IBM techexplorer Hypermedia Browser, Professional Edition"] != null);

    return bPro;
}


function isActiveXControl( ) 
{
    if ( !isIE() )
        return false;

    var aObjects = document.body.all.tags("OBJECT");
 
    if (aObjects.length > 0)
    {
	    i = 0;
	    while ( aObjects.length > i)
		{
		    if (aObjects[i].classid == "clsid:5AFAB315-AD87-11D3-98BB-002035EFB1A4")
			{ return true; }
			i++;
		}
    }
    
    return false;
}


function isTEJavaEnabled()
{
    if ( (isNetscape() && isProPluginNetscape()) || isActiveXControl() )
    {
        return true;
    }

    return false;
}


function setTEDimensions(dummyTE, latexString, width, oldheight)
{
    if ( isTEJavaEnabled() )
    {
        newheight = 0;
        count     = 0;
        
        while ( !dummyTE.isReady() );

        newheight = dummyTE.getHeightFromTeXStringAndWidth( latexString, width );
    }
    else
    {
        newheight = oldheight;      
    }

    return newheight;
}


function displayAutosizeLaTeX(dummyTE, latexString, defaultWidth, defaultHeight)
{
    var content = AutosizeLaTeXString(dummyTE, latexString, defaultWidth, defaultHeight)
    document.write( content );
    document.close();

}


function AutosizeLaTeXString(dummyTE, latexString, defaultWidth, defaultHeight)
{
    var content = "";

    if (isActiveXControl())
    {
        content += "<OBJECT align=absmiddle CLASSID='clsid:5AFAB315-AD87-11D3-98BB-002035EFB1A4'CODEBASE='AXTCHEXP.OCX'/>";
        content += "<PARAM NAME='DataType' VALUE='0'/>";
        content += "<PARAM NAME='Data' VALUE='" + latexString +"'/>";
        content += "<PARAM NAME='AutoSize' VALUE='TRUE'/>";
        content += "</OBJECT/>";
    }
    else 
    { 
        width = 0; height = 0; depth = 0; count = 0;

        if (isNetscape() && isProPluginNetscape())
        {
            while ( !dummyTE.isReady() );

            width  = dummyTE.getWidthFromTeXString( latexString );
            height = dummyTE.getHeightFromTeXString( latexString );
            depth  = dummyTE.getDepthFromTeXString( latexString );
        }
        else
        {
            width  = defaultWidth;
            height = defaultHeight;
            depth  = defaultHeight/2;
        }

        content = content + "<EMBED align=absmiddle WIDTH=" + width + " HEIGHT=" + height;
        content = content + " TYPE=\"application/x-techexplorer\" ";
        content = content + " TEXDATA=\"" + latexString + "\" >";
    }
    return content;
}


function displayLaTeXWithFixedWidth(dummyTE, latexString, width, height, controlName, pluginName)
{
    var newheight = setTEDimensions(dummyTE, latexString, width, height )
    content = "";
        
    if (isActiveXControl())
    {
        content = content + "<OBJECT CLASSID=\"clsid:5AFAB315-AD87-11D3-98BB-002035EFB1A4\"";

        if (controlName != "")
        {
            content = content + " NAME='" + controlName + "' ";
        }

        content = content + " WIDTH=" + width + " HEIGHT=" + newheight + ">";
        content = content + " <PARAM NAME=\"DataType\" VALUE=\"0\">";
        content = content + " <PARAM NAME=\"Data\" VALUE=\"" + latexString + "\" >";
        content = content + "</OBJECT>";
    } 
    else  
    {
        content = content + "<EMBED WIDTH=" + width + " HEIGHT=" + newheight;

        if (pluginName != "")
        {
            content = content + " NAME='" + pluginName + "' ";
        }
        
        content = content + " TYPE=\"application/x-techexplorer\" ";
        content = content + " TEXDATA=\"" + latexString + "\" >";
        content = content + "</EMBED>";    
    }

    document.write( content );
    document.close();
}
