<%#
 Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
 Licensed to the public under the Apache License 2.0.
-%>

<%+header%>

<script type="text/javascript">//<![CDATA[
	var stxhr = new XHR();
	function run_command(node, action_command)
	{
        if(confirm('It commands the specified node in the network.\nAre you sure you want to continue?\n'))
		{
			var node_id = node.value;
			var action = action_command;
			var legend = document.getElementById('diag-rc-legend');
			var output = document.getElementById('diag-rc-output');
			if (legend && output)
			{
			    output.innerHTML =
				    '<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" /> ' +
				    '<%:Waiting for command to complete...%>'
			    ;

			    legend.parentNode.style.display = 'block';
			    legend.style.display = 'inline';

			    stxhr.post('<%=url('admin/services/can')%>/command_node_' + action + '/' + node_id, { token: '<%=token%>' },
				    function(x)
				    {
					    if (x.responseText)
					    {
						    legend.style.display = 'none';
						    output.innerHTML = String.format('<pre>%h</pre>', x.responseText);
					    }
					    else
					    {
						    legend.style.display = 'none';
						    output.innerHTML = '<span class="error"><%:Invalid node specified!%></span>';
					    }
				    }
			    );
		    }
        }
	}
	function update_status(field)
	{
		var com = field.value;

		var legend = document.getElementById('diag-rc-legend');
		var output = document.getElementById('diag-rc-output');

		if (legend && output)
		{
			output.innerHTML =
				'<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" /> ' +
				'<%:Waiting for command to complete...%>'
			;

			legend.parentNode.style.display = 'block';
			legend.style.display = 'inline';

			stxhr.post('<%=url('admin/services/can')%>/can_command/' + com, { token: '<%=token%>' },
				function(x)
				{
					if (x.responseText)
					{
						legend.style.display = 'none';
						output.innerHTML = String.format('<pre>%h</pre>', x.responseText);
					}
					else
					{
						legend.style.display = 'none';
						output.innerHTML = '<span class="error"><%:Invalid command specified!%></span>';
					}
				}
			);
		}
	}
//]]></script>

<form method="post" action="<%=url('admin/services/can/canopencom')%>">
	<div class="cbi-map">
		<h2 name="content"><%:Command Section%></h2>
		<div class="cbi-section" style="display:none">
                <strong id="diag-rc-legend"></strong>
                <span id="diag-rc-output"></span>
        </div>
		<div class="cbi-section">
			<legend><%:Upload/Download SDO (The syntax is specified in CiA 309-3: [node] &lt command &gt &lt parameter &gt)%></legend>

			<div class="table">
				<div class="tr">
					<div class="td left">
						<input style="margin: 5px 0" type="text" value="" name="sdo" /><br />
						<input type="button" value="<%:Execute%>" class="cbi-button cbi-button-apply" onclick="update_status(this.form.sdo)" />
					</div>
				</div>
			</div>
		</div>
		<div class="cbi-section">
            <legend><%:Command specified node in the Gateway %></legend>
            <div class="table">
               	<div class="tr">
                    <table class='table-responsive'>
                    <tr>
	                    <td>
		                    <span><%:Node ID  %></span>
                            <input style="margin: 5px 0" type="text" value="" name="nodeid" /><br />
	                    </td>
	                    <td>
		                    <input type="button" value="<%:Start%>" class="cbi-button cbi-button-apply" onclick="run_command(this.form.nodeid, 'start')" />
	                    </td>
	                    <td>
		                    <input type="button" value="<%:Stop%>" class="cbi-button cbi-button-apply" onclick="run_command(this.form.nodeid, 'stop')" />
	                    </td>
	                    <td>
		                    <input type="button" value="<%:Reset node%>" class="cbi-button cbi-button-apply" onclick="run_command(this.form.nodeid, 'reset')" />
	                    </td>
	                    <td>
		                    <input type="button" value="<%:Pre-OP%>" class="cbi-button cbi-button-apply" onclick="run_command(this.form.nodeid, 'preop')" />
	                    </td>
	                    <td>
		                    <input type="button" value="<%:Reset Communication%>" class="cbi-button cbi-button-apply" onclick="run_command(this.form.nodeid, 'reset_comm')" />
	                    </td>
                    </tr>	
                    </table>
                </div>
            </div>
        </div>
	</div>
</form>

<%+footer%>
