File:  [LON-CAPA] / loncom / interface / lonhtmlcommon.pm
Revision 1.161: download - view: text, annotated - select for diffs
Wed Sep 5 05:00:24 2007 UTC (16 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_5_X, version_2_5_2, HEAD
- correct documentation
- remove unused array @rowcols

    1: # The LearningOnline Network with CAPA
    2: # a pile of common html routines
    3: #
    4: # $Id: lonhtmlcommon.pm,v 1.161 2007/09/05 05:00:24 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:     if ($no_hh_mm_ss) {
  467:         $result .= &mt('[_1] [_2] [_3] ',
  468:                        $monthselector,$dayselector,$yearselector);
  469:         if (!$nolink) {
  470:             $result .= &mt('[_1]Select Date[_2]',$cal_link,'</a>');
  471:         }
  472:     } else {
  473:         $result .= &mt('[_1] [_2] [_3] [_4] [_5]m [_6]s ',
  474:                       $monthselector,$dayselector,$yearselector,
  475:                       $hourselector,$minuteselector,$secondselector);
  476:         if (!$nolink) {
  477:             $result .= &mt('[_1]Select Date[_2]',$cal_link,'</a>');
  478:         }
  479:     }
  480:     $result .= "</span>\n<!-- end $dname date setting form -->\n";
  481:     return $result;
  482: }
  483: 
  484: ##############################################
  485: ##############################################
  486: 
  487: =pod
  488: 
  489: =item &get_date_from_form
  490: 
  491: get_date_from_form retrieves the date specified in an &date_setter form.
  492: 
  493: Inputs:
  494: 
  495: =over 4
  496: 
  497: =item $dname
  498: 
  499: The name passed to &datesetter, which prefixes the form elements.
  500: 
  501: =item $defaulttime
  502: 
  503: The unix time to use as the default in case of poor inputs.
  504: 
  505: =back
  506: 
  507: Returns: Unix time represented in the form.
  508: 
  509: =cut
  510: 
  511: ##############################################
  512: ##############################################
  513: sub get_date_from_form {
  514:     my ($dname) = @_;
  515:     my ($sec,$min,$hour,$day,$month,$year);
  516:     #
  517:     if (defined($env{'form.'.$dname.'_second'})) {
  518:         my $tmpsec = $env{'form.'.$dname.'_second'};
  519:         if (($tmpsec =~ /^\d+$/) && ($tmpsec >= 0) && ($tmpsec < 60)) {
  520:             $sec = $tmpsec;
  521:         }
  522: 	if (!defined($tmpsec) || $tmpsec eq '') { $sec = 0; }
  523:     } else {
  524:         $sec = 0;
  525:     }
  526:     if (defined($env{'form.'.$dname.'_minute'})) {
  527:         my $tmpmin = $env{'form.'.$dname.'_minute'};
  528:         if (($tmpmin =~ /^\d+$/) && ($tmpmin >= 0) && ($tmpmin < 60)) {
  529:             $min = $tmpmin;
  530:         }
  531: 	if (!defined($tmpmin) || $tmpmin eq '') { $min = 0; }
  532:     } else {
  533:         $min = 0;
  534:     }
  535:     if (defined($env{'form.'.$dname.'_hour'})) {
  536:         my $tmphour = $env{'form.'.$dname.'_hour'};
  537:         if (($tmphour =~ /^\d+$/) && ($tmphour >= 0) && ($tmphour < 24)) {
  538:             $hour = $tmphour;
  539:         }
  540:     } else {
  541:         $hour = 0;
  542:     }
  543:     if (defined($env{'form.'.$dname.'_day'})) {
  544:         my $tmpday = $env{'form.'.$dname.'_day'};
  545:         if (($tmpday =~ /^\d+$/) && ($tmpday > 0) && ($tmpday < 32)) {
  546:             $day = $tmpday;
  547:         }
  548:     }
  549:     if (defined($env{'form.'.$dname.'_month'})) {
  550:         my $tmpmonth = $env{'form.'.$dname.'_month'};
  551:         if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
  552:             $month = $tmpmonth - 1;
  553:         }
  554:     }
  555:     if (defined($env{'form.'.$dname.'_year'})) {
  556:         my $tmpyear = $env{'form.'.$dname.'_year'};
  557:         if (($tmpyear =~ /^\d+$/) && ($tmpyear > 1900)) {
  558:             $year = $tmpyear - 1900;
  559:         }
  560:     }
  561:     if (($year<70) || ($year>137)) { return undef; }
  562:     if (defined($sec) && defined($min)   && defined($hour) &&
  563:         defined($day) && defined($month) && defined($year) &&
  564:         eval('&timelocal($sec,$min,$hour,$day,$month,$year)')) {
  565:         return &timelocal($sec,$min,$hour,$day,$month,$year);
  566:     } else {
  567:         return undef;
  568:     }
  569: }
  570: 
  571: ##############################################
  572: ##############################################
  573: 
  574: =pod
  575: 
  576: =item &pjump_javascript_definition()
  577: 
  578: Returns javascript defining the 'pjump' function, which opens up a
  579: parameter setting wizard.
  580: 
  581: =cut
  582: 
  583: ##############################################
  584: ##############################################
  585: sub pjump_javascript_definition {
  586:     my $Str = <<END;
  587:     function pjump(type,dis,value,marker,ret,call,hour,min,sec) {
  588:         parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
  589:                  +"&value="+escape(value)+"&marker="+escape(marker)
  590:                  +"&return="+escape(ret)
  591:                  +"&call="+escape(call)+"&name="+escape(dis)
  592:                  +"&defhour="+escape(hour)+"&defmin="+escape(min)
  593:                  +"&defsec="+escape(sec),"LONCAPAparms",
  594:                  "height=350,width=350,scrollbars=no,menubar=no");
  595:     }
  596: END
  597:     return $Str;
  598: }
  599: 
  600: ##############################################
  601: ##############################################
  602: 
  603: =pod
  604: 
  605: =item &javascript_nothing()
  606: 
  607: Return an appropriate null for the users browser.  This is used
  608: as the first arguement for window.open calls when you want a blank
  609: window that you can then write to.
  610: 
  611: =cut
  612: 
  613: ##############################################
  614: ##############################################
  615: sub javascript_nothing {
  616:     # mozilla and other browsers work with "''", but IE on mac does not.
  617:     my $nothing = "''";
  618:     my $user_browser;
  619:     my $user_os;
  620:     $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
  621:     $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
  622:     if (! defined($user_browser) || ! defined($user_os)) {
  623:         (undef,$user_browser,undef,undef,undef,$user_os) = 
  624:                            &Apache::loncommon::decode_user_agent();
  625:     }
  626:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
  627:         $nothing = "'javascript:void(0);'";
  628:     }
  629:     return $nothing;
  630: }
  631: 
  632: ##############################################
  633: ##############################################
  634: sub javascript_docopen {
  635:     # safari does not understand document.open() and loads "text/html"
  636:     my $nothing = "''";
  637:     my $user_browser;
  638:     my $user_os;
  639:     $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
  640:     $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
  641:     if (! defined($user_browser) || ! defined($user_os)) {
  642:         (undef,$user_browser,undef,undef,undef,$user_os) = 
  643:                            &Apache::loncommon::decode_user_agent();
  644:     }
  645:     if ($user_browser eq 'safari' && $user_os =~ 'mac') {
  646:         $nothing = "document.clear()";
  647:     } else {
  648: 	$nothing = "document.open('text/html','replace')";
  649:     }
  650:     return $nothing;
  651: }
  652: 
  653: 
  654: ##############################################
  655: ##############################################
  656: 
  657: =pod
  658: 
  659: =item &StatusOptions()
  660: 
  661: Returns html for a selection box which allows the user to choose the
  662: enrollment status of students.  The selection box name is 'Status'.
  663: 
  664: Inputs:
  665: 
  666: $status: the currently selected status.  If undefined the value of
  667: $env{'form.Status'} is taken.  If that is undefined, a value of 'Active'
  668: is used.
  669: 
  670: $formname: The name of the form.  If defined the onchange attribute of
  671: the selection box is set to document.$formname.submit().
  672: 
  673: $size: the size (number of lines) of the selection box.
  674: 
  675: $onchange: javascript to use when the value is changed.  Enclosed in 
  676: double quotes, ""s, not single quotes.
  677: 
  678: Returns: a perl string as described.
  679: 
  680: =cut
  681: 
  682: ##############################################
  683: ##############################################
  684: sub StatusOptions {
  685:     my ($status, $formName,$size,$onchange)=@_;
  686:     $size = 1 if (!defined($size));
  687:     if (! defined($status)) {
  688:         $status = 'Active';
  689:         $status = $env{'form.Status'} if (exists($env{'form.Status'}));
  690:     }
  691: 
  692:     my $Str = '';
  693:     $Str .= '<select name="Status"';
  694:     if(defined($formName) && $formName ne '' && ! defined($onchange)) {
  695:         $Str .= ' onchange="document.'.$formName.'.submit()"';
  696:     }
  697:     if (defined($onchange)) {
  698:         $Str .= ' onchange="'.$onchange.'"';
  699:     }
  700:     $Str .= ' size="'.$size.'" ';
  701:     $Str .= '>'."\n";
  702:     foreach my $type (['Active',  &mt('Currently Has Access')],
  703: 		      ['Future',  &mt('Will Have Future Access')],
  704: 		      ['Expired', &mt('Previously Had Access')],
  705: 		      ['Any',     &mt('Any Access Status')]) {
  706: 	my ($name,$label) = @$type;
  707: 	$Str .= '<option value="'.$name.'" ';
  708: 	if ($status eq $name) {
  709: 	    $Str .= 'selected="selected" ';
  710: 	}
  711: 	$Str .= '>'.$label.'</option>'."\n";
  712:     }
  713: 
  714:     $Str .= '</select>'."\n";
  715: }
  716: 
  717: ########################################################
  718: ########################################################
  719: 
  720: =pod
  721: 
  722: =item Progess Window Handling Routines
  723: 
  724: These routines handle the creation, update, increment, and closure of 
  725: progress windows.  The progress window reports to the user the number
  726: of items completed and an estimate of the time required to complete the rest.
  727: 
  728: =over 4
  729: 
  730: 
  731: =item &Create_PrgWin
  732: 
  733: Writes javascript to the client to open a progress window and returns a
  734: data structure used for bookkeeping.
  735: 
  736: Inputs
  737: 
  738: =over 4
  739: 
  740: =item $r Apache request
  741: 
  742: =item $title The title of the progress window
  743: 
  744: =item $heading A description (usually 1 line) of the process being initiated.
  745: 
  746: =item $number_to_do The total number of items being processed.
  747: 
  748: =item $type Either 'popup' or 'inline' (popup is assumed if nothing is
  749:        specified)
  750: 
  751: =item $width Specify the width in charaters of the input field.
  752: 
  753: =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
  754: 
  755: =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 
  756: 
  757: =back
  758: 
  759: Returns a hash containing the progress state data structure.
  760: 
  761: 
  762: =item &Update_PrgWin
  763: 
  764: Updates the text in the progress indicator.  Does not increment the count.
  765: See &Increment_PrgWin.
  766: 
  767: Inputs:
  768: 
  769: =over 4
  770: 
  771: =item $r Apache request
  772: 
  773: =item $prog_state Pointer to the data structure returned by &Create_PrgWin
  774: 
  775: =item $displaystring The string to write to the status indicator
  776: 
  777: =back
  778: 
  779: Returns: none
  780: 
  781: 
  782: =item Increment_PrgWin
  783: 
  784: Increment the count of items completed for the progress window by 1.  
  785: 
  786: Inputs:
  787: 
  788: =over 4
  789: 
  790: =item $r Apache request
  791: 
  792: =item $prog_state Pointer to the data structure returned by Create_PrgWin
  793: 
  794: =item $extraInfo A description of the items being iterated over.  Typically
  795: 'student'.
  796: 
  797: =back
  798: 
  799: Returns: none
  800: 
  801: 
  802: =item Close_PrgWin
  803: 
  804: Closes the progress window.
  805: 
  806: Inputs:
  807: 
  808: =over 4 
  809: 
  810: =item $r Apache request
  811: 
  812: =item $prog_state Pointer to the data structure returned by Create_PrgWin
  813: 
  814: =back
  815: 
  816: Returns: none
  817: 
  818: =back
  819: 
  820: =cut
  821: 
  822: ########################################################
  823: ########################################################
  824: 
  825: my $uniq=0;
  826: sub get_uniq_name {
  827:     $uniq++;
  828:     return 'uniquename'.$uniq;
  829: }
  830: 
  831: # Create progress
  832: sub Create_PrgWin {
  833:     my ($r, $title, $heading, $number_to_do,$type,$width,$formname,
  834: 	$inputname)=@_;
  835:     if (!defined($type)) { $type='popup'; }
  836:     if (!defined($width)) { $width=55; }
  837:     my %prog_state;
  838:     $prog_state{'type'}=$type;
  839:     if ($type eq 'popup') {
  840: 	$prog_state{'window'}='popwin';
  841: 	my $start_page =
  842: 	    &Apache::loncommon::start_page($title,undef,
  843: 					   {'only_body' => 1,
  844: 					    'bgcolor'   => '#88DDFF',
  845: 					    'js_ready'  => 1});
  846: 	my $end_page = &Apache::loncommon::end_page({'js_ready'  => 1});
  847: 
  848: 	#the whole function called through timeout is due to issues
  849: 	#in mozilla Read BUG #2665 if you want to know the whole story
  850: 	&r_print($r,'<script type="text/javascript">'.
  851:         "var popwin;
  852:          function openpopwin () {
  853:          popwin=open(\'\',\'popwin\',\'width=400,height=100\');".
  854:         "popwin.document.writeln(\'".$start_page.
  855:               "<h4>$heading<\/h4>".
  856:               "<form action= \"\" name=\"popremain\" method=\"post\">".
  857:               '<input type="text" size="'.$width.'" name="remaining" value="'.
  858: 	      &mt('Starting').'" /><\\/form>'.$end_page.
  859:               "\');".
  860:         "popwin.document.close();}".
  861:         "\nwindow.setTimeout(openpopwin,0)</script>");
  862: 	$prog_state{'formname'}='popremain';
  863: 	$prog_state{'inputname'}="remaining";
  864:     } elsif ($type eq 'inline') {
  865: 	$prog_state{'window'}='window';
  866: 	if (!$formname) {
  867: 	    $prog_state{'formname'}=&get_uniq_name();
  868: 	    &r_print($r,'<form action="" name="'.$prog_state{'formname'}.'">');
  869: 	} else {
  870: 	    $prog_state{'formname'}=$formname;
  871: 	}
  872: 	if (!$inputname) {
  873: 	    $prog_state{'inputname'}=&get_uniq_name();
  874: 	    &r_print($r,$heading.' <input type="text" name="'.$prog_state{'inputname'}.
  875: 		     '" size="'.$width.'" />');
  876: 	} else {
  877: 	    $prog_state{'inputname'}=$inputname;
  878: 	    
  879: 	}
  880: 	if (!$formname) { &r_print($r,'</form>'); }
  881: 	&Update_PrgWin($r,\%prog_state,&mt('Starting'));
  882:     }
  883: 
  884:     $prog_state{'done'}=0;
  885:     $prog_state{'firststart'}=&Time::HiRes::time();
  886:     $prog_state{'laststart'}=&Time::HiRes::time();
  887:     $prog_state{'max'}=$number_to_do;
  888:     
  889:     return %prog_state;
  890: }
  891: 
  892: # update progress
  893: sub Update_PrgWin {
  894:     my ($r,$prog_state,$displayString)=@_;
  895:     &r_print($r,'<script type="text/javascript">'.$$prog_state{'window'}.'.document.'.
  896: 	     $$prog_state{'formname'}.'.'.
  897: 	     $$prog_state{'inputname'}.'.value="'.
  898: 	     $displayString.'";</script>');
  899:     $$prog_state{'laststart'}=&Time::HiRes::time();
  900: }
  901: 
  902: # increment progress state
  903: sub Increment_PrgWin {
  904:     my ($r,$prog_state,$extraInfo)=@_;
  905:     $$prog_state{'done'}++;
  906:     my $time_est= (&Time::HiRes::time() - $$prog_state{'firststart'})/
  907:         $$prog_state{'done'} *
  908: 	($$prog_state{'max'}-$$prog_state{'done'});
  909:     $time_est = int($time_est);
  910:     #
  911:     my $min = int($time_est/60);
  912:     my $sec = $time_est % 60;
  913:     # 
  914:     my $str;
  915:     if ($min == 0 && $sec > 1) {
  916:         $str = '[_2] seconds';
  917:     } elsif ($min == 1 && $sec > 1) {
  918:         $str = '1 minute [_2] seconds';
  919:     } elsif ($min == 1 && $sec < 2) {
  920:         $str = '1 minute';
  921:     } elsif ($min < 10 && $sec > 1) {
  922:         $str = '[_1] minutes, [_2] seconds';
  923:     } elsif ($min >= 10 || $sec < 2) {
  924:         $str = '[_1] minutes';
  925:     }
  926:     $time_est = &mt($str,$min,$sec);
  927:     #
  928:     my $lasttime = &Time::HiRes::time()-$$prog_state{'laststart'};
  929:     if ($lasttime > 9) {
  930:         $lasttime = int($lasttime);
  931:     } elsif ($lasttime < 0.01) {
  932:         $lasttime = 0;
  933:     } else {
  934:         $lasttime = sprintf("%3.2f",$lasttime);
  935:     }
  936:     if ($lasttime == 1) {
  937:         $lasttime = '('.$lasttime.' '.&mt('second for').' '.$extraInfo.')';
  938:     } else {
  939:         $lasttime = '('.$lasttime.' '.&mt('seconds for').' '.$extraInfo.')';
  940:     }
  941:     #
  942:     my $user_browser = $env{'browser.type'} if (exists($env{'browser.type'}));
  943:     my $user_os      = $env{'browser.os'}   if (exists($env{'browser.os'}));
  944:     if (! defined($user_browser) || ! defined($user_os)) {
  945:         (undef,$user_browser,undef,undef,undef,$user_os) = 
  946:                            &Apache::loncommon::decode_user_agent();
  947:     }
  948:     if ($user_browser eq 'explorer' && $user_os =~ 'mac') {
  949:         $lasttime = '';
  950:     }
  951:     &r_print($r,'<script>'.$$prog_state{'window'}.'.document.'.
  952: 	     $$prog_state{'formname'}.'.'.
  953: 	     $$prog_state{'inputname'}.'.value="'.
  954: 	     $$prog_state{'done'}.'/'.$$prog_state{'max'}.
  955: 	     ': '.$time_est.' '.&mt('remaining').' '.$lasttime.'";'.'</script>');
  956:     $$prog_state{'laststart'}=&Time::HiRes::time();
  957: }
  958: 
  959: # close Progress Line
  960: sub Close_PrgWin {
  961:     my ($r,$prog_state)=@_;
  962:     if ($$prog_state{'type'} eq 'popup') {
  963: 	&r_print($r,'<script>popwin.close()</script>'."\n");
  964:     } elsif ($$prog_state{'type'} eq 'inline') {
  965: 	&Update_PrgWin($r,$prog_state,&mt('Done'));
  966:     }
  967:     undef(%$prog_state);
  968: }
  969: 
  970: sub r_print {
  971:     my ($r,$to_print)=@_;
  972:     if ($r) {
  973: 	$r->print($to_print);
  974: 	$r->rflush();
  975:     } else {
  976: 	print($to_print);
  977:     }
  978: }
  979: 
  980: # ------------------------------------------------------- Puts directory header
  981: 
  982: sub crumbs {
  983:     my ($uri,$target,$prefix,$form,$size,$noformat,$skiplast)=@_;
  984:     if (! defined($size)) {
  985:         $size = '+2';
  986:     }
  987:     if ($target) {
  988:         $target = ' target="'.
  989:                   &Apache::loncommon::escape_single($target).'"';
  990:     }
  991:     my $output='';
  992:     unless ($noformat) { $output.='<br /><tt><b>'; }
  993:     $output.='<font size="'.$size.'">'.$prefix.'/';
  994:     if ($env{'user.adv'}) {
  995: 	my $path=$prefix.'/';
  996: 	foreach my $dir (split('/',$uri)) {
  997:             if (! $dir) { next; }
  998:             $path .= $dir;
  999: 	    if ($path eq $uri) {
 1000: 		if ($skiplast) {
 1001: 		    $output.=$dir;
 1002:                     last;
 1003: 		} 
 1004: 	    } else {
 1005: 		$path.='/'; 
 1006: 	    }	    
 1007:             my $href_path = &HTML::Entities::encode($path,'<>&"');
 1008: 	    &Apache::loncommon::inhibit_menu_check(\$href_path);
 1009: 	    $output.=qq{<a href="$href_path" $target>$dir</a>/};
 1010: 	}
 1011:     } else {
 1012: 	foreach my $dir (split('/',$uri)) {
 1013:             if (! $dir) { next; }
 1014: 	    $output.=$dir.'/';
 1015: 	}
 1016:     }
 1017:     if ($uri !~ m|/$|) { $output=~s|/$||; }
 1018:     return $output.'</font>'.($noformat?'':'</b></tt><br />');
 1019: }
 1020: 
 1021: # --------------------- A function that generates a window for the spellchecker
 1022: 
 1023: sub spellheader {
 1024:     my $start_page=
 1025: 	&Apache::loncommon::start_page('Speller Suggestions',undef,
 1026: 				       {'only_body'   => 1,
 1027: 					'js_ready'    => 1,
 1028: 					'bgcolor'     => '#DDDDDD',
 1029: 				        'add_entries' => {
 1030: 					    'onload' => 
 1031:                                                'document.forms.spellcheckform.submit()',
 1032:                                              }
 1033: 				        });
 1034:     my $end_page=
 1035: 	&Apache::loncommon::end_page({'js_ready'  => 1}); 
 1036: 
 1037:     my $nothing=&javascript_nothing();
 1038:     return (<<ENDCHECK);
 1039: <script type="text/javascript"> 
 1040: //<!-- BEGIN LON-CAPA Internal
 1041: var checkwin;
 1042: 
 1043: function spellcheckerwindow(string) {
 1044:     var esc_string = string.replace(/\"/g,'&quot;');
 1045:     checkwin=window.open($nothing,'spellcheckwin','height=320,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
 1046:     checkwin.document.writeln('$start_page<form name="spellcheckform" action="/adm/spellcheck" method="post"><input type="hidden" name="text" value="'+esc_string+'" /><\\/form>$end_page');
 1047:     checkwin.document.close();
 1048: }
 1049: // END LON-CAPA Internal -->
 1050: </script>
 1051: ENDCHECK
 1052: }
 1053: 
 1054: # ---------------------------------- Generate link to spell checker for a field
 1055: 
 1056: sub spelllink {
 1057:     my ($form,$field)=@_;
 1058:     my $linktext=&mt('Check Spelling');
 1059:     return (<<ENDLINK);
 1060: <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>
 1061: ENDLINK
 1062: }
 1063: 
 1064: # ------------------------------------------------- Output headers for HTMLArea
 1065: 
 1066: {
 1067:     my @htmlareafields;
 1068:     sub init_htmlareafields {
 1069: 	undef(@htmlareafields);
 1070:     }
 1071:     
 1072:     sub add_htmlareafields {
 1073: 	my (@newfields) = @_;
 1074: 	push(@htmlareafields,@newfields);
 1075:     }
 1076: 
 1077:     sub get_htmlareafields {
 1078: 	return @htmlareafields;
 1079:     }
 1080: }
 1081: 
 1082: sub htmlareaheaders {
 1083:     if (&htmlareablocked()) { return ''; }
 1084:     unless (&htmlareabrowser()) { return ''; }
 1085:     my $lang='en';
 1086:     if (&mt('htmlarea_lang') ne 'htmlarea_lang') {
 1087: 	$lang=&mt('htmlarea_lang');
 1088:     }
 1089:     return (<<ENDHEADERS);
 1090: <script type="text/javascript">
 1091: _editor_url='/htmlarea/';
 1092: _editor_lang='$lang';
 1093: </script>
 1094: <script type="text/javascript" src="/htmlarea/htmlarea.js"></script>
 1095: <link rel="stylesheet" type="text/css" href="/htmlarea/htmlarea.css" />
 1096: ENDHEADERS
 1097: }
 1098: 
 1099: # ------------------------------------------------- Activate additional buttons
 1100: 
 1101: sub htmlareaaddbuttons {
 1102:     if (&htmlareablocked()) { return ''; }
 1103:     unless (&htmlareabrowser()) { return ''; }
 1104:     return (<<ENDADDBUTTON);
 1105:     var config=new HTMLArea.Config();
 1106:     config.registerButton('ed_math','LaTeX Inline',
 1107: 			  '/htmlarea/images/ed_math.gif',false,
 1108: 			    function(editor,id) {
 1109: 			      editor.surroundHTML('&nbsp;<m>\$','\$</m>&nbsp;');
 1110: 			    }
 1111: 			  );
 1112:     config.registerButton('ed_math_eqn','LaTeX Equation',
 1113: 			  '/htmlarea/images/ed_math_eqn.gif',false,
 1114: 			    function(editor,id) {
 1115: 			      editor.surroundHTML(
 1116: 				     '&nbsp;\\n<center><m>\\\\[','\\\\]</m></center>\\n&nbsp;');
 1117: 			    }
 1118: 			  );
 1119:     config.toolbar.push(['ed_math','ed_math_eqn']);
 1120: ENDADDBUTTON
 1121: }
 1122: 
 1123: # ----------------------------------------------------------------- Preferences
 1124: 
 1125: sub disablelink {
 1126:     my @fields=@_;
 1127:     if (defined($#fields)) {
 1128: 	unless ($#fields>=0) { return ''; }
 1129:     }
 1130:     return '<a href="'.&HTML::Entities::encode('/adm/preferences?action=set_wysiwyg&wysiwyg=off&returnurl=','<>&"').&escape($ENV{'REQUEST_URI'}).'">'.&mt('Disable WYSIWYG Editor').'</a>';
 1131: }
 1132: 
 1133: sub enablelink {
 1134:     my @fields=@_;
 1135:     if (defined($#fields)) {
 1136: 	unless ($#fields>=0) { return ''; }
 1137:     }
 1138:     return '<a href="'.&HTML::Entities::encode('/adm/preferences?action=set_wysiwyg&wysiwyg=on&returnurl=','<>&"').&escape($ENV{'REQUEST_URI'}).'">'.&mt('Enable WYSIWYG Editor').'</a>';
 1139: }
 1140: 
 1141: # ----------------------------------------- Script to activate only some fields
 1142: 
 1143: sub htmlareaselectactive {
 1144:     my @fields=@_;
 1145:     unless (&htmlareabrowser()) { return ''; }
 1146:     if (&htmlareablocked()) { return '<br />'.&enablelink(@fields); }
 1147:     my $output='<script type="text/javascript" defer="1">'.
 1148: 	&htmlareaaddbuttons();
 1149:     foreach(@fields) {
 1150: 	$output.="\nHTMLArea.replace('$_',config);";
 1151:     }
 1152:     $output.="\nwindow.status='Activated Editfields';\n</script><br />".
 1153: 	&disablelink(@fields);
 1154:     return $output;
 1155: }
 1156: 
 1157: # --------------------------------------------------------------------- Blocked
 1158: 
 1159: sub htmlareablocked {
 1160:     unless ($env{'environment.wysiwygeditor'} eq 'on') { return 1; }
 1161:     return 0;
 1162: }
 1163: 
 1164: # ---------------------------------------- Browser capable of running HTMLArea?
 1165: 
 1166: sub htmlareabrowser {
 1167:     return 1;
 1168: }
 1169: 
 1170: ############################################################
 1171: ############################################################
 1172: 
 1173: =pod
 1174: 
 1175: =item breadcrumbs
 1176: 
 1177: Compiles the previously registered breadcrumbs into an series of links.
 1178: FAQ and BUG links will be placed on the left side of the table if they
 1179: are defined for the last registered breadcrumb.  
 1180: Additionally supports a 'component', which will be displayed on the
 1181: right side of the table (without a link).
 1182: A link to help for the component will be included if one is specified.
 1183: 
 1184: All inputs can be undef without problems.
 1185: 
 1186: Inputs: $component (the large text on the right side of the table),
 1187:         $component_help
 1188:         $menulink (boolean, controls whether to include a link to /adm/menu)
 1189:         $helplink (if 'nohelp' don't include the orange help link)
 1190:         $css_class (optional name for the class to apply to the table for CSS)
 1191: Returns a string containing breadcrumbs for the current page.
 1192: 
 1193: =item clear_breadcrumbs
 1194: 
 1195: Clears the previously stored breadcrumbs.
 1196: 
 1197: =item add_breadcrumb
 1198: 
 1199: Pushes a breadcrumb on the stack of crumbs.
 1200: 
 1201: input: $breadcrumb, a hash reference.  The keys 'href','title', and 'text'
 1202: are required.  If present the keys 'faq' and 'bug' will be used to provide
 1203: links to the FAQ and bug sites. If the key 'no_mt' is present the 'title' 
 1204: and 'text' values won't be sent through &mt()
 1205: 
 1206: returns: nothing    
 1207: 
 1208: =cut
 1209: 
 1210: ############################################################
 1211: ############################################################
 1212: {
 1213:     my @Crumbs;
 1214:     
 1215:     sub breadcrumbs {
 1216:         my ($component,$component_help,$menulink,$helplink,$css_class) = @_;
 1217:         #
 1218: 	$css_class ||= 'LC_breadcrumbs';
 1219:         my $Str = "\n".'<table class="'.$css_class.'"><tr><td>';
 1220:         #
 1221:         # Make the faq and bug data cascade
 1222:         my $faq = '';
 1223:         my $bug = '';
 1224: 	my $help='';
 1225:         # The last breadcrumb does not have a link, so handle it separately.
 1226:         my $last = pop(@Crumbs);
 1227:         #
 1228:         # The first one should be the course or a menu link
 1229: 	if (!defined($menulink)) { $menulink=1; }
 1230:         if ($menulink) {
 1231:             my $description = 'Menu';
 1232:             if (exists($env{'request.course.id'}) && 
 1233:                 $env{'request.course.id'} ne '') {
 1234:                 $description = 
 1235:                     $env{'course.'.$env{'request.course.id'}.'.description'};
 1236:             }
 1237:             unshift(@Crumbs,{
 1238:                     href   =>'/adm/menu',
 1239:                     title  =>'Go to main menu',
 1240:                     target =>'_top',
 1241:                     text   =>$description,
 1242:                 });
 1243:         }
 1244:         my $links .= 
 1245:             join('-&gt;',
 1246:                  map {
 1247:                      $faq = $_->{'faq'} if (exists($_->{'faq'}));
 1248:                      $bug = $_->{'bug'} if (exists($_->{'bug'}));
 1249:                      $help = $_->{'help'} if (exists($_->{'help'}));
 1250:                      my $result = '<a href="'.$_->{'href'}.'" ';
 1251:                      if (defined($_->{'target'}) && $_->{'target'} ne '') {
 1252:                          $result .= 'target="'.$_->{'target'}.'" ';
 1253:                      }
 1254: 		     if ($_->{'no_mt'}) {
 1255: 			 $result .='title="'.$_->{'title'}.'">'.
 1256: 			     $_->{'text'}.'</a>';
 1257: 		     } else {
 1258: 			 $result .='title="'.&mt($_->{'title'}).'">'.
 1259: 			     &mt($_->{'text'}).'</a>';
 1260: 		     }
 1261:                      $result;
 1262:                      } @Crumbs
 1263:                  );
 1264:         $links .= '-&gt;' if ($links ne '');
 1265: 	if ($last->{'no_mt'}) {
 1266: 	    $links .= '<b>'.$last->{'text'}.'</b>';
 1267: 	} else {
 1268: 	    $links .= '<b>'.&mt($last->{'text'}).'</b>';
 1269: 	}
 1270:         #
 1271:         my $icons = '';
 1272:         $faq = $last->{'faq'} if (exists($last->{'faq'}));
 1273:         $bug = $last->{'bug'} if (exists($last->{'bug'}));
 1274:         $help = $last->{'help'} if (exists($last->{'help'}));
 1275:         $component_help=($component_help?$component_help:$help);
 1276: #        if ($faq ne '') {
 1277: #            $icons .= &Apache::loncommon::help_open_faq($faq);
 1278: #        }
 1279: #        if ($bug ne '') {
 1280: #            $icons .= &Apache::loncommon::help_open_bug($bug);
 1281: #        }
 1282: 	if ($faq ne '' || $component_help ne '' || $bug ne '') {
 1283: 	    $icons .= &Apache::loncommon::help_open_menu($component,
 1284: 							 $component_help,
 1285: 							 $faq,$bug);
 1286: 	}
 1287:         #
 1288:         $Str .= $links.'</td>';
 1289:         #
 1290:         if (defined($component)) {
 1291:             $Str .= '<td class="'.$css_class.'_component">'.
 1292:                 &mt($component);
 1293: 	    if ($icons ne '') {
 1294: 		$Str .= '&nbsp;'.$icons;
 1295: 	    }
 1296: 	    $Str .= '</td>';
 1297:         }
 1298:         $Str .= '</tr></table>'."\n";
 1299:         #
 1300:         # Return the @Crumbs stack to what we started with
 1301:         push(@Crumbs,$last);
 1302:         shift(@Crumbs);
 1303:         #
 1304:         return $Str;
 1305:     }
 1306: 
 1307:     sub clear_breadcrumbs {
 1308:         undef(@Crumbs);
 1309:     }
 1310: 
 1311:     sub add_breadcrumb {
 1312:         push (@Crumbs,@_);
 1313:     }
 1314: 
 1315: } # End of scope for @Crumbs
 1316: 
 1317: ############################################################
 1318: ############################################################
 1319: 
 1320: # Nested table routines.
 1321: #
 1322: # Routines to display form items in a multi-row table with 2 columns.
 1323: # Uses nested tables to divide form elements into segments.
 1324: # For examples of use see loncom/interface/lonnotify.pm 
 1325: #
 1326: # Can be used in following order: ...
 1327: # &start_pick_box()
 1328: # row1
 1329: # row2
 1330: # row3   ... etc.
 1331: # &submit_row(0
 1332: # &end_pick_box()
 1333: #
 1334: # where row1, row 2 etc. are chosen from &role_select_row,&course_select_row,
 1335: # &status_select_row and &email_default_row
 1336: #
 1337: # Can also be used in following order:
 1338: #
 1339: # &start_pick_box()
 1340: # &row_title()
 1341: # &row_closure()
 1342: # &row_title()
 1343: # &row_closure()  ... etc.
 1344: # &submit_row()
 1345: # &end_pick_box()
 1346: #
 1347: # In general a &submit_row() call should proceed the call to &end_pick_box(),
 1348: # as this routine adds a button for form submission.
 1349: # &submit_row() does not require a &row_closure after it.
 1350: #  
 1351: # &start_pick_box() creates a bounding table with 1-pixel wide black border.
 1352: # rows should be placed between calls to &start_pick_box() and &end_pick_box.
 1353: #
 1354: # &row_title() adds a title in the left column for each segment.
 1355: # &row_closure() closes a row with a 1-pixel wide black line.
 1356: #
 1357: # &role_select_row() provides a select box from which to choose 1 or more roles 
 1358: # &course_select_row provides ways of picking groups of courses
 1359: #    radio buttons: all, by category or by picking from a course picker pop-up
 1360: #      note: by category option is only displayed if a domain has implemented 
 1361: #                selection by year, semester, department, number etc.
 1362: #
 1363: # &status_select_row() provides a select box from which to choose 1 or more
 1364: #  access types (current access, prior access, and future access)  
 1365: #
 1366: # &email_default_row() provides text boxes for default e-mail suffixes for
 1367: #  different authentication types in a domain.
 1368: #
 1369: # &row_title() and &row_closure() are called internally by the &*_select_row
 1370: # routines, but can also be called directly to start and end rows which have 
 1371: # needs that are not accommodated by the *_select_row() routines.    
 1372: 
 1373: sub start_pick_box {
 1374:     my ($css_class) = @_;
 1375:     if (defined($css_class)) {
 1376: 	$css_class = 'class="'.$css_class.'"';
 1377:     } else {
 1378: 	$css_class= 'class="LC_pick_box"';
 1379:     }
 1380:     my $output = <<"END";
 1381:  <table $css_class>
 1382: END
 1383:     return $output;
 1384: }
 1385: 
 1386: sub end_pick_box {
 1387:     my $output = <<"END";
 1388:        </table>
 1389: END
 1390:     return $output;
 1391: }
 1392: 
 1393: sub row_title {
 1394:     my ($title,$css_title_class,$css_value_class) = @_;
 1395:     $css_title_class ||= 'LC_pick_box_title';
 1396:     $css_title_class = 'class="'.$css_title_class.'"';
 1397: 
 1398:     $css_value_class ||= 'LC_pick_box_value';
 1399:     $css_value_class = 'class="'.$css_value_class.'"';
 1400: 
 1401:     my $output = <<"ENDONE";
 1402:            <tr class="LC_pick_box_row">
 1403:             <td $css_title_class>
 1404: 	       $title:
 1405:             </td>
 1406:             <td $css_value_class>
 1407: ENDONE
 1408:     return $output;
 1409: }
 1410: 
 1411: sub row_closure {
 1412:     my ($no_separator) =@_;
 1413:     my $output = <<"ENDTWO";
 1414:             </td>
 1415:            </tr>
 1416: ENDTWO
 1417:     if (!$no_separator) {
 1418:         $output .= <<"ENDTWO";
 1419:            <tr>
 1420:             <td colspan="2" class="LC_pick_box_separator">
 1421:             </td>
 1422:            </tr>
 1423: ENDTWO
 1424:     }
 1425:     return $output;
 1426: }
 1427: 
 1428: sub role_select_row {
 1429:     my ($roles,$title,$css_class,$show_separate_custom,$cdom,$cnum) = @_;
 1430:     my $output;
 1431:     if (defined($title)) {
 1432:         $output = &row_title($title,$css_class);
 1433:     }
 1434:     $output .= qq|
 1435:                                   <select name="roles" multiple >\n|;
 1436:     foreach my $role (@$roles) {
 1437:         my $plrole;
 1438:         if ($role eq 'ow') {
 1439:             $plrole = &mt('Course Owner');
 1440:         } elsif ($role eq 'cr') {
 1441:             if ($show_separate_custom) {
 1442:                 if ($cdom ne '' && $cnum ne '') {
 1443:                     my %course_customroles = &course_custom_roles($cdom,$cnum);
 1444:                     foreach my $crrole (sort(keys(%course_customroles))) {
 1445:                         my ($plcrrole) = ($crrole =~ m|^cr/[^/]+/[^/]+/(.+)$|);
 1446:                         $output .= '  <option value="'.$crrole.'">'.$plcrrole.
 1447:                                    '</option>';
 1448:                     }
 1449:                 }
 1450:             } else {
 1451:                 $plrole = &mt('Custom Role');
 1452:             }
 1453:         } else {
 1454:             $plrole=&Apache::lonnet::plaintext($role);
 1455:         }
 1456:         if (($role ne 'cr') || (!$show_separate_custom)) {
 1457:             $output .= '  <option value="'.$role.'">'.$plrole.'</option>';
 1458:         }
 1459:     }
 1460:     $output .= qq|                </select>\n|;
 1461:     if (defined($title)) {
 1462:         $output .= &row_closure();
 1463:     }
 1464:     return $output;
 1465: }
 1466: 
 1467: sub course_select_row {
 1468:     my ($title,$formname,$totcodes,$codetitles,$idlist,$idlist_titles,
 1469: 	$css_class) = @_;
 1470:     my $output = &row_title($title,$css_class);
 1471:     $output .= qq|
 1472: <script type="text/javascript">
 1473:     function coursePick (formname) {
 1474:         for  (var i=0; i<formname.coursepick.length; i++) {
 1475:             if (formname.coursepick[i].value == 'category') {
 1476:                 courseSet('');
 1477:             }
 1478:             if (!formname.coursepick[i].checked) {
 1479:                 if (formname.coursepick[i].value == 'specific') {
 1480:                     formname.coursetotal.value = 0;
 1481:                     formname.courselist = '';
 1482:                 }
 1483:             }
 1484:         }
 1485:     }
 1486:     function setPick (formname) {
 1487:         for  (var i=0; i<formname.coursepick.length; i++) {
 1488:             if (formname.coursepick[i].value == 'category') {
 1489:                 formname.coursepick[i].checked = true;
 1490:             }
 1491:             formname.coursetotal.value = 0;
 1492:             formname.courselist = '';
 1493:         }
 1494:     }
 1495: </script>
 1496:     |;
 1497:     my $courseform='<b>'.&Apache::loncommon::selectcourse_link
 1498:                      ($formname,'pickcourse','pickdomain','coursedesc','',1).'</b>';
 1499:         $output .= '<input type="radio" name="coursepick" value="all" onclick="coursePick(this.form)" />'.&mt('All courses').'<br />';
 1500:     if ($totcodes > 0) {
 1501:         my $numtitles = @$codetitles;
 1502:         if ($numtitles > 0) {
 1503:             $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 />';
 1504:             $output .= '<table><tr><td>'.$$codetitles[0].'<br />'."\n".
 1505:                '<select name="'.$$codetitles[0].
 1506:                '" onChange="setPick(this.form);courseSet('."'$$codetitles[0]'".')">'."\n".
 1507:                ' <option value="-1" />Select'."\n";
 1508:             my @items = ();
 1509:             my @longitems = ();
 1510:             if ($$idlist{$$codetitles[0]} =~ /","/) {
 1511:                 @items = split(/","/,$$idlist{$$codetitles[0]});
 1512:             } else {
 1513:                 $items[0] = $$idlist{$$codetitles[0]};
 1514:             }
 1515:             if (defined($$idlist_titles{$$codetitles[0]})) {
 1516:                 if ($$idlist_titles{$$codetitles[0]} =~ /","/) {
 1517:                     @longitems = split(/","/,$$idlist_titles{$$codetitles[0]});
 1518:                 } else {
 1519:                     $longitems[0] = $$idlist_titles{$$codetitles[0]};
 1520:                 }
 1521:                 for (my $i=0; $i<@longitems; $i++) {
 1522:                     if ($longitems[$i] eq '') {
 1523:                         $longitems[$i] = $items[$i];
 1524:                     }
 1525:                 }
 1526:             } else {
 1527:                 @longitems = @items;
 1528:             }
 1529:             for (my $i=0; $i<@items; $i++) {
 1530:                 $output .= ' <option value="'.$items[$i].'">'.$longitems[$i].'</option>';
 1531:             }
 1532:             $output .= '</select></td>';
 1533:             for (my $i=1; $i<$numtitles; $i++) {
 1534:                 $output .= '<td>'.$$codetitles[$i].'<br />'."\n".
 1535:                           '<select name="'.$$codetitles[$i].
 1536:                           '" onChange="courseSet('."'$$codetitles[$i]'".')">'."\n".
 1537:                           '<option value="-1">&lt;-Pick '.$$codetitles[$i-1].'</option>'."\n".
 1538:                           '</select>'."\n".
 1539:                           '</td>';
 1540:             }
 1541:             $output .= '</tr></table><br />';
 1542:         }
 1543:     }
 1544:     $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";
 1545:     $output .= &row_closure();
 1546:     return $output;
 1547: }
 1548: 
 1549: sub status_select_row {
 1550:     my ($types,$title,$css_class) = @_;
 1551:     my $output; 
 1552:     if (defined($title)) {
 1553:         $output = &row_title($title,$css_class,'LC_pick_box_select');
 1554:     }
 1555:     $output .= qq|
 1556:                                     <select name="types" multiple>\n|;
 1557:     foreach my $status_type (sort(keys(%{$types}))) {
 1558:         $output .= '  <option value="'.$status_type.'">'.$$types{$status_type}.'</option>';
 1559:     }
 1560:     $output .= qq|                   </select>\n|; 
 1561:     if (defined($title)) {
 1562:         $output .= &row_closure();
 1563:     }
 1564:     return $output;
 1565: }
 1566: 
 1567: sub email_default_row {
 1568:     my ($authtypes,$title,$descrip,$css_class) = @_;
 1569:     my $output = &row_title($title,$css_class);
 1570:     $output .= $descrip.
 1571: 	&Apache::loncommon::start_data_table().
 1572: 	&Apache::loncommon::start_data_table_header_row().
 1573: 	'<th>'.&mt('Authentication Method').'</th>'.
 1574: 	'<th align="right">'.&mt('Username -> e-mail conversion').'</th>'."\n".
 1575: 	&Apache::loncommon::end_data_table_header_row();
 1576:     my $rownum = 0;
 1577:     foreach my $auth (sort(keys(%{$authtypes}))) {
 1578:         my ($userentry,$size);
 1579:         if ($auth =~ /^krb/) {
 1580:             $userentry = '';
 1581:             $size = 25;
 1582:         } else {
 1583:             $userentry = 'username@';
 1584:             $size = 15;
 1585:         }
 1586:         $output .= &Apache::loncommon::start_data_table_row().
 1587: 	    '<td>  '.$$authtypes{$auth}.'</td>'.
 1588: 	    '<td align="right">'.$userentry.
 1589: 	    '<input type="text" name="'.$auth.'" size="'.$size.'" /></td>'.
 1590: 	    &Apache::loncommon::end_data_table_row();
 1591:     }
 1592:     $output .= &Apache::loncommon::end_data_table();
 1593:     $output .= &row_closure();
 1594:     return $output;
 1595: }
 1596: 
 1597: 
 1598: sub submit_row {
 1599:     my ($title,$cmd,$submit_text,$css_class) = @_;
 1600:     my $output = &row_title($title,$css_class,'LC_pick_box_submit');
 1601:     $output .= qq|
 1602:              <br />
 1603:              <input type="hidden" name="command" value="$cmd" />
 1604:              <input type="submit" value="$submit_text"/> &nbsp;
 1605:              <br /><br />
 1606:             \n|;
 1607:     return $output;
 1608: }
 1609: 
 1610: sub course_custom_roles {
 1611:     my ($cdom,$cnum) = @_;
 1612:     my %returnhash=();
 1613:     my %coursepersonnel=&Apache::lonnet::dump('nohist_userroles',$cdom,$cnum);
 1614:     foreach my $person (sort(keys(%coursepersonnel))) {
 1615:         my ($role) = ($person =~ /^([^:]+):/);
 1616:         my ($end,$start) = split(/:/,$coursepersonnel{$person});
 1617:         if ($end == -1 && $start == -1) {
 1618:             next;
 1619:         }
 1620:         if ($role =~ m|^cr/[^/]+/[^/]+/[^/]|) {
 1621:             $returnhash{$role} ++;
 1622:         }
 1623:     }
 1624:     return %returnhash;
 1625: }
 1626: 
 1627: 
 1628: ##############################################
 1629: ##############################################
 1630:                                                                              
 1631: # echo_form_input
 1632: #
 1633: # Generates html markup to add form elements from the referrer page
 1634: # as hidden form elements (values encoded) in the new page.
 1635: #
 1636: # Intended to support two types of use 
 1637: # (a) to allow backing up to earlier pages in a multi-page 
 1638: # form submission process using a breadcrumb trail.
 1639: #
 1640: # (b) to allow the current page to be reloaded with form elements
 1641: # set on previous page to remain unchanged.  An example would
 1642: # be where the a page containing a dynamically-built table of data is 
 1643: # is to be redisplayed, with only the sort order of the data changed. 
 1644: #  
 1645: # Inputs:
 1646: # 1. Reference to array of form elements in the submitted form on 
 1647: # the referrer page which are to be excluded from the echoed elements.
 1648: #
 1649: # 2. Reference to array of regular expressions, which if matched in the  
 1650: # name of the form element n the referrer page will be omitted from echo. 
 1651: #
 1652: # Outputs: A scalar containing the html markup for the echoed form
 1653: # elements (all as hidden elements, with values encoded). 
 1654: 
 1655: 
 1656: sub echo_form_input {
 1657:     my ($excluded,$regexps) = @_;
 1658:     my $output = '';
 1659:     foreach my $key (keys(%env)) {
 1660:         if ($key =~ /^form\.(.+)$/) {
 1661:             my $name = $1;
 1662:             my $match = 0;
 1663:             if ((!@{$excluded}) || (!grep/^$name$/,@{$excluded})) {
 1664:                 if (defined($regexps)) {
 1665:                     if (@{$regexps} > 0) {
 1666:                         foreach my $regexp (@{$regexps}) {
 1667:                             if ($name =~ /\Q$regexp\E/) {
 1668:                                 $match = 1;
 1669:                                 last;
 1670:                             }
 1671:                         }
 1672:                     }
 1673:                 }
 1674:                 if (!$match) {
 1675:                     if (ref($env{$key})) {
 1676:                         foreach my $value (@{$env{$key}}) {
 1677:                             $value = &HTML::Entities::encode($value,'<>&"');
 1678:                             $output .= '<input type="hidden" name="'.$name.
 1679:                                              '" value="'.$value.'" />'."\n";
 1680:                         }
 1681:                     } else {
 1682:                         my $value = &HTML::Entities::encode($env{$key},'<>&"');
 1683:                         $output .= '<input type="hidden" name="'.$name.
 1684:                                              '" value="'.$value.'" />'."\n";
 1685:                     }
 1686:                 }
 1687:             }
 1688:         }
 1689:     }
 1690:     return $output;
 1691: }
 1692: 
 1693: ##############################################
 1694: ##############################################
 1695:                                                                              
 1696: # set_form_elements
 1697: #
 1698: # Generates javascript to set form elements to values based on
 1699: # corresponding values for the same form elements when the page was
 1700: # previously submitted.
 1701: #     
 1702: # Last submission values are read from hidden form elements in referring 
 1703: # page which have the same name, i.e., generated by &echo_form_input(). 
 1704: #
 1705: # Intended to be called by onload event.
 1706: #
 1707: # Inputs:
 1708: # (a) Reference to hash of echoed form elements to be set.
 1709: #
 1710: # In the hash, keys are the form element names, and the values are the
 1711: # element type (selectbox, radio, checkbox or text -for textbox, textarea or
 1712: # hidden).
 1713: #
 1714: # (b) Optional reference to hash of stored elements to be set.
 1715: #
 1716: # If the page being displayed is a page which permits modification of
 1717: # previously stored data, e.g., the first page in a multi-page submission,
 1718: # then if stored is supplied, form elements will be set to the last stored
 1719: # values.  If user supplied values are also available for the same elements
 1720: # these will replace the stored values. 
 1721: #        
 1722: # Output:
 1723: #  
 1724: # javascript function - set_form_elements() which sets form elements,
 1725: # expects an argument: formname - the name of the form according to 
 1726: # the DOM, e.g., document.compose
 1727: 
 1728: sub set_form_elements {
 1729:     my ($elements,$stored) = @_;
 1730:     my %values;
 1731:     my $output .= 'function setFormElements(courseForm) {
 1732: ';
 1733:     if (defined($stored)) {
 1734:         foreach my $name (keys(%{$stored})) {
 1735:             if (exists($$elements{$name})) {
 1736:                 if (ref($$stored{$name}) eq 'ARRAY') {
 1737:                     $values{$name} = $$stored{$name};
 1738:                 } else {
 1739:                     @{$values{$name}} = ($$stored{$name});
 1740:                 }
 1741:             }
 1742:         }
 1743:     }
 1744: 
 1745:     foreach my $key (keys(%env)) {
 1746:         if ($key =~ /^form\.(.+)$/) {
 1747:             my $name = $1;
 1748:             if (exists($$elements{$name})) {
 1749:                 @{$values{$name}} = &Apache::loncommon::get_env_multiple($key);
 1750:             }
 1751:         }
 1752:     }
 1753: 
 1754:     foreach my $name (keys(%values)) {
 1755:         for (my $i=0; $i<@{$values{$name}}; $i++) {
 1756:             $values{$name}[$i] = &HTML::Entities::decode($values{$name}[$i],'<>&"');
 1757:             $values{$name}[$i] =~ s/([\r\n\f]+)/\\n/g;
 1758:             $values{$name}[$i] =~ s/"/\\"/g;
 1759:         }
 1760:         if ($$elements{$name} eq 'text') {
 1761:             my $numvalues = @{$values{$name}};
 1762:             if ($numvalues > 1) {
 1763:                 my $valuestring = join('","',@{$values{$name}});
 1764:                 $output .= qq|
 1765:   var textvalues = new Array ("$valuestring");
 1766:   var total = courseForm.elements['$name'].length;
 1767:   if (total > $numvalues) {
 1768:       total = $numvalues;
 1769:   }    
 1770:   for (var i=0; i<total; i++) {
 1771:       courseForm.elements['$name']\[i].value = textvalues[i];
 1772:   }
 1773: |;
 1774:             } else {
 1775:                 $output .= qq|
 1776:   courseForm.elements['$name'].value = "$values{$name}[0]";
 1777: |;
 1778:             }
 1779:         } else {
 1780:             $output .=  qq|
 1781:   var elementLength = courseForm.elements['$name'].length;
 1782:   if (elementLength==undefined) {
 1783: |;
 1784:             foreach my $value (@{$values{$name}}) {
 1785:                 if ($$elements{$name} eq 'selectbox') {
 1786:                     $output .=  qq|
 1787:       if (courseForm.elements['$name'].options[0].value == "$value") {
 1788:           courseForm.elements['$name'].options[0].selected = true;
 1789:       }|;
 1790:                 } elsif (($$elements{$name} eq 'radio') ||
 1791:                          ($$elements{$name} eq 'checkbox')) {
 1792:                     $output .= qq|
 1793:       if (courseForm.elements['$name'].value == "$value") {
 1794:           courseForm.elements['$name'].checked = true;
 1795:       }|;
 1796:                 }
 1797:             }
 1798:             $output .= qq|
 1799:   }
 1800:   else {
 1801:       for (var i=0; i<courseForm.elements['$name'].length; i++) {
 1802: |;
 1803:             if ($$elements{$name} eq 'selectbox') {
 1804:                 $output .=  qq|
 1805:           courseForm.elements['$name'].options[i].selected = false;|;
 1806:             } elsif (($$elements{$name} eq 'radio') || 
 1807:                      ($$elements{$name} eq 'checkbox')) {
 1808:                 $output .= qq|
 1809:           courseForm.elements['$name']\[i].checked = false;|; 
 1810:             }
 1811:             $output .= qq|
 1812:       }
 1813:       for (var j=0; j<courseForm.elements['$name'].length; j++) {
 1814: |;
 1815:             foreach my $value (@{$values{$name}}) {
 1816:                 if ($$elements{$name} eq 'selectbox') {
 1817:                     $output .=  qq|
 1818:           if (courseForm.elements['$name'].options[j].value == "$value") {
 1819:               courseForm.elements['$name'].options[j].selected = true;
 1820:           }|;
 1821:                 } elsif (($$elements{$name} eq 'radio') ||
 1822:                          ($$elements{$name} eq 'checkbox')) { 
 1823:                       $output .= qq|
 1824:           if (courseForm.elements['$name']\[j].value == "$value") {
 1825:               courseForm.elements['$name']\[j].checked = true;
 1826:           }|;
 1827:                 }
 1828:             }
 1829:             $output .= qq|
 1830:       }
 1831:   }
 1832: |;
 1833:         }
 1834:     }
 1835:     $output .= "
 1836: }\n";
 1837:     return $output;
 1838: }
 1839: 
 1840: ##############################################
 1841: ##############################################
 1842: 
 1843: # javascript_valid_email
 1844: #
 1845: # Generates javascript to validate an e-mail address.
 1846: # Returns a javascript function which accetps a form field as argumnent, and
 1847: # returns false if field.value does not satisfy two regular expression matches
 1848: # for a valid e-mail address.  Backwards compatible with old browsers without
 1849: # support for javascript RegExp (just checks for @ in field.value in this case). 
 1850: 
 1851: sub javascript_valid_email {
 1852:     my $scripttag .= <<'END';
 1853: function validmail(field) {
 1854:     var str = field.value;
 1855:     if (window.RegExp) {
 1856:         var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
 1857:         var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"; //"
 1858:         var reg1 = new RegExp(reg1str);
 1859:         var reg2 = new RegExp(reg2str);
 1860:         if (!reg1.test(str) && reg2.test(str)) {
 1861:             return true;
 1862:         }
 1863:         return false;
 1864:     }
 1865:     else
 1866:     {
 1867:         if(str.indexOf("@") >= 0) {
 1868:             return true;
 1869:         }
 1870:         return false;
 1871:     }
 1872: }
 1873: END
 1874:     return $scripttag;
 1875: }
 1876: 
 1877: 1;
 1878: 
 1879: __END__

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