Java Script Does not Calculate on Condition; Instead posts the condiotion

Discussion in 'JavaScript' started by immortal_king, Jan 21, 2011.

  1. #1
    The following form calculates the Profit or Loss on the factors provided in the Script.

    I wanted to add a condition where if the TRADE field is "BOUGHT" then the calculation to attain the Field POINTS should be (exit_rate - entry_rate) and while the the TRADE field is "SOLD" then the calculation to attain the Field POINTS should be (entry_rate - exit_rate).

    Instead the information in Trade Field has no effect but the data provided in "entry_rate" and "exit_rate" but posts BOUGHT in "trade"field.

    Please Help and thanks in advance.

    ============================================================

    <html>
    <head><title>Profit / Loss Calculator</title></head>
    <body>


    <div align="center">
    <form>
    <table width="757" border="0" align="center" cellpadding="3" cellspacing="3">
    <tr>
    <td>&nbsp;</td>

    <td><div align="center">Contract</div></td>
    <td><div align="center">Trade</div></td>
    <td><div align="center">Entry Date</div></td>
    <td><div align="center">Entry Rate</div></td>
    <td><div align="center">Exit Date</div></td>
    <td><div align="center">Exit Rate</div></td>
    <td></td>
    </tr>
    <tr>
    <td>&nbsp;</td>

    <td><div align="center"><input name="contract" type="text" id="contract" class="textarea required"></div></td>
    <td><div align="center"><input name="trade" type="text" id="trade" class="textarea required"></div></td>
    <td><div align="center"><input name="entry_date" type="text" id="entry_date" class="textarea required"></div></td>
    <td><div align="center"><input name="entry_rate" type="text" id="entry_rate" class="textarea required"></div></td>
    <td><div align="center"><input name="exit_date" type="text" id="exit_date" class="textarea required"></div></td>
    <td><div align="center"><input name="exit_rate" type="text" id="exit_rate" class="textarea required"></div></td>

    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>

    <td><div align="center">Capital</div></td>
    <td><div align="center">Lot</div></td>
    <td><div align="center">Points</div></td>
    <td><div align="center">Profits/Loss</div></td>
    <td><div align="center">Percentage</div></td>
    <td><div align="center">Trade Cost</div></td>

    <td></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><div align="center"><input name="capital" type="text" id="capital" class="textarea required"></div></td>
    <td><div align="center"><input name="lot" type="text" id="lot" class="textarea required" value="50" onFocus="this.value=''"></div></td>
    <td><div align="center"><input name="points" type="text" id="points" class="textarea required"></div></td>
    <td><div align="center"><input name="profit_loss" type="text" id="profit_loss" class="textarea required"></div></td>
    <td><div align="center"><input name="percentage" type="text" id="percentage" class="textarea required"></div></td>
    <td><div align="center"><input name="trade_cost" type="text" id="trade_cost" class="textarea required"></div></td>


    <td>&nbsp;</td>
    </tr>

    <tr><td>&nbsp;</td><td colspan="6"><div align="center"><input name="doSave" type="submit" id="doSave" value="Save"></div></td><td>&nbsp;</td></tr>

    </table></div>
    </form>

    <script type="text/javascript">
    function addEvent(els, type, func) {
    if(!(els instanceof Array)) els = [els];
    for(var i=0, n=els.length; i<n; ++i) {
    if(els.addEventListener) els.addEventListener(type, func, false);
    else els.attachEvent("on"+type, func);
    }
    }

    String.prototype.trim = function() {
    return this.replace(/^\s+/, "").replace(/\s+$/, "");
    };
    var trade = document.getElementById("trade");
    var lot = document.getElementById("lot");
    var capital = document.getElementById("capital");
    var entry_rate = document.getElementById("entry_rate");
    var exit_rate = document.getElementById("exit_rate");
    var points = document.getElementById("points");
    var profit_loss = document.getElementById("profit_loss");
    var percentage = document.getElementById("percentage");
    var trade_cost = document.getElementById("trade_cost");

    addEvent(
    [entry_rate, exit_rate],
    "keyup",
    function(e) {
    var vtrade = trade.value.trim();
    var ventry_rate = entry_rate.value.trim();
    var vexit_rate = exit_rate.value.trim()
    var vlot = lot.value.trim()
    var vcapital = capital.value.trim()
    var nentry_rate = ventry_rate*1;
    var nexit_rate = vexit_rate*1;
    if(ventry_rate=="" || vexit_rate=="" || isNaN(nentry_rate) || isNaN(nexit_rate)) {
    points.value = percentage.value = "";
    return;
    }

    if (trade.value = "SOLD") {
    (points.value = nentry_rate - nexit_rate);
    }
    if (trade.value = "BOUGHT") {
    (points.value = nexit_rate - nentry_rate);
    }
    capital.value = (nentry_rate * vlot) * 25 / 100;
    profit_loss.value = (points.value) * vlot;
    percentage.value = (profit_loss.value) * 100 / vcapital;
    trade_cost.value = (((nentry_rate * vlot) * 0.00004) + 9.9324) + (((nexit_rate * vlot) * 0.00004) + ((nexit_rate * vlot) * 0.00017) + 9.9324);
    }
    );
    </script>


    </body>
    </html>

    ============================================================
     
    immortal_king, Jan 21, 2011 IP