// Update all the AP totals for level, bonus & spent
function calc_ap( )
{
  var level = validate_number(document.getElementById("level").value, 2, 150);
  var bonus = validate_number(document.getElementById("bonus").value, 0, 900);
  
  hide_javascript_warning( );

  document.getElementById("level").value = level;
  document.getElementById("bonus").value = bonus;

  var total = (level - 1) * 20;
  document.getElementById("levelap").value = total;
  total += bonus;
  document.getElementById("totalap").value = total;
  
  var spent = getSpentAp( );
  total -= spent;
  document.getElementById("apremain").value = total + " (" + spent + ")";
  
  if (total < 0)
  {
    document.getElementById("warning").style.visibility = "visible";
  }
  else
  {
    document.getElementById("warning").style.visibility = "hidden";
  }

  if (total >= 0)
  {
    document.getElementById("apremain").style.backgroundColor = "#008000";
  }
  else
  {
    document.getElementById("apremain").style.backgroundColor = "#FF0000";
  }
  
  for (i = 0; i < 8; i++)
  {
    document.getElementById("st_base" + i).value = 10 + level;
    document.getElementById("st_max" + i).value = Math.round(15 + (level * 1.5));
  } // for i
  
  calc_hp_str( );
  
  set_skill_base( );
  set_mutation_base( );
  set_tradeskill_base( );

  calc_stat(1);
  calc_skill(1);
  calc_mutation(1);

  update_faction_colours( );
  
  create_link( );
}
// Generic number validation routine
function validate_number(str, min, max)
{
	reg = /[^0-9]/g;
	str = str.replace(reg, "");
  
  if (str == "")
  {
    return min;
  }
  else
  {
    if (parseInt(str, 10) < min) return min;
    if (parseInt(str, 10) > max) return max;
    return parseInt(str, 10);
  }
}
// Calculate the total spent AP
function getSpentAp( )
{
  var total = 0;
  
  for (i = 0; i < 8; i++)
  {
    total += parseInt(validate_number(document.getElementById("stat" + i).value, 0, 80) * 5);
  } // for i
  for (i = 0; i < 9; i++)
  {
    total += parseInt(validate_number(document.getElementById("skill" + i).value, 0, 240));
  } // for i
  for (i = 1; i < 11; i++)
  {
    total += parseInt(validate_number(document.getElementById("mutation" + i).value, 0, 240));
  } // for i
  
  return total;
}
function levelup( )
{
  var stat = validate_number(document.getElementById("level").value, 0, 150);
  
  if (stat < 150) stat++; 
  
  document.getElementById("level").value = stat;
  
  calc_ap(0);
}
function leveldown( )
{
  var stat = validate_number(document.getElementById("level").value, 0, 150);
  
  if (stat > 2) stat--; 
  
  document.getElementById("level").value = stat;
  
  calc_ap(0);
}
function bonusup( )
{
  var stat = validate_number(document.getElementById("bonus").value, 0, 900);
  
  if (stat < 891) stat += 10; 
  else if (stat > 890) stat = 900;
  
  document.getElementById("bonus").value = stat;
  
  calc_ap(0);
}
function bonusdown( )
{
  var stat = validate_number(document.getElementById("bonus").value, 0, 900);
  
  if (stat > 9) stat -= 10; 
  else if (stat < 10) stat = 0;
  
  document.getElementById("bonus").value = stat;
  
  calc_ap(0);
}
// Display the hint text for level
function level_selected( )
{
  mutation_popup_close( );

  prepare_hint("Your Level");
  hint_text("Your character will gain 20 AP for each level that it attains.");
  hint_add("Increasing levels will also increase the 'base' values for skills, stats and mutations.");
  hint_add("");
  hint_add("On release, the maximum level available will be 45.");
  hint_add("However, subsequent expansions will eventually raise the level cap to 150.");
}
// Display the hint text for bonus
function bonus_selected( )
{
  mutation_popup_close( );

  prepare_hint("Bonus AP");
  hint_text("Extra AP is available through many quests, especially in each starter town.");
  hint_add("Whilst there are over 270 APs worth of quests available, some are very time consuming, and others are faction based. It is estimated that a hardy player should be able to get around 200.");
  hint_add("");
  hint_add("It is unknown at this time whether or not the subsequent expansions will add more available bonus AP.");
}
function show_cat(i)
{
  mutation_popup_close( );

  switch (i)
  {
    case 1: // Stats
      prepare_hint("Stats");
      hint_text("In Fallen Earth, stats are a way to represent an individual's basic attributes and give an idea of that person's strength, agility, and smarts. These stats relate to physical endurance, mental dexterity, social magnetism, creative intelligence, technical ingenuity, and an aptitude for using extra-sensory abilities.");
      hint_add("");
      hint_add("The stats for every attribute determine the base values for all Active Skills and Mutation Paths, in addition to limiting the maximum possible values for Tradeskills. Special abilities and equipment can raise or lower stats; for instance, wearing heavy armor temporarily reduces the Coordination attribute or goggles that increase Perception.");
      hint_add("");
      hint_add("Derived attributes further define a character's traits and are calculated based on the characters stats. ");
      break;
    case 2: // Skills
      prepare_hint("Skills");
    hint_text("The character's learned abilities. They are involved in normal, non-mutation based combat, using items, and interacting with the environment. All players have some rating in all the active skills.");
      hint_add("");
      hint_add("Active skills have a base value which is based on 2 of your attributes. For example, a character's base Melee is equal to 75% of their strength and 25% of their coordination. Skills listed here (Active Skills) are raised at a cost of 1 AP per point up to double the base value of the skill.");
      break;
    case 3: // Mutations
      prepare_hint("Mutations");
    hint_text("Maybe it was the virus, maybe it was the radiation, or maybe it was some kind of manipulation of the basic genetic structure shared by all humans. Most likely, it was some combination of all these factors that created the different Mutation Paths available to the denizens of the Grand Canyon Province. These choices range from Telepathic to Empathy to Nano-Manipulation, and each one has its own unique abilities. Each Mutation Path falls into one of three fundamental types: Augmentation, Restoration, and Destruction. The types describe the purpose of most, but not all, mutations in that path. ");
      hint_add("Everyone begins with mutations in the Alpha Path (the 'GlobalTech Standard Clone Upgrade Package'), which provides healing and regeneration, in addition to some attacks. The Alpha Mutation Path is always equal to twice the player's Willpower. AP cannot be spent on it directly.");
      break;
    case 4: // Tradeskills
      prepare_hint("Tradeskills");
      hint_text("Tradeskills represent a character's aptitude for crafting and fixing everything from rifles to computers; they even include things like cooking stews and brewing up poisons. The better your Tradeskills, the better items you'll be able to craft in that area.");
      hint_add("");
      hint_add("To improve a Tradeskill, use it. As long as an item is still challenging for you (i.e., near your Tradeskill level), you'll improve as you craft it. Each item made by crafting boosts the skill (up to a certain limit that is determined by each item). The number of points earned from making an item varies based on the length of time required to make it, value of the components, etc.");
      break;
    case 5: // Factions
      set_faction_hint(6);
      break;
    default:
      break;
  } // switch
}
function derived_hint( )
{
  mutation_popup_close( );

  prepare_hint("Derived Stats");
  hint_text("Derived attributes further describe a character's traits, but unlike the primary abilities, they cannot be directly modified by spending APs. Instead, they are determined by a combination of other attributes and updated immediately as those attributes change.");
  hint_add_stat("Health", "Health represents how much damage one can take before becoming incapacitated. Strength and Endurance determine how much Health a character has.");
  hint_add_stat("Stamina", "Stamina defines how much energy a character can exert before exhaustion sets in. When Stamina bottoms out, a character also suffers reduced speed. Stamina is a product of Endurance and Willpower.");   
  hint_add_stat("Gamma", "Gamma powers a character's abilities. An individual whose Gamma completely depletes must wait for it to replenish before using another mutation. Gamma determined using a combination of Intelligence and Willpower.");
  hint_add_stat("Body Saves", "Body defines a person's resistance to invasive special abilities, such as poisons and disease. Endurance and Strength are the primary factors in the Body attribute.");
  hint_add_stat("Mind Saves", "The Mind attribute is used as a saving throw against mental attacks, such as Telepathy. Mind is a combination of Intelligence and Willpower.");
  hint_add_stat("Reflex Saves", "A character with a high Reflexes attribute can quickly sidestep special abilities (i.e., an Acid Spray). Coordination and Dexterity are used to calculate Reflexes.");
}
function highlight_level( )
{
  document.getElementById("level").select( );
}
function highlight_bonusap( )
{
  document.getElementById("bonus").select( );
}
