/*
>--------------------------------------------------------------------------------<
| CREDITS INFORMATION															 |
>--------------------------------------------------------------------------------<
| Created By: Benny McLennan													 |
| File Name: armyAJAX.js														 |
| File Location: javascripts/ajax												 |
| Generic Description: All mainly used AJAX methods for the BMF Generic Framework|
| Date Created: May 20th 2009													 |
| Date Edited: N/A																 |
| Copyright: © Army Roster 2008 - 2009											 |
>--------------------------------------------------------------------------------<
| Website: http://www.armyroster.com											 |
| Website Restriction: Level 0 													 |
| Important Notes: Required on every page of the website. Website runs off AJAX  |
| Date Installed: May 27th 2009	 												 |
| Date Modified: N/A															 |
|																				 |
| Notes: If you wish to use this script, please feel free. No permission is 	 |
|        required. We would, however, like a brief email notifying us of your use|
| 		 of our script(s). 														 |
| 						 														 |
|		 Thank you,																 |
|		 Benny																	 |
>--------------------------------------------------------------------------------<
*/


function armylist(action, id, id2, title) {
	$("#NewUploadError").html("")
	$("#UploadError").html("")
	$("#NewUploadError").css("padding","0px");
	$("#UploadError").css("padding","0px");

	var errors = '';
	var verrors = '';
	var noErrors = true;
	var Send = '';
	if (action === 'create') {
		if (($("#Army").val() == '') || ($("#Army").val() == 'Army Name')) {
			verrors = verrors + "<br />You must select an Army Name.";
			noErrors = false;
		} else {
			var army = $("#Army").val();
		}
		if ($("#Faction").val() > 0) {
			var faction = $("#Faction").val();
		} else {
			verrors = verrors + "<br />You must select a Faction from the dropdown.";
			$("#CreatePoints").html("<select name=\"PointLimitID\" id=\"PointLimitID\" style=\"width: 250px\"><option value=\"0\">Please select a Faction</option></select>");
			noErrors = false;
		}
		if ($("#PointLimitID").val() > 0) {
			var point = $("#PointLimitID").val();
		} else {
			verrors = verrors + "<br />You must select a Point Limit from the dropdown.";
			noErrors = false;
		}

		if (noErrors == true) {
			Send = "Action=" + action + "&FactionID=" + $("#Faction").val() + "&Army=" + encodeURIComponent(army) + "&PointLimit=" + point;
		}
	}

	if (action === 'getpoints') {
		if ($("#Faction").val() > 0) {
			Send = "Action=" + action + "&FactionID=" + $("#Faction").val();
		} else {
			verrors = "<br />No Faction has been selected.";
			$("#CreatePoints").html("<select name=\"PointLimitID\" id=\"PointLimitID\" style=\"width: 250px\"><option value=\"0\">Please select a Faction</option></select>");
			noErrors = false;
		}
	}
	if (action === 'duplicate') {
		if (id > 0) {
			Send = "Action=" + action + "&ArmyID=" + id;
		} else {
			verrors = "<br />No Army has been selected.";
			noErrors = false;
		}
	}
	if (action === 'delete') {
		if (id > 0) {
			var deleteme = confirm("Are you sure you want to delete this Army List?");
			if (deleteme == true) {
				var Send = "Action=" + action + "&ArmyID=" + id;
			} else {
				noErrors = false;
				var vnoErrors = true;
			}
		} else {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />No Army List has been selected.");
			$("#UploadError").css("padding","5px");
			noErrors = false;
		}
	}
	if (action === 'deleteunit') {
		if (id > 0) {
			var deleteme = confirm("Are you sure you want to delete this Unit?");
			if (deleteme == true) {
				var Send = "Action=" + action + "&ArmyUnitID=" + id + "&ArmyID=" + id2;
			} else {
				noErrors = false;
				var vnoErrors = true;
			}
		} else {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />No Unit has been selected.");
			$("#UploadError").css("padding","5px");
			noErrors = false;
		}
	}
	if (action === 'editunit') {
		if (id > 0) {
			var Send = "Action=" + action + "&ArmyUnitID=" + id + "&ArmyID=" + id2;
		} else {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />No Unit has been selected.");
			$("#UploadError").css("padding","5px");
			noErrors = false;
		}
	}
	if (action === 'purge') {
		if (id > 0) {
			var deleteme = confirm("Are you sure you want to purge this Army List? All of the units and options given to those units will be deleted from your list.");
			if (deleteme == true) {
				var Send = "Action=" + action + "&ArmyID=" + id;
			} else {
				noErrors = false;
				var vnoErrors = true;
			}
		} else {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />No Army List has been selected.");
			$("#UploadError").css("padding","5px");
			noErrors = false;
		}
	}

	if (noErrors == true) {
		$.ajax({
		beforeSend: loadingScript(),
		type: "POST",
		url: "validatescripts/builder/ajax/armylist.inc.php",
		data: Send,
		success: function (responseText) 
			{
				if (action === 'getpoints') {
					if (responseText == 2) {
						$("#CreatePoints").html("<select name=\"PointLimitID\" id=\"PointLimitID\" style=\"width: 250px\"><option value=\"0\">Please select a Faction</option></select>");
						var aerror = "<br />No Faction has been selected.";
					} else if (responseText == 3) {
						$("#CreatePoints").html("<select name=\"PointLimitID\" id=\"PointLimitID\" style=\"width: 250px\"><option value=\"0\">Please select a Faction</option></select>");
						var aerror = "<br />That Faction does not have any Point Limits set. Please see the \"Factions\" section to add point limits for your factions.";
					} else {
						$("#CreatePoints").html(responseText);
					}
					if ((responseText > 0) && (responseText < 12)) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#UploadError").css("padding","5px");
					}
				} else if ((action === 'duplicate') || (action === 'delete')) {
					if (responseText == 1) {
						newhash('action=armylists&Area=overview');
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + responseText);
						$("#UploadError").css("padding","5px");
					}
				} else if (action === 'purge') {

					if (responseText.length < 150) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + responseText);
						$("#UploadError").css("padding","5px");
					} else {
						$("#armycontent" + id).html(responseText);
					}
				} else if (action === 'deleteunit') {
					if (responseText.length < 50) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + responseText);
						$("#UploadError").css("padding","5px");
					} else {
						
						var typeid = $("#UnitType" + id2).val();
						var unitid = $("#Unit" + id2).val();
						var unitselectblock = $("#newunitblock" + id2).html();
						$("#armycontent" + id2).html(responseText);
						$("#newunitblock" + id2).html(unitselectblock);
						$("#UnitType" + id2).val(typeid);
						$("#Unit" + id2).val(unitid);
						
					}
				} else if (action === 'editunit') {

					if (responseText.length < 50) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + responseText);
						$("#UploadError").css("padding","5px");
					} else {
						$('#dialogprofile').dialog('destroy');		
						$("#dialogprofile").html(responseText);
						$("#dialogprofile").attr("title", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Edit Unit: " + title);
						$('#dialogprofile').dialog('open');			
						$("#dialogprofile").dialog({
							bgiframe: false,
							closeOnEscape: true,
							draggable: true,
							resizable: false,
							width: 632,
							modal: true,
						});
					}
				} else if (action === 'create') {
					if (responseText == 1) {
						newhash('action=armylists&Area=overview');
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + responseText);
						$("#UploadError").css("padding","5px");
					}
				}
				loadedScript();
			}
		});
	} else {
		if (vnoErrors == true) {
			
		} else {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + verrors + "</span>");
			$("#UploadError").css("padding","5px");
			noErrors = true;	
		}
	}
}
function getunits(action, id, fid, rehash, forover) {
	$("#NewUploadError").html("")
	$("#UploadError").html("")
	$("#NewUploadError").css("padding","0px");
	$("#UploadError").css("padding","0px");

	var errors = '';
	var verrors = '';
	var noErrors = true;
	var Send = '';
	if (action === 'units') {
		if (id > 0) {
			if ($("#UnitType" + id).val() > 0) {
				Send = "Action=" + action + "&FactionID=" + fid + "&UnitTypeID=" + $("#UnitType" + id).val() + "&ArmyID=" + id;
			} else {
				errors = "<br />You must select a Unit Type from the dropdown list.";
				noErrors = false;
			}
		} else {
			errors = "<br />No Faction has been selected.";
			noErrors = false;
		}
	}
	if ((action === 'saveunits') || (action === 'savecloseunits')) {
		if (fid > 0) {
			if (id > 0) {
				if (($("#Custom").val() == '') || ($("#Custom").val() == 'Custom Name')) {
					var customname = '';
				} else {
					var customname = $("#Custom").val();
				}
				Send = "Action=" + action + "&ArmyUnitID=" + fid + "&ArmyID=" + id + "&Items=" + encodeURIComponent($("#selecteditems").val()) + "&Cost=" + $("#unitcosttotal").val() + "&OldCost=" + $("#oldunitcosttotal").val() + "&Quantity=" + $("#NewQuantity").val() + "&OldQuantity=" + $("#oldQuantity").val() + "&CustomName=" + customname;
			} else {
				errors = "<br />No Army has been selected.";
				noErrors = false;
			}
		} else {
			errors = "<br />No Unit has been selected.";
			noErrors = false;
		}
	}
	if (action === 'closeunits') {
		$('#dialogprofile').dialog('close');			
		$('#dialogprofile').dialog('destroy');		
		$("#dialogprofile").html("");
	}
	

	if (action === 'changequantity') {
		if ($("#NewQuantity").val() > 0) {
			var mini = $("#MinQuan").val() * 1;
			var maxi = $("#MaxQuan").val() * 1;
			var quan = $("#NewQuantity").val() * 1;
			var verrors = '';
			if (quan < mini) {
				verrors = "<br />Your Quantity must be higher that the Quantity Limit minimum.";
				$("#NewQuantity").val($("#prevq").val());
				noErrors = false;
			}
			if (quan > maxi) {
				
				verrors = "<br />Your Quantity must be lower that the Quantity Limit maximum.";
				$("#NewQuantity").val($("#prevq").val());
				noErrors = false;
			}
			if (noErrors == true) {
				var forhash = $("#prevq").val();
				if ($("#previousitemsselected").val() == '') {
					
				} else {
					var ids = $("#previousitemsselected").val().split("&&&");
					for (var i = 0; i < ids.length; i++) {
						var newids = ids[i].split("*");
						nid = newids[0];
						nfid = newids[1];
						getunits("additemmulti", nid, nfid, forhash);
					}
				}
				
				var unitcosttotal = $("#unitcosttotal").val();
				unitcosttotal = unitcosttotal.replace("pts", "");
				unitcosttotal = unitcosttotal - $("#prev").val();
				unitcosttotal = unitcosttotal * 1;
				var responseText = quan * $("#basepoints").val();
				responseText = responseText * 1;
				unitcosttotal += responseText;
				$("#unitcosttotal").val(unitcosttotal + "pts");
				$("#prev").val(responseText);
				$("#prevq").val(quan);
			}
		} else {
			verrors = "<br />You must enter a value greater than 0 for your Quantity.";
			noErrors = false;
			$("#NewQuantity").val($("#prevq").val());
		}
		if (noErrors == false) {
			
		} else {
			noErrors = null;
		}
	}
	if (action === 'additemmulti') {
		noErrors = null;
		if (rehash > 0) {
			if ($("#multiply" + id + fid).val() == 'Q') {
				var price = $("#price" + id + fid).val() * rehash;
			} else if ($("#multiply" + id + fid).val() == 'S') {
				var price = $("#price" + id + fid).val() * 1;
			}
			price = price * 1;
			var oldcost = $("#unitcosttotal").val().replace("pts", "");
			var oldcost = oldcost * 1;
			oldcost -= price;
			$("#unitcosttotal").val(oldcost + "pts")
		}
		if (forover == 2) {
			if ($("#itemgroup" + id).val() > 0) {
				if ($("#itemgroupprev" + id).val() == 0) {
					var price = $("#hidselprice" + id + $("#itemgroup" + id).val()).val() * 1;
					$("#itemgroupprev" + id).val(id + "*" + $("#itemgroup" + id).val());
				} else {
					var itemgroupprev = $("#itemgroupprev" + id).val().replace("*", "");
					var price = (($("#hidselprice" + id + $("#itemgroup" + id).val()).val() * 1) - ($("#hidselprice" + itemgroupprev).val()));
					$("#itemgroupprev" + id).val(id + "*" + $("#itemgroup" + id).val());
				}
			} else if ($("#itemgroup" + id).val() == 0) {
				var itemgroupprev = $("#itemgroupprev" + id).val().replace("*", "");
				var price = $("#hidselprice" + itemgroupprev).val() * 1;
				$("#itemgroupprev" + id).val(0);
			}
		} else {
			if ($("#quanoverwrite" + id + fid).val() == 1) {
				var price = $("#price" + id + fid).val() * $("#quanoverwriteamount" + id + fid).val();
				var oldprice = $("#price" + id + fid).val() * $("#quanoverwriteprev" + id + fid).val();
				$("#quanoverwriteprev" + id + fid).val($("#quanoverwriteamount" + id + fid).val());
			} else {
				if ($("#multiply" + id + fid).val() == 'Q') {
					var price = $("#price" + id + fid).val() * $("#NewQuantity").val();
				} else if ($("#multiply" + id + fid).val() == 'S') {
					var price = $("#price" + id + fid).val() * 1;
				}
			}
		}
		if (forover == 2) {
			if ($("#itemgroup" + id).val() > 0) {
				
				/*if (rehash > 0) {
				} else {
					if ($("#previousitemsselected").val() == '') {
						$("#previousitemsselected").val(id + "*" + $("#itemgroup" + id).val());
					} else {
						$("#previousitemsselected").val($("#previousitemsselected").val() + "&&&" + id + "*" + $("#itemgroup" + id).val());
					}
				}*/
				
				var unitcosttotal = $("#unitcosttotal").val();
				unitcosttotal = unitcosttotal.replace("pts", "");
				unitcosttotal = unitcosttotal * 1;
				price = price * 1;
				unitcosttotal += price;
				$("#unitcosttotal").val(unitcosttotal + "pts");
				$("#previousselecteddrop" + id).val($("#itemgroup" + id).val())
				
				if ($("#selecteditems").val() == '') {
					$("#selecteditems").val($("#itemgroup" + id).val());
				} else {
					var origselected = $("#selecteditems").val();
					var preselecteditems = $("#selecteditems").val().split("&&&");
					for (var i = 0; i < preselecteditems.length; i++) {
						if (preselecteditems[i] == $("#itemgroup" + id).val()) {
						} else {
							$("#selecteditems").val(origselected + "&&&" + $("#itemgroup" + id).val());
						}
					}
				}
				
				
			} else if ($("#itemgroup" + id).val() == 0) {
				
				/*var ids = $("#previousitemsselected").val().split("&&&");
				var itemsselected = '';
				for (var i = 0; i < ids.length; i++) {
					if (ids[i] == $("#itemgroupprev" + id).val()) {
					} else {
						if (itemsselected == '') {
							itemsselected = ids[i];
						} else {
							itemsselected = itemsselected + "&&&" + ids[i];
						}
					}
				}
				$("#previousitemsselected").val(itemsselected);*/
				
				var unitcosttotal = $("#unitcosttotal").val();
				unitcosttotal = unitcosttotal.replace("pts", "");
				unitcosttotal = unitcosttotal * 1;
				price = price * 1;
				unitcosttotal -= price;
				$("#unitcosttotal").val(unitcosttotal + "pts");
				if ($("#selecteditems").val() == '') {
				} else {
					var origselected = $("#selecteditems").val();
					var preselecteditems = $("#selecteditems").val().split("&&&");
					var newselected = '';
					for (var i = 0; i < preselecteditems.length; i++) {
						if (preselecteditems[i] == $("#previousselecteddrop" + id).val()) {
						} else {
							if (i == 0) {
								newselected = preselecteditems[i];
							} else {
								newselected = newselected + "&&&" + preselecteditems[i];
							}
						}
					}
					if (newselected != '') {
						$("#selecteditems").val(newselected);
					}
				}
				
			}
		} else {
			if ($("#" + id + fid).attr('checked') == true) {
				if ($("#quanoverwrite" + id + fid).val() == 1) {
					if ($("#quanoverwriteamount" + id + fid).css("display") == "none") {
						$("#quanoverwriteamount" + id + fid).css("display", "block");
					}
				}
				
				if (rehash > 0) {
				} else {
					if ($("#previousitemsselected").val() == '') {
						$("#previousitemsselected").val(id + "*" + fid);
					} else {
						$("#previousitemsselected").val($("#previousitemsselected").val() + "&&&" + id + "*" + fid);
					}
				}
				
				var unitcosttotal = $("#unitcosttotal").val();
				unitcosttotal = unitcosttotal.replace("pts", "");
				unitcosttotal = unitcosttotal * 1;
				if (forover == 1) {
					if ($("#quanoverwrite" + id + fid).val() == 1) {
						unitcosttotal = unitcosttotal - (oldprice * 1);
					}
				}
				price = price * 1;
				unitcosttotal += price;
				$("#unitcosttotal").val(unitcosttotal + "pts");
				
				if ($("#selecteditems").val() == '') {
					$("#selecteditems").val(fid);
				} else {

					if ($("#quanoverwriteamount" + id + fid).css("display") == "block") {
						
						var origselected = $("#selecteditems").val();
						var preselecteditems = $("#selecteditems").val().split("&&&");
						var newselected = '';
						var used = 0;
						for (var i = 0; i < preselecteditems.length; i++) {
							if (preselecteditems[i] > 0) {
								if (preselecteditems[i] == fid) {
								} else {
									if (i == 0) {
										newselected = preselecteditems[i];
									} else {
										newselected = newselected + "&&&" + preselecteditems[i];
									}
								}
							} else {
								var multiselect = preselecteditems[i].split("-");
								if (multiselect[0] == fid) {
									used = 1;
									if (i == 0) {
										newselected = multiselect[0] + "-" + $("#quanoverwriteamount" + id + fid).val();
									} else {
										newselected = newselected + "&&&" + multiselect[0] + "-" + $("#quanoverwriteamount" + id + fid).val();
									}
								} else {
									if (i == 0) {
										newselected = preselecteditems[i];
									} else {
										newselected = newselected + "&&&" + preselecteditems[i];
									}
								}
							}
						}
						if (used == 0) {
							newselected = origselected + "&&&" + fid + "-" + $("#quanoverwriteamount" + id + fid).val();
						}
						if (newselected != '') {
							$("#selecteditems").val(newselected);
						}
								
					} else {
						var origselected = $("#selecteditems").val();
						var preselecteditems = $("#selecteditems").val().split("&&&");
						for (var i = 0; i < preselecteditems.length; i++) {
							if (preselecteditems[i] == fid) {
							} else {
								$("#selecteditems").val(origselected + "&&&" + fid);
							}
						}
						
					}
				}
			} else if ($("#" + id + fid).attr('checked') == false) {
				if ($("#quanoverwrite" + id + fid).val() == 1) {
					if ($("#quanoverwriteamount" + id + fid).css("display") == "block") {
						$("#quanoverwriteamount" + id + fid).css("display", "none");
					}
				}
	
				
				var ids = $("#previousitemsselected").val().split("&&&");
				var itemsselected = '';
				for (var i = 0; i < ids.length; i++) {
					
					if (ids[i] == (id + "*" + fid)) {
					} else {
						if (itemsselected == '') {
							itemsselected = ids[i];
						} else {
							itemsselected = itemsselected + "&&&" + ids[i];
						}
					}
				}
				$("#previousitemsselected").val(itemsselected);
				
				var unitcosttotal = $("#unitcosttotal").val();
				unitcosttotal = unitcosttotal.replace("pts", "");
				unitcosttotal = unitcosttotal * 1;
	
				price = price * 1;
				unitcosttotal -= price;
				$("#unitcosttotal").val(unitcosttotal + "pts");
				

				
				
				if ($("#selecteditems").val() == '') {

				} else {
					
					
					if ($("#quanoverwriteamount" + id + fid).css("display") == "none") {
						
						
						var origselected = $("#selecteditems").val();
						var preselecteditems = $("#selecteditems").val().split("&&&");
						var newselected = '';
						var used = 0;
						for (var i = 0; i < preselecteditems.length; i++) {
							if (preselecteditems[i] > 0) {
								if (preselecteditems[i] == fid) {
								} else {
									if (i == 0) {
										newselected = preselecteditems[i];
									} else {
										newselected = newselected + "&&&" + preselecteditems[i];
									}
								}
							} else {
								var multiselect = preselecteditems[i].split("-");
								if (multiselect[0] == fid) {

								} else {
									if (i == 0) {
										newselected = preselecteditems[i];
									} else {
										newselected = newselected + "&&&" + preselecteditems[i];
									}
								}
							}
						}

						if (newselected != '') {
							$("#selecteditems").val(newselected);
						}
								
					} else {
						var origselected = $("#selecteditems").val();
						var preselecteditems = $("#selecteditems").val().split("&&&");
						var newselected = '';
						for (var i = 0; i < preselecteditems.length; i++) {
							if (preselecteditems[i] == fid) {
							} else {
								if (i == 0) {
									newselected = preselecteditems[i];
								} else {
									newselected = newselected + "&&&" + preselecteditems[i];
								}
							}
						}
						if (newselected != '') {
							$("#selecteditems").val(newselected);
						}
					}
				}
			}
		}
	}
	if (action === 'addopen') {
		if (id > 0) {
			if ($("#UnitType" + id).val() > 0) {
				var unittype = $("#UnitType" + id).val();
			} else {
				errors = "<br />You must select a Unit Type from the dropdown list.";
				noErrors = false;
			}
			if ($("#Unit" + id).val() > 0) {
				var unitid = $("#Unit" + id).val();
			} else {
				errors = "<br />You must select a Unit from the dropdown list.";
				noErrors = false;
			}
			if (noErrors == true) {
				Send = "Action=" + action + "&FactionID=" + fid + "&UnitTypeID=" + unittype + "&ArmyID=" + id + "&UnitID=" + unitid;
			}
		}		
	}
	if (action === 'genarmy') {
		if (fid > 0) {
			Send = "Action=" + action + "&ArmyID=" + id;
		}	
	}
	if (noErrors == true) {
		$.ajax({
		beforeSend: loadingScript(),
		type: "POST",
		url: "validatescripts/builder/ajax/armylist.getunits.inc.php",
		data: Send,
		success: function (responseText) 
			{
				if (action === 'units') {
					if (responseText == 2) {
						var aerror = "<br />No Unit Type has been selected.";
					} else if (responseText == 3) {
						var aerror = "<br />That Unit Type does not exist.";
					} else if (responseText == 4) {
						var aerror = "<br />That Faction does not exist.";
					} else if (responseText == 5) {
						var aerror = "<br />Your Unit Type and Faction do not belong to the same Game System.";
					} else if (responseText == 6) {
						var aerror = "<br />That Game System does not exist.";
					} else if (responseText == 7) {
						var aerror = "<br />That Game System does not belong to you.";
					} else {
						$("#selectunitlist" + id).html(responseText);
					}
				} else if (action === 'saveunits') {

					if (responseText == 2) {
						var aerror = "<br />No Unit has been selected.";
					} else if (responseText == 3) {
						var aerror = "<br />That Unit does not exist.";
					} else if (responseText == 4) {
						var aerror = "<br />That Army does not exist.";
					} else if (responseText == 5) {
						var aerror = "<br />That Faction does not exist.";
					} else if (responseText == 6) {
						var aerror = "<br />That Game System does not exist.";
					} else if (responseText == 7) {
						var aerror = "<br />That Game System does not belong to you.";
					} else {
						getunits("genarmy", id, fid);
						alert("You Unit information has been saved.");
					}
					
				} else if (action === 'savecloseunits') {

					if (responseText == 2) {
						var aerror = "<br />No Unit has been selected.";
					} else if (responseText == 3) {
						var aerror = "<br />That Unit does not exist.";
					} else if (responseText == 4) {
						var aerror = "<br />That Army does not exist.";
					} else if (responseText == 5) {
						var aerror = "<br />That Faction does not exist.";
					} else if (responseText == 6) {
						var aerror = "<br />That Game System does not exist.";
					} else if (responseText == 7) {
						var aerror = "<br />That Game System does not belong to you.";
					} else {
						$('#dialogprofile').dialog('close');			
						$('#dialogprofile').dialog('destroy');		
						$("#dialogprofile").html("");
						getunits("genarmy", id, fid);
					}
					
				} else if (action === 'addopen') {
					if (responseText == 2) {
						var aerror = "<br />No Unit Type has been selected.";
					} else if (responseText == 3) {
						var aerror = "<br />No Unit has been selected.";
					} else if (responseText == 4) {
						var aerror = "<br />No Army has been selected.";
					} else if (responseText == 5) {
						var aerror = "<br />That Unit Type does not exist.";
					} else if (responseText == 6) {
						var aerror = "<br />That Faction does not exist.";
					} else if (responseText == 7) {
						var aerror = "<br />Your Unit Type and Faction do not belong to the same Game System.";
					} else if (responseText == 8) {
						var aerror = "<br />That Unit does not exist.";
					} else if (responseText == 9) {
						var aerror = "<br />That Game System does not exist.";
					} else if (responseText == 10) {
						var aerror = "<br />That Army List does not belong to you.";
					} else if (responseText == 11) {
						var aerror = "<br />That Game System does not belong to you.";
					} else {
						$('#dialogprofile').dialog('destroy');		
						$("#dialogprofile").html(responseText);
						$("#dialogprofile").attr("title", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add Unit: " + $("#Unit" + id + " option:selected").text());
						$('#dialogprofile').dialog('open');			
						$("#dialogprofile").dialog({
							bgiframe: false,
							closeOnEscape: true,
							draggable: true,
							resizable: false,
							width: 632,
							modal: true,
						});
						getunits("genarmy", id, fid);
					}
				} else if (action === 'genarmy') {
					if (responseText == 2) {
						var aerror = "<br />No Army has been selected.";
					} else if (responseText == 3) {
						var aerror = "<br />That Army does not exist.";
					} else if (responseText == 4) {
						var aerror = "<br />That Faction not exist.";
					} else if (responseText == 5) {
						var aerror = "<br />That Game System does not exist.";
					} else if (responseText == 6) {
						var aerror = "<br />That Game System does not belong to you.";
					} else {
						var typeid = $("#UnitType" + id).val();
						var unitid = $("#Unit" + id).val();
						var unitselectblock = $("#newunitblock" + id).html();
						$("#armycontent" + id).html(responseText);
						$("#newunitblock" + id).html(unitselectblock);
						$("#UnitType" + id).val(typeid);
						$("#Unit" + id).val(unitid);
					}
				}
				if ((responseText > 0) && (responseText < 12)) {
					$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
					$("#NewUploadError").css("padding","5px");
				}
				loadedScript();
			}
		});
	} else {
		if (noErrors == null) {
			
		} else {
			if (verrors == '') {
				$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + errors + "</span>");
				$("#UploadError").css("padding","5px");
				noErrors = true;	
			} else {
				$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + verrors + "</span>");
				$("#NewUploadError").css("padding","5px");
				noErrors = true;	
			}
		}
	}
}











