var FUTURES_CALCULATOR =
{
    futures : Object(),
    AJAX_PROVIDER : "com.iv.service.calculator.FuturesCalculatorAjaxProvider",
    SQL_DATE : null,

    init: function()
    {
        AJAX.onError = FUTURES_CALCULATOR.onError;
        var fd = FORM_UTILS.getElementValue("futures_data").split("|");
        for (var i = 0; i < fd.length; i++)
        {
            var fs = new Object();
            var f = fd[i].split("{}");
            fs.expiration = f[0];
            fs.price = f[1];
            fs.symbol = f[2];
            FUTURES_CALCULATOR.futures[fs.expiration] = fs;
        }
        FUTURES_CALCULATOR.SQL_DATE = new Date(FORM_UTILS.getElementValue("sql_date"));
        FORM_UTILS.setElementValue("fut_exp",FORM_UTILS.getElementValue("fut_date"));
        var expiration = FORM_UTILS.getElementValue("fut_exp");
        var fs = FUTURES_CALCULATOR.futures[expiration];
        FORM_UTILS.setElementValue("fut_symb", fs.symbol);
        FORM_UTILS.setElementValue("price", fs.price);
        FUTURES_CALCULATOR.findOptions();
        window.setInterval("FUTURES_CALCULATOR.showLoading()", 50);
    },

    sendRequest : function(map)
    {
        AJAX.onReady = FUTURES_CALCULATOR.onAjaxResponse;
        AJAX.open(FUTURES_CALCULATOR.AJAX_PROVIDER);
        AJAX.send(map);
    },

    showLoading : function()
    {
        if (AJAX.count == 0)
        {
            FORM_UTILS.hideElement("loading");
        } else
        {
            FORM_UTILS.showElement("loading");
        }
    },
    selectFutures : function()
    {
        var expiration = FORM_UTILS.getElementValue("fut_exp");
        var fs = FUTURES_CALCULATOR.futures[expiration];
        FORM_UTILS.setElementValue("fut_symb", fs.symbol);
        FORM_UTILS.setElementValue("price", fs.price);
        FUTURES_CALCULATOR.findOptions();
    },

    selectOptions : function()
    {
        if (FORM_UTILS.getElementValue("exp_date") == "FLEX")
            return;
        FUTURES_CALCULATOR.setDaysToExpiration();
        var map = new XMLMap();
        map.put("action", "get_option");
        map.loadFromForm("instrument_id", "price", "fut_exp", "exp_date");
        FUTURES_CALCULATOR.sendRequest(map);
    },

    findOptions : function()
    {
        var map = new XMLMap();
        map.put("action", "find_options");
        map.loadFromForm("instrument_id", "price", "fut_exp");
        FUTURES_CALCULATOR.sendRequest(map);
    },

    onCalculateButton : function()
    {
        var map = new XMLMap();
        map.put("action", "calculate");
        map.loadFromForm("style", "price", "strike", "days_to_exp",
                "volatility", "interest_rate");
        FUTURES_CALCULATOR.sendRequest(map);
    },

    onCalculateVolatilityButton : function()
    {
        var map = new XMLMap();
        map.put("action", "calculate_volatility");
        map.loadFromForm("style", "price", "strike", "days_to_exp",
                "volatility", "interest_rate", "impl_call_put", "impl_price");
        FUTURES_CALCULATOR.sendRequest(map);
    },

    onDaysToExpChange : function()
    {
        FUTURES_CALCULATOR.setExpDate();
        var map = new XMLMap();
        map.put("action", "calculate_interest_rate");
        map.loadFromForm("days_to_exp");
        FUTURES_CALCULATOR.sendRequest(map);
    },

    onStrikeChange : function()
    {
        var map = new XMLMap();
        map.put("action", "strike_change");
        map.loadFromForm("instrument_id", "fut_exp", "days_to_exp","strike", "volatility");
        FUTURES_CALCULATOR.sendRequest(map);
    },
    setExpDate: function()
    {
        var dte;
        try
        {
            FORM_UTILS.setElementValue("exp_date", "FLEX");
            dte = FORM_UTILS.getElementValue("days_to_exp");
            dte = parseInt(dte);
            var date = new Date();
            date.setTime(dte * 1000 * 60 * 60 * 24 + FUTURES_CALCULATOR.SQL_DATE.getTime());
            var month = date.getMonth() + 1;
            month = month > 9 ? month : "0" + month;
            var day = date.getDate();
            day = day > 9 ? day : "0" + day;
            date = month + "/" + day + "/" + date.getYear();
            FORM_UTILS.setElementValue("exp_date", date);
        } catch(e)
        {
            FUTURES_CALCULATOR.onError("Incorrect number in days to expiration");
        }
    },

    daysBetween : function(date1, date2)
    {
        return Math.round((date2.getTime() - date1.getTime()) / 1000 / 60 / 60 / 24);
    },

    setDaysToExpiration : function()
    {
        var date1 = FUTURES_CALCULATOR.SQL_DATE;
        var date2 = new Date(FORM_UTILS.getElementValue("exp_date"));
        if (isNaN(date2))
        {
            FORM_UTILS.setElementValue("days_to_exp", "0");
        } else
        {
            var days = FUTURES_CALCULATOR.daysBetween(date1, date2);
            FORM_UTILS.setElementValue("days_to_exp", days)
        }
    },

    onAjaxResponse : function()
    {
        FUTURES_CALCULATOR.onError("");
        var map = AJAX.getResponseXMLMap();
        var action = map.get("action");
        switch (action)
                {
            case "find_options":
            {
                var arr = map.getArray("options");
                if (arr.length == 0)
                {
                    FORM_UTILS.setElementValue("strike",
                            FORM_UTILS.getElementValue("price"));
                } else
                {
                    map.loadToForm("strike");
                }
                map.loadToForm("volatility", "interest_rate");
                arr.unshift("FLEX");
                FORM_UTILS.setSelectOptions("exp_date", arr, 1);
                FUTURES_CALCULATOR.setDaysToExpiration();
                if (FORM_UTILS.getElementValue("has_options") == "false")
                {
                    FUTURES_CALCULATOR.onError("No options for this instrument")
                }
                if (FORM_UTILS.getElementValue("days_to_exp") != 0)
                    FUTURES_CALCULATOR.onCalculateButton();
                break;
            }
            case "get_option":
            {
                map.loadToForm("volatility", "interest_rate");
                break;
            }
            case "calculate":
            {
                map.loadToForm("opt_value_call", "opt_value_put", "delta_call",
                        "delta_put", "theta_call", "theta_put", "vega_call", "vega_put",
                        "gamma_call", "gamma_put");
                break;
            }
            case "calculate_volatility":
            {
                map.loadToForm("impl_volatility")
                break;
            }
            case "calculate_interest_rate":
            {
                map.loadToForm("interest_rate");
                break;
            }
            case "strike_change":
            {
                map.loadToForm("volatility");
                break;
            }
            default : alert("switch error!");
        }
    },
    onError : function(description)
    {
        document.getElementById("error_string").innerHTML = "<pre>" + description + "</pre>"
    }
}