var Move;
var CounterCharge = false;
var LastInitiative;

function Roll(Max){
	DieRoll = Math.floor((Math.random() * Max));
	DieRoll = DieRoll + 1;
	return DieRoll;
}

function Command(){

	var CmdRoll = Roll(100);
	if (CmdRoll <= 20){ alert("Initiative gives 1 Action. (Add 1 for Exceptional Leader.)"); }
	else if(CmdRoll <= 50){ alert("Initiative gives 2 Actions. (Add 1 for Exceptional Leader.)"); }
	else if(CmdRoll <= 90){ alert("Initiative gives 3 Actions. (Add 1 for Exceptional Leader.)"); }
	else{ alert("Initiative gives 4 Actions. (Add 1 for Exceptional Leader.)"); }

}

function Initiative(){
	var InitRoll = Roll(100);
	if (LastInitiative == "Attacker"){
			InitRoll = InitRoll + 30;
	}
	else if (LastInitiative == "Defender"){
		InitRoll = InitRoll - 30;
	}
	if (InitRoll <= 50){
		alert("Attacker has Initiative.");
		LastInitiatiuve = "Attacker";
	}
	else{

		alert("Defender has Initiative.");
		LastInitiatiuve = "Defender";
	}


}

function Delay() {
	var DelayRoll = Roll(100);
	if (document.getElementById("ActorLeader").checked == true){ DelayRoll = DelayRoll + 10;}
	if (DelayRoll <= 5){
		return "Delayed";
	}
	else if (DelayRoll <=10){
		return "HalfDelayed";
	}
	else{
		return "NoDelay";
	}
}

function Result() {

	var Act = document.getElementById("ActionList").value;

	var Acting = document.getElementById("ActorList").value;
	var Targeted = document.getElementById("TargetList").value;
	var Ground = document.getElementById("CoverList").value;
	var Move = "";

	var DelayResult = Delay();



	if (Act == "Regroup"){
		var RegroupResp = confirm("Does the unit need to regroup?");
		if (RegroupResp == true){
			var RallyRoll = Roll(100);
			var Losses = confirm ("Has the unit lost any bases?");
			if (Losses == true){ RallyRoll = RallyRoll + 10; }
			if (document.getElementById("ActorLeader").checked == true){
				RallyRoll = RallyRoll - 10;
				var Except = confirm("Is the attached leader Exceptional?");
				if (Except == true){
					RallyRoll = RallyRoll - 10;
				}
				}
			if (RallyRoll <= 25){
				alert("The unit rallies, and is no longer Disordered or Demoralized.")
			}
			else if (RallyRoll <= 75){
				alert("The unit rallies, going from Demoralized to Disordered, or from Disordered to OK.");
			}
			else {
				alert("The unit fails to rally.");
			}
		}
		else {
			alert("Unit has rested.");
		}
	}
	else if(Act =="Advance"){
		if (DelayResult == "Delayed"){
			alert("Orders were lost. The unit will stand.");
			return;
		}
		else if (DelayResult == "HalfDelayed") {
			Move = "Orders are delayed. ";
			if (Acting == "HC"){
				Move = Move + "Unit will move 9 inches.";
			}
			if (Acting == "MC"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "LH"){
				Move = Move + "Unit will move 12 inches.";
			}
			else if (Acting == "CT"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "Pikemen"){
				Move = Move + "Unit will move 3 inches.";
			}
			else if (Acting == "HSpears"){
				Move = Move + "Unit will move 3 inches.";
			}
			else if (Acting == "HSwords"){
				Move = Move + "Unit will move 3 inches.";
			}
			else if (Acting == "MSpears"){
				Move = Move + "Unit will move 6 inch.";
			}
			else if (Acting == "MSwords"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "LMI"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "Bowmen"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "LI"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "Levy"){
				Move = Move + "Unit will move 3 inches.";
			}
			else if (Acting == "Artillery"){
				Move = Move + "Unit will move 1 inch.";
			}
			else if (Acting == "EL"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "LCH"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "HCH"){
				Move = Move + "Unit will move 6 inches.";
			}

			alert (Move);
			Move = "";
		}
		else{
			if (Acting == "HC"){
				Move = Move + "Unit will move 12 inches.";
			}
			if (Acting == "MC"){
				Move = Move + "Unit will move 12 inches.";
			}
			else if (Acting == "LH"){
				Move = Move + "Unit will move 18 inches.";
			}
			else if (Acting == "CT"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "Pikemen"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "HSpears"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "HSwords"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "MSpears"){
				Move = Move + "Unit will move 9 inch.";
			}
			else if (Acting == "MSwords"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "LMI"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "Bowmen"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "LI"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "Levy"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "Artillery"){
				Move = Move + "Unit will move 3 inches.";
			}
			else if (Acting == "EL"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "LCH"){
				Move = Move + "Unit will move 12 inches.";
			}
			else if (Acting == "HCH"){
				Move = Move + "Unit will move 9 inches.";
			}

			alert (Move);
			Move = "";		}

	}
	else if(Act =="Retire"){
		var RetireRoll = Roll(100);
		if (DelayResult == "Delayed"){
			alert("Orders were lost. The unit will remain in place and must fight a melee immediately.");
			return;
		}
		else if (DelayResult == "HalfDelayed"){
			RetireRoll = RetireRoll - 10;
		}

		if (document.getElementById("ActorDisordered").checked == true){ RetireRoll = RetireRoll - 10; }
		if (document.getElementById("ActorFlanked").checked == true){ RetireRoll = RetireRoll - 20; }
		if (document.getElementById("ActorLeader").checked == true){
			RetireRoll = RetireRoll + 10;
			var Attached = confirm("Is the attached leader Exceptional?");
				if (Attached == true){
					RetireRoll = RetireRoll + 10;
				}

			}
		if (RetireRoll <= 15){
			Move = "Unit fails to retire, and loses a Status Level. It is locked in combat and must fight a melee immediately.";
		}
		else if (RetireRoll <= 30){
			Move = "Unit is locked in combat and must fight a melee immediately.";
		}
		else{
			if (Acting == "HC"){
				Move = "Unit will break out of combat and fall directly back up to 9 inches, facing the enemy.";
			}
			else if (Acting == "MC"){
				Move = "Unit will break out of combat and fall directly back up to 9 inches, facing the enemy.";
			}
			else if (Acting == "CT"){
				Move = "Unit will break out of combat and fall directly back up to 6 inches, facing the enemy.";
			}
			else if (Acting == "LH"){
				Move = "Unit will break out of combat and fall directly back up to 12 inches, facing the enemy.";
			}
			else if (Acting == "Pikemen"){
				Move = "Unit will break out of combat and fall directly back up to 3 inches, facing the enemy.";
			}
			else if (Acting == "HSpears"){
				Move = "Unit will break out of combat and fall directly back up to 3 inches, facing the enemy.";
			}
			else if (Acting == "HSwords"){
				Move = "Unit will break out of combat and fall directly back up to 3 inches, facing the enemy.";
			}
			else if (Acting == "MSpears"){
				Move = "Unit will break out of combat and fall directly back up to 6 inches, facing the enemy.";
			}
			else if (Acting == "MSwords"){
				Move = "Unit will break out of combat and fall directly back up to 6 inches, facing the enemy.";
			}
			else if (Acting == "LMI"){
				Move = "Unit will break out of combat and fall directly back up to 6 inches, facing the enemy.";
			}
			else if(Acting == "Bowmen"){
				Move = "Unit will break out of combat and fall directly back up to 6 inches, facing the enemy.";
			}
			else if(Acting == "LI"){
				Move = "Unit will break out of combat and fall directly back up to 6 inches, facing the enemy.";
			}
			else if(Acting == "Levy"){
				Move = "Unit will break out of combat and fall directly back up to 3 inches, facing the enemy.";
			}
			else if(Acting == "Artillery"){
				Move = "Unit cannot break out of combat. Please choose a valid order";
			}
			else if(Acting == "EL"){
				Move = "Unit will break out of combat and fall directly back up to 6 inches, facing the enemy.";
			}
			else if(Acting == "LCH"){
				Move = "Unit will break out of combat and fall directly back up to 9 inches, facing the enemy.";
			}
			else if(Acting == "HCH"){
				Move = "Unit will break out of combat and fall directly back up to 6 inches, facing the enemy.";
			}

		}

		alert(Move);

	}
	else if(Act =="March"){
		if (DelayResult == "Delayed"){
			alert("Orders were lost. The unit will stand.");
			return;
		}
		else if (DelayResult == "HalfDelayed") {
			Move = "Orders are delayed. ";
			if (Acting == "HC"){
				Move = Move + "Unit will move 12 inches.";
			}
			if (Acting == "MC"){
				Move = Move + "Unit will move 12 inches.";
			}
			else if (Acting == "LH"){
				Move = Move + "Unit will move 18 inches.";
			}
			else if (Acting == "CT"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "Pikemen"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "HSpears"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "HSwords"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "MSpears"){
				Move = Move + "Unit will move 9 inch.";
			}
			else if (Acting == "MSwords"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "LMI"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "Bowmen"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "LI"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "Levy"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "Artillery"){
				Move = Move + "Unit will move 3 inch.";
			}
			else if (Acting == "EL"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "LCH"){
				Move = Move + "Unit will move 12 inches.";
			}
			else if (Acting == "HCH"){
				Move = Move + "Unit will move 9 inches.";
			}

			alert (Move);
			Move = "";
		}
		else{
			if (Acting == "HC"){
				Move = Move + "Unit will move 24 inches.";
			}
			if (Acting == "MC"){
				Move = Move + "Unit will move 24 inches.";
			}
			else if (Acting == "LH"){
				Move = Move + "Unit will move 36 inches.";
			}
			else if (Acting == "CT"){
				Move = Move + "Unit will move 18 inches.";
			}
			else if (Acting == "Pikemen"){
				Move = Move + "Unit will move 12 inches.";
			}
			else if (Acting == "HSpears"){
				Move = Move + "Unit will move 12 inches.";
			}
			else if (Acting == "HSwords"){
				Move = Move + "Unit will move 12 inches.";
			}
			else if (Acting == "MSpears"){
				Move = Move + "Unit will move 18 inch.";
			}
			else if (Acting == "MSwords"){
				Move = Move + "Unit will move 18 inches.";
			}
			else if (Acting == "LMI"){
				Move = Move + "Unit will move 18 inches.";
			}
			else if (Acting == "Bowmen"){
				Move = Move + "Unit will move 18 inches.";
			}
			else if (Acting == "LI"){
				Move = Move + "Unit will move 18 inches.";
			}
			else if (Acting == "Levy"){
				Move = Move + "Unit will move 12 inches.";
			}
			else if (Acting == "Artillery"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "EL"){
				Move = Move + "Unit will move 18 inches.";
			}
			else if (Acting == "LCH"){
				Move = Move + "Unit will move 24 inches.";
			}
			else if (Acting == "HCH"){
				Move = Move + "Unit will move 18 inches.";
			}

			alert (Move);
			Move = "";		}

	}
	else if(Act =="Charge"){

		Move = "";
		if (Acting == "Artillery"){
			alert("Transport cannot charge. Please choose a valid action.");
			return;
		}
		if (Acting == "LI" && (Targeted != "LI" && Targeted != "Artillery") && Ground == "None" && (document.getElementById("TargetFlanked").checked == false)){
			alert("Skirmishers cannot charge formed troops in the open. Please give a valid order.");
			return;
		}

		// Determine charge movement

		if (DelayResult == "Delayed"){
			alert("Orders were lost. The unit will stand.");
			return;
		}
		else if (DelayResult == "HalfDelayed") {
			Move = "Orders are delayed. ";
			if (Acting == "HC"){
				Move = Move + "Unit will charge 9 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "MC"){
				Move = Move + "Unit will charge 9 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "CT"){
				Move = Move + "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "LH"){
				Move = Move + "Unit will move 12 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "Pikemen"){
				Move = Move + "Unit will charge 3 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "HSpears"){
				Move = Move + "Unit will charge 3 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "HSwords"){
				Move = Move + "Unit will charge 3 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "MSpears"){
				Move = Move + "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "MSwords"){
				Move = Move + "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "Levy"){
				Move = Move + "Unit will charge 3 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "Bowmen"){
				Move = Move + "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "LMI"){
				Move = Move + "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "EL"){
				Move = Move + "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "LCH"){
				Move = Move + "Unit will charge 9 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "HCH"){
				Move = Move + "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "LI"){
				Move = Move + "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}

		}
		else{
			if (Acting == "HC"){
				Move = Move + "Unit will charge 12 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "MC"){
				Move = Move + "Unit will charge 12 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "CT"){
				Move = Move + "Unit will charge 9 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "LH"){
				Move = Move + "Unit will move 18 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "Pikemen"){
				Move = Move + "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "HSpears"){
				Move = Move + "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "HSwords"){
				Move = Move + "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "MSpears"){
				Move = Move + "Unit will charge 9 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "MSwords"){
				Move = Move + "Unit will charge 9 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "Levy"){
				Move = Move + "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "Bowmen"){
				Move = Move + "Unit will charge 9 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "LMI"){
				Move = Move + "Unit will charge 9 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "EL"){
				Move = Move + "Unit will charge 9 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "LCH"){
				Move = Move + "Unit will charge 12 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "HCH"){
				Move = Move + "Unit will charge 9 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "LI"){
				Move = Move + "Unit will charge 9 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}		}
		alert(Move);

		if ((document.getElementById("TargetAggressive").checked == true) && (Targeted == "HSwords" || Targeted == "MSwords" || Targeted == "MSpears" || Targeted == "MC" || Targeted == "HC" || Targeted == "CT" || Targeted == "HCH") && (document.getElementById("TargetFlanked").checked == false)){
			CounterCharge = true;
		}

		else if (Targeted == "LI"){
			var EvadeRoll = Roll(100);
			var EvadeMove;
			if (EvadeRoll <= 30){EvadeMove = 3;}
			else if (EvadeRoll <= 60) {EvadeMove = 6;}
			else {EvadeMove = 9;}
			alert("Light Infantry may move up to " + EvadeMove + " inches to evade the charge. The charging unit may attempt to follow them with any remaining charge movement.");
		}
		else if (Targeted == "HC" || Targeted == "MC"){
			var EvadeRoll = Roll(100);
			var EvadeMove;
			if (EvadeRoll <= 30){EvadeMove = 6;}
			else if (EvadeRoll <= 60) {EvadeMove = 9;}
			else {EvadeMove = 12;}
			alert("Cavalry may move up to " + EvadeMove + " inches to evade the charge. The charging unit may attempt to follow them with any remaining charge movement.");
		}
		else if (Targeted == "LCH"){
			var EvadeRoll = Roll(100);
			var EvadeMove;
			if (EvadeRoll <= 30){EvadeMove = 6;}
			else if (EvadeRoll <= 60) {EvadeMove = 9;}
			else {EvadeMove = 12;}
			alert("Light Chariots may move up to " + EvadeMove + " inches to evade the charge. The charging unit may attempt to follow them with any remaining charge movement.");
		}
		else if (Targeted == "HCH" || Targeted == "MC"){
			var EvadeRoll = Roll(100);
			var EvadeMove;
			if (EvadeRoll <= 30){EvadeMove = 3;}
			else if (EvadeRoll <= 60) {EvadeMove = 6;}
			else {EvadeMove = 9;}
			alert("Heavy Chariots may move up to " + EvadeMove + " inches to evade the charge. The charging unit may attempt to follow them with any remaining charge movement.");
		}
		else if (Targeted == "CT"){
			var EvadeRoll = Roll(100);
			var EvadeMove;
			if (EvadeRoll <= 30){EvadeMove = 3;}
			else if (EvadeRoll <= 60) {EvadeMove = 6;}
			else {EvadeMove = 9;}
			alert("Cataphracts may move up to " + EvadeMove + " inches to evade the charge. The charging unit may attempt to follow them with any remaining charge movement.");
		}
		else if (Targeted == "LH"){
			var EvadeRoll = Roll(100);
			var EvadeMove;
			if (EvadeRoll <= 30){EvadeMove = 9;}
			else if (EvadeRoll <= 60) {EvadeMove = 12;}
			else {EvadeMove = 18;}
			alert("Light Horse may move up to " + EvadeMove + " inches to evade the charge. The charging unit may attempt to follow them with any remaining charge movement.");
		}
		Move = "";
		var Contact = confirm("Did the charging unit reach its target?");

		if (Contact == true){
			if (Targeted == "Bowmen"){
				Move = "If charged frontally, the defender will fire at the charging unit before melee is resolved. If the charging unit suffers a devastating fire result (2+ levels of status loss), it will halt 1 inch before contact. Units which have already acted for the turn may not conduct defensive fire against charges.\n\nIf the charging unit contacts its target, conduct a melee with the Attacker is Charging box checked, after any results of fire have been inflicted.";

				if (CounterCharge == true){
					Move = Move + "\nThe target unit will countercharge - also check the 'Target is Charging' box during the melee.";
				}
			}
			else{
				Move = Move + "Conduct a melee with the Attacker is Charging box checked.";
				if (CounterCharge == true){
					Move = Move + "\nThe target unit will countercharge - also check the 'Target is Charging' box during the melee.";
				}
			}

			alert(Move);
			Move = "";
			CounterCharge = false;

		}
		else {
			alert ("The charging unit will stop at the furthest point of movement.");
		}

	}
	else if(Act =="Fire"){
		if (Acting == "Pikemen" || Acting == "CT" || Acting == "EL" || Acting == "HCH"){
			alert("This type of unit is not permitted to fire. Please choose a valid action.");
			return;
		}

		var FireRoll = 0;

		var TargetRoll = 0;

		//Modifiers

		if (Acting == "Bowmen"){ FireRoll = FireRoll + 1;}
		// Fire vs. mounted - cancels skirmish mod for light horsemen
		if (Targeted == "HC" || Targeted == "MC" || Targeted == "CT" || Targeted == "LCH" || Targeted == "HCH" || Targeted == "EL") { FireRoll = FireRoll + 1; }
		if (Targeted == "LI" || Targeted == "LMI") { TargetRoll = TargetRoll + 1; }
		if (Acting == "Artillery") { FireRoll = FireRoll + 1; }
		if (Acting == "Levy") { FireRoll = FireRoll - 1; }
		// Armor mods
		//Integral

		if (Targeted == "CT") { TargetRoll = TargetRoll + 2; }
		else if ((Targeted == "EL") || (Targeted == "MC") || (Targeted == "Bowmen") || (Targeted == "LCH")){
			//No Modifier
		}
		else{
			TargetRoll = TargetRoll + 1;
		}
		// Add-on armor
		if ((document.getElementById("TargetArmored").checked == true) && (Targeted == "HSpears" || Targeted == "HSwords" || Targeted == "MSpears" || Targeted == "MSwords" || Targeted == "EL")){
			TargetRoll = TargetRoll + 1;
		}

		// Exception for heavy troops
		if(Targeted == "HSpears" || Targeted == "HSwords" || Targeted == "Pikemen"){

			TargetRoll = TargetRoll + 1;
		}



		//Cover mods
		if (Ground == "Soft"){ FireRoll = FireRoll - 1; }
		if (Ground == "Hard"){ FireRoll = FireRoll - 1; }
		if (Ground == "Fortifcation"){ FireRoll = FireRoll - 2; }

		// Number mods
		if (document.getElementById("ActBadlyOut").checked == true){ FireRoll = FireRoll;}
		else if (document.getElementById("ActOut").checked == true){ FireRoll = FireRoll;}
		else if (document.getElementById("Even").checked == true){ FireRoll = FireRoll + 1;}
		else if (document.getElementById("TargetOut").checked == true){ FireRoll = FireRoll + 2;}
		else if (document.getElementById("TargetBadlyOut").checked == true){ FireRoll = FireRoll + 3;}

		// Quality mods
		if (document.getElementById("ActorAdvantage").checked == true){ FireRoll = FireRoll + 1;}
		else if (document.getElementById("TargetAdvantage").checked == true){ TargetRoll = TargetRoll + 1;}

		if (document.getElementById("AppMode").checked == true){

			FireRoll = FireRoll + Roll(6);
			TargetRoll = TargetRoll + Roll(6);


			if (FireRoll <= TargetRoll){
				alert("Target is unaffected by fire.");
			}
			else{
				var FireDiff = FireRoll - TargetRoll;
				if (FireDiff <= 3){
					alert("Target unit loses a Status Level.");
				}
				else{
					alert("Target suffers devasting fire. It loses 2 Status Levels.");

				}
			}
		}
		else{


			var FireDiff = 0;

					if (FireRoll > TargetRoll){
						FireDiff = FireRoll - TargetRoll;
						alert("Firer has the advantage: +" + FireDiff);
					}
					else if (TargetRoll > FireRoll){
						FireDiff = TargetRoll - FireRoll;
						alert("Target has the advantage +" + FireDiff);
					}
					else {
						alert("Neither side has an advantage - it is an even roll.");
					}
			}

	}

	else if(Act =="Melee"){
		var AttRoll = 0;
		var DefRoll = 0;
		var AttMod = 0;
		var DefMod = 0;

		// Mods for troop type

		if (Acting == "HC" || Acting == "MC" || Acting == "LCH"){
			//Elephants cancel with panic despite being mounted
			if (Targeted == "HC" || Targeted == "MC" || Targeted == "CT" || Targeted == "LCH" || Targeted == "HCH" || Targeted == "LH" || Targeted == "LI" || Targeted == "LMI" || Targeted == "MSpears" || Targeted == "MSwords" || Targeted == "Bowmen" || Targeted == "Levy"){ AttRoll = AttRoll + 1; }

			if ((Targeted == "HSpears" || Targeted == "HSwords") && (document.getElementById("TargetFlanked").checked == true)){
				AttRoll = AttRoll + 1;
			}
		}

		else if (Acting == "CT" || Acting == "HCH"){
			AttRoll = AttRoll + 1;
			if (Targeted == "LMI" || Targeted == "Bowmen" || Targeted == "LH"){ AttRoll = AttRoll + 1; }
			if (Targeted == "EL"){ AttRoll = AttRoll - 1; } //Panic
		}

		else if (Acting == "LH"){
			if ((Targeted == "LI") && (Ground == "None")){ AttRoll = AttRoll + 1; }
			if (Targeted == "EL"){ AttRoll = AttRoll - 1; } //Panic
		}

		else if (Acting == "Pikemen"){
			AttRoll = AttRoll + 2;
		}

		else if (Acting == "HSpears" || Acting == "HSwords"){
			AttRoll = AttRoll + 1;
			if ((Targeted == "LMI") && (Ground == "None")){ AttRoll = AttRoll + 1; }
			if (Targeted == "LH"){ AttRoll = AttRoll + 1; }
		}

		else if (Acting == "MSpears" || Acting == "MSwords"){
			if (Targeted == "CT" || Targeted == "HCH"){ AttRoll = AttRoll; }
			else{ AttRoll = AttRoll + 1; }
		}

		else if (Acting == "LMI"){
			if (Targeted == "LI" || Targeted == "LH" || Targeted == "EL"){ AttRoll = AttRoll + 1; }
		}
		else if (Acting == "Bowmen"){
			if (Targeted == "LI" || Targeted == "LH" || Targeted == "EL"){ AttRoll = AttRoll + 1; }
		}
		else if (Acting == "LI"){
			if (Ground == "None" && (Targeted == "HC" || Targeted == "MC" || Targeted == "CT" || Targeted == "JCH" || Targeted == "LMI" || Targeted == "HSpears" || Targeted == "HSwords" || Targeted == "MSpears" || Targeted == "MSwords" || Targeted == "Bowmen")){AttRoll = AttRoll - 5;}
		}
		else if (Acting == "Artillery"){
				AttRoll = AttRoll - 2;
				if (Targeted == "LH" || Targeted == "LI"){ AttRoll = AttRoll; }
				else{ DefRoll == DefRoll -1; }
		}
		else if (Acting == "EL"){
				if (Targeted == "HC" || Targeted == "MC" || Targeted == "CT" || Targeted == "LH" || Targeted == "LCH" || Targeted == "HCH" || Targeted == "EL"){ AttRoll = AttRoll + 2; }
				else{ AttRoll = AttRoll + 1; }
		}
		else if (Acting == "Levy"){
			if (Targeted == "Bowmen" || Targeted == "LI" || Targeted == "LMI" || Targeted == "LH"){ AttRoll = AttRoll + 1; }
		}


		if (Targeted == "HC" || Targeted == "MC" || Targeted == "LCH" ){
			//Elephants cancel with panic despote being mounted
			if (Acting == "HC" || Acting == "MC" || Acting == "CT" || Acting == "LCH" || Acting == "HCH" || Acting == "LH" || Acting == "LI" || Acting == "LMI" || Acting == "MSpears" || Acting == "MSwords" || Acting == "Bowmen" || Acting == "Levy"){ DefRoll = DefRoll + 1; }

			if ((Acting == "HSpears" || Acting == "HSwords") && (document.getElementById("ActorFlanked").checked == true)){
				DefRoll = DefRoll + 1;
			}
		}
		else if (Targeted == "CT" || Targeted == "HCH" ){
			AttRoll = AttRoll + 1;
			if (Acting == "LMI" || Acting == "Bowmen" || Acting == "LH"){ DefRoll = DefRoll + 1; }
			if (Acting == "EL"){ DefRoll = DefRoll - 1; } //Panic
		}
		else if (Targeted == "LH"){
			if ((Acting == "LI") && (Ground == "None")){ AttRoll = AttRoll + 1; }
			if (Acting == "EL"){ DefRoll = DefRoll - 1; } //Panic
		}
		else if (Targeted == "Pikemen"){
			DefRoll = DefRoll + 2;
		}
		else if (Targeted == "HSpears" || Targeted == "HSwords"){
			DefRoll = DefRoll + 1;
			if ((Acting == "LMI") && (Ground == "None")){ DefRoll = DefRoll + 1; }
			if (Acting == "LH"){ DefRoll = DefRoll + 1; }
		}
		else if (Targeted == "MSpears" || Targeted == "MSwords"){
			if (Acting == "CT" || Acting == "HCH"){ DefRoll = DefRoll; }
			else{ DefRoll = DefRoll + 1; }
		}
		else if (Targeted == "LMI"){
			if (Acting == "LI" || Acting == "LH" || Acting == "EL"){ DefRoll = DefRoll + 1; }
		}
		else if (Targeted == "Bowmen"){
			if (Acting == "LI" || Acting == "LH"){ DefRoll = DefRoll + 1; }
		}
		else if (Targeted == "LI"){
			if (Ground == "None" && (Acting == "HC" || Acting == "MC" || Acting == "CT" || Acting == "JCH" || Acting == "LMI" || Acting == "HSpears" || Acting == "HSwords" || Acting == "MSpears" || Acting == "MSwords" || Acting == "Bowmen")){DefRoll = DefRoll - 5;}
		}
		else if (Targeted == "Artillery"){
				DefRoll = DefRoll -2;
				if (Acting == "LH" || Targeted == "LI"){ AttRoll = AttRoll; }
				else{ AttRoll == AttRoll -1; }
		}
		else if (Targeted == "EL"){
				if (Acting == "HC" || Acting == "MC" || Acting == "CT" || Acting == "LH" || Acting == "LCH" || Acting == "HCH" || Acting == "EL"){ DefRoll = DefRoll + 2; }
				else{ DefRoll = DefRoll + 1; }
		}
		else if (Targeted == "Levy"){
			if (Acting == "Bowmen" || Acting == "LI" || Acting == "LMI" || Acting == "LH"){ DefRoll = DefRoll + 1; }
		}


		// Armor Modifiers

		if (Acting == "HSpears" || Acting == "HSwords" || Acting == "MSpears" || Acting == "MSwords" || Acting == "Pikemen" || Acting == "HC"){ AttRoll = AttRoll + 1; }
		else if (Acting == "CT"){ AttRoll = AttRoll + 2;}

		if (Targeted == "HSpears" || Targeted == "HSwords" || Targeted == "MSpears" || Targeted == "MSwords" || Targeted == "Pikemen" || Targeted == "HC"){ DefRoll = DefRoll + 1; }
		else if (Targeted == "CT"){ DefRoll = DefRoll + 2;}

		// Add-on armor
		if ((document.getElementById("ActorArmored").checked == true) && (Acting == "HSpears" || Acting == "HSwords" || Acting == "MSpears" || Acting == "MSwords" || Acting == "EL")){
			AttRoll = AttRoll + 1;
		}
		if ((document.getElementById("TargetArmored").checked == true) && (Targeted == "HSpears" || Targeted == "HSwords" || Targeted == "MSpears" || Targeted == "MSwords" || Targeted == "EL")){
			DefRoll = DefRoll + 1;
		}
		// Exception for heavy troops
		if(Acting == "HSpears" || Acting == "HSwords" || Acting == "Pikemen"){

			AttRoll = AttRoll + 1;
		}
		if(Targeted == "HSpears" || Targeted == "HSwords" || Targeted == "Pikemen"){

			DefRoll = DefRoll + 1;
		}




		// Leader Modifiers
		if (document.getElementById("ActorLeader").checked == true){

			var LeaderQual = confirm("Is attacker's leader Exceptional?");
			if(LeaderQual == true){
				AttRoll = AttRoll + 2;
			}else{
				AttRoll = AttRoll + 1;
			}

		}
		if (document.getElementById("TargetLeader").checked == true){

			var LeaderQual = confirm("Is defender's leader Exceptional?");
			if(LeaderQual == true){
				DefRoll = DefRoll + 2;
			}else{
				DefRoll = DefRoll + 1;
			}
		}

		// Charging and Impact
		// Charging Unit is aggressive
		if ((document.getElementById("ActorCharging").checked == true) && (document.getElementById("ActorAggressive").checked == true)){
			if (Acting == "MSwords" || Acting == "HSwords" || Acting == "MSpears" || Acting == "MC" || Acting == "HC" || Acting == "CT" || Acting == "HCH"){
				AttRoll = AttRoll + 1;
			}

			// Mounted do not get bonus in rough
			if ((Ground != "None") && (Acting == "MC" || Acting == "HC" || Acting == "CT" || Acting == "HCH" || Acting == "LCH" || Acting == "LH")){
				AttRoll = AttRoll - 1;
			}
			// Subtract bous for exceptions

			//No impact for foot against mounted
			if ((Acting == "MSwords" || Acting == "HSwords" || Acting == "MSpears") && (Targeted == "MC" || Targeted == "HC" || Targeted == "CT" || Targeted == "HCH")){
				AttRoll = AttRoll -1;
			}
			//No impact for any against EL
			if (Targeted == "EL"){
				AttRoll = AttRoll -1;
			}
			//Target is not flanked
			if (document.getElementById("TargetFlanked").checked == false){
				if ((Acting == "MC" || Acting == "HC" || Acting == "CT" || Acting == "HCH") && (Targeted == "HSpears" || Targeted == "Pikemen" || Targeted == "MSpears")){
					AttRoll = AttRoll -1;
				}
				if ((Acting == "HSwords" || Acting == "MSwords") && (Targeted == "HSwords" || Targeted == "MSwords")){
					AttRoll = AttRoll -1;
				}
			}
		}

		//Target unit is aggressive
		if ((document.getElementById("TargetCharging").checked == true) && (document.getElementById("TargetAggressive").checked == true)){
			if (Targeted == "MSwords" || Targeted == "HSwords" || Targeted == "MSpears" || Targeted == "MC" || Targeted == "HC" || Targeted == "CT" || Targeted == "HCH"){
				DefRoll = DefRoll + 1;
			}
			// Subtract bous for exceptions
			// Mounted do not get bonus in rough
			if ((Ground != "None") && (Targeted == "MC" || Targeted == "HC" || Targeted == "CT" || Targeted == "HCH" || Targeted == "LCH" || Targeted == "LH")){
				DefRoll = DefRoll - 1;
			}

			//No impact for foot against mounted
			if ((Targeted == "MSwords" || Targeted == "HSwords" || Targeted == "MSpears") && (Acting == "MC" || Acting == "HC" || Acting == "CT" || Acting == "HCH")){
				DefRoll = DefRoll -1;
			}
			//No impact for any against EL
			if (Acting == "EL"){
				DefRoll = DefRoll -1;
			}
			//Target is not flanked
			if (document.getElementById("ActorFlanked").checked == false){
				if ((Targeted == "MC" || Targeted == "HC" || Targeted == "CT" || Targeted == "HCH") && (Acting == "HSpears" || Acting == "Pikemen" || Acting == "MSpears")){
					DefRoll = DefRoll -1;
				}
				if ((Targeted == "HSwords" || Targeted == "MSwords") && (Acting == "HSwords" || Acting == "MSwords")){
					DefRoll = DefRoll -1;
				}
			}
		}

		//Always give charging EL a bonus
		if ((document.getElementById("ActorCharging").checked == true) && (Acting == "EL")){ AttRoll = AttRoll + 1; }
		if ((document.getElementById("TargetCharging").checked == true) && (Targeted == "EL")){ DefRoll = DefRoll + 1; }


		// Flanked

		if (document.getElementById("ActorFlanked").checked == true){ AttRoll = AttRoll - 2;}
		if (document.getElementById("TargetFlanked").checked == true){ DefRoll = DefRoll - 2;}

		// Disorder Modifiers
		if (document.getElementById("ActorDisordered").checked == true){ AttRoll = AttRoll - 1;}
		if (document.getElementById("TargetDisordered").checked == true){ DefRoll = DefRoll - 1;}

		// Number Mods
		if (document.getElementById("ActBadlyOut").checked == true){ AttRoll = AttRoll - 2;}
		if (document.getElementById("TargetBadlyOut").checked == true){ DefRoll = DefRoll - 2;}
		if (document.getElementById("ActOut").checked == true){ AttRoll = AttRoll - 1;}
		if (document.getElementById("TargetOut").checked == true){ DefRoll = DefRoll - 1;}

		// Height Advantage
		if (document.getElementById("ActorUphill").checked == true){ AttRoll = AttRoll + 1;}
		if (document.getElementById("TargetUphill").checked == true){ DefRoll = DefRoll + 1;}

		//Quality mods
		if (document.getElementById("ActorAdvantage").checked == true){ AttRoll = AttRoll + 1;}
		if (document.getElementById("TargetAdvantage").checked == true){ DefRoll = DefRoll + 1;}

		// Terrain Mods

		if ((Ground == "Soft" || Ground == "RoughNoCover") && (Acting == "HSwords" || Acting == "Levy")){ AttRoll = AttRoll - 1; }
		if ((Ground == "Soft" || Ground == "RoughNoCover") && (Acting == "HSpears" || Acting == "Pikemen" || Acting == "HC" || Acting == "MC"|| Acting == "CT" || Acting == "LH" || Acting == "LCH" || Acting == "HCH")){ AttRoll = AttRoll - 2; }

		if ((Ground == "Soft" || Ground == "RoughNoCover") && (Targeted == "HSwords" || Targeted == "Levy")){ DefRoll = DefRoll - 1; }
		if ((Ground == "Soft" || Ground == "RoughNoCover") && (Targeted == "HSpears" || Targeted == "Pikemen" || Targeted == "HC" || Targeted == "MC" || Targeted == "CT" || Targeted == "LH" || Targeted == "LCH" || Targeted == "HCH")){ DefRoll = DefRoll - 2; }

		if ((Ground == "Hard")&& (Acting == "MSpears" || Acting == "MSwords" || Acting == "LMI" || Acting == "Bowmen")){ AttRoll = AttRoll - 1;}
		if ((Ground == "Hard")&& (Acting == "HSpears" || Acting == "HSwords" || Acting == "Pikemen" || Acting == "Levy")){ AttRoll = AttRoll - 2;}
		if ((Ground == "Hard")&& (Acting == "HC" || Acting == "MC"|| Acting == "CT" || Acting == "LH" || Acting == "LCH" || Acting == "HCH" || Acting == "EL")){ AttRoll = AttRoll - 3;}

		if ((Ground == "Hard")&& (Targeted == "MSpears" || Targeted == "MSwords" || Targeted == "LMI" || Targeted == "Bowmen")){ DefRoll = DefRoll - 1;}
		if ((Ground == "Hard")&& (Targeted == "HSpears" || Targeted == "HSwords" || Targeted == "Pikemen" || Targeted == "Levy")){ DefRoll = DefRoll - 2;}
		if ((Ground == "Hard")&& (Targeted == "HC" || Targeted == "MC"|| Targeted == "CT" || Targeted == "LH" || Targeted == "LCH" || Targeted == "HCH" || Targeted == "EL")){ DefRoll = DefRoll - 3;}

		if (Ground == "Fortification"){ DefRoll = DefRoll + 2;}
		if ((Ground == "Fortification")&& (Acting == "HC" || Acting == "MC"|| Acting == "CT" || Acting == "LH" || Acting == "LCH" || Acting == "HCH" || Acting == "EL")){ AttRoll = AttRoll - 2;}

		// Two-handed weapons

		var TwoHanded = false;
		if (Acting == "HSwords" || Acting == "MSwords"){

				TwoHanded = confirm("Does the acting unit have two-handed weapons?");
				if (TwoHanded == true){
					AttRoll = AttRoll + 1;
					TwoHanded = false;
				}
		}
		if (Targeted == "HSwords" || Targeted == "MSwords"){
				TwoHanded = confirm("Does the target unit have two-handed weapons?");
				if (TwoHanded == true){
					DefRoll = DefRoll + 1;
					TwoHanded = false;
				}
		}


// Populate dice mods
	AttMod = AttRoll;
	DefMod = DefRoll;
	var AttackerDice = Roll(6);
	var DefenderDice = Roll(6)
	AttRoll = AttRoll + AttackerDice;
	DefRoll = DefRoll + DefenderDice;

		// Combat Outcomes
	if (document.getElementById("AppMode").checked == true){
		if (AttRoll < DefRoll){
			var DefWin = DefRoll - AttRoll;


			if (DefWin <= 2){
				Move = "Acting unit loses a Status Level.";
				if (document.getElementById("ActorLeader").checked == true){
					var LeaderKill = Roll(6);
					if (LeaderKill <= 1){
						Move = Move + "\n\nThe leader with the Acting unit has been killed or captured.";
					}
				}
			}
			else if (DefWin <= 4){
				Move = "Acting unit loses 2 Status Levels.";
				if (document.getElementById("ActorLeader").checked == true){
					var LeaderKill = Roll(6);
					if (LeaderKill <= 1){
						Move = Move + "\n\nThe leader with the Acting unit has been killed or captured.";
					}
				}
			}
			else if (DefWin <= 6){
				Move = "Acting unit loses 3 Status Levels.";
				if (document.getElementById("ActorLeader").checked == true){
					var LeaderKill = Roll(6);
					if (LeaderKill <= 2){
						Move = Move + "\n\nThe leader with the Acting unit has been killed or captured.";
					}
				}
			}
			else{
				Move = "Acting unit is destroyed. Remove from play.";
				if (document.getElementById("ActorLeader").checked == true){
					var LeaderKill = Roll(6);
					if (LeaderKill <= 3){
						Move = Move + "\n\nThe leader with the Acting unit has been killed or captured.";
					}
				}

			}
		}
		else if (AttRoll == DefRoll){
			Move = "Ongoing struggle: Acting and Target units both lose a status level.";
		}
		else{
			var AttWin = AttRoll - DefRoll;


			if (AttWin <= 2){
				Move = "Target unit loses a Status Level.";
				if (document.getElementById("TargetLeader").checked == true){
					var LeaderKill = Roll(6);
					if (LeaderKill <= 1){
						Move = Move + "\n\nThe leader with the Targeted unit has been killed or captured.";
					}
				}
			}
			else if (AttWin <= 4){
				Move = "Target unit loses 2 Status Levels.";
				if (document.getElementById("TargetLeader").checked == true){
					var LeaderKill = Roll(6);
					if (LeaderKill <= 1){
						Move = Move + "\n\nThe leader with the Targeted unit has been killed or captured.";
					}
				}
			}
			else if (AttWin <= 6){
				Move = "Target unit loses 3 Status Levels.";
				if (document.getElementById("TargetLeader").checked == true){
					var LeaderKill = Roll(6);
					if (LeaderKill <= 2){
						Move = Move + "\n\nThe leader with the Targeted unit has been killed or captured.";
					}
				}
			}
			else{
				Move = "Target unit is destroyed. Remove from play.";
				if (document.getElementById("TargetLeader").checked == true){
					var LeaderKill = Roll(6);
					if (LeaderKill <= 3){
						Move = Move + "\n\nThe leader with the Targeted unit has been killed or captured.";
					}
				}

			}
		}
		alert(Move);
		Move = "";
	}
	else{ //Dice Mode
	var DiceDiff = 0;

		if (AttMod > DefMod){
			DiceDiff = AttMod - DefMod;
			alert("Attacker has the advantage: +" + DiceDiff);
		}
		else if (DefMod > AttMod){
			DiceDiff = DefMod - AttMod;
			alert("Defender has the advantage +" + DiceDiff);
		}
		else {
			alert("Neither side has an advantage - it is an even roll.");
		}

	}


}
	else if (Act == "ChargeTest"){


		var Tempt = Roll(10);
		if (document.getElementById("ActorLeader").checked == true){
			Tempt = Tempt + 3;
			}
		if (Tempt <= 5){
			alert("The unit will charge. An attached leader may accompany it if desired.");
		}
		else{
			alert("The unit resists the temptation to charge.");
		}

	}



}