function DialogResult(value)
{
	this.value = value;
}
DialogResult.OK = new DialogResult(1);
DialogResult.CANCEL = new DialogResult(2);

// ewwww
// Only implement if no native implementation is available
if (typeof(Array.isArray) === 'undefined')
{
	Array.isArray = function(obj)
	{
		return Object.prototype.toString.call(obj) === '[object Array]';
	};
}

function Dialog()
{

}
Dialog.__handleButton = function(dialog, button, callback)
{
	if (typeof(button.btn.callback) == 'function')
	{
		var ret = button.btn.callback(dialog, button);
		if (typeof(ret) == 'boolean')
		{
			if (!ret)
			{
				// callback returned false; do not continue processing
				return;
			}
		}
	}

	if (typeof(callback) == 'function')
	{
		var ret = callback(dialog, button);
		if (typeof(ret) == 'boolean')
		{
			if (!ret)
			{
				// callback returned false; do not continue processing
				return;
			}
		}
	}

	if (typeof(button.dialogResult) !== 'undefined')
	{
		dialog.window.Hide();
	}
};



Dialog.ShowDialog = function(title, prompt, buttons, fields, callback)
{
	var dlg = new Dialog();
	dlg.getField = function(id)
	{
		for (var i = 0; i < this.fields.length; i++)
		{
			if (this.fields[i].id == id) return this.fields[i];
		}
		return null;
	};
	dlg.fields = fields;

	var wnd = new Window();
	wnd.dlg = dlg;
	dlg.window = wnd;

	wnd.SetTitle(title);
	wnd.ParentElement.addEventListener("keydown", function(e)
	{
		if (e.keyCode == 13)
		{
			Dialog.__handleButton(this.NativeObject.dlg, this.NativeObject.dlg.__okButton, null);
		}
	});

	var cel = wnd.GetContentElement();
	var p = document.createElement("p");
	p.innerHTML = prompt;
	cel.appendChild(p);

	if (Array.isArray(fields))
	{
		var formview = document.createElement("table");
		formview.className = "uwt-formview";

		for (var i = 0; i < fields.length; i++)
		{
			fields[i].element = null;
			fields[i].getValue = function()
			{
				return null;
			};

			var tr = document.createElement("tr");
			tr.className = "uwt-formview-item";

			var td = document.createElement("td");
			td.className = "uwt-formview-item-title";
			
			if (fields[i].type != "literal")
			{
				var label = document.createElement("label");
				label.innerHTML = fields[i].title;
				td.appendChild(label);

				tr.appendChild(td);
			}

			td = document.createElement("td");
			td.className = "uwt-formview-item-content";

			if (fields[i].type == "literal")
			{
				td.innerHTML = fields[i].value;
				td.colSpan = 2;
			}
			else if (fields[i].type == "text" || fields[i].type == "numeric")
			{
				var input = document.createElement("input");
				if (fields[i].type == "numeric")
				{
					input.type = "number";
				}
				else
				{
					input.type = "text";
				}
				td.appendChild(input);

				if (typeof(fields[i].value) == 'number')
				{
					input.value = fields[i].value;
				}

				fields[i].element = input;
				fields[i].getValue = function()
				{
					return this.element.value;
				};
			}
			else if (fields[i].type == "choice")
			{
				var select = document.createElement("select");
				for (var j = 0; j < fields[i].choices.length; j++)
				{
					var option = document.createElement("option");
					option.innerHTML = fields[i].choices[j].title;
					if (typeof(fields[i].choices[j].value) !== 'undefined')
					{
						option.value = fields[i].choices[j].value;
					}
					if (typeof(fields[i].choices[j].color) !== 'undefined')
					{
						option.setAttribute("data-color", fields[i].choices[j].color);
					}
					select.appendChild(option);
				}
				td.appendChild(select);

				select.NativeObject = new DropDownWrapper(select);
				fields[i].element = select;
				fields[i].getValue = function()
				{
					return this.element.value;
				};
			}
			tr.appendChild(td);

			formview.appendChild(tr);
		}

		cel.appendChild(formview);
	}

	var ftt = wnd.GetFooterElement();
	ftt.style.display = "";
	if (Array.isArray(buttons))
	{
		for (var i = 0; i < buttons.length; i++)
		{
			var btn = document.createElement("a");
			btn.className = "uwt-button";
			if (buttons[i].className)
			{
				System.ClassList.Add(btn, buttons[i].className);
			}
			if (buttons[i].title)
			{
				btn.innerHTML = buttons[i].title;
			}

			if (typeof(buttons[i].dialogResult) !== 'undefined')
			{
				btn.dialogResult = buttons[i].dialogResult;
				if (btn.dialogResult == DialogResult.OK)
				{
					dlg.__okButton = btn;
				}
			}
			btn.btn = buttons[i];
			btn.dlg = dlg;

			btn.addEventListener("click", function()
			{
				Dialog.__handleButton(this.dlg, this, callback);
			});
			ftt.appendChild(btn);
		}
	}

	wnd.ShowDialog();
};