--- loncom/homework/edit.pm	2005/06/07 01:33:19	1.93
+++ loncom/homework/edit.pm	2006/07/27 20:47:02	1.103
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA 
 # edit mode helpers
 #
-# $Id: edit.pm,v 1.93 2005/06/07 01:33:19 albertel Exp $
+# $Id: edit.pm,v 1.103 2006/07/27 20:47:02 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -32,6 +32,9 @@ use strict;
 use Apache::lonnet;
 use HTML::Entities();
 use Apache::lonlocal;
+use lib '/home/httpd/lib/perl/';
+use LONCAPA;
+ 
 
 # Global Vars
 # default list of colors to use in editing
@@ -92,10 +95,12 @@ sub start_table {
     foreach my $namespace (reverse @Apache::lonxml::namespace) {
 	my $testtag=$namespace.'::'.$tag;
 	$tagnum=$Apache::lonxml::insertlist{"$testtag.num"};
+	&Apache::lonxml::debug(" $testtag ");
 	if (defined($tagnum)) { last; }
     }
     if (!defined ($tagnum)) {$tagnum=$Apache::lonxml::insertlist{"$tag.num"};}
     my $color = $Apache::lonxml::insertlist{"$tagnum.color"};
+    &Apache::lonxml::debug(" $tagnum -- $color");
     if (!defined($color)) {
 	$color = $Apache::edit::colorlist[$Apache::edit::colordepth];
     }
@@ -109,7 +114,7 @@ sub start_table {
 sub end_table {
     $Apache::edit::colordepth--;
     my $result='</table></div>';
-    $result.="<table><tr><td>";
+    $result.='<div align="left"><table><tr><td>';
 
     my ($tagname,$closingtag);
     if (defined($Apache::edit::inserttag[-2])) {
@@ -119,7 +124,7 @@ sub end_table {
 	$closingtag=$Apache::edit::inserttag[-1];
     }
     $result.=&innerinsertlist('edit',$tagname,$closingtag).
-	"</td></tr></table>";
+	"</td></tr></table></div>";
     pop(@Apache::edit::inserttag);
     return $result;
 }
@@ -161,7 +166,7 @@ sub handle_delete {
     }
     if (!$result) {
 	my $endtag='/'.$token->[1];
-	my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser);
+	my $bodytext=&Apache::lonxml::get_all_text($endtag,$parser,$style);
 	$$parser['-1']->get_token();
 	&Apache::lonxml::debug("Deleting :$bodytext: for $token->[1]");
 	&Apache::lonxml::end_tag($tagstack,$parstack,$token);
@@ -309,6 +314,25 @@ sub insert_numericalresponse {
 </numericalresponse>';
 }
 
+sub insert_customresponse {
+    return '
+<customresponse>
+    <answer type="loncapa/perl">
+    </answer>
+    <textline />
+    <hintgroup>
+    <startouttext /><endouttext />
+    </hintgroup>
+</customresponse>';
+}
+
+sub insert_customresponse_answer {
+    return '
+    <answer type="loncapa/perl">
+    </answer>
+';
+}
+
 sub insert_stringresponse {
     return '
 <stringresponse answer="" type="">
@@ -477,6 +501,44 @@ sub insert_script {
     return "\n<script type=\"loncapa/perl\"></script>";
 }
 
+sub js_change_detection {
+    my $unsaved=&mt("There are unsaved changes");
+    return (<<SCRIPT);
+<script type="text/javascript">
+var clean = true;
+var is_submit = false;
+var still_ask = false;
+function compareForm(event_) {
+        if (!event_ && window.event) {
+          event_ = window.event;
+        }
+	if ((!is_submit || (is_submit && still_ask)) && !clean) {
+	    still_ask = false;
+	    is_submit = false;
+            event_.returnValue = "$unsaved";
+            return "$unsaved";
+        }
+}
+function unClean() {
+     clean=false;
+}
+window.onbeforeunload = compareForm;
+</script>
+SCRIPT
+}
+
+sub form_change_detection {
+    return ' onsubmit="is_submit=true;" ';
+}
+
+sub element_change_detection {
+    return ' onchange="unClean();" ';
+}
+
+sub submit_ask_anyway {
+    return ' onclick="still_ask=true;" ';
+}
+
 sub textarea_sizes {
     my ($data)=@_;
     my $count=0;
@@ -495,10 +557,11 @@ sub editline {
     my ($tag,$data,$description,$size)=@_;
     $data=&HTML::Entities::encode($data,'<>&"');
     if ($description) { $description="<br />".$description."<br />"; }
+    my $change_code = &element_change_detection();
     my $result = <<"END";
 $description
 <input type="text" name="homework_edit_$Apache::lonxml::curdepth" 
-       value="$data" size="$size" />
+       value="$data" size="$size" $change_code />
 END
     return $result;
 }
@@ -517,13 +580,13 @@ sub editfield {
     if ($rows < $minheight) { $rows = $minheight; }
     if ($description) { $description="<br />".$description."<br />"; }
     if ($usehtmlarea) {
-	push @Apache::lonxml::htmlareafields,'homework_edit_'.
-	    $Apache::lonxml::curdepth;
+	&Apache::lonhtmlcommon::add_htmlareafields('homework_edit_'.
+						   $Apache::lonxml::curdepth);
     }
-    return $description."\n".'&nbsp;&nbsp;&nbsp;<textarea style="width:100%" rows="'.$rows.
+    return $description."\n".'<textarea style="width:100%" rows="'.$rows.
 	'" cols="'.$cols.'" name="homework_edit_'.
 	$Apache::lonxml::curdepth.'" id="homework_edit_'.
-	$Apache::lonxml::curdepth.'">'.
+	$Apache::lonxml::curdepth.'" '.&element_change_detection().'>'.
 	&HTML::Entities::encode($data,'<>&"').'</textarea>'.
 	($usehtmlarea?&Apache::lonhtmlcommon::spelllink('lonhomework',
 			 'homework_edit_'.$Apache::lonxml::curdepth):'')."\n";
@@ -623,7 +686,7 @@ sub checked_arg {
 	    $text=$option;
 	    $value='value="'.$option.'"';
 	}
-	$result.="<nobr><input type='checkbox' $value name='".
+	$result.="<nobr><label><input type='checkbox' $value name='".
 	    &html_element_name($name)."'";
 	foreach my $selected (split(/,/,$allselected)) {
 	    if ( $selected eq $option ) {
@@ -631,7 +694,7 @@ sub checked_arg {
 		last;
 	    }
 	}
-	$result.=" />$text</nobr>\n";
+	$result.=&element_change_detection()." />$text</label></nobr>\n";
     }
     return $result;
 }
@@ -642,7 +705,8 @@ sub text_arg {
     if (!defined $size) { $size=20; }
     my $arg=$token->[2]{$name};
     $result=&mt($description).'&nbsp;<input name="'.&html_element_name($name).
-	'" type="text" value="'.$arg.'" size="'.$size.'" />';
+	'" type="text" value="'.$arg.'" size="'.$size.'" '.
+	&element_change_detection().'/>';
     return '<nobr>'.$result.'</nobr>';
 }
 
@@ -668,7 +732,7 @@ sub select_arg {
 	}
     }
     $result.='<nobr>'.$description.'&nbsp;<select name="'.
-	&html_element_name($name).'">
+	&html_element_name($name).'" '.&element_change_detection().' >
        '.$optionlist.'
       </select></nobr>';
     return $result;
@@ -701,6 +765,7 @@ sub select_or_text_arg {
  	((!$found)?' selected="selected"':'').
  	">".&mt('Type-in value')."</option>\n";
 #
+    my $change_code=&element_change_detection();
     my $element=&html_element_name($name);
     my $selectelement='select_list_'.$element;
     my $typeinelement='type_in_'.$element;
@@ -725,7 +790,7 @@ $optionlist
        value="$typeinvalue" 
 onChange="$hiddenvalue=$typedinvalue;"
 onFocus="$selectedindex=$selecttypeinindex-1;" />
-<input type="hidden" name="$element" value="$selected" />
+<input type="hidden" name="$element" value="$selected" $change_code />
 </nobr>
 ENDSELECTORTYPE
 }
@@ -737,13 +802,13 @@ sub entercoords {
     unless ($Apache::edit::bgimgsrc) { return ''; }
     if ($idx) { $idx.='_'; }
     if ($idy) { $idy.='_'; }
-    my $bgfile=&Apache::lonnet::escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
+    my $bgfile=&escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
     my $form    = 'lonhomework';
     my $element;
     if (! defined($mode) || $mode eq 'attribute') {
-        $element = &Apache::lonnet::escape("$Apache::lonxml::curdepth");
+        $element = &escape("$Apache::lonxml::curdepth");
     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
-        $element = &Apache::lonnet::escape('homework_edit_'.
+        $element = &escape('homework_edit_'.
                                            $Apache::lonxml::curdepth);
     }
     my $id=$Apache::lonxml::curdepth;
@@ -772,13 +837,13 @@ sub entercoords {
 sub entercoord {
     my ($idx,$mode,$width,$height,$type) = @_;
     unless ($Apache::edit::bgimgsrc) { return ''; }
-    my $bgfile=&Apache::lonnet::escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
+    my $bgfile=&escape(&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$Apache::edit::bgimgsrc));
     my $form    = 'lonhomework';
     my $element;
     if (! defined($mode) || $mode eq 'attribute') {
-        $element = &Apache::lonnet::escape("$idx\_$Apache::lonxml::curdepth");
+        $element = &escape("$idx\_$Apache::lonxml::curdepth");
     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
-        $element = &Apache::lonnet::escape('homework_edit_'.
+        $element = &escape('homework_edit_'.
                                            $Apache::lonxml::curdepth);
     }
     my $id=$Apache::lonxml::curdepth;
@@ -812,14 +877,14 @@ sub browse {
     my $form    = 'lonhomework';
     my $element;
     if (! defined($mode) || $mode eq 'attribute') {
-        $element = &Apache::lonnet::escape("$id\_$Apache::lonxml::curdepth");
+        $element = &escape("$id\_$Apache::lonxml::curdepth");
     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
-        $element = &Apache::lonnet::escape('homework_edit_'.
+        $element = &escape('homework_edit_'.
                                            $Apache::lonxml::curdepth);	
     }
     my $titleelement;
     if ($titleid) {
-	$titleelement=",'','','".&Apache::lonnet::escape("$titleid\_$Apache::lonxml::curdepth")."'";
+	$titleelement=",'','','".&escape("$titleid\_$Apache::lonxml::curdepth")."'";
     }
     my $result = <<"ENDBUTTON";
 <a href=\"javascript:openbrowser('$form','$element'$titleelement)\"\>Select</a>
@@ -834,14 +899,14 @@ sub search {
     my $form    = 'lonhomework';
     my $element;
     if (! defined($mode) || $mode eq 'attribute') {
-        $element = &Apache::lonnet::escape("$id\_$Apache::lonxml::curdepth");
+        $element = &escape("$id\_$Apache::lonxml::curdepth");
     } elsif ($mode eq 'textnode') {  # for data between <tag> ... </tag>
-        $element = &Apache::lonnet::escape('homework_edit_'.
+        $element = &escape('homework_edit_'.
                                            $Apache::lonxml::curdepth);
     }
     my $titleelement;
     if ($titleid) {
-	$titleelement=",'".&Apache::lonnet::escape("$titleid\_$Apache::lonxml::curdepth")."'";
+	$titleelement=",'".&escape("$titleid\_$Apache::lonxml::curdepth")."'";
     }
     my $result = <<"ENDBUTTON";
 <a href=\"javascript:opensearcher('$form','$element'$titleelement)\"\>Search</a>