--- loncom/xml/lonxml.pm	2007/06/11 20:52:40	1.448
+++ loncom/xml/lonxml.pm	2007/08/17 21:24:21	1.451
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # XML Parser Module 
 #
-# $Id: lonxml.pm,v 1.448 2007/06/11 20:52:40 albertel Exp $
+# $Id: lonxml.pm,v 1.451 2007/08/17 21:24:21 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -319,13 +319,14 @@ sub xmlparse {
 	     }
 	 }
      }
- } elsif ($env{'construct.style'} && ($env{'request.state'} eq 'construct')) {
+ } elsif ($env{'construct.style'}
+	  && ($env{'request.state'} eq 'construct')) {
      my $location=&Apache::lonnet::filelocation('',$env{'construct.style'});
      my $styletext=&Apache::lonnet::getfile($location);
-       if ($styletext ne '-1') {
-          %style_for_target = (%style_for_target,
-                          &Apache::style::styleparser($target,$styletext));
-      }
+     if ($styletext ne '-1') {
+	 %style_for_target = (%style_for_target,
+			      &Apache::style::styleparser($target,$styletext));
+     }
  }
 #&printalltags();
  my @pars = ();
@@ -967,8 +968,8 @@ sub decreasedepth {
 sub get_id {
     my ($parstack,$safeeval)=@_;
     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
-    if ($env{'request.state'} eq 'construct' && $id =~ /(\.|_)/) {
-	&error(&mt("IDs are not allowed to contain &quot;<tt>_</tt>&quot; or &quot;<tt>.</tt>&quot;"));
+    if ($env{'request.state'} eq 'construct' && $id =~ /([._]|[^\w\d\s[:punct:]])/) {
+	&error(&mt("ID &quot;[_1]&quot; contains invalid characters, IDs are only allowed to contain letters, numbers, spaces and -",'<tt>'.$id.'</tt>'));
     }
     if ($id =~ /^\s*$/) { $id = $Apache::lonxml::curdepth; }
     return $id;
@@ -1349,11 +1350,12 @@ SIMPLECONTENT
 
 
 sub inserteditinfo {
-      my ($result,$filecontents,$filetype)=@_;
+      my ($filecontents,$filetype)=@_;
       $filecontents = &HTML::Entities::encode($filecontents,'<>&"');
 #      my $editheader='<a href="#editsection">Edit below</a><hr />';
       my $xml_help = '';
       my $initialize='';
+      my $add_to_onload;
       if ($filetype eq 'html') {
 	  my $addbuttons=&Apache::lonhtmlcommon::htmlareaaddbuttons();
 	  $initialize=&Apache::lonhtmlcommon::spellheader();
@@ -1369,6 +1371,11 @@ $addbuttons
 	var editor=new HTMLArea("filecont",config);
 	editor.registerPlugin(FullPage);
 	editor.generate();
+	setTimeout(
+		   function () {
+		       HTMLArea._addEvents(editor._doc,
+					   ["keypress","mousedown"], unClean);
+		   },300);
     }
 </script>
 FULLPAGE
@@ -1381,7 +1388,7 @@ $addbuttons
 </script>
 FULLPAGE
 	  }
-          $result=~s/\<body([^\>]*)\>/\<body onload="initDocument()" $1\>/i;
+	  $add_to_onload = 'initDocument();';
 	  $xml_help=&Apache::loncommon::helpLatexCheatsheet();
       }
       my $cleanbut = '';
@@ -1400,24 +1407,24 @@ $cleanbut
 <input type="submit" name="viewmode" accesskey="v" value="$lt{'vi'}" />
 BUTTONS
       $buttons.=&Apache::lonhtmlcommon::spelllink('xmledit','filecont');
+      my $textarea_events = &Apache::edit::element_change_detection();
+      my $form_events     = &Apache::edit::form_change_detection();
       my $editfooter=(<<ENDFOOTER);
 $initialize
 <hr />
 <a name="editsection" />
-<form method="post" name="xmledit">
+<form $form_events method="post" name="xmledit">
 $xml_help
 <input type="hidden" name="editmode" value="$lt{'ed'}" />
 $buttons<br />
-<textarea style="width:100%" cols="80" rows="44" name="filecont" id="filecont">$filecontents</textarea>
+<textarea $textarea_events style="width:100%" cols="80" rows="44" name="filecont" id="filecont">$filecontents</textarea>
 <br />$buttons
 <br />
 </form>
 $titledisplay
 </body>
 ENDFOOTER
-#      $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
-      $result=~s/(\<\/body\>)/$editfooter/is;
-      return $result;
+      return ($editfooter,$add_to_onload);;
 }
 
 sub get_target {
@@ -1527,24 +1534,31 @@ ENDNOTFOUND
 #
     unless ($env{'request.state'} eq 'published') {
 	if ($env{'form.editmode'} && (!($env{'form.viewmode'})) && (!($env{'form.discardview'})))
-	    {
+	{
+	    my ($edit_info, $add_to_onload)=
+		&inserteditinfo($filecontents,$filetype);
+
 	    my $displayfile=$request->uri;
 	    $displayfile=~s/^\/[^\/]*//;
-	    my %options = ();
+	    my %options = 
+		('add_entries' =>
+		    {'onload'   => $add_to_onload, });
 	    if ($env{'environment.remote'} ne 'off') {
 		$options{'bgcolor'}   = '#FFFFFF';
+		$options{'only_body'} = 1;
 	    }
-	    my $start_page = &Apache::loncommon::start_page(undef,undef,
+	    my $js = &Apache::edit::js_change_detection();
+	    my $start_page = &Apache::loncommon::start_page(undef,$js,
 							    \%options);
 	    $result=$start_page.
 		&Apache::lonxml::message_location().'<h3>'.
 		$displayfile.
-		'</h3>'.&Apache::loncommon::end_page();
-	    $result=&inserteditinfo($result,$filecontents,$filetype);
+		'</h3>'.
+		$edit_info.
+		&Apache::loncommon::end_page();
 	}
     }
     if ($filetype eq 'html') { &writeallows($request->uri); }
-	
     
     &Apache::lonxml::add_messages(\$result);
     $request->print($result);