# html-lib.pl
# Common functions 

# config_icons(context, program)
# Displays up to 17 icons, one for each type of configuration directive, for
# some context (global, virtual, directory or htaccess)
sub config_icons
{
local($m, $func, $e, %etype, $i, $c);
      foreach $m (sort { $a cmp $b } (keys %httpd_modules)) {
              $func = $m."_directives";
	      foreach $e (&$func($httpd_modules{$m})) {
		      if ($e->{$_[0]}) { $etype{$e->{'type'}}++; }
		      }
              }
      print "<table width=100% cellpadding=5>\n";
      for($i=0; $text{"type_$i"}; $i++) {
	      if ($etype{$i} && $access_types{$i}) {
		      if ($c%5 == 0) { print "<tr>\n"; }
		      print "<td valign=top align=center width=20%>\n";
		      &generate_icon("images/type_icon_$i.gif", $text{"type_$i"},
			             $_[1]."type=$i");
		      print "</td>\n";
		      if ($c%5 == 4) { print "<tr>\n"; }
		      $c++;
		      }
	      }
      for($i=2; $i<@_; $i++) {
	      if ($c%5 == 0) { print "<tr>\n"; }
	      print "<td valign=top align=center width=20%>\n";
	      &generate_icon($_[$i]->{'icon'}, $_[$i]->{'name'}, $_[$i]->{'link'});
	      print "</td>\n";
	      if ($c%5 == 4) { print "<tr>\n"; }
	      $c++;
	      }
      print "</table><p>\n";
}

sub footHere
{
        print "<tr><tr></table></table><hr><p>\n";
        &footer($config{'back_return'},$text{'index'});
        print "<p><p><p>\n";
        do "footer.pl";
}

# restart_button()
# Returns HTML for a link to put in the top-right corner of every page
sub restart_button
{
	$rv = "<a href=\"tta_restart.cgi\">$text{'tta_restart'}</a><br>\n";
	$rv .= "<a href=\"http://localhost/tarantella/help/en-us/admintocs/TOC_index.html\">$text{'tta_help'}</a>\n";
	$rv .= "<a href=\"ldap_restart.cgi\">$text{'ldap_restart'}</a>\n";
        if ( $config{'is_ldapcache'} eq "true" ) {
       	    $rv .= "<a href=\"ldapcache_restart.cgi\">$text{'ldapcache_restart'}</a>\n";
        }
	$rv .= "<a href=\"file:/opt/netscape/ldap/manual/en/slapd/ag/./contents.htm\">$text{'ldap_help'}</a>\n";
return $rv;
}

# show_error([message])
# Display an error message and exit. The variable $whatfailed must be set
# to the name of the operation that failed.
sub show_error
{
	print "<hr>\n";
	print "<h3>",($whatfailed ? "$whatfailed : " : ""),@_,"</h3>\n";
	print "<hr>\n";
}

# footCacheHere()
# Display a message, close tables and put page foot ... 
# almost ready to exit with good look & feel
sub footCacheHere {
 print "<table width=100% border>\n";
 print "<tr $tb> <td><b>|</b></td> </tr>\n";
 print "<tr></tr><tr><center><b>$text{'ldapcache_disable'}</center></b></tr>\n";
 print "<tr><tr></table></table><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
}

# htmlCacheapp(msgObj,formObj)
# Display all htmls labels to get input for "ttacacheapp" object 
sub htmlCacheapp 
{
 print "<tr><td valign=top><b>$msgObj</b>&nbsp;</td></tr>\n";
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_3tierlogin'}</b>","f3tierlogin"),"</td>\n"; 
 print "	<td valign=top><input TYPE=\"text\" NAME=\"tta3tier_login\" VALUE=\"$tta3tier_login\" cols=\"20\" size=\"20\" ALIGN=left></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_3tierpassword'}</b>","f3tierpassword"),"</td>\n"; 
 print "	<td valign=top><input TYPE=\"button\" NAME=\"?\" VALUE=\"?\" onClick=\"showPasswd($formObj);\" >"; 
 print "	<input TYPE=\"password\" NAME=\"tta3tier_password\" VALUE=\"$tta3tier_password\" cols=\"20\" size=\"20\" ALIGN=left></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_3tiertitle'}</b>","f3tiertitle"),"&nbsp;</td>\n";
print "		<td valign=top><input TYPE=\"text\" NAME=\"tta3tier_title\" VALUE=\"$tta3tier_title\" cols=\"30'\" size=\"30\" ALIGN=left></td></tr>\n"; 
 print "  	<tr><td valign=top>",&hlink("<b>$text{'f_3tierdomain'}</b>","f3tierdomain"),"</td>\n"; 
 print "        <td valign=top><input TYPE=\"text\" NAME=\"tta3tier_domain\" VALUE=\"$tta3tier_domain\" cols=\"12\" size=\"12\" ALIGN=left></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_3tierstringcmd'}</b>","f3tierstringcmd"),"</td>\n"; 
 print "	<td valign=top><input TYPE=\"text\" NAME=\"tta3tier_stringcmd\" VALUE=\"$tta3tier_stringcmd\" cols=\"60\" size=\"40\" ALIGN=left></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_3tierappkey1'}</b>","f3tierappkey1"),"</td>\n"; 
 print "	<td valign=top><input TYPE=\"text\" NAME=\"tta3tier_appkey1\" VALUE=\"$tta3tier_appkey1\" cols=\"20\" size=\"20\" ALIGN=left></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_3tierappkey2'}</b>","f3tierappkey2"),"</td>\n"; 
 print "	<td valign=top><input TYPE=\"text\" NAME=\"tta3tier_appkey2\" VALUE=\"$tta3tier_appkey2\" cols=\"20\" size=\"20\" ALIGN=left></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_3tierappkey3'}</b>","f3tierappkey3"),"</td>\n"; 
 print "	<td valign=top><input TYPE=\"text\" NAME=\"tta3tier_appkey3\" VALUE=\"$tta3tier_appkey3\" cols=\"20\" size=\"20\" ALIGN=left></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_3tierappkey4'}</b>","f3tierappkey4"),"</td>\n"; 
 print "	<td valign=top><input TYPE=\"text\" NAME=\"tta3tier_appkey4\" VALUE=\"$tta3tier_appkey4\" cols=\"20\" size=\"20\" ALIGN=left></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_3tierappkey5'}</b>","f3tierappkey5"),"</td>\n"; 
 print "	<td valign=top><input TYPE=\"text\" NAME=\"tta3tier_appkey5\" VALUE=\"$tta3tier_appkey5\" cols=\"20\" size=\"20\" ALIGN=left></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_3tierappkey6'}</b>","f3tierappkey6"),"</td>\n"; 
 print "	<td valign=top><input TYPE=\"text\" NAME=\"tta3tier_appkey6\" VALUE=\"$tta3tier_appkey6\" cols=\"20\" size=\"20\" ALIGN=left></td></tr>\n"; 
 print "        <tr><td valign=top>",&hlink("<b>$text{'f_3tiermaxsess'}</b>","f3tiermaxsess"),"&nbsp;</td>\n";
 if ( $tta3tier_maxsess eq "" ) { $tta3tier_maxsess="9999"; }
 print "        <td valign=top><input TYPE=\"text\" NAME=\"tta3tier_maxsess\" VALUE=\"$tta3tier_maxsess\" cols=\"4\" size=\"4\" ALIGN=left onChange=\"isNroThis(this)\">\n";

 print "   <tr><td valign=top>",&hlink("<b>$text{'f_ticketstatus'}</b>","fticketstatus"),"&nbsp;</td><td valign=top><select NAME=\"tta3tier_ticketstatus\">\n";
 print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_ticketstatus_sel'}</b>","fticket_status_sel"),"\n";
 
@l_target = split(/,/,$text{'ticket_status_value'}, -1);
$nItems=0;
foreach $item (@l_target) {
     print "         <option ";
     if ( $postform ne "" && $item eq $tta3tier_ticketstatus ) { print "selected " ; }
     print "VALUE=\"$item\" ";
     $targetext='ticket_'.$item.'_tit';
     print ">$text{$targetext}</b>\n";
     $nItems++;
}
print "        </select></td></tr>\n";
 print "        <tr><td valign=top>",&hlink("<b>$text{'f_endticket'}</b>","fendticket"),"&nbsp;</td>\n";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"tta3tier_endticket\" VALUE=\"$tta3tier_endticket\" cols=\"6\" size=\"6\" ALIGN=left onChange=\"\">\n";
 print "        <tr><td valign=top>",&hlink("<b>$text{'f_ticketcounter'}</b>","fticketcounter"),"&nbsp;</td>\n";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"tta3tier_ticketcounter\" VALUE=\"$tta3tier_ticketcounter\" cols=\"4\" size=\"4\" ALIGN=left onChange=\"isNroThis(this)\">\n";

 print "   <tr><td valign=top>",&hlink("<b>$text{'f_doifendticket'}</b>","fdoifendticket"),"&nbsp;</td><td valign=top><select NAME=\"tta3tier_doifendticket\">\n";
 print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_doifendticket_sel'}</b>","f_doifendticket_sel"),"\n";
 
@l_target = split(/,/,$text{'ticket_ifend_value'}, -1);
$nItems=0;
foreach $item (@l_target) {
     print "         <option ";
     if ( $postform ne "" && $item eq $tta3tier_doifendticket ) { print "selected " ; }
     print "VALUE=\"$item\" ";
     $targetext='ticket_'.$item.'_tit';
     print ">$text{$targetext}</b>\n";
     $nItems++;
}
print "        </select></td></tr>\n";

print "</td></tr>\n";
}

#script_cache_chek()
#Print javascript lines to check "ttacacheapp" object input fields
#
sub script_cache_chek
{
    print "     if ( emptyField(obj.tta3tier_login) == false ) {\n";
    print "          if ( emptyField(obj.tta3tier_password) == true ) {\n";
    print "                 alert(obj.tta3tier_password.name+\":\"+msg1+\"->\"+obj.tta3tier_password.value+\"<-\");\n";
    print "                 obj.tta3tier_password.focus();\n";
    print "                 return false;\n";
    print "          }\n";
    print "          if ( emptyField(obj.tta3tier_title) == true ) {\n";
    print "                 alert(obj.tta3tier_title.name+\":\"+msg1+\"->\"+obj.tta3tier_title.value+\"<-\");\n";
    print "                 obj.tta3tier_title.focus();\n";
    print "                 return false;\n";
    print "          }\n";
    print "          nro=parseInt(obj.tta3tier_maxsess.value);\n";
    print "          if ( nro+1 > 0) { \n";
    print "               obj.tta3tier_maxsess.value=nro;\n";
    print "          } else {;\n";
    print "               alert(obj.tta3tier_maxsess.name+\":\"+msg1+\"->\"+obj.tta3tier_maxsess.value+\"<-\");\n";
    print "               obj.tta3tier_maxsess.focus();\n";
    print "               return false;\n";
    print "          };\n";
    print "          if ( get_value(\"select\",obj.tta3tier_ticketstatus) == \"none\" ) {\n";
    print "                alert(obj.tta3tier_ticketstatus.name+\":\"+msg1+\"->\"+obj.tta3tier_ticketstatus.value+\"<-\");\n";
    print "                      obj.tta3tier_ticketstatus.focus();\n";
    print "                      return false;\n";
    print "          }   \n";
    print "          if ( get_value(\"select\",obj.tta3tier_ticketstatus) != \"no\" ) {\n";
    print "               if ( emptyField(obj.tta3tier_endticket) == true ) {\n";
    print "                      alert(obj.tta3tier_endticket.name+\":\"+msg1+\"->\"+obj.tta3tier_endticket.value+\"<-\");\n";
    print "                      obj.tta3tier_endticket.focus();\n";
    print "                      return false;\n";
    print "               }\n";
    print "               nro=parseInt(obj.tta3tier_ticketcounter.value);\n";
    print "               if ( nro+1 > 0) { \n";
    print "                    obj.tta3tier_ticketcounter.value=nro;\n";
    print "               } else {;\n";
    print "                    alert(obj.tta3tier_ticketcounter.name+\":\"+msg1+\"->\"+obj.tta3tier_ticketcounter.value+\"<-\");\n";
    print "                    obj.tta3tier_ticketcounter.focus();\n";
    print "                    return false;\n";
    print "               };\n";
    print "               if ( emptyField(obj.tta3tier_doifendticket) == \"none\" ) {\n";
    print "                      alert(obj.tta3tier_doifendticket.name+\":\"+msg1+\"->\"+obj.tta3tier_doifendticket.value+\"<-\");\n";
    print "                      obj.tta3tier_doifendticket.focus();\n";
    print "                      return false;\n";
    print "               }\n";
    print "          }   \n";
    print "     }\n";
}

# htmlCacheShowPassw(msgObj,formObj)
# Display all htmls labels to get input for "ttacacheapp" object 
sub htmlCacheaShowPassw
{
 print "/******************************************************************\n"; 
 print "   showPasswd(formObj)\n"; 
 print "   Show Password value from form.\n"; 
 print "*******************************************************************/\n"; 
 print "function showPasswd(formObj) {\n"; 
 print "   alert(\"Password es:\"+formObj.tta3tier_password.value+\"\");\n"; 
 print "}\n"; 
}

# htmlCheckAlias
# Out javascript to control aliases, it is need to get @_ with hash object list
sub htmlCheckAlias
{
  local (@obj);         

  print "/******************************************************************\n"; 
  print "   checkAlias_$_[0](Obj)\n"; 
  print "   checkAlias from the array and return false is already exist\n"; 
  print "*******************************************************************/\n"; 
  print "function checkAlias_$_[0](Obj) {\n"; 
  print "   var isalready = false; \n";
  print "   var listAlias = new Array();\n"; 
  @obj=eval("\&list_$_[0]()");
  $nItem=0;
  foreach $item (@obj) {print " listAlias[".$nItem++."] = \"".$item->{'alias'}."\";\n"; }

  print "   \n"; 
  print "   for (var i=0; i < listAlias.length; i++) {\n"; 
  print "       if ( Obj.value == listAlias[i] ) { \n"; 
  print "            isalready = true; \n";
  print "            break;\n"; 
  print "       }\n"; 
  print "   }\n"; 
  print "   if ( isalready == true ) { \n"; 
  print "          alert(Obj.name+\":\"+msg1+\"->\"+Obj.value+\"<-\");\n";
  print "                    Obj.focus();\n";
  print "   }\n"; 
  print "   return isalready;\n"; 
  print "}\n"; 
}

# htmlWinLdapUsers(msgObj,formObj)
# Open Window to show Users
sub htmlWinLdapUsers
{
 print "<input type=button onClick='window.open(\"/tarantella/tta_selusr.cgi?form=winldapusers\", \"selusr\", \"toolbar=no,menubar=no,scrollbar=yes,width=750,height=450\")' value=\"?\">\n";
}

sub outicons_table
# This is to output code for the "Tabs" interface menu 
{
          local ($i) = 0;
          foreach $lnk (@olinks) {
             print "<td valign=top align=center width=$per\%>\n";
             print "<table border bgcolor=#ffffff><tr><td><a href=$lnk>",
                   "<img src=$oicons[$i] alt=\"\" border=0></a>",
                   "</td></tr></table>\n";
             print "<a href=$lnk>$otitles[$i]</a></td>\n";
             $i++;
             if ($pos++ % $cols == $cols - 1) { print "</tr><tr>\n"; }
          }
          while($pos++ % $cols) {
                print "<td width=$per\%></td>\n";
          }
          print "</tr><tr>\n"; 
}

sub outconfig_icons
{
	local($i);
        local ($n) = 0;
	for($i=2; $i<@_; $i++) {
             print "<td valign=top align=center width=$per\%>\n";
             print "<table border bgcolor=#ffffff><tr><td><a href=$_[$i]->{'link'}>",
                   "<img src=$_[$i]->{'icon'} alt=\"\" border=0></a>",
                   "</td></tr></table>\n";
             print "<a href=$_[$i]->{'link'}>$_[$i]->{'name'}</a></td>\n";
             $n++;
             if ($pos++ % $cols == $cols - 1) { print "</tr><tr>\n"; }
        }
        while($pos++ % $cols) {
                print "<td width=$per\%></td>\n";
        }
        print "</tr><tr>\n"; 
}

1;  # Return true
