function McxElementListView(parentElement)
{
	this.ParentElement = parentElement;
	this.CaptionElement = this.ParentElement.children[0];
	this.HeaderElement = this.ParentElement.children[1];
	this.HeaderRowElement = this.HeaderElement.children[0];

	this.HeaderAddRowButton = this.HeaderRowElement.children[0].children[0];
	this.HeaderAddRowButton.NativeObject = this;
	this.HeaderAddRowButton.addEventListener("click", function(e)
	{
		var lv = this.NativeObject;
		lv.addRowBefore(0);

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

	this.BodyElement = this.ParentElement.children[2];

	this.buildRow = function()
	{
		var lvInstId = this.ParentElement.getAttribute("data-ecid");
		
		var tr = document.createElement("tr");
		tr.appendChild(this.createAddRemoveColumn());

		var xhr = new XMLHttpRequest();
		var url = System.ExpandRelativePath("~/" + System.TenantName + "/flowController.htmld");
		xhr.open("POST", url);

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

		for (var i = 1; i < this.HeaderRowElement.children.length; i++)
		{
			var colInstId = this.HeaderRowElement.children[i].getAttribute("data-ecid");
			var fqecid = lvInstId + ":*:" + colInstId;
			
			var td = document.createElement("td");
			var input = document.createElement("input");
			input.id = "ec_" + fqecid;
			input.name = "ec_" + fqecid;
			input.setAttribute("data-ecid", "ec_" + fqecid);
			input.type = "text";
			td.appendChild(input);

			tr.appendChild(td);
		}
		return tr;
	};
	this.addRowBefore = function(beforeIndex)
	{
		var tr = this.buildRow();
		this.BodyElement.insertBefore(tr, this.BodyElement.children[beforeIndex]);
	};
	this.addRowAfter = function(afterElement)
	{
		var tr = this.buildRow();
		this.BodyElement.insertAfter(tr, afterElement);
	};
	this.removeRow = function(row)
	{
		row.remove();
	};
	this.removeRowAt = function(index)
	{
		this.BodyElement.children[index].remove();
	};

	this.createAddRemoveColumn = function()
	{
		var td = document.createElement("td");
		td.className = "uwt-listview-row-buttons";
		
		var aAdd = document.createElement("a");
		aAdd.className = "uwt-listview-row-add";
		aAdd.href = "#";
		aAdd.NativeObject = this;
		aAdd.addEventListener("click", function()
		{
			this.NativeObject.addRowAfter(this.parentElement);
		});
		td.appendChild(aAdd);

		var aRemove = document.createElement("a");
		aRemove.className = "uwt-listview-row-remove";
		aRemove.href = "#";
		aRemove.NativeObject = this;
		aRemove.addEventListener("click", function()
		{
			this.NativeObject.removeRow(this.parentElement.parentElement);
		});
		td.appendChild(aRemove);

		return td;
	};
}

window.addEventListener("load", function()
{
	var items = document.getElementsByClassName("mcx-element");
	for (var i = 0; i < items.length; i++)
	{
		if (items[i].tagName === "TABLE" && System.ClassList.Contains(items[i], "uwt-listview"))
		{
			items[i].McxElementListView = new McxElementListView(items[i]);
		}
	}
});