#!/usr/bin/perl
# mon_acctarget.cgi
# filter Monitor to Tarantella Services

require './tarantella-lib.pl';
if ( ! $access{'monitor'} )   { &redirect("index.cgi"); }
if ( $config{'has_monitor'} ne "true" )  { &redirect("index.cgi"); }
&ReadParse();
&header($text{'index_monitor_acc'}, "", "monitor_acc" );
$monTarget=$in{monTarget};
if ( !$monTarget ) { &redirect("mon_accdef.cgi"); }
$monCondition=$in{monCondition};
if ( !$monCondition ) { &redirect("mon_accdef.cgi"); }
$monAcction=$in{monAcction};
if ( !$monAcction ) { &redirect("mon_accdef.cgi"); }
$monConditionValue=$in{monConditionValue};
$monScript=$in{monScript};

 print "<hr><p>\n"; 
 print "<SCRIPT SRC=\"global.js\">\n"; 
 print "</SCRIPT>\n"; 
 print "<SCRIPT LANGUAGE=\"JavaScript\">\n"; 
 print "<!--\n"; 
 print "/*  Strings for locales  */\n"; 
 print "var msg0=\"".$text{'msg_0'},"\";\n"; 
 print "var msg1=\"".$text{'msg_1'},"\";\n"; 
 print "var msg2=\"".$text{'msg_2'},"\";\n"; 
 print "var msg3=\"".$text{'msg_3'},"\";\n"; 
 print "var msg4=\"".$text{'msg_4'},"\";\n"; 
 print "var msg5=\"".$text{'msg_5'},"\";\n"; 
 print "var msg6=\"".$text{'msg_6'},"\";\n"; 
 print "var msg7=\"".$text{'msg_7'},"\";\n"; 
 print "/******************************************************************\n"; 
 print "   Chek_fields(obj)\n"; 
 print "*******************************************************************/\n"; 
 print "function Chek_fields(obj) {\n"; 
 print "     if ( emptyField(obj.acctAlias) == true ) {\n";
 print "               alert(\"error obj.acctAlias.name\"+\":\"+msg1);\n";
 print "               return false;\n";
 print "     }  \n";
 if ( $monTarget eq "user" ) {
    print "     if ( emptyField(obj.username) == true ) {\n";
    print "               alert(\"error $monTarget\"+\":\"+msg1);\n";
    print "               return false;\n";
    print "     }  \n";
 } elsif  ( $monTarget eq "ou" ) {
    print "     if ( get_value(\"select\",obj.user_ou) == \"none\" ) {\n";
    print "               alert(\"error $text{'obj_target'}\"+\":\"+msg1);\n";
    print "               return false;\n";
    print "     }  \n";
 } elsif  ( $monTarget eq "host" ) {
    print "     if ( get_value(\"select\",obj.hostname) == \"none\" ) {\n";
    print "               alert(\"error $monTarget\"+\":\"+msg1);\n";
    print "               return false;\n";
    print "     }  \n";
 } elsif  ( $monTarget eq "grphosts" ) {
    print "     if ( get_value(\"select\",obj.usergrphosts) == \"none\" ) {\n";
    print "               alert(\"error $monTarget\"+\":\"+msg1);\n";
    print "               return false;\n";
    print "     }  \n";
 } elsif  ( $monTarget eq "app" ) {
    print "     if ( get_value(\"select\",obj.appname) == \"none\" ) {\n";
    print "               alert(\"error $monTarget\"+\":\"+msg1);\n";
    print "               return false;\n";
    print "     }  \n";
 } elsif  ( $monTarget eq "grpapps" ) {
    print "     if ( get_value(\"select\",obj.grpappsname) == \"none\" ) {\n";
    print "               alert(\"error $monTarget\"+\":\"+msg1);\n";
    print "               return false;\n";
    print "     }  \n";
 }
 print "     return true;\n";
 print "}\n"; 
 print "//-->\n"; 
 if ( $monTarget eq "user" ) {
	 print "/******************************************************************\n";
 	print "   testCookie(Obj,formObj)\n";
 	print "   testCookie from form.\n";
 	print "*******************************************************************/\n";
 	print "function testCookie(obj,formObj) {\n";
 	print "   if ( emptyField(obj) == true ) {\n";
 	print "         val=getCookie(formObj.username.name);\n";
 	print "         if ( val != formObj.username.value ) { \n";
 	print "               obj.value=\"\"; \n";
 	print "         } else { \n";
 	print "               CheckCookie(obj);\n";
 	print "         }\n";
 	print "    } \n";
 	print "}\n";
 }
 print "</SCRIPT>\n"; 
 print "<p>\n"; 
 print "<table width=100% border>\n";
 print "<tr $tb> <td><b>$text{'titidx_monitor_acc'}  </b></td> </tr>\n";
 print "<tr $cb> <td><table width=100%>\n";
 print "<form METHOD=POST name=mon_acc ACTION=\"addacct.cgi\" onSubmit=\"return Chek_fields(this)\">\n"; 

print "<input TYPE=\"hidden\" NAME=\"monTarget\" VALUE=\"$monTarget\">\n";
print "<input TYPE=\"hidden\" NAME=\"monCondition\" VALUE=\"$monCondition\">\n";
print "<input TYPE=\"hidden\" NAME=\"monConditionValue\" VALUE=\"$monConditionValue\">\n";
print "<input TYPE=\"hidden\" NAME=\"monAcction\" VALUE=\"$monAcction\">\n";
print "<input TYPE=\"hidden\" NAME=\"monScript\" VALUE=\"$monScript\">\n";
 print "        <tr><td valign=top>",&hlink("<b>$text{'f_aliasacc'}</b>","faliasacc"),"&nbsp;</td>\n";
print "         <td valign=top> <input TYPE=\"text\" NAME=\"aliaAcct\" VALUE=\"\" cols=\"12\" size=\"12\" ALIGN=left></td></tr>\n";
print "        <tr><td valign=top>",&hlink("<b>$text{'f_descrip'}</b>","fdescrip"),"</td>\n";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"descrip\" VALUE=\"$descrip\" cols=\"40\" size=\"40\" ALIGN=left></td></tr>\n";

print "   <tr><td valign=top>",&hlink("<b>$text{'f_mon_target'}</b>","fmon_target"),"&nbsp;\n";

if ( $monTarget eq "user" ) {
    print "        ",&hlink("<b>$text{'f_username'}</b>","fusername"),"&nbsp;</td>";
    print "        <td valign=top>\n";
    $msgObj=$text{'selusr_win'};
    $formObj="mon_acc.username";
    &htmlWinLdapUsers($msgObj,$formObj);
    print "        <input TYPE=\"text\" NAME=\"username\" VALUE=\"$username\" cols=\"22\" size=\"12\" ALIGN=left onFocus=\"CheckCookie(this);\" onChange=\"mon_acc.userou.value=''\" >\n";
    print "        <input TYPE=\"text\" NAME=\"userou\" VALUE=\"$userou\" cols=\"22\" size=\"22\" ALIGN=left onFocus=\"testCookie(this,mon_acc)\"></td></tr>\n";
} elsif  ( $monTarget eq "ou" ) {
    print "   ",&hlink("<b>$text{'f_userou'}</b>","fuserou"),"&nbsp;</td>\n";
    print "      <td valign=top><select NAME=\"user_ou\">\n";
    print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selecou'}</b>","fselecou"),"\n";
    $nItems=0;
    foreach $item (&list_ous()) {
         if ( $nItems == 0 ) {
              print " <option VALUE=\"".$item->{'alias'}."\" >". $item->{'name'}."\n";
        }
         else {
              print " <option VALUE=\"".$item->{'alias'}."\" >".$item->{'name'}."  ";
         }
         $nItems++;
    }
    print "        </select></td></tr>\n";
} elsif  ( $monTarget eq "host" ) {
    print "   ",&hlink("<b>$text{'f_hostname'}</b>","fhostname"),"&nbsp;</td>\n";
    print "   <td valign=top><select NAME=\"hostname\">\n";
    print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selechosts'}</b>","fselechosts"),"\n";
   $nItems=0;
   foreach $item (&list_hosts()) {
        if ( $nItems == 0 ) {
             print " <option VALUE=\"".$item->{'alias'}."\">". $item->{'name'}."\n";
         }
        else {
             print " <option VALUE=\"".$item->{'alias'}."\" >".$item->{'name'}."  ";
        }
        $nItems++;
    }
    print "   </select></td></tr>\n";
} elsif  ( $monTarget eq "grphosts" ) {
    print "   ",&hlink("<b>$text{'f_usergrphosts'}</b>","fusergrphosts"),"&nbsp;</td>\n";
    print "   <td valign=top><select NAME=\"usergrphosts\">\n";
    print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selecgrphosts'}</b>","fselecgrphosts"),"\n";
   $nItems=0;
   foreach $item (&list_grphosts()) {
        if ( $nItems == 0 ) {
             print " <option VALUE=\"".$item->{'alias'}."\">". $item->{'name'}."\n";
       }
        else {
             print " <option VALUE=\"".$item->{'alias'}."\" >".$item->{'name'}."  ";
        }
        $nItems++;
   }
   print "        </select></td></tr>\n";
} elsif  ( $monTarget eq "app" ) {
    print "   ",&hlink("<b>$text{'f_appobjname'}</b>","fappobjname"),"&nbsp;</td>\n";
    print "   <td valign=top><select NAME=\"appname\">\n";
    print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selecapp'}</b>","fselecapps"),"\n";
    $nItems=0;
    foreach $item (&list_apps()) {
         if ( $nItems == 0 ) {
              print " <option VALUE=\"".$item->{'alias'}."\">". $item->{'name'}."\n";
        }
         else {
              print " <option VALUE=\"".$item->{'alias'}."\" >".$item->{'name'}."  ";
         }
         $nItems++;
    }
     print "        </select></td></tr>\n";
} elsif  ( $monTarget eq "grpapps" ) {
     print "   ",&hlink("<b>$text{'f_grpappsobjname'}</b>","fgrpappsobjname"),"&nbsp;</td>\n";
      print "   <td valign=top><select NAME=\"grpappsname\">\n";
      print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selecgrpapps'}</b>","fselecgrpapps"),"\n";
     $nItems=0;
     foreach $item (&list_grpapps()) {
          if ( $nItems == 0 ) {
               print " <option VALUE=\"".$item->{'alias'}."\">". $item->{'name'}."\n";
          }
          else {
               print " <option VALUE=\"".$item->{'alias'}."\" >".$item->{'name'}."  ";
          }
          $nItems++;
     }
     print "        </select></td></tr>\n";
}

@l_target = split(/,/,$text{'mon_target_value'}, -1);
$nItems=0;
foreach $item (@l_target) {
     if ( $item eq $monTarget ) {
        $targetext='mon_'.$item.'_sess';
        $t_monTarget=$text{$targetext};
        last;
     }
     $nItems++;
}

@l_conditions = split(/,/,$text{'mon_conditions_value'}, -1);
@l_conditions_tit = split(/,/,$text{'mon_conditions_desc'}, -1);
$nItems=0;
foreach $item (@l_conditions) {
     if ( $item eq $monCondition ) {
        $t_monCondition=$l_conditions_tit[$nItems];
        last;
     }
     $nItems++;
}

@l_acctions = split(/,/,$text{'mon_acctions_value'}, -1);
@l_acctions_tit = split(/,/,$text{'mon_acctions_desc'}, -1);
$nItems=0;
foreach $item (@l_acctions) {
     if ( $item eq $monAcction ) {
        $t_monAcction=$l_acctions_tit[$nItems];
        last;
     }
     $nItems++;
}

foreach $scr (&load_scriptlist()) { 
     if ( $scr->{'name'} eq $monScript ) {
        $t_monScript=$scr->{'name'}." (".$scr->{'desc'}.")";
        last;
     }
}
print "  <tr><td valign=top $cb><b> $t_monTarget -> $t_monCondition $monConditionValue -> $t_monAcction</b></td></tr>\n";
print "  <tr><td valign=top $cb><i>[ $t_monScript ]</i></td></tr>\n";

print "        <tr><td valign=top><input TYPE=\"submit\" NAME=\"",$text{'f_sendreq'},"\" VALUE=\"",$text{'f_sendreq'},"\">\n";
 print "        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>\n";
 print "        <td valign=top><input TYPE=\"reset\" NAME=\"",$text{'f_cancelreq'},"\" VALUE=\"",$text{'f_cancelreq'},"\"></td></tr>\n";
 print "	</form></td></tr>\n"; 
 print "<tr><tr></table></table><hr><p>\n";
if ( "$postform" ne "winldapusers") {  
   &footer($config{'back_return'},$text{'index'}); 
}
 print "<p><p><p>\n";
 do "footer.pl";
 exit;


