﻿var _maxTop = 20;
var _stack = new Array();
var _stackShow = new Array();
var _top = -1;

var _formula = "";
var _showFormula = "";
var _memory = "";
var _showMemory = "";

function PushToStack() {
    if (_top + 1 < _maxTop) {
        _stack[++_top] = _formula;
        _stackShow[_top] = _showFormula;
    } else {
        for (i = 0; i < _top; i++) {
            _stack[i] = _stack[i + 1];
            _stackShow[i] = _stackShow[i + 1];
        }
        _stack[_top] = _formula;
        _stackShow[_top] = _showFormula;
    }
    SetControls();
}

function Undo() {
    if (_top == -1) {
        return false;
    }
    _formula = _stack[_top];
    _showFormula = _stackShow[_top--];
    SetControls();
    return true;
}

function EnableControl(control, enable) {
    document.getElementById(control).disabled = !enable;
}

function SetFormula() {
    _showFormula = Find("HIHShowFormula").value;
    _formula = Find("HIHFormula").value;
    SetControls();
}

function SetControls() {
    if (_top == -1)
        EnableControl("BUndo", false);
    else
        EnableControl("BUndo", true);

    if (_memory == "")
        EnableControl("BRetriveMemory", false);
    else
        EnableControl("BRetriveMemory", true);

    document.getElementById("TShowFormula").value = _showFormula;
    document.getElementById("TFormula").value = _formula;
}

function AddToFormulaWithParans(action) {
    PushToStack();
    _formula += action + "(";
    _showFormula += action + "(";
    SetControls();
}

function AddToFormula(action) {
    PushToStack();
    _formula += action;
    _showFormula += action;
    SetControls();
}

function ClearFormula() {
    PushToStack();
    _formula = "";
    _showFormula = "";
    SetControls();
}

function SaveFormula() {
    _memory = _formula;
    _showMemory = _showFormula;
    SetControls();
}

function AddParameterToFormula(parameter) {

    PushToStack();

    parameter.toString().toLowerCase();

    switch (parameter) {

        case "Price":
            _formula += "[" + parameter + "]";
            _showFormula += "[" + "مبلغ قرارداد" + "]"; break;

        case "Volume":
            _formula += "[" + parameter + "]";
            _showFormula += "[" + "حجم قرارداد" + "]"; break;

        case "memory":
            _formula += _memory;
            _showFormula += _showMemory; break;

        case "addlist":
        case "list":
            _formula += "[" + Find("LSTAgent").value + "]";
            _showFormula += "[" + Find("LSTAgent").options[Find("LSTAgent").selectedIndex].text + "]"; break;

        case "addnumber":
        case "number":
            _formula += Find("TNumber").value;
            _showFormula += Find("TNumber").value; break;

    }
    SetControls();
}

function Find(name) {
    var found = null;
    var elements = document.all;
    for (i = 0; i < elements.length; i++) {
        if (elements[i].id.indexOf(name) != -1) {
            found = elements[i];
            break;
        }
    }
    return found;
}