<!--
// Declare init variables
ds = document.Stats;
hdArray = new Array(0,12,6,8,8,10,8,10,10,6,4,4);
raceArray = new Array();
raceArray[1] = new Array(0,0,2,0,0,-2);
raceArray[2] = new Array(0,2,-2,0,0,0);
raceArray[3] = new Array(-2,0,2,0,0,0);
raceArray[4] = new Array(0,0,0,0,0,0);
raceArray[5] = new Array(2,-2,0,0,0,-2);
raceArray[6] = new Array(-2,2,0,0,0,0);
raceArray[7] = new Array(0,0,0,0,0,0);

function clearAll() {
	ds = document.Stats;
  ds.reset();
	}
function random() {
	var now = new Date();
	random.m = 714025;
	random.a = 4096;
	random.c = 150889;
	random.seed = ((now.getTime() % random.m) * random.a + random.c) % random.m;
	return Math.random(random.seed);
	}
function randNum(min,max) {
	var rand = Math.round(max * random());
	if (rand < min) rand = randNum(min,max);
	return rand;
	}
function reRoll() {
  ds = document.Stats;
  dc = document.criteria;
	n = document.rm.rmeth.length;
	for(i=0;i<n;i++) {
		if(document.rm.rmeth[i].checked) rollMethod = 1 + i;
		}
  npcLevel = dc.level.options[dc.level.selectedIndex].value;
  if(dc.cclass.selectedIndex == 0) dc.cclass.selectedIndex = randNum(1,11);
  npcHD = hdArray[dc.cclass.selectedIndex];
  npcClass = dc.cclass.options[dc.cclass.selectedIndex].value;
  if(dc.race.selectedIndex == 0) dc.race.selectedIndex = randNum(1,7);
  npcRace = dc.race.options[dc.race.selectedIndex].value;
	ds.RollButton.value = "Re-Roll";
  clearAll();
	rollFlag = 1;
	if(rollMethod == 1) rollStats(3);
	if(rollMethod == 2) rollStats(4);
	if(rollMethod == 3) rollStats(6);
	}
function getMethod() {
	n = document.rm.rmeth.length;
	for(i=0;i<n;i++) {
		if(document.rm.rmeth[i].checked) {
      rollMethod = 1 + i;
      methodFlag = 1;
      }
		}
	}
function rollStats(z) {
	statArray = new Array(6);
	diceArray = new Array(z);
	if(rollMethod == 1) {
		for(i=0;i<6;i++) {
			for(j=0;j<z;j++) diceArray[j] = randNum(1,6);
			statArray[i] = diceArray[0] + diceArray[1] + diceArray[2];
			}
		}
  if(rollMethod == 2) {
		for(i=0;i<6;i++) {
			for(j=0;j<z;j++) {
				diceArray[j] = randNum(1,6);
				}
			diceArray.sort().reverse();
			statArray[i] = diceArray[0] + diceArray[1] + diceArray[2];
			}
    }
  if(rollMethod == 3) {
		for(i=0;i<6;i++) {
			for(j=0;j<z;j++) {
				diceArray[j] = randNum(1,6);
				}
			diceArray.sort().reverse();
			statArray[i] = diceArray[0] + diceArray[1] + diceArray[2];
			}
    }
  statSorter();
  dispStats();
	}
function myCompare(a,b) { return Number(a) - Number(b); }
function statSorter() {
  statArray.sort(myCompare).reverse();
  if(dc.cclass.selectedIndex == 1) {  //Barbarian STR CON DEX WIS INT CHR
    sstr = statArray[0];
    scon = statArray[1];
    sdex = statArray[2];
    swis = statArray[3];
    sint = statArray[4];
    schr = statArray[5];
    }
  if(dc.cclass.selectedIndex == 2) {  //Bard      DEX INT CHR STR WIS CON
    sdex = statArray[0];
    sint = statArray[1];
    schr = statArray[2];
    sstr = statArray[3];
    swis = statArray[4];
    scon = statArray[5];
    }
  if(dc.cclass.selectedIndex == 3) {  //Cleric    WIS STR CHR CON DEX INT
    swis = statArray[0];
    sstr = statArray[1];
    schr = statArray[2];
    scon = statArray[3];
    sdex = statArray[4];
    sint = statArray[5];
    }
  if(dc.cclass.selectedIndex == 4) {  //Druid     WIS STR CON DEX INT CHR
    swis = statArray[0];
    sstr = statArray[1];
    scon = statArray[2];
    sdex = statArray[3];
    sint = statArray[4];
    schr = statArray[5];
    }
  if(dc.cclass.selectedIndex == 5) {  //Fighter   STR CON DEX INT WIS CHR
    sstr = statArray[0];
    scon = statArray[1];
    sdex = statArray[2];
    sint = statArray[3];
    swis = statArray[4];
    schr = statArray[5];
    }
  if(dc.cclass.selectedIndex == 6) {  //Monk      DEX STR WIS CON CHR INT
    sdex = statArray[0];
    sstr = statArray[1];
    swis = statArray[2];
    scon = statArray[3];
    schr = statArray[4];
    sint = statArray[5];
    }
  if(dc.cclass.selectedIndex == 7) {  //Paladin   STR WIS CHR CON DEX INT
    sstr = statArray[0];
    swis = statArray[1];
    schr = statArray[2];
    scon = statArray[3];
    sdex = statArray[4];
    sint = statArray[5];
    }
  if(dc.cclass.selectedIndex == 8) {  //Ranger    STR CON WIS DEX INT CHR
    sstr = statArray[0];
    scon = statArray[1];
    swis = statArray[2];
    sdex = statArray[3];
    sint = statArray[4];
    schr = statArray[5];
    }
  if(dc.cclass.selectedIndex == 9) {  //Rogue     DEX INT CHR STR CON WIS
    sdex = statArray[0];
    sint = statArray[1];
    schr = statArray[2];
    sstr = statArray[3];
    scon = statArray[4];
    swis = statArray[5];
    }
  if(dc.cclass.selectedIndex == 10) {  //Sorcerer  CHR INT DEX CON WIS STR
    schr = statArray[0];
    sint = statArray[1];
    sdex = statArray[2];
    scon = statArray[3];
    swis = statArray[4];
    sstr = statArray[5];
    }
  if(dc.cclass.selectedIndex == 11) {  //Wizard    INT DEX CON WIS STR CHR
    sint = statArray[0];
    sdex = statArray[1];
    scon = statArray[2];
    swis = statArray[3];
    sstr = statArray[4];
    schr = statArray[5];
    }
  statArray[0] = sstr;
  statArray[1] = sdex;
  statArray[2] = scon;
  statArray[3] = sint;
  statArray[4] = swis;
  statArray[5] = schr;
  }
function dispStats() {
  modArray = new Array();
  totArray = new Array();
  for(n=0;n<6;n++) {
    totArray[n] = statArray[n] + raceArray[dc.race.selectedIndex][n];
    if(totArray[n] == 1) modArray[n] = "-5";
    if(totArray[n] > 1 && totArray[n] < 4) modArray[n] = -4;
    if(totArray[n] > 3 && totArray[n] < 6) modArray[n] = -3;
    if(totArray[n] > 5 && totArray[n] < 8) modArray[n] = -2;
    if(totArray[n] > 7 && totArray[n] < 10) modArray[n] = -1;
    if(totArray[n] > 9 && totArray[n] < 12) modArray[n] = 0;
    if(totArray[n] > 11 && totArray[n] < 14) modArray[n] = +1;
    if(totArray[n] > 13 && totArray[n] < 16) modArray[n] = +2;
    if(totArray[n] > 15 && totArray[n] < 18) modArray[n] = +3;
    if(totArray[n] > 17 && totArray[n] < 20) modArray[n] = +4;
    if(totArray[n] > 19 && totArray[n] < 22) modArray[n] = +5;
    }
  ds.rout.value = npcRace;
  ds.cout.value = npcClass;
  ds.hitpoints.value = (1*0);
	ds.Str.value = statArray[0];
  ds.rmod0.value = raceArray[dc.race.selectedIndex][0];
  ds.tot0.value = totArray[0];
  ds.mod0.value = modArray[0];
	ds.Dex.value = statArray[1];
  ds.rmod1.value = raceArray[dc.race.selectedIndex][1];
  ds.tot1.value = totArray[1];
  ds.mod1.value = modArray[1];
	ds.Con.value = statArray[2];
  ds.rmod2.value = raceArray[dc.race.selectedIndex][2];
  ds.tot2.value = totArray[2];
  ds.mod2.value = modArray[2];
	ds.Int.value = statArray[3];
  ds.rmod3.value = raceArray[dc.race.selectedIndex][3];
  ds.tot3.value = totArray[3];
  ds.mod3.value = modArray[3];
	ds.Wis.value = statArray[4];
  ds.rmod4.value = raceArray[dc.race.selectedIndex][4];
  ds.tot4.value = totArray[4];
  ds.mod4.value = modArray[4];
	ds.Chr.value = statArray[5];
  ds.rmod5.value = raceArray[dc.race.selectedIndex][5];
  ds.tot5.value = totArray[5];
  ds.mod5.value = modArray[5];
  if(dc.hpgen.selectedIndex == 0) {
    ds.hitpoints.value = npcHD + modArray[2];
    for(n=0;n<(dc.level.selectedIndex);n++) {
      hpRoll = randNum(1,(npcHD));
      //alert("HD: " + npcHD + ", Roll: " + hpRoll + ", + stat bonus:" + modArray[2]);
      ds.hitpoints.value = (ds.hitpoints.value*1) + hpRoll + modArray[2];
      }
    }
  else {
    ds.hitpoints.value = npcHD + modArray[2];
    for(n=0;n<(dc.level.selectedIndex);n++) {
      hpRoll = randNum(1,(npcHD/2));
      //alert("HD: " + npcHD + ", Roll: " + hpRoll + ", + half-max: " + (npcHD/2) + ", + stat bonus:" + modArray[2]);
      ds.hitpoints.value = (ds.hitpoints.value*1) + hpRoll + (npcHD/2) + modArray[2];
      }
    }
	}
// -->

