/*
>--------------------------------------------------------------------------------<
| CREDITS INFORMATION															 |
>--------------------------------------------------------------------------------<
| Created By: Benny McLennan													 |
| File Name: mainAJAX.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 gamesystem(action, id, span, statID, accordionActive, span2) {
	if (accordionActive >= 0) {
	} else {
		var accordionActive = false;
	}
	var noErrors = true;
	$("#UploadError").html("");
	$("#UploadError").css("padding","0px");
	if (action === 'create') {
		if (($("#GameSystem").val()=="") || ($("#GameSystem").val()=="Game System Name")) {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />Enter a name for your Game System.");
			$("#UploadError").css("padding","5px");
			noErrors = false;
		} else {
			var gameSystem = $("#GameSystem").val();
		}
		var Send = "Action=" + action + "&GameSystem=" + encodeURIComponent(gameSystem);
	}
	if (action === 'delete') {
		if (id > 0) {
			var deleteme = confirm("Are you sure you want to delete this Game System?\n\nBy doing this you will delete ALL relative data. This includes all Army Lists, Factions, Units, and Items. In addition all other information that relates to it such as reviews and comments will be deleted.\n\nThis information will NOT be retrievable.");
			if (deleteme == true) {
				var Send = "Action=" + action + "&ID=" + id;
			} else {
				noErrors = false;
			}
		} else {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />No Game System has been selected.");
			$("#UploadError").css("padding","5px");
			noErrors = false;
		}
	}
	if (action === 'duplicate') {
		if (id > 0) {
			var Send = "Action=" + action + "&ID=" + id;
		} else {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />No Game System has been selected.");
			$("#UploadError").css("padding","5px");
			noErrors = false;
		}
	}
	if (action === 'addstat') {
		var Error = '';
		var Type = $("#Type" + id).val();
		if (($("#NewStat" + id).val()=="") || ($("#NewStat" + id).val()=="Stat / Save")) {
			Error = Error + "<br />You must enter a name for your new Stat / Save";
			noErrors = false;
		} else {
			var stat = $("#NewStat" + id).val();
		}

		if (Type === 'stat') {
			if (($("#Order" + id).val()=="") || ($("#Order" + id).val()=="Order")) {
				Error = Error + "<br />You must enter an order value for your new Stat / Save";
				noErrors = false;
			} else {
				if  ($("#Order" + id).val() > 0) {
					var order = $("#Order" + id).val();
				} else {
					Error = Error + "<br />You must enter an order value for your new Stat / Save that is greater than 0.";
					noErrors = false;
				}
			}
		} else {
			var order = 1;
		}
		if (($("#Min" + id).val()=="") || ($("#Min" + id).val()=="Minimum")) {
			Error = Error + "<br />You must enter a minimum value for your new Stat / Save.";
			noErrors = false;
		} else {
			var minimum = $("#Min" + id).val();
		}
		if (($("#Max" + id).val()=="") || ($("#Max" + id).val()=="Maximum")) {
			Error = Error + "<br />You must enter a maximum value for your new Stat / Save.";
			noErrors = false;
		} else {
			var maximum = $("#Max" + id).val();
		}
		
		if (noErrors == false) {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + Error);
			$("#UploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&ID=" + id + "&Stat=" + stat + "&Order=" + order + "&Minimum=" + minimum + "&Maximum=" + maximum + "&stosa=" + Type;
		}
	}
	if (action === 'addexception') {
		var Error = '';
		
		if (($("#NewExcepton" + id).val()=="") || ($("#NewExcepton" + id).val()=="Exception")) {
			Error = Error + "<br />You must enter a value for your Stat Exception";
			noErrors = false;
		} else {
			var exception = $("#NewExcepton" + id).val();
		}

		if (noErrors == false) {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + Error);
			$("#UploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&ID=" + id + "&Exception=" + encodeURIComponent(exception);
		}
	}
	if (action === 'deleteexception') {
		if (statID > 0) {
			
		} else {
			Error = Error + "<br />You must select a Exception";
			noErrors = false;
		}

		if (noErrors == false) {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + Error);
			$("#UploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&ID=" + id + "&Exception=" + statID;
		}
	}
	if (action === 'saveexception') {
		var Error = '';
		for (var i = 1; i <= span; i++) {

			if ($("#Char" + id + "Num" + i).val()=="") {
				Error = "<br />You must have a value entered for each one of your exceptions.";
				noErrors = false;
			} else {
				if (i == 1) {
					var char = $("#Char" + id + "Num" + i).val();
				} else {
					char = char + "&&&" + $("#Char" + id + "Num" + i).val();
				}
			}

			if ($("#CharID" + id + "Num" + i).val()=="") {
				noErrors = false;
			} else {
				if (i == 1) {
					var charid = $("#CharID" + id + "Num" + i).val();
				} else {
					charid = charid + "&&&" + $("#CharID" + id + "Num" + i).val();
				}
			}

		}

		if (noErrors == false) {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + Error);
			$("#UploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&ID=" + id + "&Span=" + span + "&CharID=" + encodeURIComponent(charid) + "&Char=" + encodeURIComponent(char);
		}
	}
	if (action === 'savechanges') {
		var Error = '';
		var OrderError = '';
		var StatError = '';
		var MinError = '';
		var MaxError = '';
		if ($("#GameSystem" + id).val()=="") {
			GSError = "<br />You must have a name for your Game System.";
			noErrors = false;
		} else {
			var gamesystem = $("#GameSystem" + id).val();
		}
		for (var i = 0; i < span; i++) {
			if ($("#Order" + id + "Num" + i).val()=="") {
				OrderError = "<br />You must have a value entered for each one of your orders.";
				noErrors = false;
			} else {
				if (i == 0) {
					var order = $("#Order" + id + "Num" + i).val();
				} else {
					order = order + "&&&" + $("#Order" + id + "Num" + i).val();
				}
			}

			if ($("#Stat" + id + "Num" + i).val()=="") {
				StatError = "<br />You must have a value entered for each one of your stats.";
				noErrors = false;
			} else {
				if (i == 0) {
					var stat = $("#Stat" + id + "Num" + i).val();
				} else {
					stat = stat + "&&&" + $("#Stat" + id + "Num" + i).val();
				}
			}

			if ($("#Min" + id + "Num" + i).val()=="") {
				MinError = "<br />You must have a value entered for each one of your minimum stat values.";
				noErrors = false;
			} else {
				if (i == 0) {
					var minimum = $("#Min" + id + "Num" + i).val();
				} else {
					minimum = minimum + "&&&" + $("#Min" + id + "Num" + i).val();
				}
			}
			if ($("#Max" + id + "Num" + i).val()=="") {
				MaxError = "<br />You must have a value entered for each one of your maximum stat values.";
				noErrors = false;
			} else {
				if (i == 0) {
					var maximum = $("#Max" + id + "Num" + i).val();
				} else {
					maximum = maximum + "&&&" + $("#Max" + id + "Num" + i).val();
				}
			}

			if ($("#StatID" + id + "Num" + i).val()=="") {
				noErrors = false;
			} else {
				if (i == 0) {
					var statid = $("#StatID" + id + "Num" + i).val();
				} else {
					statid = statid + "&&&" + $("#StatID" + id + "Num" + i).val();
				}
			}

		}
		
		for (var i = 0; i < span2; i++) {

			if ($("#SStat" + id + "Num" + i).val()=="") {
				SaveError = "<br />You must have a value entered for each one of your saves.";
				noErrors = false;
			} else {
				if (i == 0) {
					var save = $("#SStat" + id + "Num" + i).val();
				} else {
					save = save + "&&&" + $("#SStat" + id + "Num" + i).val();
				}
			}

			if ($("#SMin" + id + "Num" + i).val()=="") {
				SMinError = "<br />You must have a value entered for each one of your minimum save values.";
				noErrors = false;
			} else {
				if (i == 0) {
					var sminimum = $("#SMin" + id + "Num" + i).val();
				} else {
					sminimum = sminimum + "&&&" + $("#SMin" + id + "Num" + i).val();
				}
			}
			if ($("#SMax" + id + "Num" + i).val()=="") {
				SMaxError = "<br />You must have a value entered for each one of your maximum save values.";
				noErrors = false;
			} else {
				if (i == 0) {
					var smaximum = $("#SMax" + id + "Num" + i).val();
				} else {
					smaximum = smaximum + "&&&" + $("#SMax" + id + "Num" + i).val();
				}
			}

			if ($("#SaveID" + id + "Num" + i).val()=="") {
				noErrors = false;
			} else {
				if (i == 0) {
					var saveid = $("#SaveID" + id + "Num" + i).val();
				} else {
					saveid = saveid + "&&&" + $("#SaveID" + id + "Num" + i).val();
				}
			}
		}

		if (noErrors == false) {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + OrderError + StatError + MinError + MaxError + SaveError + SMinError + SMaxError + GSError);
			$("#UploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&ID=" + id + "&Span=" + span + "&Stat=" + encodeURIComponent(stat) + "&Save=" + encodeURIComponent(save) + "&Order=" + encodeURIComponent(order) + "&Minimum=" + encodeURIComponent(minimum) + "&Maximum=" + encodeURIComponent(maximum) + "&SMinimum=" + encodeURIComponent(sminimum) + "&SMaximum=" + encodeURIComponent(smaximum) + "&StatID=" + encodeURIComponent(statid) + "&SaveID=" + encodeURIComponent(saveid) + "&GameSystem=" + encodeURIComponent(gamesystem);
		}
	}
	if (action === 'gsavechanges') {
		var Error = '';
		var OrderError = '';
		var StatError = '';
		var MinError = '';
		var MaxError = '';
		if ($("#GameSystem" + id).val()=="") {
			GSError = "<br />You must have a name for your Game System.";
			noErrors = false;
		} else {
			var gamesystem = $("#GameSystem" + id).val();
		}

		if (noErrors == false) {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + OrderError + StatError + MinError + MaxError + SaveError + SMinError + SMaxError + GSError);
			$("#UploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&ID=" + id + "&Span=" + span + "&GameSystem=" + encodeURIComponent(gamesystem);
		}
	}
	if (action === 'sasavechanges') {
		var Error = '';
		var OrderError = '';
		var StatError = '';
		var MinError = '';
		var MaxError = '';
		
		for (var i = 0; i < span2; i++) {

			if ($("#SStat" + id + "Num" + i).val()=="") {
				SaveError = "<br />You must have a value entered for each one of your saves.";
				noErrors = false;
			} else {
				if (i == 0) {
					var save = $("#SStat" + id + "Num" + i).val();
				} else {
					save = save + "&&&" + $("#SStat" + id + "Num" + i).val();
				}
			}

			if ($("#SMin" + id + "Num" + i).val()=="") {
				SMinError = "<br />You must have a value entered for each one of your minimum save values.";
				noErrors = false;
			} else {
				if (i == 0) {
					var sminimum = $("#SMin" + id + "Num" + i).val();
				} else {
					sminimum = sminimum + "&&&" + $("#SMin" + id + "Num" + i).val();
				}
			}
			if ($("#SMax" + id + "Num" + i).val()=="") {
				SMaxError = "<br />You must have a value entered for each one of your maximum save values.";
				noErrors = false;
			} else {
				if (i == 0) {
					var smaximum = $("#SMax" + id + "Num" + i).val();
				} else {
					smaximum = smaximum + "&&&" + $("#SMax" + id + "Num" + i).val();
				}
			}

			if ($("#SaveID" + id + "Num" + i).val()=="") {
				noErrors = false;
			} else {
				if (i == 0) {
					var saveid = $("#SaveID" + id + "Num" + i).val();
				} else {
					saveid = saveid + "&&&" + $("#SaveID" + id + "Num" + i).val();
				}
			}
		}

		if (noErrors == false) {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + OrderError + StatError + MinError + MaxError + SaveError + SMinError + SMaxError + GSError);
			$("#UploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&ID=" + id + "&Span=" + span + "&Save=" + encodeURIComponent(save) + "&SMinimum=" + encodeURIComponent(sminimum) + "&SMaximum=" + encodeURIComponent(smaximum) + "&SaveID=" + encodeURIComponent(saveid);
		}
	}
	if ((action === 'deletestat') || (action === 'duplicatestat')) {
		var Error = '';
		if (statID > 0) {
			var Send = "Action=" + action + "&ID=" + id + "&StatID=" + statID;
		} else {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must select a stat to delete.");
			$("#UploadError").css("padding","5px");
			noError = false;
		}
	}
	if (action === 'deletesave') {
		var Error = '';
		if (statID > 0) {
			var Send = "Action=" + action + "&ID=" + id + "&StatID=" + statID;
		} else {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must select a save to delete.");
			$("#UploadError").css("padding","5px");
			noError = false;
		}
	}
	if (noErrors == true) {
		$.ajax({
		beforeSend: loadingScript(),
		type: "POST",
		url: "validatescripts/builder/ajax/gamesystem.inc.php",
		data: Send,
		success: function (responseText) 
			{
				if (action === 'create') {
					if (responseText == 1) {
						newhash('action=gamesystems&Area=overview');
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + responseText + "</span>");
						$("#UploadError").css("padding","5px");
					}
				}
				if ((action === 'delete') || (action === 'duplicate')) {
					if (responseText == 1) {
						AR_getter(accordionActive);
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + responseText + "</span>");
						$("#UploadError").css("padding","5px");
					}
				}
				if (action === 'addstat') {
					if (responseText.length > 150) {
						$("#stats" + id).html(responseText);
						$("#statsSaved1" + id).html("Stat / Save has been added successfully");
						$("#statsSaved1" + id).css("display","block");
						$("#statsSaved1" + id).fadeOut(2000);
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + responseText + "</span>");
						$("#UploadError").css("padding","5px");
					}
				}
				if (action === 'addexception') {
					if (responseText.length > 150) {
						$("#stats" + id).html(responseText);
						$("#exceptionSaved" + id).html("Exception has been added successfully");
						$("#exceptionSaved" + id).css("display","block");
						$("#exceptionSaved" + id).fadeOut(2000);
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + responseText + "</span>");
						$("#UploadError").css("padding","5px");
					}
				}
				if (action === 'saveexception') {
					if (responseText.length > 150) {
						$("#stats" + id).html(responseText);
						$("#exceptionSaved" + id).html("Exceptions have been saved successfully");
						$("#exceptionSaved" + id).css("display","block");
						$("#exceptionSaved" + id).fadeOut(2000);
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + responseText + "</span>");
						$("#UploadError").css("padding","5px");
					}
				}
				if (action === 'deleteexception') {
					if (responseText.length > 150) {
						$("#stats" + id).html(responseText);
						$("#exceptionSaved" + id).html("Exception has been deleted successfully");
						$("#exceptionSaved" + id).css("display","block");
						$("#exceptionSaved" + id).fadeOut(2000);
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + responseText + "</span>");
						$("#UploadError").css("padding","5px");
					}
				}
				if (action === 'savechanges') {
					
					if (responseText.length > 300) {
						$("#stats" + id).html(responseText);
						$("#statsSaved1" + id).html("Stats data has been saved");
						$("#statsSaved1" + id).css("display","block");
						$("#statsSaved1" + id).fadeOut(2000);
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + responseText + "</span>");
						$("#UploadError").css("padding","5px");
					}
				}
				if (action === 'sasavechanges') {
					if (responseText.length > 300) {
						$("#stats" + id).html(responseText);
						$("#statsSaved2" + id).html("Saves data has been saved");
						$("#statsSaved2" + id).css("display","block");
						$("#statsSaved2" + id).fadeOut(2000);
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + responseText + "</span>");
						$("#UploadError").css("padding","5px");
					}
				}
				if (action === 'gsavechanges') {
					
					if (responseText.length > 300) {
						$("#Header" + id).html(gamesystem);
						$("#statsSaved3" + id).html("General Details have has been saved");
						$("#statsSaved3" + id).css("display","block");
						$("#statsSaved3" + id).fadeOut(2000);
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + responseText + "</span>");
						$("#UploadError").css("padding","5px");
					}
				}
				if (action === 'deletestat') {
					var Error = '';
					if (responseText.length > 300) {
						$("#stats" + id).html(responseText);
						$("#statsSaved1" + id).html("Stat has been deleted successfully");
						$("#statsSaved1" + id).css("display","block");
						$("#statsSaved1" + id).fadeOut(2000);
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />" + responseText);
						$("#UploadError").css("padding","5px");
						noError = false;
					}
				}
				if (action === 'deletesave') {
					var Error = '';
					if (responseText.length > 300) {
						$("#stats" + id).html(responseText);
						$("#statsSaved2" + id).html("Save has been deleted successfully");
						$("#statsSaved2" + id).css("display","block");
						$("#statsSaved2" + id).fadeOut(2000);
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />" + responseText);
						$("#UploadError").css("padding","5px");
						noError = false;
					}
				}
				loadedScript();
			}
		});
	}
}
function gsContentSwitch(action, id, cell, cell2, countsystems, type) {
	
	var noErrors = true;
	$("#UploadError").html("");
	$("#UploadError").css("padding","0px");
	if (id > 0) {
		if ((action === 'general') || (action === 'itemgroups') || (action === 'abilitygroups') || (action === 'rulegroups') || (action === 'items') || (action === 'abilities') || (action === 'rules') || (action === 'unittypes')) {
			var Send = "Action=" + action + "&GameSystemID=" + id + "&Cell=" + cell + "&Cell2=" + cell2 + "&CountSystems=" + countsystems + "&ItemType=" + encodeURIComponent(type);
		}
		if ((action === 'fgeneral') || (action === 'units') || (action === 'pointstypes')) {
			var Send = "Action=" + action + "&FactionID=" + id + "&Cell=" + cell + "&Cell2=" + cell2 + "&CountSystems=" + countsystems;
		}
	} else {
		noError = false;
	}
	if (noErrors == true) {
		$.ajax({
		beforeSend: loadingScript(),
		type: "POST",
		url: "validatescripts/builder/ajax/gamesystem.contentswitch.inc.php",
		data: Send,
		success: function (responseText) 
			{
				if (responseText > 0) {

					if ((action === 'general') || (action === 'itemgroups') || (action === 'abilitygroups') || (action === 'rulegroups') || (action === 'items') || (action === 'abilities') || (action === 'rules') || (action === 'unittypes')) {
						if (responseText == 2) {
							var aerror = "<br />No Game System has been selected.";
						} else if (responseText == 3) {
							var aerror = "<br />That Game System does not exist.";
						} else if (responseText == 4) {
							var aerror = "<br />This Game System does not belong to you.";
						} else if (responseText == 5) {
							var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS89578.";
						} else if (responseText == 6) {
							var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS51995.";
						}
					}

					if ((action === 'fgeneral') || (action === 'units') || (action === 'pointstypes')) {
						if (responseText == 2) {
							var aerror = "<br />No Faction has been selected.";
						} else if (responseText == 3) {
							var aerror = "<br />That Faction does not exist.";
						} else if (responseText == 4) {
							var aerror = "<br />This Game System does not belong to you.";
						} else if (responseText == 5) {
							var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS61664.";
						}
					}

				} else {
					if (action === 'general') {
						$("#contentLink1" + id).css("background-color","#662b2b");
						$("#contentLink1" + id).css("background-image","url(images/builder/section_open.png)");
						$("#contentLink2" + id).css("background-color","");
						$("#contentLink2" + id).css("background-image","");
						$("#contentLink3" + id).css("background-color","");
						$("#contentLink3" + id).css("background-image","");
						$("#contentLink4" + id).css("background-color","");
						$("#contentLink4" + id).css("background-image","");
					} else if (action === 'unittypes') {
						$("#contentLink1" + id).css("background-color","");
						$("#contentLink1" + id).css("background-image","");
						$("#contentLink2" + id).css("background-color","#662b2b");
						$("#contentLink2" + id).css("background-image","url(images/builder/section_open.png)");
						$("#contentLink3" + id).css("background-color","");
						$("#contentLink3" + id).css("background-image","");
						$("#contentLink4" + id).css("background-color","");
						$("#contentLink4" + id).css("background-image","");
					} else if (action === 'itemgroups') {
						$("#contentLink1" + id).css("background-color","");
						$("#contentLink1" + id).css("background-image","");
						$("#contentLink2" + id).css("background-color","");
						$("#contentLink2" + id).css("background-image","");
						$("#contentLink3" + id).css("background-color","#662b2b");
						$("#contentLink3" + id).css("background-image","url(images/builder/section_open.png)");
						$("#contentLink4" + id).css("background-color","");
						$("#contentLink4" + id).css("background-image","");
						$("#contentLink7" + id).css("background-color","");
						$("#contentLink7" + id).css("background-image","");
						$("#contentLink6" + id).css("background-color","");
						$("#contentLink6" + id).css("background-image","");
						$("#contentLink5" + id).css("background-color","#662b2b");
						$("#contentLink5" + id).css("background-image","url(images/builder/section_open.png)");
					} else if (action === 'items') {
						$("#contentLink1" + id).css("background-color","");
						$("#contentLink1" + id).css("background-image","");
						$("#contentLink2" + id).css("background-color","");
						$("#contentLink2" + id).css("background-image","");
						$("#contentLink3" + id).css("background-color","");
						$("#contentLink3" + id).css("background-image","");
						$("#contentLink4" + id).css("background-color","#662b2b");
						$("#contentLink4" + id).css("background-image","url(images/builder/section_open.png)");
						$("#contentLink7" + id).css("background-color","");
						$("#contentLink7" + id).css("background-image","");
						$("#contentLink6" + id).css("background-color","");
						$("#contentLink6" + id).css("background-image","");
						$("#contentLink5" + id).css("background-color","#662b2b");
						$("#contentLink5" + id).css("background-image","url(images/builder/section_open.png)");
					} 

					if (action === 'fgeneral') {
						$("#contentLink1" + id).css("background-color","#662b2b");
						$("#contentLink1" + id).css("background-image","url(images/builder/section_open.png)");
						$("#contentLink2" + id).css("background-color","");
						$("#contentLink2" + id).css("background-image","");
						$("#contentLink3" + id).css("background-color","");
						$("#contentLink3" + id).css("background-image","");
					} else if (action === 'units') {
						$("#contentLink1" + id).css("background-color","");
						$("#contentLink1" + id).css("background-image","");
						$("#contentLink2" + id).css("background-color","");
						$("#contentLink2" + id).css("background-image","");
						$("#contentLink3" + id).css("background-color","#662b2b");
						$("#contentLink3" + id).css("background-image","url(images/builder/section_open.png)");
					} else if (action === 'pointstypes') {
						$("#contentLink1" + id).css("background-color","");
						$("#contentLink1" + id).css("background-image","");
						$("#contentLink3" + id).css("background-color","");
						$("#contentLink3" + id).css("background-image","");
						$("#contentLink2" + id).css("background-color","#662b2b");
						$("#contentLink2" + id).css("background-image","url(images/builder/section_open.png)");
					}
					if ((action === 'general') || (action === 'itemgroups') || (action === 'abilitygroups') || (action === 'rulegroups') || (action === 'unittypes') || (action === 'items') || (action === 'abilities') || (action === 'rules')) {
						$("#gsContent" + id).html(responseText);
						if (action === 'abilities') {

							$("#contentLink1" + id).css("background-color","");
							$("#contentLink1" + id).css("background-image","");
							$("#contentLink2" + id).css("background-color","");
							$("#contentLink2" + id).css("background-image","");
							$("#contentLink3" + id).css("background-color","");
							$("#contentLink3" + id).css("background-image","");
							$("#contentLink4" + id).css("background-color","#662b2b");
							$("#contentLink4" + id).css("background-image","url(images/builder/section_open.png)");
							$("#contentLink5" + id).css("background-color","");
							$("#contentLink5" + id).css("background-image","");
							$("#contentLink6" + id).css("background-color","#662b2b");
							$("#contentLink6" + id).css("background-image","url(images/builder/section_open.png)");
							$("#contentLink7" + id).css("background-color","");
							$("#contentLink7" + id).css("background-image","");

						} else if (action === 'rules') {
							$("#contentLink1" + id).css("background-color","");
							$("#contentLink1" + id).css("background-image","");
							$("#contentLink2" + id).css("background-color","");
							$("#contentLink2" + id).css("background-image","");
							$("#contentLink3" + id).css("background-color","");
							$("#contentLink3" + id).css("background-image","");
							$("#contentLink4" + id).css("background-color","#662b2b");
							$("#contentLink4" + id).css("background-image","url(images/builder/section_open.png)");
							$("#contentLink5" + id).css("background-color","");
							$("#contentLink5" + id).css("background-image","");
							$("#contentLink6" + id).css("background-color","");
							$("#contentLink6" + id).css("background-image","");
							$("#contentLink7" + id).css("background-color","#662b2b");
							$("#contentLink7" + id).css("background-image","url(images/builder/section_open.png)");
						} else if (action === 'abilitygroups') {
							$("#contentLink1" + id).css("background-color","");
							$("#contentLink1" + id).css("background-image","");
							$("#contentLink2" + id).css("background-color","");
							$("#contentLink2" + id).css("background-image","");
							$("#contentLink3" + id).css("background-color","#662b2b");
							$("#contentLink3" + id).css("background-image","url(images/builder/section_open.png)");
							$("#contentLink4" + id).css("background-color","");
							$("#contentLink4" + id).css("background-image","");
							$("#contentLink5" + id).css("background-color","");
							$("#contentLink5" + id).css("background-image","");
							$("#contentLink6" + id).css("background-color","#662b2b");
							$("#contentLink6" + id).css("background-image","url(images/builder/section_open.png)");
							$("#contentLink7" + id).css("background-color","");
							$("#contentLink7" + id).css("background-image","");
						} else if (action === 'rulegroups') {
							$("#contentLink1" + id).css("background-color","");
							$("#contentLink1" + id).css("background-image","");
							$("#contentLink2" + id).css("background-color","");
							$("#contentLink2" + id).css("background-image","");
							$("#contentLink3" + id).css("background-color","#662b2b");
							$("#contentLink3" + id).css("background-image","url(images/builder/section_open.png)");
							$("#contentLink4" + id).css("background-color","");
							$("#contentLink4" + id).css("background-image","");
							$("#contentLink5" + id).css("background-color","");
							$("#contentLink5" + id).css("background-image","");
							$("#contentLink6" + id).css("background-color","");
							$("#contentLink6" + id).css("background-image","");
							$("#contentLink7" + id).css("background-color","#662b2b");
							$("#contentLink7" + id).css("background-image","url(images/builder/section_open.png)");
						} 
					}
					if ((action === 'fgeneral') || (action === 'units') || (action === 'pointstypes')) {
						$("#fsContent" + id).html(responseText);
					}
				}
				if (responseText > 0) {
					$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
					$("#NewUploadError").css("padding","5px");
				}
				loadedScript();
			}
		});
	}
}

function groupEdit(action, id, title, gsID, type) {
	$("#UploadError").html("");
	$("#UploadError").css("padding","0px");
	$("#NewUploadError").html("");
	$("#NewUploadError").css("padding","0px");
	var no_errors = true;
	if (action === 'create') {

		if (type === 'I') {

			TestGroupName = 'Item Group Name';

		}

		if (type === 'A') {

			TestGroupName = 'Ability Group Name';

		}

		if (type === 'R') {

			TestGroupName = 'Rule Group Name';

		}

		if (($("#GroupName").val()=="") || ($("#GroupName").val()== TestGroupName)) {
			if (type === 'I') {
				$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must enter a name for your new Item Group</span>");
			}
			if (type === 'R') {
				$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must enter a name for your new Rule Group</span>");
			}

			if (type === 'A') {
				$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must enter a name for your new Ability Group</span>");
			}			
			$("#UploadError").css("padding","5px");
		} else {
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/gamesystems.itemgroup.inc.php",
			data: "GameSystemID=" + id + "&ItemGroupName=" + encodeURIComponent($("#GroupName").val()) + "&Action=" + encodeURIComponent(action) + "&ItemType=" + encodeURIComponent(type),
			success: function (responseText) 
				{
					if (responseText == 2) {

						var aerror = "<br />No Game System has been selected.";

					} else if (responseText == 3) {

						var aerror = "<br />That Game System does not exist.";

					} else if (responseText == 4) {

						var aerror = "<br />That Game System does not belong to you.";

					} else if (responseText == 5) {

						var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS48584.";

					} else if (responseText == 6) {
						if (type === 'I') {
							var aerror = "<br />That Item Group Name has used illegal characters. Please make sure you only use website safe characters.";
						}
				
						if (type === 'A') {
							var aerror = "<br />That Ability Group Name has used illegal characters. Please make sure you only use website safe characters.";
						}
				
						if (type === 'R') {
							var aerror = "<br />That Rule Group Name has used illegal characters. Please make sure you only use website safe characters.";
						}

					} else if (responseText == 7) {
						if (type === 'I') {
							var aerror = "<br />That Item Group has already been added to this Game System.";
						}
				
						if (type === 'A') {
							var aerror = "<br />That Ability Group has already been added to this Game System.";
						}
				
						if (type === 'R') {
							var aerror = "<br />That Rule Group has already been added to this Game System.";
						}

					} else {

						$("#itemsListed").html(responseText);
						$("#Named").html("<input type=\"text\" tabindex=\"55\" name=\"GroupName\" id=\"GroupName\" style=\"width: 250px;\" class=\"TextField\" value=\"" + TestGroupName + "\" onFocus=\"if((!this._haschanged) && (this.value == '" + TestGroupName + "')){this.value=''; this._haschanged=true; this.style.color = '#000'} else if ((this._haschanged) && (this.value==='')) { this.value='" + TestGroupName + "'; this._haschanged=false; this.style.color = '#999'}\" onBlur=\"if ((this._haschanged) && (this.value==='')) { this.value='" + TestGroupName + "'; this._haschanged=false; this.style.color = '#999'}\" />");
					}
					if (responseText > 0) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#UploadError").css("padding","5px");
					}
					loadedScript();
				}
			});
		}
	}
	if ((action === 'groupconnect') || (action === 'groupdelete') || (action === 'groupmodify')) {
		if (action === 'groupdelete') {
			if (id > 0) {
				var itemc = id;	
				var Send = "ItemConnectID=" + itemc + "&Action=" + action;
			} else {
				$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must select an Item to delete.</span>");
				$("#NewUploadError").css("padding","5px");
				no_errors = false;
			}
		}
		if (action === 'groupconnect') {
			if ($("#ItemGroup").val()==0) {
				$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must select an Item from the dropdown list.</span>");
				$("#NewUploadError").css("padding","5px");
				no_errors = false;
			} else {
				var itemc = $("#ItemGroup").val();
				var Send = "ItemID=" + itemc + "&ItemGroupID=" + id + "&Action=" + action;
			}
		}
		if (action === 'groupmodify') {
			if (type === 'I') {

				TestGroupName = 'Item Group Name';

			}

			if (type === 'A') {

				TestGroupName = 'Ability Group Name';

			}

			if (type === 'R') {

				TestGroupName = 'Rule Group Name';

			}
			if (($("#NewItemGroupName").val()=="") || ($("#NewItemGroupName").val()== TestGroupName)) {
				$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must enter an" + TestGroupName + ".</span>");
				$("#NewUploadError").css("padding","5px");
				no_errors = false;
			} else {
				if (id > 0) {
					var ItemGroupName = $("#NewItemGroupName").val();
					var Send = "ItemGroupName=" + ItemGroupName + "&ItemGroupID=" + id + "&Action=" + action;
				} else {
					$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must select an Item Group to modify.</span>");
					$("#NewUploadError").css("padding","5px");
					no_errors = false;
				}

			}
		}
		if (no_errors == true) {
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/gamesystems.itemgroup.inc.php",
			data: Send,
			success: function (responseText) 
				{
					if (action === 'groupconnect') {
						if (responseText == 2) {
							if (type == 'I') {
								var aerror = "<br />No Item Group has been selected.";
							} else if (type == 'A') {
								var aerror = "<br />No Ability Group has been selected.";
							} else if (type == 'R') {
								var aerror = "<br />No Rule Group has been selected.";
							}
						} else if (responseText == 3) {
							if (type == 'I') {
								var aerror = "<br />That Item Group does not exist.";
							} else if (type == 'A') {
								var aerror = "<br />That Ability Group does not exist.";
							} else if (type == 'R') {
								var aerror = "<br />That Rule Group does not exist.";
							}
						} else if (responseText == 4) {
							var aerror = "<br />That Game System does not exist.";
						} else if (responseText == 5) {
							var aerror = "<br />That Game System does not belong to you.";
						} else if (responseText == 6) {
							if (type == 'I') {
								var aerror = "<br />That Item has already been added to the Item Group.";
							} else if (type == 'A') {
								var aerror = "<br />That Ability has already been added to the Ability Group.";
							} else if (type == 'R') {
								var aerror = "<br />That Rule has already been added to the Rule Group.";
							}
						} else {

							$("#ItemBox").html(responseText);
						}
					}
					if (action === 'groupmodify') {
						if (responseText == 2) {
							if (type == 'I') {
								var aerror = "<br />No Item Group has been selected.";
							} else if (type == 'A') {
								var aerror = "<br />No Ability Group has been selected.";
							} else if (type == 'R') {
								var aerror = "<br />No Rule Group has been selected.";
							}
						} else if (responseText == 3) {
							if (type == 'I') {
								var aerror = "<br />That Item Group does not exist.";
							} else if (type == 'A') {
								var aerror = "<br />That Ability Group does not exist.";
							} else if (type == 'R') {
								var aerror = "<br />That Rule Group does not exist.";
							}
						} else if (responseText == 4) {
							var aerror = "<br />That Game System does not exist.";
						} else if (responseText == 5) {
							var aerror = "<br />That Game System does not belong to you.";
						} else if (responseText == 6) {
							var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS74201.";
						} else if (responseText == 7) {
							var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS14118.";
						} else {
							if (type == 'I') {
								gsContentSwitch('itemgroups', gsID,'CellEven','CellOdd', '0', type);
							} else if (type == 'A') {
								gsContentSwitch('abilitygroups', gsID,'CellEven','CellOdd', '0', type);
							} else if (type == 'R') {
								gsContentSwitch('rulegroups', gsID,'CellEven','CellOdd', '0', type);
							}
							$("#groupSaved").html("Data saved");
							$("#groupSaved").css("display","block");
							$("#groupSaved").fadeOut(2000);
						}
					}
					
					if (action === 'groupdelete') {
						if (responseText == 2) {
							var aerror = "<br />No Item has been selected.";
						} else if (responseText == 3) {
							var aerror = "<br />That Item Group does not exist.";
						} else if (responseText == 4) {
							var aerror = "<br />That Item Group does 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 if (responseText == 7) {
							var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS16230.";
						} else {
							$("#ItemBox").html(responseText);
						}
					}
					if (responseText > 0) {
						$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#NewUploadError").css("padding","5px");
					}
					loadedScript();
					
				}
			});
		}
	}

	if ((action === 'open') || (action === 'delete')) {
		if (id > 0) {
			if (action == 'delete') {
				if (type === 'I') {

					var deleteme = confirm("Are you sure you want to delete this Item Group? All Items will be unlinked from this Item Group.\n\nPlease note that this will NOT delete the Items themselves, just the relation the Items have with the Item Group.");

				}

				if (type === 'A') {

					var deleteme = confirm("Are you sure you want to delete this Ability Group? All Abilities will be unlinked from this Ability Group.\n\nPlease note that this will NOT delete the Abilities themselves, just the relation the Abilities have with the Ability Group.");

				}

				if (type === 'R') {

					var deleteme = confirm("Are you sure you want to delete this Rule Group? All Rules will be unlinked from this Rule Group.\n\nPlease note that this will NOT delete the Rules themselves, just the relation the Rules have with the Rule Group.");

				}				if (deleteme == true) {
					noErrors = true;
				} else {
					noErrors = false;
				}
			} else {
				noErrors = true;
			}
			if (noErrors == true) {
				$.ajax({
				beforeSend: loadingScript(),
				type: "POST",
				url: "validatescripts/builder/ajax/gamesystems.itemgroup.inc.php",
				data: "ItemGroupID=" + id + "&Action=" + encodeURIComponent(action) + "&ItemType=" + encodeURIComponent(type),
				success: function (responseText) 
					{
					if (action == 'open') {	
						if (responseText == 2) {
							var aerror = "<br />No Item has been selected.";
						} else if (responseText == 3) {
							var aerror = "<br />That Item Group does not exist.";
						} else if (responseText == 4) {
							var aerror = "<br />That Game System does not exist.";
						} else if (responseText == 5) {
							var aerror = "<br />That Game System does not belong to you.";
						} else if (responseText == 6) {
							var aerror = "<br />That Item Group does not belong to you.";
						} else if (responseText == 7) {
							var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS68146.";
						} else {
							$('#dialogprofile').dialog('destroy');		
							$("#dialogprofile").html(responseText);
							title = title.replace("***", "'")
							if (type === 'I') {

								$("#dialogprofile").attr("title", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Edit Item Group: " + title);

							}

							

							if (type === 'A') {

								$("#dialogprofile").attr("title", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Edit Ability Group: " + title);

							}

							

							if (type === 'R') {

								$("#dialogprofile").attr("title", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Edit Rule Group: " + title);

							}
							$('#dialogprofile').dialog('open');			
							$("#dialogprofile").dialog({
								bgiframe: false,
								closeOnEscape: true,
								draggable: true,
								resizable: false,
								width: 632,
								modal: true,
							});
						}
					}
					if (action == 'delete') {
						if (responseText == 2) {
							var aerror = "<br />No Item has been selected.";
						} else if (responseText == 3) {
							var aerror = "<br />That Item Group does not exist.";
						} else if (responseText == 4) {
							var aerror = "<br />That Game System does not exist.";
						} else if (responseText == 5) {
							var aerror = "<br />That Game System does not belong to you.";
						} else if (responseText == 6) {
							var aerror = "<br />That Item Group does not belong to you.";
						} else if (responseText == 7) {
							var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS68146.";
						} else {
							$("#itemsListed").html(responseText);
						}
					}

					if (responseText > 0) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#UploadError").css("padding","5px");
					}
					loadedScript();
					}

				});
			}
		}
	}
}
function typeEdit(action, id, title, gsID, previous) {
	$("#UploadError").html("");
	$("#UploadError").css("padding","0px");
	$("#NewUploadError").html("");
	$("#NewUploadError").css("padding","0px");
	var no_errors = true;
	if (action === 'create') {
		if (($("#UnitTypeName").val()=="") || ($("#UnitTypeName").val()=="Unit Type Name")) {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must enter a name for your new Unit Type.</span>");
			$("#UploadError").css("padding","5px");
		} else {
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/gamesystems.unittype.inc.php",
			data: "GameSystemID=" + id + "&UnitTypeName=" + encodeURIComponent($("#UnitTypeName").val()) + "&Action=" + encodeURIComponent(action),
			success: function (responseText) 
				{
					if (responseText == 2) {
						var aerror = "<br />No Game System has been selected.";
					} else if (responseText == 3) {
						var aerror = "<br />That Game System does not exist.";
					} else if (responseText == 4) {
						var aerror = "<br />That Game System does not belong to you.";
					} else if (responseText == 5) {
						var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS58556.";
					} else if (responseText == 6) {
						var aerror = "<br />That Unit Type Name is not made up of legal website characters. Please try again.";
					} else if (responseText == 7) {
						var aerror = "<br />That Unit Type has already been added to the Game System.";
					} else {
						$("#typesListed").html(responseText);
					}
					loadedScript();
					if (responseText > 0) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#UploadError").css("padding","5px");
					}
				}
			});
		}
	}
	
	
	if ((action === 'openunittypes') || (action === 'saveunittypes')) {
		if (id > 0) {
			if (action === 'openunittypes') {
				
				var Send = "Action=" + action + "&FactionID=" + gsID + "&UnitTypeID=" + id;
				
			} else if (action === 'saveunittypes') {
				
							var Send = "Action=" + action + "&UnitTypeID=" + id + "&Price=" + price + "&Preselected=" + value + "&Deselectable=" + dvalue + "&Quantity=" + qauntity + "&Multiply=" + multiply;	
			}
			if (noErrors == true) {
				$.ajax({
				beforeSend: loadingScript(),
				type: "POST",
				url: "validatescripts/builder/ajax/factions.unittypeunits.inc.php",
				data: Send,
				success: function (responseText) 
					{
			
						if (responseText == 2) {
							var aerror = "<br />No Faction has been set.";
						} else if (responseText == 3) {
							var aerror = "<br />No Unit Type has been set.";
						} else if (responseText == 4) {
							var aerror = "<br />That Unit Type 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 {
							$("#unittypegroups").html(responseText);

							if (action === 'openunittypes') {
								var ids = previous.split("&&&");
								for (var i = 0; i < ids.length; i++) {
									if (ids[i] == gsID) {
										$("#type" + gsID).css("background-color","#662b2b");
										$("#type" + gsID).css("background-image","url(images/builder/section_open.png)");
									} else {
										$("#type" + ids[i]).css("background-color","");
										$("#type" + ids[i]).css("background-image","");
									}
								}
							} else if (action === 'saveitem') {
								$("#itemSaved").html("Data Saved");
								$("#itemSaved").css("display","block");
								$("#itemSaved").fadeOut(2000);
							}
						}
	
						loadedScript();
						if (responseText > 0) {
							$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
							$("#NewUploadError").css("padding","5px");
						}
					}
			
				});
			}
		}
	}
	if (action === 'addunit') {
		if (id == 0) {
			var aerror = "<br />No Unit has been selected.";
			noErrors = false;
		}
		if ($("#Unit").val() > 0) {
			var units = $("#Unit").val();
		} else {
			var aerror = "<br />You must select a Unit from the dropdown list.";
			noErrors = false;
		}
		if (noErrors == false) {
			$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
			$("#NewUploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&UnitID=" + units + "&UnitTypeID=" + id;
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/factions.unittypeunits.inc.php",
			data: Send,
			success: function (responseText) 
				{
					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 />That Unit Type does not exist.";
					} else if (responseText == 5) {
						var aerror = "<br />That Unit does not exist.";
					} else if (responseText == 6) {
						var aerror = "<br />That Faction does not exist.";
					} else if (responseText == 7) {
						var aerror = "<br />That Game System does not exist.";
					} else if (responseText == 8) {
						var aerror = "<br />That Game System does not belong to you.";
					} else if (responseText == 9) {
						var aerror = "<br />That Unit has already been added to this Unit Type for this Faction.";
					} else {
						$("#unitsselected").html(responseText);
						$("#unitsselectedoptions").html("");
					}
					loadedScript();
					if (responseText > 0) {
						$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#NewUploadError").css("padding","5px");
					}
				}
			});
		}
	}

	if (action === 'deletepointstypes') {
		if (id > 0) {
			if (action == 'delete') {
				var deleteme = confirm("Are you sure you want to delete this Points Limit?");
				if (deleteme == true) {
					noErrors = true;
				} else {
					noErrors = false;
				}
			} else {
				noErrors = true;
			}
			if (noErrors == true) {
				alert(1);
				$.ajax({
				beforeSend: loadingScript(),
				type: "POST",
				url: "validatescripts/builder/ajax/gamesystems.unittype.inc.php",
				data: "PointLimitID=" + id + "&Action=" + encodeURIComponent(action),
				success: function (responseText) 
					{
						alert(1);
						if (responseText == 2) {
							var aerror = "<br />No Point Limit has been selected.";
						} else if (responseText == 3) {
							var aerror = "<br />That Point Limit does not exist.";
						} else if (responseText == 4) {
							var aerror = "<br />That Faction does 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 {
							$("#typesListed").html(responseText);
						}
	
						loadedScript();
						if (responseText > 0) {
							$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
							$("#UploadError").css("padding","5px");
						}
					}

				});
			}
		}
	}


	if ((action === 'open') || (action === 'delete')) {
		if (id > 0) {
			if (action == 'delete') {
				var deleteme = confirm("Are you sure you want to delete this Unit Type? The units from every faction you own will be unlinked and will have to be re-established to a new Unit Type.\n\nPlease note that this will NOT delete the Units themselves, just the relation the Units have with the Unit Type.");
				if (deleteme == true) {
					noErrors = true;
				} else {
					noErrors = false;
				}
			} else {
				noErrors = true;
			}
			if (noErrors == true) {
				$.ajax({
				beforeSend: loadingScript(),
				type: "POST",
				url: "validatescripts/builder/ajax/gamesystems.unittype.inc.php",
				data: "UnitTypeID=" + id + "&Action=" + encodeURIComponent(action),
				success: function (responseText) 
					{
					if (action == 'open') {	
						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 Game System does not exist.";
						} else if (responseText == 5) {
							var aerror = "<br />That Game System does not belong to you.";
						} else if (responseText == 6) {
							var aerror = "<br />That Unit Type does not belong to you.";
						} else if (responseText == 7) {
							var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS68920.";
						} else {
							$('#dialogprofile').dialog('destroy');		
							$("#dialogprofile").html(responseText);
							title = title.replace("***", "'")
							$("#dialogprofile").attr("title", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Edit Item Group: " + title);
							$('#dialogprofile').dialog('open');			
							$("#dialogprofile").dialog({
								bgiframe: false,
								closeOnEscape: true,
								draggable: true,
								resizable: false,
								width: 632,
								modal: true,
							});
						}
						

					}
					if (action == 'delete') {
						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 Game System does not exist.";
						} else if (responseText == 5) {
							var aerror = "<br />That Game System does not belong to you.";
						} else if (responseText == 6) {
							var aerror = "<br />That Unit Type does not belong to you.";
						} else if (responseText == 7) {
							var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS68146.";
						} else {
							$("#typesListed").html(responseText);
						}
					}

					loadedScript();
					if (responseText > 0) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#UploadError").css("padding","5px");
					}
					}

				});
			}
		}
	}

	if (action === 'itemDesc') {
		var ItemDescription = $("#NewUnitTypeName").val();
		if (($("#NewUnitTypeName").val()=="") || ($("#NewUnitTypeName").val()=="Unit Type Name")) {
			$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must enter a name for your Unit Type.</span>");
			$("#NewUploadError").css("padding","5px");
		} else {
			if ($("#DisplayOrder").val() > 0) {
				$.ajax({
				beforeSend: loadingScript(),
				type: "POST",
				url: "validatescripts/builder/ajax/gamesystems.unittype.inc.php",
				data: "UnitTypeID=" + id + "&UnitTypeName=" + encodeURIComponent($("#NewUnitTypeName").val()) + "&Action=" + encodeURIComponent(action) + "&DisplayOrder=" + $("#DisplayOrder").val(),
				success: function (responseText) 
					{
						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 Game System does not exist.";
						} else if (responseText == 5) {
							var aerror = "<br />That Game System does not belong to you.";
						} else if (responseText == 6) {
							var aerror = "<br />That Item Name has used illegal characters. Please make sure you only use website safe characters.";
						} else {
							gsContentSwitch('unittypes', gsID,'CellEven','CellOdd', '0');
							$("#groupSaved").html("Data saved");
							$("#groupSaved").css("display","block");
							$("#groupSaved").fadeOut(2000);
							
						}
						if (responseText > 0) {
							$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
							$("#NewUploadError").css("padding","5px");
						}
						loadedScript();
					}
				});
			} else {
				$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must enter a Display Order that is great than 0.</span>");
				$("#NewUploadError").css("padding","5px");
			}
		}
	}
	if ((action === 'addpointstypes') || (action === 'savepointstypes')) {
		if (action === 'savepointstypes') {
			var ids = previous.split("&&&");
			for (var i = 0; i < ids.length; i++) {
				if (($("#nmintype" + ids[i]).val() >= 0) && (($("#nmaxtype" + ids[i]).val() >= 0) || ($("#nmaxtype" + ids[i]).val() == '-'))) {
				} else {
					error = "<br />Please make sure your Minimum and Maximum values for your Unit Type Limits is equal to or greater than 0.";
					no_errors = false;
					i = ids.length;
				}
				if (no_errors == true) {
					if (i == 0) {
						var mintype = $("#nmintype" + ids[i]).val();	
						var maxtype = $("#nmaxtype" + ids[i]).val();	
					} else {
						mintype = mintype + "&&&" + $("#nmintype" + ids[i]).val();	
						maxtype = maxtype + "&&&" + $("#nmaxtype" + ids[i]).val();	
					}
				}
			}

			if ($("#NPoints").val() > 0) {
				var points = $("#NPoints").val();
			} else {
				error = "<br />Please make sure your Points are greater than 0.";
				no_errors = false;
			}
			var PointLimitID = "&PointLimitID=" + gsID;
		} else {
			var ids = previous.split("&&&");
			for (var i = 0; i < ids.length; i++) {
				if (($("#mintype" + ids[i]).val() >= 0) && (($("#maxtype" + ids[i]).val() >= 0) || ($("#maxtype" + ids[i]).val() == '-'))) {
				} else {
					error = "<br />Please make sure your Minimum and Maximum values for your Unit Type Limits is equal to or greater than 0.";
					no_errors = false;
					i = ids.length;
				}
				if (no_errors == true) {
					if (i == 0) {
						var mintype = $("#mintype" + ids[i]).val();	
						var maxtype = $("#maxtype" + ids[i]).val();	
					} else {
						mintype = mintype + "&&&" + $("#mintype" + ids[i]).val();	
						maxtype = maxtype + "&&&" + $("#maxtype" + ids[i]).val();	
					}
				}
			}
			
			if ($("#Points").val() > 0) {
				var points = $("#Points").val();
			} else {
				error = "<br />Please make sure your Points are greater than 0.";
				no_errors = false;
			}
			var PointLimitID = "";
		}
		if (no_errors == true) {
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/gamesystems.unittype.inc.php",
			data: "UnitTypeID=" + id + "&Min=" + encodeURIComponent(mintype) + "&Max=" + encodeURIComponent(maxtype) + "&IDS=" + encodeURIComponent(previous) + "&Action=" + encodeURIComponent(action) + "&FactionID=" + encodeURIComponent(id) + "&PointsLimit=" + encodeURIComponent(points) + PointLimitID,
			success: function (responseText) 
				{
					if (responseText == 2) {
						var aerror = "<br />No Faction has been selected.";
					} else if (responseText == 3) {
						var aerror = "<br />That Faction does not exist.";
					} else if (responseText == 4) {
						var aerror = "<br />That Game System does not exist.";
					} else if (responseText == 5) {
						var aerror = "<br />That Game System does not belong to you.";
					} else if (responseText == 6) {
						var aerror = "<br />That Item Name has used illegal characters. Please make sure you only use website safe characters.";
					} else {
						if (action === 'savepointstypes') {
							gsContentSwitch('pointstypes', id,'CellEven','CellOdd', '0');
							$("#pointsSaved").html("Data saved");
							$("#pointsSaved").css("display","block");
							$("#pointsSaved").fadeOut(2000);
						} else {
							$("#typesListed").html(responseText);
						}
					}
					if (responseText > 0) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#UploadError").css("padding","5px");
					}
					loadedScript();
				}
			});
		} else {
			if (action === 'savepointstypes') {
				$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + error + "</span>");
				$("#NewUploadError").css("padding","5px");
			} else {
				$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + error + "</span>");
				$("#UploadError").css("padding","5px");
			}

		}
	}
	
	if (action === 'openpointstype') {

		if (id > 0) {
		} else {
			error = "<br />There is no Point Limit selected.";
			no_errors = false;
		}
		if (no_errors == true) {
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/factions.unittypeunits.inc.php",
			data: "PointLimitID=" + id + "&Action=" + encodeURIComponent(action),
			success: function (responseText) 
				{
					if (responseText == 2) {
						var aerror = "<br />No Point Limit has been selected.";
					} else if (responseText == 3) {
						var aerror = "<br />That Point Limit does not exist.";
					} else if (responseText == 4) {
						var aerror = "<br />That Faction does 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 {
						$('#dialogprofile').dialog('destroy');		
						$("#dialogprofile").html(responseText);
						title = title.replace("***", "'")
						$("#dialogprofile").attr("title", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Edit Item: " + title);
						$('#dialogprofile').dialog('open');			
						$("#dialogprofile").dialog({
							bgiframe: false,
							closeOnEscape: true,
							draggable: true,
							resizable: false,
							width: 632,
							modal: true,
						});
					}
					if (responseText > 0) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#UploadError").css("padding","5px");
					}
					loadedScript();
				},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				alert(XMLHttpRequest);
				alert(textStatus);
				alert(errorThrown);
				
			}
			});
		} else {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + error + "</span>");
			$("#UploadError").css("padding","5px");
		}
	}
	
}
function provideStats(ItemID) {

	if ($("#statoption").val() == 0) {

		$("#statoption").val(1);

		$("#statline").css("display","block");

	} else {

		$("#statoption").val(0);

		$("#statline").css("display","none");

	}

	if (ItemID > 0) {

		var Send = "ItemID=" + ItemID + "&Statline=" + $("#statoption").val();

		$.ajax({

		beforeSend: loadingScript(),

		type: "POST",

		url: "validatescripts/builder/ajax/gamesystems.items.statlinepresent.inc.php",

		data: Send,

		success: function (responseText) 

			{

				if (responseText == 3) {

					var aerror = "<br />That Game System does not belong to you. You cannot edit an Item in a Game System that does not belong to you.";

				} else {

					

				}

				loadedScript();

				if (responseText > 0) {

					$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");

					$("#NewUploadError").css("padding","5px");

				}

			}

		});

	}

}

	
function itemEdit(action, id, title, gsID, span, type) {	
	$("#NewUploadError").html("");
	$("#UploadError").html("");
	$("#NewUploadError").css("padding","0px");
	$("#UploadError").css("padding","0px");
	var no_errors = true;
	if (type === 'A') {
		var itemname = "Ability";
		var anitemname = "an Ability";
	}
	if (type === 'I') {
		var itemname = "Item";
		var anitemname = "a Item";
	}
	if (type === 'R') {
		var itemname = "Rule";
		var anitemname = "a Rule";
	}
	if (action === 'modifystats') {
		var Error = '';
		var ids = id.split("&&&");
		for (var i = 0; i < ids.length; i++) {

			if (($("#StatLine" + ids[i]).val() >= 0) || ($("#StatLine" + ids[i]).val() == '-')) {
				if (i == 0) {
					var values = $("#StatLine" + ids[i]).val();
				} else {
					values = values + "&&&" + $("#StatLine" + ids[i]).val();
				}
			} else {
				error = "<br />Please make sure you have input values for your stats.";
				no_errors = false;
				i = ids.length;
			}
		}		
		if (noErrors == false) {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + error + "</span");
			$("#UploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&ItemID=" + gsID + "&IDs=" + encodeURIComponent(id) + "&Values=" + encodeURIComponent(values);
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/gamesystems.items.inc.php",
			data: Send,
			success: function (responseText) 
				{
					
					if (responseText == 2) {
						var aerror = "<br />No Item has been selected.";
					} else if (responseText == 3) {
						var aerror = "<br />That Item does not exist.</span>";
					} else if (responseText == 4) {
						var aerror = "<br />That Game System does not exist.";
					} else if (responseText == 5) {
						var aerror = "<br />That Game System does not belong to you.";
					} else {
						$("#statlineSaved").html("Data saved");
						$("#statlineSaved").css("display","block");
						$("#statlineSaved").fadeOut(2000);
					}
					loadedScript();
					if (responseText > 0) {
						$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#NewUploadError").css("padding","5px");
					}
				}
			});
		}
	}
	
	
	if (action === 'create') {
		if (type === 'I') {
			TestName = 'Item Name';
		}
		if (type === 'A') {
			TestName = 'Ability Name';
		}
		if (type === 'R') {
			TestName = 'Rule Name';
		}
		if (($("#ItemName").val()=="") || ($("#ItemName").val()== TestName)) {
			if (type === 'I') {
				$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must enter a name for your new Item.</span>");
			}
			if (type === 'R') {
				$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must enter a name for your new Rule.</span>");
			}
			if (type === 'A') {
				$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must enter a name for your new Ability.</span>");
			}
			$("#UploadError").css("padding","5px");
		} else {
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/gamesystems.items.inc.php",
			data: "GameSystemID=" + id + "&ItemName=" + encodeURIComponent($("#ItemName").val()) + "&Action=" + encodeURIComponent(action) + "&ItemType=" + encodeURIComponent(type),
			success: function (responseText) 
				{
					if (responseText == 2) {
						var aerror = "<br />No Game System has been selected.";
					} else if (responseText == 3) {
						var aerror = "<br />That Game System does not exist.</span>";
					} else if (responseText == 4) {
						var aerror = "<br />That Game System does not belong to you.";
					} else if (responseText == 5) {
						var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS52221.";
					} else if (responseText == 6) {
						var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS53891.";
					} else if (responseText == 7) {
						var aerror = "<br />That Item Name has used illegal characters. Please make sure you only use website safe characters.";
					} else if (responseText == 8) {
						var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS11191.";
					} else if (responseText == 9) {
							var aerror = "<br />That Game System already has " + anitemname + " with that name.";
					} else {
						$("#itemsListed").html(responseText);
						$("#Named").html("<input type=\"text\" tabindex=\"55\" name=\"ItemName\" id=\"ItemName\" style=\"width: 250px;\" class=\"TextField\" value=\"" + TestName + "\" onFocus=\"if((!this._haschanged) && (this.value == '" + TestName + "')){this.value=''; this._haschanged=true; this.style.color = '#000'} else if ((this._haschanged) && (this.value==='')) { this.value='" + TestName + "'; this._haschanged=false; this.style.color = '#999'}\" onBlur=\"if ((this._haschanged) && (this.value==='')) { this.value='" + TestName + "'; this._haschanged=false; this.style.color = '#999'}\" />");
					}
					if (responseText > 0) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#UploadError").css("padding","5px");
					}
					loadedScript();
				}
			});
		}
	}

	if (action === 'itemDesc') {
		var ItemDescription = $("#ItemDescription").val();
		if ((($("#NewItemName").val()=="") || ($("#NewItemName").val()=="Item Name")) && ($("#DPrice").val() > 0)) {
			$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must enter a name for your " + itemname + " and ensure your Default Price is equal to 0 or higher.</span>");
			$("#NewUploadError").css("padding","5px");
		} else {
			alert($("#NewItemName").val());
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/gamesystems.items.inc.php",
			data: "ItemID=" + id + "&ItemName=" + encodeURIComponent($("#NewItemName").val()) + "&Action=" + encodeURIComponent(action) + "&ItemDescription=" + encodeURIComponent(ItemDescription) + "&DPrice=" + encodeURIComponent($("#DPrice").val()),
			success: function (responseText) 
				{
					
					if (responseText == 2) {
						var aerror = "<br />No " + itemname + " has been seleted.";
					} else if (responseText == 3) {
						var aerror = "<br />That " + itemname + " does not exist.";
					} else if (responseText == 4) {
						var aerror = "<br />That Game System does not exist.";
					} else if (responseText == 5) {
						var aerror = "<br />That Game System does not belong to you.";
					} else if (responseText == 6) {
						var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS53241.";
					} else if (responseText == 7) {
						var aerror = "<br />That " + itemname + " Description has used illegal characters. Please make sure you only use website safe characters.";
					} else if (responseText == 8) {
						var aerror = "<br />That " + itemname + " Name has used illegal characters. Please make sure you only use website safe characters.";
					} else {

						$("#descSaved").html("Data saved");
						$("#descSaved").css("display","block");
						$("#descSaved").fadeOut(2000);
						if (type === 'I') {
							gsContentSwitch('items', gsID, 'CellEven', 'CellOdd', 0, 'I');
						}
			
						if (type === 'R') {
							gsContentSwitch('rules', gsID, 'CellEven', 'CellOdd', 0, 'R');
						}
			
						if (type === 'A') {
							gsContentSwitch('abilities', gsID, 'CellEven', 'CellOdd', 0, 'A');
						}
					}
					if (responseText > 0) {
						$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#NewUploadError").css("padding","5px");
					}
					loadedScript();
				}
			});
		}
	}
	
	
	
	if ((action === 'statm') || (action === 'savem') || (action === 'statd') || (action === 'saved') || (action === 'statc') || (action === 'savec')) {
		var error = '';
		if (action === 'statd') {
			if (id > 0) {
				var StatModifierID = id;	
			} else {
				error = error + "<br />You must select a modification to delete.";
				no_errors = false;
			}
			if (no_errors == true) {
				var Send = "StatModifierID=" + StatModifierID + "&Action=" + action + "&ItemID=" + gsID;
			}
		}
		if (action === 'statc') {
			var ids = id.split("&&&");
			for (var i = 0; i < ids.length; i++) {
				if (($("#StatOperator" + ids[i]).val()!= '-') && ($("#StatOperator" + ids[i]).val()!= '+')) {
					error = "<br />Please make sure you have selected operators for your modifications.";
					no_errors = false;
					i = ids.length;
				} else {
	
				}
				if ($("#StatModifierValue" + ids[i]).val() > 0) {

				} else {
					error = "<br />Please make sure you have values for all your modifiers. They must be above 0.";
					no_errors = false;
					i = ids.length;
				}
				if (no_errors == true) {
					if (i == 0) {
						var StatOperator = $("#StatOperator" + ids[i]).val();	
						var StatModifierValue = $("#StatModifierValue" + ids[i]).val();	
					} else {
						StatOperator = StatOperator + "&&&" + $("#StatOperator" + ids[i]).val();	
						StatModifierValue = StatModifierValue + "&&&" + $("#StatModifierValue" + ids[i]).val();	
					}
				}
			}			if (no_errors == true) {
				var Send = "StatOperator=" + encodeURIComponent(StatOperator) + "&StatModifierValue=" + encodeURIComponent(StatModifierValue) + "&Action=" + action + "&StatModifierID=" + encodeURIComponent(id);
			}
		}
		if (action === 'statm') {
			if ($("#Stat").val()==0) {
				error = error + "<br />You must select a stat for the modifier from the dropdown list.";
				no_errors = false;
			} else {
				var Stat = $("#Stat").val();	
			}
			if (($("#StatOperator").val()!= '-') && ($("#StatOperator").val()!= '+')) {
				error = error + "<br />You must select a stat operator for the modifier from the dropdown list.";
				no_errors = false;
			} else {
				var StatOperator = $("#StatOperator").val();	
			}
			if (($("#StatModifierValue").val()=="") || ($("#StatModifierValue").val()=="Modifier")) {
				error = error + "<br />You must enter a modifier for the stat you are attempting to add as a modifier.";
				no_errors = false;
			} else {
				if ($("#StatModifierValue").val() > 0) {
					var StatModifierValue = $("#StatModifierValue").val();	
				} else {
					error = error + "<br />The value you enter for the modifier must be above 0.";
					no_errors = false;
				}
			}
			if (no_errors == true) {
				var Send = "StatID=" + Stat + "&StatOperator=" + encodeURIComponent(StatOperator) + "&StatModifierValue=" + StatModifierValue + "&Action=" + action + "&ItemID=" + id;
			}
		}
		if (action === 'saved') {
			if (id > 0) {
				var SaveModifierID = id;	
			} else {
				error = error + "<br />You must select a modification to delete.";
				no_errors = false;
			}
			if (no_errors == true) {
				var Send = "SaveModifierID=" + SaveModifierID + "&Action=" + action + "&ItemID=" + gsID;
			}
		}
		if (action === 'savec') {
			var ids = id.split("&&&");
			for (var i = 0; i < ids.length; i++) {
				if (($("#SaveOperator" + ids[i]).val()!= '-') && ($("#SaveOperator" + ids[i]).val()!= '+')) {
					error = "<br />Please make sure you have selected operators for your modifications.";
					no_errors = false;
					i = ids.length;
				} else {
	
				}
				if ($("#SaveModifierValue" + ids[i]).val() > 0) {

				} else {
					error = "<br />Please make sure you have values for all your modifiers. They must be above 0.";
					no_errors = false;
					i = ids.length;
				}
				if (no_errors == true) {
					if (i == 0) {
						var SaveOperator = $("#SaveOperator" + ids[i]).val();	
						var SaveModifierValue = $("#SaveModifierValue" + ids[i]).val();	
					} else {
						SaveOperator = SaveOperator + "&&&" + $("#SaveOperator" + ids[i]).val();	
						SaveModifierValue = SaveModifierValue + "&&&" + $("#SaveModifierValue" + ids[i]).val();	
					}
				}
			}
			if (no_errors == true) {
				var Send = "SaveOperator=" + encodeURIComponent(SaveOperator) + "&SaveModifierValue=" + encodeURIComponent(SaveModifierValue) + "&Action=" + action + "&SaveModifierID=" + encodeURIComponent(id);
			}
		}
		if (action === 'savem') {
			if ($("#Save").val()==0) {
				error = error + "<br />You must select a save for the modifier from the dropdown list.";
				no_errors = false;
			} else {
				var save = $("#Save").val();	
			}
			if (($("#SaveOperator").val()!= '-') && ($("#SaveOperator").val()!= '+')) {
				error = error + "<br />You must select a save operator for the modifier from the dropdown list.";
				no_errors = false;
			} else {
				var SaveOperator = $("#SaveOperator").val();	
			}
			if (($("#SaveModifierValue").val()=="") || ($("#SaveModifierValue").val()=="Modifier")) {
				error = error + "<br />You must enter a modifier for the save you are attempting to add as a modifier.";
				no_errors = false;
			} else {
				if ($("#SaveModifierValue").val() > 0) {
					var SaveModifierValue = $("#SaveModifierValue").val();	
				} else {
					error = error + "<br />The value you enter for the modifier must be above 0.";
					no_errors = false;
				}
			}
			if (no_errors == true) {
				var Send = "SaveID=" + save + "&SaveOperator=" + encodeURIComponent(SaveOperator) + "&SaveModifierValue=" + SaveModifierValue + "&Action=" + action + "&ItemID=" + id;
			}
		}
		if (no_errors == true) {
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/gamesystems.items.statsave.inc.php",
			data: Send,
			success: function (responseText) 
				{
					if (action === 'statm') {
						if (responseText == 2) {
							var aerror = "<br />No " + itemname + " has been selected.";
						} else if (responseText == 3) {
							var aerror = "<br />That " + itemname + " does not exist.";
						} else if (responseText == 4) {
							var aerror = "<br />No Stat has been selected.";
						} else if (responseText == 5) {
							var aerror = "<br />That Stat 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 if (responseText == 8) {
							var aerror = "<br />That Stat Modifier has already been added to this " + itemname + ".";
						} else if (responseText == 9) {
							var aerror = "<br />You must selected either a \"-\" or \"+\" operator for your modifier.";
						} else {
							$("#StatBox").html(responseText);
						}
					}
					if (action === 'statd') {
						if (responseText == 2) {
							var aerror = "<br />No " + itemname + " has been selected.";
						} else if (responseText == 3) {
							var aerror = "<br />That " + itemname + " does not exist.";
						} else if (responseText == 4) {
							var aerror = "<br />No Stat Modifier has been selected.";
						} else if (responseText == 5) {
							var aerror = "<br />That Stat Modifier does not exist.";
						} else if (responseText == 6) {
							var aerror = "<br />That Stat does not exist.";
						} else if (responseText == 7) {
							var aerror = "<br />That Game System does not exist.";
						} else if (responseText == 8) {
							var aerror = "<br />That Game System does not belong to you.";
						} else if (responseText == 9) {
							var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS99644.";
						} else {
							$("#StatBox").html(responseText);
						}
					}
					if (action === 'statc') {
						if (responseText == 2) {
							var aerror = "<br />There are fields missing for one or more of you Stat Modifiers. Please make sure everything is filled out correctly.";
						} else if (responseText == 3) {
							var aerror = "<br />That Stat Modifier does not exist.";
						} else if (responseText == 4) {
							var aerror = "<br />That Stat does not exist.";
						} else if (responseText == 5) {
							var aerror = "<br />That Item 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 if (responseText == 8) {
							var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS99644.";
						} else {
							$("#StatBox").html(responseText);
							$("#statSaved").html("Data saved");
							$("#statSaved").css("display","block");
							$("#statSaved").fadeOut(2000);
						}
					}
					if (action === 'savem') {
						if (responseText == 2) {
							var aerror = "<br />No " + itemname + " has been selected.";
						} else if (responseText == 3) {
							var aerror = "<br />That " + itemname + " does not exist.";
						} else if (responseText == 4) {
							var aerror = "<br />No Stat has been selected.";
						} else if (responseText == 5) {
							var aerror = "<br />That Stat 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 if (responseText == 8) {
							var aerror = "<br />That Save Modifier has already been added to this " + itemname + ".";
						} else if (responseText == 9) {
							var aerror = "<br />You must selected either a \"-\" or \"+\" operator for your modifier.";
						} else {
							$("#SaveBox").html(responseText);
						}
					}

					if (action === 'saved') {
						if (responseText == 2) {
							var aerror = "<br />No " + itemname + " has been selected.";
						} else if (responseText == 3) {
							var aerror = "<br />That " + itemname + " does not exist.";
						} else if (responseText == 4) {
							var aerror = "<br />No Save Modifier has been selected.";
						} else if (responseText == 5) {
							var aerror = "<br />That Save Modifier does not exist.";
						} else if (responseText == 6) {
							var aerror = "<br />That Save does not exist.";
						} else if (responseText == 7) {
							var aerror = "<br />That Game System does not exist.";
						} else if (responseText == 8) {
							var aerror = "<br />That Game System does not belong to you.";
						} else if (responseText == 9) {
							var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS99644.";
						} else {
							$("#SaveBox").html(responseText);
						}
					}
					if (action === 'savec') {
						if (responseText == 2) {
							var aerror = "<br />There are fields missing for one or more of your Save Modifiers. Please make sure everything is filled out correctly.";
						} else if (responseText == 3) {
							var aerror = "<br />That Save Modifier does not exist.";
						} else if (responseText == 4) {
							var aerror = "<br />That Save does not exist.";
						} else if (responseText == 5) {
							var aerror = "<br />That Item 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 if (responseText == 8) {
							var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS93664.";
						} else {
							$("#SaveBox").html(responseText);
							$("#saveSaved").html("Data saved");
							$("#saveSaved").css("display","block");
							$("#saveSaved").fadeOut(2000);
						}
					}
					loadedScript();
					if (responseText > 0) {
						$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#NewUploadError").css("padding","5px");
					}
				}
			});
		} else {
			$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + error +"</span>");
			$("#NewUploadError").css("padding","5px");
		}
	}

	if ((action === 'open') || (action === 'delete') || (action === 'duplicate')) {
		
		if (id > 0) {
			if (action == 'delete') {

				if (type === 'A') {
					var deleteme = confirm("Are you sure you want to delete this Ability? This information will not be retrievable.");
				}
				if (type === 'I') {
					var deleteme = confirm("Are you sure you want to delete this Item? This information will not be retrievable.");
				}
				if (type === 'R') {
					var deleteme = confirm("Are you sure you want to delete this Rule? This information will not be retrievable.");
				}
				if (deleteme == true) {
					noErrors = true;
				} else {
					noErrors = false;
				}
			} else {
				noErrors = true;
			}
			if (noErrors == true) {
				$.ajax({
				beforeSend: loadingScript(),
				type: "POST",
				url: "validatescripts/builder/ajax/gamesystems.items.inc.php",
				data: "ItemID=" + id + "&Action=" + encodeURIComponent(action) + "&ItemType=" + encodeURIComponent(type),
				success: function (responseText) 
					{
						if (action == 'open') {
							if (responseText == 2) {
								var aerror = "<br />No Item has been selected.";
							} else if (responseText == 3) {
								var aerror = "<br />That Item does not exist.</span>";
							} else if (responseText == 4) {
								var aerror = "<br />That Game System does not exist.";
							} else if (responseText == 5) {
								var aerror = "<br />That Game System does not belong to you.";
							} else if (responseText == 6) {
								var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS58446.";
							} else {
								$('#dialogprofile').dialog('destroy');		
								$("#dialogprofile").html(responseText);
								title = title.replace("***", "'")
								$("#dialogprofile").attr("title", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Edit Item: " + title);
								$('#dialogprofile').dialog('open');			
								$("#dialogprofile").dialog({
									bgiframe: false,
									closeOnEscape: true,
									draggable: true,
									resizable: false,
									width: 632,
									modal: true,
								});
							}

						}
						if (action == 'delete') {
							if (responseText == 2) {
									var aerror = "<br />No " + itemname + " has been seleted.";
							} else if (responseText == 3) {
									var aerror = "<br />That " + itemname + " does not exist.</span>";
							} else if (responseText == 4) {
								var aerror = "<br />That Game System does not exist.";
							} else if (responseText == 5) {
								var aerror = "<br />That Game System does not belong to you.";
							} else if (responseText == 6) {
								var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS96341.";
							} else {
								$("#itemsListed").html(responseText);
							}
						}
						if (action == 'duplicate') {
							if (responseText == 2) {
									var aerror = "<br />No " + itemname + " has been seleted.";
							} else if (responseText == 3) {
									var aerror = "<br />That " + itemname + " does not exist.</span>";
							} else if (responseText == 4) {
								var aerror = "<br />That Game System does not exist.";
							} else if (responseText == 5) {
								var aerror = "<br />That Game System does not belong to you.";
							} else if (responseText == 6) {
								var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS11141.";
							} else {
								$("#itemsListed").html(responseText);
							}
						}
						loadedScript();
						if (responseText > 0) {
							$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
							$("#UploadError").css("padding","5px");
						}
					}

				});
			}
		}
	}
}
function unitEdit(action, id, title, UnitID, costs) {	

	$("#NewUploadError").html("");
	$("#UploadError").html("");
	$("#NewUploadError").css("padding","0px");
	$("#UploadError").css("padding","0px");
	var noErrors = true;

	
	if (action === 'create') {

		if (($("#UnitName" + id).val()=="" + id) || ($("#UnitName" + id).val()== "Unit Name")) {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must enter a name for your new Unit.</span>");

			$("#UploadError").css("padding","5px");
		} else {

			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/factions.units.inc.php",
			data: "FactionID=" + id + "&UnitName=" + encodeURIComponent($("#UnitName" + id).val()) + "&Action=" + encodeURIComponent(action),
			success: function (responseText) 
				{
					if (responseText == 2) {
						var aerror = "<br />No Faction has been selected.";
					} else if (responseText == 3) {
						var aerror = "<br />That Faction does not exist.</span>";
					} else if (responseText == 4) {
						var aerror = "<br />That Game System does not exist.</span>";
					} else if (responseText == 5) {
						var aerror = "<br />That Game System does not belong to you.";
					} else if (responseText == 6) {
						var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS44441.";
					} else if (responseText == 7) {
						var aerror = "<br />That Item Name has used illegal characters. Please make sure you only use website safe characters.";
					} else if (responseText == 8) {
						var aerror = "<br />That Unit has already been added to this Faction.";
					} else {
						$("#unitsListed" + id).html(responseText);
						$("#Named").html("<input type=\"text\" tabindex=\"55\" name=\"UnitName" + id + "\" id=\"UnitName" + id + "\" style=\"width: 250px;\" class=\"TextField\" value=\"Unit Name\" onFocus=\"if((!this._haschanged) && (this.value == 'Unit Name')){this.value=''; this._haschanged=true; this.style.color = '#000'} else if ((this._haschanged) && (this.value==='')) { this.value='Unit Name'; this._haschanged=false; this.style.color = '#999'}\" onBlur=\"if ((this._haschanged) && (this.value==='')) { this.value='Unit Name'; this._haschanged=false; this.style.color = '#999'}\" />");
					}
					if (responseText > 0) {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#UploadError").css("padding","5px");
					}
					loadedScript();
				}
			});
		}
	}
	
	if (action === 'modifystats') {
		var Error = '';
		var ids = id.split("&&&");
		var numbers = costs.split("&&&");
		for (var i = 0; i < ids.length; i++) {
			if (($("#Stat" + ids[i] + "C" + numbers[i]).val() >= 0) || ($("#Stat" + ids[i] + "C" + numbers[i]).val() == '-')) {
				if (i == 0) {
					var values = $("#Stat" + ids[i] + "C" + numbers[i]).val();
				} else {
					values = values + "&&&" + $("#Stat" + ids[i] + "C" + numbers[i]).val();
				}
			} else {
				error = "<br />Please make sure you have input values for your stats.";
				no_errors = false;
				i = ids.length;
			}
		}	
		if (noErrors == false) {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + error + "</span");
			$("#UploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&UnitID=" + UnitID + "&StatIDs=" + encodeURIComponent(id) + "&Values=" + encodeURIComponent(values) + "&Numbers=" + encodeURIComponent(costs);
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/factions.units.inc.php",
			data: Send,
			success: function (responseText) 
				{
					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 Faction does 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 if (responseText == 7) {
						var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS557764.";
					} else {
						$("#allstatlinesSaved").html("Data saved");
						$("#allstatlinesSaved").css("display","block");
						$("#allstatlinesSaved").fadeOut(2000);
					}
					loadedScript();
					if (responseText > 0) {
						$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#NewUploadError").css("padding","5px");
					}
				}
			});
		}
	}

	if (action === 'addstats') {
		var error = '';
		noErrors = true;
		var ids = id.split("&&&");
		for (var i = 0; i < ids.length; i++) {
			if ($("#StatLine" + ids[i]).val() != '') {
				if (i == 0) {
					var values = $("#StatLine" + ids[i]).val();
				} else {
					values = values + "&&&" + $("#StatLine" + ids[i]).val();
				}
			} else {
				error = "<br />Please make sure you have input values for your stats.";
				noErrors = false;
				i = ids.length;
			}
		}		
		if (($("#NewStatLabel").val()=="") || ($("#NewStatLabel").val() === "Label")) {
			error = "<br />You must enter a Label for your new Stat.";
			noErrors = false;
		} else {
			var label = $("#NewStatLabel").val();
		}
		if (noErrors == false) {
			$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + error + "</span");
			$("#NewUploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&UnitID=" + UnitID + "&IDs=" + encodeURIComponent(id) + "&Values=" + encodeURIComponent(values) + "&Label=" + encodeURIComponent(label);
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/factions.units.inc.php",
			data: Send,
			success: function (responseText) 
				{
					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 Faction does 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 if (responseText == 7) {
						var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS204344.";
					} else if (responseText == 8) {
						var aerror = "<br />There is an issue with one or more of the values you have supplied for your new Stats. Please make sure that all of the values entered are allowed within the limits of the Game System that this unit belongs to.";
					} else {
						var ids = id.split("&&&");
						for (var i = 0; i < ids.length; i++) {
							$("#StatLine" + ids[i]).val(0);
						}	
						$("#statlineSaved").html("Stat added");
						$("#statlineSaved").css("display","block");
						$("#statlineSaved").fadeOut(2000);
						$("#UnitsStats").html(responseText);
						$("#NewStatLabel").val("Label");
						$("#NewStatLabel").css("color", "#000");
					}
					loadedScript();
					if (responseText > 0) {
						$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#NewUploadError").css("padding","5px");
					}
				}
			});
		}
	}


	if (action === 'deletestats') {
		var error = '';
		noErrors = true;
		var ids = id.split("&&&");
		if (id > 0) {

			var Send = "Action=" + action + "&UnitID=" + encodeURIComponent(UnitID) + "&Number=" + encodeURIComponent(id);
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/factions.units.inc.php",
			data: Send,
			success: function (responseText) 
				{
					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 Faction does 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 if (responseText == 7) {
						var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS249714.";
					} else {

						$("#UnitsStats").html(responseText);
						$("#NewStatLabel").val("Label");
						$("#NewStatLabel").css("color", "#000");
						$("#statlineSaved").html("Stat deleted");
						$("#statlineSaved").css("display","block");
						$("#statlineSaved").fadeOut(2000);
					}
					loadedScript();
					if (responseText > 0) {
						$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#NewUploadError").css("padding","5px");
					}
				}
			});
		} else {
			$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">No Stat has been selected.</span");
			$("#NewUploadError").css("padding","5px");
		}
	}

	if (action === 'unitDesc') {
		var UnitDescription = $("#UnitDescription").val();
		if (($("#NewUnitName").val()=="") || ($("#NewUnitName").val()=="Unit Name")) {
			var aerror = "<br />That Game System does not exist.";
			noErrors = false;
		}
		var typecosts = '';
		var ids = costs.split("&&&");
		for (var i = 0; i < ids.length; i++) {
			if ($("#typecost" + ids[i]).val() >= 0) {
				if (i == 0) {
					typecosts = $("#typecost" + ids[i]).val();
				} else {
					typecosts = typecosts + "&&&" + $("#typecost" + ids[i]).val();
				}				
			} else {
				var aerror = "<br />Please make sure that all of your Unit Type Costs are equal to or greater than 0.";
				noErrors = false;
				i = ids.length;
			}
		}
		if ($("#BasePoints").val() > 0) {
									  
		} else {
			var aerror = "<br />You must have a positive number for your Unit's Base Points.";
			noErrors = false;
		}
		if ($("#MinQuan").val() > 0) {
									  
		} else {
			var aerror = "<br />You must have a positive number for your Unit's Minimum Quantity.";
			noErrors = false;
		}
		if (($("#MaxQuan").val() > 0) || ($("#MaxQuan").val() == 'Unlimited') || ($("#MaxQuan").val() == 'unlimited')) {
									  
		} else {
			var aerror = "<br />You must have a positive number for your Unit's Maximum Quantity or, if you wish there to be no limit just enter in \"unlimited\" as the value.";
			noErrors = false;
		}

		if (noErrors == false) {
			$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
			$("#NewUploadError").css("padding","5px");
		} else {
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/factions.units.inc.php",
			data: "UnitID=" + UnitID + "&UnitName=" + encodeURIComponent($("#NewUnitName").val()) + "&BasePoints=" + $("#BasePoints").val() + "&MaxQuan=" + $("#MaxQuan").val() + "&MinQuan=" + $("#MinQuan").val() + "&Action=" + encodeURIComponent(action) + "&UnitDescription=" + encodeURIComponent(UnitDescription) + "&Costs=" + encodeURIComponent(costs) + "&Values=" + encodeURIComponent(typecosts),
			success: function (responseText) 
				{
					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 Faction does 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 if (responseText == 7) {
						var aerror = "<br />That Unit Description has used illegal characters. Please make sure you only use website safe characters.";
					} else if (responseText == 8) {
						var aerror = "<br />That Unit Name has used illegal characters. Please make sure you only use website safe characters.";
					} else if (responseText == 9) {
						var aerror = "<br />You must enter a name for your Unit.";
					} else {
						gsContentSwitch('units', id,'CellEven','CellOdd', '0');
						$("#descSaved").html("Data saved");
						$("#descSaved").css("display","block");
						$("#descSaved").fadeOut(2000);
					}
					if (responseText > 0) {
						$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#NewUploadError").css("padding","5px");
					}
					loadedScript();
				}
			});
		}
	}
	
	if ((action === 'open') || (action === 'delete') || (action === 'duplicate')) {
		if (id > 0) {
			if (action == 'delete') {
				var deleteme = confirm("Are you sure you want to delete this Unit? This information will not be retrievable.");
				if (deleteme == true) {
					noErrors = true;
				} else {
					noErrors = false;
				}
			} else {
				noErrors = true;
			}
			if (noErrors == true) {
				$.ajax({
				beforeSend: loadingScript(),
				type: "POST",
				url: "validatescripts/builder/ajax/factions.units.inc.php",
				data: "UnitID=" + UnitID + "&Action=" + encodeURIComponent(action) + "&Type=I",
				success: function (responseText) 
					{

						if (action == 'open') {
							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 Faction does 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 if (responseText == 7) {
								var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS557764.";
							} else if (responseText == 8) {
								var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS553214.";
							} else {
								$('#dialogprofile').dialog('destroy');		
								$("#dialogprofile").html(responseText);
								title = title.replace("***", "'")
								$("#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,
								});
							}

						}
						if ((action === 'delete') || (action == 'duplicate')) {
							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 Faction does 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 if (responseText == 7) {
								var aerror = "<br />An error has occurred please try again or contact a system administrator with error code: ARGS557764.";
							} else {
								$("#unitsListed" + id).html(responseText);
							}
							
						}

						loadedScript();
						if (responseText > 0) {
							$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
							$("#UploadError").css("padding","5px");
						}
					}

				});
			}
		}
	}
}

function unitGroups(action, id, previous, type) {	

	var noErrors = true;
	$("#NewUploadError").html("");
	$("#NewUploadError").css("padding","0px");
	if (action === 'open') {
		if (id > 0) {
			var Send = "Action=" + action + "&UnitItemGroupID=" + id;
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/factions.unititems.inc.php",
			data: Send,
			success: function (responseText) 
				{
		
					if (action == 'open') {
						if (responseText == 2) {
							var aerror = "<br />No Unit Item Group has been selected.";
						} else if (responseText == 3) {
							var aerror = "<br />That Unit Item Group does not exist.";
						} else if (responseText == 4) {
							var aerror = "<br />That Item Group does not exist.";
						} else if (responseText == 5) {
							var aerror = "<br />That Unit does not exist.";
						} else if (responseText == 6) {
							var aerror = "<br />That Faction does not exist.";
						} else if (responseText == 7) {
							var aerror = "<br />That Game System does not exist.";
						} else if (responseText == 8) {
							var aerror = "<br />That Game System does not belong to you.";
						} else {
							$("#itemGroupSwitch").html(responseText);
							var ids = previous.split("&&&");
							for (var i = 0; i < ids.length; i++) {
								if (ids[i] == id) {
									$("#" + id).css("background-color","#662b2b");
									$("#" + id).css("background-image","url(images/builder/section_open.png)");
								} else {
									$("#" + ids[i]).css("background-color","");
									$("#" + ids[i]).css("background-image","");
								}
							}
						}
		
					}

					loadedScript();
					if (responseText > 0) {
						$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#NewUploadError").css("padding","5px");
					}
				}
		
			});
		}
	}
	if (action === 'preselected') {
		if ($("#Preselected").val() == 0) {
			$("#Preselected").val(1);
		} else {
			$("#Preselected").val(0);
		}
	}
	if (action === 'deselectable') {
		if ($("#Deselectable").val() == 0) {
			$("#Deselectable").val(1);
		} else {
			$("#Deselectable").val(0);
		}
	}
	if ((action === 'openitem') || (action === 'saveitem')) {
		if (id > 0) {
			if (action === 'openitem') {
				
				var Send = "Action=" + action + "&UnitItemGroupItemID=" + id + "&Type=" + type;
				
			} else if (action === 'saveitem') {
				
				if (($("#Price").val() > 0) || ($("#Price").val() == 0)) {
					var price = $("#Price").val();	
				} else {
					$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must enter a value equal to or greater than 0 for your Price.</span>");
					$("#NewUploadError").css("padding","5px");
					noErrors = false;
				}
				
				if (($("#Quantity").val() > 0) || ($("#Quantity").val() == 0)) {
					if (($("#OverwriteQuan").val() == 0) && ($("#Quantity").val() > 0)) {
						$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must \"Allow Quantity Overwrite\" to set a value for your \"Preset Quantity\".</span>");
						$("#NewUploadError").css("padding","5px");
						noErrors = false;
					} else {
						var qauntity = $("#Quantity").val();	
					}
				} else {
					$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />You must enter a value equal to or greater than 0 for your Preset Quantity.</span>");
					$("#NewUploadError").css("padding","5px");
					noErrors = false;
				}
				if ($("#Preselected").val() == 0) {
					var value = 0;
				} else if ($("#Preselected").val() == 1) {
					var value = 1;
				}
				if ($("#Multiply").val() == 'Q') {
					var multiply = 'Q';
				} else if ($("#Multiply").val() == 'S') {
					var multiply = 'S';
				} else {
					var multiply = 'N';
				}
				if ($("#Deselectable").val() == 0) {
					var dvalue = 0;
				} else if ($("#Deselectable").val() == 1) {
					var dvalue = 1;
				}
				var Send = "Action=" + action + "&UnitItemGroupItemID=" + id + "&Price=" + price + "&Preselected=" + value + "&Deselectable=" + dvalue + "&Quantity=" + qauntity + "&Multiply=" + multiply;	
			}
			if (noErrors == true) {
				$.ajax({
				beforeSend: loadingScript(),
				type: "POST",
				url: "validatescripts/builder/ajax/factions.unititems.inc.php",
				data: Send,
				success: function (responseText) 
					{
			
						if (responseText == 2) {
							var aerror = "<br />No Group Item has been selected.";
						} else if (responseText == 3) {
							var aerror = "<br />That Group Item does not exist.";
						} else if (responseText == 4) {
							var aerror = "<br />That Unit Item Group does not exist.";
						} else if (responseText == 5) {
							var aerror = "<br />That Item Group does not exist.";
						} else if (responseText == 6) {
							var aerror = "<br />That Unit does not exist.";
						} else if (responseText == 7) {
							var aerror = "<br />That Faction does not exist.";
						} else if (responseText == 8) {
							var aerror = "<br />That Game System does not exist.";
						} else if (responseText == 9) {
							var aerror = "<br />That Game System does not belong to you.";
						} else if (responseText == 10) {
							if (type == 'I') {
								var aerror = "<br />Sorry, but you already have an item preselected for the group. If you wish to have more than one item preselected, you must allow for multple selections located at the top of the options panel below.";
							} else if (type == 'A') {
								var aerror = "<br />Sorry, but you already have an ability preselected for the group. If you wish to have more than one ability preselected, you must allow for multple selections located at the top of the options panel below.";
							} else if (type == 'R') {
								var aerror = "<br />Sorry, but you already have an rule preselected for the group. If you wish to have more than one rule preselected, you must allow for multple selections located at the top of the options panel below.";
							}
						} else {
							$("#itemsselectedoptions").html(responseText);
							if (action === 'openitem') {
								var ids = previous.split("&&&");
								for (var i = 0; i < ids.length; i++) {
									if (ids[i] == id) {
										$("#item" + id).css("background-color","#662b2b");
										$("#item" + id).css("background-image","url(images/builder/section_open.png)");
									} else {
										$("#item" + ids[i]).css("background-color","");
										$("#item" + ids[i]).css("background-image","");
									}
								}
							} else if (action === 'saveitem') {
								$("#itemSaved").html("Data Saved");
								$("#itemSaved").css("display","block");
								$("#itemSaved").fadeOut(2000);
							}
						}
	
						loadedScript();
						if (responseText > 0) {
							$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
							$("#NewUploadError").css("padding","5px");
						}
					}
			
				});
			}
		}
	}
	
	if (action === 'managegroupsswitch') {
		if (id > 0) {
			var Send = "Action=" + action + "&UnitID=" + id + "&Type=" + type;
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/factions.unitoptiongroups.inc.php",
			data: Send,
			success: function (responseText) 
				{
				$("#manageGroups").html(responseText);
				if (type == "I") {
					$("#itemgroupbutton").css("background-color","#662b2b");
					$("#itemgroupbutton").css("background-image","url(images/builder/section_open.png)");
					$("#abilitygroupbutton").css("background-color","");
					$("#abilitygroupbutton").css("background-image","url(images/builder/section_closed.png)");
					$("#rulegroupbutton").css("background-color","");
					$("#rulegroupbutton").css("background-image","url(images/builder/section_closed.png)");
				} else if (type == "A") {
					$("#itemgroupbutton").css("background-color","");
					$("#itemgroupbutton").css("background-image","url(images/builder/section_closed.png)");
					$("#abilitygroupbutton").css("background-color","#662b2b");
					$("#abilitygroupbutton").css("background-image","url(images/builder/section_open.png)");
					$("#rulegroupbutton").css("background-color","");
					$("#rulegroupbutton").css("background-image","url(images/builder/section_closed.png)");
				} else if (type == "R") {
					$("#itemgroupbutton").css("background-color","");
					$("#itemgroupbutton").css("background-image","url(images/builder/section_closed.png)");
					$("#abilitygroupbutton").css("background-color","");
					$("#abilitygroupbutton").css("background-image","url(images/builder/section_closed.png)");
					$("#rulegroupbutton").css("background-color","#662b2b");
					$("#rulegroupbutton").css("background-image","url(images/builder/section_open.png)");
				}

				loadedScript();
				}
			});
		}
	}
	
	
	if (action === 'addgroup') {
		if (id == 0) {
			var aerror = "<br />No Unit has been selected.";
			noErrors = false;
		}
		if ($("#ItemGroup").val() > 0) {
			var itemgroup = $("#ItemGroup").val();
		} else {
			var aerror = "<br />You must select a Group from the dropdown list.";
			noErrors = false;
		}
		if (noErrors == false) {
			$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
			$("#NewUploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&UnitID=" + id + "&ItemGroupID=" + itemgroup + "&Type=" + type;
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/factions.unititems.inc.php",
			data: Send,
			success: function (responseText) 
				{
					if (responseText == 2) {
						var aerror = "<br />No Unit has been selected.";
					} else if (responseText == 3) {
						if (type == "I") {
							var aerror = "<br />No Item Group has been selected.";
						} else if (type == "A") {
							var aerror = "<br />No Ability Group has been selected.";
						} else if (type == "R") {
							var aerror = "<br />No Rule Group has been selected.";
						}
					} else if (responseText == 4) {
						if (type == "I") {
							var aerror = "<br />That Item Group does not exist.";
						} else if (type == "A") {
							var aerror = "<br />That Ability Group does not exist.";
						} else if (type == "R") {
							var aerror = "<br />That Rule Group does not exist.";
						}
					} else if (responseText == 5) {
						var aerror = "<br />That Unit does not exist.";
					} else if (responseText == 6) {
						var aerror = "<br />That Faction does not exist.";
					} else if (responseText == 7) {
						var aerror = "<br />That Game System does not exist.";
					} else if (responseText == 8) {
						var aerror = "<br />That Game System does not belong to you.";
					} else if (responseText == 9) {
						if (type == "I") {
							var aerror = "<br />That Item Group has already been added to this Unit.";
						} else if (type == "A") {
							var aerror = "<br />That Ability Group has already been added to this Unit.";
						} else if (type == "R") {
							var aerror = "<br />That Rule Group has already been added to this Unit.";
						}
					} else {
						$("#unititemgrouplist").html(responseText);
						$("#itemGroupSwitch").html("");

					}
					loadedScript();
					if (responseText > 0) {
						$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#NewUploadError").css("padding","5px");
					}
				}
			});
		}
	}
	
	if (action === 'additem') {
		if (id == 0) {
			var aerror = "<br />No Item has been selected.";
			noErrors = false;
		}
		if ($("#Item").val() > 0) {
			var items = $("#Item").val();
		} else {
			if (type == "I") {
				var aerror = "<br />You must select an Item from the dropdown list.";
			} else if (type == "A") {
				var aerror = "<br />You must select an Ability from the dropdown list.";
			} else if (type == "R") {
				var aerror = "<br />You must select a Rule from the dropdown list.";
			}
			noErrors = false;
		}
		if (noErrors == false) {
			$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
			$("#NewUploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&ItemID=" + items + "&UnitID=" + previous + "&UnitItemGroupID=" + id + "&Type=" + type;
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/factions.unititems.inc.php",
			data: Send,
			success: function (responseText) 
				{
					if (responseText == 2) {
						var aerror = "<br />No Unit has been selected.";
					} else if (responseText == 3) {
						if (type == "I") {
							var aerror = "<br />No Item Group has been selected.";
						} else if (type == "A") {
							var aerror = "<br />No Ability Group has been selected.";
						} else if (type == "R") {
							var aerror = "<br />No Rule Group has been selected.";
						}
					} else if (responseText == 4) {
						if (type == "I") {
							var aerror = "<br />No Item has been selected.";
						} else if (type == "A") {
							var aerror = "<br />No Ability has been selected.";
						} else if (type == "R") {
							var aerror = "<br />No Rule has been selected.";
						}
					} else if (responseText == 5) {
						if (type == "I") {
							var aerror = "<br />That Item Group does not exist.";
						} else if (type == "A") {
							var aerror = "<br />That Ability Group does not exist.";
						} else if (type == "R") {
							var aerror = "<br />That Rule Group does not exist.";
						}
					} else if (responseText == 6) {
						if (type == "I") {
							var aerror = "<br />That Item Group does not exist.";
						} else if (type == "A") {
							var aerror = "<br />That Ability Group does not exist.";
						} else if (type == "R") {
							var aerror = "<br />That Rule Group does not exist.";
						}
					} else if (responseText == 7) {
						if (type == "I") {
							var aerror = "<br />That Item Group does not exist.";
						} else if (type == "A") {
							var aerror = "<br />That Ability Group does not exist.";
						} else if (type == "R") {
							var aerror = "<br />That Rule Group does not exist.";
						}
					} else if (responseText == 8) {
						var aerror = "<br />That Unit does not exist.";
					} else if (responseText == 9) {
						var aerror = "<br />That Faction does not exist.";
					} else if (responseText == 10) {
						var aerror = "<br />That Game System does not exist.";
					} else if (responseText == 11) {
						var aerror = "<br />That Game System does not belong to you.";
					} else if (responseText == 12) {
						if (type == "I") {
							var aerror = "<br />That Item has already been added to this Unit.";
						} else if (type == "A") {
							var aerror = "<br />That Ability has already been added to this Unit.";
						} else if (type == "R") {
							var aerror = "<br />That Rule has already been added to this Unit.";
						}
					} else {
						$("#itemsselected").html(responseText);
						$("#itemsselectedoptions").html("");
					}
					loadedScript();
					if (responseText > 0) {
						$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#NewUploadError").css("padding","5px");
					}
				}
			});
		}
	}
	if (action === 'deletegroup') {
		if (id == 0) {
			var aerror = "<br />No Group has been selected.";
			noErrors = false;
		}

		if (noErrors == false) {
			$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
			$("#NewUploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&UnitItemGroupID=" + id + "&Type=" + type;
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/factions.unititems.inc.php",
			data: Send,
			success: function (responseText) 
				{
					if (responseText == 2) {
						if (type == "I") {
							var aerror = "<br />No Item Group has been selected.";
						} else if (type == "A") {
							var aerror = "<br />No Ability Group has been selected.";
						} else if (type == "R") {
							var aerror = "<br />No Rule Group has been selected.";
						}
					} else if (responseText == 3) {
						if (type == "I") {
							var aerror = "<br />No Item Group has been selected.";
						} else if (type == "A") {
							var aerror = "<br />No Ability Group has been selected.";
						} else if (type == "R") {
							var aerror = "<br />No Rule Group has been selected.";
						}
					} else if (responseText == 4) {
						if (type == "I") {
							var aerror = "<br />That Item Group does not exist.";
						} else if (type == "A") {
							var aerror = "<br />That Ability Group does not exist.";
						} else if (type == "R") {
							var aerror = "<br />That Rule Group does not exist.";
						}
					} else if (responseText == 5) {
						var aerror = "<br />That Unit does not exist.";
					} else if (responseText == 6) {
						var aerror = "<br />That Faction does not exist.";
					} else if (responseText == 7) {
						var aerror = "<br />That Game System does not exist.";
					} else if (responseText == 8) {
						var aerror = "<br />That Game System does not belong to you.";
					} else if (responseText == 9) {
						if (type == "I") {
							var aerror = "<br />That Item Group has already been added to this Unit.";
						} else if (type == "A") {
							var aerror = "<br />That Ability Group has already been added to this Unit.";
						} else if (type == "R") {
							var aerror = "<br />That Rule Group has already been added to this Unit.";
						}
					} else {
						$("#unititemgrouplist").html(responseText);
						$("#itemGroupSwitch").html("");

					}
					loadedScript();
					if (responseText > 0) {
						$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#NewUploadError").css("padding","5px");
					}
				}
			});
		}
	}
	if (action === 'deleteitem') {
		if (id == 0) {
			var aerror = "<br />No Item has been selected.";
			noErrors = false;
		}

		if (noErrors == false) {
			$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
			$("#NewUploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&UnitItemGroupItemID=" + id + "&Type=" + type;
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/factions.unititems.inc.php",
			data: Send,
			success: function (responseText) 
				{
					if (responseText == 2) {
						if (type == "I") {
							var aerror = "<br />No Item has been selected.";
						} else if (type == "A") {
							var aerror = "<br />No Ability has been selected.";
						} else if (type == "R") {
							var aerror = "<br />No Rule has been selected.";
						}
					} else if (responseText == 3) {
						if (type == "I") {
							var aerror = "<br />That Item does not exist.";
						} else if (type == "A") {
							var aerror = "<br />That Ability does not exist.";
						} else if (type == "R") {
							var aerror = "<br />That Rule does not exist.";
						}
					} else if (responseText == 4) {
						if (type == "I") {
							var aerror = "<br />That Item Group does not exist.";
						} else if (type == "A") {
							var aerror = "<br />That Ability Group does not exist.";
						} else if (type == "R") {
							var aerror = "<br />That Rule Group does not exist.";
						}
					} else if (responseText == 5) {
						if (type == "I") {
							var aerror = "<br />That Item Group does not exist.";
						} else if (type == "A") {
							var aerror = "<br />That Ability Group does not exist.";
						} else if (type == "R") {
							var aerror = "<br />That Rule Group does not exist.";
						}
					} else if (responseText == 6) {
						if (type == "I") {
							var aerror = "<br />That Item does not exist.";
						} else if (type == "A") {
							var aerror = "<br />That Ability does not exist.";
						} else if (type == "R") {
							var aerror = "<br />That Rule does not exist.";
						}
					} else if (responseText == 7) {
						var aerror = "<br />That Unit does not exist.";
					} else if (responseText == 8) {
						var aerror = "<br />That Faction does not exist.";
					} else if (responseText == 9) {
						var aerror = "<br />That Game System does not exist.";
					} else if (responseText == 10) {
						var aerror = "<br />That Game System does not belong to you.";
					} else {
						$("#itemsselected").html(responseText);
						$("#itemsselectedoptions").html("");
					}
					loadedScript();
					if (responseText > 0) {
						$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
						$("#NewUploadError").css("padding","5px");
					}
				}
			});
		}
	}
	
	if ((action === 'overwrite') || (action === 'multiple')) {
		var Value = 0;
		var OValue = 0;
		var MValue = 0;
		

		

		if (action === 'overwrite') {
			if ($("#OverwriteQuan").val() == 0) {
				$("#OverwriteQuan").val(1);
				OValue = 1;
			} else {
				$("#OverwriteQuan").val(0);
				OValue = 0;
			}
			Value = OValue;
		}
		if (action === 'multiple') {
			if ($("#MultipleSelections").val() == 0) {
				$("#MultipleSelections").val(1);
				MValue = 1;
			} else {
				$("#MultipleSelections").val(0);
				MValue = 0;
			}
			Value = MValue;
		}
		if (noErrors == false) {
			$("#NewUploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\">" + aerror + "</span>");
			$("#NewUploadError").css("padding","5px");
		} else {
			var Send = "Action=" + action + "&UnitItemGroupID=" + id + "&Value=" + Value;
			$.ajax({
			beforeSend: loadingScript(),
			type: "POST",
			url: "validatescripts/builder/ajax/factions.unititems.inc.php",
			data: Send,
			success: function (responseText) 
				{
					loadedScript();
				}
			});
		}
	}
}

function faction(action, id, span, accordionActive) {
	if (accordionActive >= 0) {
	} else {
		var accordionActive = false;
	}
	var noErrors = true;
	$("#UploadError").html("");
	$("#UploadError").css("padding","0px");
	if (action === 'create') {
		if (($("#Faction").val()=="") || ($("#Faction").val()=="Faction Name")) {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />Enter a name for your Faction");
			$("#UploadError").css("padding","5px");
			noErrors = false;
		} else {
			var faction = $("#Faction").val();
			var gameSystem = $("#GameSystem").val();
		}
		var Send = "Action=" + action + "&Faction=" + encodeURIComponent(faction) + "&GameSystem=" + encodeURIComponent(gameSystem);
	}
	
	if (action === 'delete') {
		if (id > 0) {
			var deleteme = confirm("Are you sure you want to delete this Faction?\n\nBy doing this you will delete ALL relative data. This includes all Army Lists, Units, and Items. In addition all other information that relates to it such as reviews and comments will be deleted.\n\nThis information will NOT be retrievable.");
			if (deleteme == true) {
				var Send = "Action=" + action + "&Faction=" + id;
			} else {
				noErrors = false;
			}
		} else {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />No Game System has been selected.</span>");
			$("#UploadError").css("padding","5px");
			noErrors = false;
		}
	}
	if (action === 'duplicate') {
		if (id > 0) {
			var Send = "Action=" + action + "&Faction=" + id;
		} else {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />No Faction has been selected.</span>");
			$("#UploadError").css("padding","5px");
			noErrors = false;
		}
	}
	if (action === 'savechanges') {
		var Error = '';
		if (($("#Faction" + id).val()=="") || ($("#Faction" + id).val()=="Faction Name")) {
			$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />Enter a name for your Faction.</span>");
			$("#UploadError").css("padding","5px");
			noErrors = false;
		} else {
			var faction = $("#Faction" + id).val();
		}
		var Send = "Action=" + action + "&Faction=" + id + "&FactionName=" + encodeURIComponent(faction);
	}

	if (noErrors == true) {
		$.ajax({
		beforeSend: loadingScript(),
		type: "POST",
		url: "validatescripts/builder/ajax/faction.inc.php",
		data: Send,
		success: function (responseText) 
			{
				if (action === 'create') {
					if (responseText == 1) {
						newhash('action=factions&Area=overview');
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />" + responseText);
						$("#UploadError").css("padding","5px");
					}
					loadedScript();
				}
				if ((action === 'delete') || (action === 'duplicate')) {
					if (responseText == 1) {
						AR_getter(accordionActive);
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />" + responseText);
						$("#UploadError").css("padding","5px");
					}
					loadedScript();
				}

				if (action === 'savechanges') {
					if (responseText === $("#Faction" + id).val()) {
						$("#faction" + id).html(responseText);
						$("#factionSaved" + id).html("Faction data has been saved");
						$("#factionSaved" + id).css("display","block");
						$("#factionSaved" + id).fadeOut(2000);
					} else {
						$("#UploadError").html("<span class=\"redtext\">The following errors occurred:</span><span class=\"yellowtext\"><br />" + responseText);
						$("#UploadError").css("padding","5px");

					}

					loadedScript();
				}

			}
		});
	}
}