File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.174: download - view: text, annotated - select for diffs
Thu May 29 00:19:30 2008 UTC (16 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
bug 5710.  Include timezone information.

    1: # The LearningOnline Network with CAPA
    2: # a pile of common html routines
    3: #
    4: # $Id: lonhtmlcommon.pm,v 1.174 2008/05/29 00:19:30 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ######################################################################
   29: ######################################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::lonhtmlcommon - routines to do common html things
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Referenced by other mod_perl Apache modules.
   40: 
   41: =head1 INTRODUCTION
   42: 
   43: lonhtmlcommon is a collection of subroutines used to present information
   44: in a consistent html format, or provide other functionality related to
   45: html.
   46: 
   47: =head2 General Subroutines
   48: 
   49: =over 4
   50: 
   51: =cut 
   52: 
   53: ######################################################################
   54: ######################################################################
   55: 
   56: package Apache::lonhtmlcommon;
   57: 
   58: use strict;
   59: use Time::Local;
   60: use Time::HiRes;
   61: use Apache::lonlocal;
   62: use Apache::lonnet;
   63: use LONCAPA;
   64: 
   65: ##############################################
   66: ##############################################
   67: 
   68: =pod
   69: 
   70: =item authorbombs
   71: 
   72: =cut
   73: 
   74: ##############################################
   75: ##############################################
   76: 
   77: sub authorbombs {
   78:     my $url=shift;
   79:     $url=&Apache::lonnet::declutter($url);
   80:     my ($udom,$uname)=($url=~m{^($LONCAPA::domain_re)/($LONCAPA::username_re)/});
   81:     my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
   82:     foreach (keys %bombs) {
   83: 	if ($_=~/^$udom\/$uname\//) {
   84: 	    return '<a href="/adm/bombs/'.$url.
   85: 		'"><img src="'.&Apache::loncommon::lonhttpdurl('/adm/lonMisc/bomb.gif').'" border="0" /></a>'.
   86: 		&Apache::loncommon::help_open_topic('About_Bombs');
   87: 	}
   88:     }
   89:     return '';
   90: }
   91: 
   92: ##############################################
   93: ##############################################
   94: 
   95: sub recent_filename {
   96:     my $area=shift;
   97:     return 'nohist_recent_'.&escape($area);
   98: }
   99: 
  100: sub store_recent {
  101:     my ($area,$name,$value,$freeze)=@_;
  102:     my $file=&recent_filename($area);
  103:     my %recent=&Apache::lonnet::dump($file);
  104:     if (scalar(keys(%recent))>20) {
  105: # remove oldest value
  106: 	my $oldest=time();
  107: 	my $delkey='';
  108: 	foreach my $item (keys(%recent)) {
  109: 	    my $thistime=(split(/\&/,$recent{$item}))[0];
  110: 	    if (($thistime ne "always_include") && ($thistime<$oldest)) {
  111: 		$oldest=$thistime;
  112: 		$delkey=$item;
  113: 	    }
  114: 	}
  115: 	&Apache::lonnet::del($file,[$delkey]);
  116:     }
  117: # store new value
  118:     my $timestamp;
  119:     if ($freeze) {
  120:         $timestamp = "always_include";
  121:     } else {
  122:         $timestamp = time();
  123:     }   
  124:     &Apache::lonnet::put($file,{ $name => 
  125: 				 $timestamp.'&'.&escape($value) });
  126: }
  127: 
  128: sub remove_recent {
  129:     my ($area,$names)=@_;
  130:     my $file=&recent_filename($area);
  131:     return &Apache::lonnet::del($file,$names);
  132: }
  133: 
  134: sub select_recent {
  135:     my ($area,$fieldname,$event)=@_;
  136:     my %recent=&Apache::lonnet::dump(&recent_filename($area));
  137:     my $return="\n<select name='$fieldname'".
  138: 	($event?" onchange='$event'":'').
  139: 	">\n<option value=''>--- ".&mt('Recent')." ---</option>";
  140:     foreach my $value (sort(keys(%recent))) {
  141: 	unless ($value =~/^error\:/) {
  142: 	    my $escaped = &Apache::loncommon::escape_url($value);
  143: 	    &Apache::loncommon::inhibit_menu_check(\$escaped);
  144: 	    $return.="\n<option value='$escaped'>".
  145: 		&unescape((split(/\&/,$recent{$value}))[1]).
  146: 		'</option>';
  147: 	}
  148:     }
  149:     $return.="\n</select>\n";
  150:     return $return;
  151: }
  152: 
  153: sub get_recent {
  154:     my ($area, $n) = @_;
  155:     my %recent=&Apache::lonnet::dump(&recent_filename($area));
  156: 
  157: # Create hash with key as time and recent as value
  158: # Begin filling return_hash with any 'always_include' option
  159:     my %time_hash = ();
  160:     my %return_hash = ();
  161:     foreach my $item (keys %recent) {
  162:         my ($thistime,$thisvalue)=(split(/\&/,$recent{$item}));
  163:         if ($thistime eq 'always_include') {
  164:             $return_hash{$item} = &unescape($thisvalue);
  165:             $n--;
  166:         } else {
  167:             $time_hash{$thistime} = $item;
  168:         }
  169:     }
  170: 
  171: # Sort by decreasing time and return key value pairs
  172:     my $idx = 1;
  173:     foreach my $item (reverse(sort(keys(%time_hash)))) {
  174:        $return_hash{$time_hash{$item}} =
  175:                   &unescape((split(/\&/,$recent{$time_hash{$item}}))[1]);
  176:        if ($n && ($idx++ >= $n)) {last;}
  177:     }
  178: 
  179:     return %return_hash;
  180: }
  181: 
  182: sub get_recent_frozen {
  183:     my ($area) = @_;
  184:     my %recent=&Apache::lonnet::dump(&recent_filename($area));
  185: 
  186: # Create hash with all 'frozen' items
  187:     my %return_hash = ();
  188:     foreach my $item (keys(%recent)) {
  189:         my ($thistime,$thisvalue)=(split(/\&/,$recent{$item}));
  190:         if ($thistime eq 'always_include') {
  191:             $return_hash{$item} = &unescape($thisvalue);
  192:         }
  193:     }
  194:     return %return_hash;
  195: }
  196: 
  197: 
  198: 
  199: =pod
  200: 
  201: =item textbox
  202: 
  203: =cut
  204: 
  205: ##############################################
  206: ##############################################
  207: sub textbox {
  208:     my ($name,$value,$size,$special) = @_;
  209:     $size = 40 if (! defined($size));
  210:     $value = &HTML::Entities::encode($value,'<>&"');
  211:     my $Str = '<input type="text" name="'.$name.'" size="'.$size.'" '.
  212:         'value="'.$value.'" '.$special.' />';
  213:     return $Str;
  214: }
  215: 
  216: ##############################################
  217: ##############################################
  218: 
  219: =pod
  220: 
  221: =item checkbox
  222: 
  223: =cut
  224: 
  225: ##############################################
  226: ##############################################
  227: sub checkbox {
  228:     my ($name,$checked,$value) = @_;
  229:     my $Str = '<input type="checkbox" name="'.$name.'" ';
  230:     if (defined($value)) {
  231:         $Str .= 'value="'.$value.'"';
  232:     } 
  233:     if ($checked) {
  234:         $Str .= ' checked="1"';
  235:     }
  236:     $Str .= ' />';
  237:     return $Str;
  238: }
  239: 
  240: 
  241: =pod
  242: 
  243: =item radiobutton
  244: 
  245: =cut
  246: 
  247: ##############################################
  248: ##############################################
  249: sub radio {
  250:     my ($name,$checked,$value) = @_;
  251:     my $Str = '<input type="radio" name="'.$name.'" ';
  252:     if (defined($value)) {
  253:         $Str .= 'value="'.$value.'"';
  254:     } 
  255:     if ($checked eq $value) {
  256:         $Str .= ' checked="1"';
  257:     }
  258:     $Str .= ' />';
  259:     return $Str;
  260: }
  261: 
  262: ##############################################
  263: ##############################################
  264: 
  265: =pod
  266: 
  267: =item &date_setter
  268: 
  269: &date_setter returns html and javascript for a compact date-setting form.
  270: To retrieve values from it, use &get_date_from_form().
  271: 
  272: Inputs
  273: 
  274: =over 4
  275: 
  276: =item $dname 
  277: 
  278: The name to prepend to the form elements.  
  279: The form elements defined will be dname_year, dname_month, dname_day,
  280: dname_hour, dname_min, and dname_sec.
  281: 
  282: =item $currentvalue
  283: 
  284: The current setting for this time parameter.  A unix format time
  285: (time in seconds since the beginning of Jan 1st, 1970, GMT.  
  286: An undefined value is taken to indicate the value is the current time.
  287: Also, to be explicit, a value of 'now' also indicates the current time.
  288: 
  289: =item $special
  290: 
  291: Additional html/javascript to be associated with each element in
  292: the date_setter.  See lonparmset for example usage.
  293: 
  294: =item $includeempty 
  295: 
  296: =item $state
  297: 
  298: Specifies the initial state of the form elements.  Either 'disabled' or empty.
  299: Defaults to empty, which indiciates the form elements are not disabled. 
  300: 
  301: =back
  302: 
  303: Bugs
  304: 
  305: The method used to restrict user input will fail in the year 2400.
  306: 
  307: =cut
  308: 
  309: ##############################################
  310: ##############################################
  311: sub date_setter {
  312:     my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
  313:         $no_hh_mm_ss,$defhour,$defmin,$defsec,$nolink) = @_;
  314:     my $wasdefined=1;
  315:     if (! defined($state) || $state ne 'disabled') {
  316:         $state = '';
  317:     }
  318:     if (! defined($no_hh_mm_ss)) {
  319:         $no_hh_mm_ss = 0;
  320:     }
  321:     if ($currentvalue eq 'now') {
  322: 	$currentvalue=time;
  323:     }
  324:     if ((!defined($currentvalue)) || ($currentvalue eq '')) {
  325: 	$wasdefined=0;
  326: 	if ($includeempty) {
  327: 	    $currentvalue = 0;
  328: 	} else {
  329: 	    $currentvalue = time;
  330: 	}
  331:     }
  332:     # other potentially useful values:     wkday,yrday,is_daylight_savings
  333:     my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','','');
  334:     if ($currentvalue) {
  335: 	($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = 
  336: 	    localtime($currentvalue);
  337: 	$year += 1900;
  338:     }
  339:     unless ($wasdefined) {
  340: 	if (($defhour) || ($defmin) || ($defsec)) {
  341: 	    ($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = 
  342: 		localtime(time);
  343: 	    $year += 1900;
  344: 	    $sec=($defsec?$defsec:0);
  345: 	    $min=($defmin?$defmin:0);
  346: 	    $hour=($defhour?$defhour:0);
  347: 	} elsif (!$includeempty) {
  348: 	    $sec=0;
  349: 	    $min=0;
  350: 	    $hour=0;
  351: 	}
  352:     }
  353:     my $result = "\n<!-- $dname date setting form -->\n";
  354:     $result .= <<ENDJS;
  355: <script type="text/javascript">
  356:     function $dname\_checkday() {
  357:         var day   = document.$formname.$dname\_day.value;
  358:         var month = document.$formname.$dname\_month.value;
  359:         var year  = document.$formname.$dname\_year.value;
  360:         var valid = true;
  361:         if (day < 1) {
  362:             document.$formname.$dname\_day.value = 1;
  363:         } 
  364:         if (day > 31) {
  365:             document.$formname.$dname\_day.value = 31;
  366:         }
  367:         if ((month == 1)  || (month == 3)  || (month == 5)  ||
  368:             (month == 7)  || (month == 8)  || (month == 10) ||
  369:             (month == 12)) {
  370:             if (day > 31) {
  371:                 document.$formname.$dname\_day.value = 31;
  372:                 day = 31;
  373:             }
  374:         } else if (month == 2 ) {
  375:             if ((year % 4 == 0) && (year % 100 != 0)) {
  376:                 if (day > 29) {
  377:                     document.$formname.$dname\_day.value = 29;
  378:                 }
  379:             } else if (day > 29) {
  380:                 document.$formname.$dname\_day.value = 28;
  381:             }
  382:         } else if (day > 30) {
  383:             document.$formname.$dname\_day.value = 30;
  384:         }
  385:     }
  386:     
  387:     function $dname\_disable() {
  388:         document.$formname.$dname\_month.disabled=true;
  389:         document.$formname.$dname\_day.disabled=true;
  390:         document.$formname.$dname\_year.disabled=true;
  391:         document.$formname.$dname\_hour.disabled=true;
  392:         document.$formname.$dname\_minute.disabled=true;
  393:         document.$formname.$dname\_second.disabled=true;
  394:     }
  395: 
  396:     function $dname\_enable() {
  397:         document.$formname.$dname\_month.disabled=false;
  398:         document.$formname.$dname\_day.disabled=false;
  399:         document.$formname.$dname\_year.disabled=false;
  400:         document.$formname.$dname\_hour.disabled=false;
  401:         document.$formname.$dname\_minute.disabled=false;
  402:         document.$formname.$dname\_second.disabled=false;        
  403:     }
  404: 
  405:     function $dname\_opencalendar() {
  406:         if (! document.$formname.$dname\_month.disabled) {
  407:             var calwin=window.open(
  408: "/adm/announcements?pickdate=yes&formname=$formname&element=$dname&month="+
  409: document.$formname.$dname\_month.value+"&year="+
  410: document.$formname.$dname\_year.value,
  411:              "LONCAPAcal",
  412:               "height=350,width=350,scrollbars=yes,resizable=yes,menubar=no");
  413:         }
  414: 
  415:     }
  416: </script>
  417: ENDJS
  418:     $result .= '  <span style="white-space: nowrap;">';
  419:     my $monthselector = qq{<select name="$dname\_month" $special $state onchange="javascript:$dname\_checkday()" >};
  420:     # Month
  421:     my @Months = qw/January February  March     April   May      June 
  422:                     July    August    September October November December/;
  423:     # Pad @Months with a bogus value to make indexing easier
  424:     unshift(@Months,'If you can read this an error occurred');
  425:     if ($includeempty) { $monthselector.="<option value=''></option>"; }
  426:     for(my $m = 1;$m <=$#Months;$m++) {
  427:         $monthselector .= qq{      <option value="$m" };
  428:         $monthselector .= "selected " if ($m-1 eq $month);
  429:         $monthselector .= '> '.&mt($Months[$m]).' </option>';
  430:     }
  431:     $monthselector.= '  </select>';
  432:     # Day
  433:     my $dayselector = qq{<input type="text" name="$dname\_day" $state value="$mday" size="3" $special onchange="javascript:$dname\_checkday()" />};
  434:     # Year
  435:     my $yearselector = qq{<input type="year" name="$dname\_year" $state value="$year" size="5" $special onchange="javascript:$dname\_checkday()" />};
  436:     #
  437:     my $hourselector = qq{<select name="$dname\_hour" $special $state >};
  438:     if ($includeempty) { 
  439:         $hourselector.=qq{<option value=''></option>};
  440:     }
  441:     for (my $h = 0;$h<24;$h++) {
  442:         $hourselector .= qq{<option value="$h" };
  443:         $hourselector .= "selected " if (defined($hour) && $hour == $h);
  444:         $hourselector .= ">";
  445:         my $timest='';
  446:         if ($h == 0) {
  447:             $timest .= "12 am";
  448:         } elsif($h == 12) {
  449:             $timest .= "12 noon";
  450:         } elsif($h < 12) {
  451:             $timest .= "$h am";
  452:         } else {
  453:             $timest .= $h-12 ." pm";
  454:         }
  455:         $timest=&mt($timest);
  456:         $hourselector .= $timest." </option>\n";
  457:     }
  458:     $hourselector .= "  </select>\n";
  459:     my $minuteselector = qq{<input type="text" name="$dname\_minute" $special $state value="$min" size="3" />};
  460:     my $secondselector= qq{<input type="text" name="$dname\_second" $special $state value="$sec" size="3" />};
  461:     my $cal_link;
  462:     if (!$nolink) {
  463:         $cal_link = qq{<a href="javascript:$dname\_opencalendar()">};
  464:     }
  465:     #
  466:     my $dt = DateTime->from_epoch(epoch => $currentvalue)
  467:                      ->set_time_zone(&Apache::lonlocal::gettimezone());
  468:     my $tzone = ' '.$dt->time_zone_short_name().' ';
  469:     if ($no_hh_mm_ss) {
  470:         $result .= &mt('[_1] [_2] [_3] ',
  471:                        $monthselector,$dayselector,$yearselector).
  472:                    $tzone;
  473:         if (!$nolink) {
  474:             $result .= &mt('[_1]Select Date[_2]',$cal_link,'</a>');
  475:         }
  476:     } else {
  477:         $result .= &mt('[_1] [_2] [_3] [_4] [_5]m [_6]s ',
  478:                       $monthselector,$dayselector,$yearselector,
  479:                       $hourselector,$minuteselector,$secondselector).
  480:                    $tzone;
  481:         if (!$nolink) {
  482:             $result .= &mt('[_1]Select Date[_2]',$cal_link,'</a>');
  483:         }
  484:     }
  485:     $result .= "</span>\n<!-- end $dname date setting form -->\n";
  486:     return $result;
  487: }
  488: 
  489: 
  490: sub build_url {
  491:     my ($base, $fields)=@_;
  492:     my $url;
  493:     $url = $base.'?';
  494:     foreach my $key (keys(%$fields)) {
  495:         $url.=&escape($key).'='.&escape($$fields{$key}).'&amp;';
  496:     }
  497:     $url =~ s/&amp;$//;
  498:     return $url;
  499: }
  500: 
  501: 
  502: ##############################################
  503: ##############################################
  504: 
  505: =pod
  506: 
  507: =item &get_date_from_form
  508: 
  509: get_date_from_form retrieves the date specified in an &date_setter form.
  510: 
  511: Inputs:
  512: 
  513: =over 4
  514: 
  515: =item $dname
  516: 
  517: The name passed to &datesetter, which prefixes the form elements.
  518: 
  519: =item $defaulttime
  520: 
  521: The unix time to use as the default in case of poor inputs.
  522: 
  523: =back
  524: 
  525: Returns: Unix time represented in the form.
  526: 
  527: =cut
  528: 
  529: ##############################################
  530: ##############################################
  531: sub get_date_from_form {
  532:     my ($dname) = @_;
  533:     my ($sec,$min,$hour,$day,$month,$year);
  534:     #
  535:     if (defined($env{'form.'.$dname.'_second'})) {
  536:         my $tmpsec = $env{'form.'.$dname.'_second'};
  537:         if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
  538:             $sec = $tmpsec;
  539:         }
  540: 	if (!defined($tmpsec) || $tmpsec eq '') { $sec = 0; }
  541:     } else {
  542:         $sec = 0;
  543:     }
  544:     if (defined($env{'form.'.$dname.'_minute'})) {
  545:         my $tmpmin = $env{'form.'.$dname.'_minute'};
  546:         if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
  547:             $min = $tmpmin;
  548:         }
  549: 	if (!defined($tmpmin) || $tmpmin eq '') { $min = 0; }
  550:     } else {
  551:         $min = 0;
  552:     }
  553:     if (defined($env{'form.'.$dname.'_hour'})) {
  554:         my $tmphour = $env{'form.'.$dname.'_hour'};
  555:         if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
  556:             $hour = $tmphour;
  557:         }
  558:     } else {
  559:         $hour = 0;
  560:     }
  561:     if (defined($env{'form.'.$dname.'_day'})) {
  562:         my $tmpday = $env{'form.'.$dname.'_day'};
  563:         if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
  564:             $day = $tmpday;
  565:         }
  566:     }
  567:     if (defined($env{'form.'.$dname.'_month'})) {
  568:         my $tmpmonth = $env{'form.'.$dname.'_month'};
  569:         if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
  570:             $month = $tmpmonth - 1;
  571:         }
  572:     }
  573:     if (defined($env{'form.'.$dname.'_year'})) {
  574:         my $tmpyear = $env{'form.'.$dname.'_year'};
  575:         if (($tmpyear =~ /^\d+$/) && ($tmpyear > 1900)) {
  576:             $year = $tmpyear - 1900;
  577:         }
  578:     }
  579:     if (($year<70) || ($year>137)) { return undef; }
  580:     if (defined($sec) && defined($min)   && defined($hour) &&
  581:         defined($day) && defined($month) && defined($year) &&
  582:         eval('&timelocal($sec,$min,$hour,$day,$month,$year)')) {
  583:         return &timelocal($sec,$min,$hour,$day,$month,$year);
  584:     } else {
  585:         return undef;
  586:     }
  587: }
  588: 
  589: ##############################################
  590: ##############################################
  591: 
  592: =pod
  593: 
  594: =item &pjump_javascript_definition()
  595: 
  596: Returns javascript defining the 'pjump' function, which opens up a
  597: parameter setting wizard.
  598: 
  599: =cut
  600: 
  601: ##############################################
  602: ##############################################
  603: sub pjump_javascript_definition {
  604:     my $Str = <<END;
  605:     function pjump(type,dis,value,marker,ret,call,hour,min,sec) {
  606:         parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
  607:                  +"&value="+escape(value)+"&marker="+escape(marker)
  608:                  +"&return="+escape(ret)
  609:                  +"&call="+escape(call)+"&name="+escape(dis)
  610:                  +"&defhour="+escape(hour)+"&defmin="+escape(min)
  611:                  +"&defsec="+escape(sec),"LONCAPAparms",
  612:                  "height=350,width=350,scrollbars=no,menubar=no");
  613:     }
  614: END
  615:     return $Str;
  616: }
  617: 
  618: ##############################################
  619: ##############################################
  620: 
  621: =pod
  622: 
  623: =item &javascript_nothing()
  624: 
  625: Return an appropriate null for the users browser.  This is used
  626: as the first arguement for window.open calls when you want a blank
  627: window that you can then write to.
  628: 
  629: =cut
  630: 
  631: ##############################################
  632: ##############################################
  633: sub javascript_nothing {
  634:     # mozilla and other browsers work with "''", but IE on mac does not.
  635:     my $nothing = "''";
  636:     my $user_browser;
  637:     my $user_os;
  638:     $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
  639:     $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
  640:     if (! defined($user_browser) || ! defined($user_os)) {
  641:         (undef,$user_browser,undef,undef,undef,$user_os) = 
  642:                            &Apache::loncommon::decode_user_agent();
  643:     }
  644:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
  645:         $nothing = "'javascript:void(0);'";
  646:     }
  647:     return $nothing;
  648: }
  649: 
  650: ##############################################
  651: ##############################################
  652: sub javascript_docopen {
  653:     my ($mimetype) = @_;
  654:     $mimetype ||= 'text/html';
  655:     # safari does not understand document.open() and loads "text/html"
  656:     my $nothing = "''";
  657:     my $user_browser;
  658:     my $user_os;
  659:     $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
  660:     $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
  661:     if (! defined($user_browser) || ! defined($user_os)) {
  662:         (undef,$user_browser,undef,undef,undef,$user_os) = 
  663:                            &Apache::loncommon::decode_user_agent();
  664:     }
  665:     if ($user_browser eq 'safari' && $user_os =~ 'mac') {
  666:         $nothing = "document.clear()";
  667:     } else {
  668: 	$nothing = "document.open('$mimetype','replace')";
  669:     }
  670:     return $nothing;
  671: }
  672: 
  673: 
  674: ##############################################
  675: ##############################################
  676: 
  677: =pod
  678: 
  679: =item &StatusOptions()
  680: 
  681: Returns html for a selection box which allows the user to choose the
  682: enrollment status of students.  The selection box name is 'Status'.
  683: 
  684: Inputs:
  685: 
  686: $status: the currently selected status.  If undefined the value of
  687: $env{'form.Status'} is taken.  If that is undefined, a value of 'Active'
  688: is used.
  689: 
  690: $formname: The name of the form.  If defined the onchange attribute of
  691: the selection box is set to document.$formname.submit().
  692: 
  693: $size: the size (number of lines) of the selection box.
  694: 
  695: $onchange: javascript to use when the value is changed.  Enclosed in 
  696: double quotes, ""s, not single quotes.
  697: 
  698: Returns: a perl string as described.
  699: 
  700: =cut
  701: 
  702: ##############################################
  703: ##############################################
  704: sub StatusOptions {
  705:     my ($status, $formName,$size,$onchange,$mult)=@_;
  706:     $size = 1 if (!defined($size));
  707:     if (! defined($status)) {
  708:         $status = 'Active';
  709:         $status = $env{'form.Status'} if (exists($env{'form.Status'}));
  710:     }
  711: 
  712:     my $Str = '';
  713:     $Str .= '<select name="Status"';
  714:     if (defined($mult)){
  715:         $Str .= ' multiple="multiple" ';
  716:     }
  717:     if(defined($formName) && $formName ne '' && ! defined($onchange)) {
  718:         $Str .= ' onchange="document.'.$formName.'.submit()"';
  719:     }
  720:     if (defined($onchange)) {
  721:         $Str .= ' onchange="'.$onchange.'"';
  722:     }
  723:     $Str .= ' size="'.$size.'" ';
  724:     $Str .= '>'."\n";
  725:     foreach my $type (['Active',  &mt('Currently Has Access')],
  726: 		      ['Future',  &mt('Will Have Future Access')],
  727: 		      ['Expired', &mt('Previously Had Access')],
  728: 		      ['Any',     &mt('Any Access Status')]) {
  729: 	my ($name,$label) = @$type;
  730: 	$Str .= '<option value="'.$name.'" ';
  731: 	if ($status eq $name) {
  732: 	    $Str .= 'selected="selected" ';
  733: 	}
  734: 	$Str .= '>'.$label.'</option>'."\n";
  735:     }
  736: 
  737:     $Str .= '</select>'."\n";
  738: }
  739: 
  740: ########################################################
  741: ########################################################
  742: 
  743: =pod
  744: 
  745: =item Progess Window Handling Routines
  746: 
  747: These routines handle the creation, update, increment, and closure of 
  748: progress windows.  The progress window reports to the user the number
  749: of items completed and an estimate of the time required to complete the rest.
  750: 
  751: =over 4
  752: 
  753: 
  754: =item &Create_PrgWin
  755: 
  756: Writes javascript to the client to open a progress window and returns a
  757: data structure used for bookkeeping.
  758: 
  759: Inputs
  760: 
  761: =over 4
  762: 
  763: =item $r Apache request
  764: 
  765: =item $title The title of the progress window
  766: 
  767: =item $heading A description (usually 1 line) of the process being initiated.
  768: 
  769: =item $number_to_do The total number of items being processed.
  770: 
  771: =item $type Either 'popup' or 'inline' (popup is assumed if nothing is
  772:        specified)
  773: 
  774: =item $width Specify the width in charaters of the input field.
  775: 
  776: =item $formname Only useful in the inline case, if a form already exists, this needs to be used and specfiy the name of the form, otherwise the Progress line will be created in a new form of it's own
  777: 
  778: =item $inputname Only useful in the inline case, if a form and an input of type text exists, use this to specify the name of the input field 
  779: 
  780: =back
  781: 
  782: Returns a hash containing the progress state data structure.
  783: 
  784: 
  785: =item &Update_PrgWin
  786: 
  787: Updates the text in the progress indicator.  Does not increment the count.
  788: See &Increment_PrgWin.
  789: 
  790: Inputs:
  791: 
  792: =over 4
  793: 
  794: =item $r Apache request
  795: 
  796: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
  797: 
  798: =item $displaystring The string to write to the status indicator
  799: 
  800: =back
  801: 
  802: Returns: none
  803: 
  804: 
  805: =item Increment_PrgWin
  806: 
  807: Increment the count of items completed for the progress window by 1.  
  808: 
  809: Inputs:
  810: 
  811: =over 4
  812: 
  813: =item $r Apache request
  814: 
  815: =item $prog_state Pointer to the data structure returned by Create_PrgWin
  816: 
  817: =item $extraInfo A description of the items being iterated over.  Typically
  818: 'student'.
  819: 
  820: =back
  821: 
  822: Returns: none
  823: 
  824: 
  825: =item Close_PrgWin
  826: 
  827: Closes the progress window.
  828: 
  829: Inputs:
  830: 
  831: =over 4 
  832: 
  833: =item $r Apache request
  834: 
  835: =item $prog_state Pointer to the data structure returned by Create_PrgWin
  836: 
  837: =back
  838: 
  839: Returns: none
  840: 
  841: =back
  842: 
  843: =cut
  844: 
  845: ########################################################
  846: ########################################################
  847: 
  848: my $uniq=0;
  849: sub get_uniq_name {
  850:     $uniq++;
  851:     return 'uniquename'.$uniq;
  852: }
  853: 
  854: # Create progress
  855: sub Create_PrgWin {
  856:     my ($r, $title, $heading, $number_to_do,$type,$width,$formname,
  857: 	$inputname)=@_;
  858:     if (!defined($type)) { $type='popup'; }
  859:     if (!defined($width)) { $width=55; }
  860:     my %prog_state;
  861:     $prog_state{'type'}=$type;
  862:     if ($type eq 'popup') {
  863: 	$prog_state{'window'}='popwin';
  864: 	my $start_page =
  865: 	    &Apache::loncommon::start_page($title,undef,
  866: 					   {'only_body' => 1,
  867: 					    'bgcolor'   => '#88DDFF',
  868: 					    'js_ready'  => 1});
  869: 	my $end_page = &Apache::loncommon::end_page({'js_ready'  => 1});
  870: 
  871: 	#the whole function called through timeout is due to issues
  872: 	#in mozilla Read BUG #2665 if you want to know the whole story
  873: 	&r_print($r,'<script type="text/javascript">'.
  874:         "var popwin;
  875:          function openpopwin () {
  876:          popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
  877:         "popwin.document.writeln(\'".$start_page.
  878:               "<h4>".&mt("$heading")."<\/h4>".
  879:               "<form action= \"\" name=\"popremain\" method=\"post\">".
  880:               '<input type="text" size="'.$width.'" name="remaining" value="'.
  881: 	      &mt('Starting').'" /><\\/form>'.$end_page.
  882:               "\');".
  883:         "popwin.document.close();}".
  884:         "\nwindow.setTimeout(openpopwin,0)</script>");
  885: 	$prog_state{'formname'}='popremain';
  886: 	$prog_state{'inputname'}="remaining";
  887:     } elsif ($type eq 'inline') {
  888: 	$prog_state{'window'}='window';
  889: 	if (!$formname) {
  890: 	    $prog_state{'formname'}=&get_uniq_name();
  891: 	    &r_print($r,'<form action="" name="'.$prog_state{'formname'}.'">');
  892: 	} else {
  893: 	    $prog_state{'formname'}=$formname;
  894: 	}
  895: 	if (!$inputname) {
  896: 	    $prog_state{'inputname'}=&get_uniq_name();
  897: 	    &r_print($r,&mt("$heading [_1]",' <input type="text" name="'.$prog_state{'inputname'}.'" size="'.$width.'" />'));
  898: 	} else {
  899: 	    $prog_state{'inputname'}=$inputname;
  900: 	    
  901: 	}
  902: 	if (!$formname) { &r_print($r,'</form>'); }
  903: 	&Update_PrgWin($r,\%prog_state,&mt('Starting'));
  904:     }
  905: 
  906:     $prog_state{'done'}=0;
  907:     $prog_state{'firststart'}=&Time::HiRes::time();
  908:     $prog_state{'laststart'}=&Time::HiRes::time();
  909:     $prog_state{'max'}=$number_to_do;
  910:     
  911:     return %prog_state;
  912: }
  913: 
  914: # update progress
  915: sub Update_PrgWin {
  916:     my ($r,$prog_state,$displayString)=@_;
  917:     &r_print($r,'<script type="text/javascript">'.$$prog_state{'window'}.'.document.'.
  918: 	     $$prog_state{'formname'}.'.'.
  919: 	     $$prog_state{'inputname'}.'.value="'.
  920: 	     $displayString.'";</script>');
  921:     $$prog_state{'laststart'}=&Time::HiRes::time();
  922: }
  923: 
  924: # increment progress state
  925: sub Increment_PrgWin {
  926:     my ($r,$prog_state,$extraInfo)=@_;
  927:     $$prog_state{'done'}++;
  928:     my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
  929:         $$prog_state{'done'} *
  930: 	($$prog_state{'max'}-$$prog_state{'done'});
  931:     $time_est = int($time_est);
  932:     #
  933:     my $min = int($time_est/60);
  934:     my $sec = $time_est % 60;
  935:     # 
  936:     my $str;
  937:     if ($min == 0 && $sec > 1) {
  938:         $str = '[_2] seconds';
  939:     } elsif ($min == 1 && $sec > 1) {
  940:         $str = '1 minute [_2] seconds';
  941:     } elsif ($min == 1 && $sec < 2) {
  942:         $str = '1 minute';
  943:     } elsif ($min < 10 && $sec > 1) {
  944:         $str = '[_1] minutes, [_2] seconds';
  945:     } elsif ($min >= 10 || $sec < 2) {
  946:         $str = '[_1] minutes';
  947:     }
  948:     $time_est = &mt($str,$min,$sec);
  949:     #
  950:     my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
  951:     if ($lasttime > 9) {
  952:         $lasttime = int($lasttime);
  953:     } elsif ($lasttime < 0.01) {
  954:         $lasttime = 0;
  955:     } else {
  956:         $lasttime = sprintf("%3.2f",$lasttime);
  957:     }
  958:     if ($lasttime == 1) {
  959:         $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
  960:     } else {
  961:         $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
  962:     }
  963:     #
  964:     my $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
  965:     my $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
  966:     if (! defined($user_browser) || ! defined($user_os)) {
  967:         (undef,$user_browser,undef,undef,undef,$user_os) = 
  968:                            &Apache::loncommon::decode_user_agent();
  969:     }
  970:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
  971:         $lasttime = '';
  972:     }
  973:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
  974: 	     $$prog_state{'formname'}.'.'.
  975: 	     $$prog_state{'inputname'}.'.value="'.
  976: 	     $$prog_state{'done'}.'/'.$$prog_state{'max'}.
  977: 	     ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
  978:     $$prog_state{'laststart'}=&Time::HiRes::time();
  979: }
  980: 
  981: # close Progress Line
  982: sub Close_PrgWin {
  983:     my ($r,$prog_state)=@_;
  984:     if ($$prog_state{'type'} eq 'popup') {
  985: 	&r_print($r,'<script>popwin.close()</script>'."\n");
  986:     } elsif ($$prog_state{'type'} eq 'inline') {
  987: 	&Update_PrgWin($r,$prog_state,&mt('Done'));
  988:     }
  989:     undef(%$prog_state);
  990: }
  991: 
  992: sub r_print {
  993:     my ($r,$to_print)=@_;
  994:     if ($r) {
  995: 	$r->print($to_print);
  996: 	$r->rflush();
  997:     } else {
  998: 	print($to_print);
  999:     }
 1000: }
 1001: 
 1002: # ------------------------------------------------------- Puts directory header
 1003: 
 1004: sub crumbs {
 1005:     my ($uri,$target,$prefix,$form,$size,$noformat,$skiplast)=@_;
 1006:     if (! defined($size)) {
 1007:         $size = '+2';
 1008:     }
 1009:     if ($target) {
 1010:         $target = ' target="'.
 1011:                   &Apache::loncommon::escape_single($target).'"';
 1012:     }
 1013:     my $output='';
 1014:     unless ($noformat) { $output.='<br /><tt><b>'; }
 1015:     $output.='<font size="'.$size.'">'.$prefix.'/';
 1016:     if ($env{'user.adv'}) {
 1017: 	my $path=$prefix.'/';
 1018: 	foreach my $dir (split('/',$uri)) {
 1019:             if (! $dir) { next; }
 1020:             $path .= $dir;
 1021: 	    if ($path eq $uri) {
 1022: 		if ($skiplast) {
 1023: 		    $output.=$dir;
 1024:                     last;
 1025: 		} 
 1026: 	    } else {
 1027: 		$path.='/'; 
 1028: 	    }	    
 1029:             my $href_path = &HTML::Entities::encode($path,'<>&"');
 1030: 	    &Apache::loncommon::inhibit_menu_check(\$href_path);
 1031: 	    if ($form) {
 1032: 	        my $href = 'javascript:'.$form.".action='".$href_path."';".$form.'.submit();';
 1033: 	        $output.=qq{<a href="$href" $target>$dir</a>/};
 1034: 	    } else {
 1035: 	        $output.=qq{<a href="$href_path" $target>$dir</a>/};
 1036: 	    }
 1037: 	}
 1038:     } else {
 1039: 	foreach my $dir (split('/',$uri)) {
 1040:             if (! $dir) { next; }
 1041: 	    $output.=$dir.'/';
 1042: 	}
 1043:     }
 1044:     if ($uri !~ m|/$|) { $output=~s|/$||; }
 1045:     return $output.'</font>'.($noformat?'':'</b></tt><br />');
 1046: }
 1047: 
 1048: # --------------------- A function that generates a window for the spellchecker
 1049: 
 1050: sub spellheader {
 1051:     my $start_page=
 1052: 	&Apache::loncommon::start_page('Speller Suggestions',undef,
 1053: 				       {'only_body'   => 1,
 1054: 					'js_ready'    => 1,
 1055: 					'bgcolor'     => '#DDDDDD',
 1056: 				        'add_entries' => {
 1057: 					    'onload' => 
 1058:                                                'document.forms.spellcheckform.submit()',
 1059:                                              }
 1060: 				        });
 1061:     my $end_page=
 1062: 	&Apache::loncommon::end_page({'js_ready'  => 1}); 
 1063: 
 1064:     my $nothing=&javascript_nothing();
 1065:     return (<<ENDCHECK);
 1066: <script type="text/javascript"> 
 1067: //<!-- BEGIN LON-CAPA Internal
 1068: var checkwin;
 1069: 
 1070: function spellcheckerwindow(string) {
 1071:     var esc_string = string.replace(/\"/g,'&quot;');
 1072:     checkwin=window.open($nothing,'spellcheckwin','height=320,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
 1073:     checkwin.document.writeln('$start_page<form name="spellcheckform" action="/adm/spellcheck" method="post"><input type="hidden" name="text" value="'+esc_string+'" /><\\/form>$end_page');
 1074:     checkwin.document.close();
 1075: }
 1076: // END LON-CAPA Internal -->
 1077: </script>
 1078: ENDCHECK
 1079: }
 1080: 
 1081: # ---------------------------------- Generate link to spell checker for a field
 1082: 
 1083: sub spelllink {
 1084:     my ($form,$field)=@_;
 1085:     my $linktext=&mt('Check Spelling');
 1086:     return (<<ENDLINK);
 1087: <a href="javascript:if (typeof(document.$form.onsubmit)!='undefined') { if (document.$form.onsubmit!=null) { document.$form.onsubmit();}};spellcheckerwindow(this.document.forms.$form.$field.value);">$linktext</a>
 1088: ENDLINK
 1089: }
 1090: 
 1091: # ------------------------------------------------- Output headers for HTMLArea
 1092: 
 1093: {
 1094:     my @htmlareafields;
 1095:     sub init_htmlareafields {
 1096: 	undef(@htmlareafields);
 1097:     }
 1098:     
 1099:     sub add_htmlareafields {
 1100: 	my (@newfields) = @_;
 1101: 	push(@htmlareafields,@newfields);
 1102:     }
 1103: 
 1104:     sub get_htmlareafields {
 1105: 	return @htmlareafields;
 1106:     }
 1107: }
 1108: 
 1109: sub htmlareaheaders {
 1110:     return if (&htmlareablocked());
 1111:     return if (!&htmlareabrowser());
 1112:     return (<<ENDHEADERS);
 1113: <script type="text/javascript" src="/fckeditor/fckeditor.js"></script>
 1114: ENDHEADERS
 1115: }
 1116: 
 1117: # ----------------------------------------------------------------- Preferences
 1118: 
 1119: sub disablelink {
 1120:     my @fields=@_;
 1121:     if (defined($#fields)) {
 1122: 	unless ($#fields>=0) { return ''; }
 1123:     }
 1124:     return '<a href="'.&HTML::Entities::encode('/adm/preferences?action=set_wysiwyg&wysiwyg=off&returnurl=','<>&"').&escape($ENV{'REQUEST_URI'}).'">'.&mt('Disable WYSIWYG Editor').'</a>';
 1125: }
 1126: 
 1127: sub enablelink {
 1128:     my @fields=@_;
 1129:     if (defined($#fields)) {
 1130: 	unless ($#fields>=0) { return ''; }
 1131:     }
 1132:     return '<a href="'.&HTML::Entities::encode('/adm/preferences?action=set_wysiwyg&wysiwyg=on&returnurl=','<>&"').&escape($ENV{'REQUEST_URI'}).'">'.&mt('Enable WYSIWYG Editor').'</a>';
 1133: }
 1134: 
 1135: # ------------------------------------------------- lang to use in html editor
 1136: sub htmlarea_lang {
 1137:     my $lang='en';
 1138:     if (&mt('htmlarea_lang') ne 'htmlarea_lang') {
 1139: 	$lang=&mt('htmlarea_lang');
 1140:     }
 1141:     return $lang;
 1142: }
 1143: 
 1144: # ----------------------------------------- Script to activate only some fields
 1145: 
 1146: sub htmlareaselectactive {
 1147:     my @fields=@_;
 1148:     unless (&htmlareabrowser()) { return ''; }
 1149:     if (&htmlareablocked()) { return '<br />'.&enablelink(@fields); }
 1150:     my $output='<script type="text/javascript" defer="1">';
 1151:     my $lang = &htmlarea_lang();
 1152:     foreach my $field (@fields) {
 1153: 	$output.="
 1154: {
 1155:     var oFCKeditor = new FCKeditor('$field');
 1156:     oFCKeditor.Config['CustomConfigurationsPath'] = 
 1157: 	'/fckeditor/loncapaconfig.js';    
 1158:     oFCKeditor.ReplaceTextarea();
 1159:     oFCKeditor.Config['AutoDetectLanguage'] = false;
 1160:     oFCKeditor.Config['DefaultLanguage'] = '$lang';
 1161: }";
 1162:     }
 1163:     $output.="\nwindow.status='Activated Editfields';\n</script><br />".
 1164: 	&disablelink(@fields);
 1165:     return $output;
 1166: }
 1167: 
 1168: # --------------------------------------------------------------------- Blocked
 1169: 
 1170: sub htmlareablocked {
 1171:     unless ($env{'environment.wysiwygeditor'} eq 'on') { return 1; }
 1172:     return 0;
 1173: }
 1174: 
 1175: # ---------------------------------------- Browser capable of running HTMLArea?
 1176: 
 1177: sub htmlareabrowser {
 1178:     return 1;
 1179: }
 1180: 
 1181: ############################################################
 1182: ############################################################
 1183: 
 1184: =pod
 1185: 
 1186: =item breadcrumbs
 1187: 
 1188: Compiles the previously registered breadcrumbs into an series of links.
 1189: FAQ and BUG links will be placed on the left side of the table if they
 1190: are defined for the last registered breadcrumb.  
 1191: Additionally supports a 'component', which will be displayed on the
 1192: right side of the table (without a link).
 1193: A link to help for the component will be included if one is specified.
 1194: 
 1195: All inputs can be undef without problems.
 1196: 
 1197: Inputs: $component (the large text on the right side of the table),
 1198:         $component_help
 1199:         $menulink (boolean, controls whether to include a link to /adm/menu)
 1200:         $helplink (if 'nohelp' don't include the orange help link)
 1201:         $css_class (optional name for the class to apply to the table for CSS)
 1202: Returns a string containing breadcrumbs for the current page.
 1203: 
 1204: =item clear_breadcrumbs
 1205: 
 1206: Clears the previously stored breadcrumbs.
 1207: 
 1208: =item add_breadcrumb
 1209: 
 1210: Pushes a breadcrumb on the stack of crumbs.
 1211: 
 1212: input: $breadcrumb, a hash reference.  The keys 'href','title', and 'text'
 1213: are required.  If present the keys 'faq' and 'bug' will be used to provide
 1214: links to the FAQ and bug sites. If the key 'no_mt' is present the 'title' 
 1215: and 'text' values won't be sent through &mt()
 1216: 
 1217: returns: nothing    
 1218: 
 1219: =cut
 1220: 
 1221: ############################################################
 1222: ############################################################
 1223: {
 1224:     my @Crumbs;
 1225:     
 1226:     sub breadcrumbs {
 1227:         my ($component,$component_help,$menulink,$helplink,$css_class) = @_;
 1228:         #
 1229: 	$css_class ||= 'LC_breadcrumbs';
 1230:         my $Str = "\n".'<table class="'.$css_class.'"><tr><td>';
 1231:         #
 1232:         # Make the faq and bug data cascade
 1233:         my $faq = '';
 1234:         my $bug = '';
 1235: 	my $help='';
 1236:         # The last breadcrumb does not have a link, so handle it separately.
 1237:         my $last = pop(@Crumbs);
 1238:         #
 1239:         # The first one should be the course or a menu link
 1240: 	if (!defined($menulink)) { $menulink=1; }
 1241:         if ($menulink) {
 1242:             my $description = 'Menu';
 1243:             my $no_mt_descr = 0;
 1244:             if (exists($env{'request.course.id'}) && 
 1245:                 $env{'request.course.id'} ne '') {
 1246:                 $description = 
 1247:                     $env{'course.'.$env{'request.course.id'}.'.description'};
 1248:                 $no_mt_descr = 1;
 1249:             }
 1250:             unshift(@Crumbs,{
 1251:                     href   =>'/adm/menu',
 1252:                     title  =>'Go to main menu',
 1253:                     target =>'_top',
 1254:                     text   =>$description,
 1255:                     no_mt  =>$no_mt_descr,
 1256:                 });
 1257:         }
 1258:         my $links .= 
 1259:             join('-&gt;',
 1260:                  map {
 1261:                      $faq = $_->{'faq'} if (exists($_->{'faq'}));
 1262:                      $bug = $_->{'bug'} if (exists($_->{'bug'}));
 1263:                      $help = $_->{'help'} if (exists($_->{'help'}));
 1264:                      my $result = '<a href="'.$_->{'href'}.'" ';
 1265:                      if (defined($_->{'target'}) && $_->{'target'} ne '') {
 1266:                          $result .= 'target="'.$_->{'target'}.'" ';
 1267:                      }
 1268: 		     if ($_->{'no_mt'}) {
 1269: 			 $result .='title="'.$_->{'title'}.'">'.
 1270: 			     $_->{'text'}.'</a>';
 1271: 		     } else {
 1272: 			 $result .='title="'.&mt($_->{'title'}).'">'.
 1273: 			     &mt($_->{'text'}).'</a>';
 1274: 		     }
 1275:                      $result;
 1276:                      } @Crumbs
 1277:                  );
 1278:         $links .= '-&gt;' if ($links ne '');
 1279: 	if ($last->{'no_mt'}) {
 1280: 	    $links .= '<b>'.$last->{'text'}.'</b>';
 1281: 	} else {
 1282: 	    $links .= '<b>'.&mt($last->{'text'}).'</b>';
 1283: 	}
 1284:         #
 1285:         my $icons = '';
 1286:         $faq = $last->{'faq'} if (exists($last->{'faq'}));
 1287:         $bug = $last->{'bug'} if (exists($last->{'bug'}));
 1288:         $help = $last->{'help'} if (exists($last->{'help'}));
 1289:         $component_help=($component_help?$component_help:$help);
 1290: #        if ($faq ne '') {
 1291: #            $icons .= &Apache::loncommon::help_open_faq($faq);
 1292: #        }
 1293: #        if ($bug ne '') {
 1294: #            $icons .= &Apache::loncommon::help_open_bug($bug);
 1295: #        }
 1296: 	if ($faq ne '' || $component_help ne '' || $bug ne '') {
 1297: 	    $icons .= &Apache::loncommon::help_open_menu($component,
 1298: 							 $component_help,
 1299: 							 $faq,$bug);
 1300: 	}
 1301:         #
 1302:         $Str .= $links.'</td>';
 1303:         #
 1304:         if (defined($component)) {
 1305:             $Str .= '<td class="'.$css_class.'_component">'.
 1306:                 &mt($component);
 1307: 	    if ($icons ne '') {
 1308: 		$Str .= '&nbsp;'.$icons;
 1309: 	    }
 1310: 	    $Str .= '</td>';
 1311:         }
 1312:         $Str .= '</tr></table>'."\n";
 1313:         #
 1314:         # Return the @Crumbs stack to what we started with
 1315:         push(@Crumbs,$last);
 1316:         shift(@Crumbs);
 1317:         #
 1318:         return $Str;
 1319:     }
 1320: 
 1321:     sub clear_breadcrumbs {
 1322:         undef(@Crumbs);
 1323:     }
 1324: 
 1325:     sub add_breadcrumb {
 1326:         push (@Crumbs,@_);
 1327:     }
 1328: 
 1329: } # End of scope for @Crumbs
 1330: 
 1331: ############################################################
 1332: ############################################################
 1333: 
 1334: # Nested table routines.
 1335: #
 1336: # Routines to display form items in a multi-row table with 2 columns.
 1337: # Uses nested tables to divide form elements into segments.
 1338: # For examples of use see loncom/interface/lonnotify.pm 
 1339: #
 1340: # Can be used in following order: ...
 1341: # &start_pick_box()
 1342: # row1
 1343: # row2
 1344: # row3   ... etc.
 1345: # &submit_row()
 1346: # &end_pick_box()
 1347: #
 1348: # where row1, row 2 etc. are chosen from &role_select_row,&course_select_row,
 1349: # &status_select_row and &email_default_row
 1350: #
 1351: # Can also be used in following order:
 1352: #
 1353: # &start_pick_box()
 1354: # &row_title()
 1355: # &row_closure()
 1356: # &row_title()
 1357: # &row_closure()  ... etc.
 1358: # &submit_row()
 1359: # &end_pick_box()
 1360: #
 1361: # In general a &submit_row() call should proceed the call to &end_pick_box(),
 1362: # as this routine adds a button for form submission.
 1363: # &submit_row() does not require a &row_closure after it.
 1364: #  
 1365: # &start_pick_box() creates a bounding table with 1-pixel wide black border.
 1366: # rows should be placed between calls to &start_pick_box() and &end_pick_box.
 1367: #
 1368: # &row_title() adds a title in the left column for each segment.
 1369: # &row_closure() closes a row with a 1-pixel wide black line.
 1370: #
 1371: # &role_select_row() provides a select box from which to choose 1 or more roles 
 1372: # &course_select_row provides ways of picking groups of courses
 1373: #    radio buttons: all, by category or by picking from a course picker pop-up
 1374: #      note: by category option is only displayed if a domain has implemented 
 1375: #                selection by year, semester, department, number etc.
 1376: #
 1377: # &status_select_row() provides a select box from which to choose 1 or more
 1378: #  access types (current access, prior access, and future access)  
 1379: #
 1380: # &email_default_row() provides text boxes for default e-mail suffixes for
 1381: #  different authentication types in a domain.
 1382: #
 1383: # &row_title() and &row_closure() are called internally by the &*_select_row
 1384: # routines, but can also be called directly to start and end rows which have 
 1385: # needs that are not accommodated by the *_select_row() routines.    
 1386: 
 1387: sub start_pick_box {
 1388:     my ($css_class) = @_;
 1389:     if (defined($css_class)) {
 1390: 	$css_class = 'class="'.$css_class.'"';
 1391:     } else {
 1392: 	$css_class= 'class="LC_pick_box"';
 1393:     }
 1394:     my $output = <<"END";
 1395:  <table $css_class>
 1396: END
 1397:     return $output;
 1398: }
 1399: 
 1400: sub end_pick_box {
 1401:     my $output = <<"END";
 1402:        </table>
 1403: END
 1404:     return $output;
 1405: }
 1406: 
 1407: sub row_title {
 1408:     my ($title,$css_title_class,$css_value_class) = @_;
 1409:     $css_title_class ||= 'LC_pick_box_title';
 1410:     $css_title_class = 'class="'.$css_title_class.'"';
 1411: 
 1412:     $css_value_class ||= 'LC_pick_box_value';
 1413:     $css_value_class = 'class="'.$css_value_class.'"';
 1414: 
 1415:     if ($title ne '') {
 1416:         $title .= ':';
 1417:     }
 1418:     my $output = <<"ENDONE";
 1419:            <tr class="LC_pick_box_row">
 1420:             <td $css_title_class>
 1421: 	       $title
 1422:             </td>
 1423:             <td $css_value_class>
 1424: ENDONE
 1425:     return $output;
 1426: }
 1427: 
 1428: sub row_closure {
 1429:     my ($no_separator) =@_;
 1430:     my $output = <<"ENDTWO";
 1431:             </td>
 1432:            </tr>
 1433: ENDTWO
 1434:     if (!$no_separator) {
 1435:         $output .= <<"ENDTWO";
 1436:            <tr>
 1437:             <td colspan="2" class="LC_pick_box_separator">
 1438:             </td>
 1439:            </tr>
 1440: ENDTWO
 1441:     }
 1442:     return $output;
 1443: }
 1444: 
 1445: sub role_select_row {
 1446:     my ($roles,$title,$css_class,$show_separate_custom,$cdom,$cnum) = @_;
 1447:     my $output;
 1448:     if (defined($title)) {
 1449:         $output = &row_title($title,$css_class);
 1450:     }
 1451:     $output .= qq|
 1452:                                   <select name="roles" multiple >\n|;
 1453:     foreach my $role (@$roles) {
 1454:         my $plrole;
 1455:         if ($role eq 'ow') {
 1456:             $plrole = &mt('Course Owner');
 1457:         } elsif ($role eq 'cr') {
 1458:             if ($show_separate_custom) {
 1459:                 if ($cdom ne '' && $cnum ne '') {
 1460:                     my %course_customroles = &course_custom_roles($cdom,$cnum);
 1461:                     foreach my $crrole (sort(keys(%course_customroles))) {
 1462:                         my ($plcrrole) = ($crrole =~ m|^cr/[^/]+/[^/]+/(.+)$|);
 1463:                         $output .= '  <option value="'.$crrole.'">'.$plcrrole.
 1464:                                    '</option>';
 1465:                     }
 1466:                 }
 1467:             } else {
 1468:                 $plrole = &mt('Custom Role');
 1469:             }
 1470:         } else {
 1471:             $plrole=&Apache::lonnet::plaintext($role);
 1472:         }
 1473:         if (($role ne 'cr') || (!$show_separate_custom)) {
 1474:             $output .= '  <option value="'.$role.'">'.$plrole.'</option>';
 1475:         }
 1476:     }
 1477:     $output .= qq|                </select>\n|;
 1478:     if (defined($title)) {
 1479:         $output .= &row_closure();
 1480:     }
 1481:     return $output;
 1482: }
 1483: 
 1484: sub course_select_row {
 1485:     my ($title,$formname,$totcodes,$codetitles,$idlist,$idlist_titles,
 1486: 	$css_class) = @_;
 1487:     my $output = &row_title($title,$css_class);
 1488:     $output .= &course_selection($formname,$totcodes,$codetitles,$idlist,$idlist_titles);
 1489:     $output .= &row_closure();
 1490:     return $output;
 1491: }
 1492: 
 1493: sub course_selection {
 1494:     my ($formname,$totcodes,$codetitles,$idlist,$idlist_titles) = @_;
 1495:     my $output = qq|
 1496: <script type="text/javascript">
 1497:     function coursePick (formname) {
 1498:         for  (var i=0; i<formname.coursepick.length; i++) {
 1499:             if (formname.coursepick[i].value == 'category') {
 1500:                 courseSet('');
 1501:             }
 1502:             if (!formname.coursepick[i].checked) {
 1503:                 if (formname.coursepick[i].value == 'specific') {
 1504:                     formname.coursetotal.value = 0;
 1505:                     formname.courselist = '';
 1506:                 }
 1507:             }
 1508:         }
 1509:     }
 1510:     function setPick (formname) {
 1511:         for  (var i=0; i<formname.coursepick.length; i++) {
 1512:             if (formname.coursepick[i].value == 'category') {
 1513:                 formname.coursepick[i].checked = true;
 1514:             }
 1515:             formname.coursetotal.value = 0;
 1516:             formname.courselist = '';
 1517:         }
 1518:     }
 1519: </script>
 1520:     |;
 1521:     my $courseform='<b>'.&Apache::loncommon::selectcourse_link
 1522:                      ($formname,'pickcourse','pickdomain','coursedesc','',1).'</b>';
 1523:         $output .= '<input type="radio" name="coursepick" value="all" onclick="coursePick(this.form)" />'.&mt('All courses').'<br />';
 1524:     if ($totcodes > 0) {
 1525:         my $numtitles = @$codetitles;
 1526:         if ($numtitles > 0) {
 1527:             $output .= '<input type="radio" name="coursepick" value="category" onclick="coursePick(this.form);alert('."'".&mt('Choose categories, from left to right')."'".')" />'.&mt('Pick courses by category:').' <br />';
 1528:             $output .= '<table><tr><td>'.$$codetitles[0].'<br />'."\n".
 1529:                '<select name="'.$$codetitles[0].
 1530:                '" onChange="setPick(this.form);courseSet('."'$$codetitles[0]'".')">'."\n".
 1531:                ' <option value="-1" />Select'."\n";
 1532:             my @items = ();
 1533:             my @longitems = ();
 1534:             if ($$idlist{$$codetitles[0]} =~ /","/) {
 1535:                 @items = split(/","/,$$idlist{$$codetitles[0]});
 1536:             } else {
 1537:                 $items[0] = $$idlist{$$codetitles[0]};
 1538:             }
 1539:             if (defined($$idlist_titles{$$codetitles[0]})) {
 1540:                 if ($$idlist_titles{$$codetitles[0]} =~ /","/) {
 1541:                     @longitems = split(/","/,$$idlist_titles{$$codetitles[0]});
 1542:                 } else {
 1543:                     $longitems[0] = $$idlist_titles{$$codetitles[0]};
 1544:                 }
 1545:                 for (my $i=0; $i<@longitems; $i++) {
 1546:                     if ($longitems[$i] eq '') {
 1547:                         $longitems[$i] = $items[$i];
 1548:                     }
 1549:                 }
 1550:             } else {
 1551:                 @longitems = @items;
 1552:             }
 1553:             for (my $i=0; $i<@items; $i++) {
 1554:                 $output .= ' <option value="'.$items[$i].'">'.$longitems[$i].'</option>';
 1555:             }
 1556:             $output .= '</select></td>';
 1557:             for (my $i=1; $i<$numtitles; $i++) {
 1558:                 $output .= '<td>'.$$codetitles[$i].'<br />'."\n".
 1559:                           '<select name="'.$$codetitles[$i].
 1560:                           '" onChange="courseSet('."'$$codetitles[$i]'".')">'."\n".
 1561:                           '<option value="-1">&lt;-Pick '.$$codetitles[$i-1].'</option>'."\n".
 1562:                           '</select>'."\n".
 1563:                           '</td>';
 1564:             }
 1565:             $output .= '</tr></table><br />';
 1566:         }
 1567:     }
 1568:     $output .= '<input type="radio" name="coursepick" value="specific" onclick="coursePick(this.form);opencrsbrowser('."'".$formname."','dccourse','dcdomain','coursedesc','','1'".')" />'.&mt('Pick specific course(s):').' '.$courseform.'&nbsp;&nbsp;<input type="text" value="0" size="4" name="coursetotal" /><input type="hidden" name="courselist" value="" />selected.<br />'."\n";
 1569:     return $output;
 1570: }
 1571: 
 1572: sub status_select_row {
 1573:     my ($types,$title,$css_class) = @_;
 1574:     my $output; 
 1575:     if (defined($title)) {
 1576:         $output = &row_title($title,$css_class,'LC_pick_box_select');
 1577:     }
 1578:     $output .= qq|
 1579:                                     <select name="types" multiple>\n|;
 1580:     foreach my $status_type (sort(keys(%{$types}))) {
 1581:         $output .= '  <option value="'.$status_type.'">'.$$types{$status_type}.'</option>';
 1582:     }
 1583:     $output .= qq|                   </select>\n|; 
 1584:     if (defined($title)) {
 1585:         $output .= &row_closure();
 1586:     }
 1587:     return $output;
 1588: }
 1589: 
 1590: sub email_default_row {
 1591:     my ($authtypes,$title,$descrip,$css_class) = @_;
 1592:     my $output = &row_title($title,$css_class);
 1593:     $output .= $descrip.
 1594: 	&Apache::loncommon::start_data_table().
 1595: 	&Apache::loncommon::start_data_table_header_row().
 1596: 	'<th>'.&mt('Authentication Method').'</th>'.
 1597: 	'<th align="right">'.&mt('Username -> e-mail conversion').'</th>'."\n".
 1598: 	&Apache::loncommon::end_data_table_header_row();
 1599:     my $rownum = 0;
 1600:     foreach my $auth (sort(keys(%{$authtypes}))) {
 1601:         my ($userentry,$size);
 1602:         if ($auth =~ /^krb/) {
 1603:             $userentry = '';
 1604:             $size = 25;
 1605:         } else {
 1606:             $userentry = 'username@';
 1607:             $size = 15;
 1608:         }
 1609:         $output .= &Apache::loncommon::start_data_table_row().
 1610: 	    '<td>  '.$$authtypes{$auth}.'</td>'.
 1611: 	    '<td align="right">'.$userentry.
 1612: 	    '<input type="text" name="'.$auth.'" size="'.$size.'" /></td>'.
 1613: 	    &Apache::loncommon::end_data_table_row();
 1614:     }
 1615:     $output .= &Apache::loncommon::end_data_table();
 1616:     $output .= &row_closure();
 1617:     return $output;
 1618: }
 1619: 
 1620: 
 1621: sub submit_row {
 1622:     my ($title,$cmd,$submit_text,$css_class) = @_;
 1623:     my $output = &row_title($title,$css_class,'LC_pick_box_submit');
 1624:     $output .= qq|
 1625:              <br />
 1626:              <input type="hidden" name="command" value="$cmd" />
 1627:              <input type="submit" value="$submit_text"/> &nbsp;
 1628:              <br /><br />
 1629:             \n|;
 1630:     return $output;
 1631: }
 1632: 
 1633: sub course_custom_roles {
 1634:     my ($cdom,$cnum) = @_;
 1635:     my %returnhash=();
 1636:     my %coursepersonnel=&Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
 1637:     foreach my $person (sort(keys(%coursepersonnel))) {
 1638:         my ($role) = ($person =~ /^([^:]+):/);
 1639:         my ($end,$start) = split(/:/,$coursepersonnel{$person});
 1640:         if ($end == -1 && $start == -1) {
 1641:             next;
 1642:         }
 1643:         if ($role =~ m|^cr/[^/]+/[^/]+/[^/]|) {
 1644:             $returnhash{$role} ++;
 1645:         }
 1646:     }
 1647:     return %returnhash;
 1648: }
 1649: 
 1650: 
 1651: ##############################################
 1652: ##############################################
 1653:                                                                              
 1654: # echo_form_input
 1655: #
 1656: # Generates html markup to add form elements from the referrer page
 1657: # as hidden form elements (values encoded) in the new page.
 1658: #
 1659: # Intended to support two types of use 
 1660: # (a) to allow backing up to earlier pages in a multi-page 
 1661: # form submission process using a breadcrumb trail.
 1662: #
 1663: # (b) to allow the current page to be reloaded with form elements
 1664: # set on previous page to remain unchanged.  An example would
 1665: # be where the a page containing a dynamically-built table of data is 
 1666: # is to be redisplayed, with only the sort order of the data changed. 
 1667: #  
 1668: # Inputs:
 1669: # 1. Reference to array of form elements in the submitted form on 
 1670: # the referrer page which are to be excluded from the echoed elements.
 1671: #
 1672: # 2. Reference to array of regular expressions, which if matched in the  
 1673: # name of the form element n the referrer page will be omitted from echo. 
 1674: #
 1675: # Outputs: A scalar containing the html markup for the echoed form
 1676: # elements (all as hidden elements, with values encoded). 
 1677: 
 1678: 
 1679: sub echo_form_input {
 1680:     my ($excluded,$regexps) = @_;
 1681:     my $output = '';
 1682:     foreach my $key (keys(%env)) {
 1683:         if ($key =~ /^form\.(.+)$/) {
 1684:             my $name = $1;
 1685:             my $match = 0;
 1686:             if ((!@{$excluded}) || (!grep/^$name$/,@{$excluded})) {
 1687:                 if (defined($regexps)) {
 1688:                     if (@{$regexps} > 0) {
 1689:                         foreach my $regexp (@{$regexps}) {
 1690:                             if ($name =~ /\Q$regexp\E/) {
 1691:                                 $match = 1;
 1692:                                 last;
 1693:                             }
 1694:                         }
 1695:                     }
 1696:                 }
 1697:                 if (!$match) {
 1698:                     if (ref($env{$key})) {
 1699:                         foreach my $value (@{$env{$key}}) {
 1700:                             $value = &HTML::Entities::encode($value,'<>&"');
 1701:                             $output .= '<input type="hidden" name="'.$name.
 1702:                                              '" value="'.$value.'" />'."\n";
 1703:                         }
 1704:                     } else {
 1705:                         my $value = &HTML::Entities::encode($env{$key},'<>&"');
 1706:                         $output .= '<input type="hidden" name="'.$name.
 1707:                                              '" value="'.$value.'" />'."\n";
 1708:                     }
 1709:                 }
 1710:             }
 1711:         }
 1712:     }
 1713:     return $output;
 1714: }
 1715: 
 1716: ##############################################
 1717: ##############################################
 1718:                                                                              
 1719: # set_form_elements
 1720: #
 1721: # Generates javascript to set form elements to values based on
 1722: # corresponding values for the same form elements when the page was
 1723: # previously submitted.
 1724: #     
 1725: # Last submission values are read from hidden form elements in referring 
 1726: # page which have the same name, i.e., generated by &echo_form_input(). 
 1727: #
 1728: # Intended to be called by onload event.
 1729: #
 1730: # Inputs:
 1731: # (a) Reference to hash of echoed form elements to be set.
 1732: #
 1733: # In the hash, keys are the form element names, and the values are the
 1734: # element type (selectbox, radio, checkbox or text -for textbox, textarea or
 1735: # hidden).
 1736: #
 1737: # (b) Optional reference to hash of stored elements to be set.
 1738: #
 1739: # If the page being displayed is a page which permits modification of
 1740: # previously stored data, e.g., the first page in a multi-page submission,
 1741: # then if stored is supplied, form elements will be set to the last stored
 1742: # values.  If user supplied values are also available for the same elements
 1743: # these will replace the stored values. 
 1744: #        
 1745: # Output:
 1746: #  
 1747: # javascript function - set_form_elements() which sets form elements,
 1748: # expects an argument: formname - the name of the form according to 
 1749: # the DOM, e.g., document.compose
 1750: 
 1751: sub set_form_elements {
 1752:     my ($elements,$stored) = @_;
 1753:     my %values;
 1754:     my $output .= 'function setFormElements(courseForm) {
 1755: ';
 1756:     if (defined($stored)) {
 1757:         foreach my $name (keys(%{$stored})) {
 1758:             if (exists($$elements{$name})) {
 1759:                 if (ref($$stored{$name}) eq 'ARRAY') {
 1760:                     $values{$name} = $$stored{$name};
 1761:                 } else {
 1762:                     @{$values{$name}} = ($$stored{$name});
 1763:                 }
 1764:             }
 1765:         }
 1766:     }
 1767: 
 1768:     foreach my $key (keys(%env)) {
 1769:         if ($key =~ /^form\.(.+)$/) {
 1770:             my $name = $1;
 1771:             if (exists($$elements{$name})) {
 1772:                 @{$values{$name}} = &Apache::loncommon::get_env_multiple($key);
 1773:             }
 1774:         }
 1775:     }
 1776: 
 1777:     foreach my $name (keys(%values)) {
 1778:         for (my $i=0; $i<@{$values{$name}}; $i++) {
 1779:             $values{$name}[$i] = &HTML::Entities::decode($values{$name}[$i],'<>&"');
 1780:             $values{$name}[$i] =~ s/([\r\n\f]+)/\\n/g;
 1781:             $values{$name}[$i] =~ s/"/\\"/g;
 1782:         }
 1783:         if ($$elements{$name} eq 'text') {
 1784:             my $numvalues = @{$values{$name}};
 1785:             if ($numvalues > 1) {
 1786:                 my $valuestring = join('","',@{$values{$name}});
 1787:                 $output .= qq|
 1788:   var textvalues = new Array ("$valuestring");
 1789:   var total = courseForm.elements['$name'].length;
 1790:   if (total > $numvalues) {
 1791:       total = $numvalues;
 1792:   }    
 1793:   for (var i=0; i<total; i++) {
 1794:       courseForm.elements['$name']\[i].value = textvalues[i];
 1795:   }
 1796: |;
 1797:             } else {
 1798:                 $output .= qq|
 1799:   courseForm.elements['$name'].value = "$values{$name}[0]";
 1800: |;
 1801:             }
 1802:         } else {
 1803:             $output .=  qq|
 1804:   var elementLength = courseForm.elements['$name'].length;
 1805:   if (elementLength==undefined) {
 1806: |;
 1807:             foreach my $value (@{$values{$name}}) {
 1808:                 if ($$elements{$name} eq 'selectbox') {
 1809:                     $output .=  qq|
 1810:       if (courseForm.elements['$name'].options[0].value == "$value") {
 1811:           courseForm.elements['$name'].options[0].selected = true;
 1812:       }|;
 1813:                 } elsif (($$elements{$name} eq 'radio') ||
 1814:                          ($$elements{$name} eq 'checkbox')) {
 1815:                     $output .= qq|
 1816:       if (courseForm.elements['$name'].value == "$value") {
 1817:           courseForm.elements['$name'].checked = true;
 1818:       }|;
 1819:                 }
 1820:             }
 1821:             $output .= qq|
 1822:   }
 1823:   else {
 1824:       for (var i=0; i<courseForm.elements['$name'].length; i++) {
 1825: |;
 1826:             if ($$elements{$name} eq 'selectbox') {
 1827:                 $output .=  qq|
 1828:           courseForm.elements['$name'].options[i].selected = false;|;
 1829:             } elsif (($$elements{$name} eq 'radio') || 
 1830:                      ($$elements{$name} eq 'checkbox')) {
 1831:                 $output .= qq|
 1832:           courseForm.elements['$name']\[i].checked = false;|; 
 1833:             }
 1834:             $output .= qq|
 1835:       }
 1836:       for (var j=0; j<courseForm.elements['$name'].length; j++) {
 1837: |;
 1838:             foreach my $value (@{$values{$name}}) {
 1839:                 if ($$elements{$name} eq 'selectbox') {
 1840:                     $output .=  qq|
 1841:           if (courseForm.elements['$name'].options[j].value == "$value") {
 1842:               courseForm.elements['$name'].options[j].selected = true;
 1843:           }|;
 1844:                 } elsif (($$elements{$name} eq 'radio') ||
 1845:                          ($$elements{$name} eq 'checkbox')) { 
 1846:                       $output .= qq|
 1847:           if (courseForm.elements['$name']\[j].value == "$value") {
 1848:               courseForm.elements['$name']\[j].checked = true;
 1849:           }|;
 1850:                 }
 1851:             }
 1852:             $output .= qq|
 1853:       }
 1854:   }
 1855: |;
 1856:         }
 1857:     }
 1858:     $output .= "
 1859: }\n";
 1860:     return $output;
 1861: }
 1862: 
 1863: ##############################################
 1864: ##############################################
 1865: 
 1866: # javascript_valid_email
 1867: #
 1868: # Generates javascript to validate an e-mail address.
 1869: # Returns a javascript function which accetps a form field as argumnent, and
 1870: # returns false if field.value does not satisfy two regular expression matches
 1871: # for a valid e-mail address.  Backwards compatible with old browsers without
 1872: # support for javascript RegExp (just checks for @ in field.value in this case). 
 1873: 
 1874: sub javascript_valid_email {
 1875:     my $scripttag .= <<'END';
 1876: function validmail(field) {
 1877:     var str = field.value;
 1878:     if (window.RegExp) {
 1879:         var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
 1880:         var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"; //"
 1881:         var reg1 = new RegExp(reg1str);
 1882:         var reg2 = new RegExp(reg2str);
 1883:         if (!reg1.test(str) && reg2.test(str)) {
 1884:             return true;
 1885:         }
 1886:         return false;
 1887:     }
 1888:     else
 1889:     {
 1890:         if(str.indexOf("@") >= 0) {
 1891:             return true;
 1892:         }
 1893:         return false;
 1894:     }
 1895: }
 1896: END
 1897:     return $scripttag;
 1898: }
 1899: 
 1900: 1;
 1901: 
 1902: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>