
window.onload = init;

/* Cultures and IDs
 */
var cultures = {
                              "c1" : "Lirian",
                              "c2" : "Ogwehoweh",
                              "c3" : "Jhandihari",
                              "c4" : "Khenti", 
                              "c5" : "Aniyonema",
                              "c6" : "Acrian",
                              "c7" : "Balihu",
                              "c8" : "Dolvanni",
                              "c9" : "Koheczi",
                              "c10" : "Lasai",
                              "c11" : "Na'ren", 
                              "c12" : "Philo",
                              "c13" : "Tasso",
                              "c14" : "Unionist",
                              "c15" : "Venkestra",
                              "c16" : "Xingsol",
                              "c17" : "Yobansa"
                         };

/* Abilities and their IDs
 */
var abilities = {
                              //Lirian abilities
                            "a1" : ["Lirian Military", "Can do foo"],
                            "a2" : ["Lirian Noble", "Can do bar"],
                            "a3" : ["Kriegan Hex", "Can do baz"],
                            "a4" : ["Argelan Heal", "Can do qux"],
                            //Ogwehoweh abilities
                            "a5" : ["Hirokone Visions", "Can do foo"],
                            "a6" : ["Boudisark Berzerker", "Can do bar"],
                            "a7" : ["Kildare Merrymaking", "Can do baz"],   
                            //Jhandihari abilities                                        
                            "a8" : ["Mage Artist", "Can do foo"],
                            "a9" : ["Merchant", "Can do bar"],
                            "a10" : ["Warrior", "Can do baz"],                                           
                            "a11" : ["Scavenger", "Can do foo"],
                            //Khenti
                            "a12" : ["Theurgist", "Can do bar"],
                            "a13" : ["Laborer", "Can do baz"],                                          
                            "a14" : ["Artisan", "Can do foo"],
                            //Aniyonema 
                            "a15" : ["Aninoquisi Visions", "Can do bar"],
                            "a16" : ["Equioni Surge", "Can do baz"],                                           
                            "a17" : ["Unestala Blast", "Can do foo"],
                            //Acrian
                            "a18" : ["Scholarly", "Can do bar"],
                            //Balihu
                            "a19" : ["Defense Kata", "Can do baz"],
                            //Dolvanni                                           
                            "a20" : ["Resist Bind", "Can do foo"],
                            //Koheczi
                            "a21" : ["Totem Spirit", "Can do bar"],
                            //Lasai
                            "a22" : ["Lasai Archer", "Can do baz"], 
                            //Na'ren                                          
                            "a23" : ["Through Search", "Can do foo"],
                            //Philo
                            "a24" : ["Resist Arcane", "Can do bar"],
                            //Tasso
                            "a25" : ["Resist Poison", "Can do baz"],        
                            //Unionist                                   
                            "a26" : ["Shield of Faith", "Can do foo"],
                            //Venkestra
                            "a27" : ["Axe Fighting", "Can do bar"],
                            //Xingsol
                            "a28" : ["Strong Will", "Can do baz"]
                        };
                                       
/* Professions and IDs 
 */
var professions = {
                                     "p1" : ["Alchemist", "Can do foo professionally"],
                                     "p2" : ["Crafter", "Can do bar professionally"],
                                     "p3" : ["Criminal", "Can do baz professionally"],
                                     "p4" : ["Diplomat/Politician", "Can do qux professionally"],
                                     "p5" : ["Etheric Engineer", "Can do foo professionally"],
                                     "p6" : ["Herbalist", "Can do bar professionally"],
                                     "p7" : ["Merchant", "Can do baz professionally"],
                                     "p8" : ["Scholar", "Can do qux professionally"],
                                     "p9" : ["Spirit Talker", "Can do foo professionally"],
                                     "p10" : ["Mechanical Engineer", "Can do bar professionally"]
                                };                        

/* Headers and IDs
 */
var headers = {
                                     "h1" : ["Arcanist", "Heads the list for foo"],
                                     "h2" : ["Archer/Rifleman", "Heads the list for bar"],
                                     "h3" : ["Aristocrat", "Heads the list for baz"],
                                     "h4" : ["Chemist", "Heads the list for bar"],
                                     "h5" : ["Engineer", "Heads the list for baz"],
                                     "h6" : ["Performer", "Heads the list for bar"],
                                     "h7" : ["Physician", "Heads the list for baz"],
                                     "h8" : ["Scout", "Heads the list for bar"],
                                     "h9" : ["Soldier", "Heads the list for baz"],
                                     "h10" : ["Subversive", "Heads the list for bar"],
                                     "h11" : ["Thief", "Heads the list for baz"],
                          };

/* Skills and IDs
 * Skills also have a variable cost per skill
 */
var skills = {
                          "s1" : ["Agony", "", 4],
                          "s2" : ["Air Filter", "", 6],
                          "s3" : ["Arcane Strike", "", 6],
                          "s4" : ["Avoid", "", 1],
                          "s5" : ["Avoidance", "", 3], //Actually 3 for first point, 1 for each additional
                          "s6" : ["Awareness", "", 7],
                          "s7" : ["Basic Lockpicking", "", 4],
                          "s8" : ["Bodyguard", "", 6], //requires inspire
                          "s9" : ["Brute Fighting", "", 5],
                          "s10" : ["Chemical Blade", "", 3],
                          "s11" : ["Chemical Thrower", "", 3],
                          "s12" : ["Cloak Fighting", "", 3],
                          "s13" : ["Combat Armory", "", 5],
                          "s14" : ["Complete Tend", "", 4],
                          "s15" : ["Concussion Grenade", "", 2],
                          "s16" : ["Counting Coup", "", 4],
                          "s17" : ["Courage", "", 1],
                          "s18" : ["Covering Fire", "", 3],
                          "s19" : ["Cower", "", 4],
                          "s20" : ["Crushing Strike", "", 6], //requires brute fighting
                          "s21" : ["Decoy", "", 5],
                          "s22" : ["Defend the Weak", "", 6], //requires fenching
                          "s23" : ["Disarm", "", 6],
                          "s24" : ["Disguise", "", 7],
                          "s25" : ["Dodge", "", 10],
                          "s26" : ["Engineer's Arm", "", 6],
                          "s27" : ["Engineer's Device", "", 8],
                          "s28" : ["Escape Bonds", "", 7],
                          "s29" : ["Etheric Battery", "", 4],//requires etheric field
                          "s30" : ["Etheric Field", "", 8],
                          "s31" : ["Fast Healing", "", 4], //requires slow healing
                          "s32" : ["Fearless March", "", 3],
                          "s33" : ["Fencing", "", 8],
                          "s34" : ["Fire from Cover", "", 5],
                          "s35" : ["Florentine", "", 3],
                          "s36" : ["Gas Bomb", "", 5],
                          "s37" : ["Geomancy", "", 6],
                          "s38" : ["Glory or Death", "", 9],
                          "s39" : ["Grenade Pack", "", 6], //actually 6/grenade type + 3 for each time purchased
                          "s40" : ["Handle Poison", "", 4],
                          "s41" : ["Hearty Ballad", "", 5],
                          "s42" : ["Heroic Recounting", "", 6],
                          "s43" : ["Hobble", "", 5],
                          "s44" : ["Inspire", "", 9],
                          "s45" : ["Knife Fighter", "", 3],
                          "s46" : ["Linguistic Acuity", "", 3],
                          "s47" : ["Mage's Arms", "", 4],
                          "s48" : ["Mage's Dart", "", 4],
                          "s49" : ["Magic Circle", "", 6],
                          "s50" : ["Maim", "", 6],
                          "s51" : ["Mana Pool", "", 4],
                          "s52" : ["March On", "", 6], //requires fearless march
                          "s53" : ["Mass Cast", "", 7], //requires ritual magic
                          "s54" : ["Maximize Armor", "", 8],
                          "s55" : ["Oversight", "", 5], //requires complete tend
                          "s56" : ["Parry", "", 8],
                          "s57" : ["Piercing Shot", "", 4],
                          "s58" : ["Piercing Strike", "", 3],
                          "s59" : ["Purge Poison", "", 4],
                          "s60" : ["Rage Chakra", "", 5],
                          "s61" : ["Repair", "", 6],
                          "s62" : ["Resilient Gear", "", 3],
                          "s63" : ["Resist Poison", "", 5],
                          "s64" : ["Resist Trap", "", 4],
                          "s65" : ["Reveal/Conceal", "", 2],
                          "s66" : ["Ritual Magic", "", 3],
                          "s67" : ["Sabotage", "", 4],
                          "s68" : ["Shield 1", "", 2],
                          "s69" : ["Shield 2", "", 4], //requires shield 1
                          "s70" : ["Siege Artillery", "", 3],
                          "s71" : ["Silence", "", 4],
                          "s72" : ["Simple Traps", "", 3],
                          "s73" : ["Sky Chakra", "", 5],
                          "s74" : ["Slay", "", 7],
                          "s75" : ["Slow Healing", "", 3],
                          "s76" : ["Snipe", "", 7],
                          "s77" : ["Sonic Grenade", "", 4],
                          "s78" : ["Soothe the Beast", "", 3],
                          "s79" : ["Spell Shield", "", 6],
                          "s80" : ["Staff and Dagger", "", 3],
                          "s81" : ["Stun", "", 3],
                          "s82" : ["Summoning", "", 5], //requires ritual magic and being Jhandihari
                          "s83" : ["Surgeon", "", 5],
                          "s84" : ["Tend: Awaken", "", 2],
                          "s85" : ["Tend: Chirugeon", "", 3], //requires tend:awaken
                          "s86" : ["Tend: First Aid", "", 2], //requires tend:chirugeon and tend:restore limb
                          "s87" : ["Tend: Restore Limb", "", 3], //requires tend:awaken
                          "s88" : ["Terrain Advantage", "", 4],
                          "s89" : ["Train", "", 4],
                          "s90" : ["Trip", "", 5],
                          "s91" : ["Two Weapons", "", 4], //requires florentine
                          "s92" : ["Vital Shot", "", 7],
                          "s93" : ["Waylay", "", 8],
                          "s94" : ["Wear Armor", "", 2] //actually 2 for first point, +1 for each point after that
                    }; 
                    
var openSkills = ["s84", "s85", "s87", "s86", "s17", "s4", "s81", "s89"];                                                  

//Mapping of cultures to their available abilities
var cultureToAbility = {
                                             "c1":["a1", "a2", "a3", "a4"],
                                             "c2":["a5", "a6", "a7"],
                                             "c3":["a8", "a9", "a10", "a11"],
                                             "c4":["a12", "a13", "a14"],
                                             "c5":["a15", "16", "17"],
                                             "c6":["a18"],
                                             "c7":["a19"],
                                             "c8":["a20"],
                                             "c9":["a21"],
                                             "c10":["a22"],
                                             "c11":["a23"],
                                             "c12":["a24"],
                                             "c13":["a25"],
                                             "c14":["a26"],
                                             "c15":["a27"],
                                             "c16":["a28"]
                                      };
                                             
//Mapping of cultures to their available professions         
//TODO: Clear up with a director who can do what, for now everyone can do everything                                    
var cultureToProfession = {
                                             "c1":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c2":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c3":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c4":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c5":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c6":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c7":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c8":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c9":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c10":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c11":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c12":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c13":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c14":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c15":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c16":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"],
                                             "c17":["p1", "p3", "p2", "p4", "p5", "p6", "p7", "p8", "p9"]
                                      };
                                      
//Mapping of cultures to their available headers
//TODO go over who has what with a director, until then everyone can do everything
var cultureToHeader = {
                                             "c1":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c2":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c3":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c4":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c5":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c6":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c7":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c8":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c9":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c10":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c11":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c12":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c13":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c14":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c15":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c16":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"],
                                             "c17":["h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h11"]
                                      };
                                      
//Mapping of headers to associated skills                                      
var headerToSkill = {
                                        //Arcanist
                                        "h1":["s66", "s51", "s49", "s82", "s3", "s47", "s48", "s53", "s79", "s37", "s94"],
                                        //Archer/Rifleman
                                        "h2":["s18", "s57", "s92", "s34", "s76", "s40", "s43", "s70", "s94"],
                                        //Aristocrat
                                        "h3":["s22" , "s23", "s46", "s44", "s8", "s94", "s56", "s33", "s58", "s32"],
                                        //Chemist
                                        "h4":["s11", "s10", "s2", "s63", "s68", "s15", "s77", "s36", "s39", "s94"],
                                        //Engineer
                                        "h5":["s70", "s64", "s61", "s62", "s72", "s30", "s29", "s2", "s67", "s26", "s27", "s94"],
                                        //Performer
                                        "h6":["s68", "s56", "s52", "s94", "s41", "s19", "s41", "s32", "s78", "s38", "s16"],
                                        //Physician
                                        "h7":["s14", "s55", "s83", "s59", "s75", "s31", "s60", "s73", "s94"],
                                        //Scout
                                        "h8":["s35", "s72", "s43", "s40", "s80", "s6", "s88", "s90", "s59", "s92", "s94", "s5"],
                                        //Soldier
                                        "h9":["s54", "s50", "s13", "s58", "s20", "s69", "s91", "s9", "s56", "s74", "s94"],
                                        //Subversive 
                                        "h10":["s25", "s28", "s65", "s64", "s24", "s67", "s71", "s46", "s21", "s23", "s5", "s94"],
                                        //Thief
                                        "h11":["s45", "s72", "s93", "s12", "s90", "s25", "s65", "s28", "s64", "s7", "s5"]
                                   };
                                   
var freeformSkills={};

/* Called when the culture select dropdown is changed
 */
function cultureSelectChange()
{
     //get the selected id 
     var selectedID = document.culture.cultureSelect.options[document.culture.cultureSelect.selectedIndex].value;
     
     //create the list of possible cultural abilities
     var abilityList = getList(selectedID, cultureToAbility, abilities);
     
     //update the select element
     generateSelectBox(abilityList, document.culture.cultureAbilitySelect);
     
     //Generate the cultural profession selections
     var professionList = getList(selectedID, cultureToProfession, professions);
     generateSelectBox(professionList, document.profession.allProfessions);
     
     //Generate the header selections
     var headerList = getList(selectedID, cultureToHeader, headers);
     generateSelectBox(headerList, document.culture.allHeaders);
}

function headerSelectChange()
{

     var skillList = new Array();
     //For each header that the user has selected
     for(var ii = 0; ii < document.culture.usedHeaders.length; ii++)
     {
          //Get the list of skills for that header
          var tmpList = getList(document.culture.usedHeaders.options[ii].value, headerToSkill, skills);
          
          //Add the associated skills into the array
          for(var tmpID in tmpList)
          {
               skillList[tmpID] = tmpList[tmpID];
          }
     }
     
     //Add the open skills
     for(var ii = 0; ii < openSkills.length; ii++)
     {
          skillList[openSkills[ii]] = skills[openSkills[ii]][0];
     }
     
     //Update the skill selectbox
     generateSelectBox(skillList, document.skills.allSkills);

}

/* Move all the entries of the select element "source" to 
 * the select element "target". Used to change what headers, 
 * professions, and skills the player has
 */
function selectMove(source, target)
{
     for(var ii = 0; ii < source.options.length; ii++)
     {
          if(source.options[ii].selected)
          {
               //add the option to the target
               target.add(source.options[ii], null);
               //remove the option from the source
               // Not needed, the add moves it (WTF?) source.remove(ii);
           }
      }
}

/* This is the secret sauce. Given an identifer (e.g. culture) and a map (e.g. culture->ability), return a list of 
 * tuples consisting of the identifier and the name
 */
function getList(identifier, map, target)
{
     //get the list of identifiers from the map
     var idList = map[identifier];
    
     /*populate the returned list with the entries from target that match
      * the list of identifers from the map
      */
     var outList = [];
     for(var ii = 0; ii < idList.length; ii++)
     {
          var outID = idList[ii];
          var outName = target[idList[ii]][0];
          outList[outID] = outName;
      }
      
      return outList;
} 

function generateSelectBox(optionArray, selectElement)
{
     //Clear out the old content
     selectElement.options.length = 0;
     
     //for each thing in the array, add it to the select element
     var tmpID = "";
     for ( tmpID in optionArray) 
     { 
          selectElement.add( new Option( optionArray[tmpID], tmpID, false, false) ,null);
     }      
}

function updateAttribute(attribute, action)
{
     //increase
     if(action == "+")
     {
          attribute.value++
          
          //no attribute greater than 10
          if(attribute.value > 10)
          {     
               attribute.value = 10;
               //should disable "up" button here
          }
          else
          {
               //should re-enable "up" button here
          }
     }
     else
     {
          attribute.value--;
          var minimum;
          if(attribute.name == "spirit")
          {
               minimum = 0;
          }
          else
          {
               minimum = 1;
          }
          
          //no attribute less than one
          if(attribute.value < minimum)
          {     
               attribute.value = minimum;
               //should disable "down" button here
          }
          else
          {
               //should re-enable "down" button here
          }
      }
}

function init()
{
     generateSelectBox(cultures, document.culture.cultureSelect);
}

function calcBuildPoints()
{
     var buildPoints = 20;
     
     //add one if culture is one of the major cultures
     
     //Subtract cost of headers
     
     //Subtract cost of professions
     
     //Subtract cost of attributes
     
     //Subtract cost of skills
          
     //Set the build point total
     document.instructions.build.value = buildPoints;
}

function addFreeformHeader()
{
     addFreeformArea("ffHeaderList", "Header");
}

function addFreeformProfession()
{
     addFreeformArea("ffProfessionList", "Profession");
}

function addFreeformSkill()
{
     addFreeformArea("ffSkillList", "Skill");
}

function addFreeformArea(elName, title){
     var target = document.getElementById(elName);

     //Update the count and generate the id
     var countElement = document.getElementById("ff" + title + "Count"); //this is a stupid way to get the id
     var nameStr = "ff" + title + "Area" + countElement.value;
     countElement.value = (countElement.value -1)+ 2; //avoid overload of + as a concatenation operator
     
     alert(nameStr);     
     
     var ffAreaHeader = "\
                         <table cellspacing='5' id='3'>             \
                              <tr valign='bottom'>                       \
                                  <td nowrap='1'>"
                                        + title +
                                   ": </td>                                       \
                                   <td nowrap='1' colspan='2'>      \
                                        Points:                                     \
                                   </td>                                           \
                              </tr>                                                 \
                         ";
     var ffAreaTable = "\
                         <tr valign='bottom' id='2'>                    \
                              <td nowrap='1'>                               \
                                   <input type='text' name=' " 
                                   + title + 
                                   "Name' size='40'/>                        \
                              </td>                                                 \
                              <td nowrap='1'>                                \
                                   <input type='text' name='cost' size='3'/> \
                              </td>                                                  \
                              <td id='1'>                                         \
                                   <input type='button' onClick='removeFreeformArea(this.parentNode.parentNode.parentNode.parentNode.parentNode.id);' value='Remove'/>     \
                              </td>                                                 \
                         </tr>                                                       \
                    </table>                                                      \
                    ";

     var newDiv = document.createElement("div");
     newDiv.setAttribute('id', nameStr);
     if(target.childNodes.length == 0)
     {
          newDiv.innerHTML=ffAreaHeader + ffAreaTable;
     }
     else{
          newDiv.innerHTML="<table cellspacing='5' id='3'>" + ffAreaTable;
     }
     
     target.appendChild(newDiv);
}

function removeFreeformArea(idStr)
{
     var elementToRemove = document.getElementById(idStr);
     elementToRemove.parentNode.removeChild(elementToRemove);
}

