function McxElementContent(parentElement)
{
	this.ParentElement = parentElement;
	this.LabelTd = this.ParentElement.children[0];
	if (this.LabelTd)
	{
		if (this.LabelTd.tagName === "td")
		{
			this.LabelElement = this.LabelTd.children[0];
		}
		else
		{
			this.LabelElement = this.LabelTd;
		}
	}

	this.updateElementContentsWithFlowController = function(value, changes)
	{
		alert("update elements");
		for (var i = 0; i < changes.length; i++)
		{
			var ecid = changes[i].ecid;
			var ec = document.getElementById("ec_" + ecid);
			if (ec)
			{
				if (changes[i].addsDisplayOptions)
				{
					for (var j = 0; j < changes[i].addsDisplayOptions.length; j++)
					{
						if (changes[i].addsDisplayOptions[j].iid == "924$2")
						{
							if (value)
							{
								ec.setAttribute("disabled", "disabled");
							}
							else
							{
								ec.removeAttribute("disabled");
							}
						}
					}
				}
				if (changes[i].removesDisplayOptions)
				{
					for (var j = 0; j < changes[i].removesDisplayOptions.length; j++)
					{
						if (changes[i].removesDisplayOptions[j].iid == KnownInstanceKeys.DisplayOption__NotEnterable)
						{
							if (value)
							{
								ec.removeAttribute("disabled");
							}
							else
							{
								ec.setAttribute("disabled", "disabled");
							}
						}
					}
				}
			}
		}
	};

	this.ValueContainer = this.ParentElement.children[1];
	if (this.ValueContainer.children.length > 0)
	{
		if (this.ValueContainer.children[0].tagName === "INPUT")
		{
			this.ValueContainer.children[0].addEventListener("blur", function()
			{
				console.log(" DO validation Here!");
			});
		}
		else if (this.ValueContainer.children[0].tagName === "DIV" && System.ClassList.Contains(this.ValueContainer.children[0], "mcx-instancebrowser"))
		{
			this.ValueContainer.children[0].children[2].addEventListener("blur", function(e)
			{
				var ib = this.parentElement.NativeObject;
				console.log("IB selected items: " + ib.getSelectedItems().length);
			});
		}
		else if (this.ValueContainer.children[0].tagName === "DIV" && System.ClassList.Contains(this.ValueContainer.children[0], "uwt-checkbox"))
		{
			var inputElement = this.ValueContainer.children[1];
			inputElement.McxNativeObject = this;
			inputElement.addEventListener("change", function()
			{
				var value = this.NativeObject.GetChecked();
				var ecid = this.McxNativeObject.ParentElement.getAttribute("data-fqecid");

				var xhr = new XMLHttpRequest();
				var url = System.ExpandRelativePath("~/" + System.TenantName + "/flowController.htmld");
				xhr.NativeObject = this.McxNativeObject;
				xhr.NativeObject.ValueElement = this.NativeObject.ParentElement;
				xhr.inputElement = this;
				xhr.onreadystatechange = function()
				{
					if (this.readyState === 4)
					{
						console.log(xhr);
						if (this.status === 200)
						{
							json = JSON.parse(xhr.responseText);
							this.NativeObject.updateElementContentsWithFlowController(json.value, json.changes);
						}
						else
						{
							this.NativeObject.ValueElement.NativeObject.__inhibit_update = true;
							this.NativeObject.ValueElement.NativeObject.ToggleChecked();
							this.NativeObject.ValueElement.NativeObject.__inhibit_update = false;
							if (this.status === 403 || this.status === 401)
							{
								Alert.show("Please log in to continue", "Not Logged In", "uwt-color-danger", 5000, "NotLoggedIn");
							}
						}
					}
				};
				xhr.open("POST", url);

				var h = {
					"_flowExecutionKey": "e0s1",
					"ecid": ecid,
					"sessionSecureToken": "7733ad40-edf6-4dc9-aa90-85bcf93cc8f0",
					"clientRequestID" : "9af7ce5bc51348a6981539a98b267072",
					"value": value
				};
				xhr.send(JSON.stringify(h));

				return true;
			});
		}
	}
	
	this.InstanceID = this.ParentElement.getAttribute("data-instance-id");
	this.ECID = this.ParentElement.getAttribute("data-ecid");

	if (this.LabelElement)
	{
		this.LabelElement.NativeObject = this;
		this.LabelElement.addEventListener("contextmenu", function(e)
		{
			var cm = new ContextMenu();
			var ecid = this.NativeObject.ECID;
			var iid = this.NativeObject.InstanceID;
			cm.Items = [

				{
					"JJJJ": this.NativeObject,
					"ClassName": "MenuItemCommand",
					"Title": "Show Field Properties",
					"Visible": true,
					"Execute": function()
					{
						var popup = Popup.create(this.JJJJ.LabelTd);

						var listview = ListView.create(popup.ParentElement, {
							"columns": [
								{
									"id": "ecid",
									"title": "ECID"
								},
								{
									"id": "iid",
									"title": "Instance ID"
								}
							],
							"items": [
								{
									"columns": [
										{
											"id": "ecid",
											"value": ecid
										},
										{
											"id": "iid",
											"value": iid
										}
									]
								}
							]
						});
						popup.ParentElement.appendChild(listview.ParentElement);
						
						popup.Show();
					}
				},
				{
					"ClassName": "MenuItemCommand",
					"Title": "Show Field EC",
					"Visible": true,
					"Execute": function()
					{
						window.open(System.ExpandRelativePath("~/" + System.TenantName + "/d/inst/1$56/" + ecid + ".htmld"));
					}
				}

			];
			cm.Show(e.clientX, e.clientY, this);
			

			e.preventDefault();
			e.stopPropagation();
			return false;
		});
	}
}

window.addEventListener("load", function()
{
	var items = document.getElementsByClassName("mcx-elementcontent");
	for (var i = 0; i < items.length; i++)
	{
		// console.log("found mcx-elementcontent");
		// console.log(items[i]);
		items[i].McxNativeObject = new McxElementContent(items[i]);
	}
	/*
	var items = document.getElementsByClassName("mcx-element");
	for (var i = 0; i < items.length; i++)
	{
		items[i].McxNativeObject = new McxElementContent(items[i]);
	}
	*/
});