var RandRoll = 0;
var OrdersLost = false;
var OrdersDelayed = false;
var ResultString = "";
var InitMod = 0;



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

function Initiative(){

	var InitRoll = Roll(100);
	InitRoll = InitRoll + InitMod;
	if (InitRoll >= 50){
			alert("Allied Initiative");
			InitMod = InitMod - 20;
	}
	else {
		alert("Central Powers Initiative");
		InitMod = InitMod + 20;
	}
}

function Confusion(){

		OrdersLost = false;
		OrdersDelayed = false;

		var ConRoll = Roll(100);

	// Modify confusion roll

//	if (document.DGKForm.Actor.value == "Artillery"){
//		var Leader = confirm("Is there a commanding general within 6 inches, or a field telephone for communicating to higher command?");
//	}
//	else{

		var Leader = confirm("Is there a commanding general within 6 inches?");
//}
		if (Leader == true){
			ConRoll = ConRoll + 5;
		}

		if (document.DGKForm.Actor.value == "VetInf" || document.DGKForm.Actor.value == "VetCav"){
			ConRoll = ConRoll + 3;
		}

		if (document.DGKForm.Actor.value == "IrregInf" || document.DGKForm.Actor.value == "IrregCav"){
			ConRoll = ConRoll - 3;
		}
		if (document.DGKForm.ActorDisordered.checked == true){
			ConRoll = ConRoll - 5;
		}
	// Determine confusion

		if (ConRoll <= 5){
			OrdersLost = true;
			OrdersDelayed = false;
		}
		else if (ConRoll <= 10){
			OrdersDelayed = true;
			OrdersLost = false;
		}
		else {
			OrdersLost = false;
			OrdersDelayed = false;
		}


}


function FireResult(FireBase){
	//FireBase is the base firing number of the firer bases on troop type - already includes quality of firer and action (movinc vs. deployed fire)

	// Target Formation mods
	if (document.getElementById("r2").checked == true){

		FireBase = FireBase + 1;
	}
	if (document.getElementById("r3").checked == true){

		FireBase = FireBase + 2;
	}

	// Cover mods
	if (document.DGKForm.Cover.value == "Soft"){

		FireBase = FireBase - 1;
	}
	if (document.DGKForm.Cover.value == "Hard"){

		FireCase = FireBase - 2;
	}
	if (document.DGKForm.Cover.value == "Fortification"){

		FireBase = FireBase - 3;
	}

	// Disorder mods

	if (document.DGKForm.ActorDisordered.checked == true){

		FireBase = FireBase - 1;
	}
	if (document.DGKForm.TargetDisordered.checked == true){

			FireBase = FireBase + 1;
	}

	// Outnumbered mods

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

			FireBase = FireBase - 1;
	}
	else if (document.getElementById("ActBadlyOut").checked == true){

			FireBase = FireBase - 2;
	}
	else if (document.getElementById("TargetOut").checked == true){

			FireBase = FireBase + 1;
	}
	else if (document.getElementById("TargetBadlyOut").checked == true){

			FireBase = FireBase + 2;
	}

	// Quality mods

	if (document.DGKForm.Target.value == "VetInf" || document.DGKForm.Target.value == "VetCav"){

		FireBase = FireBase - 1;
	}
	else if (document.DGKForm.Target.value == "IrregInf" || document.DGKForm.Target.value == "IrregCav"){

			FireBase = FireBase + 1;
	}


	// Determine Results of Fire
	var FireRoll = Roll(6);

	if (FireRoll == 1){
		FireBase = FireBase - 2;
	}
	else if (FireRoll == 2){
		FireBase = FireBase - 1;
	}
	else if (FireRoll <= 4){
		FireBase = FireBase;
	}
	else if (FireRoll == 5){
		FireBase = FireBase + 1;
	}
	else if (FireRoll <=6){
		FireBase = FireBase + 2;
	}

	var FireResult = FireBase - 4;

	if (FireResult <= 0){

		ResultString = "Fire has little or no effect.";
	}
	else{
		DefLeaderKill = FireResult;
		FireResult = Math.round(FireResult/2);
		ResultString = "Target suffers " + FireResult + " level(s) of damage.";

	}
}


function Result(){

	if (document.DGKForm.Action.value == "Deploy"){

		Confusion();

		if (OrdersLost == true){

			alert("Orders have been lost. Unit may fire, but may not change formation/facing or move this turn.");
			return;
		}

		ResultString = "Unit will move up to 1 inch in deployed/tactical formation, facing in any direction. (May be combined with Moving Fire from starting position.)";

	}
	else if (document.DGKForm.Action.value == "Maneuver"){

		Confusion();

		if (OrdersLost == true){

			alert("Orders have been lost. Unit may fire, but may not change formation/facing or move this turn.");
			return;
		}
		else if (OrdersDelayed == true){

			ResultString = ResultString + "Orders have been delayed in transmission. ";
		}

		var ManeuverDist = 0;

		if (document.DGKForm.Actor.value == "RegInf" || document.DGKForm.Actor.value == "Artillery"|| document.DGKForm.Actor.value == "MG"){

			ManeuverDist = 6;
		}
		if (document.DGKForm.Actor.value == "HvyArtillery"){

			ManeuverDist = 4;
		}
		else if (document.DGKForm.Actor.value == "IrregInf"){

			ManeuverDist = 5;
		}
		else if (document.DGKForm.Actor.value == "VetInf"){

			ManeuverDist = 7;
		}
		else if (document.DGKForm.Actor.value == "IrregCav"){

			ManeuverDist = 10;
		}
		else if (document.DGKForm.Actor.value == "RegCav"){

			ManeuverDist = 12;
		}
		else if (document.DGKForm.Actor.value == "VetCav"){

			ManeuverDist = 14;
		}
		else if (document.DGKForm.Actor.value == "Vehicle"){

				ManeuverDist = 8;
		}

	// Adjust for delayed orders
		if (OrdersDelayed == true){

			ManeuverDist = ManeuverDist * .5;
		}

		if (document.DGKForm.Actor.value == "Vehicle"){
			ResultString = ResultString + "Unit will assume maneuver formation and may change facing at start of move if applicable. Base move is " + ManeuverDist + " inches for fast vehicles/transport and " + (ManeuverDist/2) + " inches for slow vehicles/transport. (May be combined with Moving Fire from starting position if capable of fire.)";
		}
		else{
			ResultString = ResultString + "Unit will assume maneuver formation (at least two bases deep) and may change facing at start of move. Base move is " + ManeuverDist + " inches. (May be combined with Moving Fire from starting position.)";
		}

	}
	else if (document.DGKForm.Action.value == "DepFire"){

		// Determine Fire Category
		if (document.DGKForm.Actor.value == "RegInf"){
			if (document.DGKForm.Range.value == 3){
				FireResult(5);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(4);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(3);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else if (document.DGKForm.Actor.value == "IrregInf"){
			if (document.DGKForm.Range.value == 3){
				FireResult(4);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(3);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(2);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else if (document.DGKForm.Actor.value == "VetInf"){
			if (document.DGKForm.Range.value == 3){
				FireResult(6);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(5);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(4);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}

		else if (document.DGKForm.Actor.value == "RegCav"){
			if (document.DGKForm.Range.value == 3){
				FireResult(4);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(3);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(2);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else if (document.DGKForm.Actor.value == "IrregCav"){
			if (document.DGKForm.Range.value == 3){
				FireResult(3);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(2);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(1);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else if (document.DGKForm.Actor.value == "VetCav"){
			if (document.DGKForm.Range.value == 3){
				FireResult(5);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(4);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(3);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else if (document.DGKForm.Actor.value == "MG"){
			if (document.DGKForm.Range.value == 3){
				FireResult(8);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(7);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(6);
			}
			else if (document.DGKForm.Range.value == 18){
				FireResult(5);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else if (document.DGKForm.Actor.value == "Artillery"){
			if (document.DGKForm.Range.value == 3){
				FireResult(9);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(8);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(7);
			}
			else if (document.DGKForm.Range.value == 18){
				FireResult(6);
			}
			else if (document.DGKForm.Range.value == 24){
				FireResult(5);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else if (document.DGKForm.Actor.value == "HvyArtillery"){
			if (document.DGKForm.Range.value == 3){
				FireResult(11);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(10);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(9);
			}
			else if (document.DGKForm.Range.value == 18){
				FireResult(8);
			}
			else if (document.DGKForm.Range.value == 24){
				FireResult(7);
			}
			else if (document.DGKForm.Range.value == 36){
						FireResult(6);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else{
			alert ("Choose another action (unit cannot fire).");
			return;
		}

	}
	else if (document.DGKForm.Action.value == "AdvFire"){

		// Determine Fire Category
		if (document.DGKForm.Actor.value == "RegInf"){
			if (document.DGKForm.Range.value == 3){
				FireResult(3);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(2);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(1);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else if (document.DGKForm.Actor.value == "IrregInf"){
			if (document.DGKForm.Range.value == 3){
				FireResult(2);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(1);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(0);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else if (document.DGKForm.Actor.value == "VetInf"){
			if (document.DGKForm.Range.value == 3){
				FireResult(4);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(3);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(2);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}

		else if (document.DGKForm.Actor.value == "RegCav"){
			if (document.DGKForm.Range.value == 3){
				FireResult(2);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(1);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(0);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else if (document.DGKForm.Actor.value == "IrregCav"){
			if (document.DGKForm.Range.value == 3){
				FireResult(1);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(0);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(-1);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else if (document.DGKForm.Actor.value == "VetCav"){
			if (document.DGKForm.Range.value == 3){
				FireResult(3);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(2);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(1);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else if (document.DGKForm.Actor.value == "MG"){
			if (document.DGKForm.Range.value == 3){
				FireResult(6);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(5);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(4);
			}
			else if (document.DGKForm.Range.value == 18){
				FireResult(3);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else if (document.DGKForm.Actor.value == "Artillery"){
			if (document.DGKForm.Range.value == 3){
				FireResult(7);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(6);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(5);
			}
			else if (document.DGKForm.Range.value == 18){
				FireResult(4);
			}
			else if (document.DGKForm.Range.value == 24){
				FireResult(3);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else if (document.DGKForm.Actor.value == "HvyArtillery"){
			if (document.DGKForm.Range.value == 3){
				FireResult(9);
			}
			else if (document.DGKForm.Range.value == 6){
				FireResult(8);
			}
			else if (document.DGKForm.Range.value == 12){
				FireResult(7);
			}
			else if (document.DGKForm.Range.value == 18){
				FireResult(6);
			}
			else if (document.DGKForm.Range.value == 24){
				FireResult(5);
			}
			else if (document.DGKForm.Range.value == 36){
				FireResult(4);
			}
			else{
				alert ("Choose another action (target is out of range).");
				return;
			}
		}
		else{
			alert ("Choose another action (unit cannot fire).");
			return;
		}

	}
	else if (document.DGKForm.Action.value == "Assault"){

		if (document.DGKForm.Actor.value == "Artillery" || document.DGKForm.Actor.value == "HvyArtillery" || document.DGKForm.Actor.value == "MG" || document.DGKForm.Actor.value == "Vehicle"){

			alert("Artillery, vehicles/Transport, and Machine Guns cannot make assaults. Please choose a valid action.");
			return;
		}
	// Check orders and movement
		Confusion();

		if (OrdersLost == true){

			alert("Orders have been lost. Unit may fire, but may not assault, change formation/facing, or move this turn.");
			return;
		}
		else if (OrdersDelayed == true){

			 var ChargeDistance = confirm("Orders have been delayed in transmission. Is the target of the assault within range (6 inches base movement for cavalry charging in the open; 3 inches base movement for all other cases)?");

			 if (ChargeDistance == false){

					alert("Move forward up to 3 inches (6 inches for cavalry charging in the open), stopping at least 1 inch short of the target, and retain position and facing. The assault may be continued on the following turn if desired and still possible.");
					return;
			}
		}
	//Check if assault is a flank or defender demoralized

		var DefDemoralized = false;
		var AttDisordered = false;
		if (document.DGKForm.ActorDisordered.checked == true){
				AttDisordered = true;
		}

		if (document.DGKForm.TargetDisordered.checked == true) {

			DefDemoralized = confirm("Is the target of the assault demoralized?");
		}

		var IsFrontal = false;
		if (document.getElementById("r3").checked == false && DefDemoralized == false && document.DGKForm.Target.value != "Vehicle"){
			IsFrontal = confirm ("Will the assault cross the target's arc of fire?");
		}
		// Do defensive fire unless in March column or being flanked, or if demoralized

		if (DefDemoralized == false && IsFrontal == true && document.getElementById("r3").checked == false){

		// Do defensive fire

		// Determine base points
		var DefFirebase = 0;

			if (document.getElementById("r2").checked == true){
				// Moving fire
				if (document.DGKForm.Target.value == "RegInf"){
					DefFirebase = 3;
				}
				else if (document.DGKForm.Target.value == "IrregInf"){
					DefFirebase = 2;
				}
				else if (document.DGKForm.Target.value == "VetInf"){
					DefFirebase = 4;
				}
				else if (document.DGKForm.Target.value == "RegCav"){
					DefFirebase = 2;
				}
				else if (document.DGKForm.Target.value == "IrregCav"){
					DefFirebase = 1;
				}
				else if (document.DGKForm.Target.value == "VetCav"){
					DefFirebase = 3;
				}
				else if (document.DGKForm.Target.value == "MG"){
					DefFirebase = 6;
				}
				else if (document.DGKForm.Target.value == "Artillery"){
					DefFirebase = 7;
				}
				else if (document.DGKForm.Target.value == "HvyArtillery"){
					DefFirebase = 6;
				}

			}
			else{
				// Deployed fire
				if (document.DGKForm.Target.value == "RegInf"){
					DefFirebase = 5;
				}
				else if (document.DGKForm.Target.value == "IrregInf"){
					DefFirebase = 4;
				}
				else if (document.DGKForm.Target.value == "VetInf"){
					DefFirebase = 6;
				}
				else if (document.DGKForm.Target.value == "RegCav"){
					DefFirebase = 4;
				}
				else if (document.DGKForm.Target.value == "IrregCav"){
					DefFirebase = 3;
				}
				else if (document.DGKForm.Target.value == "VetCav"){
					DefFirebase = 5;
				}
				else if (document.DGKForm.Target.value == "MG"){
					DefFirebase = 8;
				}
				else if (document.DGKForm.Target.value == "Artillery"){
					DefFirebase = 9;
				}
				else if (document.DGKForm.Target.value == "HvyArtillery"){
					DefFirebase = 8;
				}
			}
			// Modifications
				// Target Formation mods - attacker always in maneuver


					DefFirebase = DefFirebase + 1;

				// Disorder mods

				if (document.DGKForm.TargetDisordered.checked == true){

					DefFirebase = DefFirebase - 1;
				}
				if (document.DGKForm.ActorDisordered.checked == true){

						DefFirebase = DefFirebase + 1;
				}

				// Outnumbered mods

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

						DefFirebase = DefFirebase + 1;
				}
				else if (document.getElementById("ActBadlyOut").checked == true){

						DefFirebase = DefFirebase + 2;
				}
				else if (document.getElementById("TargetOut").checked == true){

						DefFirebase = DefFirebase - 1;
				}
				else if (document.getElementById("TargetBadlyOut").checked == true){

						DefFirebase = DefFirebase - 2;
				}

				// Quality mods

				if (document.DGKForm.Actor.value == "VetInf" || document.DGKForm.Actor.value == "VetCav"){

					DefFirebase = DefFirebase - 1;
				}
				else if (document.DGKForm.Actor.value == "IrregInf" || document.DGKForm.Actor.value == "IrregCav"){

						DefFirebase = DefFirebase + 1;
				}



				// Determine Results of Fire
				var FireRoll = Roll(6);

				if (FireRoll == 1){
					DefFirebase = DefFirebase - 2;
				}
				else if (FireRoll == 2){
					DefFirebase = DefFirebase - 1;
				}
				else if (FireRoll <= 4){
					DefFirebase = DefFirebase;
				}
				else if (FireRoll == 5){
					DefFirebase = DefFirebase + 1;
				}
				else if (FireRoll <=6){
					DefFirebase = DefFirebase + 2;
				}

				var DefFireResult = DefFirebase - 4;
				DefFireResult = Math.round(DefFireResult/2);

				if (DefFireResult <= 0){

					alert("Defensive fire has little or no effect. ");
				}
				else{
					alert("Defensive fire inflicts " + DefFireResult + " levels of damage on the assaulting unit, with immediate effect.");
					AttDisordered = true;
					if (DefFireResult <= 2){
						var Stalled = confirm("Will the assaulting unit suffer a loss of one or more bases from defensive fire?");
					}
					else{
						var Stalled = true;
					}
					if (Stalled == true){
						alert("The assaulting unit will fall back to its starting point, demoralized. The assault does not go home.");
						return;
					}
				}

		}
		else{
				ResultString = "There is no significant defensive fire.\n\n";
		}

		// Do combat

		//Calculate mods
			var AttMods = 0;
			var DefMods = 0;


		//Odds
		if (document.getElementById("ActBadlyOut").checked == true){ DefMods = DefMods + 2;}
		else if (document.getElementById("ActOut").checked == true){ DefMods = DefMods + 1;}
		else if (document.getElementById("TargetOut").checked == true){AttMods = AttMods + 1;}
		else if (document.getElementById("TargetBadlyOut").checked == true){AttMods = AttMods + 2;}

		// Quality
		if (document.DGKForm.Actor.value == "VetInf" || document.DGKForm.Actor.value == "VetCav"){
			AttMods = AttMods + 1;
		}
		if (document.DGKForm.Target.value == "VetInf" || document.DGKForm.Target.value == "VetCav"){
			DefMods = DefMods + 1;
		}
		if (document.DGKForm.Actor.value == "IrregInf" || document.DGKForm.Actor.value == "IrregCav"){
			AttMods = AttMods - 1;
		}
		if (document.DGKForm.Target.value == "IrregInf" || document.DGKForm.Target.value == "IrregCav"){
			DefMods = DefMods - 1;
		}

		// Disorder
		if (AttDisordered == true){ AttMods = AttMods - 1; }
		if (document.DGKForm.TargetDisordered.checked == true){ DefMods = DefMods -1; }

		// Type
		// Cavalry - good in open, not so much elsewhere
		if (document.DGKForm.Actor.value == "RegCav" || document.DGKForm.Actor.value == "IrregCav" || document.DGKForm.Actor.value == "VetCav"){
			if (document.DGKForm.Cover.value == "None"){
				AttMods = AttMods + 2;
			}
			else{
				AttMods = AttMods - 1;
			}
		}
		if (document.DGKForm.Target.value == "RegCav" || document.DGKForm.Target.value == "IrregCav" || document.DGKForm.Target.value == "VetCav"){
			if (document.DGKForm.Cover.value == "None"){
				DefMods = DefMods + 2;
			}
			else{
				DefMods = DefMods - 1;
			}
		}
		// Artillery, vehicles, and MGs suck in close combat
		if (document.DGKForm.Target.value == "MG" || document.DGKForm.Target.value == "Artillery" || document.DGKForm.Target.value == "HvyArtillery" || document.DGKForm.Target.value == "Vehicle"){
			DefMods = DefMods - 2;
		}

		// Cover
		if (document.DGKForm.Cover.value == "Soft"){
			DefMods = DefMods + 1;
		}
		else if (document.DGKForm.Cover.value == "Hard"){
			DefMods = DefMods + 2;
		}
		else if (document.DGKForm.Cover.value == "Fortification"){
			DefMods = DefMods + 3;
		}

		// Formation
		if (document.getElementById("r2").checked == true){
			DefMods = DefMods - 1;
		}
		else if (document.getElementById("r3").checked == true){
			DefMods = DefMods - 2;
		}

		// Rolls
		var AttCombatRoll = Roll(6);
		var DefCombatRoll = Roll(6);

		AttCombatRoll = AttCombatRoll + AttMods;
		DefCombatRoll = DefCombatRoll + DefMods;


		var Outcome = AttCombatRoll - DefCombatRoll;

		ResultString = ResultString + "There is a struggle at close quarters.\n\n";

		if (Outcome == 0){
			ResultString = ResultString + "Attacker and Defender both immediately take 2 levels of damage. Attacker is thrown back a full move (6 inches for infantry, 12 for cavalry).";

		}
		else if (Outcome == 1){
			ResultString = ResultString + "Attacker suffers 1 level of damage. Defender suffers 2 levels of damage. These are inflicted immediately. Defender is thrown back a full move (12 inches for cavalry, 6 inches for others.) Attacker occupies the position.";

		}
		else if (Outcome >= 2){
			Outcome = Math.round(Outcome/2);

			ResultString = ResultString + "Defender immediately suffers " + Outcome + " level(s) of damage. They are thrown back a full move (12 inches for cavalry, 6 inches for others.) Attacker occupies the position.";

		}
		else if (Outcome == -1){
			ResultString = ResultString + "Attacker suffers 2 levels of damage. Defender suffers 1 level of damage. These are inflicted immediately. Attacker is thrown back a full move (12 inches for cavalry, 6 inches for others.) Defender holds the position.";

		}
		else if (Outcome <= -2){
			Outcome = Math.abs(Outcome);
			Outcome = Math.round(Outcome/2);

			ResultString = ResultString + "The Attacker immediately takes " + Outcome + " level(s) of damage. They are thrown back a full move (6 inches for infantry, 12 for cavalry). Defender holds the position.";


		}

	}
	else if (document.DGKForm.Action.value == "March"){
		Confusion();

		if (OrdersLost == true){

			alert("Orders have been lost. Unit may fire, but may not change formation/facing or move this turn.");
			return;
		}
		else if (OrdersDelayed == true){

			ResultString = ResultString + "Orders have been delayed in transmission. ";
		}

		var ManeuverDist = 0;

		if (document.DGKForm.Actor.value == "RegInf" || document.DGKForm.Actor.value == "Artillery"|| document.DGKForm.Actor.value == "MG"){

			ManeuverDist = 12;
		}
		if (document.DGKForm.Actor.value == "HvyArtillery"){

			ManeuverDist = 8;
		}
		else if (document.DGKForm.Actor.value == "IrregInf"){

			ManeuverDist = 10;
		}
		else if (document.DGKForm.Actor.value == "VetInf"){

			ManeuverDist = 14;
		}
		else if (document.DGKForm.Actor.value == "IrregCav"){

			ManeuverDist = 20;
		}
		else if (document.DGKForm.Actor.value == "RegCav"){

			ManeuverDist = 24;
		}
		else if (document.DGKForm.Actor.value == "VetCav"){

			ManeuverDist = 28;
		}
		else if (document.DGKForm.Actor.value == "Vehicle"){

				ManeuverDist = 16;
		}

	// Adjust for delayed orders
		if (OrdersDelayed == true){

			ManeuverDist = ManeuverDist * .5;
		}

		if (document.DGKForm.Actor.value == "Vehicle"){
			ResultString = ResultString + "Unit will assume march formation and may change facing at start of move if applicable. Base move is " + ManeuverDist + " inches for fast vehicles/transport and " + (ManeuverDist/2) + " inches for slow vehicles/transport.";
		}
		else{
			ResultString = ResultString + "Unit will assume march formation (one base in width) and may change facing at start of move. Base move is " + ManeuverDist + " inches.";
		}


	}
	else if (document.DGKForm.Action.value == "Regroup"){

		var RallyRoll = Roll(100);

			// Modify roll
			var LeaderResp = confirm("Is there a commanding general within 6 inches?");
			if (LeaderResp == true){
				RallyRoll = RallyRoll + 15;
			}

		if (document.DGKForm.Actor.value == "VetInf" || document.DGKForm.Actor.value == "VetCav"){
			RallyRoll = RallyRoll + 10;
		}
		else if (document.DGKForm.Actor.value == "IrregInf" || document.DGKForm.Actor.value == "IrregCav"){
			RallyRoll = RallyRoll - 10;
		}

		// Determine success

		if (RallyRoll <= 10){

			ResultString = "The units fails to rally and abandons the field. It is considered destroyed and must be removed from play.";
		}

		else if (RallyRoll <= 20){

			ResultString = "The unit fails to regroup (further attempts may be made on subsequent turns).";
		}
		else if (RallyRoll <= 40){

			ResultString = "The unit regroups one level, from demoralized to disordered, or from disordered to OK. Retain formation and facing.";
		}
		else {

			ResultString = "The unit regroups, and is no longer demoralized or disordered. They may assume any desired formation/facing.";
		}

	}

	alert(ResultString);
	ResultString = "";

}

