/* character generator 1.0
   
  ( License copied from the zlib license. )

  Copyright (C) Thomas Kerwin

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Thomas Kerwin  demiurge@polyatomic.org
*/


var statArray = new Array(6);
var adjArray = new Array(6);
var totArray = new Array(6);

var humanArray = new Array(6);
var elfArray = new Array(6);
var halfelfArray = new Array(6);
var dwarfArray = new Array(6);
var gnomeArray = new Array(6);
var halforcArray = new Array(6);
var halflingArray = new Array(6);

function stats(raw, adj, tot) {
		this.raw = raw;
		this.adj = adj;
		this.tot = tot;
}

function init() {

		statArray[0] = new stats(document.getElementById('impStr'), 
														 document.getElementById('impAdjStr'), 
														 document.getElementById('impTotStr'));
		statArray[1] = new stats(document.getElementById("impDex"), 
														 document.getElementById("impAdjDex"), 
														 document.getElementById("impTotDex"));
		statArray[2] = new stats(document.getElementById("impCon"), 
														 document.getElementById("impAdjCon"), 
														 document.getElementById("impTotCon"));
		statArray[3] = new stats(document.getElementById("impInt"), 
														 document.getElementById("impAdjInt"), 
														 document.getElementById("impTotInt"));
		statArray[4] = new stats(document.getElementById("impWis"), 
														 document.getElementById("impAdjWis"), 
														 document.getElementById("impTotWis"));
		statArray[5] = new stats(document.getElementById("impCha"), 
														 document.getElementById("impAdjCha"), 
														 document.getElementById("impTotCha"));

		for( i = 0; i < humanArray.length; i++) {
				humanArray[i] = 0;
				elfArray[i] = 0;
				halfelfArray[i] = 0;
				dwarfArray[i] = 0;
				halforcArray[i] = 0;
				gnomeArray[i] = 0;
				halflingArray[i] = 0;
		}

		elfArray[1] = 2;
		elfArray[2] = -2;
		dwarfArray[2] = 2;
		dwarfArray[5] = -2;
		halforcArray[0] = 2;
		halforcArray[3] = -2;
		halforcArray[5] = -2;
		gnomeArray[0] = -2;
		gnomeArray[2] = 2;
		halflingArray[0] = -2;
		halflingArray[1] = 2;

}

function lookup_value(x) {
		if((x <= 14) && (x >= 8)) {
				return x - 8;
		} else {
				switch (x) {
					case 15:
						return 8;
					case 16:
						return 10;
					case 17:
						return 13;
					case 18:
						return 16;
				}
		}
}

function total_it() {
// calculate total points used
		var tot = 0;
		for (i = 0; i < statArray.length; i++) {
				tot += lookup_value(Number(statArray[i].raw.value));
		}
		document.getElementById("impTotal").value = tot;

// calculate final stats after adjustments
		for (i = 0; i < statArray.length; i++) {
				var t = 0;
				t += Number(statArray[i].raw.value);
				t += Number(statArray[i].adj.value);
				statArray[i].tot.value = t;
		}
}

function plus(id) {
		if(statArray[id].raw.value < 18 ) {
				statArray[id].raw.value = Number(statArray[id].raw.value) + 1;
		}
		total_it();
}

function minus(id) {
		if(statArray[id].raw.value > 8) {
				statArray[id].raw.value = Number(statArray[id].raw.value) - 1;
		}
		total_it();
}

function race_change() {
		var statset = humanArray;

		switch (document.getElementById("choRace").value) {
			case "human":
				break;
			case "elf":
				statset = elfArray;
				break;
			case "dwarf":
				statset = dwarfArray;
				break;
			case "gnome":
				statset = gnomeArray;
				break;
			case "half-elf":
				statset = halfelfArray;
				break;
			case "halfling":
				statset = halflingArray;
				break;
			case "half-orc":
				statset = halforcArray;
				break;
		}
		
		for (i = 0; i < statset.length; i++) {
				statArray[i].adj.value = statset[i];
		}
		
		total_it();
}
