var CALCULATOR = {
    futures : Object(),
    AJAX_PROVIDER : "com.iv.service.calculator.CalculatorAjaxProvider",
    SQL_DATE : null,

    init: function(){
        AJAX.onError = 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];
            CALCULATOR.futures[fs.expiration] = fs;
        }
        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 = CALCULATOR.futures[expiration];
        FORM_UTILS.setElementValue("fut_symb", fs.symbol);
        FORM_UTILS.setElementValue("price", fs.price);
        CALCULATOR.findOptions();
*/
        window.setInterval("CALCULATOR.showLoading()", 50);
    },

    sendRequest : function(map){
        AJAX.onReady = CALCULATOR.onAjaxResponse;
        AJAX.open(CALCULATOR.AJAX_PROVIDER);
        AJAX.send(map);
    },

    showLoading : function(){
        if (AJAX.count == 0){
            FORM_UTILS.hideElement("loading");
        }
        else{
            FORM_UTILS.showElement("loading");
        }
    },





    getIRate : function(SID, currency){
        var map = new XMLMap();
        map.put("action", "interpolate_interest_rate");
        map.put("SID", SID);
        map.put("currency", currency);
        map.loadFromForm("daysexp", "strike");
        CALCULATOR.sendRequest(map);
    },

    putSelectedValue : function(map, id){
        var elem = document.getElementById(id);
        map.put(id, elem.options[elem.selectedIndex].value);
      },

    calcGrek : function(action, tp, is_stock, SID, currency, rid){
        var map = new XMLMap();
        map.put("action", action == 0 ? "calculate_greeks" : "calculate_iv");
        map.put("is_stock", is_stock);
        map.put("SID", SID);
        map.put("currency", currency);
        map.put("rid", rid);
        map.loadFromForm("days", "price", "strike", "divyield", "daysexp", "vola", "intrate");

        CALCULATOR.putSelectedValue(map, "country");

        if (document.getElementById("freq")){
          CALCULATOR.putSelectedValue(map, "freq");
          map.loadFromForm("divlastdate");
          }
        if (action != 0){
          map.loadFromForm("opt_price");
          map.put("tp", tp);
          }
        CALCULATOR.sendRequest(map);
    },




    selectFutures : function(){
        var expiration = FORM_UTILS.getElementValue("fut_exp");
        var fs = CALCULATOR.futures[expiration];
        FORM_UTILS.setElementValue("fut_symb", fs.symbol);
        FORM_UTILS.setElementValue("price", fs.price);
        CALCULATOR.findOptions();
    },
    selectOptions : function(){
        if (FORM_UTILS.getElementValue("exp_date") == "FLEX")
            return;
        CALCULATOR.setDaysToExpiration();
        var map = new XMLMap();
        map.put("action", "get_option");
        map.loadFromForm("instrument_id", "price", "fut_exp", "exp_date");
        CALCULATOR.sendRequest(map);
    },

    findOptions : function(){
        var map = new XMLMap();
        map.put("action", "find_options");
        map.loadFromForm("instrument_id", "price", "fut_exp");
        CALCULATOR.sendRequest(map);
    },

    onCalculateButton : function(){
        var map = new XMLMap();
        map.put("action", "calculate");
        map.loadFromForm("style", "price", "strike", "days_to_exp",
                "volatility", "interest_rate");
        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");
        CALCULATOR.sendRequest(map);
    },

    onDaysToExpChange : function(){
        CALCULATOR.setExpDate();
        var map = new XMLMap();
        map.put("action", "calculate_interest_rate");
        map.loadFromForm("days_to_exp");
        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");
        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 + 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){
            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 = 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 = CALCULATOR.daysBetween(date1, date2);
            FORM_UTILS.setElementValue("days_to_exp", days)
        }
    },

    onAjaxResponse : function(){
        CALCULATOR.onError("");
        var map = AJAX.getResponseXMLMap();
        var action = map.get("action");
        switch (action){





            case "interpolate_interest_rate":{
                map.loadToForm("intrate", "vola");
                break;
            }

            case "calculate_iv":{
                document.ivola.ivola.value = map.get("ivola");
                break;
            }

            case "calculate_greeks":{
                map.loadToForm("oprice_c", "oprice_p");
                map.loadToForm("delta_c", "delta_p");
                map.loadToForm("gamma_c", "gamma_p");
                map.loadToForm("theta_c", "theta_p");
                map.loadToForm("vega_c", "vega_p");
                map.loadToForm("rho_c", "rho_p");
                map.loadToForm("symbol_c", "symbol_p");
                if (map.get("alpha_c"))
                  map.loadToForm("alpha_c", "alpha_p");
                if (map.get("bid_ask_c"))
                  map.loadToForm("bid_ask_c", "bid_ask_p");
                break;
            }




            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);
                CALCULATOR.setDaysToExpiration();
                if (FORM_UTILS.getElementValue("has_options") == "false"){
                    CALCULATOR.onError("No options for this instrument")
                }
                if (FORM_UTILS.getElementValue("days_to_exp") != 0)
                    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>"
    }
}
