Index: /trunk/cgi-bin/IPDB.pm
===================================================================
--- /trunk/cgi-bin/IPDB.pm	(revision 912)
+++ /trunk/cgi-bin/IPDB.pm	(revision 913)
@@ -1003,5 +1003,5 @@
 
   # Snag the allocations for this block
-  my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description,vrf,id,master_id".
+  my $sth = $dbh->prepare("SELECT masklen(cidr),cidr,city,type,custid,swip,description,vrf,id,master_id".
 	" FROM allocations WHERE parent_id = ? ORDER BY cidr");
   $sth->execute($args{parent});
@@ -1018,8 +1018,8 @@
   my $freesth = $dbh->prepare("SELECT count(*) FROM freeblocks WHERE cidr <<= ? AND master_id = ?");
   my $lfreesth = $dbh->prepare("SELECT masklen(cidr) AS maskbits FROM freeblocks WHERE cidr <<= ?".
-  	" AND master_id = ? ORDER BY masklen(cidr) LIMIT 1");
+  	" AND master_id = ? ORDER BY masklen(cidr)");
 
   my @blocklist;
-  while (my ($cidr,$city,$type,$custid,$swip,$desc,$vrf,$id,$mid) = $sth->fetchrow_array()) {
+  while (my ($blockmask,$cidr,$city,$type,$custid,$swip,$desc,$vrf,$id,$mid) = $sth->fetchrow_array()) {
     $desc .= " - vrf:$vrf" if $desc && $vrf;
     $desc = "vrf:$vrf" if !$desc && $vrf;
@@ -1033,7 +1033,19 @@
     my ($free) = $freesth->fetchrow_array();
     $lfreesth->execute($cidr, $mid);
+
+    # make sure we capture the first one separately...
     my ($lfree) = $lfreesth->fetchrow_array();
+    my $num_ipfree = ($lfree ? 2**(32-$lfree) : 0);
+    # ... so this can proceed without accidentally whacking it
+    while (my ($freemask) = $lfreesth->fetchrow_array) {
+      $num_ipfree += 2**(32-$freemask);
+    }
+    if ($type =~ /^.[dp]$/) {
+      ($num_ipfree) = $dbh->selectrow_array("SELECT count(*) FROM poolips WHERE parent_id = ? AND available = 'y'", undef, $id);
+    }
+    my $pctfree = sprintf "%0.1f", $num_ipfree / 2**(32-$blockmask) * 100;
     $lfree = "/$lfree" if $lfree;
     $lfree = '<NONE>' if !$lfree;
+
     my %row = (
 	block => $cidr,
@@ -1049,4 +1061,6 @@
 #    $row{subblock} = ($type =~ /^.r$/);         # hmf.  wonder why these won't work in the hash declaration...
     $row{listpool} = ($type =~ /^.[pd]$/);
+    # only set this on suitable types (pools, containers)
+    $row{pctfree} = "$num_ipfree IPs, $pctfree" if $type =~ /^.[cpdm]/;
     push (@blocklist, \%row);
   }
@@ -3321,5 +3335,5 @@
   } else {
     my $binfo = $dbh->selectrow_hashref(qq(
-	SELECT a.id, a.cidr AS block, a.city, a.vrf, a.parent_id, a.master_id, a.swip, $commonfields,
+	SELECT a.id, masklen(a.cidr), a.cidr AS block, a.city, a.vrf, a.parent_id, a.master_id, a.swip, $commonfields,
 		f.cidr AS reserve, f.id as reserve_id,
                 d.zone >>= a.cidr AS revavail, d.zone << a.cidr AS revpartial, d.location,
@@ -3333,4 +3347,17 @@
         WHERE a.id = ?
 	), undef, ($id) );
+
+    if ($binfo->{type} =~ /^.[dp]$/) {
+      ($binfo->{nfree}) = $dbh->selectrow_array("SELECT count(*) FROM poolips WHERE parent_id = ? AND available = 'y'", undef, $id);
+    } else {
+      # assemble free IP count
+      my $tmp = $dbh->prepare("SELECT 2^(32-masklen(cidr)) AS fc FROM freeblocks WHERE parent_id = ?");
+      $tmp->execute($id);
+      my $nfree = 0;
+      while (my ($fc) = $tmp->fetchrow_array) {
+        $nfree += $fc;
+      }
+      $binfo->{nfree} = $nfree;
+    }
 
     return $binfo;
Index: /trunk/cgi-bin/MyIPDB.pm
===================================================================
--- /trunk/cgi-bin/MyIPDB.pm	(revision 912)
+++ /trunk/cgi-bin/MyIPDB.pm	(revision 913)
@@ -81,5 +81,5 @@
 # 2 lists all entries in a container in CIDR order
 # Both show free blocks in a group at the bottom as previously
-# $IPDB::sublistlayout = 1;
+$IPDB::sublistlayout = 2;
 
 ## UI layout for showing VRFs and master blocks
Index: /trunk/cgi-bin/main.cgi
===================================================================
--- /trunk/cgi-bin/main.cgi	(revision 912)
+++ /trunk/cgi-bin/main.cgi	(revision 913)
@@ -430,4 +430,5 @@
   # Common elements
   my $pinfo = getBlockData($ip_dbh, $webvar{parent});
+  $page->param(blockpctfree => sprintf("%0.1f", $pinfo->{nfree}/2**(32-$pinfo->{masklen}) * 100 ) );
 
 ##fixme:  do we add a wrapper to not show the edit link for master blocks?
@@ -453,4 +454,5 @@
   my $cidr = new NetAddr::IP $poolinfo->{block};
   $page->param(vlan => $poolinfo->{vlan});
+  $page->param(poolpctfree => sprintf("%0.1f", $poolinfo->{nfree}/2**(32-$poolinfo->{masklen}) * 100 ) );
 
   # Tree navigation
Index: /trunk/templates/listpool.tmpl
===================================================================
--- /trunk/templates/listpool.tmpl	(revision 912)
+++ /trunk/templates/listpool.tmpl	(revision 913)
@@ -3,5 +3,6 @@
 <div class="tbltitle">Listing pool IPs for <TMPL_VAR NAME=block><br>
 (<TMPL_VAR NAME=disptype> in <TMPL_VAR NAME=city>)<TMPL_IF vlan><br>
-VLAN <TMPL_VAR NAME=vlan></TMPL_IF></div>
+VLAN <TMPL_VAR NAME=vlan></TMPL_IF>
+<TMPL_VAR NAME=poolpctfree>% free</div>
 
 <br>
Index: /trunk/templates/showsubs.tmpl
===================================================================
--- /trunk/templates/showsubs.tmpl	(revision 912)
+++ /trunk/templates/showsubs.tmpl	(revision 913)
@@ -1,4 +1,4 @@
 <div class="tbltitle">Subnets in <TMPL_VAR NAME=block>
-(<a href="<TMPL_VAR NAME=webpath>/cgi-bin/main.cgi?action=edit&amp;id=<TMPL_VAR NAME=self_id>&amp;basetype=b">Edit</a>)
+(<a href="<TMPL_VAR NAME=webpath>/cgi-bin/main.cgi?action=edit&amp;id=<TMPL_VAR NAME=self_id>&amp;basetype=b">Edit</a>)<TMPL_IF blockpctfree><br><TMPL_VAR NAME=blockpctfree>% free</TMPL_IF>
 </div>
 
Index: /trunk/templates/showsubs2.tmpl
===================================================================
--- /trunk/templates/showsubs2.tmpl	(revision 912)
+++ /trunk/templates/showsubs2.tmpl	(revision 913)
@@ -1,4 +1,5 @@
 <div class="tbltitle">Subnets in <TMPL_VAR NAME=block>
-(<a href="<TMPL_VAR NAME=webpath>/cgi-bin/main.cgi?action=edit&amp;id=<TMPL_VAR NAME=self_id>&amp;basetype=b">Edit</a>)
+(<a href="<TMPL_VAR NAME=webpath>/cgi-bin/main.cgi?action=edit&amp;id=<TMPL_VAR NAME=self_id>&amp;basetype=b">Edit</a>)<TMPL_IF blockpctfree><br><TMPL_VAR NAME=blockpctfree>% free</TMPL_IF>
+
 </div>
 <br>
@@ -30,5 +31,5 @@
 <td><TMPL_VAR NAME=type></td>
 <td><a href="<TMPL_VAR NAME=webpath>/cgi-bin/search.cgi?stype=q&amp;input=<TMPL_VAR NAME=custid>"><TMPL_VAR NAME=custid></a></td>
-<td><TMPL_IF hassubs><TMPL_VAR NAME=subfree> free<TMPL_IF subfree>, largest <TMPL_VAR ESCAPE=HTML NAME=lfree></TMPL_IF></TMPL_IF>
+<td><TMPL_IF hassubs><TMPL_VAR NAME=subfree> free<TMPL_IF subfree>, largest <TMPL_VAR ESCAPE=HTML NAME=lfree></TMPL_IF></TMPL_IF><TMPL_IF pctfree><TMPL_IF hassubs>, </TMPL_IF><TMPL_VAR NAME=pctfree>%</TMPL_IF>
 </td>
 <td><TMPL_VAR NAME=desc></td>
