var isAppletLoaded = 0;
var isPageLoaded = 0;

/* ----------------------------------------------------------------
//Helper methods
 */
function createApplet(debug)
{
    // Change these variable, if need be. 
    // Leave the rest of the function!
    var className = "uk.co.westhawk.examplev1.GenericGetOne";
    var jar = "GenericGetOne.jar";


    if (debug == null)
    {
        debug = 0;
    }

    document.write('<object id="snmpApplet"');
    if (navigator.userAgent.toLowerCase().indexOf("msie") > -1)
    {
        //IE
        document.write('classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"');
        document.write('height="1" width="1"');
        document.write('hspace="0" vspace="0" align="middle">');
        document.write('<param name="code" value="' + className + '"/>');
        document.write('<param name="archive" value="' + jar + '"/>');
    }
    else
    {
        //Mozilla etc.
        document.write('classid="java:' + className + '.class"');
        document.write('type="application/x-java-applet"');
        document.write('archive="' + jar + '"');
        document.write('height="1" width="1"');
        document.write('hspace="0" vspace="0" align="middle">');
    }
    document.write('<param name="mayscript" value="true"/>');
    document.write('<param name="debug" value="' + debug + '"/>');
    document.write('<\/object>');
}

function getSnmpApplet()
{
    var applet = document.getElementById('snmpApplet');
    return applet;
}


/* ----------------------------------------------------------------
// All the internal js functions 
 */
function pageloaded()
{
    isPageLoaded = 1;
    requestTemperature();
}

function requestTemperature()
{
    // alert('requestTemperature');
    var loaded = isPageLoaded + isAppletLoaded;
    if (loaded == 2)
    {
        requestOutside();
        requestInside();
    }
}

function setOutsideError(error)
{
    // alert(celsius + " Celsius");
    var valueElem = document.getElementById('outside');
    if (valueElem)
    {
        valueElem.innerHTML = error;
    }
}

function setInsideError(error)
{
    // alert(celsius + " Celsius");
    var valueElem = document.getElementById('inside');
    if (valueElem)
    {
        valueElem.innerHTML = error;
    }
}

function setOutside2(celsius2)
{
    var valueElem = document.getElementById('outside');
    var top_td = document.getElementById('drawOutsideTop');
    var base_td = document.getElementById('drawOutsideBase');
    var therm_div = document.getElementById('outside_div');
    var therm_base = document.getElementById('therm_outside_base');
    setTemperature(celsius2, valueElem, top_td, base_td,
        therm_div, therm_base);
}

function setInside2(celsius2)
{
    var valueElem = document.getElementById('inside');
    var top_td = document.getElementById('drawInsideTop');
    var base_td = document.getElementById('drawInsideBase');
    var therm_div = document.getElementById('inside_div');
    var therm_base = document.getElementById('therm_inside_base');
    setTemperature(celsius2, valueElem, top_td, base_td,
        therm_div, therm_base);
}

function setTemperature(celsius2, valueElem, top_td, base_td,
    therm_div, therm_base)
{
    var celsius = parseInt(celsius2);
    // alert(celsius + " Celsius");

    // range -10 -- 38 Celsius
    var max = parseInt(38);
    var min = parseInt(-10);
    var deltaRange = max - min;

    // percentage doesn't work in IE, need to use px. :-(
    // cannot get proper height in Safari, so hard code it.
    var totalHeight = 153;
    // alert("totalHeight=" + totalHeight);

    var newHeight;
    if (celsius >= max)
    {
        newHeight = totalHeight-1;
        // alert("Max");
    }
    else 
    {
        if (celsius <= min)
        {
            newHeight = 0;
        }
        else
        {
            var offset = (celsius - min);
            if (offset == 0)
            {
                offset = 1;
            }
            newHeight = ((offset / deltaRange) * totalHeight) + 1;
            newHeight = Math.floor(newHeight);
        }
    }

    if (navigator.userAgent.toLowerCase().indexOf("msie") > -1)
    {
        newHeight++;
    }

    var celsiusText = celsius + ' Celsius';
    if (valueElem)
    {
        valueElem.innerHTML = celsiusText;
    }
    top_td.title = celsiusText;
    base_td.title = celsiusText;
    therm_div.title = celsiusText;
    therm_base.title = celsiusText;

    /*
    */
    var newHeightS = newHeight + "px";
    therm_div.style.height = newHeightS;

    // alert("newHeight=" + newHeight + ", clientHeight=" + therm_div.clientHeight);
}


/* ----------------------------------------------------------------
// All the js functions that call the applet
 */
function send2(host, port, comm, oid, callback) 
{
    /*
    alert('send2: ' + host + ', ' + port + ', ' 
        + comm + ', ' + oid + ', ' + callback);
     */
    var snmpApp = getSnmpApplet();
    if (snmpApp)
    {
        snmpApp.send(host, port, comm, oid, callback);
    }
    else
    {
        alert('Error: No snmpApp!');
    }
    return false;
}

function requestOutside()
{
    // alert('requestOutside');
    var host = '192.67.4.166';
    var port = '161';
    var comm = 'public';

    // sensValue.1 in Celsius (temp * 10)
    var oid = '1.3.6.1.4.1.21796.3.3.3.1.6';
    var callback = 'setOutside';
    send2(host, port, comm, oid, callback);
}

function requestInside()
{
    /*
    */
    // alert('requestInside');
    var host = 'zimbra.westhawk.co.uk';
    var port = '161';
    var comm = 'public';

    // cpqHeTemperatureCelsius.1 in Celsius (temp)
    var oid = '1.3.6.1.4.1.232.6.2.6.8.1.4.1';
    var callback = 'setInside';
    send2(host, port, comm, oid, callback);
}


/* ----------------------------------------------------------------
// All the functions called by the applet
 */
function loaded()
{
    // Do NOT call this function yourself. It is called by the
    // applet when loaded.

    // alert('loaded');
    // Enable the 'Get Next' button
    var getnextElem = document.getElementById('gettemp');
    if (getnextElem)
    {
        getnextElem.disabled = false;
    }

    isAppletLoaded = 1;
    requestTemperature();
}

// This is the name of the callback function passed to the applet
// in send()
function setOutside(host, port, comm, requestOid, responseOid, value, error)
{
    /*
    alert('setOutside: ' + host + ', ' + port + ', ' + comm 
        + ', ' + requestOid + ', ' + responseOid 
        + ', ' + value + ', ' + error);
     */
    if (value)
    {
        var celsius = parseInt(value) / 10;
        setOutside2(celsius);
    }
    else
    {
        setOutsideError(error);
    }
}

// This is the name of the callback function passed to the applet
// in send()
function setInside(host, port, comm, requestOid, responseOid, value, error)
{
    /*
    alert('setInside: ' + host + ', ' + port + ', ' + comm 
        + ', ' + requestOid + ', ' + responseOid 
        + ', ' + value + ', ' + error);
     */
    if (value)
    {
        // This is cpu rack temperature, subtract 15 to make it room
        // temperature
        var celsius = parseInt(value) - 15;
        setInside2(celsius);
    }
    else
    {
        setInsideError(error);
    }
}


