// User is selecting a faction:
//   Hide skills/tradeskills/mutations of enemy factions
//   Show skills/tradeskills/mutations of allies
//   Highlight skills/tradeskills/mutations of faction chosen
function select_faction(id)
{
  var f, s, v;
  
  for (f = 0; f < 6; f++)
  {
    v = "#206020";
    if ((f > (parseInt(id) + 1)) || ( f < (parseInt(id) - 1)))
    {
      if (!(((f == 0) && (id == 5)) || ((f == 5) && (id == 0))))
      { 
        v = "#802020";
      }
    } // else visible
    if (f == id) v = "#00A000";
  
    for (s = 0; s < 9; s++)
    {
      document.getElementById("skt" + f + s).style.color = v;
      document.getElementById("trt" + f + s).style.color = v;
    } // for i
    for (s = 0; s < 11; s++)
    {
      document.getElementById("mut" + f + s).style.color = v;
    } // for i
  } // for f
  
  set_faction_hint(id);
  
  calc_ap( ); // Spend AP
}
// Clear faction selection
function clearfaction( )
{
  for (i = 0; i < document.forms[0].faction.length; i++)
  {
    document.forms[0].faction[i].checked = false;
  } // for i
  
  var f, s;
  
  for (f = 0; f < 6; f++)
  {
    for (s = 0; s < 9; s++)
    {
      document.getElementById("skt" + f + s).style.color = "#808080";
      document.getElementById("trt" + f + s).style.color = "#808080";
    } // for i
    for (s = 0; s < 11; s++)
    {
      document.getElementById("mut" + f + s).style.color = "#808080";
    } // for i
  } // for f
  
  set_faction_hint(6);
  
  calc_ap( ); // Spend AP
}
// Change edit box colours based on faction availability
function update_faction_colours( )
{
  var faction_skills = [
      [1,1,1,0,0,1,0,0,0],
      [0,1,1,0,0,0,1,0,1],
      [1,0,0,1,0,0,1,0,0],
      [1,0,1,0,1,0,1,1,0],
      [0,1,1,1,0,1,0,0,0],
      [0,1,1,0,1,0,0,1,0] ]; 
      
  var fa = 9;
  var i, c;
  
  for (i = 0; i < document.forms[0].faction.length; i++)
  {
    if (document.forms[0].faction[i].checked)
    {
      fa = i;
    }
  } // for i
  
  var l1, r1;
  for (i = 0; i < 9; i++)
  {
    c = "#602020"; // Default to unavilable
   
    if (fa == 9)
    {
      c = "#FFFFFF"; // No faction selected
    }
    else // faction selected
    {
      if (faction_skills[fa][i] > 0) // Own faction has skill
      {
        c = "#00A000";
      }
      else
      {
        l1 = fa - 1;
        if (l1 < 0) l1 = 5;
        r1 = fa + 1;  
        if (r1 > 5) r1 = 0;
        
        if (faction_skills[l1][i] > 0) // Ally faction has skill
        {
          c = "#206020";
        }
        if (faction_skills[r1][i] > 0) // Ally faction has skill
        {
          c = "#206020";
        }
      }
    }

    if (c == "#206020") document.getElementById("skill" + i).className  = "faction_ally_got";
    if (c == "#FFFFFF") document.getElementById("skill" + i).className  = "faction_not_selected";
    if (c == "#00A000") document.getElementById("skill" + i).className  = "faction_got";
    if (c == "#602020") document.getElementById("skill" + i).className  = "faction_not_got";
  } // for i
  
  var faction_mutations = [
      [0,0,1,0,1,1,0,0,0,0,1],
      [0,0,0,0,0,0,1,0,0,1,0],
      [0,0,1,1,0,0,0,0,0,0,1],
      [0,0,0,0,0,0,0,1,0,0,0],
      [0,1,0,0,0,0,0,0,1,1,0],
      [0,0,0,0,0,1,1,0,0,0,0] ];  
  
  var l1, r1;
  for (i = 1; i < 11; i++)
  {
    c = "#602020"; // Default to unavilable
   
    if (fa == 9)
    {
      c = "#FFFFFF"; // No faction selected
    }
    else // faction selected
    {
      if (faction_mutations[fa][i] > 0) // Own faction has skill
      {
        c = "#00A000";
      }
      else
      {
        l1 = fa - 1;
        if (l1 < 0) l1 = 5;
        r1 = fa + 1;  
        if (r1 > 5) r1 = 0;
        
        if (faction_mutations[l1][i] > 0) // Own faction has skill
        {
          c = "#206020";
        }
        if (faction_mutations[r1][i] > 0) // Own faction has skill
        {
          c = "#206020";
        }
      }
    }
    
    if (c == "#206020") document.getElementById("mutation" + i).className  = "faction_ally_got";
    if (c == "#FFFFFF") document.getElementById("mutation" + i).className  = "faction_not_selected";
    if (c == "#00A000") document.getElementById("mutation" + i).className  = "faction_got";
    if (c == "#602020") document.getElementById("mutation" + i).className  = "faction_not_got";
  } // for i
  
  var faction_tradeskills = [
      [1,0,0,0,0,1,0,1,0],
      [0,0,0,0,1,0,1,0,1],
      [1,1,0,1,0,1,0,0,1],
      [1,1,0,0,0,0,0,0,1],
      [0,0,0,0,1,1,0,1,0],
      [0,1,1,0,0,0,1,0,1] ];
     
  var l1, r1;
  for (i = 0; i < 9; i++)
  {
    c = "#602020"; // Default to unavilable
   
    if (fa == 9)
    {
      c = "#FFFFFF"; // No faction selected
    }
    else // faction selected
    {
      if (faction_tradeskills[fa][i] > 0) // Own faction has skill
      {
        c = "#00A000";
      }
      else
      {
        l1 = fa - 1;
        if (l1 < 0) l1 = 5;
        r1 = fa + 1;  
        if (r1 > 5) r1 = 0;
        
        if (faction_tradeskills[l1][i] > 0) // Own faction has skill
        {
          c = "#206020";
        }
        if (faction_tradeskills[r1][i] > 0) // Own faction has skill
        {
          c = "#206020";
        }
      }
    }
    
    if (c == "#206020") document.getElementById("tradeskill" + i).className  = "faction_ally_got";
    if (c == "#FFFFFF") document.getElementById("tradeskill" + i).className  = "faction_not_selected";
    if (c == "#00A000") document.getElementById("tradeskill" + i).className  = "faction_got";
    if (c == "#602020") document.getElementById("tradeskill" + i).className  = "faction_not_got";
  } // for i
}
// Display the hint text for the selected faction
function set_faction_hint(id)
{
  mutation_popup_close( );

  switch(id)
  {
    case 0:
      prepare_hint("CHOTA");
      hint_text("The CHOTA's main strengths are their large numbers, mutations, and ferocity. However, because of their fractured society, they rarely use these advantages. As a result of their extensive exposure to the Shiva Virus and radiation, there are many mutants among their ranks. Whereas the CHOTA are not well-armed (they rely on scavenged weapons and armor), they are very powerful fighters and can be frighteningly effective in combat.");
      hint_add("");
      hint_add("The CHOTA are allied with the Vista and Travelers, are enemies with the Techs and Lightbearers, and rivals to the organized Enforcers.");
      hint_add("");
      hint_add_stat("Motto", "Civilization destroyed humanity, so the Children will destroy Civilization.");
      hint_add("");  
      hint_add('<span style="color: #808000">Known Faction Abilities</span>');  
      hint_add("Armour Use - Pain Sponge - Armour, Dodge &amp; Melee Buff");
      hint_add("Athletics - Fitness - Coo, Dex, End &amp; Str Buff");
      hint_add("Melee - Brutalise - Damage, Armour Debuff &amp; KB");
      break;
    case 1:
      prepare_hint("Travelers");
      hint_text("Enjoying a rather casual moral attitude, the Travelers are a loose affiliation of 'families' that have their fingers poked into a variety of pies: performance troupes, gambling, information brokering, smuggling, narcotics, kneecapping, and even assassinations. It's all about business, even if it sometimes gets personal.");
      hint_add("");
      hint_add("Travelers are friendly with the Tech and CHOTA, enemies with the Enforcers and Vista, and rivals with the ritualistic Lightbearers.");
      hint_add("");
      hint_add_stat("Motto", "Commerce will keep humanity alive, especially if we get the profit.");
      hint_add("");  
      hint_add('<span style="color: #808000">Known Faction Abilities</span>');  
      hint_add("Pistols - Gun to a Knife Fight - Pistol Skill &amp; Saves Buff");
      break;
    case 2:
      prepare_hint("Techs");
      hint_text("The Techs have the strongest technological and industrial base of the factions. Owning the largest crafting facilities in the Province, Techs can make advanced items, armor, and firearms in larger amounts than any group. While they are capable of defending themselves, they have comparably less experience in combat, survival, and diplomacy than the other factions have. They tend to rely on their allies for protection, though some of them have learned to fight.");
      hint_add("");
      hint_add("Techs are allied with the Enforcers and Travelers, are enemies with the Lightbearers and Children of the Apocalypse, and arch rivals to the technology-hating Vista.");
      hint_add("");
      hint_add_stat("Motto", "Advancing science by any means necessary.");
      hint_add("");  
      hint_add('<span style="color: #808000">Known Faction Abilities</span>');  
      hint_add("Armour Use - Efficiency &amp; Equilibrium - Physical Armour &amp; Speed Buff");
      hint_add("Pistols - Perforate - Ballistic Damage, Stamina DOT");
      hint_add("Pistols - Smoking Sabot Round - Perception Debuff, Fire DOT");
      break;
    case 3:
      prepare_hint("Enforcers");
      hint_text("The only hope for this world is the law and order provided by the Enforcers. That, at least, is the theory, but there are many who do not share the vision of the world's rebirth that the Enforcers claim to embody. The faction was formed from the remnants of military units, local law enforcement, and even park rangers, but now they recruit anyone who believes that the Enforcer way is the only way to restore what has been lost.");
      hint_add("");
      hint_add("Enforcers are allied with the Techs and Lightbearers, are enemies with the Vista and Travelers, and rivals to the chaotic Children of the Apocalypse.");
      hint_add("");
      hint_add_stat("Motto", "Law and order must be maintained at all costs. ");
      hint_add("");  
      hint_add('<span style="color: #808000">Known Faction Abilities</span>');  
      hint_add("Armour Use - Evasive Maneuvers - Dodge Buff");
      hint_add("Dodge - Smoke Screen - Dodge Buff");
      hint_add("Group Tactics - Inspiration - Dodge, Weapons &amp; Saves Buff");
      hint_add("Group Tactics - Lead The Charge - Dodge, Speed &amp; Threat Buff");
      hint_add("Rifles - Rifle Smash - Speed Debuff, Damage &amp; Stun");
      break;
    case 4:
      prepare_hint("Lightbearers");
      hint_text("The Lightbearers' mission to restore health and safety to humanity gathers many to their banner. Doctors have brought their knowledge of the healing arts and the tattered remains of the Hippocratic Oath to the group. Martial artists who have chosen to use their abilities to help and protect others have become the group's warriors and defenders. Spiritual students have put a worthy philosophy into practice through the good works of Shakti's followers.");
      hint_add("");
      hint_add("Lightbearers are allies with the Enforcers and Vista, enemies with Tech and CHOTA, and rivals with the greedy Travelers.");
      hint_add("");
      hint_add_stat("Motto", "Spiritual development and mutations are the path to enlightenment.");
      hint_add("");  
      hint_add('<span style="color: #808000">Known Faction Abilities</span>');  
      hint_add("Dodge - Autumn Leaves - Dodge &amp; Speed Buff");
      hint_add("First Aid - Sand and Stone - Armour &amp; HP Buff");
      hint_add("Melee - Knife Hand Strike - Dodge Debuff &amp; Stun");
      break;
    case 5:
      prepare_hint("Vistas");
      hint_text("The Vistas are not merely guerrilla warriors, but farmers, ranchers, and craftsmen. They produce most of the food consumed in the Province, knowing more about agriculture than any other faction. They know what plants can be used to heal and what animals produce the most virulent toxins. Instead of technology, they rely on nature - something that causes many of their enemies to underestimate their capabilities.");
      hint_add("");
      hint_add("Vista are allies with the CHOTA and Lightbearers, enemies with the Travelers and Enforcers, and rivals to the technology-oriented Tech.");
      hint_add("");
      hint_add_stat("Motto", "We must preserve and protect our environment.");
      hint_add("");  
      hint_add('<span style="color: #808000">Known Faction Abilities</span>');  
      hint_add("Athletics - Survivor - Secondary Armour Buff");
      hint_add("Rifles - One Shot, One Kill - Damage &amp; HP/STA DOT");
      break;
    case 6:
      prepare_hint("Factions");
      hint_text("The basic faction system functions as a wheel. Each faction has two allied factions, one archenemy faction, and two enemy factions. For example, the Enforcers are allied with the Techs and Lightbearers. Their archenemies are the CHOTA (a.k.a., the Children of the Apocalypse), and their enemies are the Vistas and Travelers.");
      hint_add("");
      hint_add("As you gain standing with a faction, new missions, ranks, non-player characters, and restricted Knowledges become available. When you complete a mission that is a significant help to your faction, such as aiding in the capture of a conflict town or delivering important supplies, you gain Faction Points with your faction and allies, but you lose points with your enemies and archenemy faction.");
      break;
    default:
      break;
  }
}
