--- loncom/interface/loncommon.pm	2006/03/19 22:48:53	1.314
+++ loncom/interface/loncommon.pm	2006/03/21 21:49:30	1.319
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.314 2006/03/19 22:48:53 albertel Exp $
+# $Id: loncommon.pm,v 1.319 2006/03/21 21:49:30 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -2741,6 +2741,9 @@ Inputs:
 
 =item * $notopbar, if true, keep the 'what is this' info but remove the
                    navigational links
+
+=item * $bgcolor, used to override the bg coor on a webpage to a specific value
+
 =back
 
 Returns: A uniform header for LON-CAPA web pages.  
@@ -2752,11 +2755,11 @@ other decorations will be returned.
 
 sub bodytag {
     my ($title,$function,$addentries,$bodyonly,$domain,$forcereg,$customtitle,
-	$notopbar)=@_;
+	$notopbar,$bgcolor)=@_;
     $title=&mt($title);
     $function = &get_users_function() if (!$function);
     my $img=&designparm($function.'.img',$domain);
-    my $pgbg=&designparm($function.'.pgbg',$domain);
+    my $pgbg= $bgcolor || &designparm($function.'.pgbg',$domain);
     my $tabbg=&designparm($function.'.tabbg',$domain);
     my $font=&designparm($function.'.font',$domain);
     my $link=&designparm($function.'.link',$domain);
@@ -2953,6 +2956,12 @@ Inputs: none
 sub endbodytag {
     my $endbodytag='</body>';
     $endbodytag=&Apache::lontexconvert::jsMath_process()."\n".$endbodytag;
+    if ( exists( $env{'internal.head.redirect'} ) ) {
+	$endbodytag=
+	    "<br /><a href=\"$env{'internal.head.redirect'}\">".
+	    &mt('Continue').'</a>'.
+	    $endbodytag;
+    }
     return $endbodytag;
 }
 
@@ -2966,7 +2975,15 @@ Returns a uniform footer for LON-CAPA we
 
 Inputs: $title - optional title for the head
         $head_extra - optional extra HTML to put inside the <head>
-
+        $args - optional arguments
+            force_register - if is true call registerurl so the remote is 
+                             informed
+                             
+            redirect - array ref of seconds before redirect occurs
+                                    url to redirect to
+                           (side effect of setting 
+                               $env{'internal.head.redirect'} to the url 
+                               redirected too)
 =back
 
 =cut
@@ -2978,9 +2995,15 @@ sub headtag {
 	'<head>'.
 	&Apache::lonxml::fontsettings().
 	&Apache::lonhtmlcommon::htmlareaheaders();
-    
+
+    if ($args->{'force_register'}) {
+	$result .= &Apache::lonmenu::registerurl(1);
+    }
+
     if (ref($args->{'redirect'})) {
 	my ($time,$url) = @{$args->{'redirect'}};
+	$url = &Apache::lonenc::check_encrypt($url);
+	$env{'internal.head.redirect'} = $url;
 	$result.=<<ADDMETA
 <meta http-equiv="pragma" content="no-cache" />
 <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$url" />
@@ -2990,7 +3013,7 @@ ADDMETA
 	$title = 'The LearningOnline Network with CAPA';
     }
     
-    $result .= '<title>'.&mt($title).'</title>'.$head_extra;
+    $result .= '<title> LON-CAPA '.&mt($title).'</title>'.$head_extra;
     
     return $result;
 }
@@ -3042,14 +3065,21 @@ Returns a complete <html> .. <body> sect
 
 Inputs: $title - optional title for the page
         $head_extra - optional extra HTML to incude inside the <head>
-        %args - additional optional args supported are:
-                  only_body   -> is true will set &bodytag() onlybodytag arg on
-                  no_nav_bar  -> is true will set &bodytag() notopbar arg on
-                  add_entries -> additional attributes to add to the  <body>
-                  domain      -> force to color decorate a page for a 
-                                 specific domain
-                  function    -> force usage of a specific rolish color scheme
-                  redirect    -> ...
+        $args - additional optional args supported are:
+                  only_body      -> is true will set &bodytag() onlybodytag
+                                    arg on
+                  no_nav_bar     -> is true will set &bodytag() notopbar arg on
+                  add_entries    -> additional attributes to add to the  <body>
+                  domain         -> force to color decorate a page for a 
+                                    specific domain
+                  function       -> force usage of a specific rolish color
+                                    scheme
+                  redirect       -> see &headtag()
+                  bgcolor        -> override the default page bg color
+                  js_ready       -> return a string ready for being used in 
+                                    a javascript writeln
+                  force_register -> if is true will turn on the &bodytag()
+                                    $forcereg arg
 
 =back
 
@@ -3057,17 +3087,27 @@ Inputs: $title - optional title for the
 
 sub start_page {
     my ($title,$head_extra,$args) = @_;
+    #&Apache::lonnet::logthis("start_page ".join(':',caller(0)));
     my %head_args;
-    if (defined($args->{'redirect'})) {
-	$head_args{'redirect'} = $args->{'redirect'};
+    foreach my $arg ('redirect','force_register') {
+	if (defined($args->{$arg})) {
+	    $head_args{$arg} = $args->{$args};
+	}
     }
 
-    return 
+    $env{'internal.start_page'}++;
+    my $result = 
 	&Apache::lonxml::xmlbegin().
 	&headtag($title,$head_extra,\%head_args).&endheadtag().
-	&bodytag($title, $args->{'function'}, $args->{'add_entries'},
-		 $args->{'only_body'},
-		 undef,undef,undef,$args->{'no_nav_bar'});
+	&bodytag($title, 
+		 $args->{'function'},       $args->{'add_entries'},
+		 $args->{'only_body'},      $args->{'domain'},
+		 $args->{'force_register'}, undef,
+		 $args->{'no_nav_bar'},     $args->{'bgcolor'});
+    if ($args->{'js_ready'}) {
+	$result = &js_ready($result);
+    }
+    return $result;
 }
 
 =pod
@@ -3078,14 +3118,70 @@ sub start_page {
 
 Returns a complete </body></html> section for LON-CAPA web pages.
 
-Inputs: None
-
+Inputs:         $args - additional optional args supported are:
+                 js_ready     -> return a string ready for being used in 
+                                 a javascript writeln
 =back
 
 =cut
 
 sub end_page {
-    return &endbodytag."\n</html>";
+    my ($args) = @_;
+    #&Apache::lonnet::logthis("end_page ".join(':',caller(0)));
+    $env{'internal.end_page'}++;
+    my $result = &endbodytag()."\n</html>";
+    if ($args->{'js_ready'}) {
+	$result = &js_ready($result);
+    }
+    return $result;
+}
+
+sub js_ready {
+    my ($result) = @_;
+    $result = &HTML::Entities::encode($result,'<>&"');
+
+    $result =~ s/[\n\r]/ /g;
+    $result =~ s/'/\\'/g;	
+    
+    return $result;
+}
+
+sub validate_page {
+    if (  exists($env{'internal.start_page'})
+	  &&     $env{'internal.start_page'} > 1) {
+	&Apache::lonnet::logthis('start_page called multiple times '.
+				 $env{'internal.start_page'}.' '.
+				 $ENV{'request.filename'});
+    }
+    if (  exists($env{'internal.end_page'})
+	  &&     $env{'internal.end_page'} > 1) {
+	&Apache::lonnet::logthis('end_page called multiple times '.
+				 $env{'internal.end_page'}.' '.
+				 $env{'request.filename'});
+    }
+    if (     exists($env{'internal.start_page'})
+	&& ! exists($env{'internal.end_page'})) {
+	&Apache::lonnet::logthis('start_page called without end_page '.
+				 $env{'request.filename'});
+    }
+    if (   ! exists($env{'internal.start_page'})
+	&&   exists($env{'internal.end_page'})) {
+	&Apache::lonnet::logthis('end_page called without start_page'.
+				 $env{'request.filename'});
+    }
+}
+
+sub simple_error_page {
+    my ($r,$title,$msg) = @_;
+    my $page =
+	&Apache::loncommon::start_page($title).
+	&mt($msg).
+	&Apache::loncommon::end_page();
+    if (ref($r)) {
+	$r->print($page);
+	return OK;
+    }
+    return $page;
 }
 ###############################################