var calc_image = '../images/calc.gif';

var calc_start_num = false;
var calc_cur_oper = '';
var calc_acc = 0;
var calc_prev_oper = 0;
var calc_prev_entry = 0;

var calc_field;

function calc_num_value()
{
    if (calc_field.value == '0.')
        return calc_field.value;
    else if (isNaN(calc_field.value))
        return 0;
    else
        return parseFloat(calc_field.value);
}

function calc_perform_oper(oper, val1, val2)
{
    switch(oper)
    {
    case '*':
        return val1 * val2;
    case '/':
        return val1 / val2;
    case '+':
        return Math.round(
                val1 * 10000000000000 +
                val2 * 10000000000000) /
               10000000000000;
    case '-':
        return Math.round(
                val1 * 10000000000000 -
                val2 * 10000000000000) /
               10000000000000;
    default:
        return val2;
    }
}

function calc_press(button)
{
    switch (button)
    {
    case 'CE':
        calc_start_num = true;
        calc_field.value = "0";
        return;

    case 'C':
        calc_acc = 0;
        calc_prev_entry = 0;
        calc_cur_oper = "";
        calc_prev_oper = "";
        calc_start_num = true;
        calc_field.value = "0";
        return;

    case '+/-':
        calc_field.value = calc_num_value() * -1;
        return;

    case ',':
    case '.':
        if (calc_start_num || isNaN(calc_field.value))
        {
            calc_field.value = "0.";
            calc_start_num = false;
        }
        else
        {
            if (calc_field.value.indexOf(".") == -1)
                calc_field.value += ".";
        }
        return;

    case '+':
    case '-':
    case '/':
    case '*':
        calc_prev_oper = button;

        if (calc_start_num)
        {
            calc_cur_oper = button;
            return;
        }

        // No break here.

    case '=':
        calc_start_num = true;

        if (button == '=' && calc_cur_oper != '=')
        {
            calc_prev_entry = calc_num_value();
        }

        if (button == '=' && calc_cur_oper == '=')
        {
            calc_acc = calc_perform_oper(
                calc_prev_oper, calc_acc, calc_prev_entry)
        }
        else
            calc_acc = calc_perform_oper(
                calc_cur_oper, calc_acc, calc_num_value());

        calc_field.value = calc_acc;
        calc_cur_oper = button;

        return;
    }

    if (calc_start_num)
    {
        calc_field.value  = button;
        calc_start_num = false;
    }
    else
    {
        if (calc_num_value() == "0")
            calc_field.value = button;
        else
            calc_field.value += button;
    }
}

function calc_setup(frm, inp)
{
    if (!document.getElementById)
        return;

    document.write(
        '<a href="javascript:void(0);" ' +
        'onclick="calc_show_hide(\'' +
        frm + '\', \'' + inp +
        '\')"><img  style="border:none" ' +
        'src="' + calc_image +
        '"><' + '/a>');
}

function move_box(an, box)
{
    var cleft = 0;
    var ctop = 0;
    var obj = an;

    while (obj.offsetParent)
    {
        cleft += obj.offsetLeft;
        ctop += obj.offsetTop;
        obj = obj.offsetParent;
    }

    box.style.left = cleft + 'px';

    ctop += an.offsetHeight + 2;

    // Handle Internet Explorer body margins,
    // which affect normal document, but not
    // absolute-positioned stuff.
    if (document.body.currentStyle &&
        document.body.currentStyle['marginTop'])
    {
        ctop += parseInt(
            document.body.currentStyle['marginTop']);
    }

    box.style.top = ctop + 'px';
}

function calc_show_hide(frm, inp)
{
    var boxcalc = document.getElementById('attach_calc_div');
    var to_obj = document.forms[frm].elements[inp];

    if (boxcalc == null)
        return;

    if (to_obj != calc_field ||
        boxcalc.style.display=='none')
    {
        // Show and move calculator.

        boxcalc.style.position='absolute';
        move_box(to_obj, boxcalc);
        calc_field = to_obj;
        boxcalc.style.display='block';
    }
    else
        // Hide currently shown calculator.
        boxcalc.style.display='none';
    return false;
}

function calc_hide()
{
    document.getElementById('attach_calc_div')
        .style.display = 'none';
}
