// globals
NumOfSkills=100;
NumOfItems=100;
NumOfMonsters=100;
NumOfDudes=20; // the number of dudes in this expansion, also determines the number of unique phrases (1 per dude)
NumOfFirstDude=1; // the unique ID of the first dude in this set
NumOfPhrases=21;  // number of generic phrases
NumOfTypes=3;

hohumphrase="Ho hum...nothing to see.";

uniquephrases=new Array(NumOfDudes);
uniquephrases[0]="I come in peace...or do I?";
uniquephrases[1]="Take me to your leader.";
uniquephrases[2]="Where's that darn ark?";
uniquephrases[3]="Excuse please, but...duh...what was I saying?";
uniquephrases[4]="Arrrrgh! Shiver me timbers.";
uniquephrases[5]="Hey, look at me!";
uniquephrases[6]="I must eat; must sleep; eat; sleep that's all I do.";
uniquephrases[7]="Please don't tell that I've been talking.";
uniquephrases[8]="Just wait until I get my fire breath.";
uniquephrases[9]="Why do I always have to stay behind to guard the treasure?";
uniquephrases[10]="So long, thanks for playing.";
uniquephrases[11]="Hey there, hi there, don't waste your wishes!";
uniquephrases[12]="I'm gonna poke 'em and smack 'em and beat 'em up.";
uniquephrases[13]="Chop chop.";
uniquephrases[14]="See how much nicer they are when they can't talk.";
uniquephrases[15]="Must pound chest; must eat; sleep; pound chest again.";
uniquephrases[16]="So, what happens when I hit a wrong note?";
uniquephrases[17]="I'm the invisible ninja!  What?  You can see me?";
uniquephrases[18]="Throw 'em in the brig!  All of them.";
uniquephrases[19]="What was I supposed to be doing?  Scrubbing or walking the plank?";

phrases=new Array(NumOfPhrases);
phrases[0]="I'm surprised at the time.";
phrases[1]="Where is this place?";
phrases[2]="Who's your favorite Dungeon Dude?  You know it's me...";
phrases[3]="Go on...do something interesting.";
phrases[4]="I'm not afraid of anything.";
phrases[5]="I wish I could have more than 7 skills.";
phrases[6]="I wish I could have more than 7 items.";
phrases[7]="Where to now?";
phrases[8]="Hmmm....I've got an idea...";
phrases[9]="...";
phrases[10]="Run!";
phrases[11]="What's up?";
phrases[12]="What's that smell?";
phrases[13]="Who's in charge here?";
phrases[14]="Chill out.";
phrases[15]="School schmool.";
phrases[16]="Work schmork.";
phrases[17]="Who said that?";
phrases[18]="I'm a Dungeon Dud...I mean Dude.";
phrases[19]="Dude...";
phrases[20]="Whoa...";

monsters=new Array(NumOfMonsters);
monsters[0]="Giant Rat";
monsters[1]="Giant Bat";
monsters[2]="Giant Centipede";
monsters[3]="Kobold";
monsters[4]="Orc";
monsters[5]="Goblin";
monsters[6]="Evil Gnome";
monsters[7]="Bandit";
monsters[8]="Skeleton";
monsters[9]="Zombie";
monsters[10]="Poison Fungus";
monsters[11]="Dragon";
monsters[12]="Giant Snake";
monsters[13]="Giant Lizard";
monsters[14]="Shadow Monster";
monsters[15]="Fiend";
monsters[16]="Demon Fiend";
monsters[17]="Imp";
monsters[18]="Hades Hound";
monsters[19]="Hades Beast";
monsters[20]="Evil Dwarf";
monsters[21]="Evil Elf";
monsters[22]="Golem";
monsters[23]="Fire Dragon";
monsters[24]="Water Dragon";
monsters[25]="Earth Dragon";
monsters[26]="Stone Dragon";
monsters[27]="Lightning Dragon";
monsters[28]="Wind Dragon";
monsters[29]="Ogre";
monsters[30]="Troll";
monsters[31]="Skeleton Knight";
monsters[32]="Skeleton Wizard";
monsters[33]="Evil Wizard";
monsters[34]="Evil Sorcerer";
monsters[35]="Dark Fighter";
monsters[36]="Evil Thief";
monsters[37]="Ghost";
monsters[38]="Poltergeist";
monsters[39]="Giant Worm";
monsters[40]="Stone Giant";
monsters[41]="Mud Giant";
monsters[42]="Musk Monster";
monsters[43]="Gargoyle";
monsters[44]="Ghoul";
monsters[45]="Gnoll";
monsters[46]="Evil Druid";
monsters[47]="Dark Ranger";
monsters[48]="Wild Dog";
monsters[49]="Hydra";
monsters[50]="Cyclops";
monsters[51]="Lizard Man";
monsters[52]="Wolf Man";
monsters[53]="Dragon Man";
monsters[54]="Skulker";
monsters[55]="Musk Man";
monsters[56]="Medusa";
monsters[57]="Rock Snake";
monsters[58]="Minotaur";
monsters[59]="Mummy";
monsters[60]="Fire Creature";
monsters[61]="Smoke Beast";
monsters[62]="Magma Monster";
monsters[63]="Giant Spider";
monsters[64]="Rock Spider";
monsters[65]="Evil Dungeon Dude";
monsters[66]="Giant Salamander";
monsters[67]="Giant Frog";
monsters[68]="Evil Sprite";
monsters[69]="Evil Leprechaun";
monsters[70]="Evil Pixie";
monsters[71]="Giant Wasp";
monsters[72]="Giant Hornet";
monsters[73]="Troglodyte";
monsters[74]="Bug-Eyed Fiend";
monsters[75]="Vampire";
monsters[76]="Wraith";
monsters[77]="Wyvern";
monsters[78]="Vampire Wizard";
monsters[79]="Vampire Warrior";
monsters[80]="Evil Warrior";
monsters[81]="Dark Knight";
monsters[82]="Evil Adventurer";
monsters[83]="Evil Dwarf Wizard";
monsters[84]="Rock Wasp";
monsters[85]="Ghost Knight";
monsters[86]="Ghost Warrior";
monsters[87]="Phantom";
monsters[88]="Yellow Phantom";
monsters[89]="Purple Phantom";
monsters[90]="Green Phantom";
monsters[91]="Screamer";
monsters[92]="Screeching Fiend";
monsters[93]="Claw Beast";
monsters[94]="Sword Beast";
monsters[95]="Mummy Warrior";
monsters[96]="Mummy Prince";
monsters[97]="Zombie Lord";
monsters[98]="Blue Wraith";
monsters[99]="Giant Tarantula";

skills=new Array(NumOfSkills);
skills[0]="Intelligence";
skills[1]="Wisdom";
skills[2]="Dexterity";
skills[3]="Constitution";
skills[4]="Charisma";
skills[5]="Strength";
skills[6]="Martial Arts";
skills[7]="Swordplay";
skills[8]="Pick Pockets";
skills[9]="Move Silently";
skills[10]="Hide in Shadows";
skills[11]="Acrobatics";
skills[12]="Tracking";
skills[13]="Grapple";
skills[14]="High Kick";
skills[15]="Body Throw";
skills[16]="Knee Lock";
skills[17]="Strangle Hold";
skills[18]="Climb";
skills[19]="Jump";
skills[20]="Intelligence";
skills[21]="Wisdom";
skills[22]="Dexterity";
skills[23]="Constitution";
skills[24]="Charisma";
skills[25]="Strength";
skills[26]="Martial Arts";
skills[27]="Swordplay";
skills[28]="Pick Pockets";
skills[29]="Move Silently";
skills[30]="Hide in Shadows";
skills[31]="Acrobatics";
skills[32]="Tracking";
skills[33]="Grapple";
skills[34]="High Kick";
skills[35]="Body Throw";
skills[36]="Knee Lock";
skills[37]="Strangle Hold";
skills[38]="Climb";
skills[39]="Jump";
skills[40]="Intelligence";
skills[41]="Wisdom";
skills[42]="Dexterity";
skills[43]="Constitution";
skills[44]="Charisma";
skills[45]="Strength";
skills[46]="Martial Arts";
skills[47]="Swordplay";
skills[48]="Pick Pockets";
skills[49]="Move Silently";
skills[50]="Hide in Shadows";
skills[51]="Acrobatics";
skills[52]="Tracking";
skills[53]="Grapple";
skills[54]="High Kick";
skills[55]="Body Throw";
skills[56]="Knee Lock";
skills[57]="Strangle Hold";
skills[58]="Climb";
skills[59]="Jump";
skills[60]="Intelligence";
skills[61]="Wisdom";
skills[62]="Dexterity";
skills[63]="Constitution";
skills[64]="Charisma";
skills[65]="Strength";
skills[66]="Martial Arts";
skills[67]="Swordplay";
skills[68]="Pick Pockets";
skills[69]="Move Silently";
skills[70]="Hide in Shadows";
skills[71]="Acrobatics";
skills[72]="Tracking";
skills[73]="Grapple";
skills[74]="High Kick";
skills[75]="Body Throw";
skills[76]="Knee Lock";
skills[77]="Strangle Hold";
skills[78]="Climb";
skills[79]="Jump";
skills[80]="Intelligence";
skills[81]="Wisdom";
skills[82]="Dexterity";
skills[83]="Constitution";
skills[84]="Charisma";
skills[85]="Strength";
skills[86]="Martial Arts";
skills[87]="Swordplay";
skills[88]="Pick Pockets";
skills[89]="Move Silently";
skills[90]="Hide in Shadows";
skills[91]="Acrobatics";
skills[92]="Tracking";
skills[93]="Grapple";
skills[94]="High Kick";
skills[95]="Body Throw";
skills[96]="Knee Lock";
skills[97]="Strangle Hold";
skills[98]="Climb";
skills[99]="Jump";




// items
items=new Array(NumOfItems);
items[0]="Flask of Holy Water";
items[1]="Flask of Acid";
items[2]="Katana";
items[3]="Blow Gun";
items[4]="Crossbow";
items[5]="Long Bow";
items[6]="Whip";
items[7]="Large Sword";
items[8]="Small Sword";
items[9]="Battle Axe";
items[10]="Pole Arm";
items[11]="Dagger";
items[12]="Scimitar";
items[13]="Halberd";
items[14]="Spear";
items[15]="Staff";
items[16]="Club";
items[17]="Flail";
items[18]="Javelin";
items[19]="Net";
items[20]="Small Shield";
items[21]="Large Shield";
items[22]="Plate Armor";
items[23]="Leather Armor";
items[24]="Gauntlets";
items[25]="Chain Armor";
items[26]="Helmet";
items[27]="Breast Plate";
items[28]="Legpiece";
items[29]="Kneepiece";
items[30]="Shinpiece";
items[31]="Armpiece";
items[32]="Elbowpiece";
items[33]="Shoulderpiece";
items[34]="Splint Armor";
items[35]="Robes";
items[36]="Plate Boots";
items[37]="Back Plate";
items[38]="Face Guard";
items[39]="Cuisse (Thighpiece)";
items[40]="Grappling Hook";
items[41]="Bottle";
items[42]="Candle";
items[43]="Mirror";
items[44]="Pouch";
items[45]="Shovel";
items[46]="Tent";
items[47]="Rope";
items[48]="Torch";
items[49]="Lantern";
items[50]="Stick";
items[51]="Lock Pick";
items[52]="Rations";
items[53]="Chain";
items[54]="Flint Stone";
items[55]="Parchment";
items[56]="Chalk";
items[57]="Blanket";
items[58]="Monocular";
items[59]="Flask of Holy Water";
items[60]="Flask of Acid";
items[61]="Katana";
items[62]="Blow Gun";
items[63]="Crossbow";
items[64]="Long Bow";
items[65]="Whip";
items[66]="Large Sword";
items[67]="Small Sword";
items[68]="Battle Axe";
items[69]="Pole Arm";
items[70]="Dagger";
items[71]="Scimitar";
items[72]="Halberd";
items[73]="Spear";
items[74]="Staff";
items[75]="Club";
items[76]="Flail";
items[77]="Javelin";
items[78]="Net";
items[79]="Small Shield";
items[80]="Large Shield";
items[81]="Plate Armor";
items[82]="Leather Armor";
items[83]="Gauntlets";
items[84]="Chain Armor";
items[85]="Helmet";
items[86]="Breast Plate";
items[87]="Legpiece";
items[88]="Kneepiece";
items[89]="Shinpiece";
items[90]="Armpiece";
items[91]="Elbowpiece";
items[92]="Shoulderpiece";
items[93]="Splint Armor";
items[94]="Robes";
items[95]="Plate Boots";
items[96]="Back Plate";
items[97]="Face Guard";
items[98]="Cuisse (Thighpiece)";
items[99]="Grappling Hook";

name="";
level=1;
exp=0;
att=14;
def=14;
hp=10;
hp_max=10;
gp=0;
busy=false;
game_state="normal";  // options: normal, train, practice, rest, explore, eat
dd_items=new Array(7);
dd_skills=new Array(7);
for (i=0; i < 7; i++) {
	dd_items[i]="-1,-1";  // "type (deprecated), index"
	dd_skills[i]="-1";    // "index"
}

function load_dd() {

}

function unload_dd() {

}

// item_point is a string in the form of "item_type, item_index into the array"
function item_to_string(item_pointer) {
	var parts=item_pointer.split(",");
	var item_type=eval(parts[0]);
	var item_index=eval(parts[1]);
	var temp_index=item_index;
	var item_string="";  // the string name of the given item

	item_index=temp_index%100;
	if (Math.floor(temp_index/100) > 0) item_string="+"+Math.floor(temp_index/100)+" ";

	if (item_index > NumOfItems) item_index=0;

	if (item_type == 1) 
		item_string=items[item_index];
	else if (item_type == 2)
		item_string=items[item_index];
	else if (item_type == 3)
		item_string=items[item_index];
	
	if (item_string == "-1,-1") item_string="";
	return item_string;	
}

// adds an item to the inventory and displays it and updates the status
function item_(item_type_gained, item_gained) {
	var found=false;  // look for a place to put the item
	var i=0;

	do {
		if ( (dd_items[i]=="-1,-1") || (i==7) ) found=true;
		else i++;
	} while (!found);

	dd_items[i]=item_type_gained + "," + item_gained;

	document.forms[0].field2.value=item_to_string(dd_items[0]);
	document.forms[0].field5.value=item_to_string(dd_items[1]);
	document.forms[0].field8.value=item_to_string(dd_items[2]);
	document.forms[0].field11.value=item_to_string(dd_items[3]);
	document.forms[0].field14.value=item_to_string(dd_items[4]);
	document.forms[0].field17.value=item_to_string(dd_items[5]);
	document.forms[0].field20.value=item_to_string(dd_items[6]);
	
	document.forms[0].field22.value=item_to_string(dd_items[0]);
	document.forms[0].field24.value=item_to_string(dd_items[1]);
	document.forms[0].field26.value=item_to_string(dd_items[2]);
	document.forms[0].field28.value=item_to_string(dd_items[3]);
	document.forms[0].field30.value=item_to_string(dd_items[4]);
	document.forms[0].field32.value=item_to_string(dd_items[5]);
	document.forms[0].field34.value=item_to_string(dd_items[6]);

} // end item_

function skill_(skill_gained) {
	var found=false;  // look for a place to put the item
	var i=0;

	do {
		if ( (dd_skills[i]=="-1") || (i==7) ) found=true;
		else i++;
	} while (!found);

	dd_skills[i]=skill_gained;

	if (dd_skills[0]!="-1") document.forms[0].field3.value=skills[dd_skills[0]];
	if (dd_skills[1]!="-1") document.forms[0].field6.value=skills[dd_skills[1]];
	if (dd_skills[2]!="-1") document.forms[0].field9.value=skills[dd_skills[2]];
	if (dd_skills[3]!="-1") document.forms[0].field12.value=skills[dd_skills[3]];
	if (dd_skills[4]!="-1") document.forms[0].field15.value=skills[dd_skills[4]];
	if (dd_skills[5]!="-1") document.forms[0].field18.value=skills[dd_skills[5]];
	if (dd_skills[6]!="-1") document.forms[0].field21.value=skills[dd_skills[6]];

	if (dd_skills[0]!="-1") document.forms[0].field23.value=skills[dd_skills[0]];
	if (dd_skills[1]!="-1") document.forms[0].field25.value=skills[dd_skills[1]];
	if (dd_skills[2]!="-1") document.forms[0].field27.value=skills[dd_skills[2]];
	if (dd_skills[3]!="-1") document.forms[0].field29.value=skills[dd_skills[3]];
	if (dd_skills[4]!="-1") document.forms[0].field31.value=skills[dd_skills[4]];
	if (dd_skills[5]!="-1") document.forms[0].field33.value=skills[dd_skills[5]];
	if (dd_skills[6]!="-1") document.forms[0].field35.value=skills[dd_skills[6]];

} // end skill_

function gp_(x) {
	gp=eval(eval(gp)+eval(x));
	document.forms[0].field19.value=gp;
}

function exp_(x) {
	var new_level=0;
	var message="";

	exp=eval(eval(exp)+eval(x));
	document.forms[0].field7.value=exp;

	if (exp > 1000) {
		new_level=Math.floor(exp/1000) + 3;
	} else if (exp >= 500) new_level=3;
	else if (exp >= 250) new_level=2;

	if (new_level > level) {
		level=new_level;
		hp_max+=Math.floor(Math.random()*10+1);
		att++;
		def++;
		document.forms[0].field4.value = level;
		document.forms[0].field10.value=att + " / +" + Math.floor(att/2);
		document.forms[0].field13.value=def + " / +" + Math.floor(def/2);

		message="You gained a level: Level= "+level+",  Max HP= " + hp_max+",  Attack= "+att+",  Defense= "+def;
		alert(message);
	}
}

function hp_(x) {
	hp=eval(eval(hp)+eval(x));
	document.forms[0].field16.value=hp;
}

function explore(count) {
	var action=Math.floor(Math.random()*100+1);
	var function_name="";
	var gp_gained=Math.floor(Math.random()*level*10 + 1);
	var item_type_gained=0;
	var item_gained=0;
	var ran_monster=Math.floor(Math.random()*NumOfMonsters);
	var found=true;
	var hp_lost=0;
	var new_state=0;
	var ran_phrase=0;
	var phrase="";
	var dd_number = eval(document.forms[0].dd_number.value);
	var exp_gained=Math.floor(Math.random()*70+2);

	if (count == 0) {
		if (!busy) explore(1);
	} else {
		// don't allow anyone to explore if they are supposed to be doing something else
		if (game_state!="normal") {
			document.forms[0].status.value="Sorry, you need to "+game_state+".";
			return;
		}
		if (game_state=="explore") game_state="normal";

		// they really should name the poor dude
		if (document.forms[0].field1.value=="") {
			document.forms[0].status.value="You should name me before we go wandering off into the unknown.";
			return;
		}

		busy=true;
		count++;

		// do nothing for a wait of 10 * 200ms
		if (count < 10) {
			document.forms[0].status.value="Exploring.";
			for (i=0; i < count; i++)
				document.forms[0].status.value+=".";
			
			function_name="explore("+count+");"
			window.setTimeout(function_name,200);

		// once the waiting is through, proceed to determine what happens
		} else {
			busy=false;
			count=0;
		
			if (action < 18) { // you find gold
				gp_(gp_gained);
				document.forms[0].status.value="You find " + gp_gained + " gold piece(s).";

			} else if (action < 37) {  // dungeon dude speaks
				// 38% of the time, the Dude says his unique phrase, the rest of the time it's a generic random phrase
				if (Math.floor(Math.random()*100) < 39) {  
					document.forms[0].status.value=document.forms[0].field1.value+" says, " + uniquephrases[dd_number-NumOfFirstDude];;
						
				} else {

				  ran_phrase=Math.floor(Math.random()*(16+NumOfPhrases));
				  document.forms[0].status.value=document.forms[0].field1.value+" says, ";

				  // either show a custom phrase or one of the 16 standard phrases
				  if (ran_phrase >= 16) {
					phrase=phrases[ran_phrase-16];
				  } else {
					
				  switch(ran_phrase) {
					case 0: 
						phrase="A "+items[Math.floor(Math.random()*NumOfItems)]+" is a cool thing to have.";
						break;
					case 1: 
						phrase="A "+items[Math.floor(Math.random()*NumOfItems)]+" is a worthless thing to have.";
						break;
					case 2: 
						phrase=skills[Math.floor(Math.random()*NumOfSkills)]+" is a cool skill to have.";
						break;
					case 3: 
						phrase=skills[Math.floor(Math.random()*NumOfSkills)]+" is a worthless skill to have.";
						break;
					case 4: 
						phrase="'"+document.forms[0].field1.value+"'"+" is a sweet name to have.";
						break;
					case 5: 
						phrase="'"+document.forms[0].field1.value+"'"+" is an awful name to have.";
						break;
					case 6: 
						phrase="Will I ever make it to level "+eval(level+Math.floor(Math.random()*10))+".";
						break;
					case 7: 
						phrase="Who's your favorite Dungeon Dude?  You know it's me...";
						break;
					case 8: 
						phrase="I once defeated "+Math.floor(Math.random()*20 + 1)+" "+monsters[Math.floor(Math.random()*NumOfMonsters)]+"(s).";
						break;
					case 9: 
						phrase="I can't believe I only have "+exp+" experience points";
						break;
					case 10: 
						phrase="I can't believe I've only reached level  "+level+".";
						break;
					case 11: 
						phrase="I can't believe I only have "+hp+" hit points.";
						break;
					case 12:
						phrase="A(n) "+items[Math.floor(Math.random()*NumOfItems)]+" is a cool item to have.";
						break;
					case 13:
						phrase="A(n) "+items[Math.floor(Math.random()*NumOfItems)]+" is a worthless item to have.";
						break;
					case 14:
						phrase="A "+items[Math.floor(Math.random()*NumOfItems)]+" is a cool item to have.";
						break;
					case 15:
						phrase="A "+items[Math.floor(Math.random()*NumOfItems)]+" is a worthless item to have.";
						break;
				  } // end of switch ran_phrase
                          } // end of else not > 16
				} // end of else not < 39
				
				document.forms[0].status.value+=phrase;
				return;
			
			} else if (action < 41) {  //change state
				new_state=Math.floor(Math.random()*10);
				if (new_state<3) game_state="eat";
				else if (new_state==3) game_state="rest";
				else if (new_state<6) game_state="practice";
				else if (new_state<=9) game_state="train";

				document.forms[0].status.value="You're tired of exploring.  You really need to "+game_state+" instead.";			

			} else if (action < 44) {   
				if (dd_items[6]=="-1,-1") {	 // you find an item	
					document.forms[0].status.value="You found an item!";	

					do {
						found=true;
						item_type_gained=Math.floor(Math.random()*NumOfTypes+1);
						item_gained=Math.floor(Math.random()*NumOfItems);
						for (i=0; i < 7; i++) {
							if (dd_items[i] == (item_type_gained + "," + item_gained) ) found = false;
						}
					} while (!found);

					item_(item_type_gained, item_gained);
				} else { // inventory is full
					document.forms[0].status.value=hohumphrase;
				}

			} else if (action < 63) { //you defeated a monster
					exp_gained=Math.floor(Math.random()*70+2);
					exp_(exp_gained);
					gp_(gp_gained);
					document.forms[0].status.value="You defeated a(n) "+monsters[ran_monster]+" +"+exp_gained+" exp and "+gp_gained+" gp.";
			
			} else if (action < 73) { // you were injured by a monster
					hp_lost=Math.floor(Math.random()*3*level+1);
					hp_(eval(0-hp_lost));
					if (hp <=0) {
						gp_lost=Math.floor(Math.random()*100*level+1);
						if (gp_lost > gp) gp_lost=gp;
						gp_lost=0-gp_lost;
						gp_(gp_lost);
						document.forms[0].status.value="You were injured by a(n) "+monsters[ran_monster]+".  You have fainted.  You lost "+eval(0-gp_lost)+" gp!";
						game_state="rest";
					} else document.forms[0].status.value="You were injured by a(n) "+monsters[ran_monster]+" and lost "+hp_lost+" hit point(s).";
			
			} else {
				document.forms[0].status.value=hohumphrase;
			}

			exp_(1);

		} // end of else count is >= 10

	}  //end of else not count==0

}

function train(count) {
	var action=Math.floor(Math.random()*100+1);
	var function_name="";
	var skill_gained=0;
	var found=true;

	if (count == 0) {
		if (!busy) train(1);
	} else {
		if ( (game_state!="normal") && (game_state!="train") ) {
			document.forms[0].status.value="Sorry, you need to "+game_state+".";
			return;
		}

		if (game_state=="train") game_state="normal";
		busy=true;
		count++;

		if (count < 10) {
			document.forms[0].status.value="Training.";
			for (i=0; i < count; i++)
				document.forms[0].status.value+=".";
			
			function_name="train("+count+");"
			window.setTimeout(function_name,200);
		} else {
			busy=false;
			count=0;
		
			if (action < 8) {
				if (dd_skills[6]=="-1") {	 // you learned a new skill	
					document.forms[0].status.value="You learned a new skill!";	

					do {
						found=true;
						skill_gained=Math.floor(Math.random()*NumOfSkills);
						for (i=0; i < 7; i++) {
							if (dd_skills[i ]== skill_gained) found=false;
						}
					} while (!found);

					skill_(skill_gained);
				} else { // skill list is full
					document.forms[0].status.value="Training completed; you gained an experience point.";
				}
				
			} else {
				document.forms[0].status.value="Training completed; you gained an experience point.";
			}

			exp_(1);

		} // end of else count is >= 10

	}  //end of else not count==0

}

function practice(count) {
	var action=Math.floor(Math.random()*100+1);
	var function_name="";
	var exp_gained=Math.floor(Math.random() * 20 + 2);

	if (count == 0) {
		if (!busy) practice(1);
	} else {
		if ( (game_state!="normal") && (game_state!="practice") ) {
			document.forms[0].status.value="Sorry, you need to "+game_state+".";
			return;
		}

		if (game_state=="practice") game_state="normal";
		busy=true;
		count++;

		if (count < 20) {
			document.forms[0].status.value="Practicing.";
			for (i=0; i < count; i++)
				document.forms[0].status.value+=".";
			
			function_name="practice("+count+");"
			window.setTimeout(function_name,200);
		} else {
			busy=false;
			count=0;

			exp_gained=Math.floor(Math.random() * 20 + 2);
			document.forms[0].status.value="Practice makes perfect; you gained " + exp_gained + " experience points.";
			exp_(exp_gained);

		} // end of else count is >= 10

	}  //end of else not count==0

}

function rest(count) {
	var action=Math.floor(Math.random()*100+1);
	var function_name="";
	var hp_gained=0;

	if (count == 0) {
		if (!busy) rest(1);
	} else {
		if ( (game_state!="normal") && (game_state!="rest") ) {
			document.forms[0].status.value="Sorry, you need to "+game_state+".";
			return;
		}

		if (game_state=="rest") game_state="normal";
		busy=true;
		count++;

		if (count < 50) {
			document.forms[0].status.value="Resting.";
			for (i=0; i < count; i++)
				document.forms[0].status.value+=".";
			
			function_name="rest("+count+");"
			window.setTimeout(function_name,550);
		} else {
			busy=false;
			count=0;

			hp_gained=hp_max-hp;
			hp_(hp_gained);
			document.forms[0].status.value="Any lost hit points were regained.";
			
		} // end of else count is >= 10

	}  //end of else not count==0
}

function eat(count) {
	var action=Math.floor(Math.random()*100+1);
	var function_name="";
	var hp_gained=0;

	if (count == 0) {
		if (!busy) eat(1);
	} else {
		if ( (game_state!="normal") && (game_state!="eat") ) {
			document.forms[0].status.value="Sorry, you need to "+game_state+".";
			return;
		}

		if (game_state=="eat") game_state="normal";
		busy=true;
		count++;

		if (count < 10) {
			document.forms[0].status.value="Eating.";
			for (i=0; i < count; i++)
				document.forms[0].status.value+=".";
			
			function_name="eat("+count+");"
			window.setTimeout(function_name,350);
		} else {
			busy=false;
			count=0;

			if (action < 50) {
				document.forms[0].status.value="Hmm...You weren't hungry after all.";
			} else if (action < 72) {
				hp_gained=hp_max-hp;
				hp_(hp_gained);
				document.forms[0].status.value="You feel refreshed; any lost hit points were regained."; 
			} else {
				exp_(1);
				document.forms[0].status.value="You feel refreshed; you gained an experience point."; 
			}
		} // end of else count is >= 10
	}  //end of else not count==0
}

function new_dude() {
	if (busy) return;

	answer=prompt("Click 'OK' to ERASE EVERYTHING.  Click 'Cancel' to change your mind.","OK"); 
	if (answer) {
		name="";
		level=1;
		exp=0;
		att=14;
		def=14;
		hp=10;
		hp_max=10;
		gp=0;
		busy=false;
		game_state="normal";
		for (i=0; i < 7; i++) {
			dd_items[i]="-1,-1";
			dd_skills[i]="-1";
		}

		document.forms[0].status.value="Welcome";
		document.forms[0].field1.value="";
		document.forms[0].field2.value="";
		document.forms[0].field3.value="";
		document.forms[0].field4.value="1";
		document.forms[0].field5.value="";
		document.forms[0].field6.value="";
		document.forms[0].field7.value="0";
		document.forms[0].field8.value="";
		document.forms[0].field9.value="";
		document.forms[0].field10.value="14 / +7";
		document.forms[0].field11.value="";
		document.forms[0].field12.value="";
		document.forms[0].field13.value="14 / +7";
		document.forms[0].field14.value="";
		document.forms[0].field15.value="";
		document.forms[0].field16.value="10";
		document.forms[0].field17.value="";
		document.forms[0].field18.value="";
		document.forms[0].field19.value="0";
		document.forms[0].field20.value="";
		document.forms[0].field21.value="";
		document.forms[0].field22.value="";
		document.forms[0].field23.value="";
		document.forms[0].field24.value="";
		document.forms[0].field25.value="";
		document.forms[0].field26.value="";
		document.forms[0].field27.value="";
		document.forms[0].field28.value="";
		document.forms[0].field29.value="";
		document.forms[0].field30.value="";
		document.forms[0].field31.value="";
		document.forms[0].field32.value="";
		document.forms[0].field33.value="";
		document.forms[0].field34.value="";
		document.forms[0].field35.value="";
	} 

}

function display() {
	var message="";
	DispWin=window.open('', 'NewWin', 'toolbar=no, resizeable=1, width=450, height=450, scrollbars=1');

	message+="<h2>Dungeon Dudes #"+document.forms[0].dd_number.value+"</h2>";
	message+="<table><tr><td>";
	message+="<b>Name:</b> "+document.forms[0].field1.value+"<br />";
	message+="<b>LVL:</b> "+document.forms[0].field4.value+"<br />";
	message+="<b>EXP:</b> "+document.forms[0].field7.value+"<br />";
	message+="<b>ATT:</b> "+document.forms[0].field10.value+"<br />";
	message+="<b>DEF:</b> "+document.forms[0].field13.value+"<br />";
	message+="<b>HP:</b> "+document.forms[0].field16.value+"<br />";
	message+="<b>GP:</b> "+document.forms[0].field19.value+"<br />";
	message+="</td><td>";
 	message+="<img src="+document.images[0].src+" height='70' alt='' /><br /></td></tr></table>";

	message+="<table><tr><td>";
	message+="<p>";
	message+="<ul>";
	message+="<li><b>Items</b></li>";
	message+="<li>"+document.forms[0].field2.value+"</li>";
	message+="<li>"+document.forms[0].field5.value+"</li>";
	message+="<li>"+document.forms[0].field8.value+"</li>";
	message+="<li>"+document.forms[0].field11.value+"</li>";
	message+="<li>"+document.forms[0].field14.value+"</li>";
	message+="<li>"+document.forms[0].field17.value+"</li>";
	message+="<li>"+document.forms[0].field20.value+"</li>";
	message+="</ul></p></td><td>";

	message+="<p>";
	message+="<ul>";
	message+="<li><b>Skills</b></li>";
	message+="<li>"+document.forms[0].field3.value+"</li>";
	message+="<li>"+document.forms[0].field6.value+"</li>";
	message+="<li>"+document.forms[0].field9.value+"</li>";
	message+="<li>"+document.forms[0].field12.value+"</li>";
	message+="<li>"+document.forms[0].field15.value+"</li>";
	message+="<li>"+document.forms[0].field18.value+"</li>";
	message+="<li>"+document.forms[0].field21.value+"</li>";
	message+="</ul></p></td></tr></table>";


	message+="<h5>Print this page by using standard browser controls such as CTRL+P.</br>";
	message+="Dungeon Dudes &copy; 2003-2004 Jeffrey A. Mills, DVM Dimensions Game Software</h5>";

	DispWin.document.write(message);

} // end display()

function load() {
	var dd_code=document.forms[0].load_save.value;
	var dd_code_parts=dd_code.split(",");
	var key=dd_code_parts[0];

	if ( eval( eval(dd_code_parts[50])  - eval(key) ) != eval(document.forms[0].dd_number.value) ) {
		alert("Oops, that code is for Dungeon Dude # "+eval( eval(dd_code_parts[50])  - eval(key) )+".  I'll convert your data as closely as possible." );
	}
		
	level=eval(eval(dd_code_parts[1])-eval(key));
	exp=eval(eval(dd_code_parts[3])-eval(key));
	hp=eval(eval(dd_code_parts[5])-eval(key));
	att=eval(eval(dd_code_parts[7])-eval(key));
	def=eval(eval(dd_code_parts[9])-eval(key));
	hp_max=eval(eval(dd_code_parts[11])-eval(key));
	gp=eval(eval(dd_code_parts[13])-eval(key));
	
	for (i=0; i < 7; i++) {
		dd_items[i]=dd_code_parts[15+i*5]+","+dd_code_parts[16+i*5];
		dd_skills[i]=eval(eval(dd_code_parts[18+i*5])-eval(key));
	}

	name=dd_code_parts[51];

	document.forms[0].status.value="Welcome back "+name+".";
	document.forms[0].field1.value=name;
	document.forms[0].field4.value=level;
	document.forms[0].field7.value=exp;
	document.forms[0].field10.value=att + " / +" + Math.floor(att/2);
	document.forms[0].field13.value=def + " / +" + Math.floor(def/2);
	document.forms[0].field16.value=hp;
	document.forms[0].field19.value=gp;
	document.forms[0].field2.value=item_to_string(dd_items[0]);
	document.forms[0].field5.value=item_to_string(dd_items[1]);
	document.forms[0].field8.value=item_to_string(dd_items[2]);
	document.forms[0].field11.value=item_to_string(dd_items[3]);
	document.forms[0].field14.value=item_to_string(dd_items[4]);
	document.forms[0].field17.value=item_to_string(dd_items[5]);
	document.forms[0].field20.value=item_to_string(dd_items[6]);
	document.forms[0].field22.value=item_to_string(dd_items[0]);
	document.forms[0].field24.value=item_to_string(dd_items[1]);
	document.forms[0].field26.value=item_to_string(dd_items[2]);
	document.forms[0].field28.value=item_to_string(dd_items[3]);
	document.forms[0].field30.value=item_to_string(dd_items[4]);
	document.forms[0].field32.value=item_to_string(dd_items[5]);
	document.forms[0].field34.value=item_to_string(dd_items[6]);

	if (dd_skills[0]!="-1") document.forms[0].field3.value=skills[dd_skills[0]];
	else document.forms[0].field3.value="";
	if (dd_skills[1]!="-1") document.forms[0].field6.value=skills[dd_skills[1]];
	else document.forms[0].field6.value="";	
	if (dd_skills[2]!="-1") document.forms[0].field9.value=skills[dd_skills[2]];
	else document.forms[0].field9.value="";	
	if (dd_skills[3]!="-1") document.forms[0].field12.value=skills[dd_skills[3]];
	else document.forms[0].field12.value="";
	if (dd_skills[4]!="-1") document.forms[0].field15.value=skills[dd_skills[4]];
	else document.forms[0].field15.value="";
	if (dd_skills[5]!="-1") document.forms[0].field18.value=skills[dd_skills[5]];
	else document.forms[0].field18.value="";
	if (dd_skills[6]!="-1") document.forms[0].field21.value=skills[dd_skills[6]];
	else document.forms[0].field21.value="";
	if (dd_skills[0]!="-1") document.forms[0].field23.value=skills[dd_skills[0]];
	else document.forms[0].field23.value="";
	if (dd_skills[1]!="-1") document.forms[0].field25.value=skills[dd_skills[1]];
	else document.forms[0].field25.value="";	
	if (dd_skills[2]!="-1") document.forms[0].field27.value=skills[dd_skills[2]];
	else document.forms[0].field27.value="";	
	if (dd_skills[3]!="-1") document.forms[0].field29.value=skills[dd_skills[3]];
	else document.forms[0].field29.value="";
	if (dd_skills[4]!="-1") document.forms[0].field31.value=skills[dd_skills[4]];
	else document.forms[0].field31.value="";
	if (dd_skills[5]!="-1") document.forms[0].field33.value=skills[dd_skills[5]];
	else document.forms[0].field33.value="";
	if (dd_skills[6]!="-1") document.forms[0].field35.value=skills[dd_skills[6]];
	else document.forms[0].field35.value="";

}  // end load()

function save() {
	var dd_code="";
	var key=Math.floor(Math.random()*10+1); // key values 1-10 will be for version one of the software
	name=document.forms[0].field1.value;
	
	dd_code+=key+",";
	dd_code+=eval(eval(level) + key) +",";
	dd_code+=Math.floor(Math.random()*10)+",";
	dd_code+=eval(eval(exp) + key)+",";
	dd_code+=Math.floor(Math.random()*10)+",";
	dd_code+=eval(eval(hp) + key)+",";	
	dd_code+=Math.floor(Math.random()*10)+",";
	dd_code+=eval(eval(att) + key) +",";
	dd_code+=Math.floor(Math.random()*10)+",";
	dd_code+=eval(eval(def) + key) +",";
	dd_code+=Math.floor(Math.random()*10)+",";
	dd_code+=eval(eval(hp_max) + key) +",";
	dd_code+=Math.floor(Math.random()*10)+",";
	dd_code+=eval(eval(gp) + key) +",";
	dd_code+=Math.floor(Math.random()*10)+",";
	
	for (i=0; i < 7; i++) {
		dd_code+=dd_items[i]+",";
		dd_code+=Math.floor(Math.random()*10)+",";
		dd_code+=eval(eval(dd_skills[i]) + key) +",";
		dd_code+=Math.floor(Math.random()*10)+",";
	}

	dd_code+=eval(eval(document.forms[0].dd_number.value) + key) + ",";
	dd_code+=name;
	document.forms[0].load_save.value=dd_code;
} // end save()

function deletestuff() {
	temp_items=new Array(7);
	temp_skills=new Array(7);
	for (i=0; i < 7; i++) {
		temp_items[i]=dd_items[i];
		temp_skills[i]=dd_skills[i];
	}

	// delete everything then add it back if it wasn't deleted
	for (i=0; i < 7; i++) {
		dd_items[i]="-1,-1";  // "type, index"
		dd_skills[i]="-1";    // "index"
	}

	// items
	item_parts=temp_items[0].split(",");
	if ((document.forms[0].check22.checked==false) && (temp_items[0] != "-1,-1") ) {
		item_(item_parts[0],item_parts[1]);
	} 
	item_parts=temp_items[1].split(",");
	if ((document.forms[0].check24.checked==false)  && (temp_items[1] != "-1,-1") ) {
		item_(item_parts[0],item_parts[1]);
	} 
	item_parts=temp_items[2].split(",");
	if ((document.forms[0].check26.checked==false)  && (temp_items[2] != "-1,-1") ) {
		item_(item_parts[0],item_parts[1]);
	} 
	item_parts=temp_items[3].split(",");
	if ((document.forms[0].check28.checked==false)  && (temp_items[3] != "-1,-1") ) {
		item_(item_parts[0],item_parts[1]);
	} 
	item_parts=temp_items[4].split(",");
	if ((document.forms[0].check30.checked==false)  && (temp_items[4] != "-1,-1") ) {
		item_(item_parts[0],item_parts[1]);
	} 
	item_parts=temp_items[5].split(",");
	if ((document.forms[0].check32.checked==false)  && (temp_items[5] != "-1,-1") ) {
		item_(item_parts[0],item_parts[1]);
	} 
	item_parts=temp_items[6].split(",");
	if ((document.forms[0].check34.checked==false)  && (temp_items[6] != "-1,-1") ) {
		item_(item_parts[0],item_parts[1]);
	} 

	// skills
	if ((document.forms[0].check23.checked==false)  && (temp_skills[0] != "-1") ) {
		skill_(temp_skills[0]);
	} 
	if ((document.forms[0].check25.checked==false)  && (temp_items[1] != "-1") ) {
		skill_(temp_skills[1]);
	} 
	if ((document.forms[0].check27.checked==false)  && (temp_items[2] != "-1") ) {
		skill_(temp_skills[2]);
	} 
	if ((document.forms[0].check29.checked==false)  && (temp_items[3] != "-1") ) {
		skill_(temp_skills[3]);
	} 
	if ((document.forms[0].check31.checked==false)  && (temp_items[4] != "-1") ) {
		skill_(temp_skills[4]);
	} 
	if ((document.forms[0].check33.checked==false)  && (temp_items[5] != "-1") ) {
		skill_(temp_skills[5]);
	} 
	if ((document.forms[0].check35.checked==false)  && (temp_items[6] != "-1") ) {
		skill_(temp_skills[6]);
	} 

	document.forms[0].field2.value=item_to_string(dd_items[0]);
	document.forms[0].field5.value=item_to_string(dd_items[1]);
	document.forms[0].field8.value=item_to_string(dd_items[2]);
	document.forms[0].field11.value=item_to_string(dd_items[3]);
	document.forms[0].field14.value=item_to_string(dd_items[4]);
	document.forms[0].field17.value=item_to_string(dd_items[5]);
	document.forms[0].field20.value=item_to_string(dd_items[6]);
	document.forms[0].field22.value=item_to_string(dd_items[0]);
	document.forms[0].field24.value=item_to_string(dd_items[1]);
	document.forms[0].field26.value=item_to_string(dd_items[2]);
	document.forms[0].field28.value=item_to_string(dd_items[3]);
	document.forms[0].field30.value=item_to_string(dd_items[4]);
	document.forms[0].field32.value=item_to_string(dd_items[5]);
	document.forms[0].field34.value=item_to_string(dd_items[6]);

	if (dd_skills[0]!="-1") document.forms[0].field3.value=skills[dd_skills[0]];
	else document.forms[0].field3.value="";
	if (dd_skills[1]!="-1") document.forms[0].field6.value=skills[dd_skills[1]];
	else document.forms[0].field6.value="";	
	if (dd_skills[2]!="-1") document.forms[0].field9.value=skills[dd_skills[2]];
	else document.forms[0].field9.value="";	
	if (dd_skills[3]!="-1") document.forms[0].field12.value=skills[dd_skills[3]];
	else document.forms[0].field12.value="";
	if (dd_skills[4]!="-1") document.forms[0].field15.value=skills[dd_skills[4]];
	else document.forms[0].field15.value="";
	if (dd_skills[5]!="-1") document.forms[0].field18.value=skills[dd_skills[5]];
	else document.forms[0].field18.value="";
	if (dd_skills[6]!="-1") document.forms[0].field21.value=skills[dd_skills[6]];
	else document.forms[0].field21.value="";
	if (dd_skills[0]!="-1") document.forms[0].field23.value=skills[dd_skills[0]];
	else document.forms[0].field23.value="";
	if (dd_skills[1]!="-1") document.forms[0].field25.value=skills[dd_skills[1]];
	else document.forms[0].field25.value="";	
	if (dd_skills[2]!="-1") document.forms[0].field27.value=skills[dd_skills[2]];
	else document.forms[0].field27.value="";	
	if (dd_skills[3]!="-1") document.forms[0].field29.value=skills[dd_skills[3]];
	else document.forms[0].field29.value="";
	if (dd_skills[4]!="-1") document.forms[0].field31.value=skills[dd_skills[4]];
	else document.forms[0].field31.value="";
	if (dd_skills[5]!="-1") document.forms[0].field33.value=skills[dd_skills[5]];
	else document.forms[0].field33.value="";
	if (dd_skills[6]!="-1") document.forms[0].field35.value=skills[dd_skills[6]];
	else document.forms[0].field35.value="";

} // end deletestuff()

function about() {
	alert("Dungeons Dudes...'Not Just Another Virtual Pet' (c)2003-2004 Jeffrey A. Mills, DVM Dimensions Game Software");
} // end about()
