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 <= 50){ alert("Initiative gives 1 Action. (Add 1 for Exceptional Leader.)"); }
	else if(CmdRoll <= 85){ alert("Initiative gives 2 Actions. (Add 1 for Exceptional Leader.)"); }
	else{ alert("Initiative gives 3 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 <= 10){
		return "Delayed";
	}
	else if (DelayRoll <=20){
		return "HalfDelayed";
	}
	else{
		return "NoDelay";
	}
}

function Result() {

	var Act = document.MedForm.Action.value;

	var Acting = document.MedForm.Actor.value;
	var Targeted = document.MedForm.Target.value;
	var Ground = document.MedForm.Cover.value;


	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; }
			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 == "Knights"){
				Move = Move + "Unit will move 6 inches.";
			}
			if (Acting == "MediumHorsemen"){
				Move = Move + "Unit will move 8 inches.";
			}
			else if (Acting == "FootKnights"){
				Move = Move + "Unit will move 3 inches.";
			}
			else if (Acting == "Footmen"){
				Move = Move + "Unit will move 3 inches.";
			}
			else if (Acting == "Pikemen"){
				Move = Move + "Unit will move 3 inches.";
			}
			else if (Acting == "Archers"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "Crossbowmen"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "Transport"){
				Move = Move + "Unit will move 1 inch.";
			}
			else if (Acting == "Levy"){
				Move = Move + "Unit will move 3 inches.";
			}
			else if (Acting == "Horsemen"){
				Move = Move + "Unit will move 9 inches.";
			}
			else { // Skirmishers
				Move = Move + "Unit will move 6 inches.";
			}
			alert (Move);
			Move = "";
		}
		else{
			if (Acting == "Knights"){
				Move = "Unit will move 12 inches.";
			}
			else if (Acting == "MediumHorsemen"){
				Move = "Unit will move 16 inches, or may move 8 inches and shoot if armed with missile weapons.";
			}
			else if (Acting == "FootKnights"){
				Move = "Unit will move 6 inches.";
			}
			else if (Acting == "Footmen"){
				Move = "Unit will move 6 inches.";
			}
			else if (Acting == "Pikemen"){
				Move = "Unit will move 6 inches.";
			}
			else if (Acting == "Archers"){
				Move = "Unit will move 9 inches.";
			}
			else if (Acting == "Crossbowmen"){
				Move = "Unit will move 9 inches.";
			}
			else if (Acting == "Transport"){
				Move = "Unit will move 3 inches.";
			}
			else if (Acting == "Levy"){
				Move = "Unit will move 6 inches.";
			}
			else if (Acting == "Horsemen"){
				Move = "Unit will move 18 inches, or may move 9 inches and shoot if armed with missile weapons.";
			}
			else { // Skirmishers
				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; }
		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 == "Knights"){
				Move = "Unit will break out of combat and fall directly back up to 6 inches, facing the enemy.";
			}
			else if (Acting == "MediumHorsemen"){
				Move = "Unit will break out of combat and fall directly back up to 8 inches, facing the enemy.";
			}
			else if (Acting == "FootKnights"){
				Move = "Unit will break out of combat and fall directly back up to 3 inches, facing the enemy.";
			}
			else if (Acting == "Footmen"){
				Move = "Unit will break out of combat and fall directly back up to 3 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 == "Archers"){
				Move = "Unit will break out of combat and fall directly back up to 6 inches, facing the enemy.";
			}
			else if (Acting == "Crossbowmen"){
				Move = "Unit will break out of combat and fall directly back up to 6 inches, facing the enemy.";
			}
			else if (Acting == "Transport"){
				Move = "Unit cannot fall back. Please choose a valid action.";
			}
			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 == "Horsemen"){
				Move = "Unit will break out of combat and fall directly back up to 9 inches, facing the enemy.";
			}
			else { // Skirmishers
				Move = "Unit will break out of combat and fall directly back up to 9 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 == "Knights"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "MediumHorsemen"){
				Move = Move + "Unit will move 10 inches.";
			}
			else if (Acting == "FootKnights"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "Footmen"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "Pikemen"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "Archers"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "Crossbowmen"){
				Move = Move + "Unit will move 9 inches.";
			}
			else if (Acting == "Transport"){
				Move = Move + "Unit will move 3 inches.";
			}
			else if (Acting == "Levy"){
				Move = Move + "Unit will move 6 inches.";
			}
			else if (Acting == "Horsemen"){
				Move = Move + "Unit will move 12 inches.";
			}
			else { // Skirmishers
				Move = Move + "Unit will move 9 inches.";
			}
			Move = Move + " Unit must form a column one base in width, and may change facing at the start of the move.";
			alert (Move);
			Move = "";
		}
		else{
			if (Acting == "Knights"){
				Move = "Unit will move 18 inches.";
			}
			else if (Acting == "MediumHorsemen"){
				Move = "Unit will move 20 inches.";
			}
			else if (Acting == "FootKnights"){
				Move = "Unit will move 9 inches.";
			}
			else if (Acting == "Footmen"){
				Move = "Unit will move 9 inches.";
			}
			else if (Acting == "Pikemen"){
				Move = "Unit will move 9 inches.";
			}
			else if (Acting == "Archers"){
				Move = "Unit will move 12 inches.";
			}
			else if (Acting == "Crossbowmen"){
				Move = "Unit will move 12 inches.";
			}
			else if (Acting == "Transport"){
				Move = "Unit will move 6 inches.";
			}
			else if (Acting == "Levy"){
				Move = "Unit will move 9 inches.";
			}
			else if (Acting == "Horsemen"){
				Move = "Unit will move 24 inches.";
			}
			else { // Skirmishers
				Move = "Unit will move 12 inches.";
			}
			Move = Move + " Unit must form a column one base in width, and may change facing at the start of the move.";
			alert(Move);

			Move = "";
		}
	}
	else if (Act == "ResistCharge"){
		if (Acting != "Knights"){
			alert("Only knights are tempted to charge. Please choose a valid action.");
			return;
		}
		var TemptRoll = Roll(100);
		if (document.getElementById("ActorLeader").checked == true){
			var Excep = confirm ("Is the leader Exceptional?");
			if (Excep == true){
				TemptRoll = TemptRoll + 20;
			}
			else{
				TemptRoll = TemptRoll + 10;
			}
		}
		if (TemptRoll <= 50){
			alert("The knights must charge!");
		}
		else{
			alert("The knights obey their orders, and do not make an impulsive charge. They may act normally.");
		}


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

		Move = "";
		if (Acting == "Transport"){
			alert("Transport cannot charge. Please choose a valid action.");
			return;
		}
		if (Acting == "Skirmishers" && (Targeted != "Skirmishers" && Targeted != "Cannon") && Ground == "None"){
			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 == "Knights"){
				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 == "MediumHorsemen"){
				Move = Move + "Unit will charge 8 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "FootKnights"){
				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 == "Footmen"){
				Move = Move + "Unit will move 3 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 == "Archers"){
				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 == "Crossbowmen"){
				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 == "Horsemen"){
				Move = Move + "Unit will charge 9 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else { // Skirmishers
				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 == "Knights"){
				Move = "Unit will charge 12 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "MediumHorsemen"){
				Move = Move + "Unit will charge 16 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "FootKnights"){
				Move = "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "Footmen"){
				Move = "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "Pikemen"){
				Move = "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "Archers"){
				Move = "Unit will charge 9 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "Crossbowmen"){
				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 = "Unit will charge 6 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else if (Acting == "Horsemen"){
				Move = "Unit will charge 18 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
			else { // Skirmishers
				Move = "Unit will charge 9 inches.\n\nUnit may wheel before charging, but must move directly forward into contact after wheeling.";
			}
		}
		alert(Move);

		CounterCharge = false;

		if ((Acting == "Knights" || Acting == "MediumHorsemen") && (Targeted == "Knights" || Targeted == "MediumHorsemen")){
			CounterCharge = confirm("A frontal charge will allow the Target to perform a counter-charge. Will the Target countercharge?");
			if (CounterCharge == true){
					alert("Move the Target unit forward an equal distance to the charging unit until contact is made, or the charging unit has no more movement.");
			}
		}

		if (Targeted == "Skirmishers"){
			var EvadeRoll = Roll(100);
			var EvadeMove;
			if (EvadeRoll <= 30){EvadeMove = 3;}
			else if (EvadeRoll <= 60) {EvadeMove = 6;}
			else {EvadeMove = 9;}
			alert("Skirmishers may move up to " + EvadeMove + " inches to evade the charge. The charging unit may attempt to follow them with any remaining charge movement.");
		}
		if (Targeted == "Horsemen"){
			var EvadeRoll = Roll(100);
			var EvadeMove;
			if (EvadeRoll <= 30){EvadeMove = 6;}
			else if (EvadeRoll <= 60) {EvadeMove = 9;}
			else {EvadeMove = 12;}
			alert("Horsemen may move up to " + EvadeMove + " inches to evade the charge. The charging unit may attempt to follow them with any remaining charge movement.");
		}
		if ((Targeted == "MediumHorsemen") && (CounterCharge == false)){
			var EvadeRoll = Roll(100);
			var EvadeMove;
			if (EvadeRoll <= 30){EvadeMove = 4;}
			else if (EvadeRoll <= 60) {EvadeMove = 8;}
			else {EvadeMove = 10;}
			alert("Horsemen 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 == "Archers" || Targeted == "Crossbowmen"){
				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.";
			}
			else{
				Move = Move + "Conduct a melee with the Attacker is Charging box checked.";
			}

			alert(Move);
			Move = "";

		}
		else {
			alert ("The charging unit will stop at the furthest point of movement.");
		}
	}
	else if (Act == "TrapTest"){
		if (Acting != "Knights" && Acting != "MediumHorsemen"){
			alert("Only charging knights and medium horsemen need to test to detect traps. Please choose a valid action.");
			return;
		}
		var DetectRoll = Roll(100);
		if (document.getElementById("ActorLeader").checked == true){
			var ExcepDet = confirm ("Is the leader Exceptional?");
			if (ExcepDet == true){
				DetectRoll = DetectRoll + 20;
			}
			else{
				DetectRoll = DetectRoll + 10;
			}
		}
		if (DetectRoll <= 15){
			alert("The charging unit falls into the traps and suffers 2 status levels of damage. They will stop short 1 inch short of the target of the charge.");
		}
		else if (DetectRoll <= 30){
			alert("The charging unit falls into the traps and suffers 1 status level of damage. They will stop short 1 inch short of the target of the charge.");
		}
		else if (DetectRoll <= 60){
			alert("The charging unit sees the traps and avoids damage. They will stop short 1 inch short of the target of the charge.");
		}
		else{
			alert("The charging avoids the traps and completes the charge - it should be continued normally.");
		}
	}
	else if (Act == "RiddenDown"){ // Only for footmen hit by retreating knights
		if (Acting == "Knights" || Acting == "MediumHorsemen" || Acting == "Horsemen"){
			alert("Mounted units do not suffer from being ridden down. Please choose a valid action.");
			return;
		}
		var RiddenRoll = Roll(100);
		if (document.getElementById("ActorLeader").checked == true){
			var Ridden = confirm ("Is the leader Exceptional?");
			if (Ridden == true){
				RiddenRoll = RiddenRoll + 20;
			}
			else{
				RiddenRoll = RiddenRoll + 10;
			}
		}
		//Troop types
		if (Acting == "Levy" || Acting == "Skirmishers"){
			RiddenRoll = RiddenRoll - 20;
		}
		else if (Acting == "Archers" || Acting == "Crossbowmen" || Acting == "Footmen"){
			RiddenRoll = RiddenRoll - 10;
		}


		if (RiddenRoll <= 15){
			alert("The unit is trampled into the dust, suffering 3 status levels of damage.");
		}
		else if (RiddenRoll <= 30){
			alert("The unit is badly affected, suffering 2 status levels of damage.");
		}
		else if (RiddenRoll <= 60){
			alert("The unit is shaken by their fleeing fellows, suffering 1 status level of damage.");
		}
		else{
			alert("The unit gets out of the way, and remains unaffected.");
		}
	}
	else if (Act == "Fire"){

		if (Acting == "Knights" || Acting == "FootKnights" || Acting == "Footmen" || Acting == "Pikemen" || Acting == "Levy"){
			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 == "Skirmishers") { FireRoll = FireRoll - 1; }
		else if (Acting == "Horsemen" || Acting == "MediumHorsemen") { FireRoll = FireRoll - 1; }
		// Fire vs. mounted - cancels skirmish mod for light horsemen
		if (Targeted == "Knights" || Targeted == "MediumHorsemen" || Targeted == "Transport") { FireRoll = FireRoll + 1; }
		if (Targeted == "Skirmishers") { TargetRoll = TargetRoll + 1; }
		// Armor mods

		if ((Targeted == "Knights" || Targeted == "FootKnights") && Acting != "Crossbowmen") { TargetRoll = TargetRoll + 2; }
		if ((Targeted == "Knights" || Targeted == "FootKnights") && Acting == "Crossbowmen") { TargetRoll = TargetRoll + 1; }
		if (Targeted == "Footmen" || Targeted == "Pikemen" || Targeted == "MediumHorsemen") { 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;}

		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 { // Melee
		var AttRoll = 0;
		var DefRoll = 0;
		var AttMod = 0;
		var DefMod = 0;

		// Mods for troop type
		if (Acting == "Transport"){
			alert("Transport may not attack. Please choose a valid action.");
			return;
		}
		if (Targeted == "Transport"){
			var Looting = Roll (100);
			if (document.getElementById("ActorLeader").checked == true){
				var Control = confirm ("Is the leader Exceptional?");
				if (Control == true){
					Looting = Looting + 20;
				}
				else{
					Looting = Looting + 10;
				}
			}
			if (Looting <= 50){
				alert("The transport has been captured or destroyed and the attacking unit will lose 1 status level, because the troops are engaged in looting and pillaging. Remove the transport from play.")
				return;
			}
			else{
				alert("The transport has been captured or destroyed. Remove the transport from play.")
				return;
			}
		}
		if (Acting == "Knights"){
			AttRoll = AttRoll + 2;
			if (Targeted == "Archers" || Targeted == "Crossbowmen" || Targeted == "Knights" || Targeted == "MediumHorsemen" || Targeted == "Horsemen"){ AttRoll = AttRoll + 1; }
		}
		if (Acting == "MediumHorsemen"){
			if (Targeted == "Archers" || Targeted == "Crossbowmen" || Targeted == "Knights" || Targeted == "MediumHorsemen" || Targeted == "Horsemen" || Targeted == "Levy"){ AttRoll = AttRoll + 1; }
		}
		else if (Acting == "FootKnights"){
			AttRoll = AttRoll + 1;
			if ((Targeted == "Archers" || Targeted == "Crossbowmen") && Ground == "None"){ AttRoll = AttRoll + 1; }
			if (Targeted == "Horsemen"){ AttRoll = AttRoll + 1; }
		}
		else if (Acting == "Footmen"){
			AttRoll = AttRoll + 1;
			if (Targeted == "Knights") {AttRoll = AttRoll -1;}
			if ((Targeted == "Archers" || Targeted == "Crossbowmen") && Ground == "None"){ AttRoll = AttRoll + 1; }
			if (Targeted == "Horsemen"){ AttRoll = AttRoll + 1; }
		}
		else if (Acting == "Pikemen"){
			AttRoll = AttRoll + 1;
		}
		else if (Acting == "Archers"){
			if (Targeted == "Horsemen" || Targeted == "Skirmishers"){ AttRoll = AttRoll + 1; }
		}
		else if (Acting == "Crossbowmen"){
			if (Targeted == "Horsemen" || Targeted == "Skirmishers"){ AttRoll = AttRoll + 1; }
		}
		else if (Acting == "Levy"){
			AttRoll = AttRoll - 2;
			if (Targeted == "Archers" || Targeted == "Crossbowmen" || Targeted == "Horsemen" || Targeted == "Skirmishers"){ AttRoll = AttRoll + 1; }
		}
		else if (Acting == "Horsemen"){
			if (Targeted == "Skirmishers" && Ground == "None"){ AttRoll = AttRoll + 1; }
		}
		else { // Skirmishers
			if (Ground == "None" && (Targeted == "Knights" || Targeted == "FootKnights" || Targeted == "Footmen" || Targeted == "Archers" || Targeted == "Pikemen" || Targeted == "MediumHorsemen" || Targeted == "Horsemen" || Targeted == "Levy" || Targeted == "Crossbowmen")){AttRoll = AttRoll - 5;}
		}


		if (Targeted == "Knights"){
			DefRoll = DefRoll + 2;
			if (Acting == "Archers" || Acting == "Crossbowmen" || Acting == "Knights" || Acting == "MediumHorsemen" || Acting == "Horsemen"){ DefRoll = DefRoll + 1; }
		}
		if (Targeted == "MediumHorsemen"){
			if (Acting == "Archers" || Acting == "Crossbowmen" || Acting == "Knights" || Acting == "MediumHorsemen" || Acting == "Horsemen" || Acting == "Levy"){ DefRoll = DefRoll + 1; }
		}
		else if (Targeted == "FootKnights"){
			DefRoll = DefRoll + 1;
			if ((Acting == "Archers" || Acting == "Crossbowmen") && Ground == "None"){ DefRoll = DefRoll + 1; }
			if (Acting == "Horsemen"){ DefRoll = DefRoll + 1; }
		}
		else if (Targeted == "Footmen"){
			DefRoll = DefRoll + 1;
			if (Acting == "Knights") {DefRoll = DefRoll -1;}
			if ((Acting == "Archers" || Acting == "Crossbowmen") && Ground == "None"){ DefRoll = DefRoll + 1; }
			if (Acting == "Horsemen"){ DefRoll = DefRoll + 1; }
		}
		else if (Targeted == "Pikemen"){
			DefRoll = DefRoll + 1;
		}
		else if (Targeted == "Archers"){
			if (Acting == "Horsemen" || Acting == "Skirmishers"){ DefRoll = DefRoll + 1; }
		}
		else if (Targeted == "Crossbowmen"){
			if (Acting == "Horsemen" || Acting == "Skirmishers"){ DefRoll = DefRoll + 1; }
		}
		else if (Targeted == "Levy"){
			DefRoll = DefRoll -2;
			if (Acting == "Archers" || Acting == "Crossbowmen" || Acting == "Horsemen" || Acting == "Skirmishers"){ DefRoll = DefRoll + 1; }
		}
		else if (Targeted == "Horsemen"){
			if (Acting == "Skirmishers" && Ground == "None"){ DefRoll = DefRoll + 1; }
		}
		else { // Skirmishers
			if (Ground == "None" && (Acting == "Knights" || Acting == "FootKnights" || Acting == "Footmen" || Acting == "Archers" || Acting == "Pikemen"|| Acting == "MediumHorsemen" || Acting == "Horsemen" || Acting == "Levy" || Acting == "Crossbowmen")){DefRoll = DefRoll - 5;}
		}

		// Armor Modifiers
		if (Acting == "Knights" || Acting == "FootKnights"){ AttRoll = AttRoll + 1; }
		if (Targeted == "Knights" || Targeted == "FootKnights"){ 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 Knights
		if (document.getElementById("Charging").checked == true && Ground == "None" && Acting == "Knights" && Targeted != "Pikemen" && CounterCharge != true){ AttRoll = AttRoll + 2;}
		if (document.getElementById("Charging").checked == true && Ground == "None" && Acting == "MediumHorsemen" && Targeted != "Pikemen" && CounterCharge != true){ AttRoll = AttRoll + 1;}


		// Flanked

		if (document.getElementById("ActorFlanked").checked == true && Acting != "Skirmishers" && Acting != "Horsemen"){ AttRoll = AttRoll - 2;}
		if (document.getElementById("TargetFlanked").checked == true && Targeted != "Skirmishers" && Targeted != "Horsemen"){ 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;}

		// Terrain Mods
		if ((Ground == "Soft" || Ground == "RoughNoCover") && (Acting == "Knights" || Acting == "MediumHorsemen" || Acting == "Horsemen")){ AttRoll = AttRoll - 2;}
		if (Ground == "Hard" && (Acting == "Knights"|| Acting == "MediumHorsemen"  || Acting == "Horsemen")){ AttRoll = AttRoll - 3;}
		if (Ground == "Fortification" && (Acting == "Knights"|| Acting == "MediumHorsemen"  || Acting == "Horsemen")){ AttRoll = AttRoll - 2;}

		if ((Ground == "Soft" || Ground == "RoughNoCover") && (Targeted == "Knights" || Targeted == "MediumHorsemen" || Targeted == "Horsemen")){ DefRoll = DefRoll - 2;}
		if (Ground == "Hard" && (Targeted == "Knights" || Targeted == "Horsemen")){ DefRoll = DefRoll - 3;}

		if (Ground == "Soft" && (Acting == "FootKnights" || Acting == "Footmen" || Acting == "Levy")){ AttRoll = AttRoll - 1;}
		if (Ground == "Hard" && (Acting == "FootKnights" || Acting == "Footmen" || Acting == "Levy" || Acting == "Pikemen")){ AttRoll = AttRoll - 2;}
		if (Ground == "Soft" && Acting == "Pikemen"){ AttRoll = AttRoll - 2;}
		if (Ground == "Hard" && (Acting == "Archers" || Acting == "Crossbowmen")){ AttRoll = AttRoll - 1;}
		if (Ground == "Fortification" && (Acting == "FootKnights" || Acting == "Footmen" || Acting == "Levy" || Acting == "Archers" || Acting == "Crossbowmen" || Acting == "Skirmishers" || Acting == "Pikemen")){ AttRoll = AttRoll - 1;}

		if (Ground == "Soft" && (Targeted == "FootKnights" || Targeted == "Footmen" || Targeted == "Levy")){ DefRoll = DefRoll - 1;}
		if (Ground == "Hard" && (Targeted == "FootKnights" || Targeted == "Footmen" || Targeted == "Levy" || Targeted == "Pikemen")){ DefRoll = DefRoll - 2;}
		if (Ground == "Soft" && Targeted == "Pikemen"){ DefRoll = DefRoll - 2;}
		if (Ground == "Hard" && (Targeted == "Archers" || Targeted == "Crossbowmen")){ DefRoll = DefRoll - 1;}


// 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;
			// Two-handed weapons
			if(Targeted == "Footmen" || Targeted == "FootKnights"){ DefWin = DefWin + 1; }

			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;

			// Two-handed weapons
			if(Acting == "Footmen" || Acting == "FootKnights"){ AttWin = AttWin + 1; }

			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.");
		}

	}

}
}