var lastcmd = ""; // save last command for Again button
var trace_single = "OFF";

// This function writes the 2-way html form into the document.
function trace2way()
{
	with (document)
	{
		open();

		// Set up command-line form.
		// We don't need to submit the form to the SERVER.  'return false'
		// prevents this from happening.  We just want to use the form
		// values internally in the CLIENT-side javascript.
		
		writeln("<form name='c2w_form'");
		writeln("onSubmit='exec_script(); return false'>");

		// Box to display Chime's messages orig 11/35
		if (win95())
			writeln("<textarea name='chimemessages' rows=9 cols=33></textarea>");
		else
			writeln("<textarea name='chimemessages' rows=9 cols=27></textarea>");

		// Command-line input slot.
		writeln("<br clear=right>");
		writeln("<input type='text' name='rasmolscript'");
		writeln("size='50' maxlength='100'><font size=+1> Enter </font>");

		// 'Again' button to repeat last command if desired (perhaps edited).
		writeln("<input type='button' name='sendbut' value='Again'");
		writeln("onClick='again_button()'>");

		// Button to clear the message box
		writeln("<input type='button' value='Clear'");
		writeln("onClick='this.form.chimemessages.value=\"\"'>");

		writeln("</form>");// must have only one text/form to submit w/ Enter!
	}
}

function trace_control()
{
	with (document)
	{
		open();

		writeln("<form name='form_trace' onSubmit='return false'>");

		// TRACE OPTIONS
		writeln("<p>Script tracing options:<br>");

		// SINGLE STEP
		writeln("<input type='button' value='Single step tracing'");
		writeln("onClick='toggle_single_step()'> is");
		writeln("<input type='text' name='singstep'");
		writeln("size='4' value='" + trace_single + "'>* <small>This can");
		writeln("be turned on by clicking the button or with the command 'javascript toggle_single_step()'");
		writeln("in a script file.</small><br>");

		writeln("* Do not type directly into the slot above marked *.");
		writeln("Use button to change modes.");

		// TRAP COMMAND
		writeln("<br>Trap command <input type=text name='trap_command'>");
		writeln("<small>When any word or phrase you type here is executed");
		writeln("by Chime, you will be alerted. You do not need to press Enter.</small>");

		// end of this form
		writeln("</form>");
		// close() forces the display of the page.
		close();
	}
}

// This function sends the command line to Chime and then cleans up.
function exec_script()
{
	var cmd = document.c2w_form.rasmolscript.value;

	// expand 's ' to 'script '
	if (left_equals("s ", cmd))
		cmd = "script" + cmd.substring(1);
	if (left_equals("script ", cmd) && cmd.indexOf(".spt") == -1)
		cmd = trim_right(cmd) + ".spt";
	if (cmd == "vsu")
		cmd = "view show user";

	// spt2Chime() must quote the string is passes to Chime.
	// Therefore there may not be any double quotes in the string itself.
	// If there are any prohibited double quotes in cmd ...
	if (count_ss('"', cmd))
	{
		if (count_ss("'", cmd)) // and there are also single quotes
		{
			// we can't fix it
			alert("Illegal command syntax:\n" +
				"Sorry, only one level of quotes (single quotes)\n" +
				"are permitted in immediate-mode commands to Chime.")
			return;
		}
		// fix it: substitute ' for "
		cmd = substitute("'", '"', cmd);
		alert("Please use only single quotes.\n" +
			"Command has been changed to:\n" +
			cmd);
	}

	document.c2w_form.chimemessages.value = del_after_line(20,
		document.c2w_form.chimemessages.value);
	scriptToChime(cmd);
	lastcmd = cmd; // save this command for Again
	document.c2w_form.rasmolscript.value = ""; // clear the command slot
}

function again_button()
{
	document.c2w_form.rasmolscript.value = lastcmd; // put last command in slot
	document.c2w_form.rasmolscript.focus(); // put cursor in slot
}

function toggle_single_step()
{
	if (trace_single == "OFF")
		trace_single = "ON";
	else
		trace_single = "OFF";
// This function may be called from the Chime-script
// when running in non-debug mode.
	if (getCookie("debugc") == "ON")
		document.form_trace.singstep.value = trace_single;
}

function say_scroll()
{
	if (getCookie("scroll_said") != "yes")
	{
		alert('Scroll down for more debugging controls.');
		putCookie("scroll_said", "yes");
	}
}
