function post(url, payload, callback)
{
	var xhr = new XMLHttpRequest();
	xhr.callback = callback;
	xhr.onreadystatechange = function()
	{
		if (this.readyState == 4)
		{
			if (typeof(this.callback) == 'function')
			{
				this.callback();
			}
		}
	};

	var payloadText = null;
	if (typeof(payload) === 'string' || typeof(payload) == 'boolean' || typeof(payload) == 'number')
	{
		payloadText = payload.toString();
	}
	else if (typeof(payload) === 'object')
	{
		payloadText = JSON.stringify(payload);
	}
	
	xhr.open("POST", url);
	xhr.send(JSON.stringify(payload));
}

function alert(message)
{
	if (typeof(message) == 'string')
	{
		Dialog.ShowDialog("", message, [ { "dialogResult": DialogResult.OK, "title": "OK", "className": "uwt-color-primary" }]);
	}
	else if (typeof(message) == 'object')
	{
		Dialog.ShowDialog("", message.content, [ { "dialogResult": DialogResult.OK, "title": "OK", "className": "uwt-color-primary", "callback": function(dlg, btn)
			{
				message.callback();
			}
		 }]);
	}
}

window.addEventListener("load", function()
{
	var tsbProvision = document.getElementById("tsbProvision");
	tsbProvision.addEventListener("click", function()
	{
		// usage: Dialog.ShowDialog(title, prompt, buttons, fields, callback)
		Dialog.ShowDialog
		(
			"Provision New Tenant(s)",
			"",
			[
				{
					"dialogResult": DialogResult.OK,
					"title": "OK",
					"className": "uwt-color-primary",
					"callback": function(dlg, btn)
					{
						post("/api.php", {
							"action": "createTenant",
							"tenantName": dlg.getField("tenant_name").getValue(),
							"tenantType": dlg.getField("tenant_type").getValue(),
							"tenantCount": dlg.getField("tenant_count").getValue()
						}, function()
						{
							window.location.reload();
						});
						return true;
					}
				},
				{
					"dialogResult": DialogResult.CANCEL,
					"title": "Cancel"
				}
			],
			[
				{
					"type": "literal",
					"value": "<p>Please specify the name of your new tenant and select a tenant type.  For more information about the various tenant types please consult the <a href=\"#\">Mocha documentation</a>.</p>"
				},
				{
					"id": "tenant_name",
					"type": "text",
					"title": "Tenant name"
				},
				{
					"id": "tenant_type",
					"type": "choice",
					"title": "Tenant type",
					"choices":
					[
						{
							"title": "Production",
							"value": "{5535F488-6C8E-42BC-86A3-664D70411A2A}",
							"color": "#85C744"
						},
						{
							"title": "Sandbox",
							"value": "{6A3767FD-6606-426F-B8D9-507B9FFE595C}",
							"color": "#F1C40F"
						},
						{
							"title": "Implementation",
							"value": "{E860A64C-C156-49E7-80E5-E3BA3CE4D15D}",
							"color": "#2BBCE0"
						},
						{
							"title": "Development",
							"value": "{B4F900F1-2C22-41FD-AAB9-7C956663256F}",
							"color": "#E73C3C"
						},
						{
							"title": "Internal Development",
							"value": "{716D4C16-040F-4EBB-A920-DDF88765A017}",
							"color": "#E73C3C"
						},
						{
							"title": "Sandbox Preview Preview",
							"value": "{b225d85b-d1a2-4ce7-90bc-410f2d5c0a53}",
							"color": "#F1C40F"
						}
					]
				},
				{
					"type": "literal",
					"value": "<p>You may specify the number of tenants to create.  If this is more than 1, tenants are named 'tenant<i>N</i>', where 'tenant' is the specified tenant name and 'N' is a value from 1 to the number of tenants requested.</p>"
				},
				{
					"id": "tenant_count",
					"type": "numeric",
					"title": "Number of tenants",
					"value": 1,
					"minimumValue": 1,
					"maximumValue": 9
				}
			]
		);
	});

});