// Funtion to update the base & max of mutations
function set_mutation_base( )
{
  var total, i;
  var level = validate_number(document.getElementById("level").value, 2, 150);
  var chari = validate_number(document.getElementById("stat0").value, 0, 80);
  var coord = validate_number(document.getElementById("stat1").value, 0, 80);
  var dexte = validate_number(document.getElementById("stat2").value, 0, 80);
  var endur = validate_number(document.getElementById("stat3").value, 0, 80);
  var intel = validate_number(document.getElementById("stat4").value, 0, 80);
  var perce = validate_number(document.getElementById("stat5").value, 0, 80);
  var stren = validate_number(document.getElementById("stat6").value, 0, 80);
  var willp = validate_number(document.getElementById("stat7").value, 0, 80);
  
  var min = 10 + level;
  
  total = willp; // Alpha
  document.getElementById("mu_base0").value = min + total; 
  document.getElementById("mu_max0").value = (min + total) * 2;
  total = Math.round((endur * .75) + (willp * .25)); // Empathic
  document.getElementById("mu_base1").value = min + total;
  document.getElementById("mu_max1").value = (min + total) * 2
  total = Math.round((intel * .75) + (willp * .25)); // Enhancement
  document.getElementById("mu_base2").value = min + total;
  document.getElementById("mu_max2").value = (min + total) * 2
  total = Math.round((intel * .75) + (willp * .25)); // Nano-Manipulation
  document.getElementById("mu_base3").value = min + total;
  document.getElementById("mu_max3").value = (min + total) * 2
  total = Math.round((endur * .75) + (willp * .25)); // Path-Manipulation
  document.getElementById("mu_base4").value = min + total;
  document.getElementById("mu_max4").value = (min + total) * 2
  total = Math.round((stren * .75) + (willp * .25)); // Primal
  document.getElementById("mu_base5").value = min + total;
  document.getElementById("mu_max5").value = (min + total) * 2
  total = Math.round((perce * .75) + (willp * .25)); // Sonic Manipulation 
  document.getElementById("mu_base6").value = min + total;
  document.getElementById("mu_max6").value = (min + total) * 2
  total = Math.round((perce * .75) + (willp * .25)); // Suppression  
  document.getElementById("mu_base7").value = min + total;
  document.getElementById("mu_max7").value = (min + total) * 2
  total = Math.round((intel * .75) + (willp * .25)); // Telekenesis
  document.getElementById("mu_base8").value = min + total;
  document.getElementById("mu_max8").value = (min + total) * 2
  total = Math.round((chari * .75) + (willp * .25)); // Telepathy
  document.getElementById("mu_base9").value = min + total;
  document.getElementById("mu_max9").value = (min + total) * 2
  total = Math.round((perce * .75) + (willp * .25)); // Thermal Manipulation  
  document.getElementById("mu_base10").value = min + total;
  document.getElementById("mu_max10").value = (min + total) * 2
}
// Funtion to perform the calculation for skill values
// Triggered from skill edit box changes
function calc_mutation(loop)
{
  var stat, min, max;
  var i;
  
  for (i = 0; i < 11; i++)
  {
    stat = parseInt(validate_number(document.getElementById("mutation" + i).value, 0, 240));
    document.getElementById("mutation" + i).value = stat;
    
    min = parseInt(document.getElementById("mu_base" + i).value);
    max = parseInt(document.getElementById("mu_max" + i).value);
    
    if ((stat + min) > max)
    {
      document.getElementById("mu_actual" + i).style.backgroundColor = "#FF0000";
    }
    else
    {
      document.getElementById("mu_actual" + i).style.backgroundColor = "#008000";
    }
    
    if (i > 0)
    {
      document.getElementById("mu_actual" + i).value = stat + min;
    }
    else
    {
      document.getElementById("mu_actual" + i).value = stat + min + min;
    }
  } // for i
  
  if (loop == 0)
  {
    calc_ap( ); // Spend AP
  }

  //show_muation_popup(loop);
}
// Decrease a mutation by 1
function mutationdown(i)
{
  var stat = validate_number(document.getElementById("mutation" + i).value, 0, 240);
  
  if (stat > 0) stat--; 
  
  document.getElementById("mutation" + i).value = stat;
  
  calc_mutation(0);
}
// Increase a mutation by 1
function mutationup(i)
{
  var stat = validate_number(document.getElementById("mutation" + i).value, 0, 240);
  
  if (stat < 240) stat++; 
  
  document.getElementById("mutation" + i).value = stat;
  
  calc_mutation(0);
}
// Display the hint text for the selected mutation
function mutation_selected(i)
{
  show_muation_popup(i);

  switch(i)
  {
    case 0:
      prepare_hint("Alpha");
      hint_text("General");
      hint_add("Fairly low-power mutations possessed by all characters as part of the cloning process. They provide some basic buffs, recovery abilities, and speed modifiers. This skill is always equal to double your Willpower.");
      hint_add("");
      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 1:
      prepare_hint("Empathic");
      hint_text("Restoration");
      hint_add("Empathy is primarily a healing and buffing mutation. Allows mutated empaths to resuscitate the dead, by taking massive damage on themselves. Higher levels of this Mutation Path can also drain Health and Stamina from an opponent.");
      hint_add("Favored by the Lightbearers.");
      hint_add("");
      hint_add_stat("Stats", "Endurance 75% Willpower 25%");
      hint_add_ability("Abilities", "Benevolence (heal) Empower (AOE team hp/sta heal) Preservation (elemental Cure) Priority (self heal) Restoration (resurrection) Sympathy Pains (psionic damage melee shield)");
      break;
    case 2:
      prepare_hint("Enhancement");
      hint_text("Augmentation");
      hint_add("The character can nudge the molecular structure and properties of some common items, exaggerating the already-present chemical properties of an item.  Enhancement is primarily a crowd-control and damage mutation path.");
      hint_add("Favored by the Techs and CHOTA.");
      hint_add("");
      hint_add_stat("Stats", "Intelligence 75% Willpower 25%");
      hint_add_ability("Abilities", "Ablate (elemental buff) Calibration (team armour buff) Dissolve (chemical DOT) Regenerate (hp/sta regen) Reinforce (armour buff) Resilience (body/reflex/mind buff)");
      break;
    case 3:
      prepare_hint("Nano-Manipulation");
      hint_text("Restoration");
      hint_add("The character can control some of the nanites left on their bodies from the regeneration process, allowing them to increase their own abilities.  Nano-Manipulation is primarily a self-buff mutation path with a few attack abilities.");
      hint_add("Favored by the Techs.");
      hint_add("");
      hint_add_stat("Stats", "Intelligence 75% Willpower 25%");
      hint_add_ability("Abilities", "Cannibalise (hp/sta to gamma syphon) Filtration (states cure) Reconstruction (AOE resurrection) Revitalise (hp/gamma regen) Self Repair (self heal) Vital Osmosis (gamma to heal other)");
      break;
    case 4:
      prepare_hint("Patho-Transmission");
      hint_text("Destruction");
      hint_add("The character is a walking plague carrier, and can use these infections to attack others.  Patho-Transmission is primarily a debuff and Damage over Time (DoT) mutation path.");
      hint_add("Favored by the CHOTA.");
      hint_add("");
      hint_add_stat("Stats", "Endurance 75% Willpower 25%");
      hint_add_ability("Abilities", "Antibody (elemental buff) Debilitating Weakness (str/end debuff) Patient Zero (AOE DOT) Sapping Sickness (sta drain &amp; end debuff) Shaking Plague (snare &amp; def/coo debuff) Wracking Pains (DOT)");
      break;
    case 5:
      prepare_hint("Primal");
      hint_text("Augmentation");
      hint_add("The character has developed genetic traits that can be used to increase strength, speed, etc... Primal is primarily a self-buff mutation path.");
      hint_add("Favored by the Vistas and CHOTA.");
      hint_add("");
      hint_add_stat("Stats", "Strength 75% Willpower 25%");
      hint_add_ability("Abilities", "Beast Might (str/end buff) Feral Grip (str/coo buff) Hawkeye (dex/per buff) Longstride (run speed buff) Missing Link (coo/per buff) Wildheart (sta regen)");
      break;
    case 6:
      prepare_hint("Sonic Influence");
      hint_text("Destruction");
      hint_add("The character can control sound waves, reducing or increasing sound.  Sonic Manipulation has many facets including buffs, crowd control, and damage abilities.");
      hint_add("Favored by the Travelers and Vistas.");
      hint_add("");
      hint_add_stat("Stats", "Perception 75% Willpower 25%");
      hint_add_ability("Abilities", "Cat. Dissonance (AOE damage &amp; stun) Dev. Shock (AOE armour debuff) Equilibrium (snare &amp; coo/per debuff) Insulation (armour buff) Rending Vibration (DOT) Sonic Lance (crushing damage)");
      break;
    case 7:
      prepare_hint("Suppression");
      hint_text("Destruction");
      hint_add("Suppression allows a character to drain bio-energy from other mutations, remove mutation buffs, and increase his/her own resistance to mutations.");
      hint_add("Favored by the Enforcers.");
      hint_add("");
      hint_add_stat("Stats", "Perception 75% Willpower 25%");
      hint_add_ability("Abilities", "Convert energy (gamma to hp/sta syphon) Denial (state cure) Energy Blitz (AOE damage hp/sta) Equaliser (stat debuff) Sap Stamina (sta syphon) Syphon Energy (gamma syphon)");
      break;
    case 8:
      prepare_hint("Telekinesis");
      hint_text("Augmentation");
      hint_add("The user creates kinetic energy with his or her mind and directs it at an enemy for damage and even occasional debuffs.");
      hint_add("Favored by the Lightbearers.");
      hint_add("");
      hint_add_stat("Stats", "Intelligence 75% Willpower 25%");
      hint_add_ability("Abilities", "Alacrity (run speed buff) Ambient Absorb (gamma regen) Forced Restraint (root &amp; dex debuff) Kinetic Curtain (team armour buff) Kinetic Shield (armour buff) Repel (AOE KD/crush damage)");
      break;
    case 9:
      prepare_hint("Telepathy");
      hint_text("Restoration");
      hint_add("The character can affect the minds of others, such as distracting opponents or making himself seem more likable.  Telepathy is primarily a crowd-control and self-buffing mutation path.");
      hint_add("Favored by the Lightbearers and the Travelers.");
      hint_add("");
      hint_add_stat("Stats", "Charisma 75% Willpower 25%");
      hint_add_ability("Abilities", "Accelerated Recovery (HOT) Ignore Pain (HP buff) Mind Over Matter (team HOT aura) One Mind,... (AOE element/state cure) Psionic Shock (damage &amp; stun) Renew Consc. (resurrection)");
      break;
    case 10:
      prepare_hint("Thermal Control");
      hint_text("Destruction");
      hint_add("The player can control the movement of molecules, increasing or decreasing heat in an area.  Thermal Manipulation is primarily a damage, Damage over Time (DoT), and self-buff mutation path.");
      hint_add("Favored by the CHOTA and the Techs.");
      hint_add("");
      hint_add_stat("Stats", "Perception 75% Willpower 25%");
      hint_add_ability("Abilities", "Cold Snap (snare &amp; coo debuff) Hypothermia (Cold DOT) Lighter Fluid (fire debuff) Molotov Mut. (fire damage &amp; DOT) Regen. Fever (Element/state cure) Scorch. Rebuke (fire dam melee shield)");
      break;
    default:
      break;
  }
}

function mutation_popup_close( )
{
  document.getElementById("mutation_popup").style.visibility = "hidden";
}
function show_muation_popup(id)
{
  if (id == 0)
  {
    mutation_popup_close( );
    return;
  }
  
  var mutations = ["Alpha", "Empathic", "Enhancement", "Nano-Manipulation", "Patho-Transmission", "Primal", "Sonic Influence", 
                    "Suppression", "Telekinesis", "Telepathy", "Thermal Control" ];

  document.getElementById("mutation_popup").style.visibility = "visible";
  document.getElementById("mutation_header").innerHTML = mutations[id] + " Mutation Abilities";
  
  var points = document.getElementById("mu_actual" + id).value;
  var pcol = document.getElementById("mu_actual" + id).style.backgroundColor;

  var abilities = [
      ["Bolster", "Gird", "Disrupt", "Patch", "Quicken", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;"],
      ["Priority", "Benevolence", "Empower", "Restoration", "Preservation", "Sympathy Pains", "&nbsp;", "&nbsp;", "&nbsp;"],
      ["Reinforce", "Calibration", "Ablate", "Regenerate", "Resilience", "Dissolve", "&nbsp;", "&nbsp;", "&nbsp;"],
      ["Self Repair", "Revitalise", "Vital Osmosis", "Reconstruction", "Filtration", "Cannibalise", "&nbsp;", "&nbsp;", "&nbsp;"],
      ["Sapping Sickness", "Patient Zero", "Wracking Pains", "Shaking Plague", "Debilitating Weakness", "Antibody", "&nbsp;", "&nbsp;", "&nbsp;"],
      ["Missing Link", "Longstride", "Beast Might", "Hawkeye", "Feral Grip", "Wildheart", "&nbsp;", "&nbsp;", "&nbsp;"],
      ["Rending Vibration", "Catastrophic Dissonance", "Equilibrium Shock", "Sonic Lance", "Devestating Shockwave", "Insulation", "&nbsp;", "&nbsp;", "&nbsp;"],
      ["Siphon Energy", "Equalizer", "Sap Stamina", "Denial", "Energy Blitz", "Convert Energy", "&nbsp;", "&nbsp;", "&nbsp;"],
      ["Kinetic Shield", "Repel", "Alacrity", "Kinetic Curtain", "Ambient Absorbtion", "Forced Restraint", "&nbsp;", "&nbsp;", "&nbsp;"],
      ["Accelerated Recovery", "Mind Over Matter", "Ignore Pain", "One Mind, No Affliction", "Renew Consciousness", "Psionic Shock", "&nbsp;", "&nbsp;", "&nbsp;"],
      ["Hypothermia", "Molotov Mutation", "Scorching Rebuke", "Lighter Fluid", "Cold Snap", "Regenerative Fever", "&nbsp;", "&nbsp;", "&nbsp;"] ];
  var levels = ["45 63 81 99 117 135 153", "48 66 84 102 120 138 156", "51 69 87 105 123 141 156", "54 72 90 108 126 144 162", "57 75 93 111 129 147 164", "60 78 96 114 132 150", "", "", "", ""];
  
  for (i = 0; i < 9; i++)
  {
    document.getElementById("m" + i).innerHTML = abilities[id][i];
    
    ranks = levels[i].split(" ");

    current_rank = 0;
    next_rank = 0;

    for (j = 0; j < ranks.length; j++)
    {
      if (points >= parseInt(ranks[j]))
      {
        current_rank = j + 1;
      }    
      else
      {
        if (next_rank == 0)
        {
          next_rank = ranks[j];
        }
      }
      
      if (abilities[id][i] != "&nbsp;")
      {
        if (current_rank == 0)
        {
          document.getElementById("m1" + i).innerHTML = '<span class="mutation_popup_not">' + current_rank + '</span>';
        }
        else
        {
          document.getElementById("m1" + i).innerHTML = '<span class="mutation_popup_got">' + current_rank + '</span>';
        }
        
        if (next_rank == 0)
        {
          document.getElementById("m2" + i).innerHTML = "?";
        }
        else
        {
          document.getElementById("m2" + i).innerHTML = next_rank;
        }
      }
      else
      {
        document.getElementById("m1" + i).innerHTML = "&nbsp;";
        document.getElementById("m2" + i).innerHTML = "&nbsp;";
      }
    } // for j
  } // for i
  
  if (pcol == "rgb(255, 0, 0)")
  {
    document.getElementById("mutation_footer").innerHTML = 'Current Points: <span class="mutation_current_points_bad">' + points + '</span>';
  }
  else
  {
    document.getElementById("mutation_footer").innerHTML = 'Current Points: <span class="mutation_current_points_good">' + points + '</span>';
  }
}
function highlight_mutation(i)
{
  document.getElementById("mutation" + i).select( );
}