--- loncom/homework/chemresponse.pm	2006/07/03 01:16:04	1.68
+++ loncom/homework/chemresponse.pm	2007/04/18 00:48:06	1.73
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # chemical equation style response
 #
-# $Id: chemresponse.pm,v 1.68 2006/07/03 01:16:04 albertel Exp $
+# $Id: chemresponse.pm,v 1.73 2007/04/18 00:48:06 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -110,14 +110,15 @@ function substituent(r) {document.applet
 CHEMJS
 
     my $start_page = 
-        &Apache::loncommon::start_page('Molecule Editor',$js,
+        &Apache::loncommon::start_page('Molecule Editor',undef,
 				       {'only_body' => 1,
+					'js_ready'  => 1,
 					'bgcolor'   => '#FFFFFF',});
-    my $end_page = 
-	&Apache::loncommon::end_page();
-
+    my $end_page =
+ 	&Apache::loncommon::end_page({'js_ready' => 1,});
+	
     my $body=<<CHEMPAGE;
-$start_page
+$js
 <center>
 <form>
   <table width="440"><tr>
@@ -155,17 +156,26 @@ $insert_answer
 <input type="button" value="  Help  " onclick = "javascript:openHelpWindow()" />
 </form>
 </center>
-$end_page
 CHEMPAGE
 
-    $body=&HTML::Entities::encode($body,'<>&"');
-    $body=~s/\n/ /g;
+    $body=&Apache::loncommon::js_ready($body);
     my $nothing=&Apache::lonhtmlcommon::javascript_nothing();
     my $docopen=&Apache::lonhtmlcommon::javascript_docopen();
     my $display=&mt('Draw Molecule');
     if (defined($shown_text)) { $display=&mt($shown_text); }
+    my $function = 
+	'LONCAPA_draw_molecule_'.&Apache::lonhtmlcommon::get_uniq_name();
     my $result=<<CHEMINPUT;
-<input type="button" value="$display" onclick="javascript:editor=window.open($nothing,'jmeedit','width=500,height=500,menubar=no,scrollbars=no,resizable=yes');editor.$docopen;editor.document.write('$body');editor.document.close();editor.focus()" />
+<script type="text/javascript">
+    function $function() {
+	editor=window.open($nothing,'jmeedit','width=500,height=500,menubar=no,scrollbars=no,resizable=yes');
+	editor.$docopen;
+	editor.document.write('$start_page $body $end_page');
+	editor.document.close();
+	editor.focus();
+    }
+</script>
+<input type="button" value="$display" onclick="javascript:$function();void(0);" />
 CHEMINPUT
     return $result;
 }
@@ -235,6 +245,7 @@ sub start_organicresponse {
 	$result .='</nobr><br /><nobr>';
 	$result .=&Apache::edit::text_arg('Correct Answer:','answer',
 					  $token,40);
+	$result .='</nobr><br /><nobr>';
 	$result .=&Apache::edit::text_arg('JME string of the answer (automatically updated when using the Draw Molecule button):',
 					  'jmeanswer',$token);
 	my $jmeanswer=&Apache::lonxml::get_param('jmeanswer',$parstack,
@@ -316,6 +327,10 @@ sub end_organicresponse {
 	}
 	$result.=&Apache::response::answer_footer('organicresponse');
     }
+    if ($target eq 'web') {
+	&Apache::response::setup_prior_tries_hash(\&format_prior_answer_organic,
+						  ['molecule'])
+    }
 
     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
 	$target eq 'tex' || $target eq 'analyze') {
@@ -325,6 +340,14 @@ sub end_organicresponse {
     return $result;
 }
 
+sub format_prior_answer_organic {
+    my ($mode,$answer,$other_data) = @_;
+    my $result=&mt('Smile representation: "[_1]"','<tt>'.$answer.'</tt>');
+    my $jme=$other_data->[0];
+    $result.=&jme_img($jme,$answer,400);
+    return $result;
+}
+
 sub start_organicstructure {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
     my $result;
@@ -456,7 +479,7 @@ sub start_reactionresponse {
 		&Apache::lonxml::default_homework_load($safeeval);
 	    }
 	    @Apache::scripttag::parser_env = @_;
-	    $Apache::inputtags::answertxt{$id}=&Apache::run::run("return &chemparse(q\0$ans\0);",$safeeval);
+	    $Apache::inputtags::answertxt{$id}=[&Apache::run::run("return &chemparse(q\0$ans\0);",$safeeval)];
 	}
     } elsif ($target eq "edit") {
 	$result .=&Apache::edit::tag_start($target,$token);
@@ -531,6 +554,9 @@ sub end_reactionresponse {
 	}
 	$result.=&Apache::response::answer_footer('reactionresponse');
     }
+    if ($target eq 'web') {
+	&Apache::response::setup_prior_tries_hash(\&format_prior_response_reaction);
+    }
 
     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
 	$target eq 'tex' || $target eq 'analyze') {
@@ -540,6 +566,12 @@ sub end_reactionresponse {
     return $result;
 }
 
+sub format_prior_response_reaction {
+    my ($mode,$answer) =@_;
+    return '<span class="LC_prior_reaction">'.
+	    &HTML::Entities::encode($answer,'"<>&').'</span>';
+}
+
 sub start_chem {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
     my $result = '';