<% local form, viewlibrary, page_info, session = ... 
htmlviewfunctions = require("htmlviewfunctions")
html = require("acf.html")
%>

<script type="text/javascript">
	if (typeof jQuery == 'undefined') {
		document.write('<script type="text/javascript" src="<%= html.html_escape(page_info.wwwprefix) %>/js/jquery-latest.js"><\/script>');
	}
</script>

<script type="text/javascript">
	$(function(){
		$('input, select').first().focus();
		$('input, select').keydown(function(e){
			if (e.keyCode == 13) {
				$(this).parents('form').find('input.submit').click();
				return false;
			}
		});
	});
</script>

<% htmlviewfunctions.displaycommandresults({"deletedevice", "editdevice", "editdeviceparams", "overridedeviceparams"}, session) %>

<%
if form.value.values then
	local header_level = htmlviewfunctions.displaysectionstart(cfe({label="Device"}), page_info)
	htmlviewfunctions.displayinfo(form)
	htmlviewfunctions.displayitem(cfe({label="Device ID", readonly=true, value=form.value.result.value[1].device_id}), page_info)
	local header_level2 = htmlviewfunctions.displaysectionstart(cfe({label="Parameter Values"}), page_info, htmlviewfunctions.incrementheader(header_level))
	val = require("session").serialize("values", form.value.values.value)
	val = string.gsub(val, "[^\n]*%{%}\n", "")
	print("<pre>"..val.."</pre>")
	htmlviewfunctions.displaysectionend(header_level2)
	htmlviewfunctions.displaysectionend(header_level)
	form.value.values = nil
elseif form.value.result then
	local func = haserl.loadfile(page_info.viewfile:gsub("searchdevices", "listdevices"))
	func(form.value.result, viewlibrary, page_info, session)
end
%>

<%
	form.value.result = nil
	htmlviewfunctions.displayitem(form, page_info)
%>
