// Stats Calculator scripts v 1.0. Coyright 2003 Teras Kasi Order All rights reserved.
// Stephen C. Hiraoka schiraoka@hotmail.com

function fillForm(self) { // Fill Form When Race Changes
  // Set Variables
  var selRace=self.options[self.selectedIndex].value
	var maxBase = new Array(9)
		maxBase[0]=1000
		maxBase[1]=500
		maxBase[2]=400
		maxBase[3]=1000
		maxBase[4]=450
		maxBase[5]=400
		maxBase[6]=1000
		maxBase[7]=500
		maxBase[8]=500
	
	// Assign Racial Modifiers to Form Elements
  if (selRace=="Bot") {
	  document.getElementById("racBon1").value=0;
	  document.getElementById("racBon2").value=0;
	  document.getElementById("racBon3").value=0;
	  document.getElementById("racBon4").value=300;
	  document.getElementById("racBon5").value=300;
	  document.getElementById("racBon6").value=100;
	  document.getElementById("racBon7").value=100;
	  document.getElementById("racBon8").value=100;
	  document.getElementById("racBon9").value=100;
	} else
  if (selRace=="Hum") {
	  document.getElementById("racBon1").value=100;
	  document.getElementById("racBon2").value=100;
	  document.getElementById("racBon3").value=100;
	  document.getElementById("racBon4").value=100;
	  document.getElementById("racBon5").value=100;
	  document.getElementById("racBon6").value=100;
	  document.getElementById("racBon7").value=100;
	  document.getElementById("racBon8").value=100;
	  document.getElementById("racBon9").value=100;
	} else
  if (selRace=="Ith") {
	  document.getElementById("racBon1").value=0;
	  document.getElementById("racBon2").value=0;
	  document.getElementById("racBon3").value=0;
	  document.getElementById("racBon4").value=0;
	  document.getElementById("racBon5").value=0;
	  document.getElementById("racBon6").value=150;
	  document.getElementById("racBon7").value=300;
	  document.getElementById("racBon8").value=300;
	  document.getElementById("racBon9").value=150;
	} else
  if (selRace=="Mon") {
	  document.getElementById("racBon1").value=0;
	  document.getElementById("racBon2").value=0;
	  document.getElementById("racBon3").value=0;
	  document.getElementById("racBon4").value=0;
	  document.getElementById("racBon5").value=0;
	  document.getElementById("racBon6").value=150;
	  document.getElementById("racBon7").value=300;
	  document.getElementById("racBon8").value=300;
	  document.getElementById("racBon9").value=150;
	} else
  if (selRace=="Rod") {
	  document.getElementById("racBon1").value=0;
	  document.getElementById("racBon2").value=0;
	  document.getElementById("racBon3").value=0;
	  document.getElementById("racBon4").value=200;
	  document.getElementById("racBon5").value=200;
	  document.getElementById("racBon6").value=450;
	  document.getElementById("racBon7").value=0;
	  document.getElementById("racBon8").value=0;
	  document.getElementById("racBon9").value=50;
	} else
  if (selRace=="Sul") {
	  document.getElementById("racBon1").value=200;
	  document.getElementById("racBon2").value=0;
	  document.getElementById("racBon3").value=0;
	  document.getElementById("racBon4").value=300;
	  document.getElementById("racBon5").value=0;
	  document.getElementById("racBon6").value=0;
	  document.getElementById("racBon7").value=0;
	  document.getElementById("racBon8").value=0;
	  document.getElementById("racBon9").value=400;
	} else
  if (selRace=="Tra") {
	  document.getElementById("racBon1").value=250;
	  document.getElementById("racBon2").value=300;
	  document.getElementById("racBon3").value=400;
	  document.getElementById("racBon4").value=0;
	  document.getElementById("racBon5").value=0;
	  document.getElementById("racBon6").value=0;
	  document.getElementById("racBon7").value=0;
	  document.getElementById("racBon8").value=0;
	  document.getElementById("racBon9").value=100;
	} else
  if (selRace=="Twi") {
	  document.getElementById("racBon1").value=0;
	  document.getElementById("racBon2").value=0;
	  document.getElementById("racBon3").value=250;
	  document.getElementById("racBon4").value=250;
	  document.getElementById("racBon5").value=300;
	  document.getElementById("racBon6").value=0;
	  document.getElementById("racBon7").value=100;
	  document.getElementById("racBon8").value=0;
	  document.getElementById("racBon9").value=0;
	} else
  if (selRace=="Woo") {
	  document.getElementById("racBon1").value=350;
	  document.getElementById("racBon2").value=350;
	  document.getElementById("racBon3").value=150;
	  document.getElementById("racBon4").value=200;
	  document.getElementById("racBon5").value=100;
	  document.getElementById("racBon6").value=100;
	  document.getElementById("racBon7").value=100;
	  document.getElementById("racBon8").value=150;
	  document.getElementById("racBon9").value=100;
	} else
  if (selRace=="Zab") {
	  document.getElementById("racBon1").value=200;
	  document.getElementById("racBon2").value=0;
	  document.getElementById("racBon3").value=0;
	  document.getElementById("racBon4").value=300;
	  document.getElementById("racBon5").value=0;
	  document.getElementById("racBon6").value=0;
	  document.getElementById("racBon7").value=0;
	  document.getElementById("racBon8").value=0;
	  document.getElementById("racBon9").value=400;
	} else
  if (selRace=="Non") {
	  document.getElementById("racBon1").value=0;
	  document.getElementById("racBon2").value=0;
	  document.getElementById("racBon3").value=0;
	  document.getElementById("racBon4").value=0;
	  document.getElementById("racBon5").value=0;
	  document.getElementById("racBon6").value=0;
	  document.getElementById("racBon7").value=0;
	  document.getElementById("racBon8").value=0;
	  document.getElementById("racBon9").value=0;
	}
	
	// Calculate and Assign Total Racial Bonuses to Form totRacBon Form Element
	  document.getElementById("totRacBon").value=
		  parseInt(document.getElementById("racBon1").value, 10) +
		  parseInt(document.getElementById("racBon2").value, 10) +
		  parseInt(document.getElementById("racBon3").value, 10) +
		  parseInt(document.getElementById("racBon4").value, 10) +
		  parseInt(document.getElementById("racBon5").value, 10) +
		  parseInt(document.getElementById("racBon6").value, 10) +
		  parseInt(document.getElementById("racBon7").value, 10) +
		  parseInt(document.getElementById("racBon8").value, 10) +
		  parseInt(document.getElementById("racBon9").value, 10);
			
	// Calculate and Assign min/max Stat Values to Form Elements. Reset stat migration and totals.
	  for (i=1; i<=9; i++) {
		  var racVal=parseInt(document.getElementById("racBon"+i).value)
			var maxBaseVal=maxBase[i-1]
			
	    document.getElementById("racMin"+i).value=racVal+300;
			if (selRace=="Hum") {
				document.getElementById("racMax"+i).value=1100;
			} else {
			document.getElementById("racMax"+i).value=racVal+maxBaseVal; }
	    document.getElementById("migStat"+i).value=0;
	    document.getElementById("totStat"+i).value=racVal+300;
	    document.getElementById("migStatTot").value=1800;
		}
	
	// Sample and Survey Drains Calculations
	var statFoc=parseInt(document.getElementById("totStat8").value, 10)
	var statQui=parseInt(document.getElementById("totStat5").value, 10)
	var survMindCost=Math.round(((-100)+(statFoc/15))*100)/100
	var sampMindCost=Math.round(((-124)+(statFoc/12.5))*100)/100
	var sampActnCost=Math.round(((-124)+(statQui/12.5))*100)/100
	
	document.getElementById("survMind").value=survMindCost;
	document.getElementById("sampMind").value=sampMindCost;
	document.getElementById("sampActn").value=sampActnCost;
}


function calcMig(fieldNum) { // Compute Total Single Stat and Migration Points. Update Fields.
  var minVal=parseInt(document.getElementById("racMin"+fieldNum).value, 10)
	var maxVal=parseInt(document.getElementById("racMax"+fieldNum).value, 10)
	var migVal=parseInt(document.getElementById("migStat"+fieldNum).value, 10)
	var totVal=minVal+migVal
	var migMax=maxVal-minVal // Highest value which can be entered in migration
	var migStatTot=1800

	var anum=/(^\d+$)|(^\d+\.\d+$)/ // Check if value is a valid number
	if (anum.test(migVal)) {
	  testresult=true
	}	else {
		alert("Please enter a valid number.")
		testresult=false
		}

	
	if (totVal<minVal) {
	  document.getElementById("migStat"+fieldNum).value=0;
		alert("Value entered is below minimum allowabale. Lowest possible value has been entered in the field");
		migVal=0
	}
	
	if (migVal>migMax) {
	  document.getElementById("migStat"+fieldNum).value=migMax;
		alert("Value entered is above maximum allowabale. Highest possible value has been entered in the field");
		migVal=migMax
	}
	
	for (i=1; i<=9; i++) {
	  migStatTot=migStatTot-parseInt(document.getElementById("migStat"+i).value);
		if (migStatTot<0) {
			document.getElementById("migStatTot").style.color="#ff0000";
		} else {
			document.getElementById("migStatTot").style.color="#000000";
		}
	}
	
	document.getElementById("migStatTot").value=migStatTot;
  document.getElementById("totStat"+fieldNum).value=minVal+migVal;
	
	// Sample/Survey, Entertainer and Regeneration
	// Get Relevant Stats
	var statFor=parseInt(document.getElementById("totStat8").value, 10)
	var statQui=parseInt(document.getElementById("totStat5").value, 10)
	var statCon=parseInt(document.getElementById("totStat3").value, 10)
	var statSta=parseInt(document.getElementById("totStat6").value, 10)
	var statWil=parseInt(document.getElementById("totStat9").value, 10)
	
	// Sample/Survey Calculations and Fields Updates
	var survMindCost=Math.round((((statFor/15.0)-100)*100)/100)*(-1)
	var sampMindCost=Math.round((((statFor/12.5)-124)*100)/100)*(-1)
	var sampActnCost=Math.round((((statQui/12.5)-124)*100)/100)*(-1)	
	document.getElementById("survMind").value=survMindCost;
	document.getElementById("sampMind").value=sampMindCost;
	document.getElementById("sampActn").value=sampActnCost;
	
	// Entertainer Calculations and Fields Updates
	var entNormCost=Math.round((((statQui/37.5)-40)*100)/100)*(-1)
	var entFlourCost=Math.round((entNormCost/2)*100)/100
	document.getElementById("entNorm").value=entNormCost;
	document.getElementById("entFlour").value=entFlourCost;
	
	// Regeneration Calculations and Fields Updates var result=Math.round(original*100)/100	
	var regSitH=Math.round((statCon*13/1200)*100)/100
	var regSitA=Math.round((statSta*13/1200)*100)/100
	var regSitM=Math.round((statWil*13/1200)*100)/100
	var regStaH=Math.round((regSitH*(4/7))*100)/100
	var regStaA=Math.round((regSitA*(4/7))*100)/100
	var regStaM=Math.round((regSitM*(4/7))*100)/100
	var regKneH=Math.round((regSitH*(5/7))*100)/100
	var regKneA=Math.round((regSitA*(5/7))*100)/100
	var regKneM=Math.round((regSitM*(5/7))*100)/100
	var regProH=Math.round((regSitH*(4/7))*100)/100
	var regProA=Math.round((regSitA*(4/7))*100)/100
	var regProM=Math.round((regSitM*(4/7))*100)/100
	
	document.getElementById("regSitHea").value=regSitH;
	document.getElementById("regSitAct").value=regSitA;
	document.getElementById("regSitMin").value=regSitM;
	document.getElementById("regStaHea").value=regStaH;
	document.getElementById("regStaAct").value=regStaA;
	document.getElementById("regStaMin").value=regStaM;
	document.getElementById("regKneHea").value=regKneH;
	document.getElementById("regKneAct").value=regKneA;
	document.getElementById("regKneMin").value=regKneM;
	
	
	// TK Skills Cost Calculations and Fields Updates		
	var thisNum = 100000/65
	var hamCost = new Array (69)
  //  Health              Action           Mind
	    // Berserk
	    hamCost[0]=90;      hamCost[1]=60;   hamCost[2]=5;
      // Intimidate
	    hamCost[3]=80;      hamCost[4]=60;   hamCost[5]=10;
			// Taunt
	    hamCost[6]=50;      hamCost[7]=50;   hamCost[8]=75;
			// Lunge 1.444x
	    hamCost[9]=9;       hamCost[10]=13;  hamCost[11]=9;
			// Warcry
	    hamCost[12]=160;    hamCost[13]=90;  hamCost[14]=10;
			// Hit1
	    hamCost[15]=6;      hamCost[16]=9;   hamCost[17]=6;
			// Stun 1.444x
	    hamCost[18]=9;      hamCost[19]=13;  hamCost[20]=9;
      // Blind 1.444x
	    hamCost[21]=9;      hamCost[22]=13;  hamCost[23]=9;
      // Spin1 1.444x
	    hamCost[24]=9;      hamCost[25]=13;  hamCost[26]=9;
      // Hit2
	    hamCost[27]=12;     hamCost[28]=18;  hamCost[29]=12;
      // KD1 1.444x
	    hamCost[30]=15;     hamCost[31]=22;  hamCost[32]=15;
      // Dizzy
	    hamCost[33]=12;     hamCost[34]=18;  hamCost[35]=12;
      // BodyHit
	    hamCost[36]=18;     hamCost[37]=27;  hamCost[38]=18;
      // PowerBoost
	    hamCost[39]=0;      hamCost[40]=0;   hamCost[41]=0;
      // Combo1 1.444x
	    hamCost[42]=15;     hamCost[43]=22;  hamCost[44]=15;
      // LegHit
	    hamCost[45]=18;     hamCost[46]=27;  hamCost[47]=18;
		  // Spin2
	    hamCost[48]=20.667; hamCost[49]=31;  hamCost[50]=20.667;
      // ForceOfWill
	    hamCost[51]=0;      hamCost[52]=0;   hamCost[53]=0;
      // KD2
	    hamCost[54]=24;     hamCost[55]=36;  hamCost[56]=24;
		  // Combo2
	    hamCost[57]=20.667; hamCost[58]=31;  hamCost[59]=20.667;
		  // HeadHit
	    hamCost[60]=20.667; hamCost[61]=31;  hamCost[62]=20.667;
		  // Hit3
	    hamCost[63]=26.667; hamCost[64]=40;  hamCost[65]=26.667;
		  // Center of Being
	    hamCost[66]=50;     hamCost[67]=100; hamCost[68]=150;
				
	for (var i=0; i <= 22; i++) {
	  var hCount = i*3
	  var aCount = (i*3)+1
	  var mCount = (i*3)+2
		
	  var hBase = hamCost[hCount]
	  var aBase = hamCost[aCount]
	  var mBase = hamCost[mCount]
		
		var sStat = parseInt(document.getElementById("totStat2").value, 10)
		var qStat = parseInt(document.getElementById("totStat5").value, 10)
		var fStat = parseInt(document.getElementById("totStat8").value, 10)
			
		var hStep = thisNum/hBase
		var aStep = thisNum/aBase
		var mStep = thisNum/mBase
		
		var hTotal = (hBase-0.555)-(sStat/hStep)
		var aTotal = (aBase-0.555)-(qStat/aStep)
		var mTotal = (mBase-0.555)-(fStat/mStep)
			
		// Begin rounding check and correct
		var hRound = Math.round(hTotal)
		var aRound = Math.round(aTotal)
		var mRound = Math.round(mTotal)
	
		var hCheck = hTotal-hRound
		var aCheck = aTotal-aRound
		var mCheck = mTotal-mRound
			
		if (hCheck > 0.444444444444444 && hCheck < 0.5) {
			hTotal = hRound + 0.5
		}
		if (aCheck > 0.444444444444444 && aCheck < 0.5) {
			aTotal = aRound + 0.5
		}
		if (mCheck > 0.444444444444444 && mCheck < 0.5) {
			mTotal = mRound + 0.5
		}
		// End rounding check and correct
		
		document.getElementById("hSkill" + (i+1)).value = Math.round(hTotal)
		document.getElementById("aSkill" + (i+1)).value = Math.round(aTotal)
		document.getElementById("mSkill" + (i+1)).value = Math.round(mTotal)
	}
	
	// Calculate and update Power Boost with buff values
	document.getElementById("hSkill14").value = "+" + Math.round(document.getElementById("totStat7").value/2)
	document.getElementById("aSkill14").value = "+" + Math.round(document.getElementById("totStat7").value/2)
	document.getElementById("mSkill14").value = "+" + Math.round(document.getElementById("totStat7").value/2)
	
	// Enter "n/a" for Force of Will fields
	document.getElementById("hSkill18").value = "n/a"
	document.getElementById("aSkill18").value = "n/a"
	document.getElementById("mSkill18").value = "n/a"
	
}