WT Calculate Daru Cost

by gdarussian

HTML

<p>
    <h1>Calculate Daru Cost</h1>
    <hr />
</p>

<p>
    <label>Troop type:</label>
    <select id="troop" size="1" onChange="calcdaru()">
        <option value="2">Lancer</option>
        <option value="2">Hunter</option>
        <option value="5">Priest</option>
        <option value="5">Paladin</option>
        <option value="6">Gryphon</option>
        <option value="15" selected="selected">Knight</option>
        <option value="15">Angel</option>
    </select>
</p>

<p>
    <label>From Level:</label>
    <input id="Lv1" size=1 value ="" maxlength=4 onChange="calcdaru()" />
</p>

<p>
    <label>To Level:</label>
    <input id="Lv2" size=1 value ="" maxlength=4 onChange="calcdaru()" />
    <hr>
</p>

<p>
    Cost = <div id="answer"></div>
</p>

CSS

body { 
    margin: 1em; 
    font-size: 85%;
}

h1 {
    font-weight: bold;
    font-size: big;
    margin: 0 1em 1em 0;
}

* {
    font-family:arial, helvetica, sans-serif;
    margin:0;
    padding:0;
}

p {
    font-size:1em;
    margin: 0 0 .8em 0;
}

.result {
    font-weight: bold;
    font-size: 100%;
}

.result0 { color: black; }
.result1 { color: black; }
.result2 { color: green; }
.result3 { color: deepskyblue; }
.result4 { color: lime; }
.result5 { color: blue; }
.result6 { color: red; }
.result7 { color: purple; }
.result8 { color: orange; }
.result9 { color: fuchsia; }

table {
    margin: 1em 0 0 0
}

html>body {
    font-size: 12px;
}

label 
{
    display: inline-block; 
    width: 70px; 
}

select {
    width: 105px;
}
input {
    width: 100px;
}

JavaScript

// *** Calculate Daru Cost *** //
// By Myrkr

function calcdaru() {
    var trtype = parseInt(document.getElementById('troop').value);
    var lv1 = parseInt(document.getElementById('Lv1').value);
    var lv2 = parseInt(document.getElementById('Lv2').value);
    if (isNaN(lv1) || isNaN(lv2)) answer = '';
    else if (lv1 < 1 || lv2 < 2 || lv1 >= lv2) answer = 'Invalid level range';
    else {
        var d1 = trtype * (lv1 * lv1 * (lv1 + 1) * (lv1 + 1)) / 4;
        var d2 = trtype * (lv2 * lv2 * (lv2 + 1) * (lv2 + 1)) / 4;
        answer = '' + (d2 - d1);
        for (var i = 4; i < 41; i += 4) {
            if (answer.length >= i) {
                answer = (answer.substring(0, answer.length + 1 - i) + ',' + answer.substring(answer.length + 1 - i));
            }
        }
        answer += ' Daru';
    }
    document.getElementById('answer').innerHTML = answer;
}