
function calculate() {
	// Retrieves the unit name from the dropdown box
	var unitName = document.getElementById("unitName").value;
	unitName = formatUnitName(unitName);
	// Retrieves the attack power of the attacking unit
	var unitAttack = Unit[unitName].baseAttack;
	// Retrieves the number of units attacking
	var quantity = document.getElementById("quantity").value;
	//Retrieves the terrain type
	var terrainName = document.getElementById("terrainName").value;
	// Retrieves the three radio buttons indicating the range
	var elements = document.getElementById("range").elements;
	// Retrives the range selected
	var range;
	for(var i = 0; i < 3; i++) {
		if(elements[i].checked) range = elements[i].value;
	}
	// Calculates the damage percentage given depending on the range
    var value = 1;
	if(range == 4)
		value = 0.75;
	else if(range == 5)
		value = 0.50;
	else if(range == 6)
		value = 0.25;
	// Changes the innerHTML
	var table = document.getElementsByTagName("td");
	for(var i = 0; i < table.length; i += 2) {
		//table[i] = <td>unitName</td>
		//table[i+i] = <td>unitsDrestroyed</td>
		var foeUnitName = formatUnitName(table[i].innerHTML);
		var currentUnit = Unit[foeUnitName];
		var attackBonus = checkAttackBonus(unitName, currentUnit.name.toLowerCase(), terrainName, currentUnit.level, currentUnit.category, currentUnit.type);
		var defenseBonus = checkDefenseBonus(unitName, currentUnit.name.toLowerCase(), terrainName, currentUnit.level, currentUnit.category, currentUnit.type);
		table[i+1].innerHTML = Math.floor((unitAttack + attackBonus) * quantity / (currentUnit.baseDefense + defenseBonus) * value);
	}
}

function checkAttackBonus(unitName, foeName, terrainName, foeLevelName, foeCategoryName, foeTypeName) {
	var value = 0;
	var bonus = Unit[unitName].attackTargets[terrainName];
	if(bonus != null)
		value += bonus;
	bonus = Unit[unitName].attackTargets[foeLevelName];
	if(bonus != null)
		value += bonus;
	bonus = Unit[unitName].attackTargets[foeCategoryName];
	if(bonus != null)
		value += bonus;
	bonus = Unit[unitName].attackTargets[foeTypeName];
	if(bonus != null)
		value += bonus;
	return value;
}

function checkDefenseBonus(unitName, foeName, terrainName, foeLevelName, foeCategoryName, foeTypeName) {
	var value = 0;
	var bonus = Unit[foeName].defenseTargets[terrainName];
	if(bonus != null)
		value += bonus;
	bonus = Unit[foeName].defenseTargets[foeLevelName];
	if(bonus != null)
		value += bonus;
	bonus = Unit[foeName].defenseTargets[foeCategoryName];
	if(bonus != null)
		value += bonus;
	bonus = Unit[unitName].defenseTargets[foeTypeName];
	if(bonus != null)
		value += bonus;
	return value;
}

function removeSpaces(unitName) {
	var index = unitName.split(" ");
	var result = index[0];
	for(i = 1; i < index.length; i++) {
		if(index[i] != null)
			result += index[i];
	}
	return result;
}

function formatUnitName(name) {
	var newName = name.toLowerCase();
	newName = removeSpaces(newName);
	if(newName == "praetorian")
		newName = "pretorian";
	return newName;
}

