// ogl/class.js

/*
  3EProfiler character sheet source file.
  Copyright (C) 2007 Myth-Weavers Games
  **
*/
// **

// Class information used by 3EProfiler that was released by WotC under
// the Open Gaming License (OGL). This file however, is not released under
// the OGL (see above).

// Defines the order of classes (used in the classSkills object).
var classIndex = {
  fringer:       0x100,
  noble:       0x80,
  scoundrel:   0x40,
  scout:       0x20,
  soldier:       0x10,
  specialist:       0x8,
  adept:       0x4,
  consular:       0x2,
  guardian:       0x1
};

function _getClassBitSet()
{
  // Determine which classes the character has and return the bitset.
  var classes = sheet().Class.value.toLowerCase().split("/");
  var classbits = 0;
  for (var i in classes)
    if (classIndex[classes[i]])
      classbits |= classIndex[classes[i]];
  return classbits;
}


function SaveCalc(save)
{
	var cond = parseInt(sheet()["CondVal"].value);
	
  ZeroFill(
    sheet()[save + "LvorArm"],
    sheet()[save + "CBonus"],
    sheet()[save + "Ability"],
	sheet()[save + "Misc"]);

  sheet()[save].value = Add(
	10,
    sheet()[save + "LvorArm"].value,
    sheet()[save + "CBonus"].value,
    sheet()[save + "Ability"].value,
	sheet()[save + "Misc"].value,
  	cond);
  
  FortChange();
} 


// Event handler for changing the XP Change.
// The function reads the change in XP, then applies it to the current
// XP slot, also verifing if the next level XP should change.
function ApplyXPChange()
{

  // Ensure we have a value to change.
  if (!sheet().XPChange.value.length)
    return;

  // Parse the proper values from the sheet.
  var change = parseInt(sheet().XPChange.value);
  if (isNaN(change))
    return;

  var current = parseInt(sheet().XPCurrent.value);
  if (isNaN(current))
    current = 0;

  var updated = current + change;

  // Set the sheet.
  sheet().XPCurrent.value = updated;
  sheet().XPNext.value = _XPForNextLevel(updated);

}

// Sets the next level XP if the current XP is edited directly.
function ApplyXPNext()
{

  // Parse the proper values from the sheet.
  if (!sheet().XPCurrent.value.length)
    return;

  var updated = parseInt(sheet().XPCurrent.value);
  if (isNaN(updated))
    return;

  // Update the sheet.
  sheet().XPNext.value = _XPForNextLevel(updated);
}

// Calculates the XP for the next level, based on the current XP.
function _XPForNextLevel(currentXP)
{
  // Sanity check.
  if (typeof currentXP != "number")
    return "?";

  var level = 0;
  var nextLevelXP = 0;
  while (currentXP >= nextLevelXP)
    nextLevelXP += level++ * 1000;
  return nextLevelXP;
}

// Updates all level dependencies.
function OnLevelChanged()
{
  // All we need to do is update the max ranks for the skills.


  // Determine the toal level of the character.
  var levels = sheet().Level.value.split("/");
  var totalLevel = 0;
  for (var i in levels)
    totalLevel += isNaN(parseInt(levels[i])) ? 0 : parseInt(levels[i]);
  if (!totalLevel)
    return;

//  sheet().MaxRank.value = totalLevel + 3;
//  sheet().MaxRankCC.value = (totalLevel + 3) / 2;

}


sizeModifiers = {
  c:-8,
  g:-4,
  h:-2,
  l:-1,
  m:0,
  s:1,
  t:2,
  d:4,
  f:8
};

function OnSizeChanged()
{


  // Update the AC inputs.
  sizeMod = Clean(sizeModifiers[sheet().Size.value.charAt(0).toLowerCase()]);
  sheet().ACSize.value = sizeMod;
  ACCalc();

  // Update the BAB inputs.
  sheet().MABSize.value = sizeMod;
  MBABCalc();
  sheet().RABSize.value = sizeMod;
  RBABCalc();
}


// void ACCalc(void)
// Recalculates the ac table.
function ACCalc()
{

  ZeroFill(
    sheet().ACClass,
    sheet().ACDex,
    sheet().ACSize,
    sheet().ACMisc);

  sheet().AC.value = Clean(Add(
    10,
    sheet().ACClass.value,
    sheet().ACDex.value,
    sheet().ACSize.value,
    sheet().ACMisc.value));

}

// void ACChangeArmor(void)
// Copies the data from the armor table to the ac block and recalculates
// the ac.
function ACChangeArmor()
{
  sheet().ACArmor.value = GetNum(sheet().ArmorBonus);
  ACCalc();
}


// void InitCalc(void)
// Calculates the total initiative.
function InitCalc()
{

  ZeroFill(sheet().InitTotalMod);
  
  var slots = document.getElementById("skills").rows.length - 2;
  var j;

	for (var i = 1; i <= slots; i++)
	{
		if (i < 10) {
			    j = "0" + String(i);
		} else {
			    j = i;
	}
	if (sheet()["Skill"+String(j)].value="initiative")	
	sheet().initTotalMod.value = sheet()["Skill"+String(j)+"Mod"].value;
}
}