function random() { 
  random.seed = (random.seed*random.a + random.c) % random.m; 

  return random.seed / random.m; 
}
random.m=714025; random.a=4096; random.c=150889; 
random.seed = (new Date()).getTime()%random.m;

var wTYPES = ["Sword", "Axe", "Mace", "Arrows", "Bow", "Polearm"];

var wSPECS = ["Glowing", "Defending", "Wounding", "MasterWork", "Adamantine", "Seeking", "Critical Damage"];

var aTYPES = ["Chain Mail Hauberk", "Great Helm", "Small Helm", "Plate Mail Chestplate", "Leather Greaves", "Spiked Gloves", "Girdle", "Bandolero", "Leather Jerkin", "Leather Trowsers", "Steel Gauntlets"];

var aSPECS = ["Glowing", "Saintlyhood", "Healing", "MasterWork", "Adamantine"];

var shTYPES = ["Buckler", "Small Wooden Round Shield", "Iron Kite Shield", "Steel Kite Shield", "Large Wooden Shield", "Steel Tower Shield"];

var shSPECS = ["Blocking", "Roaring", "Dancing","Spiked","Holy","MasterWork"];

var ptTYPES = ["Small Vial", "Elixer", "Potion", "Rune Encrusted Vial", "Large Potion", "Ceramic Jug"];

var ptSPECS = ["Jump","Jump","Jump","Jump","Jump","Spider Climb","Spider Climb","Spider Climb","Spider Climb","Spider Climb","Cure Light Wounds","Cure Light Wounds","Cure Light Wounds","Cure Light Wounds","Cure Light Wounds","Cure Light Wounds","Cure Light Wounds","Cure Light Wounds","Cure Light Wounds","Cure Light Wounds","Love", "Vision","Vision","Vision","Vision", "Swimming","Swimming","Swimming","Swimming", "Hiding","Hiding","Hiding","Hiding","Sneaking","Sneaking","Sneaking","Sneaking", "Timelessness(oil)","Reduce","Reduce","Reduce","Reduce","Reduce", "Enlarge","Enlarge","Enlarge","Enlarge","Enlarge","Speak With Animals","Speak With Animals","Speak With Animals", "Clairsentience","Clairsentience","Clairsentience", "Charisma","Charisma","Charisma","Intelligence","Intelligence","Intelligence","Wisdom","Wisdom","Wisdom","Alter Self","Alter Self","Alter Self", "Blur", "Dark Vision","Dark Vision", "Dark Vision",  "Ghoul Touch", "Ghoul Touch", "Ghoul Touch", "Delay Poison","Delay Poison","Delay Poison", "Endurance","Endurance","Endurance", "Cure Moderate Wounds", "Cure Moderate Wounds", "Cure Moderate Wounds","Detect Thoughts","Detect Thoughts","Detect Thoughts", "Levitate","Levitate","Levitate", "Aid","Aid", "Invisibility","Invisibility", "Lesser Restoration", "Cat's Grace", "Bull's Strength", "Truth","Glibness", "Nondetection", "Tongues"];

var rgTYPES = ["Iron Ring", "Silver Ring", "Platinum Ring", "Mythril Ring", "Gold Ring", "Copper Ring", "Brass Ring", "Bronze Ring"];

var rgSPECS = ["Climbing","Jumping","Protection +1","Protection +1","Protection +1","Warmth","Feather Falling","Feather Falling","Swimming", "Sustanence", "Counterspells","Mind Shielding", "Protection +2","Protection +2","Force Shield","Ram", "Animal Friendship", "Chameleon Power", "Water Walking", "Minor Elemental Resistance"];


var wNspecs = 7;
var wNtypes = 6;
var aNspecs = 5;
var aNtypes = 11;
var shNspecs =6;
var shNtypes = 6;
var ptNspecs =100;
var ptNtypes = 6;
var rgNspecs =20;
var rgNtypes = 8;

var first = true;
var counter = 0;
var item = new Object();
var chest = new Array();
chest[counter] = "";
lastcolor = 0;

function ClearData()
{
   
   first = true;
   counter = -1;
   item = new Object();
   chest = new Array();
   chest[counter] = "";
   lastcolor = 0;
   display();
   
}

function GenTreasure(t)
{
	GenItem(t);
}

function GenItem(t)
{
	if (t == -1) t = getNum(5,1) - 1;
	item = makeitem(t);
	item.index = counter;
  	chest[item.index] = item.description;
  	display(item);
}

function makeitem(t)
{

  	var Idata = new Object;
  	Idata.type = getItemType(t);
  	if (t == 3)
  	{
     		Idata.magical = true;
     		Idata.special = true;
     		Idata.potion = true;
  	}
  	else
  	{
	     	Idata.potion = false;
  	}
	  if (t == 4)
	  {
		     Idata.magical = true;
		     Idata.special = true;
		     Idata.ring = true;
	  }
	  else
	  {
		     Idata.ring = false
	  }
	  if(Idata.type == "Arrows") Idata.type = "Arrows("+getNum(20,5)+")";
	  Idata.plus = getPlus(t,Idata);
	  Idata.special = getSpecial(t,Idata);
	  Idata.description = getDescription(Idata); 
	  return Idata;  
}

function display(Idata)
{
	var constructionData = "";
	var c = document.getElementById("content");
	var n = document.getElementById("numberbox");
	var d = document.getElementById("descriptionbox");
	if (first)
	{
		c.innerHTML = "";
		n.innerHTML = "";
		d.innerHTML = "";
		constructionData = "";
		counter=0;
	}
	if(Idata)
	{
		constructionData += "<div onclick='describe("+item.index+")'";
   		constructionData += " style='color:"+GetColor()+"' class=\"itemStyle\" onmouseover=\"this.className='itemStyleOver'\" onmouseout=\"this.className='itemStyle'\"> >>: ";
   		if (Idata.plus)
		{
			constructionData += "+" + Idata.plus;
		}
   		constructionData += " " + Idata.type;
		if (Idata.special)
		{
			constructionData += " of " + Idata.special;
		}
   		constructionData += "</div>";
		c.innerHTML += constructionData;
		n.style.display="block";
		counter++;
		n.innerHTML = " " + counter;
   	}
}

function getItemType(t)
{
   	var type = "";
   	var num = 0;
   	if (t == 0) //if weapon button was pressed
	{
		num = getNum((wNtypes),1);
 		type = wTYPES[num - 1];
   	}
   	if ( t == 1) // armor button was pressed
   	{
      		num = getNum((aNtypes),1);
      		type = aTYPES[num - 1]
   	}
   	if ( t == 2) // shield button was pressed
   	{
      		num = getNum((shNtypes),1);
      		type = shTYPES[num - 1]
   	}
   	if (t == 3) // potion button was pressed
   	{
      		num = getNum((ptNtypes),1);
     	 	type = ptTYPES[num - 1]
   	}
   	if (t == 4) // potion button was pressed
   	{
      		num = getNum((rgNtypes),1);
      		type = rgTYPES[num - 1]
   	}
   	first = false;
   	return type;
}


function getNum(sides, numdice)
{
   	var i=0;
   	var die=0;
   	var total=0; 
   	for(i=0;i<numdice;i++)
   	{
          		die = Math.ceil(random()*sides);
          		total=total  + die;   
   	}
   	return total;
}

function getPlus(t,objData)
{
   	var base = getNum(100,1);
      	if (( t >= 0 ) && (t <= 2))
   	{
		if(base < 40) 
     		{
       	 		plus = "";
	       	 	objData.magical = false;
     		}
	     	else if (base < 70)
     		{
        			plus = 1;
	        		objData.magical = true;
     		}
	     	else if(base < 85)
     		{
        			plus = 2;
			objData.magical = true;
     		}
	     	else if(base< 95)
     		{
        			plus = 3;
	        		objData.magical = true;
     		}
	     	else if(base < 100)
     		{
        			plus = 4;
	        		objData.magical = true;
     		}
	     	else
     		{
        			plus = 5;
	        		objData.magical = true;
     		}
	 }
	 else 
   	{
		plus = "";
   	}
	return plus;
}
function getSpecial(t,objData)
{
    	var num =0;
    	var data = 0;
    	var chances = objData.plus;
    	if (t >= 3)  //is a potion or ring?
    	{
      		data = genSpecial(t);
    	}
    	else
	{
	      	for (i = 1; i < chances + 1;i++)
      		{
         			num = getNum(100, 1);
	         		if (num >= 94)
         			{
            				data = genSpecial(t);
            				objData.special = true;
	            			done = true;
         			}
         			else
	         		{
            				objData.special = false;
            				done = false;
	         		}
         			if (done)
         			{
           				i = chances;
	         		}
      		}
	}
    	return data;
}

function genSpecial(t)
{
   	var num;
   	var data;
   	if (t == 0)
   	{
     		num = getNum(wNspecs,1);
     		data = wSPECS[num - 1];
   	}
   	if (t == 1) 
   	{
     		num = getNum(aNspecs,1);
     		data = aSPECS[num -1]
   	}
   	if (t == 2) 
   	{
    		 num = getNum(shNspecs,1);
     		data = shSPECS[num -1]
   	}
   	if (t == 3)
   	{
    		num = getNum(ptNspecs,1);
     		data = ptSPECS[num -1]
   	}
   	if (t == 4)
   	{
     		num = getNum(rgNspecs,1);
     		data = rgSPECS[num -1]
   	}
   	return data;
}


function getDescription(objItem)
{
     	var textdescription = "";
   	if (objItem.magical)
     	{
	         	if (objItem.plus)
         		{
			textdescription += "This is a Magical "+objItem.type+" with a bonus of +" + objItem.plus + ".";
         		}
	         	if(objItem.special)
		{
			if (objItem.plus)
            			{ 
				textdescription += " Upon closer examination of this "+objItem.type+"'s runes you discover it has the special power of " + objItem.special;
			}
			if (objItem.ring)
			{
				textdescription += "Upon closer examination of this "+objItem.type+" you discover runes describing the magical property of " + objItem.special;
			}
			if (objItem.potion)
			{
				textdescription += "Upon closer examination of the liquid in this "+objItem.type+" you discover it has the magical power of " + objItem.special;
			}
		} 
	}
	else
	{
		textdescription += "This is merely a "+objItem.type;
	}
	return textdescription;
}

function describe(num)
{
	var d = document.getElementById("descriptionbox");
	d.innerHTML = chest[num];
}
var colorSelection = 	[ "#0099FF", "#995500", "#008800","#CCCC99","#AA55AA","#FF9900"];

function GetColor()
{
	var color = "";
	var num = 0; 
	var x = false;
	while (x==false) //keep picking a color until it does not match the last one picked.
	{
		num = getNum(6,1) - 1;
		if (num != lastcolor)  x=true;
	}
	color = colorSelection[num];
	lastcolor = num;
	return color;
}