<!--
// This is javascript file CHIM2WAY.JS by Eric Martz
// Thanks to Tim Maffett of MDLI for showing me how to do this!
// For a complete set of files to use as an example, see (and download)
// Creating Chime Web Pages at http://www.umass.edu/microbio/chime
//
// Include this file in your chime page with a line in the <head> such as:
// <script src="chim2way.js"></script>

//
// In the body of your document, call chime_2way() like this:
// <script language="javascript">chime_2way()</script>

// This inserts a 2-way command-line interface into the document.
//
// The present code is designed for the case in which the plug-in and
// the command-line interface are in the same[frame in the same] document.
//
// The <embed ..>  which invokes Chime must include:
// MessageCallback="ChimeMsg"
//
// A WORD TO THE WISE: WHEN MODIFYING THESE FILES TO BUILD YOUR OWN CHIME
// WEB PAGE, CHANGE AS LITTLE AS POSSIBLE AND MAKE ONLY ONE CHANGE AT 
// TIME. IF YOU MAKE LOTS OF CHANGES AND RENAME LOTS OF THINGS, DEBUGGING
// JAVASCRIPT CAN BE VERY FRUSTRATING AND SLOW.  I KNOW!     -->


var lastcmd = "";		// save last command for Again button



// The following function puts Chime's messages in the message textarea.

function ChimeMsg(buttonname, message )
{
	document.c2w_form.chimemessages.value = message + "\r\n" +
	document.c2w_form.chimemessages.value;
	document.c2w_form.chimemessages.value = del_after_line(50,
	document.c2w_form.chimemessages.value);
}



// The following function writes the 2-way html form into the document.

function Chime_2way(target_chime)
{	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(\"" + target_chime + "\"); return false'>");

	// The following codes for the Command-line input slot.

	  writeln("<input type='text' name='rasmolscript'");
	  writeln("size='50' maxlength='100'><font size=+1> Enter </font>");

	// We'll use Enter to execute the command line, so we don't need
	// an Execute button.  But here's how you could do it if you
	// wanted to.

		// Command-line execution button
		// writeln("<input type='button' name='sendbut' value='Do It'");
		// this.form.rasmolscript.value needs the 'form' here
		// writeln("onClick='ScriptToChime(\"" + target_chime + "\", this.form.rasmolscript.value)'>");
		// writeln("<br>");
		// 'Again' button to repeat last command if desired (perhaps edited).

 	  writeln("<input type='button' name='sendbut' value='Again'");
	  writeln("onClick='again_button()'>");
				
	  writeln("<input type='button' value='Clear'");		// Button to clear the message box

	  writeln("onClick='this.form.chimemessages.value=\"\"'>");

	  writeln("<br>");						// Box to display Chime's messages

	  writeln("<textarea name='chimemessages' rows=8 cols=45></textarea>");
	  writeln("<br>Messages from Chime appear <i>most recent at the top</i> in the box above.");
	  writeln("(Only the last 50 lines are kept.)");

	// end of this form

	  writeln("</form>");

	  close();							// close() forces the display of the page.
  }
}

// The following function sends the script to the Chime plug-in.
// The mechanism is to create a new document in the tiny 'dummy' frame.
// This new document contains a hidden button to execute the script.
// The button executes immediately (does not need to be clicked).

function ScriptToChime(target_chime, rasmol_script)
{
	with (top.emptyframe.document)
  {

	  open();
	  writeln("<html><head></head><body bgcolor=white>");
	  writeln("<embed type=\"application/x-spt\" hidden=true");
	  writeln(" width=10 height=10 button=push target=\"" + target_chime + "\"");
	  writeln(" script=\"" + rasmol_script + "\" immediate=1>");
	  writeln("</body></html>");
	  close();

  }

}


// The following function sends the command line to Chime and then cleans up.

function exec_script(target_chime)
{
	ScriptToChime(target_chime, document.c2w_form.rasmolscript.value);

	lastcmd = document.c2w_form.rasmolscript.value;		// 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
}


// END OF CHIM2WAY.JS