//NERO Build Calculator.
//1999 Seth Peck.  webmaster@nerocolorado.com
//If you want this for your site, you may copy and modify this directly, as long as I get credit!
//Modified June 29, 1999 by James Rapinac

//Script History:
//1.0  --  Converts XP to BP
//1.1  --  Add Goblin Blankets, Loose XP
//1.2  --  Add Event Lengths, Jobs
//1.3  --  Start with Build and Loose XP
//1.4  --  Fixed a Looping Error
//1.5  --  Added Extra Functions
//1.5.1 -  Added Loose Build, changed layout

function calcBuild(start)
{
    var c = 0;
    var r = 3;
    var j = 3;
    var b = 15;
    var exp = start;
    while (start > r) {
	      c = 0;
	      while (c < 10 && start >= r) {
	        start -= r;
		        c++;
		        b++;
	      }
	      j++;
	      r += j;
	      c = 0;
	  }
    var l = Math.floor((b -5) / 10);

 document.calculator.bd.value = b;
 document.calculator.lv.value = l;
 document.calculator.ex.value = exp;
 document.calculator.rt.value = l * (l + 5) / 2;
 document.calculator.expToBuild.value = (document.calculator.rt.value *1) - start;
 document.calculator.buildToLevel.value = (((l + 1)*10)+5) - b;
 var rate = document.calculator.rt.value;
 var expToBuild = document.calculator.expToBuild.value;
 document.calculator.lxp.value = rate - expToBuild;

 calcExpLevel((document.calculator.lv.value*1) +1);
}

function addExp() {
var exp = document.calculator.ex.value * 1;
var b = document.calculator.bd.value * 1;
newExp = (exp + b) * 1;
calcBuild(newExp);
}

function addBlanket(days) {
var newExp = 0;
var exp = document.calculator.ex.value * 1;
var b = document.calculator.bd.value * 1;

newExp = exp + b*days;
calcBuild(newExp);
}

function addHalf(days) {
var newExp = 0;
var exp = document.calculator.ex.value * 1;
var b = document.calculator.bd.value * 1;

newExp = exp + Math.ceil(b*days/2);
calcBuild(newExp);
}

function addLoose() {
var newExp = 0;
var exp = document.calculator.ex.value * 1;
var ls = document.calculator.loose.value * 1;

newExp = exp + ls;
calcBuild(newExp);
}

function calcExp() {
var newExp = 0;
var ls = document.calculator.cl.value * 1;
var b = document.calculator.cb.value * 1;

var c = 0;
var r = 3;
var j = 3;
var start = 15;

while (start < b) {
	      c = 0 ;
	      while (c < 10 && start < b) {
		        newExp += r;
		        c++;
		        start++;
	      }
	      j++;
	      r += j;
	      c = 0 ;
	  }
 var l = Math.floor((b-5) / 10);
 document.calculator.bd.value = b;
 document.calculator.lv.value = l;
 document.calculator.ex.value = newExp + ls;
 document.calculator.rt.value = l * (l + 5) / 2;
 document.calculator.expToBuild.value = (document.calculator.rt.value *1) - ls;
 document.calculator.buildToLevel.value = (((l + 1)*10)+5) - b;
 var rate = document.calculator.rt.value;
 var expToBuild = document.calculator.expToBuild.value;
 document.calculator.lxp.value = rate - expToBuild;
 calcExpLevel((document.calculator.lv.value*1) +1);
}

function calcExpLevel(Level) {
 Level *= 1;
 var exp = 0;
 var i;
 for (i=1;i<=Level;i++) {
    exp += (10*(i+2))*(Level-i);
 }
 document.calculator.expToLevel.value = exp;
}
