<%doc>

-- Device Worksheet --

Interface for viewing/updating a particular Device 
and all its relevant info in one page

</%doc>
%
<%attr>
title        => 'Device' 
section      => 'Management'
</%attr>
%
%
%#######################################################################
%#
%# Args section
%#
%#######################################################################
<%args>
$user       => $ui->get_current_user($r)
$id         => undef
$search     => undef
$edit       => undef
$editgen    => undef
$editloc    => undef
$editcomments => undef
$editmgmt   => undef
$editattr   => undef
$editips    => undef
$add_ip     => undef
$add_ip_int => undef
$add_ip_address => undef
$editints   => undef
$editdevbgp => undef
$editbgp    => undef
$ipsort     => "address"
$ifsort     => "number"
$peersort   => "ip"
$showvlan   => "all"
$intadd     => undef
$intaddnum  => undef
$overwrite_if_descr => undef
$if_auto_dns => undef
$submit     => undef
$view       => "Basics"
$deviceadd  => undef
$newhost    => undef
$newip      => undef
$toporoot   => Netdot->config->get('NMS_DEVICE')
$topodepth_up    => 1
$topodepth_down  => 1
$topovlans  => 0
$toponames  => 0
$specific_vlan  => 0
$stp_number     => undef
$state_digest   => undef
$_action        => undef
</%args>
%
%
%
%#######################################################################
%#
%# INIT section
%#
%#######################################################################
%
<%init>
my $DEBUG      = 0;
my %cssitem    = ( 0 => "formtablec1", 1 => "formtablec2" );
my $maxselect  = 100;
my $o          = undef;
my $ci         = 0;
my $name;
my %ifvlans;
my %vlans;
my $fqdn;
my (@arp_caches, @fwt);
my @modules;
my %ifacecolors = ( 
    manual  => "cccccc",
    removed => "ff8e8e",
    );

print '%ARGS is  <pre>', Dumper(%ARGS), '</pre><br>' if $DEBUG;

# Check if user can access this section
# We don't want average users seeing this page, its only for admins and operators.
# Users have a special limited page user_device.html
my $manager = $ui->get_permission_manager($r);
unless ( $manager && $manager->can($user, "access_section", 'device.html') ){
    $m->comp('/generic/error.mhtml', error=>"You don't have permission to access this page");
}

if ( $id ){
    unless ($o = Device->retrieve($id)){
	$m->comp('/generic/error.mhtml', error=>"Could not retrieve Device with id $id");
    }   
}
if ( $o ){  
    if( $intadd ){
	eval { $o->add_interfaces($intaddnum) };
	if ( my $e = $@ ){
	    $m->comp('/generic/error.mhtml', error=>$e);	
	}	
    }elsif( defined $overwrite_if_descr ){
	if ( $overwrite_if_descr == 0 || ($submit && ($submit eq 'confirm')) ){
	    eval { $o->set_overwrite_if_descr($overwrite_if_descr); };
	    if ( my $e = $@ ){
	     	$m->comp('/generic/error.mhtml', error=>$e);	
	    }
	    undef($submit);
	}elsif ( $overwrite_if_descr == 1 ){
	    $m->comp('/generic/confirm.html', 
		     %ARGS, 
		     target  =>'../management/device.html', 
		     message =>'Any currently saved interface descriptions will be overwritten by '. 
		     'SNMP-fetched data the next time the device is discovered. Are you sure?',
		     );
	}
    }elsif ( defined $if_auto_dns ){
	eval { $o->set_interfaces_auto_dns($if_auto_dns); };
	if ( my $e = $@ ){
	    $m->comp('/generic/error.mhtml', error=>$e);	
	}
    }elsif ( $add_ip eq 'Add' ){
	eval { $o->add_ip($add_ip_address, $add_ip_int) };
	if ( my $e = $@ ){
	    $m->comp('/generic/error.mhtml', error=>$e);	
	}
	$add_ip = undef;
    }
}

if( $deviceadd && $newhost ){
    if ( Device->search(name=>$newhost) ){
	# It's already there
	print "<p><b>Device $newhost already exists in DB</b>";   
	$m->abort;
    }else{
	# Minimal info to create device manually
	eval {
	    $o = Device->insert({name=>$newhost, snmp_managed=>0, canautoupdate=>0});
	};
	if ( my $e = $@ ){
	    $m->comp('/generic/error.mhtml', error=>$e);	
	}
    }
}elsif( $submit ){

    # insert/update
    my $new_digest;
    if ( $o ){
	$new_digest = $o->get_digest();
    }
    eval {
	Netdot::Model->do_transaction( sub{ 
	    if ( $state_digest && $new_digest && ($state_digest ne $new_digest) ){
		$ui->throw_user("This device changed while you were editing");
	    }
	    $ui->form_to_db(%ARGS); 
				       });
    };
    if ( my $e = $@ ){
	$m->comp('/generic/error.mhtml', error=>$e);
    }
    # Re-read $o to avoid seeing the old values
    $o = undef;
    $o = Device->retrieve($id);
}

if ( $o && $o->name ){
    $name = (defined $o->name->name) ? $o->name->name : "Name N/A!";
    $fqdn = $name . "." . $o->name->zone->name;
    $id = $o->id;

}elsif ( ! $search ){
    $m->comp('/generic/error.mhtml', error=>"Device undefined or no name set");
}

if ( $view eq "Interfaces" || $view eq "Topology" || $view eq "All" ){
    # Build a hash of Interface Vlans
    foreach my $if ( $o->interfaces ){
	if ( my @ifv = $if->vlans ){
	    foreach my $ifv ( @ifv ){
		my $vid = $ifv->vlan->vid;
		$ifvlans{$if}{$vid} = "";
		$vlans{$vid} = $ifv->vlan->id;
	    }
	}
    }
}

my $refresh_url = "device.html?id=$id&view=$view";

</%init>

<div id="sectiondetail">

%#######################################################################
%#
%# Device view section
%#
%#######################################################################

<!-- Header Table -->
<div class="containeroutside">
  <div class="containerheadleftoutside">
  Device: <b><% $fqdn %></b>&nbsp;
 <a href="http://<% $fqdn %>" target="#">[HTTP]</a> <a href="https://<% $fqdn %>" target="#">[HTTPS]</a>
  </div><!-- close containerheadleftoutside -->

 <div class="containerheadrightoutside">
    <a href="<% $refresh_url %>">[refresh]</a>
%    if ( $o->snmp_managed ){
%        if ( $manager && $manager->can($user, 'edit', $o) ){
	    <a href="updatedevice.html?device=<% $id %>&action=discover">[snmp-update]</a>
%        }
%    }
%    if ( $manager && $manager->can($user, 'delete', $o) ){
        <a href="delete.html?table=Device&id=<% $id %>">[delete]</a>
%    }
 </div> <!-- close containerheadrightoutside -->

%#######################################################################
%#
%# Navigation Bar
%#
%#######################################################################

<div id="navbar">
    <ul id="navigation">
%     if( $view eq "Basics" ) {
        <li><span>Basics</span></li>
%     } else {
        <li><a href="device.html?id=<% $id %>&view=Basics">Basics</a></li>
%     }
%     if( $view eq "Interfaces" ) {
        <li><span>Interfaces</span></li>
%     } else {
        <li><a href="device.html?id=<% $id %>&view=Interfaces">Interfaces</a></li>
%     }
%     if ( @modules = $o->modules ){
%       if( $view eq "Modules" ) {
          <li><span>Modules</span></li>
%       } else {
          <li><a href="device.html?id=<% $id %>&view=Modules">Modules</a></li>
%       }
%     }
%     if( $view eq "IP" ) {
        <li><span>IP info</span></li>
%     } else {
        <li><a href="device.html?id=<% $id %>&view=IP">IP info</a></li>
%     }
%     if ( $o->bgppeers ){
%       if( $view eq "BGP" ) {
          <li><span>BGP Peers</span></li>
%       } else {
          <li><a href="device.html?id=<% $id %>&view=BGP">BGP Peers</a></li>
%       }
%     }
%     my @circuits = $o->get_circuits;
%     if( scalar @circuits ){
%         if( $view eq "Circuit" ){
              <li><span>Circuits</span></li>
%         }
%         else{
              <li><a href="device.html?id=<% $id %>&view=Circuit">Circuits</a></li>
%         }
%     }
%     if ( $o->stp_enabled ){
%       if( $view eq "STP" ) {
          <li><span>Spanning Tree</span></li>
%       } else {
          <li><a href="device.html?id=<% $id %>&view=STP">Spanning Tree</a></li>
%       }
%     }
%     if( $view eq "Topology" ) {
        <li><span>Topology</span></li>
%     } else {
        <li><a href="device.html?id=<% $id %>&view=Topology">Topology</a></li>
%     }
%     if ( @arp_caches = $o->arp_caches ){
%       if( $view eq "ARP" ) {
          <li><span>ARP</span></li>
%       } else {
          <li><a href="device.html?id=<% $id %>&view=ARP">ARP</a></li>
%       }
%     }
%     if ( @fwt = $o->forwarding_tables ){
%       if( $view eq "FWT" ) {
          <li><span>FWT</span></li>
%       } else {
          <li><a href="device.html?id=<% $id %>&view=FWT">FWT</a></li>
%       }
%     }
%     if( $view eq "History" ) {
        <li><span>History</span></li>
%     } else {
        <li><a href="device.html?id=<% $id %>&view=History">History</a></li>
%     }
%     if( $view eq "Rights" ) {
        <li><span>Access Rights</span></li>
%     } else {
        <li><a href="device.html?id=<% $id %>&view=Rights">Access Rights</a></li>
%     }
%     if( $view eq "All" ) {
        <li><span>All</span></li>
%     } else {
        <li><a href="device.html?id=<% $id %>&view=All">All</a></li>
%     }
    </ul>
</div>

 <div class="containerbodyoutside">

%#######################################################################
%#
%# Basics
%#
%#######################################################################

% if ( $view eq "Basics" || $view eq "All" ){

% if ( $editgen || $editloc || $editcomments || $editmgmt ) {
        <form name="netdotform" action="device.html" method="POST">
	    <input type="hidden" name="id" value="<% $id %>">
	    <input type="hidden" name="view" value="<% $view %>">
% }
    <div class="container">
	<!-- General Info Table -->
    	<div class="containerheadleft"><b>General</b></div>
    	<div class="containerheadright">
% if ( $editgen ) {
	    <input type="button" name="cancel_button" value="cancel" onClick="history.go(-1);">
	    <input type="submit" name="submit" value="save" >
%           my $digest = $o->get_digest();
            <input type="hidden" name="state_digest" value="<% $digest %>">

% }else{
%    if ( $manager && $manager->can($user, "edit", $o) ){
         <a href="device.html?id=<% $id %>&view=<% $view %>&editgen=1">[edit]</a>
%    }else{
         &nbsp;
%    }
% }
        </div>
        <div class="containerbody">
<%perl>
my (@field_headers, @cell_data);

$ui->add_to_fields(o=>$o, edit=>$editgen, fields=>['name', 'aliases'], field_headers=>\@field_headers, 
		   cell_data=>\@cell_data, linkpages=>['host.html', '']);

$ui->add_to_fields(o=>$o, edit=>$editgen, 
		   fields=>['asset_id',], 
		   linkpages=>['view.html'],
		   field_headers=>\@field_headers, cell_data=>\@cell_data);

if ( my $asset = $o->asset_id ){
    push( @field_headers, $ui->col_descr_link('Product', 'type', 'Type:') );
    push( @cell_data, 
	  &{sub{	
	      my $pr;
	      if ( ($pr = $asset->product_id) && $pr->type ){
		  '<a href="view.html?table=ProductType&id='.$pr->type->id.'">'.$pr->type->name."</a>";
	      }
	    }}
	);
    push( @field_headers, $ui->col_descr_link('Asset', 'physaddr', 'Base MAC:') );
    push( @cell_data, 
	  &{sub{	
	      if ( my $phy = $asset->physaddr ){
		  '<a href="view.html?table=PhysAddr&id='.$phy->id.'">'.$phy->address."</a>";
	      }
	    }}
	);
    push( @field_headers, $ui->col_descr_link('Asset', 'custom_serial', 'Custom S/N:') );
    push( @cell_data, $asset->custom_serial);

    push( @field_headers, $ui->col_descr_link('Asset', 'inventory_number', 'Inventory:') );
    push( @cell_data, $asset->inventory_number);
}

$ui->add_to_fields(o=>$o, edit=>$editgen, 
		   fields=>['os'], 
		   field_headers=>\@field_headers, cell_data=>\@cell_data, 
    );
my $os_used = pop @cell_data;
my $latest_os = $o->product->latest_os if $o->product;
if ( !$editgen && $latest_os && ($os_used ne $latest_os) ){
    push (@cell_data, $os_used . " <span style=\"color:red;\">(Recommended OS: " . $latest_os . ")</span>");
}else{
    push (@cell_data, $os_used);
}

$ui->add_to_fields(o=>$o, edit=>$editgen, 
		   fields=>['extension'], 
		   field_headers=>\@field_headers, cell_data=>\@cell_data, 
    );

</%perl>

<& /generic/attribute_table.mhtml, field_headers=>\@field_headers, data=>\@cell_data, 
                          width=>"2", headercolwidth=>"15%", datacolwidth=>"35%" &>


      </div> <!-- containerbody -->
   </div> <!-- container -->
<!-- End General Info Table -->

<!-- Location Table -->
<div class="container">
    <div class="containerheadleft">
        Location/Contacts
    </div>
    <div class="containerheadright">
%	if ($editloc){	 
        <input type="button" name="cancel_button" value="cancel" onClick="history.go(-1);">
        <input type="submit" name="submit" value="save">
%	}else{
%           if ( $manager && $manager->can($user, "edit", $o) ){
                <a href="device.html?id=<% $id %>&view=<% $view %>&editloc=1">[edit]</a>
%           }else{
                &nbsp;
%    	    }
%	}
    </div>
    <div class="containerbody">
<%perl>
(@field_headers, @cell_data) = ();

$ui->add_to_fields(o=>$o, edit=>$editloc, fields=>['owner', 'used_by'], 
		   field_headers=>\@field_headers, cell_data=>\@cell_data, 
		   linkpages=>['view.html','view.html','view.html']);

my @devcontacts = $o->contacts;
push( @field_headers, $ui->table_descr_link('DeviceContacts', 'Contacts:') );
push( @cell_data, $ui->select_multiple(object=>$o, joins=>\@devcontacts, join_table=>"DeviceContacts", 
				       this_field=>"device", other_table=>"ContactList", 
				       other_field=>"contactlist", isEditing=>$editloc, action=>"delete", 
				       makeLink=>1, linkPage=>"contactlist.html", returnAsVar=>1 ) );
      


push( @field_headers, $ui->col_descr_link('Device', 'site', 'Site:')  );
push( @cell_data, &{sub{
    my $js = sprintf("onChange=\"jsrsSendqueryNM('Room', 'Device__%d__room', this.options[selectedIndex].text);\"", $o->id);
    $ui->select_lookup(object=>$o, column=>"site", lookup=>"Site",
		       edit=>$editloc, linkPage=>"view.html", htmlExtra=>$js, returnAsVar=>1)
    }} );


my @rooms;
if ( $o->site ){
    @rooms = sort { $a->name cmp $b->name } $o->site->rooms;
}
push( @field_headers, $ui->col_descr_link('Device', 'room', 'Room:') );
push( @cell_data, &{sub{
    $ui->select_lookup(object=>$o, column=>"room", lookup=>"Room", 
		       edit=>$editloc, defaults=>\@rooms,
		       makeLink=>1, linkPage=>"view.html", returnAsVar=>1)
    }} );

{
my @fields = ('rack');
$ui->add_to_fields(o=>$o, edit=>$editloc, fields=>\@fields, field_headers=>\@field_headers, cell_data=>\@cell_data);
}

</%perl>

    <& /generic/attribute_table.mhtml, width=>"2", field_headers=>\@field_headers, data=>\@cell_data &>

    </div>
</div>
<!-- End Location Table -->


<!-- Management Table -->
<div class="container">
    <div class="containerheadleft">
        Management
    </div>
    <div class="containerheadright">
%	 if ($editmgmt){	 
        <input type="button" name="cancel_button" value="cancel" onClick="history.go(-1);">
        <input type="submit" name="submit" value="save">
%	 }else{
%           if ( $manager && $manager->can($user, "edit", $o) ){
                <a href="device.html?id=<% $id %>&view=<% $view %>&editmgmt=1">[edit]</a>
%           }else{
                &nbsp;
% 	    }
%	 }
    </div>
    <div class="containerbody">
<%perl>
(@field_headers, @cell_data) = ();

$ui->add_to_fields(o=>$o, edit=>0, fields=>['date_installed', 'last_updated'], field_headers=>\@field_headers, cell_data=>\@cell_data);

{
    my @fields = ('oobname','oobnumber','monitored', 'monitoring_path_cost', 'customer_managed', 
		  'monitor_config', 'monitor_config_group', 'down_from', 'down_until', 
		  'snmp_managed', 'auto_dns');
    $ui->add_to_fields(o=>$o, edit=>$editmgmt, fields=>\@fields, field_headers=>\@field_headers, cell_data=>\@cell_data);
    
    push( @field_headers, $ui->col_descr_link('Device', 'layers', 'Layers:') );
    push( @cell_data, 
	  &{sub{	
	      my @layers;
	      if ( @layers = $o->list_layers ){
		  sprintf("%s", ( join ', ', @layers));
	      }
	    }}
	);
}

$ui->add_to_fields(o=>$o, edit=>0, fields=>['ipforwarding'], 
		   field_headers=>\@field_headers, cell_data=>\@cell_data);


if ( $o->snmp_managed ){
    my @fields = ('snmp_version', 'community', 'snmp_bulk', 'canautoupdate', 'snmp_polling',
		   'collect_arp', 'collect_fwt', 'collect_stp', 'snmp_conn_attempts', 'snmp_down');
    $ui->add_to_fields(o=>$o, edit=>$editmgmt, fields=>\@fields, field_headers=>\@field_headers, cell_data=>\@cell_data);
    
    @fields = ('last_arp', 'last_fwt', 'sysname', 'syslocation', 'sysdescription');
    $ui->add_to_fields(o=>$o, edit=>0, fields=>\@fields, field_headers=>\@field_headers, cell_data=>\@cell_data);
}
if ( $o->snmp_version eq "3" ){
    my @fields = ('snmp_authkey', 'snmp_authprotocol', 'snmp_privkey', 'snmp_privprotocol', 'snmp_securitylevel', 
		  'snmp_securityname' );
    $ui->add_to_fields(o=>$o, edit=>$editmgmt, fields=>\@fields, field_headers=>\@field_headers, cell_data=>\@cell_data);    
}

# Add the snmp_target IP.  We want to show only the device IPs when editing
# Do not retrieve all the IPs unless we are indeed editing
if ( $o->snmp_managed ){
    my %tmp;
    if ( $editmgmt ){
	my $ips = $o->get_ips();
	%tmp = $ui->form_field(object=>$o, column=>"snmp_target", defaults=>$ips, edit=>$editmgmt);
    }else{
	%tmp = $ui->form_field(object=>$o, column=>"snmp_target", edit=>$editmgmt);	    
    }
    push( @field_headers, $tmp{label} );
    push( @cell_data, $tmp{value} );
}
</%perl>

       <& /generic/attribute_table.mhtml, width=>"4", field_headers=>\@field_headers, data=>\@cell_data &>

    </div>
</div>
<!-- End Management Table -->



<div class="container">
    <div class="containerheadleft">
        Comments
    </div>
    <div class="containerheadright">
%	if ( $editcomments ){
            <input type="button" name="cancel_button" value="Cancel" onClick="history.go(-1);">
            <input type="submit" name="submit" value="save">
%	}else{
%           if ( $manager && $manager->can($user, "edit", $o) ){
                <a href="device.html?id=<% $id %>&view=<% $view %>&editcomments=1">[edit]</a>
%           }else{
                &nbsp;
%	    }
%	}
    </div>
    <div class="containerbody">

<%perl>
my %tmp;
%tmp = $ui->form_field(object=>$o, column=>"info", edit=>$editcomments, htmlExtra=>'cols="80" rows="10"');
print $tmp{value};         
</%perl>

    </div>
 </div>

%   if ( $editgen || $editloc || $editcomments || $editmgmt ) {
      </form>
%   }



<!-- Display Custom Attributes -->
% my @attributes = $o->attributes;
  <div class="container">
    <div class="containerheadleft"><strong>Custom Attributes</strong></div>
    <div class="containerheadright">
%     if ( $manager && $manager->can($user, 'edit', $o) ){
%         if ( @attributes ) {
              <a href="device.html?id=<% $o %>&editattr=1">[edit]</a>
%         }
          <a href="edit.html?table=DeviceAttr&device=<% $o %>">[add]</a>
%     }else{
          &nbsp;
%     }
    </div>
    <div class="containerbody">
        <& /generic/sortresults.mhtml, object=>\@attributes, withedit=>$editattr, return_args=>"?id=$id" &>
    </div>
  </div>
<!-- End Display Custom Attributes -->

% } 
<!-- End Basics Section -->

%#######################################################################
%#
%# Interfaces section
%#
%#######################################################################
% my (@headers, @rows, @row, @rowstyles);

%if ( $view eq "Interfaces" || $view eq "All" ){

<script language="javascript">
<!--
/* 
 * Show only interfaces belonging to selected VLAN
 * 
*/
function showVlan(){
    var name = document.showvlans.showvlan.value;
    window.location = "device.html?id=<% $id %>&view=<% $view %>&showvlan="+name;
}

-->
</script>

<%perl>
   my $ifs = $o->interfaces_by($ifsort); 
    #
    # Store values in a hash first.  Thay way we can quicly verify if 
    # at least one of the interfaces has that field set to some value 
    # and decide whether to add that column or not
    # 
    my %intinfo;
    foreach my $if ( @$ifs ){ 
	foreach my $field ( qw/jack room_char jack_char description neighbor/ ){
	    if ( defined($if->$field) ){
		$intinfo{$field}{$if->id} = $if->$field;
	    }
	}
    }
    # Use this var to avoid checking for permissions for each int
    my $can_edit = ($manager && $manager->can($user, "edit", $o))? 1 : 0;

    ### Headers
    my @closet_jacks;
    (@headers, @rows) = ();
    
    if ( $editints ){
      push @headers, '[del]';
    }
    
    push @headers, '<a href="device.html?id=' . $id . '&view=' . $view . '&ifsort=number">Number</a>';
    push @headers, '<a href="device.html?id=' . $id . '&view=' . $view . '&ifsort=name">Name</a>';
    
    if ( ! $editints ){
      push @headers, '<a href="device.html?id=' . $id . '&view=' . $view . '&ifsort=speed">Speed</a>';
      push @headers, '<a href="device.html?id=' . $id . '&view=' . $view . '&ifsort=vlan">VLAN</a>';
      push @headers, '<a href="device.html?id=' . $id . '&view=' . $view . '&ifsort=oper_status">Status</a>';
    }
    push @headers, '<a href="device.html?id=' . $id . '&view=' . $view . '&ifsort=monitored">Mon?</a>';
    push @headers, '<a href="device.html?id=' . $id . '&view=' . $view . '&ifsort=snmp">SNMP?</a>';
    
    if ( exists $intinfo{jack} || $editints ){
      push @headers, '<a href="device.html?id=' . $id . '&view=' . $view . '&ifsort=jack">Jack(cable)</a>';
      my @closets = $o->room->closets if ( $o->room );
      foreach my $cl ( @closets ) {
	  push @closet_jacks,  $cl->horizontalcables;
      }
      @closet_jacks = sort { $a->get_label cmp $b->get_label } @closet_jacks;
    }
    if ( exists $intinfo{room_char} || $editints ){
      push @headers, 'Room';
    }
    if ( exists $intinfo{jack_char} || $editints ){
      push @headers, 'Jack';
    }
    if ( exists $intinfo{description} || $editints ){
      push @headers, '<a href="device.html?id=' . $id . '&view=' . $view . '&ifsort=descr">Descr.</a>';
    }
    push @headers, 'Neighbor';


    ### Actual data
    foreach my $if ( @$ifs ){
        next if ( !exists $ifvlans{$if}{$showvlan} && $showvlan ne "all" );
	my (@row) = ();	
      
	if ( $editints ){	 
	    push @row, '<input type="checkbox" name="' . "Interface__" . $if->id . "__delete" . '" >';
	}
      
	push @row, $ui->form_field(object=>$if, column=>"number", edit=>$editints, htmlExtra=>"style=\"width: 5em;\"", 
				   linkPage=>"interface.html", returnValOnly=>1);
	push @row, $ui->form_field(object=>$if, column=>"name", edit=>$editints, htmlExtra=>"style=\"width: 10em;\"", 
				   linkPage=>"interface.html", returnValOnly=>1);
      
	if ( !$editints ){
	    push @row, $if->speed_pretty;
	    push @row, 
	    &{sub{
		my $ac = "";
		if ( exists $ifvlans{$if} ){
		    if ( (keys %{$ifvlans{$if}} > 3) ) {
			my $rc = "<select name=\"vlan_list\" ";
			my $sc = "onChange=\"window.open(this.options[this.selectedIndex].value,'_top')\">\n";
			my $tc = "<option value=\"$refresh_url\" selected>--Select--</option>\n";
			$ac = $rc . $sc . $tc;
			foreach my $v ( sort { $a <=> $b } keys %{$ifvlans{$if}} ) {
			    my $link = "view.html?table=Vlan&id=$vlans{$v}";
			    $ac .= "<option value=\"$link\">$v</option>\n";
			}
			$ac .= "</select>\n";
		    }else{
			my @links;
			foreach my $v ( keys %{$ifvlans{$if}} ){
			    push @links, sprintf("<a href=\"view.html?table=Vlan&id=%s\">%s</a>", $vlans{$v}, $v );
			}
			$ac .= join ', ', @links;
		    }
		    $ac;
		}
	      }};
	    push @row, $ui->form_field(object=>$if, column=>"oper_status", edit=>$editints, returnValOnly=>1);
	}
	
	push @row, $ui->form_field(object=>$if, column=>"monitored", edit=>$editints, returnValOnly=>1);
	push @row, $ui->form_field(object=>$if, column=>"snmp_managed", edit=>$editints, returnValOnly=>1);
	
	if ( exists $intinfo{jack} || $editints ){
	    my %args = (object=>$if, column=>"jack", linkPage=>"view.html", edit=>$editints, returnValOnly=>1);
	    if ( @closet_jacks && (scalar(@closet_jacks) <= $ui->config->get('DEFAULT_SELECTMAX')) ){
		$args{defaults} = \@closet_jacks;
	    }
	    push @row, $ui->form_field(%args);      
	}
	if ( exists $intinfo{room_char} || $editints ){
	    push @row, $ui->form_field(object=>$if, column=>"room_char", edit=>$editints, htmlExtra =>"style=\"width: 4em;\"", 
				       returnValOnly=>1);
	}
	if ( exists $intinfo{jack_char} || $editints ){
	    push @row, $ui->form_field(object=>$if, column=>"jack_char", edit=>$editints, htmlExtra =>"style=\"width: 7em;\"", 
				       returnValOnly=>1);
	}
	if ( exists $intinfo{description} || $editints ){
	    push @row, $ui->form_field(object=>$if, column=>"description", edit=>$editints, returnValOnly=>1);
	}
	
	my $ac = "";
	if ( exists $intinfo{neighbor}{$if->id} ){
	    $ac = $ui->form_field(object=>$if, column=>"neighbor", edit=>$editints, linkPage=>1, returnValOnly=>1);
	    $ac .= "<br>";
	}
	if ( !$editints && $can_edit ){
	    #  Do not allow user to add neighbors on virtual interfaces
	    if ( $if->type ne "53" && $if->type ne "propVirtual" ){
		$ac .= "<a class=\"hand\" onClick=\"window.open('addneighbor.html?int_id=$if&url=$refresh_url', 'Add Neighbor', 'width=600,height=200')\">[add]</a>";
	    }
	}
	push @row, $ac;
	
	push @rows, \@row;
	my $doc_status = $if->doc_status;
	if (  exists $ifacecolors{$doc_status} ){
	    push @rowstyles, "background-color: #$ifacecolors{$doc_status};"; 
	}else{
	    push @rowstyles, "";
	} 
	
    } #foreach
    
</%perl>

<!-- Interface Table -->
<div class="container">
    <div class="containerheadleft">
        Interfaces
    </div>
    <div class="containerheadright">

%   if ( $editints ){	 
    <form name="netdotform" action="device.html" method="POST">
	<input type="hidden" name="id" value="<% $id %>">
	<input type="hidden" name="view" value="<% $view %>">
	<input type="hidden" name="ifsort" value="<% $ifsort %>">
	<input type="button" name="cancel_button" value="cancel" onClick="history.go(-1);">
	<input type="submit" name="submit" value="save">
%   }else{

        <form name="showvlans">
	VLAN view: <select name="showvlan" onChange="showVlan();">
%       if ( $showvlan eq 'all' ){
            <option value="all" SELECTED>all</option>
%       }else{
            <option value="all">all</option>
%       }
%       foreach my $vid ( sort { $a <=> $b } keys %vlans ){
%           if ( $showvlan eq $vid ){
                <option value="<% $vid %>" SELECTED><% $vid %></option>     
%           }else{
                <option value="<% $vid %>"><% $vid %></option>     
%           }
%       }
        </select>
        </form> 
        Legend: 
%	foreach my $status ( keys %ifacecolors ){
	    <span style="background-color: #<% $ifacecolors{$status} %>;"><% $status %></span>
%       }
	&nbsp;&nbsp;
%       if ( $manager && $manager->can($user, "edit", $o) ){
            <a href="device.html?id=<% $id %>&view=<% $view %>&ifsort=<% $ifsort %>&editints=1">[edit]</a>
%       }
%  }

    </div>
    <div class="containerbody">
        <& /generic/data_table.mhtml, field_headers=>\@headers, data=>\@rows, rowstyle=>\@rowstyles &>
    </div>
    <div class="containerheadright">
%	  if ( $editints ){	 
            <input type="button" name="cancel_button" value="cancel" onClick="history.go(-1);">
            <input type="submit" name="submit" value="save">
%	  }
    </div>
</div>

% if ( ! $editints && ($view eq "Interfaces" || $view eq "All") ){
%  if ( $manager && $manager->can($user, 'edit', $o) ){
<div class="container">
    <div class="containerheadleft">
        Options
    </div>
    <div class="containerheadright">&nbsp;</div>
    <div class="containerbody">
    <p>
    <form name="add_ints_form" action="device.html" method="POST">
      <input type="hidden" name="id" value="<% $id %>">
      <input type="hidden" name="view" value="<% $view %>">
      Manually 
      <input type="submit" name="intadd" value="Add">
      <input type="text" name="intaddnum" value="1" size="3"> interfaces
    </form>
    </p>
    <p>
    <form name="overwrite_form" action="device.html" method="POST">
      <input type="hidden" name="id" value="<% $id %>">
      <input type="hidden" name="view" value="<% $view %>">
      SNMP Updates Overwrite Descriptions:
      <select name="overwrite_if_descr">
          <option value="" selected>--Select--</option>
          <option value="0">No</option>
          <option value="1">Yes</option>
      </select>
      <input type="submit" name="Set" value="Set">
    </form>
    </p>
    </div>
%   }
% }
</div>

%   if ($editints){
       </form>
%   }
% } # if view eq "Interfaces"

<!-- End Interfaces Section -->

%#######################################################################
%#
%# Modules
%#
%#######################################################################

% if ( $view eq "Modules" || $view eq "All"){

<%perl>
my %modules;
if ( @modules ){
    foreach my $m ( @modules ) {
	if ( $m->contained_in ){
	    push @{$modules{$m->contained_in}{children}}, $m;
	}else{
	    push @{$modules{root}}, $m;
	}
    }
}
print "<pre>", Dumper(%modules), "</pre>" if $DEBUG;
</%perl>

<div class="container">
    <div class="containerhead">
        Device Modules (<% scalar @modules %>)
    </div>
    <div class="containerbody">
       <& .show_modules, modules=>\%modules, objects=>$modules{root} &>
    </div>
</div>
% }

<%def .show_modules>
<%args>
$modules
$objects
</%args>

<div class="container">
    <div class="containerbody">
       <& /generic/sortresults.mhtml, object=>$objects, view => "row" , sort=>0 &>
% foreach my $obj ( @$objects ){
%   if ( defined $modules->{$obj->number}{children} ){
%    # Order by position
%    my @children = sort { $a->pos <=> $b->pos } @{$modules->{$obj->number}{children}};
<div class="container">
    <div class="containerheadleft">
        Module <% $obj->number %> contains:
    </div>
    <div class="containerheadright">&nbsp;
    </div>
    <div class="containerbody">
         <& .show_modules, modules=>$modules, objects=>\@children &>
    </div>
</div>
%   }
% }
    </div>
</div>
</%def>

%#######################################################################
%#
%# IP info
%#
%#######################################################################

% if ( $view eq "IP" || $view eq "All" ){

%  # Get all ip address info
%  my $ips;
%  if ( $o && $ipsort ){
%    $ips = $o->get_ips(sort_by=>$ipsort);
%  }

<!-- IPs Table -->

<div class="container">
    <div class="containerheadleft">
        IPs
    </div>
    <div class="containerheadright">
%   if ( $editips ) {
      <form name="netdotform" action="device.html" method="POST">
	  <input type="hidden" name="id" value="<% $id %>">
	  <input type="hidden" name="view" value="<% $view %>">
	  <input type="hidden" name="ipsort" value="<% $ipsort %>">
	  <input type="button" name="cancel_button" value="cancel" onClick="history.go(-1);">
          <input type="submit" name="submit" value="save">
%   }
%   if ( !$editips && !$add_ip ){
%       if ( $manager && $manager->can($user, "edit", $o) ){
          <a href="device.html?id=<% $id %>&view=<% $view %>&ipsort=<% $ipsort %>&editips=1">[edit]</a>
          <a href="device.html?id=<% $id %>&view=<% $view %>&ipsort=<% $ipsort %>&add_ip=1">[add]</a>
%       }
%   }
    </div>
    <div class="containerbody">

<%perl>
if (scalar @$ips){
    (@headers, @rows, @row) = ();

    @headers = ('<a href="device.html?id='. $id .'&view='. $view .'&ipsort=address">Address</a>',
		'Subnet',
		'<a href="device.html?id='. $id .'&view='. $view .'&ipsort=interface">Interface</a>',
		'DNS Name(s)',
		'Services',
		);

    my $can_edit = ($manager && $manager->can($user, "edit", $o))? 1 : 0;

    foreach my $ip ( @$ips ){	   
	next if ( ! exists $ifvlans{$ip->interface}{$showvlan} && 
		  $showvlan ne "all" );
	my (@row) = ();
	push( @row, 
	      &{sub{
		  my $a = "";
		  if ($editips){  
		      $a .= '<input type="checkbox" name="' . "Ipblock__" . $ip->id . "__delete" . '" >[del] ';
		      $a .=  $ip->address ;
		      $a .= '<br>';
		  }else{
		      $a .= '<a href="ip.html?id=' . $ip->id . '">' . $ip->address . '</a><br>';
		  }
		  $a;
	      }} );
	push( @row, 
	      &{sub{
		  my $a = "";
		  if ( $ip->parent ){
		      if ($editips){  
			  $a .=  $ip->parent->address . '/' . $ip->parent->prefix ;
		      }else{
			  $a .= '<a href="ip.html?id=' . $ip->parent->id . '">' . $ip->parent->address . '/' . 
			      $ip->parent->prefix . '</a><br>';
		      }
		  }
		  $a;
	      }} );
	push( @row,
	      &{sub{
		  my @ints = $o->interfaces;
		  $ui->select_lookup(object=>$ip, table=>"Ipblock", column=>'interface', lookup=>"Interface",
				     defaults=>\@ints, edit=>$editips, returnAsVar=>1, linkPage=>"interface.html");
	      }} );
	push( @row, 
	      &{sub{
		  my $a = "";
		  if ( $ip->a_records ){
		      foreach my $ar ( $ip->a_records ){
			  # Don't let user delete the RR that the Device name points to
			  # otherwise a referential integrity error would happen
			  if ( $editips && ($ar->rr->id != $o->name->id) ){
			      $a .= '<input type="checkbox" name="' . "RR__" . $ar->rr->id . "__delete" . '" >[del] ';
			  }
			  $a .=  $ui->form_field(object=>$ar->rr, column=>"name", edit=>0, 
						 linkPage=>"host.html", returnValOnly=>1);
		      }
		  }
		  $a;
	      }} );
	push( @row, 
	      &{sub{
		  my $a = "";
		  if ( scalar ($ip->services) ){
		      foreach my $ipsrv ($ip->services){
			  if ( $editips ){
			      $a .= '<input type="checkbox" name="' . "IpService__" . $ipsrv->id . "__delete" . '" > [del] ';
			  }
			  $a .= '<a href="view.html?table=IpService&id=' . $ipsrv->id . '">' . $ipsrv->service->name . '</a>';
			  $a .= '<br>';
		      }
		  }
		  if ( !$editips && $can_edit ){
                      $a .= "<a class=\"hand\" onClick=\"window.open('addservice.html?ip=$ip&url=$refresh_url', 'Create IP service', 'width=400,height=200')\">[add]</a>";
		  }
		  $a;
	      }} );
	push( @rows, \@row );
	
    }# foreach
	
	$m->comp('/generic/data_table.mhtml', field_headers=>\@headers, data=>\@rows );

} # endif scalar @ips

if ( !$editips ){
    (@headers, @rows) = ();
    
    if ( $add_ip ){
	# Let user add a new IP           
</%perl>
      <form name="netdotform" action="device.html" method="POST">
	  <input type="hidden" name="id" value="<% $id %>">
	  <input type="hidden" name="view" value="<% $view %>">
	  <input type="hidden" name="ipsort" value="<% $ipsort %>">
<%perl>
	@headers = ( 'Address', 'Interface' );
	(@row) = ();
	push( @row, 
	      &{sub{
		  my $ac = "";
		  $ac .= '<input type="text" name="add_ip_address" >';
		  $ac;
	      }} );
	push( @row, 
	      &{sub{
		  my $ac = "";
		  $ac .= '<select name="add_ip_int" >';
		  $ac .= '<option value="">Select Interface</option>';
		  foreach my $int ( sort { $a->get_label cmp $b->get_label } $o->interfaces ){
		      $ac .= '<option value="' . $int->id . '">' . $int->get_label . '</option>';
		  }
		  $ac .= '</select>';
		  $ac;
	      }} );
	push( @rows, \@row );

    }
    $m->comp('/generic/data_table.mhtml', field_headers=>\@headers, data=>\@rows );
    if ( $add_ip ){
	print '<p>';
	print '<input type="button" name="cancel_button" value="Cancel" onClick="history.go(-1);">';
	print '<input type="submit" name="add_ip" value="Add">';
    }
}
if ( $editips || $add_ip ) {
    print '</form>';
}
</%perl>
    </div>
</div>
% if ( !$editips && !$add_ip ){
<div class="container">
    <div class="containerheadleft">
        Options
    </div>
    <div class="containerheadright">&nbsp;</div>
    <div class="containerbody">
%   if ( $manager && $manager->can($user, 'edit', $o) ){
      <p>
      <form name="if_auto_dns_form" action="device.html" method="POST">
        <input type="hidden" name="id" value="<% $id %>">
        <input type="hidden" name="view" value="<% $view %>">
        Set Auto DNS on all IP interfaces to:
        <select name="if_auto_dns">
          <option value="" selected>--Select--</option>
          <option value="0">No</option>
          <option value="1">Yes</option>
        </select>
        <input type="submit" name="Set" value="Set">
      </form>
      </p>
%   }
    </div>
% }
</div>

% }

<!-- End IPs Section -->


%#######################################################################
%#
%# BGP Peers
%#
%#######################################################################

% if ( $view eq "BGP" || $view eq "All"){

% my (@field_headers, @cell_data);
% push( @field_headers, "BGP ID: " );
% push( @cell_data, $ui->form_field(object=>$o, column=>"bgpid", edit=>$editdevbgp, returnValOnly=>1) );
% push( @field_headers, "BGP Local AS: " );
% push( @cell_data, $ui->form_field(object=>$o, column=>"bgplocalas", edit=>$editdevbgp, returnValOnly=>1) );

<div class="container">
   <div class="containerheadleft">Device BGP Info</div>
   <div class="containerheadright">
%         if ( $editdevbgp ) {
            <form name="netdotform" action="device.html" method="POST">
	      <input type="hidden" name="id" value="<% $id %>">
	      <input type="hidden" name="view" value="<% $view %>">
	      <input type="button" name="cancel_button" value="cancel" onClick="history.go(-1);">
	      <input type="submit" name="submit" value="save">
%         }else{
%             if ( $manager && $manager->can($user, "edit", $o) ){
                  <a href="device.html?id=<% $id %>&view=<% $view %>&editdevbgp=1">[edit]</a>
%             }
%         }
   </div>
   <div class="containerbody">
             <& /generic/attribute_table.mhtml, field_headers=>\@field_headers, data=>\@cell_data &>
%         if ( $editdevbgp ) {
             </form>
%         }

   </div>
</div>

<%perl>
    my $ipeers = $o->get_bgp_peers(type=>"internal", sort=>$peersort);
    my $epeers = $o->get_bgp_peers(type=>"external", sort=>$peersort);
    my $whois_url = Netdot->config->get('BGP_AS_WHOIS_URL');
</%perl>

%   if ( $ipeers || $epeers ){

<!-- BGP Peers Table -->
<div class="container">
    <div class="containerheadleft">
        BGP Peers
    </div>
    <div class="containerheadright">

%     if ( $editbgp ) {
      <form name="netdotform" action="device.html" method="POST">
	<input type="hidden" name="id" value="<% $id %>">
	<input type="hidden" name="view" value="<% $view %>">
        <input type="button" name="cancel_button" value="cancel" onClick="history.go(-1);">
	<input type="submit" name="submit" value="save">
%     }else{
%         if ( $manager && $manager->can($user, "edit", $o) ){
              <a href="device.html?id=<% $id %>&view=<% $view %>&editbgp=1">[edit]</a>
%         }
%     }
    </div>
    <div class="containerbody">

<%perl>

(@headers, @rows) = ();

push( @headers, '<a href="device.html?id=' . $id . '&view=' . $view . '&peersort=ip">Remote IP</a>' );
push( @headers, '<a href="device.html?id=' . $id . '&view=' . $view . '&peersort=id">ID</a>' );
push( @headers, '<a href="device.html?id=' . $id . '&view=' . $view . '&peersort=entity">Entity</a>' );
push( @headers, '<a href="device.html?id=' . $id . '&view=' . $view . '&peersort=asname">AS Name</a>' );
push( @headers, '<a href="device.html?id=' . $id . '&view=' . $view . '&peersort=asnumber">AS Number</a>' );
push( @headers, 'Monitored?' );

if ( $epeers ){
    push @rows, ["<strong>External (eBGP)</strong>", "", "", "", "", ""];
    
    foreach my $peer ( @$epeers ){
	my (@row) = ();
	push( @row, $ui->form_field(object=>$peer, column=>"bgppeeraddr", 
				    edit=>$editbgp, linkPage=>"view.html", returnValOnly=>1) );
	push( @row, $peer->bgppeerid  );
	push( @row, '<a href="view.html?table=Entity&id=' . $peer->entity->id . '">' . $peer->entity->name . '</a>' );
	push( @row, $peer->entity->asname  );
	my $asn = $peer->entity->asnumber;
	if ( $whois_url && !($asn >= 64512 && $asn <= 65535) ){
	    my $url = $whois_url;
	    $url =~ s/<ASNUMBER>/$asn/;
	    push( @row, "<a target=\"#\" href=\"$url\">$asn</a>" );
	}else{
	    push( @row, $asn );
	}
	push( @row, $ui->radio_group_boolean(object=>$peer, column=>"monitored", edit=>$editbgp, returnAsVar=>1) );
	push( @rows, \@row );
    }
    push @rows, ["&nbsp;", "", "", "", "", ""];
}
if ( $ipeers ){
    push @rows, ["<strong>Internal (iBGP)</strong>", "", "", "", "", ""];
    
    foreach my $peer (@$ipeers){
	my (@row) = ();
	push( @row, $ui->form_field(object=>$peer, column=>"bgppeeraddr", 
				    edit=>$editbgp, linkPage=>"view.html", returnValOnly=>1) );
	push( @row, $peer->bgppeerid  );
	push( @row, '<a href="view.html?table=Entity&id=' . $peer->entity->id . '">' . $peer->entity->name . '</a>' );
	push( @row, $peer->entity->asname  );
	if ( $whois_url ){
	    my $asn = $peer->entity->asnumber;
	    my $url = $whois_url;
	    $url =~ s/<ASNUMBER>/$asn/;
	    push( @row, "<a target=\"#\" href=\"$url\">$asn</a>" );
	}else{
	    push( @row, $peer->entity->asnumber );
	}
	push( @row, $ui->radio_group_boolean(object=>$peer, column=>"monitored", edit=>$editbgp, returnAsVar=>1) );
	push( @rows, \@row );
    }
}
</%perl>
    
<& /generic/data_table.mhtml, field_headers=>\@headers, data=>\@rows &>

%   if ( $editbgp ) {
      </form>
%   }
    </div>
</div>
%  }

% }
<!-- End BGP Peers Section -->

%#######################################################################
%#
%# STP
%#
%#######################################################################

% if ( $view eq "STP" || $view eq "All"){

%     my (@field_headers, @cell_data);

%     # General info
%     if ( my $asset = $o->asset_id ){
%         $ui->add_to_fields(o=>$asset, edit=>0, fields=>['physaddr'], 
%                            field_headers=>\@field_headers, cell_data=>\@cell_data, 
%                            linkpages=>['mac.html']);
%     }
%     my @fields = ('stp_enabled', 'stp_type');
%     if ( $o->stp_type eq 'mst' ){
%         # MST info
%         push @fields, ('stp_mst_region', 'stp_mst_rev', 'stp_mst_digest');
%     }
%     $ui->add_to_fields(o=>$o, edit=>0, fields=>\@fields, field_headers=>\@field_headers, 
%    	 	         cell_data=>\@cell_data);

<div class="container">
   <div class="containerheadleft">Spanning Tree Info</div>
   <div class="containerheadright">&nbsp;</div>
   <div class="containerbody">
             <& /generic/attribute_table.mhtml, field_headers=>\@field_headers, data=>\@cell_data &>
   </div>
</div>

%    # Instance Info
%     my @instances;
%     if ( @instances = $o->stp_instances ){
<div class="container">
    <div class="containerhead">
        STP Instances
    </div>
    <div class="containerbody">

        <table border="0" cellspacing="0" cellpadding="0" class="tablebackground">
        <tr class="rowtitle2">
            <th>Number</th>
            <th>Device</th>
            <th>Root Port</th>
            <th>Root Bridge</th>
            <th>Bridge Priority</th>
            <th>STP Graph</th>
        </tr>
        
%   foreach my $stp_inst ( sort { $a->number <=> $b->number } @instances) {
        <tr>
	    <td align="left" valign="middle">
	        &nbsp;
	        <a href="view.html?table=STPInstance&id=<% $stp_inst->id %>"><% $stp_inst->number %></a>
	    </td>
	    <td align="left" valign="middle">
    	        &nbsp;
	        <a href="../management/device.html?table=Device&id=<% $id %>"><% $fqdn %></a>
	    </td>
	    <td align="left" valign="middle">
	        <% $stp_inst->root_port %>
	    </td>
	    <td align="left" valign="middle">
	        <% $stp_inst->root_bridge %>
	    </td>
	    <td align="left" valign="middle">
	        <% $stp_inst->bridge_priority %>
	    </td>
	    <td align="left" valign="middle">
	        &nbsp;
	        <a href="device.html?id=<% $id %>&view=<% $view %>&stp_number=<% $stp_inst->number %>">[Graph It]</a>
	    </td>
	</tr>
%   }
        </table>

    </div>
</div>

%     }

% if ( defined($stp_number) ) {
<!-- Spanning Tree Graph -->
<div class="container">
    <div class="containerhead">Spanning Tree Graph (Instance: <% $stp_number %>)</div>
    <div class="containerbody">

        <center>
%           print $ui->build_device_stp_graph_html(id=>$id, number=>$stp_number, view=>$view,
%                                                  web_path=>$r->dir_config('NetdotPath') );
        <center>
   </div>
</div>
% }


% }


%#######################################################################
%#
%# Topology
%#
%#######################################################################
%
% if ( $view eq "Topology" || $view eq "All" ){
%
<strong>Note</strong>: Depths greater than 2 may take a long time to render
<p>
<form name="change_topo_view_form" action="device.html" method="GET">
      <input type="hidden" name="id" value="<% $id %>">
      <input type="hidden" name="view" value="<% $view %>">
<table>
  <tr>
      <th>Show Vlans?</th>
      <th>Show this Vlan only?</th>
      <th>Show Interface Names?</th>
      <th>Depth up</th>
      <th>Depth down</th>
      <th>Reference Point</th>
      <th>&nbsp;</th>
  </tr>
  <tr>
      <td><input type="checkbox" name="topovlans" onChange="if(document.change_topo_view_form.topovlans.checked==false){document.change_topo_view_form.specific_vlan.value=0;}" value="1" <% ($topovlans ? "checked" : "") %>></td>
      <td><select name="specific_vlan" onChange="if(document.change_topo_view_form.specific_vlan.value!=0){document.change_topo_view_form.topovlans.checked=true;}">
%       if ( $specific_vlan == 0 ) {
          <option value="" SELECTED>--Select--</option>
%       } else {
          <option value="">--Select--</option>
%       }
%       foreach my $vid ( sort { $a <=> $b } keys %vlans ){
%           if ( $specific_vlan == $vid ){
                <option value="<% $vid %>" SELECTED><% $vid %></option>     
%           }else{
                <option value="<% $vid %>"><% $vid %></option>     
%           }
%       }
        </select></td>
      <td><input type="checkbox" name="toponames" value="1"  <% ($toponames ? "checked" : "") %>></td>
      <td><input type="text" name="topodepth_up" size=2 value="<% $topodepth_up %>"></td>
      <td><input type="text" name="topodepth_down" size=2 value="<% $topodepth_down %>"></td>
      <td><input type="text" name="toporoot" size=20 value="<% $toporoot %>"></td>
      
    <td><input type="submit" value="Change View"></td>
  </tr>
</table>
</form>

<!-- Topology drawing -->
<div class="container">
    <div class="containerhead">
        Topology - Layer 2
    </div>
    <div class="containerbody">
    <center>
%       $toporoot = Device->search(name=>$toporoot)->first || 
%                   $m->comp('/generic/error.mhtml', error=>"Cannot find root device: $toporoot");
%       print $ui->build_device_topology_graph_html(id=>$id, view=>$view, root=>$toporoot->id,
%                                                   depth_up=>$topodepth_up, depth_down=>$topodepth_down,
%                                                   web_path=>$r->dir_config('NetdotPath'), 
%                                                   show_vlans=>$topovlans, show_names=>$toponames, 
%                                                   specific_vlan=>$specific_vlan);
    </center>
    </div>
</div>

% } # End Topology stuff

%#######################################################################
%#
%# ARP
%#
%#######################################################################
%
% if ( $view eq "ARP" || $view eq "All" ){
%
<!-- ARP Cache Table -->
<div class="container">
    <div class="containerhead">
        ARP Caches
    </div>
    <div class="containerbody">
        <& /generic/sortresults.mhtml, object => \@arp_caches , view => "row" , sort=>0 &>
    </div>
</div>
% }
%
%
<!-- End ARP Cache Table -->

%#######################################################################
%#
%# FWT
%#
%#######################################################################
%
% if ( $view eq "FWT" || $view eq "All" ){
%
<!-- FWT Cache Table -->
<div class="container">
    <div class="containerhead">
        Forwarding Tables
    </div>
    <div class="containerbody">
        <& /generic/sortresults.mhtml, object => \@fwt , view => "row" , sort=>0&>
    </div>
</div>
% }
%
%
<!-- End FWT Cache Table -->

%#######################################################################
%#
%# History
%#
%#######################################################################
%
% if ( $view eq "History" || $view eq "All" ){
%   my @h_objs = $o->get_history;
%
<!-- History Table -->
<div class="container">
    <div class="containerhead">
        History
    </div>
    <div class="containerbody">
        <& /generic/sortresults.mhtml, object => \@h_objs , view => "row" &>
    </div>
</div>
% }
%
%
<!-- End History Section -->
%#######################################################################
%#
%# Circuit
%#
%#######################################################################

%if ( $view eq "Circuit" || $view eq "All" ){
    <div class="container">
        <div class="containerhead">Circuits</div>
        <div class="containerbody">
            <& /generic/sortresults.mhtml, object => \@circuits , view => "row" &>
        </div>
    </div>
%}


%################################################################
%# Access Rights
%################################################################

<%perl>
if ( $view eq "Rights" || $view eq "All" ){
    my @arights = AccessRight->search(object_class=>'Device', object_id=>"$o");
    my @urights  = map { $_->user_rights  } @arights;
    my @grights  = map { $_->group_rights } @arights;
    my @all_rights = ( @urights, @grights);
</%perl>

% if ( $manager && $manager->can($user, "access_section", 'device.html:edit_rights') ){
      <div class="container">
      <div class="containerheadleft">Access Rights</div>
      <div class="containerheadright">
      &nbsp;
%     if ( $edit ne 'edit_rights' ){
%         if ( $manager && $manager->can($user, 'edit', $o) ){
%             if ( @all_rights ){
                  <a href="device.html?id=<% $o %>&view=Rights&edit=edit_rights">[edit]</a>
%             }
              <a href="device.html?id=<% $o %>&view=Rights&_action=VIEW_ADD_RIGHTS">[add]</a>
%	  }
%     }
      </div>
      <div class="containerbody">
<%perl>
if ( $_action eq 'VIEW_ADD_RIGHTS' ){
    my $user_type = $user->getAttribute('USER_TYPE');
    if ( $user_type eq "Admin" ) {
	$m->comp('/generic/access_right_form.html', showheader=>1, 
		 object_class=>'Device', object_id=>$o->id, 
		 return_comp=>$m->current_comp->path, return_id=>$o->id);
    }else {
	$m->comp('/generic/error.mhtml', error=>"You don't have permission to manage rights!");
    }
}elsif ( $edit eq 'edit_rights' ){
    $m->comp('/generic/sortresults.mhtml', object=>\@urights, withedit=>1, return_args=>"?id=$o");
    $m->comp('/generic/sortresults.mhtml', object=>\@grights, withedit=>1, return_args=>"?id=$o");
}elsif ( @all_rights ) {
    $m->comp('/management/list_rights.mhtml', rights=>\@all_rights, show_object=>0);
}
</%perl>
      </div>
      </div>
    
%  }
% }


  </div> <!-- close containerbodyoutside --> 
 </div> <!-- close containeroutside -->
</div> <!-- close sectiondetail -->

