// Function to perform the calculation for stat values
// Triggered from skill edit box changes
function calc_stat(loop)
{
  var stat, min, max;
  var i;
  
  for (i = 0; i < 8; i++)
  {
    stat = parseInt(validate_number(document.getElementById("stat" + i).value, 0, 80));
    document.getElementById("stat" + i).value = stat;
    
    min = parseInt(document.getElementById("st_base" + i).value);
    max = parseInt(document.getElementById("st_max" + i).value);
    
    if ((stat + min) > max)
    {
      document.getElementById("st_actual" + i).style.backgroundColor = "#FF0000";
    }
    else
    {
      document.getElementById("st_actual" + i).style.backgroundColor = "#008000";
    }
    
    document.getElementById("st_actual" + i).value = stat + min;
  } // for i
  
  if (loop == 0)
  {
    calc_ap( ); // Spend AP
  }
}
// Update the Health, Stamina, Gamma & Max Weight based on the new stats
function calc_hp_str( )
{
  var level = validate_number(document.getElementById("level").value, 2, 150);
  var endur = validate_number(document.getElementById("stat3").value, 0, 80);
  var intel = validate_number(document.getElementById("stat4").value, 0, 80);
  var stren = validate_number(document.getElementById("stat6").value, 0, 80);
  var willp = validate_number(document.getElementById("stat7").value, 0, 80);

  var health = (level * 5) + 50 + stren + (endur * 4);
  var stamina = (level * 5) + 50 + willp + (endur * 4);
  var gamma = (level * 5) + 50 + (willp * 4) + intel;
  var weight = (level * .5) + 45 + (stren * .5);
  
  document.getElementById("health").value = health;
  document.getElementById("stamina").value = stamina;
  document.getElementById("gamma").value = gamma;
  document.getElementById("maxweight").value = weight;
}
// Decrease a stat by 1
function statdown(i)
{
  var stat = validate_number(document.getElementById("stat" + i).value, 0, 80);
  
  if (stat > 0) stat--; 
  
  document.getElementById("stat" + i).value = stat;
  
  calc_stat(0);
}
// Increase a stat by 1
function statup(i)
{
  var stat = validate_number(document.getElementById("stat" + i).value, 0, 80);
  
  if (stat < 80) stat++; 
  
  document.getElementById("stat" + i).value = stat;
  
  calc_stat(0);
}
// Display the hint text for the selected stat
function stat_selected(i)
{
  mutation_popup_close( );

  switch(i)
  {
    case 0:
      prepare_hint("Charisma");
      hint_text("Charisma affects a character's Group Tactics Skill and which missions NPCs offer you. In the future, Charisma will have more applications. Raise your Charisma if you want to have a high Group Tactics Skill or you want to take missions limited by Charisma.");
      hint_add("Increasing Stats raises the base &amp; max of skills and mutations. 1 point stat increase costs 5 AP");
      hint_add("");
      hint_add_stat("Derived Stats", "None");
      hint_add_stat("Skills", "Group Tactics (75%) Social (75%)");
      hint_add_stat("Mutations", "Telepathy (75%)");
      hint_add_stat("Saves", "None");
      break;
    case 1:
      prepare_hint("Coordination");
      hint_text("Coordination affects a character's Athletics, Dodge, Melee, and Stealth Skills, as well as a character's Reflexes save. Raise your Coordination to increase your defensive abilities.");
      hint_add("Increasing Stats raises the base &amp; max of skills and mutations. 1 point stat increase costs 5 AP");
      hint_add("");
      hint_add_stat("Derived Stats", "None");
      hint_add_stat("Skills", "Armour Use (50%) Athletics (25%) Dodge (75%) Melee (25%)");
      hint_add_stat("Mutations", "None");
      hint_add_stat("Saves", "Reflex");
      break;
    case 2:
      prepare_hint("Dexterity");
      hint_text("Dexterity is used in determining a character's Melee, Pistol, and Rifle Skills and a character's Reflexes save. Raise your Dexterity to increase your offensive abilities.");
      hint_add("Increasing Stats raises the base &amp; max of skills and mutations. 1 point stat increase costs 5 AP");
      hint_add("");
      hint_add_stat("Derived Stats", "None");
      hint_add_stat("Skills", "Pistol (75%) Rifle (75%)");
      hint_add_stat("Mutations", "None");
      hint_add_stat("Saves", "Reflex");
      break;
    case 3:
      prepare_hint("Endurance");
      hint_text("Endurance is used to calculate Health, Stamina, and Body save, and partly determine a character's Armor Use and Athletics Skills. Increase your Endurance to increase your overall survivability.");
      hint_add("Increasing Stats raises the base &amp; max of skills and mutations. 1 point stat increase costs 5 AP");
      hint_add("");
      hint_add_stat("Derived Stats", "Health &amp; Stamina");
      hint_add_stat("Skills", "Armour Use (50%) Athletics (75%)");
      hint_add_stat("Mutations", "Empathic (75%) Patho-Transmission (75%)");
      hint_add_stat("Saves", "Body");
      break;
    case 4:
      prepare_hint("Intelligence");
      hint_text("Intelligence is used in determining Bio Energy, the First Aid Skill, Group Tactics Skill, all Tradeskills, and a character's Mind save. Increase your Intelligence to max out your First Aid, use Mutations, or use Tradeskills.");
      hint_add("Increasing Stats raises the base &amp; max of skills and mutations. 1 point stat increase costs 5 AP");
      hint_add("");
      hint_add_stat("Derived Stats", "Gamma");
      hint_add_stat("Skills", "First Aid (75%) All Tradeskills (75%)");
      hint_add_stat("Mutations", "Empathic (75%) Nano-Manipulation (75%) Telekinesis (75%)");
      hint_add_stat("Saves", "Mind");
      break;
    case 5:
      prepare_hint("Perception");
      hint_text("Perception is used as a secondary component in many Skills. Raising Perception is useful for all characters.");
      hint_add("Increasing Stats raises the base &amp; max of skills and mutations. 1 point stat increase costs 5 AP");
      hint_add("");
      hint_add_stat("Derived Stats", "None");
      hint_add_stat("Skills", "Dodge (25%) First Aid (25%) Group Tactics (25%) Pistol (25%) Rifle (25%) Social (25%) All Tradeskills (25%)");
      hint_add_stat("Mutations", "Sonic Influence (75%) Suppression (75%) Thermal Control (75%)");
      hint_add_stat("Saves", "None");
      break;
    case 6:
      prepare_hint("Strength");
      hint_text("Strength determines a character's Armor Use, Body save, Encumbrance, Melee Skills, and Health. Raise your Strength if you wish to play a Melee character or a highly damage-resistant character."); 
      hint_add("Increasing Stats raises the base &amp; max of skills and mutations. 1 point stat increase costs 5 AP");
      hint_add("");
      hint_add_stat("Derived Stats", "Health &amp; Max Weight");
      hint_add_stat("Skills", "Melee (75%)");
      hint_add_stat("Mutations", "Primal (75%)");
      hint_add_stat("Saves", "Body");
      break;
    case 7:
      prepare_hint("Willpower");
      hint_text("Willpower primarily determines how much Stamina and Bio Energy a character has and the character's Mind save. Raise your Willpower if you wish to use Mutations.");
      hint_add("Increasing Stats raises the base &amp; max of skills and mutations. 1 point stat increase costs 5 AP");
      hint_add("");
      hint_add_stat("Derived Stats", "Stamina &amp; Gamma");
      hint_add_stat("Skills", "None");
      hint_add_stat("Mutations", "All (25%)");
      hint_add_stat("Saves", "Mind");
      break;
    default:
      break;
  } 	 	 	 	
}
function highlight_stat(i)
{
  document.getElementById("stat" + i).select( );
}
