
var baseStats;
var maxStats;
var currentStats;
var Skills;
var Equipment;
var Weapons;
var Special;
var Advances;
var AdvanceProbabilities;
var PossibleSkills;
var PossibleEquipment;
var PossibleWeapons;
var PossibleWeaponsCC;
var HiredGunType;
var Rating;
var WeaponPicks;
var WeaponsPicksCC;
var SkillPicks;
var WyrdPrimary;
var WyrdSecondary;
var WyrdMultipleSecondary;

function clearHiredGun() {
		document.getElementById('hiredGunXml').innerHTML = '';
		document.getElementById('hiredGunTable').innerHTML = '';
}

function contains(arryA,strB){
	  for(var i=0;i<arryA.length;i++){ if(arryA[i]===strB) { return true; } }
	  return false;
}

function generateHiredGun()
{
	//In the setup functions, use ,, with no spaces to maintain a comma in the output, use a single comma and space to group things as one possible selection.
	HiredGunType = document.forms.GenerateHiredGun.HiredGunType.value;
	
	if (HiredGunType == 'Random')
	{
		var randomHG = Math.floor(Math.random() * 5) + 1;
		switch (randomHG)
		{
			case 1:
				HiredGunType = 'Underhive Scum';
				break;
			case 2:
				HiredGunType = 'Bounty Hunter';
				break;
			case 3:
				HiredGunType = 'Ratskin Scout';
				break;
			case 4:
				HiredGunType = 'Pit Slave';
				break;
			case 5:
				HiredGunType = 'Wyrd';
				break;
		}
	}
	
	if (HiredGunType == 'Wyrd')
	{
		var randomWyrd = Math.floor(Math.random() * 4) + 1;
		switch (randomWyrd)
		{
			case 1:
				HiredGunType = 'Beastmaster';
				break;
			case 2:
				HiredGunType = 'Telekinetic';
				break;
			case 3:
				HiredGunType = 'Pyromaniac';
				break;
			case 4:
				HiredGunType = 'Telepath';
				break;
		}
	}
	
	switch (HiredGunType)
	{
		case 'Random':
			var random 
			break;
		case 'Underhive Scum':
			setupScum();
			break;
		case 'Bounty Hunter':
			setupBounty();
			break;
		case 'Ratskin Scout':
			setupScout();
			break;
		case 'Pit Slave':
			setupSlave();
			break;
		case 'Beastmaster':
			setupWyrd();	
			setupBeastmaster();
			break;
		case 'Telekinetic':
			setupWyrd();	
			setupTelekinetic();
			break;
		case 'Pyromaniac':
			setupWyrd();	
			setupPyromaniac();
			break;
		case 'Telepath':
			setupWyrd();	
			setupTelepath();
			break;
	}
	currentStats = baseStats.slice();

	var test = "";

	while(Advances > 0)
	{
		var advanceNum = Math.random();
		var cumulativeProb = 0;

		for(var i=0; i<AdvanceProbabilities.length; i++)
		{
			cumulativeProb += AdvanceProbabilities[i];
			if (cumulativeProb > advanceNum)
			{
				if (i < 9)
				{
					if (currentStats[i] - maxStats[i] == 0)
					{
						i = 0;
						cumulativeProb = 0;
						advanceNum = Math.random();
					}
					else
					{
						currentStats[i]++;
						break;
					}
				}
				else
				{
					if (contains(Skills,PossibleSkills[i-9]))
					{
						i = 0;
						cumulativeProb = 0;
						advanceNum = Math.random();						
					}
					else
					{
						Skills[Skills.length] = PossibleSkills[i-9];
						break;
					}
				}

			}
		}
		Advances = Advances - 1;
	}
	
	addArrayItems(WeaponPicksCC, PossibleWeaponsCC, Weapons);
	addArrayItems(WeaponPicks, PossibleWeapons, Weapons);
	addArrayItems(EquipmentPicks, PossibleEquipment, Equipment);


	if (WyrdPrimary.length > 0)
	{
		var primaryNum = Math.floor(Math.random() * WyrdPrimary.length);
		var primarySkill = new Array(WyrdPrimary[primaryNum]);
		var secondaryNum = Math.floor(Math.random() * WyrdSecondary.length);
		var secondarySkill = new Array(WyrdSecondary[secondaryNum]);
		
		if (secondarySkill[0] == 'EPP')
		{
			secondarySkill.splice(0,1);
			if (HiredGunType == 'Beastmaster')
			{
				primarySkill[0] = primarySkill[0].replace('3','6');	
			}
			else
			{
				addArrayItems(1, WyrdPrimary, primarySkill);
			}
		}
		else if (secondarySkill[0] == 'MMP')
		{
			secondarySkill.splice(0,1);
			var secondaryPicks = Math.floor(Math.random() * 3) + 2;
			addArrayItems(secondaryPicks, WyrdMultipleSecondary, secondarySkill);
		}


	}

	Skills.sort();
	Weapons.sort();

	var htmlOutput = '<table>';
		htmlOutput += '<tr><th colspan=9>' + HiredGunType + ' - Rating ' + Rating + '</th></tr>';
		htmlOutput += '<tr><th>M</th><th>WS</th><th>BS</th><th>S</th><th>T</th><th>W</th><th>I</th><th>A</th><th>Ld</th></tr>';
		htmlOutput += '<tr>';
		for(var i=0; i<9; i++)
		{
			htmlOutput += '<td>' + currentStats[i] + '</td>';
		}
		htmlOutput += '<tr>';
		htmlOutput += '<tr><td colspan=9>Skills: ';
		htmlOutput += getCommaSeparatedString(Skills);
		htmlOutput += '</td></tr>';
		htmlOutput += '<tr><td colspan=9>Gear: ';
		htmlOutput += getCommaSeparatedString(Weapons);
		if (Equipment.length > 0 && Weapons.length > 0)
		{
			htmlOutput += ', ';
		}
		htmlOutput += getCommaSeparatedString(Equipment);
		htmlOutput += '</td></tr>';
		if (Special.length > 0)
		{
			htmlOutput += '<tr><td colspan=9>Special: ';
			htmlOutput += getCommaSeparatedString(Special);
			htmlOutput += '</td></tr>';
		}
		if (WyrdPrimary.length > 0)
		{
			htmlOutput += '<tr><td colspan=9>Primary Power: ';
			htmlOutput += getCommaSeparatedString(primarySkill);
			htmlOutput += '</td></tr>';
			if (secondarySkill.length > 0)
			{
				htmlOutput += '<tr><td colspan=9>Secondary Power: ';
				htmlOutput += getCommaSeparatedString(secondarySkill);
				htmlOutput += '</td></tr>';
			}
		}
		
		htmlOutput += '</table>';
		
	document.getElementById('hiredGunTable').innerHTML = htmlOutput.replace(/,,/g,', ');



	if (WyrdPrimary.length > 0)
	{
		for (var primary in primarySkill)
		{
			Skills.push('Primary Power: ' + primarySkill[primary]);
		}

		for (var secondary in secondarySkill)
		{
			Skills.push('Secondary Power: ' + secondarySkill[secondary]);
		}
		Skills.sort();
	}

	var xmlOutput =	'<pre>&lt;gangmember&gt;\n';
	xmlOutput += '\t&lt;name&gt;' + HiredGunType + '&lt;/name&gt;\n';
	xmlOutput += '\t&lt;type&gt;Hired Gun&lt;/type&gt;\n';
	xmlOutput += '\t&lt;stats&gt;\n';
	xmlOutput += '\t\t&lt;M&gt;' + currentStats[0] + '&lt;/M&gt;\n';
	xmlOutput += '\t\t&lt;WS&gt;' + currentStats[1] + '&lt;/WS&gt;\n';
	xmlOutput += '\t\t&lt;BS&gt;' + currentStats[2] + '&lt;/BS&gt;\n';
	xmlOutput += '\t\t&lt;S&gt;' + currentStats[3] + '&lt;/S&gt;\n';
	xmlOutput += '\t\t&lt;T&gt;' + currentStats[4] + '&lt;/T&gt;\n';
	xmlOutput += '\t\t&lt;W&gt;' + currentStats[5] + '&lt;/W&gt;\n';
	xmlOutput += '\t\t&lt;I&gt;' + currentStats[6] + '&lt;/I&gt;\n';
	xmlOutput += '\t\t&lt;A&gt;' + currentStats[7] + '&lt;/A&gt;\n';
	xmlOutput += '\t\t&lt;LD&gt;' + currentStats[8] + '&lt;/LD&gt;\n';
	xmlOutput += '\t&lt;/stats&gt;\n';
	xmlOutput += getXML(getCommaSeparatedString(Skills), 'skill');
	xmlOutput += getXML(getCommaSeparatedString(appendSpecial(Special)), 'skill');
	xmlOutput += getXML(getCommaSeparatedString(Weapons), 'weapon');
	xmlOutput += getXML(getCommaSeparatedString(Equipment), 'equipment');
	xmlOutput += '\t&lt;cost&gt;' + Rating + '&lt;/cost&gt;\n';
	xmlOutput += '\t&lt;experience&gt;0&lt;/experience&gt;\n';
	xmlOutput += '&lt;/gangmember&gt;\n</pre>';
	document.getElementById('hiredGunXml').innerHTML = xmlOutput.replace(/,,/g,', ');

}

function addArrayItems(numPicks, sourceArray, targetArray)
{
	while (numPicks > 0)
	{
		var pickNum = Math.floor(Math.random() * sourceArray.length);
		if (!contains(targetArray,sourceArray[pickNum]))
		{
			targetArray.push(sourceArray[pickNum]);
			numPicks = numPicks - 1;
		}
	}
	return;
}

function appendSpecial(arr)
{
	for(var i=0; i<arr.length; i++)
	{
		arr[i] = 'Special: ' + arr[i];
	}
	return arr;
}

function getCommaSeparatedString(arr)
{
	var outputString = '';
	if (arr.length == 0) { return outputString; }
	for(var i=0; i<arr.length; i++)
	{
		outputString += arr[i] + ', ';
	}
	outputString = outputString.slice(0,-2);
	return outputString;
}

function getXML(inputString, type)
{
	var xml='';
	if (inputString.length == 0) { return xml; }
	var split = inputString.split(', ');
	split.sort();
	for (var item in split)
	{
		xml += '\t&lt;' + type + '&gt;' + split[item] + '&lt;/' + type + '&gt;\n';
	}
	return xml;
}

function setupScum()
{
	baseStats= new Array(4,3,3,3,3,1,3,1,7);
	maxStats = new Array(4,5,5,4,4,3,5,3,9);
	AdvanceProbabilities = new Array(0, 1/36, 1/6, 1/36, 1/36, 1/36, 1/6, 1/36, 7/36, 1/6, 1/9, 1/108, 1/108, 1/108, 1/108, 1/108, 1/108);
	PossibleSkills = new Array('Gunfighter', 'Quick Draw', 'Crack Shot', 'Fast Shot', 'Hip Shooter', 'Marksman', 'Rapid Fire', 'Killer Reputation');
	PossibleWeapons = new Array('Knife, Bolt Pistol, Bolt Pistol','Knife, Plasma Pistol, Laspistol', 'Knife, Plasma Pistol, Autopistol', 'Knife, Bolt Pistol, Hand Flamer');
	PossibleWeaponsCC = new Array();
	PossibleEquipment = new Array();
	Skills = new Array();
	Equipment = new Array();
	Weapons = new Array();
	Special = new Array();
	WyrdPrimary = new Array();
	Advances = 6;
	WeaponPicks = 1;
	WeaponPicksCC = 0;
	EquipmentPicks = 0;
	Rating = 75;
}

function setupBounty()
{
	baseStats= new Array(4,4,4,3,3,2,4,1,8);
	maxStats = new Array(4,6,6,4,4,3,6,3,9);
	AdvanceProbabilities = new Array(0, 1/18, 1/18, 1/36, 1/36, 1/36, 1/18, 1/36, 1/18, 2/9, 2/9, 1/9, 1/54, 1/54, 1/54, 1/54, 1/54, 1/54);
	PossibleSkills = new Array('Crack Shot', 'Nerves Of Steel', 'Marksman', 'Dodge', 'True Grit', 'Weaponsmith', 'Quick Draw', 'Leap', 'Killer Reputation');
	PossibleWeapons = new Array('Boltgun', 'Lasgun with Hotshot pack');
	PossibleWeaponsCC = new Array();
	PossibleEquipment = new Array('Bio-Booster', 'Blindsnake Pouch', 'Weapon Reload', 'Mesh armour', 'Bionic Eye', 'Bionic Arm', 'Bionic Leg');
	Skills = new Array();
	Equipment = new Array('Filter Plugs', 'Photo-contacts');
	Weapons = new Array('Knife', 'Boltpistol with red-dot laser sight', 'Chainsword', 'Shotgun with solid,,scatter and man-stopper shells');
	Special = new Array('Capture', 'Claim Bounty');
	WyrdPrimary = new Array();
	Advances = 3;
	WeaponPicks = 1;
	WeaponPicksCC = 0;
	EquipmentPicks = 1;
	Rating = 175;
}

function setupScout()
{
	baseStats= new Array(4,3,3,3,3,1,3,1,7);
	maxStats = new Array(4,5,5,4,4,3,5,3,9);
	AdvanceProbabilities = new Array(0, 1/6, 1/36, 1/36, 1/36, 1/36, 1/6, 1/36, 1/36, 1/12, 1/12, 1/12, 1/24, 1/24, 1/24, 1/24, 1/24, 1/24);
	PossibleSkills = new Array('Dodge', 'Leap', 'Sprint', 'Step Aside', 'Nerves of Steel', 'Ambush', 'Evade', 'Infiltration', 'Sneak Up');
	PossibleWeapons = new Array('Shotgun with solid and scatter Shells', 'Autogun', 'Lasgun');
	PossibleWeaponsCC = new Array('Club','Maul','Bludgeon','Axe');
	PossibleEquipment = new Array();
	Skills = new Array();
	Equipment = new Array('Blindsnake Pouch');
	Weapons = new Array('Knife');
	Special = new Array('Guide', 'Explore', 'Resilient');
	WyrdPrimary = new Array();
	Advances = 3;
	WeaponPicks = 1;
	WeaponPicksCC = 1;
	EquipmentPicks = 0;
	Rating = 75;
}

function setupSlave()
{
	baseStats= new Array(4,3,3,3,3,1,3,1,7);
	maxStats = new Array(4,5,5,5,5,3,5,3,9);
	AdvanceProbabilities = new Array(0, 1/6, 0, 1/6, 1/6, 1/18, 1/18, 1/6, 1/18, 1/36, 1/36, 1/36, 1/36, 1/36, 1/36);
	PossibleSkills = new Array('Dodge', 'Iron Jaw', 'True Grit', 'Infiltration', 'Parry', 'Hurl Opponent');
	PossibleWeapons = new Array('Stub Gun with dum-dum bullets', 'Laspistol', 'Autopistol');
	PossibleWeaponsCC = new Array('Rock drill', 'Claw', 'Shears', 'Chainsaw', 'Buzz saw', 'Hammer');
	PossibleEquipment = new Array();
	Skills = new Array();
	Equipment = new Array();
	Weapons = new Array();
	Special = new Array();
	WyrdPrimary = new Array();
	Advances = 4;
	WeaponPicks = 1;
	WeaponPicksCC = 1;
	EquipmentPicks = 0;
	Rating = 50;
}

function setupWyrd()
{
	baseStats= new Array(4,2,2,3,4,1,4,1,7);
	maxStats = new Array(4,2,2,3,4,1,4,1,7);
	AdvanceProbabilities = new Array();
	PossibleSkills = new Array();
	PossibleWeapons = new Array('Stub Gun with dum-dum bullets', 'Laspistol', 'Autopistol');
	PossibleWeaponsCC = new Array('Sword', 'Axe', 'Chain', 'Flail', 'Club', 'Maul', 'Bludgeon');
	PossibleEquipment = new Array();
	Skills = new Array();
	Equipment = new Array();
	Weapons = new Array();
	Special = new Array();
	Advances = 0;
	WeaponPicks = 1;
	WeaponPicksCC = 1;
	EquipmentPicks = 0;
	Rating = 125;
	WyrdSecondary = new Array('None', 'None', 'None', 'None', 'None', 'None', '"You can fly..."', 'Float', 'Weapon Jinx', 'Fearful Aura', 'Freeze Time', 'Nullify Power', 'Banshee Howl', 'Jog Trigger Finger', 'Trip Up', 'Sense Presence', 'Spider Man', 'Zen Shootist', 'Walk Through Walls', 'Chameleon', 'Mirror Image', 'Throw Voice', 'Bang-Flash', 'Lucky Aura', 'Healing Hands', 'Remove Pain', 'Cause Pain', 'Stop Bleeding', 'Precognition', 'Cannot Be Possessed', 'MMP', 'MMP', 'MMP', 'MMP', 'MMP', 'EPP');
	WyrdMultipleSecondary =  new Array('"You can fly..."', 'Float', 'Weapon Jinx', 'Fearful Aura', 'Freeze Time', 'Nullify Power', 'Banshee Howl', 'Jog Trigger Finger', 'Trip Up', 'Sense Presence', 'Spider Man', 'Zen Shootist', 'Walk Through Walls', 'Chameleon', 'Mirror Image', 'Throw Voice', 'Bang-Flash', 'Lucky Aura', 'Healing Hands', 'Remove Pain', 'Cause Pain', 'Stop Bleeding', 'Precognition', 'Cannot Be Possessed');
}

function setupBeastmaster()
{
	WyrdPrimary = new Array('D3 Giant Rats', 'D3 Ripper Jacks', 'D3 Millasaurs');
}

function setupPyromaniac()
{
	WyrdPrimary = new Array('Moltern Man', 'Spontaneous Combustion', 'Fireball', 'Wall of Flame', 'Flame Blast', 'Melta');
}

function setupTelepath()
{
	WyrdPrimary = new Array('Mind Control', 'Terrify', 'Invisibility', 'Mental Assault', 'Hallucinations', 'Mental Strength');
}

function setupTelekinetic()
{
	WyrdPrimary = new Array('Assail', 'Hail Storm', 'Crush', 'Force Field', 'Displacement', 'Fists of Fury');
}