#!/usr/bin/perl
# my_user_chooser.cgi
# A modified version of chooser.cgi that uses the my_ functions to select hosts
# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL
# stta: (@#) 1.31051043346- [2004_01_29_102036]

require '../web-lib.pl';
do 'web-lib.pl';
do 'global-lib.pl';
do 'lists-lib.pl';
do 'html-lib.pl';

&init_config();
&load_dataconf();

&ReadParse(undef, undef, 1);
$fieldType=$in{f};
%access = &get_module_acl(undef, "");
%h_hosts=&list_hosts();
$host=$in{host};

if ($in{'multi'}) {
	# selecting multiple users.
	if ($in{'frame'} == 0) {
		# base frame
		&PrintHeader();
		print "<script>\n";
                @hl = split(/\s+/, $host);
	        $len = @hl;
		print "sel = new Array($len);\n";
		print "selr = new Array($len);\n";
		print "seli = new Array($len);\n";
		for($i=0; $i<$len; $i++) {
			print "sel[$i] = \"$hl[$i]\";\n";
                        $h=$hl[$i];
                        $hinfo=$h_hosts{$h}->{name};
                        $ipinfo=$h_hosts{$h}->{ipname};
			if ($hinfo ne "" ) { print "selr[$i] = \"$hinfo\";\n"; }
			else { print "selr[$i] = \"???\";\n"; }
			if ($ipinfo ne "" ) { print "seli[$i] = \"$ipinfo\";\n"; }
			else { print "seli[$i] = \"\";\n"; }
			}
		print "</script>\n";
		print "<title>$text{'index_hosts'}</title>\n";
		print "<frameset cols='50%,50%'>\n";
		print "<frame src=\"host_chooser.cgi?frame=1&multi=1&f=$fieldType&host=$host\">\n";
		print "<frameset rows='*,50' frameborder=no>\n";
		print " <frame src=\"host_chooser.cgi?frame=2&multi=1\">\n";
		print " <frame src=\"host_chooser.cgi?frame=3&multi=1\" scrolling=no>\n";
		print "</frameset>\n";
		print "</frameset>\n";
		}
	elsif ($in{'frame'} == 1) {
		# list of all hosts to choose from
                @hl = split(/\s+/, $host);
	        $len = @hl;
		&header();
                print &out_css();
		print "<script>\n";
		print "function addhost(h, r, ip)\n";
		print "{\n";
                print "   for (var i=0; i < top.sel.length; i++) {\n";
                print "      if ( top.sel[i] == h  ) { \n";
                print "            return false; \n";
                print "       }\n";
                print "   }\n";
		print "   top.sel[top.sel.length] = h\n";
		print "   top.selr[top.selr.length] = r\n";
		print "   top.seli[top.seli.length] = ip\n";
                print "   top.opener.ifield.value = top.sel.join(' ');\n";
		print "   top.frames[1].location = top.frames[1].location\n";
		print "   return false;\n";
		print "}\n";
		print "</script>\n";
		print "<table border=0><tr><td><font size=+1>$text{'choose_listhosts'}</font></td>\n";
		print "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>\n";
                print "<table border=1><tr $cb><td>$text{choose_msg_tosel}</td></tr></table>\n";
		print "</td></tr></table><hr>\n";
	        print "<table width=100% border=1>\n";
                print "<tr $tb>\n";
                print "<td><font color='$revtext'><center><b>".&hlink($text{'f_aliashosts'},"faliashosts")."</b></center></font></td>\n";
                print "<td><font color='$revtext'><center><b>".&hlink($text{'f_hostname'},"fhostname")."</b></center></font></td>\n";
                print "<td><font color='$revtext'><center><b>".&hlink($text{'f_ipaddress'},"fipaaddress")."</b></center></font></td>\n";
                print "<td><font color='$revtext'><center><b>".&hlink($text{'f_portalive'},"fportalive")."</b></center></font></td>\n";
                print "</tr>\n";
		foreach $h (&get_host_list()) {
                        $found_h="false";
		        for($i=0; $i<$len; $i++) {
		   	    if ( $hl[$i] eq $h->{alias} ) { $found_h="true"; last; }
                        }
			if ( $found_h eq "false") { print "<tr $cb>\n"; }
                        else { print "<tr $tb>\n"; }
#			$u->[6] =~ s/'/&#39;/g;
			print "<td width=20%><a class=\"menu1\" href=\"\" onClick='return addhost(\"$h->{alias}\", \"$h->{name}\", \"$h->{ipname}\")'>$h->{alias}</a></td>\n";
		        print "<td>$h->{name}</td>\n";
		        print "<td>$h->{ipname}</td>\n";
		        print "<td>$h->{portalive} &nbsp;</td></tr>\n";
			}
		print "</table>\n";
                print "<br><br><table border=1><tr $cb><td>$text{choose_msg_tosel}</td></tr></table><br>\n";
		}
	elsif ($in{'frame'} == 2) {
		# show chosen hosts
		&header();
                print &out_css();
		print "<font size=+1>$text{'choose_selhosts'}</font><br><hr>\n";
                print "<table width=100% border=1 $cb>\n";
		print <<'EOF';
<script>
function sub(j)
{
sel2 = new Array(); selr2 = new Array(); seli2 = new Array();
for(k=0,l=0; k<top.sel.length; k++) {
	if (k != j) {
		sel2[l] = top.sel[k];
		selr2[l] = top.selr[k];
		seli2[l] = top.seli[k];
		l++;
		}
	}
top.sel = sel2; top.selr = selr2; top.seli = seli2;
top.opener.ifield.value = top.sel.join(' ');
top.frames[1].location = top.frames[1].location;
return false;
}
for(i=0; i<top.sel.length; i++) {
	document.write("<tr>\n");
	document.write("<td><a class="\menu1\" href=\"\" onClick='return sub("+i+")'>"+top.sel[i]+"</a></td>\n");
	document.write("<td>"+top.selr[i]+"</td>\n");
	document.write("<td>"+top.seli[i]+"</td></tr>\n");
	}
</script>
</table>
EOF
print "<br><br><table border=1><tr $cb><td>$text{choose_msg_tounsel}</td></tr></table><br>\n";
		}
	elsif ($in{'frame'} == 3) {
		# output OK and Cancel buttons
		&header();
		print "<form>\n";
		print "<input type=button value=\"$text{'choose_ok'}\" ",
		      "onClick='",
		      "top.close()'>\n";
#		      "onClick='top.opener.ifield.value = top.sel.join(\" \"); ",
		print "<input type=button value=\"$text{'choose_cancel'}\" ",
		      "onClick='top.close()'>\n";
		print "&nbsp;&nbsp;<input type=button value=\"$text{'choose_clear'}\" onClick='top.sel = new Array(); top.selr = new Array(); top.frames[1].location = top.frames[1].location'>\n";
		print "</form>\n";
		}
	}
else {
	# selecting just one user .. display a list of all users to choose from
	&header();
	print "<script>\n";
	print "function select(f)\n";
	print "{\n";
        if ( $fieldType eq "select" ) {
             print "     var elem=top.opener.selfield;\n";
             print "     var j  = elem.length; \n";
             print "     var k  = -1; \n";
             print "     var opts = elem.options; \n";
             print "     for(i = 0; i < j; i++){ \n";
             print "         if (opts[i].selected ) { \n";
             print "              opts[i].selected=\"\"; \n";
             print "         }  \n";
             print "         if (opts[i].value == f ){ \n";
             print "              opts[i].selected=\"1\"; \n";
             print "         }  \n";
             print "     } \n";
        } else {
	     print "top.opener.ifield.value = f;\n";
        }
	print "top.close();\n";
	print "return false;\n";
	print "}\n";
	print "</script>\n";
	print "<table border=0><tr><td><font size=+1>$text{'choose_listhosts'}</font></td>\n";
	print "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>\n";
        print "<table border=1><tr $cb><td>$text{choose_msg_tosel}</td></tr></table>\n";
	print "</td></tr></table><hr>\n";
	print "<table width=100% border=1>\n";
        print "<tr $tb>\n";
        print "<td><font color='$revtext'><center><b>".&hlink($text{'f_aliashosts'},"faliashosts")."</b></center></font></td>\n";
        print "<td><font color='$revtext'><center><b>".&hlink($text{'f_hostname'},"fhostname")."</b></center></font></td>\n";
        print "<td><font color='$revtext'><center><b>".&hlink($text{'f_ipaddress'},"fipaaddress")."</b></center></font></td>\n";
        print "<td><font color='$revtext'><center><b>".&hlink($text{'f_portalive'},"fportalive")."</b></center></font></td>\n";
        print "</tr>\n";
	foreach $h (&get_host_list()) {
		if ($host eq $h->{alias}) { print "<tr $tb>\n"; }
		else { print "<tr $cb>\n"; }
		print "<td><a class=\"menu1\" href=\"\" onClick='return select(\"$h->{alias}\")'>$h->{name}</a></td>\n";
		print "<td>$h->{name}</td>\n";
		print "<td>$h->{ipname}</td>\n";
		print "<td>$h->{portalive} &nbsp;</td></tr>\n";
		}
	print "</table>\n";
	}

sub get_host_list
{
  local @listhosts=();
  foreach $item (keys (%h_hosts)) { push (@listhosts, $h_hosts{$item}); }
  return sort {$a->{'name'} cmp $b->{'name'}} @listhosts;
}

