// statblock.js

// 3EProfiler (tm) character sheet source file.
// Copyright (C) 2007 Myth-Weavers Games.

// **

// Generates a statblock with one click of ease.

/* The Statblock format is as below:
Portrait Name LinkToCharacterSheet
Gender Alignment Race Class Level, Init +n, HP n/n, DR n/n
AC n (AC anaylsis here), Touch n, Flat-footed n, Fort +n, Ref +n, Will +n
Spec Weapon1 +n (dmg crit), Spec Weapon2 +n (dmg crit), Spec Weapon3 +n (dmg crit) Base Atk +n
Abilities Str n, Dex n, Con n, Int n, Wis n, Cha n
Condition n
*/

// void GenerateStatblock()
// Fill the statblock field with automatically parsed stats.
function GenerateStatblock() {
	if (!confirm("Generate Statblock will replace your current Statblock textfield with information from your character sheet.\n\nAre you sure to continue?"))
    return;
	
	// Declares optional elements in the Statblock
	var pic = "";
	var dr = "";
	var armor = "";
	var shield = "";
	var comma = "";
	var ac = "";
	var actype = "";
	var w1 = "";
	var w2 = "";
	var w3 = "";
	var w1n = "";
	var w2n = "";
	var w3n = "";
	
	var finalStat = ""; // Output String.
	var statArray = new Array(); // Array of Information.
	statArray = [
		"PicURL", // This field is hidden on the sheet.
		"Name",					 // 1
		"SheetURL", // This is defined by a string separately.
		"Gender",
		"Alignment",
		"Race", 				 // 5
		"Class",
		"Level",
		"Init",
		"HPWounds",
		"HP", 					 // 10
		"DamageRed",
		"ArmorName",
		"ShieldName",
		"AC",
		"ACArmor",				 // 15
		"ACShield",
		"ACDex",
		"ACSize",
		"ACDeflect",
		"ACNat",				 // 20
		"ACMisc",
		"Fort",
		"Reflex",
		"Will",
		"Weapon1Special",		 // 25
		"Weapon1",
		"Weapon1AB",
		"Weapon1Damage",
		"Weapon1Crit",
		"Weapon2Special",		 // 30
		"Weapon2",
		"Weapon2AB",
		"Weapon2Damage",
		"Weapon2Crit",
		"Weapon3Special",		 // 35
		"Weapon3",
		"Weapon3AB",
		"Weapon3Damage",
		"Weapon3Crit",
		"MABBase",				 // 40
		"Str",
		"Dex",
		"Con",
		"Int",
		"Wis",					 // 45
		"Cha",
		"text1", // This is Condition and Effects.
		"ArmorSpecial",
		"ShieldSpecial",
		"StrTemp",				 // 50
		"DexTemp",
		"ConTemp",
		"IntTemp",
		"WisTemp",
		"ChaTemp",				 // 55
		"PowerPoints",
		"ActionPoints",
		"text2", 				 // This is Additional Info on SB.
		"Speed",
		"Weapon1Ammo",			 // 60
		"Weapon2Ammo",
		"Weapon3Ammo",
		"ACTouch",
		"ACFlat",
		"PowerPointsMax" ]		 // 65	 
	
	statArray[2] = "http://www.myth-weavers.com/sheetview.php?sheetid=" + String(sheet().id.value); // Get the URL link of the character sheet.
	
	var i = 0;
	
	for (i in statArray) { // Parse each stat into the array.
		if (i == 2) continue; // Already done.
		
		// Fill in blank if empty, else assign the value into the array.
		eval("if (sheet()." + statArray[i] + ".value == '') statArray[i] = ' '; else statArray[i] = String(sheet()." + statArray[i] + ".value);");
	}
	
	// Distribute the information into pieces. Checking for empty values. Refine the information and unify.
	if (statArray[0] == " ") // Parsed but not used.
		pic = "";
	else
		pic = "[IMG]" + statArray[0] + "[/IMG] ";
		
	if (statArray[9] == " ") // HP
		statArray[9] = statArray[10];
		
	if (statArray[11] == " ") // DR
		dr = "";
	else
		dr = ", [B]DR[/B] " + statArray[11];
		
	if (statArray[48] == " " && statArray[12] == " ") // Armor
		armor = "";
	else
		armor = "[B]" + statArray[48] + " " + statArray[12] + "[/B]";
		
	if (statArray[49] == " " && statArray[13] == " ") // Shield
		shield = "";
	else
		shield = "[B]" + statArray[49] + " " + statArray[13] + "[/B]";
		
	if (armor != "" && shield != "") // The comma between armor and shield
		comma = ", ";
	else
		comma = "";
		
	for (var j = 15; j <= 21; j++) { // Checking which bonuses were used for the AC to display.
		if (statArray[j] == "0" || statArray[j] == " ") continue;
		
		switch (j) {
		case 15:
			actype = "Armor";
			break
		case 16:
			actype = "Shield";
			break
		case 17:
			actype = "Dex";
			break
		case 18:
			actype = "Size";
			break
		case 19:
			actype = "Natural";
			break
		case 20:
			actype = "Deflect";
			break
		default:
			actype = "Misc";
		}
		
		if (ac == "")
			ac = Add(statArray[j]) + " " + actype;
		else
			ac = ac + ", " + Add(statArray[j]) + " " + actype;
		
	}
	
	if (ac != "")
		ac = " (" + ac + ")";
		
	if (statArray[60] != " ") // Ammos
		statArray[60] = " (" + statArray[60] + ") ";
		
	if (statArray[61] != " ")
		statArray[61] = " (" + statArray[61] + ") ";
		
	if (statArray[62] != " ")
		statArray[62] = " (" + statArray[62] + ") ";
		
	w1n = String(statArray[25] + " " + statArray[26] + statArray[60]);
	w2n = String(statArray[30] + " " + statArray[31] + statArray[61]);
	w3n = String(statArray[35] + " " + statArray[36] + statArray[62]);
		
	// Weapons
	if (statArray[26] == " ")
		w1 = "";
	else {
		w1 = "[B]" + w1n + "[/B] " + statArray[27] + " (" + statArray[28] + ", " + statArray[29] + ")\n";
	}
		
	if (statArray[31] == " ")
		w2 = "";
	else {
		w2 = "[B]" + w2n + "[/B] " + statArray[32] + " (" + statArray[33] + ", " + statArray[34] + ")\n";
	}
		
	if (statArray[36] == " ")
		w3 = "";
	else {
		w3 = "[B]" + w3n + "[/B] " + statArray[37] + " (" + statArray[38] + ", " + statArray[39] + ")\n";
	}
		
	// Fill in None for empty condition.
	if (statArray[47] == " ")
		statArray[47] = "None";
		
	// Check if Temp scores were entered in the ability arrays.
	for (var j = 50; j <=55; j++) {
		if (statArray[j] == " ") continue;
		else
			statArray[(j-9)] = statArray[j];
	}
	
	if (statArray[56] != " ")
		statArray[56] = " [B]Power Points[/B] " + statArray[56];
	
	if (statArray[57] != " ")
		statArray[57] = " [B]Action Points[/B] " + statArray[57];
		
	if (statArray[56] != " " || statArray[57] != " ")
		statArray[56] = "," + statArray[56];
	
	if (statArray[58] != " ")
		statArray[58] = "\n" + statArray[58];
		
	if (statArray[63] != " ")
		statArray[63] = ", [B]Touch[/B] " + statArray[63];
	else
		statArray[63] = "";
		
	if (statArray[64] != " ")
		statArray[64] = ", [B]Flat-footed[/B] " + statArray[64];
	else
		statArray[64] = "";
		
	if (statArray[65] != " ")
		statArray[65] = "/" + statArray[65];
	
	// Construct the Statblock
	finalStat = String(
	"[URL=" + statArray[2] + "][B][SIZE=+1]" + statArray[1] + "[/SIZE][/B][/URL]\n" +
	statArray[3] + " " + statArray[4] + " " + statArray[5] + " " + statArray[6] + ", [B]Level[/B] " + statArray[7] + ", [B]Init[/B] " + statArray[8] + ", [B]HP[/B] " + statArray[9] + "/" + statArray[10] + dr + ", [B]Speed[/B] " + statArray[59] + "\n" +
	"[B]AC[/B] " +  statArray[14] + statArray[63] + statArray[64] + ", [B]Fort[/B] " + statArray[22] +", [B]Ref[/B] " + statArray[23] + ", [B]Will[/B] " + statArray[24] + ", [B]Base Attack Bonus[/B] " + statArray[40] + statArray[56] + statArray[65] + statArray[57] + "\n" +
	w1 +
	w2 +
	w3 +
	armor + comma + shield + ac + "\n" +
	"[B]Abilities[/B] Str " + statArray[41] + ", Dex " + statArray[42] + ", Con " + statArray[43] + ", Int " + statArray[44] + ", Wis " + statArray[45] + ", Cha " + statArray[46] + "\n" +
	"[B]Condition[/B] " + statArray[47] +
	statArray[58]);
	
	//Outputs the Statblock
	sheet().statsummary.value = finalStat;
}
